board.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /* io device remap */
  70. #ifdef RT_USING_SMART
  71. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xfffffffff0000000, 0x10000000, MMUTable, PV_OFFSET);
  72. #else
  73. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x000400000000, 0x10000000, MMUTable, 0);
  74. #endif
  75. rt_page_init(init_page_region);
  76. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  77. /* map peripheral address to virtual address */
  78. #ifdef RT_USING_HEAP
  79. /* initialize system heap */
  80. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  81. #endif
  82. /* initialize hardware interrupt */
  83. rt_hw_interrupt_init();
  84. //gpio
  85. gpio_base_addr = (size_t)rt_ioremap((void*)GPIO_BASE_ADDR, 0x1000);
  86. //pactl
  87. pactl_cs_base = (size_t)rt_ioremap((void*)PACTL_CS_ADDR, 0x1000);
  88. //stimer
  89. stimer_base_addr = (size_t)rt_ioremap((void*)STIMER_BASE, 0x1000);
  90. //mmc2_base_addr
  91. mmc2_base_addr = (size_t)rt_ioremap((void*)MMC2_BASE_ADDR, 0x1000);
  92. //mbox
  93. videocore_mbox = (size_t)rt_ioremap((void*)VIDEOCORE_MBOX, 0x1000);
  94. // mbox msg
  95. mbox = (volatile unsigned int *)rt_pages_alloc(0);
  96. //wdt
  97. wdt_base_addr = (size_t)rt_ioremap((void*)WDT_BASE, 0x1000);
  98. //mac
  99. mac_reg_base_addr = (void *)rt_ioremap((void*)MAC_REG, 0x80000);
  100. // eth data
  101. eth_send_no_cache = (void *)rt_pages_alloc(rt_page_bits(0x200000));
  102. eth_recv_no_cache = (void *)rt_pages_alloc(rt_page_bits(0x200000));
  103. /* initialize uart */
  104. rt_hw_uart_init();
  105. /* initialize timer for os tick */
  106. rt_hw_gtimer_init();
  107. #ifdef RT_USING_CONSOLE
  108. /* set console device */
  109. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  110. #endif /* RT_USING_CONSOLE */
  111. rt_kprintf("heap: 0x%08x - 0x%08x\n", HEAP_BEGIN, HEAP_END);
  112. #ifdef RT_USING_COMPONENTS_INIT
  113. rt_components_board_init();
  114. #endif
  115. rt_thread_idle_sethook(idle_wfi);
  116. }
  117. #ifdef RT_USING_SMP
  118. #include <gic.h>
  119. void rt_hw_mmu_ktbl_set(unsigned long tbl);
  120. void _secondary_cpu_entry(void);
  121. static unsigned long cpu_release_paddr[] =
  122. {
  123. [0] = 0xd8,
  124. [1] = 0xe0,
  125. [2] = 0xe8,
  126. [3] = 0xf0,
  127. [4] = 0x00
  128. };
  129. #ifndef RT_USING_SMART
  130. static void *_remap(void *paddr, size_t size)
  131. {
  132. int ret;
  133. static void *va = 0;
  134. size_t low_off = (size_t)paddr & ARCH_PAGE_MASK;
  135. if (va)
  136. return va + low_off;
  137. va = rt_kernel_space.start;
  138. while (1)
  139. {
  140. int rt_kmem_map_phy(void *va, void *pa, rt_size_t length, rt_size_t attr);
  141. ret = rt_kmem_map_phy(va, 0x0, ARCH_PAGE_SIZE, MMU_MAP_K_DEVICE);
  142. if (ret == RT_EOK)
  143. {
  144. break;
  145. }
  146. else
  147. {
  148. va += ARCH_PAGE_SIZE;
  149. }
  150. }
  151. return va + low_off;
  152. }
  153. #endif /* RT_USING_SMART */
  154. void rt_hw_secondary_cpu_up(void)
  155. {
  156. int i;
  157. void *release_addr;
  158. for (i = 1; i < RT_CPUS_NR && cpu_release_paddr[i]; ++i)
  159. {
  160. #ifdef RT_USING_SMART
  161. release_addr = rt_ioremap((void *)cpu_release_paddr[i], sizeof(cpu_release_paddr[0]));
  162. #else
  163. release_addr = _remap((void *)cpu_release_paddr[i], sizeof(cpu_release_paddr[0]));
  164. #endif
  165. __asm__ volatile ("str %0, [%1]"::"rZ"((unsigned long)_secondary_cpu_entry + PV_OFFSET), "r"(release_addr));
  166. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, release_addr, sizeof(release_addr));
  167. asm volatile ("dsb sy");
  168. asm volatile ("sev");
  169. }
  170. }
  171. void rt_hw_secondary_cpu_bsp_start(void)
  172. {
  173. rt_hw_spin_lock(&_cpus_lock);
  174. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  175. rt_hw_vector_init();
  176. arm_gic_cpu_init(0, 0);
  177. rt_hw_gtimer_init();
  178. rt_kprintf("\rcpu %d boot success\n", rt_hw_cpu_id());
  179. rt_system_scheduler_start();
  180. }
  181. void rt_hw_secondary_cpu_idle_exec(void)
  182. {
  183. asm volatile ("wfe":::"memory", "cc");
  184. }
  185. #endif