board.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-10-26 huanghe first commit
  11. * 2022-10-26 zhugengyu support aarch64
  12. * 2023-04-13 zhugengyu support RT-Smart
  13. *
  14. */
  15. #include "rtconfig.h"
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <mmu.h>
  19. #include <mm_aspace.h> /* TODO: why need application space when RT_SMART off */
  20. #include <mm_page.h>
  21. #ifdef RT_USING_SMART
  22. #include <page.h>
  23. #include <lwp_arch.h>
  24. #endif
  25. #include <gicv3.h>
  26. #if defined(TARGET_ARMV8_AARCH64)
  27. #include <psci.h>
  28. #include <gtimer.h>
  29. #include <cpuport.h>
  30. #else
  31. #include "fgeneric_timer.h" /* for aarch32 */
  32. #endif
  33. #include <interrupt.h>
  34. #include <board.h>
  35. #include "fdebug.h"
  36. #include "fprintk.h"
  37. #include "fearly_uart.h"
  38. #include "fcpu_info.h"
  39. #define LOG_DEBUG_TAG "BOARD"
  40. #define BSP_LOG_ERROR(format, ...) FT_DEBUG_PRINT_E(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  41. #define BSP_LOG_WARN(format, ...) FT_DEBUG_PRINT_W(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  42. #define BSP_LOG_INFO(format, ...) FT_DEBUG_PRINT_I(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  43. #define BSP_LOG_DEBUG(format, ...) FT_DEBUG_PRINT_D(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  44. /* mmu config */
  45. extern struct mem_desc platform_mem_desc[];
  46. extern const rt_uint32_t platform_mem_desc_size;
  47. void idle_wfi(void)
  48. {
  49. asm volatile("wfi");
  50. }
  51. /**
  52. * This function will initialize board
  53. */
  54. extern size_t MMUTable[];
  55. rt_region_t init_page_region = {
  56. PAGE_START,
  57. PAGE_END
  58. };
  59. #if defined(TARGET_ARMV8_AARCH64) /* AARCH64 */
  60. /* aarch64 use kernel gtimer */
  61. #else /* AARCH32 */
  62. /* aarch32 implment gtimer by bsp */
  63. static rt_uint32_t timer_step;
  64. void rt_hw_timer_isr(int vector, void *parameter)
  65. {
  66. GenericTimerCompare(timer_step);
  67. rt_tick_increase();
  68. }
  69. int rt_hw_timer_init(void)
  70. {
  71. rt_hw_interrupt_install(GENERIC_TIMER_NS_IRQ_NUM, rt_hw_timer_isr, RT_NULL, "tick");
  72. rt_hw_interrupt_umask(GENERIC_TIMER_NS_IRQ_NUM);
  73. timer_step = GenericTimerFrequecy();
  74. timer_step /= RT_TICK_PER_SECOND;
  75. GenericTimerCompare(timer_step);
  76. GenericTimerInterruptEnable();
  77. GenericTimerStart();
  78. return 0;
  79. }
  80. INIT_BOARD_EXPORT(rt_hw_timer_init);
  81. #endif
  82. #ifdef RT_USING_SMP
  83. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler);
  84. #endif
  85. #if defined(TARGET_ARMV8_AARCH64)
  86. void rt_hw_board_aarch64_init(void)
  87. {
  88. /* AARCH64 */
  89. #if defined(RT_USING_SMART)
  90. /* 1. init rt_kernel_space table (aspace.start = KERNEL_VADDR_START , aspace.size = ), 2. init io map range (rt_ioremap_start \ rt_ioremap_size) 3. */
  91. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xfffffffff0000000, 0x10000000, MMUTable, PV_OFFSET);
  92. #else
  93. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x80000000, 0x10000000, MMUTable, 0);
  94. #endif
  95. rt_page_init(init_page_region);
  96. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  97. /* init memory pool */
  98. #ifdef RT_USING_HEAP
  99. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  100. #endif
  101. rt_hw_interrupt_init();
  102. rt_hw_gtimer_init();
  103. /* compoent init */
  104. #ifdef RT_USING_COMPONENTS_INIT
  105. rt_components_board_init();
  106. #endif
  107. /* shell init */
  108. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  109. /* set console device */
  110. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  111. #endif
  112. rt_thread_idle_sethook(idle_wfi);
  113. #ifdef RT_USING_SMP
  114. /* install IPI handle */
  115. rt_hw_interrupt_set_priority(RT_SCHEDULE_IPI, 16);
  116. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  117. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  118. #endif
  119. }
  120. #else
  121. void rt_hw_board_aarch32_init(void)
  122. {
  123. #if defined(RT_USING_SMART)
  124. /* set io map range is 0xf0000000 ~ 0x10000000 , Memory Protection start address is 0xf0000000 - rt_mpr_size */
  125. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xf0000000, 0x10000000, MMUTable, PV_OFFSET);
  126. rt_page_init(init_page_region);
  127. /* rt_kernel_space 在start_gcc.S 中被初始化,此函数将iomap 空间放置在kernel space 上 */
  128. rt_hw_mmu_ioremap_init(&rt_kernel_space, (void*)0xf0000000, 0x10000000);
  129. /* */
  130. arch_kuser_init(&rt_kernel_space, (void*)0xffff0000);
  131. #else
  132. /*
  133. map kernel space memory (totally 1GB = 0x10000000), pv_offset = 0 if not RT_SMART:
  134. 0x80000000 ~ 0x80100000: kernel stack
  135. 0x80100000 ~ __bss_end: kernel code and data
  136. */
  137. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x80000000, 0x10000000, MMUTable, 0);
  138. rt_hw_mmu_ioremap_init(&rt_kernel_space, (void*)0x80000000, 0x10000000);
  139. #endif
  140. /* init memory pool */
  141. #ifdef RT_USING_HEAP
  142. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  143. #endif
  144. extern int rt_hw_cpu_id(void);
  145. u32 cpu_id, cpu_offset = 0;
  146. GetCpuId(&cpu_id);
  147. #if defined(FT_GIC_REDISTRUBUTIOR_OFFSET)
  148. cpu_offset = FT_GIC_REDISTRUBUTIOR_OFFSET ;
  149. #endif
  150. rt_uint32_t redist_addr = 0;
  151. #if defined(RT_USING_SMART)
  152. redist_addr = (uint32_t)rt_ioremap(GICV3_RD_BASE_ADDR, 4 * 128*1024);
  153. #else
  154. redist_addr = GICV3_RD_BASE_ADDR;
  155. #endif
  156. arm_gic_redist_address_set(0, redist_addr + (cpu_id + cpu_offset) * GICV3_RD_OFFSET, rt_hw_cpu_id());
  157. #if defined(TARGET_E2000Q)
  158. #if RT_CPUS_NR == 2
  159. arm_gic_redist_address_set(0, redist_addr + 3 * GICV3_RD_OFFSET, 1);
  160. #elif RT_CPUS_NR == 3
  161. arm_gic_redist_address_set(0, redist_addr + 3 * GICV3_RD_OFFSET, 1);
  162. arm_gic_redist_address_set(0, redist_addr, 2);
  163. #elif RT_CPUS_NR == 4
  164. arm_gic_redist_address_set(0, redist_addr + 3 * GICV3_RD_OFFSET, 1);
  165. arm_gic_redist_address_set(0, redist_addr, 2);
  166. arm_gic_redist_address_set(0, redist_addr + GICV3_RD_OFFSET, 3);
  167. #endif
  168. #else
  169. #if RT_CPUS_NR == 2
  170. arm_gic_redist_address_set(0, redist_addr + (1 + cpu_offset) * GICV3_RD_OFFSET, 1);
  171. #elif RT_CPUS_NR == 3
  172. arm_gic_redist_address_set(0, redist_addr + (1 + cpu_offset) * GICV3_RD_OFFSET, 1);
  173. arm_gic_redist_address_set(0, redist_addr + (2 + cpu_offset) * GICV3_RD_OFFSET, 2);
  174. #elif RT_CPUS_NR == 4
  175. arm_gic_redist_address_set(0, redist_addr + (1 + cpu_offset) * GICV3_RD_OFFSET, 1);
  176. arm_gic_redist_address_set(0, redist_addr + (2 + cpu_offset) * GICV3_RD_OFFSET, 2);
  177. arm_gic_redist_address_set(0, redist_addr + (3 + cpu_offset) * GICV3_RD_OFFSET, 3);
  178. #endif
  179. #endif
  180. rt_hw_interrupt_init();
  181. /* compoent init */
  182. #ifdef RT_USING_COMPONENTS_INIT
  183. rt_components_board_init();
  184. #endif
  185. /* shell init */
  186. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  187. /* set console device */
  188. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  189. #endif
  190. rt_thread_idle_sethook(idle_wfi);
  191. #ifdef RT_USING_SMP
  192. /* install IPI handle */
  193. rt_hw_interrupt_set_priority(RT_SCHEDULE_IPI, 16);
  194. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  195. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  196. #endif
  197. }
  198. #endif
  199. /**
  200. * This function will initialize hardware board
  201. */
  202. void rt_hw_board_init(void)
  203. {
  204. #if defined(TARGET_ARMV8_AARCH64)
  205. rt_hw_board_aarch64_init();
  206. #else
  207. rt_hw_board_aarch32_init();
  208. #endif
  209. }