board.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2021, 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. * 2021-07-31 GuEe-GUI config the memory/io address map
  10. * 2021-09-11 GuEe-GUI remove do-while in rt_hw_timer_isr
  11. * 2021-12-28 GuEe-GUI add smp support
  12. */
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include "board.h"
  16. #include <mmu.h>
  17. #include <gic.h>
  18. #include <gicv3.h>
  19. #include <psci.h>
  20. #include <gtimer.h>
  21. #include <cpuport.h>
  22. #include <interrupt.h>
  23. #include "drv_uart.h"
  24. struct mem_desc platform_mem_desc[] =
  25. {
  26. {0x40000000, 0x80000000, 0x40000000, NORMAL_MEM},
  27. {PL031_RTC_BASE, PL031_RTC_BASE + 0x1000, PL031_RTC_BASE, DEVICE_MEM},
  28. {PL011_UART0_BASE, PL011_UART0_BASE + 0x1000, PL011_UART0_BASE, DEVICE_MEM},
  29. {VIRTIO_MMIO_BASE, VIRTIO_MMIO_BASE + VIRTIO_MAX_NR * VIRTIO_MMIO_SIZE, VIRTIO_MMIO_BASE, DEVICE_MEM},
  30. #ifdef BSP_USING_GICV2
  31. {GIC_PL390_DISTRIBUTOR_PPTR, GIC_PL390_DISTRIBUTOR_PPTR + 0x1000, GIC_PL390_DISTRIBUTOR_PPTR, DEVICE_MEM},
  32. #endif
  33. #ifdef BSP_USING_GICV3
  34. {GIC_PL500_DISTRIBUTOR_PPTR, GIC_PL500_DISTRIBUTOR_PPTR + 0x1000, GIC_PL500_DISTRIBUTOR_PPTR, DEVICE_MEM},
  35. {GIC_PL500_REDISTRIBUTOR_PPTR, GIC_PL500_REDISTRIBUTOR_PPTR + 0xf60000, GIC_PL500_REDISTRIBUTOR_PPTR, DEVICE_MEM},
  36. #endif
  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. /**
  44. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  45. * after interrupt disabled.
  46. */
  47. void rt_hw_board_init(void)
  48. {
  49. rt_hw_init_mmu_table(platform_mem_desc, platform_mem_desc_size);
  50. rt_hw_mmu_init();
  51. /* initialize hardware interrupt */
  52. rt_hw_interrupt_init();
  53. /* initialize uart */
  54. rt_hw_uart_init();
  55. /* initialize timer for os tick */
  56. rt_hw_gtimer_init();
  57. rt_thread_idle_sethook(idle_wfi);
  58. arm_psci_init(PSCI_METHOD_HVC, RT_NULL, RT_NULL);
  59. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  60. /* set console device */
  61. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  62. #endif
  63. #ifdef RT_USING_HEAP
  64. /* initialize memory system */
  65. rt_kprintf("heap: [0x%08x - 0x%08x]\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  66. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  67. #endif
  68. #ifdef RT_USING_COMPONENTS_INIT
  69. rt_components_board_init();
  70. #endif
  71. #ifdef RT_USING_SMP
  72. /* install IPI handle */
  73. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  74. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  75. #endif
  76. }
  77. void poweroff(void)
  78. {
  79. arm_psci_system_off();
  80. }
  81. MSH_CMD_EXPORT(poweroff, poweroff...);
  82. void reboot(void)
  83. {
  84. arm_psci_system_reboot();
  85. }
  86. MSH_CMD_EXPORT(reboot, reboot...);
  87. #ifdef RT_USING_SMP
  88. void rt_hw_secondary_cpu_up(void)
  89. {
  90. int i;
  91. extern void secondary_cpu_start(void);
  92. extern rt_uint64_t rt_cpu_mpidr_early[];
  93. for (i = 1; i < RT_CPUS_NR; ++i)
  94. {
  95. arm_psci_cpu_on(rt_cpu_mpidr_early[i], (uint64_t)(secondary_cpu_start));
  96. }
  97. }
  98. void secondary_cpu_c_start(void)
  99. {
  100. rt_hw_mmu_init();
  101. rt_hw_spin_lock(&_cpus_lock);
  102. arm_gic_cpu_init(0, platform_get_gic_cpu_base());
  103. #ifdef BSP_USING_GICV3
  104. arm_gic_redist_init(0, platform_get_gic_redist_base());
  105. #endif
  106. rt_hw_vector_init();
  107. rt_hw_gtimer_local_enable();
  108. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  109. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  110. rt_system_scheduler_start();
  111. }
  112. void rt_hw_secondary_cpu_idle_exec(void)
  113. {
  114. __WFE();
  115. }
  116. #endif