board.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <mmu.h>
  13. #include <rtdevice.h>
  14. #include <gicv3.h>
  15. #include <gtimer.h>
  16. #include <cpuport.h>
  17. #include <interrupt.h>
  18. #include <ioremap.h>
  19. #include <psci.h>
  20. #include <board.h>
  21. #include <drv_uart.h>
  22. #include "mm_page.h"
  23. #define PLATFORM_MEM_TALBE(va, size) va, ((unsigned long)va + size - 1)
  24. struct mem_desc platform_mem_desc[] =
  25. {
  26. {PLATFORM_MEM_TALBE(0x20000000, 0x10000000), 0x20000000, NORMAL_MEM},
  27. {PLATFORM_MEM_TALBE(GRF_PMU_BASE, 0x10000), GRF_PMU_BASE, DEVICE_MEM},
  28. {PLATFORM_MEM_TALBE(GRF_SYS_BASE, 0x10000), GRF_SYS_BASE, DEVICE_MEM},
  29. {PLATFORM_MEM_TALBE(CRU_BASE, 0x10000), CRU_BASE, DEVICE_MEM},
  30. {PLATFORM_MEM_TALBE(UART0_MMIO_BASE, 0x10000), UART0_MMIO_BASE, DEVICE_MEM},
  31. {PLATFORM_MEM_TALBE(UART1_MMIO_BASE, 0x90000), UART1_MMIO_BASE, DEVICE_MEM},
  32. {PLATFORM_MEM_TALBE(GIC_PL600_DISTRIBUTOR_PPTR, 0x10000), GIC_PL600_DISTRIBUTOR_PPTR, DEVICE_MEM},
  33. {PLATFORM_MEM_TALBE(GIC_PL600_REDISTRIBUTOR_PPTR, 0xc0000), GIC_PL600_REDISTRIBUTOR_PPTR, DEVICE_MEM},
  34. #ifdef PKG_USING_RT_OPENAMP
  35. {PLATFORM_MEM_TALBE(AMP_SHARE_MEMORY_ADDRESS, AMP_SHARE_MEMORY_SIZE), AMP_SHARE_MEMORY_ADDRESS, NORMAL_MEM},
  36. #endif /* PKG_USING_RT_OPENAMP */
  37. };
  38. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]);
  39. void idle_wfi(void)
  40. {
  41. __asm__ volatile ("wfi");
  42. }
  43. void rt_hw_board_init(void)
  44. {
  45. extern unsigned long MMUTable[512];
  46. rt_region_t init_page_region;
  47. rt_hw_mmu_map_init(&rt_kernel_space, (void *) 0x20000000, 0xE0000000 - 1, MMUTable, 0);
  48. init_page_region.start = RT_HW_PAGE_START;
  49. init_page_region.end = RT_HW_PAGE_END;
  50. rt_page_init(init_page_region);
  51. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  52. #ifdef RT_USING_HEAP
  53. /* initialize memory system */
  54. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  55. #endif
  56. /* initialize hardware interrupt */
  57. rt_hw_interrupt_init();
  58. /* initialize uart */
  59. rt_hw_uart_init();
  60. /* initialize timer for os tick */
  61. rt_hw_gtimer_init();
  62. rt_thread_idle_sethook(idle_wfi);
  63. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  64. /* set console device */
  65. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  66. #endif
  67. #ifdef RT_USING_HEAP
  68. /* initialize memory system */
  69. rt_kprintf("heap: [0x%08x - 0x%08x]\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  70. #endif
  71. #ifdef RT_USING_COMPONENTS_INIT
  72. rt_components_board_init();
  73. #endif
  74. #ifdef RT_USING_SMP
  75. /* install IPI handle */
  76. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  77. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  78. #endif
  79. }
  80. void reboot(void)
  81. {
  82. psci_system_reboot();
  83. void *cur_base = rt_ioremap((void *) CRU_BASE, 0x100);
  84. HWREG32(cur_base + 0x00D4) = 0xfdb9;
  85. HWREG32(cur_base + 0x00D8) = 0xeca8;
  86. }
  87. MSH_CMD_EXPORT(reboot, reboot...);
  88. static void print_cpu_id(int argc, char *argv[])
  89. {
  90. rt_kprintf("rt_hw_cpu_id:%d\n", rt_hw_cpu_id());
  91. }
  92. MSH_CMD_EXPORT_ALIAS(print_cpu_id, cpuid, print_cpu_id);
  93. #ifdef RT_USING_AMP
  94. void start_cpu(int argc, char *argv[])
  95. {
  96. rt_uint32_t status;
  97. status = rt_psci_cpu_on(0x3, (rt_uint64_t) 0x7A000000);
  98. rt_kprintf("arm_psci_cpu_on 0x%X\n", status);
  99. }
  100. MSH_CMD_EXPORT(start_cpu, start_cpu);
  101. #ifdef RT_AMP_SLAVE
  102. void rt_hw_cpu_shutdown(void)
  103. {
  104. rt_psci_cpu_off(0);
  105. }
  106. #endif /* RT_AMP_SLAVE */
  107. #endif /* RT_USING_AMP */
  108. #if defined(RT_USING_SMP) || defined(RT_USING_AMP)
  109. rt_uint64_t rt_cpu_mpidr_early[] =
  110. {
  111. [0] = 0x80000000,
  112. [1] = 0x80000100,
  113. [2] = 0x80000200,
  114. [3] = 0x80000300,
  115. [RT_CPUS_NR] = 0
  116. };
  117. #endif
  118. #ifdef RT_USING_SMP
  119. void rt_hw_secondary_cpu_up(void)
  120. {
  121. int i;
  122. extern void _secondary_cpu_entry(void);
  123. rt_uint64_t entry = (rt_uint64_t)rt_kmem_v2p(_secondary_cpu_entry);
  124. for (i = 1; i < RT_CPUS_NR; ++i)
  125. {
  126. rt_psci_cpu_on(rt_cpu_mpidr_early[i], entry);
  127. }
  128. }
  129. extern unsigned long MMUTable[];
  130. void secondary_cpu_c_start(void)
  131. {
  132. rt_hw_mmu_ktbl_set((unsigned long)MMUTable);
  133. rt_hw_spin_lock(&_cpus_lock);
  134. arm_gic_cpu_init(0, platform_get_gic_cpu_base());
  135. arm_gic_redist_init(0, platform_get_gic_redist_base());
  136. rt_hw_vector_init();
  137. rt_hw_gtimer_local_enable();
  138. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  139. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  140. rt_system_scheduler_start();
  141. }
  142. void rt_hw_secondary_cpu_idle_exec(void)
  143. {
  144. rt_hw_wfe();
  145. }
  146. #endif