board.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2006-2021, 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. * 2023-07-27 zhugengyu update aarch32 gtimer usage
  14. *
  15. */
  16. #include "rtconfig.h"
  17. #include <rthw.h>
  18. #include <rtthread.h>
  19. #include <phytium_cpu.h>
  20. #include <mmu.h>
  21. #include <mm_aspace.h> /* TODO: why need application space when RT_SMART off */
  22. #include <mm_page.h>
  23. #include "phytium_interrupt.h"
  24. #ifdef RT_USING_SMART
  25. #include <page.h>
  26. #include <lwp_arch.h>
  27. #endif
  28. #include <gicv3.h>
  29. #if defined(TARGET_ARMV8_AARCH64)
  30. #include <psci.h>
  31. #include <gtimer.h>
  32. #include <cpuport.h>
  33. #else
  34. #include <gtimer.h>
  35. #endif
  36. #include <interrupt.h>
  37. #include <board.h>
  38. #include "fearly_uart.h"
  39. #include "fcpu_info.h"
  40. #include "fiopad.h"
  41. #ifdef RT_USING_SMP
  42. #include "fpsci.h"
  43. #endif
  44. extern FIOPadCtrl iopad_ctrl;
  45. /* mmu config */
  46. extern struct mem_desc platform_mem_desc[];
  47. extern const rt_uint32_t platform_mem_desc_size;
  48. rt_uint64_t rt_cpu_mpidr_table[RT_CPUS_NR];
  49. void idle_wfi(void)
  50. {
  51. asm volatile("wfi");
  52. }
  53. /**
  54. * This function will initialize board
  55. */
  56. extern size_t MMUTable[];
  57. rt_region_t init_page_region =
  58. {
  59. PAGE_START,
  60. PAGE_END
  61. };
  62. void FIOMuxInit(void)
  63. {
  64. FIOPadCfgInitialize(&iopad_ctrl, FIOPadLookupConfig(FIOPAD0_ID));
  65. #ifdef RT_USING_SMART
  66. iopad_ctrl.config.base_address = (uintptr)rt_ioremap((void *)iopad_ctrl.config.base_address, 0x2000);
  67. #endif
  68. return;
  69. }
  70. #if defined(TARGET_ARMV8_AARCH64) /* AARCH64 */
  71. /* aarch64 use kernel gtimer */
  72. #else /* AARCH32 */
  73. /* aarch32 implment gtimer by bsp */
  74. static rt_uint32_t timer_step;
  75. #define CNTP_CTL_ENABLE (1U << 0) /* Enables the timer */
  76. #define CNTP_CTL_IMASK (1U << 1) /* Timer interrupt mask bit */
  77. #define CNTP_CTL_ISTATUS (1U << 2) /* The status of the timer */
  78. void GenericTimerInterruptEnable(u32 id)
  79. {
  80. u64 ctrl = gtimer_get_control();
  81. if (ctrl & CNTP_CTL_IMASK)
  82. {
  83. ctrl &= ~CNTP_CTL_IMASK;
  84. gtimer_set_control(ctrl);
  85. }
  86. }
  87. void GenericTimerStart(u32 id)
  88. {
  89. u32 ctrl = gtimer_get_control(); /* get CNTP_CTL */
  90. if (!(ctrl & CNTP_CTL_ENABLE))
  91. {
  92. ctrl |= CNTP_CTL_ENABLE; /* enable gtimer if off */
  93. gtimer_set_control(ctrl); /* set CNTP_CTL */
  94. }
  95. }
  96. void rt_hw_timer_isr(int vector, void *parameter)
  97. {
  98. gtimer_set_load_value(timer_step);
  99. rt_tick_increase();
  100. }
  101. int rt_hw_timer_init(void)
  102. {
  103. rt_hw_interrupt_install(GENERIC_TIMER_NS_IRQ_NUM, rt_hw_timer_isr, RT_NULL, "tick");
  104. rt_hw_interrupt_umask(GENERIC_TIMER_NS_IRQ_NUM);
  105. timer_step = gtimer_get_counter_frequency();
  106. FASSERT_MSG((timer_step > 1000000), "invalid freqency %ud", timer_step);
  107. timer_step /= RT_TICK_PER_SECOND;
  108. gtimer_set_load_value(timer_step);
  109. GenericTimerInterruptEnable(GENERIC_TIMER_ID0);
  110. GenericTimerStart(GENERIC_TIMER_ID0);
  111. return 0;
  112. }
  113. INIT_BOARD_EXPORT(rt_hw_timer_init);
  114. #endif
  115. #ifdef RT_USING_SMP
  116. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler);
  117. #endif
  118. #if defined(TARGET_ARMV8_AARCH64)
  119. void rt_hw_board_aarch64_init(void)
  120. {
  121. /* AARCH64 */
  122. #if defined(RT_USING_SMART)
  123. /* 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. */
  124. rt_hw_mmu_map_init(&rt_kernel_space, (void *)0xfffffffff0000000, 0x10000000, MMUTable, PV_OFFSET);
  125. #else
  126. rt_hw_mmu_map_init(&rt_kernel_space, (void *)0xffffd0000000, 0x10000000, MMUTable, 0);
  127. #endif
  128. rt_page_init(init_page_region);
  129. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  130. /* init memory pool */
  131. #ifdef RT_USING_HEAP
  132. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  133. #endif
  134. phytium_interrupt_init();
  135. rt_hw_gtimer_init();
  136. FEarlyUartProbe();
  137. FIOMuxInit();
  138. /* compoent init */
  139. #ifdef RT_USING_COMPONENTS_INIT
  140. rt_components_board_init();
  141. #endif
  142. /* shell init */
  143. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  144. /* set console device */
  145. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  146. #endif
  147. rt_thread_idle_sethook(idle_wfi);
  148. #ifdef RT_USING_SMP
  149. FPsciInit();
  150. /* install IPI handle */
  151. rt_hw_interrupt_set_priority(RT_SCHEDULE_IPI, 16);
  152. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  153. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  154. #endif
  155. }
  156. #else
  157. #if defined(TARGET_E2000D)
  158. #define FT_GIC_REDISTRUBUTIOR_OFFSET 2
  159. #endif
  160. void rt_hw_board_aarch32_init(void)
  161. {
  162. #if defined(RT_USING_SMART)
  163. rt_uint32_t mmutable_p = 0;
  164. /* set io map range is 0xf0000000 ~ 0x10000000 , Memory Protection start address is 0xf0000000 - rt_mpr_size */
  165. rt_hw_mmu_map_init(&rt_kernel_space, (void *)0xf0000000, 0x10000000, MMUTable, PV_OFFSET);
  166. rt_hw_init_mmu_table(platform_mem_desc,platform_mem_desc_size) ;
  167. mmutable_p = (rt_uint32_t)MMUTable + (rt_uint32_t)PV_OFFSET ;
  168. rt_hw_mmu_switch((void*)mmutable_p) ;
  169. rt_page_init(init_page_region);
  170. /* rt_kernel_space 在start_gcc.S 中被初始化,此函数将iomap 空间放置在kernel space 上 */
  171. rt_hw_mmu_ioremap_init(&rt_kernel_space, (void *)0xf0000000, 0x10000000);
  172. arch_kuser_init(&rt_kernel_space, (void *)0xffff0000);
  173. #else
  174. rt_hw_mmu_map_init(&rt_kernel_space, (void *)0x80000000, 0x10000000, MMUTable, 0);
  175. rt_hw_init_mmu_table(platform_mem_desc,platform_mem_desc_size) ;
  176. rt_hw_mmu_init();
  177. rt_hw_mmu_ioremap_init(&rt_kernel_space, (void *)0x80000000, 0x10000000);
  178. #endif
  179. /* init memory pool */
  180. #ifdef RT_USING_HEAP
  181. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  182. #endif
  183. extern int rt_hw_cpu_id(void);
  184. u32 cpu_id, cpu_offset = 0;
  185. GetCpuId(&cpu_id);
  186. #if defined(FT_GIC_REDISTRUBUTIOR_OFFSET)
  187. cpu_offset = FT_GIC_REDISTRUBUTIOR_OFFSET ;
  188. #endif
  189. FEarlyUartProbe();
  190. FIOMuxInit();
  191. arm_gic_redist_address_set(0, platform_get_gic_redist_base(), rt_hw_cpu_id());
  192. rt_hw_interrupt_init();
  193. /* compoent init */
  194. #ifdef RT_USING_COMPONENTS_INIT
  195. rt_components_board_init();
  196. #endif
  197. /* shell init */
  198. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  199. /* set console device */
  200. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  201. #endif
  202. rt_thread_idle_sethook(idle_wfi);
  203. #ifdef RT_USING_SMP
  204. FPsciInit();
  205. /* install IPI handle */
  206. rt_hw_interrupt_set_priority(RT_SCHEDULE_IPI, 16);
  207. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  208. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  209. #endif
  210. }
  211. #endif
  212. /**
  213. * This function will initialize hardware board
  214. */
  215. void rt_hw_board_init(void)
  216. {
  217. #if defined(TARGET_ARMV8_AARCH64)
  218. rt_hw_board_aarch64_init();
  219. #else
  220. rt_hw_board_aarch32_init();
  221. #endif
  222. }