board.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2006-2019, 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. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #include "drv_timer.h"
  15. #include "cp15.h"
  16. #ifdef RT_USING_SMP
  17. extern void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler);
  18. void ipi_handler(){
  19. rt_scheduler_ipi_handler(0,RT_NULL);
  20. }
  21. #endif
  22. void rt_hw_timer_isr(int vector, void *parameter)
  23. {
  24. ARM_TIMER_IRQCLR = 0;
  25. rt_tick_increase();
  26. }
  27. int rt_hw_timer_init()
  28. {
  29. __DSB();
  30. rt_hw_interrupt_install(IRQ_ARM_TIMER, rt_hw_timer_isr, RT_NULL, "tick");
  31. rt_hw_interrupt_umask(IRQ_ARM_TIMER);
  32. /* timer_clock = apb_clock/(pre_divider + 1) */
  33. ARM_TIMER_PREDIV = (250 - 1);
  34. ARM_TIMER_RELOAD = 0;
  35. ARM_TIMER_LOAD = 0;
  36. ARM_TIMER_IRQCLR = 0;
  37. ARM_TIMER_CTRL = 0;
  38. ARM_TIMER_RELOAD = 10000;
  39. ARM_TIMER_LOAD = 10000;
  40. /* 23-bit counter, enable interrupt, enable timer */
  41. ARM_TIMER_CTRL = (1 << 1) | (1 << 5) | (1 << 7);
  42. return 0;
  43. }
  44. void idle_wfi(void)
  45. {
  46. asm volatile ("wfi");
  47. }
  48. #define MMU_LEVEL_MASK 0x1ffUL
  49. #define MMU_MAP_ERROR_VANOTALIGN -1
  50. #define MMU_MAP_ERROR_PANOTALIGN -2
  51. #define MMU_MAP_ERROR_NOPAGE -3
  52. #define MMU_MAP_ERROR_CONFLICT -4
  53. unsigned char main_tbl[4096] __attribute__((aligned (4096)));
  54. unsigned char __page_start[4096*100] __attribute__((aligned (4096)));
  55. unsigned long __page_off = 0;
  56. unsigned long get_free_page(void) {
  57. __page_off += 4096;
  58. return (unsigned long)(__page_start + __page_off - 4096);
  59. }
  60. #define MEM_ATTR_MEM ((0x1UL << 10) | (0x2UL << 8) | (0x0UL << 6) | (0x1UL << 2))
  61. #define MEM_ATTR_IO ((0x1UL << 10) | (0x2UL << 8) | (0x0UL << 6) | (0x2UL << 2))
  62. static int map_single_page_2M(unsigned long* lv0_tbl, unsigned long va, unsigned long pa, unsigned long attr) {
  63. int level;
  64. unsigned long* cur_lv_tbl = lv0_tbl;
  65. unsigned long page;
  66. unsigned long off;
  67. int level_shift = 39;
  68. if (va & (0x200000UL - 1)) {
  69. return MMU_MAP_ERROR_VANOTALIGN;
  70. }
  71. if (pa & (0x200000UL - 1)) {
  72. return MMU_MAP_ERROR_PANOTALIGN;
  73. }
  74. for (level = 0; level < 2; level++) {
  75. off = (va >> level_shift);
  76. off &= MMU_LEVEL_MASK;
  77. if ((cur_lv_tbl[off] & 1) == 0) {
  78. page = get_free_page();
  79. if (!page) {
  80. return MMU_MAP_ERROR_NOPAGE;
  81. }
  82. rt_memset((void *)page, 0, 4096);
  83. cur_lv_tbl[off] = page | 0x3UL;
  84. }
  85. page = cur_lv_tbl[off];
  86. if (!(page & 0x2)) {
  87. //is block! error!
  88. return MMU_MAP_ERROR_CONFLICT;
  89. }
  90. cur_lv_tbl = (unsigned long*)(page & 0x0000fffffffff000UL);
  91. level_shift -= 9;
  92. }
  93. attr &= 0xfff0000000000ffcUL;
  94. pa |= (attr | 0x1UL); //block
  95. off = (va >> 21);
  96. off &= MMU_LEVEL_MASK;
  97. cur_lv_tbl[off] = pa;
  98. return 0;
  99. }
  100. int armv8_map_2M(unsigned long* lv0_tbl, unsigned long va, unsigned long pa, int count, unsigned long attr)
  101. {
  102. int i;
  103. int ret;
  104. if (va & (0x200000 - 1))
  105. {
  106. return -1;
  107. }
  108. if (pa & (0x200000 - 1))
  109. {
  110. return -1;
  111. }
  112. for (i = 0; i < count; i++)
  113. {
  114. ret = map_single_page_2M(lv0_tbl, va, pa, attr);
  115. va += 0x200000;
  116. pa += 0x200000;
  117. if (ret != 0)
  118. {
  119. return ret;
  120. }
  121. }
  122. return 0;
  123. }
  124. /**
  125. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  126. * after interrupt disabled.
  127. */
  128. void rt_hw_board_init(void)
  129. {
  130. /* mmu set */
  131. unsigned long val64;
  132. unsigned long val32; //val32不是uint32_t,val32只是表示相关的那个寄存器是32位的
  133. int ret;
  134. val64 = 0x007f6eUL;
  135. asm volatile("msr MAIR_EL1, %0\n dsb sy\n"::"r"(val64));
  136. asm volatile("mrs %0, MAIR_EL1\n dsb sy\n":"=r"(val64));
  137. //TCR_EL1
  138. val32 = (16UL << 0)
  139. | (0x0UL << 6)
  140. | (0x0UL << 7)
  141. | (0x3UL << 8)
  142. | (0x3UL << 10)
  143. | (0x2UL << 12)
  144. | (0x0UL << 14)
  145. | (0x0UL << 16)
  146. | (0x0UL << 22)
  147. | (0x1UL << 23)
  148. | (0x2UL << 30)
  149. | (0x1UL << 32)
  150. | (0x0UL << 35)
  151. | (0x0UL << 36)
  152. | (0x0UL << 37)
  153. | (0x0UL << 38);
  154. asm volatile("msr TCR_EL1, %0\n"::"r"(val32));
  155. asm volatile("mrs %0, TCR_EL1\n":"=r"(val32));
  156. asm volatile("msr TTBR0_EL1, %0\n dsb sy\n"::"r"(main_tbl));
  157. asm volatile("mrs %0, TTBR0_EL1\n dsb sy\n":"=r"(val64));
  158. rt_memset(main_tbl, 0, 4096);
  159. ret = armv8_map_2M((unsigned long *)main_tbl, 0x0, 0x0, 32, MEM_ATTR_MEM); //32*2M = 64M
  160. if (ret)
  161. {
  162. goto skip_mmu;
  163. }
  164. ret = armv8_map_2M((unsigned long *)main_tbl, 0x3f000000, 0x3f000000, 8, MEM_ATTR_IO); //8*2M = 16M
  165. if (ret)
  166. {
  167. goto skip_mmu;
  168. }
  169. //关闭指令cache
  170. __asm__ volatile("mrs %0, SCTLR_EL1\n":"=r"(val64));
  171. val64 &= ~0x1000; //disable I
  172. __asm__ volatile("dmb sy\n msr SCTLR_EL1, %0\n isb sy\n"::"r"(val64));
  173. //清除指令cache
  174. __asm__ volatile("IC IALLUIS\n dsb sy\n isb sy\n");
  175. //清除tlb
  176. __asm__ volatile("tlbi vmalle1\n dsb sy\n isb sy\n");
  177. //SCTLR_EL1, turn on mmu
  178. asm volatile("mrs %0, SCTLR_EL1\n":"=r"(val32));
  179. val32 |= 0x1005; //enable mmu, I C M
  180. asm volatile("dmb sy\n msr SCTLR_EL1, %0\nisb sy\n"::"r"(val32));
  181. skip_mmu:
  182. /* initialize hardware interrupt */
  183. rt_hw_interrupt_init(); // in libcpu/interrupt.c. Set some data structures, no operation on device
  184. rt_hw_vector_init(); // in libcpu/interrupt.c. == rt_cpu_vector_set_base((rt_ubase_t)&system_vectors);
  185. /* initialize uart */
  186. rt_hw_uart_init(); // driver/drv_uart.c
  187. /* initialize timer for os tick */
  188. rt_hw_timer_init();
  189. rt_thread_idle_sethook(idle_wfi);
  190. #ifdef RT_USING_CONSOLE
  191. /* set console device */
  192. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  193. #endif /* RT_USING_CONSOLE */
  194. #ifdef RT_USING_HEAP
  195. /* initialize memory system */
  196. rt_kprintf("heap: 0x%08x - 0x%08x\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  197. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  198. #endif
  199. #ifdef RT_USING_COMPONENTS_INIT
  200. rt_components_board_init();
  201. #endif
  202. rt_kprintf("__page_off = %x\n", __page_off);
  203. }
  204. #ifdef RT_USING_SMP
  205. void _reset(void);
  206. void secondary_cpu_start(void);
  207. void rt_hw_secondary_cpu_up(void)
  208. {
  209. int i;
  210. int retry,val;
  211. rt_cpu_dcache_clean_flush();
  212. rt_cpu_icache_flush();
  213. /*TODO maybe, there is some bug */
  214. for(i=RT_CPUS_NR-1; i>0; i-- )
  215. {
  216. rt_kprintf("boot cpu:%d\n", i);
  217. setup_bootstrap_addr(i, (int)_reset);
  218. __SEV();
  219. __DSB();
  220. __ISB();
  221. retry = 10;
  222. rt_thread_delay(RT_TICK_PER_SECOND/1000);
  223. do
  224. {
  225. val = CORE_MAILBOX3_CLEAR(i);
  226. if (val == 0)
  227. {
  228. rt_kprintf("start OK: CPU %d \n",i);
  229. break;
  230. }
  231. rt_thread_delay(RT_TICK_PER_SECOND);
  232. retry --;
  233. if (retry <= 0)
  234. {
  235. rt_kprintf("can't start for CPU %d \n",i);
  236. break;
  237. }
  238. }while (1);
  239. }
  240. __DSB();
  241. __SEV();
  242. }
  243. void secondary_cpu_c_start(void)
  244. {
  245. uint32_t id;
  246. id = rt_hw_cpu_id();
  247. rt_kprintf("cpu = 0x%08x\n",id);
  248. rt_hw_timer_init();
  249. rt_kprintf("cpu %d startup.\n",id);
  250. rt_hw_vector_init();
  251. enable_cpu_ipi_intr(id);
  252. rt_hw_spin_lock(&_cpus_lock);
  253. rt_system_scheduler_start();
  254. }
  255. void rt_hw_secondary_cpu_idle_exec(void)
  256. {
  257. __WFE();
  258. }
  259. #endif