board.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. {PL061_GPIO_BASE, PL061_GPIO_BASE + 0x1000, PL061_GPIO_BASE, DEVICE_MEM},
  29. {PL011_UART0_BASE, PL011_UART0_BASE + 0x1000, PL011_UART0_BASE, DEVICE_MEM},
  30. {VIRTIO_MMIO_BASE, VIRTIO_MMIO_BASE + VIRTIO_MAX_NR * VIRTIO_MMIO_SIZE, VIRTIO_MMIO_BASE, DEVICE_MEM},
  31. #ifdef BSP_USING_GICV2
  32. {GIC_PL390_DISTRIBUTOR_PPTR, GIC_PL390_DISTRIBUTOR_PPTR + 0x1000, GIC_PL390_DISTRIBUTOR_PPTR, DEVICE_MEM},
  33. #endif
  34. #ifdef BSP_USING_GICV3
  35. {GIC_PL500_DISTRIBUTOR_PPTR, GIC_PL500_DISTRIBUTOR_PPTR + 0x1000, GIC_PL500_DISTRIBUTOR_PPTR, DEVICE_MEM},
  36. {GIC_PL500_REDISTRIBUTOR_PPTR, GIC_PL500_REDISTRIBUTOR_PPTR + 0xf60000, GIC_PL500_REDISTRIBUTOR_PPTR, DEVICE_MEM},
  37. #endif
  38. };
  39. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc)/sizeof(platform_mem_desc[0]);
  40. void idle_wfi(void)
  41. {
  42. asm volatile ("wfi");
  43. }
  44. /**
  45. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  46. * after interrupt disabled.
  47. */
  48. void rt_hw_board_init(void)
  49. {
  50. rt_hw_init_mmu_table(platform_mem_desc, platform_mem_desc_size);
  51. rt_hw_mmu_init();
  52. /* initialize hardware interrupt */
  53. rt_hw_interrupt_init();
  54. /* initialize uart */
  55. rt_hw_uart_init();
  56. /* initialize timer for os tick */
  57. rt_hw_gtimer_init();
  58. rt_thread_idle_sethook(idle_wfi);
  59. arm_psci_init(PSCI_METHOD_HVC, RT_NULL, RT_NULL);
  60. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  61. /* set console device */
  62. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  63. #endif
  64. #ifdef RT_USING_HEAP
  65. /* initialize memory system */
  66. rt_kprintf("heap: [0x%08x - 0x%08x]\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  67. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  68. #endif
  69. #ifdef RT_USING_COMPONENTS_INIT
  70. rt_components_board_init();
  71. #endif
  72. #ifdef RT_USING_SMP
  73. /* install IPI handle */
  74. rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
  75. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  76. #endif
  77. }
  78. void poweroff(void)
  79. {
  80. arm_psci_system_off();
  81. }
  82. MSH_CMD_EXPORT(poweroff, poweroff...);
  83. void rt_hw_cpu_shutdown()
  84. {
  85. rt_kprintf("shutdown...\n");
  86. poweroff();
  87. }
  88. void reboot(void)
  89. {
  90. arm_psci_system_reboot();
  91. }
  92. MSH_CMD_EXPORT(reboot, reboot...);
  93. #ifdef RT_USING_SMP
  94. void rt_hw_secondary_cpu_up(void)
  95. {
  96. int i;
  97. extern void secondary_cpu_start(void);
  98. extern rt_uint64_t rt_cpu_mpidr_early[];
  99. for (i = 1; i < RT_CPUS_NR; ++i)
  100. {
  101. arm_psci_cpu_on(rt_cpu_mpidr_early[i], (uint64_t)(secondary_cpu_start));
  102. }
  103. }
  104. void secondary_cpu_c_start(void)
  105. {
  106. rt_hw_mmu_init();
  107. rt_hw_spin_lock(&_cpus_lock);
  108. arm_gic_cpu_init(0, platform_get_gic_cpu_base());
  109. #ifdef BSP_USING_GICV3
  110. arm_gic_redist_init(0, platform_get_gic_redist_base());
  111. #endif
  112. rt_hw_vector_init();
  113. rt_hw_gtimer_local_enable();
  114. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  115. rt_kprintf("\rcall cpu %d on success\n", rt_hw_cpu_id());
  116. rt_system_scheduler_start();
  117. }
  118. void rt_hw_secondary_cpu_idle_exec(void)
  119. {
  120. __WFE();
  121. }
  122. #endif