board.c 6.8 KB

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