board.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-07-29 zdzn first version
  9. * 2021-12-28 GuEe-GUI add smp support
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. #include "drv_timer.h"
  16. #include "gtimer.h"
  17. #include "cpuport.h"
  18. #include "interrupt.h"
  19. #include "mmu.h"
  20. #include "raspi.h"
  21. struct mem_desc platform_mem_desc[] =
  22. {
  23. {0, 0x6400000, 0, NORMAL_MEM},
  24. {0xc00000, 0xc01000, 0xc00000, DEVICE_MEM}, /* mbox */
  25. {0x3f000000, 0x3f200000, 0x3f000000, DEVICE_MEM}, /* timer */
  26. {0x3f200000, 0x3f216000, 0x3f200000, DEVICE_MEM}, /* uart */
  27. {0x40000000, 0x40200000, 0x40000000, DEVICE_MEM}, /* core timer */
  28. {0x3F300000, 0x3F301000, 0x3F300000, DEVICE_MEM}, /* sdio */
  29. {0x3f804000, 0x3f805000, 0x3f804000, DEVICE_MEM}, /* i2c0 */
  30. {0x3f205000, 0x3f206000, 0x3f205000, DEVICE_MEM}, /* i2c1 */
  31. };
  32. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc)/sizeof(platform_mem_desc[0]);
  33. #if defined(BSP_USING_CORETIMER) || defined(RT_USING_SMP)
  34. static volatile rt_uint64_t timer_step;
  35. #define BSP_USING_CORETIMER
  36. #endif
  37. void rt_hw_timer_isr(int vector, void *parameter)
  38. {
  39. #ifdef BSP_USING_CORETIMER
  40. rt_hw_set_gtimer_val(timer_step);
  41. #else
  42. ARM_TIMER_IRQCLR = 0;
  43. #endif
  44. rt_tick_increase();
  45. }
  46. rt_uint8_t core_timer_flag;
  47. void rt_hw_timer_init(void)
  48. {
  49. rt_hw_interrupt_install(IRQ_ARM_TIMER, rt_hw_timer_isr, RT_NULL, "tick");
  50. rt_hw_interrupt_umask(IRQ_ARM_TIMER);
  51. #ifdef BSP_USING_CORETIMER
  52. rt_hw_isb();
  53. timer_step = rt_hw_get_gtimer_frq();
  54. rt_hw_dsb();
  55. timer_step /= RT_TICK_PER_SECOND;
  56. rt_hw_gtimer_enable();
  57. rt_hw_set_gtimer_val(timer_step);
  58. #ifdef RT_USING_SMP
  59. core_timer_enable(rt_hw_cpu_id());
  60. #else
  61. core_timer_enable(0);
  62. #endif
  63. #else
  64. __DSB();
  65. /* timer_clock = apb_clock/(pre_divider + 1) */
  66. ARM_TIMER_PREDIV = (250 - 1);
  67. ARM_TIMER_RELOAD = 0;
  68. ARM_TIMER_LOAD = 0;
  69. ARM_TIMER_IRQCLR = 0;
  70. ARM_TIMER_CTRL = 0;
  71. ARM_TIMER_RELOAD = 10000;
  72. ARM_TIMER_LOAD = 10000;
  73. /* 23-bit counter, enable interrupt, enable timer */
  74. ARM_TIMER_CTRL = (1 << 1) | (1 << 5) | (1 << 7);
  75. #endif
  76. }
  77. void idle_wfi(void)
  78. {
  79. asm volatile ("wfi");
  80. }
  81. /**
  82. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  83. * after interrupt disabled.
  84. */
  85. void rt_hw_board_init(void)
  86. {
  87. extern void *MMUTable;
  88. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x80000000, 0x10000000, MMUTable, 0);
  89. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  90. /* initialize hardware interrupt */
  91. rt_hw_interrupt_init(); // in libcpu/interrupt.c. Set some data structures, no operation on device
  92. /* initialize uart */
  93. rt_hw_uart_init(); // driver/drv_uart.c
  94. /* initialize timer for os tick */
  95. rt_hw_timer_init();
  96. rt_thread_idle_sethook(idle_wfi);
  97. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  98. /* set console device */
  99. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  100. #endif
  101. #ifdef RT_USING_HEAP
  102. /* initialize memory system */
  103. rt_kprintf("heap: 0x%08x - 0x%08x\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  104. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  105. #endif
  106. #ifdef RT_USING_COMPONENTS_INIT
  107. rt_components_board_init();
  108. #endif
  109. #ifdef RT_USING_SMP
  110. /* install IPI handle */
  111. rt_hw_ipi_handler_install(IRQ_ARM_MAILBOX, rt_scheduler_ipi_handler);
  112. rt_hw_interrupt_umask(IRQ_ARM_MAILBOX);
  113. enable_cpu_ipi_intr(0);
  114. #endif
  115. }
  116. #ifdef RT_USING_SMP
  117. static unsigned long cpu_release_paddr[] =
  118. {
  119. [0] = 0xd8,
  120. [1] = 0xe0,
  121. [2] = 0xe8,
  122. [3] = 0xf0,
  123. [4] = 0
  124. };
  125. void rt_hw_secondary_cpu_up(void)
  126. {
  127. int i;
  128. extern void secondary_cpu_start(void);
  129. for (i = 1; i < RT_CPUS_NR && cpu_release_paddr[i]; ++i)
  130. {
  131. __asm__ volatile ("str %0, [%1]"::"rZ"((unsigned long)secondary_cpu_start), "r"(cpu_release_paddr[i]));
  132. rt_hw_dcache_flush_range(cpu_release_paddr[i], sizeof(cpu_release_paddr[i]));
  133. __DSB();
  134. __SEV();
  135. }
  136. }
  137. void secondary_cpu_c_start(void)
  138. {
  139. int id = rt_hw_cpu_id();
  140. rt_hw_mmu_init();
  141. rt_hw_spin_lock(&_cpus_lock);
  142. rt_hw_vector_init();
  143. rt_hw_timer_init();
  144. enable_cpu_ipi_intr(id);
  145. rt_kprintf("\rcall cpu %d on success\n", id);
  146. rt_system_scheduler_start();
  147. }
  148. void rt_hw_secondary_cpu_idle_exec(void)
  149. {
  150. __WFE();
  151. }
  152. #endif