board.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. *
  13. */
  14. #include "rtconfig.h"
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include <mmu.h>
  18. #include <gicv3.h>
  19. #if defined(TARGET_ARMV8_AARCH64)
  20. #include <psci.h>
  21. #include <gtimer.h>
  22. #include <cpuport.h>
  23. #else
  24. #include "fgeneric_timer.h" /* for aarch32 */
  25. #endif
  26. #include <interrupt.h>
  27. #include <board.h>
  28. #include "fdebug.h"
  29. #include "fprintk.h"
  30. #include "fearly_uart.h"
  31. #include "fcpu_info.h"
  32. #include "fpsci.h"
  33. #define LOG_DEBUG_TAG "BOARD"
  34. #define BSP_LOG_ERROR(format, ...) FT_DEBUG_PRINT_E(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  35. #define BSP_LOG_WARN(format, ...) FT_DEBUG_PRINT_W(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  36. #define BSP_LOG_INFO(format, ...) FT_DEBUG_PRINT_I(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  37. #define BSP_LOG_DEBUG(format, ...) FT_DEBUG_PRINT_D(LOG_DEBUG_TAG, format, ##__VA_ARGS__)
  38. /* mmu config */
  39. struct mem_desc platform_mem_desc[] =
  40. #if defined(TARGET_E2000)
  41. {
  42. {
  43. 0x00U,
  44. 0x00U + 0x40000000U,
  45. 0x00U,
  46. DEVICE_MEM
  47. },
  48. {
  49. 0x40000000U,
  50. 0x40000000U + 0x10000000U,
  51. 0x40000000U,
  52. DEVICE_MEM
  53. },
  54. {
  55. 0x50000000U,
  56. 0x50000000U + 0x30000000U,
  57. 0x50000000U,
  58. DEVICE_MEM
  59. },
  60. {
  61. 0x80000000U,
  62. 0xffffffffU,
  63. 0x80000000U,
  64. NORMAL_MEM
  65. },
  66. #if defined(TARGET_ARMV8_AARCH64)
  67. {
  68. 0x1000000000,
  69. 0x1000000000 + 0x1000000000,
  70. 0x1000000000,
  71. DEVICE_MEM
  72. },
  73. {
  74. 0x2000000000,
  75. 0x2000000000 + 0x2000000000,
  76. 0x2000000000,
  77. NORMAL_MEM
  78. },
  79. #endif
  80. };
  81. #elif defined(TARGET_F2000_4) || defined(TARGET_D2000)
  82. {
  83. {
  84. 0x80000000,
  85. 0xFFFFFFFF,
  86. 0x80000000,
  87. DDR_MEM
  88. },
  89. {
  90. 0, //< QSPI
  91. 0x1FFFFFFF,
  92. 0,
  93. DEVICE_MEM
  94. },
  95. {
  96. 0x20000000, //<! LPC
  97. 0x27FFFFFF,
  98. 0x20000000,
  99. DEVICE_MEM
  100. },
  101. {
  102. FT_DEV_BASE_ADDR, //<! Device register
  103. FT_DEV_END_ADDR,
  104. FT_DEV_BASE_ADDR,
  105. DEVICE_MEM
  106. },
  107. {
  108. 0x30000000, //<! debug
  109. 0x39FFFFFF,
  110. 0x30000000,
  111. DEVICE_MEM
  112. },
  113. {
  114. 0x3A000000, //<! Internal register space in the on-chip network
  115. 0x3AFFFFFF,
  116. 0x3A000000,
  117. DEVICE_MEM
  118. },
  119. {
  120. FT_PCI_CONFIG_BASEADDR,
  121. FT_PCI_CONFIG_BASEADDR + FT_PCI_CONFIG_REG_LENGTH,
  122. FT_PCI_CONFIG_BASEADDR,
  123. DEVICE_MEM
  124. },
  125. {
  126. FT_PCI_IO_CONFIG_BASEADDR,
  127. FT_PCI_IO_CONFIG_BASEADDR + FT_PCI_IO_CONFIG_REG_LENGTH,
  128. FT_PCI_IO_CONFIG_BASEADDR,
  129. DEVICE_MEM
  130. },
  131. {
  132. FT_PCI_MEM32_BASEADDR,
  133. FT_PCI_MEM32_BASEADDR + FT_PCI_MEM32_REG_LENGTH,
  134. FT_PCI_MEM32_BASEADDR,
  135. DEVICE_MEM
  136. }
  137. #if defined(TARGET_ARMV8_AARCH64)
  138. {
  139. 0x1000000000,
  140. 0x1000000000 + 0x1000000000,
  141. 0x1000000000,
  142. DEVICE_MEM
  143. },
  144. {
  145. 0x2000000000,
  146. 0x2000000000 + 0x2000000000,
  147. 0x2000000000,
  148. NORMAL_MEM
  149. },
  150. #endif
  151. };
  152. #endif
  153. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]);
  154. #if defined(TARGET_ARMV8_AARCH64) /* AARCH64 */
  155. /* aarch64 use kernel gtimer */
  156. void idle_wfi(void)
  157. {
  158. asm volatile("wfi");
  159. }
  160. #else /* AARCH32 */
  161. static rt_uint32_t timerStep;
  162. void rt_hw_timer_isr(int vector, void *parameter)
  163. {
  164. GenericTimerCompare(timerStep);
  165. rt_tick_increase();
  166. }
  167. int rt_hw_timer_init(void)
  168. {
  169. rt_hw_interrupt_install(GENERIC_TIMER_NS_IRQ_NUM, rt_hw_timer_isr, RT_NULL, "tick");
  170. rt_hw_interrupt_umask(GENERIC_TIMER_NS_IRQ_NUM);
  171. timerStep = GenericTimerFrequecy();
  172. timerStep /= RT_TICK_PER_SECOND;
  173. GenericTimerCompare(timerStep);
  174. GenericTimerInterruptEnable();
  175. GenericTimerStart();
  176. return 0;
  177. }
  178. INIT_BOARD_EXPORT(rt_hw_timer_init);
  179. #endif
  180. #ifdef RT_USING_SMP
  181. void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler);
  182. #endif
  183. /**
  184. * This function will initialize hardware board
  185. */
  186. void rt_hw_board_init(void)
  187. {
  188. /* mmu init */
  189. #if defined(TARGET_ARMV8_AARCH64)
  190. rt_hw_init_mmu_table(platform_mem_desc, platform_mem_desc_size);
  191. rt_hw_mmu_init();
  192. #endif
  193. /* interrupt init */
  194. #if defined(TARGET_ARMV8_AARCH64)
  195. f_printk("aarch64 interrupt init \r\n");
  196. #else
  197. f_printk("aarch32 interrupt init \r\n");
  198. extern int rt_hw_cpu_id(void);
  199. u32 cpu_id, cpu_offset = 0;
  200. GetCpuId(&cpu_id);
  201. f_printk("cpu_id is %d \r\n", cpu_id);
  202. #if defined(FT_GIC_REDISTRUBUTIOR_OFFSET)
  203. cpu_offset = FT_GIC_REDISTRUBUTIOR_OFFSET ;
  204. #endif
  205. f_printk("cpu_offset is %d \r\n", cpu_offset);
  206. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (cpu_id + cpu_offset) * GICV3_RD_OFFSET, rt_hw_cpu_id());
  207. #if defined(TARGET_E2000Q)
  208. #if RT_CPUS_NR == 2
  209. f_printk("arm_gic_redist_address_set is 2 \r\n");
  210. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + 3 * GICV3_RD_OFFSET, 1);
  211. #elif RT_CPUS_NR == 3
  212. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + 3 * GICV3_RD_OFFSET, 1);
  213. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS, 2);
  214. #elif RT_CPUS_NR == 4
  215. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + 3 * GICV3_RD_OFFSET, 1);
  216. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS, 2);
  217. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + GICV3_RD_OFFSET, 3);
  218. #endif
  219. #else
  220. #if RT_CPUS_NR == 2
  221. f_printk("arm_gic_redist_address_set is 2 \r\n");
  222. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (1 + cpu_offset) * GICV3_RD_OFFSET, 1);
  223. #elif RT_CPUS_NR == 3
  224. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (1 + cpu_offset) * GICV3_RD_OFFSET, 1);
  225. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (2 + cpu_offset) * GICV3_RD_OFFSET, 2);
  226. #elif RT_CPUS_NR == 4
  227. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (1 + cpu_offset) * GICV3_RD_OFFSET, 1);
  228. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (2 + cpu_offset) * GICV3_RD_OFFSET, 2);
  229. arm_gic_redist_address_set(0, GICV3_RD_BASEADDRESS + (3 + cpu_offset) * GICV3_RD_OFFSET, 3);
  230. #endif
  231. #endif
  232. #endif
  233. rt_hw_interrupt_init();
  234. /* gtimer init */
  235. #if defined(TARGET_ARMV8_AARCH64)
  236. rt_hw_gtimer_init();
  237. #endif
  238. /* compoent init */
  239. #ifdef RT_USING_COMPONENTS_INIT
  240. rt_components_board_init();
  241. #endif
  242. /* shell init */
  243. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  244. /* set console device */
  245. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  246. #endif
  247. /* init memory pool */
  248. #ifdef RT_USING_HEAP
  249. rt_system_heap_init(HEAP_BEGIN, HEAP_END);
  250. #endif
  251. #ifdef RT_USING_SMP
  252. /* install IPI handle */
  253. rt_hw_interrupt_set_priority(RT_SCHEDULE_IPI, 16);
  254. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  255. rt_hw_interrupt_umask(RT_SCHEDULE_IPI);
  256. #endif
  257. }
  258. static void ft_reset(void)
  259. {
  260. PsciCpuReset();
  261. }
  262. MSH_CMD_EXPORT_ALIAS(ft_reset, ft_reset, ft_reset);
  263. /*@}*/