board.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #define DBG_TAG "board"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <mm_aspace.h>
  19. #include "board.h"
  20. #include "drv_uart.h"
  21. #include "cp15.h"
  22. #include "mmu.h"
  23. #include "mbox.h"
  24. #include <mm_page.h>
  25. #ifdef RT_USING_SMART
  26. #include <lwp_arch.h>
  27. #endif
  28. extern size_t MMUTable[];
  29. size_t gpio_base_addr = GPIO_BASE_ADDR;
  30. size_t uart_base_addr = UART_BASE;
  31. size_t gic_base_addr = GIC_V2_BASE;
  32. size_t arm_timer_base = ARM_TIMER_BASE;
  33. size_t pactl_cs_base = PACTL_CS_ADDR;
  34. size_t stimer_base_addr = STIMER_BASE;
  35. size_t mmc2_base_addr = MMC2_BASE_ADDR;
  36. size_t videocore_mbox = VIDEOCORE_MBOX;
  37. size_t mbox_addr = MBOX_ADDR;
  38. size_t wdt_base_addr = WDT_BASE;
  39. uint8_t *mac_reg_base_addr = (uint8_t *)MAC_REG;
  40. uint8_t *eth_send_no_cache = (uint8_t *)SEND_DATA_NO_CACHE;
  41. uint8_t *eth_recv_no_cache = (uint8_t *)RECV_DATA_NO_CACHE;
  42. #ifdef RT_USING_SMART
  43. struct mem_desc platform_mem_desc[] = {
  44. {KERNEL_VADDR_START, KERNEL_VADDR_START + 0x0fffffff, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM}
  45. };
  46. #else
  47. struct mem_desc platform_mem_desc[] = {
  48. {0x00200000, (256ul << 20) - 1, 0x00200000, NORMAL_MEM},
  49. {0xFC000000, 0x000100000000 - 1, 0xFC000000, DEVICE_MEM},
  50. };
  51. #endif
  52. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc)/sizeof(platform_mem_desc[0]);
  53. void idle_wfi(void)
  54. {
  55. asm volatile ("wfi");
  56. }
  57. /**
  58. * This function will initialize board
  59. */
  60. extern size_t MMUTable[];
  61. int rt_hw_gtimer_init(void);
  62. rt_region_t init_page_region = {
  63. PAGE_START,
  64. PAGE_END,
  65. };
  66. /**
  67. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  68. * after interrupt disabled.
  69. */
  70. void rt_hw_board_init(void)
  71. {
  72. rt_hw_earlycon_ioremap_early();
  73. /* io device remap */
  74. #ifdef RT_USING_SMART
  75. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0xfffffffff0000000, 0x10000000, MMUTable, PV_OFFSET);
  76. #else
  77. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x080000000000, 0x10000000, MMUTable, 0);
  78. #endif /* RT_USING_SMART */
  79. rt_page_init(init_page_region);
  80. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  81. /* map peripheral address to virtual address */
  82. #ifdef RT_USING_HEAP
  83. /* initialize system heap */
  84. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  85. #endif
  86. /* initialize hardware interrupt */
  87. rt_hw_interrupt_init();
  88. //gpio
  89. gpio_base_addr = (size_t)rt_ioremap((void*)GPIO_BASE_ADDR, 0x1000);
  90. //pactl
  91. pactl_cs_base = (size_t)rt_ioremap((void*)PACTL_CS_ADDR, 0x1000);
  92. //stimer
  93. stimer_base_addr = (size_t)rt_ioremap((void*)STIMER_BASE, 0x1000);
  94. //mmc2_base_addr
  95. mmc2_base_addr = (size_t)rt_ioremap((void*)MMC2_BASE_ADDR, 0x1000);
  96. //mbox
  97. videocore_mbox = (size_t)rt_ioremap((void*)VIDEOCORE_MBOX, 0x1000);
  98. // mbox msg
  99. mbox = (volatile unsigned int *)rt_pages_alloc(0);
  100. //wdt
  101. wdt_base_addr = (size_t)rt_ioremap((void*)WDT_BASE, 0x1000);
  102. //mac
  103. mac_reg_base_addr = (void *)rt_ioremap((void*)MAC_REG, 0x80000);
  104. // eth data
  105. eth_send_no_cache = (void *)rt_pages_alloc(rt_page_bits(0x200000));
  106. eth_recv_no_cache = (void *)rt_pages_alloc(rt_page_bits(0x200000));
  107. /* initialize uart */
  108. rt_hw_uart_init();
  109. /* initialize timer for os tick */
  110. rt_hw_gtimer_init();
  111. #ifdef RT_USING_CONSOLE
  112. /* set console device */
  113. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  114. #endif /* RT_USING_CONSOLE */
  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. void rt_hw_secondary_cpu_up(void)
  133. {
  134. int i;
  135. void *release_addr;
  136. for (i = 1; i < RT_CPUS_NR && cpu_release_paddr[i]; ++i)
  137. {
  138. release_addr = rt_ioremap((void *)cpu_release_paddr[i], sizeof(cpu_release_paddr[0]));
  139. __asm__ volatile ("str %0, [%1]"::"rZ"((unsigned long)_secondary_cpu_entry + PV_OFFSET), "r"(release_addr));
  140. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, release_addr, sizeof(release_addr));
  141. asm volatile ("dsb sy");
  142. asm volatile ("sev");
  143. }
  144. }
  145. void rt_hw_secondary_cpu_bsp_start(void)
  146. {
  147. rt_hw_spin_lock(&_cpus_lock);
  148. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  149. rt_hw_vector_init();
  150. arm_gic_cpu_init(0, 0);
  151. rt_hw_gtimer_init();
  152. rt_kprintf("\rcpu %d boot success\n", rt_hw_cpu_id());
  153. rt_system_scheduler_start();
  154. }
  155. void rt_hw_secondary_cpu_idle_exec(void)
  156. {
  157. asm volatile ("wfe":::"memory", "cc");
  158. }
  159. #endif