board.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-16 bigmagic first version
  9. * 2021-12-28 GuEe-GUI add smp support
  10. * 2023-03-28 WangXiaoyao sync works & memory layout fixups
  11. * code formats
  12. */
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include <mm_aspace.h>
  16. #include "board.h"
  17. #include "drv_uart.h"
  18. #include "cp15.h"
  19. #include "mmu.h"
  20. #include "mbox.h"
  21. #include <mm_page.h>
  22. #ifdef RT_USING_SMART
  23. #include <lwp_arch.h>
  24. #endif
  25. extern size_t MMUTable[];
  26. size_t gpio_base_addr = GPIO_BASE_ADDR;
  27. size_t uart_base_addr = UART_BASE;
  28. size_t gic_base_addr = GIC_V2_BASE;
  29. size_t arm_timer_base = ARM_TIMER_BASE;
  30. size_t pactl_cs_base = PACTL_CS_ADDR;
  31. size_t stimer_base_addr = STIMER_BASE;
  32. size_t mmc2_base_addr = MMC2_BASE_ADDR;
  33. size_t videocore_mbox = VIDEOCORE_MBOX;
  34. size_t mbox_addr = MBOX_ADDR;
  35. size_t wdt_base_addr = WDT_BASE;
  36. uint8_t *mac_reg_base_addr = (uint8_t *)MAC_REG;
  37. uint8_t *eth_send_no_cache = (uint8_t *)SEND_DATA_NO_CACHE;
  38. uint8_t *eth_recv_no_cache = (uint8_t *)RECV_DATA_NO_CACHE;
  39. #ifdef RT_USING_SMART
  40. struct mem_desc platform_mem_desc[] = {
  41. {KERNEL_VADDR_START, KERNEL_VADDR_START + 0x0fffffff, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM}
  42. };
  43. #else
  44. struct mem_desc platform_mem_desc[] = {
  45. {0x00200000, (128ul << 20) - 1, 0x00200000, NORMAL_MEM},
  46. {0xFC000000, 0x000100000000 - 1, 0xFC000000, DEVICE_MEM},
  47. };
  48. #endif
  49. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc)/sizeof(platform_mem_desc[0]);
  50. void idle_wfi(void)
  51. {
  52. asm volatile ("wfi");
  53. }
  54. /**
  55. * This function will initialize board
  56. */
  57. extern size_t MMUTable[];
  58. int rt_hw_gtimer_init(void);
  59. rt_region_t init_page_region = {
  60. PAGE_START,
  61. PAGE_END,
  62. };
  63. /**
  64. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  65. * after interrupt disabled.
  66. */
  67. void rt_hw_board_init(void)
  68. {
  69. extern void (*system_off)(void);
  70. extern void reboot(void);
  71. system_off = reboot;
  72. /* io device remap */
  73. #ifdef RT_USING_SMART
  74. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xfffffffff0000000, 0x10000000, MMUTable, PV_OFFSET);
  75. #else
  76. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x000400000000, 0x10000000, MMUTable, 0);
  77. #endif
  78. rt_page_init(init_page_region);
  79. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  80. /* map peripheral address to virtual address */
  81. #ifdef RT_USING_HEAP
  82. /* initialize system heap */
  83. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  84. #endif
  85. /* initialize hardware interrupt */
  86. rt_hw_interrupt_init();
  87. //gpio
  88. gpio_base_addr = (size_t)rt_ioremap((void*)GPIO_BASE_ADDR, 0x1000);
  89. //pactl
  90. pactl_cs_base = (size_t)rt_ioremap((void*)PACTL_CS_ADDR, 0x1000);
  91. //stimer
  92. stimer_base_addr = (size_t)rt_ioremap((void*)STIMER_BASE, 0x1000);
  93. //mmc2_base_addr
  94. mmc2_base_addr = (size_t)rt_ioremap((void*)MMC2_BASE_ADDR, 0x1000);
  95. //mbox
  96. videocore_mbox = (size_t)rt_ioremap((void*)VIDEOCORE_MBOX, 0x1000);
  97. // mbox msg
  98. mbox = (volatile unsigned int *)rt_pages_alloc(0);
  99. //wdt
  100. wdt_base_addr = (size_t)rt_ioremap((void*)WDT_BASE, 0x1000);
  101. //mac
  102. mac_reg_base_addr = (void *)rt_ioremap((void*)MAC_REG, 0x80000);
  103. // eth data
  104. eth_send_no_cache = (void *)rt_pages_alloc(rt_page_bits(0x200000));
  105. eth_recv_no_cache = (void *)rt_pages_alloc(rt_page_bits(0x200000));
  106. /* initialize uart */
  107. rt_hw_uart_init();
  108. /* initialize timer for os tick */
  109. rt_hw_gtimer_init();
  110. #ifdef RT_USING_CONSOLE
  111. /* set console device */
  112. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  113. #endif /* RT_USING_CONSOLE */
  114. rt_kprintf("heap: 0x%08x - 0x%08x\n", HEAP_BEGIN, HEAP_END);
  115. #ifdef RT_USING_COMPONENTS_INIT
  116. rt_components_board_init();
  117. #endif
  118. rt_thread_idle_sethook(idle_wfi);
  119. }
  120. #ifdef RT_USING_SMP
  121. #include <gic.h>
  122. void rt_hw_mmu_ktbl_set(unsigned long tbl);
  123. void _secondary_cpu_entry(void);
  124. static unsigned long cpu_release_paddr[] =
  125. {
  126. [0] = 0xd8,
  127. [1] = 0xe0,
  128. [2] = 0xe8,
  129. [3] = 0xf0,
  130. [4] = 0x00
  131. };
  132. #ifndef RT_USING_SMART
  133. static void *_remap(void *paddr, size_t size)
  134. {
  135. int ret;
  136. static void *va = 0;
  137. size_t low_off = (size_t)paddr & ARCH_PAGE_MASK;
  138. if (va)
  139. return va + low_off;
  140. va = rt_kernel_space.start;
  141. while (1)
  142. {
  143. int rt_kmem_map_phy(void *va, void *pa, rt_size_t length, rt_size_t attr);
  144. ret = rt_kmem_map_phy(va, 0x0, ARCH_PAGE_SIZE, MMU_MAP_K_DEVICE);
  145. if (ret == RT_EOK)
  146. {
  147. break;
  148. }
  149. else
  150. {
  151. va += ARCH_PAGE_SIZE;
  152. }
  153. }
  154. return va + low_off;
  155. }
  156. #endif /* RT_USING_SMART */
  157. void rt_hw_secondary_cpu_up(void)
  158. {
  159. int i;
  160. void *release_addr;
  161. for (i = 1; i < RT_CPUS_NR && cpu_release_paddr[i]; ++i)
  162. {
  163. #ifdef RT_USING_SMART
  164. release_addr = rt_ioremap((void *)cpu_release_paddr[i], sizeof(cpu_release_paddr[0]));
  165. #else
  166. release_addr = _remap((void *)cpu_release_paddr[i], sizeof(cpu_release_paddr[0]));
  167. #endif
  168. __asm__ volatile ("str %0, [%1]"::"rZ"((unsigned long)_secondary_cpu_entry + PV_OFFSET), "r"(release_addr));
  169. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, release_addr, sizeof(release_addr));
  170. asm volatile ("dsb sy");
  171. asm volatile ("sev");
  172. }
  173. }
  174. void rt_hw_secondary_cpu_bsp_start(void)
  175. {
  176. rt_hw_spin_lock(&_cpus_lock);
  177. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  178. rt_hw_vector_init();
  179. arm_gic_cpu_init(0, 0);
  180. rt_hw_gtimer_init();
  181. rt_kprintf("\rcpu %d boot success\n", rt_hw_cpu_id());
  182. rt_system_scheduler_start();
  183. }
  184. void rt_hw_secondary_cpu_idle_exec(void)
  185. {
  186. asm volatile ("wfe":::"memory", "cc");
  187. }
  188. #endif