board.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * 2020-04-16 bigmagic first version
  9. * 2021-12-28 GuEe-GUI add smp support
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. #include "mmu.h"
  16. #include "gic.h"
  17. #include "gtimer.h"
  18. #include "cpuport.h"
  19. #include "interrupt.h"
  20. #include "mbox.h"
  21. struct mem_desc platform_mem_desc[] =
  22. {
  23. {0, 0x6400000, 0, NORMAL_MEM},
  24. {0xFE200000, 0xFE400000, 0xFE200000, DEVICE_MEM}, /* uart gpio */
  25. {0xFF800000, 0xFFA00000, 0xFF800000, DEVICE_MEM}, /* gic timer */
  26. {WDT_BASE, WDT_BASE + 0x1000, WDT_BASE, DEVICE_MEM}, /* wdt */
  27. {MBOX_ADDR, MBOX_ADDR + 0x200000, MBOX_ADDR, DEVICE_MEM}, /* mbox msg */
  28. {STIMER_BASE, STIMER_BASE + 0x200000, STIMER_BASE, DEVICE_MEM}, /* stimer */
  29. {MAC_BASE_ADDR, MAC_BASE_ADDR + 0x80000, MAC_BASE_ADDR, DEVICE_MEM}, /* mac */
  30. {MMC2_BASE_ADDR, MMC2_BASE_ADDR + 0x200000, MMC2_BASE_ADDR, DEVICE_MEM}, /* mmc */
  31. {ARM_TIMER_BASE, ARM_TIMER_BASE + 0x200000, ARM_TIMER_BASE, DEVICE_MEM}, /* arm timer */
  32. {SEND_DATA_NO_CACHE, SEND_DATA_NO_CACHE + 0x200000, SEND_DATA_NO_CACHE, NORMAL_MEM}, /* eth send */
  33. {RECV_DATA_NO_CACHE, RECV_DATA_NO_CACHE + 0x200000, RECV_DATA_NO_CACHE, NORMAL_MEM}, /* eth recv */
  34. };
  35. const rt_uint32_t platform_mem_desc_size = sizeof(platform_mem_desc)/sizeof(platform_mem_desc[0]);
  36. #if !defined(BSP_USING_CORETIMER) && !defined(RT_USING_SMP)
  37. void rt_hw_timer_isr(int vector, void *parameter)
  38. {
  39. ARM_TIMER_IRQCLR = 0;
  40. rt_tick_increase();
  41. }
  42. #endif
  43. void rt_hw_timer_init(void)
  44. {
  45. #if defined(BSP_USING_CORETIMER) || defined(RT_USING_SMP)
  46. rt_hw_gtimer_init();
  47. core_timer_enable(0);
  48. #else
  49. rt_uint32_t apb_clock = 0;
  50. rt_uint32_t timer_clock = 1000000;
  51. apb_clock = bcm271x_mbox_clock_get_rate(CORE_CLK_ID);
  52. ARM_TIMER_PREDIV = (apb_clock/timer_clock - 1);
  53. ARM_TIMER_RELOAD = 0;
  54. ARM_TIMER_LOAD = 0;
  55. ARM_TIMER_IRQCLR = 1;
  56. ARM_TIMER_CTRL = 0;
  57. ARM_TIMER_RELOAD = 1000000 / RT_TICK_PER_SECOND;
  58. ARM_TIMER_LOAD = 1000000 / RT_TICK_PER_SECOND;
  59. /* 23-bit counter, enable interrupt, enable timer */
  60. ARM_TIMER_CTRL = (1 << 1) | (1 << 5) | (1 << 7);
  61. rt_hw_interrupt_install(ARM_TIMER_IRQ, rt_hw_timer_isr, RT_NULL, "tick");
  62. rt_hw_interrupt_umask(ARM_TIMER_IRQ);
  63. #endif
  64. }
  65. void idle_wfi(void)
  66. {
  67. asm volatile ("wfi");
  68. }
  69. /**
  70. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  71. * after interrupt disabled.
  72. */
  73. void rt_hw_board_init(void)
  74. {
  75. extern void *MMUTable;
  76. rt_hw_mmu_map_init(&rt_kernel_space, (void*)0x80000000, 0x10000000, MMUTable, 0);
  77. rt_hw_mmu_setup(&rt_kernel_space, platform_mem_desc, platform_mem_desc_size);
  78. /* initialize hardware interrupt */
  79. rt_hw_interrupt_init(); // in libcpu/interrupt.c. Set some data structures, no operation on device
  80. /* initialize uart */
  81. rt_hw_uart_init(); // driver/drv_uart.c
  82. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  83. /* set console device */
  84. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  85. #endif
  86. #ifdef RT_USING_HEAP
  87. /* initialize memory system */
  88. rt_kprintf("heap: 0x%08x - 0x%08x\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  89. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  90. #endif
  91. /* initialize timer for os tick */
  92. rt_hw_timer_init();
  93. rt_thread_idle_sethook(idle_wfi);
  94. #ifdef RT_USING_COMPONENTS_INIT
  95. rt_components_board_init();
  96. #endif
  97. #ifdef RT_USING_SMP
  98. /* install IPI handle */
  99. rt_hw_ipi_handler_install(IRQ_ARM_IPI_KICK, rt_scheduler_ipi_handler);
  100. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  101. #endif
  102. }
  103. #ifdef RT_USING_SMP
  104. static unsigned long cpu_release_paddr[] =
  105. {
  106. [0] = 0xd8,
  107. [1] = 0xe0,
  108. [2] = 0xe8,
  109. [3] = 0xf0,
  110. [4] = 0
  111. };
  112. void rt_hw_secondary_cpu_up(void)
  113. {
  114. int i;
  115. extern void secondary_cpu_start(void);
  116. for (i = 1; i < RT_CPUS_NR && cpu_release_paddr[i]; ++i)
  117. {
  118. __asm__ volatile ("str %0, [%1]"::"rZ"((unsigned long)secondary_cpu_start), "r"(cpu_release_paddr[i]));
  119. rt_hw_dcache_flush_range(cpu_release_paddr[i], sizeof(cpu_release_paddr[i]));
  120. __DSB();
  121. __SEV();
  122. }
  123. }
  124. void secondary_cpu_c_start(void)
  125. {
  126. int id;
  127. rt_hw_mmu_init();
  128. id = rt_hw_cpu_id();
  129. rt_hw_spin_lock(&_cpus_lock);
  130. arm_gic_cpu_init(0, platform_get_gic_cpu_base());
  131. rt_hw_vector_init();
  132. rt_hw_gtimer_local_enable();
  133. core_timer_enable(id);
  134. arm_gic_umask(0, IRQ_ARM_IPI_KICK);
  135. rt_kprintf("\rcall cpu %d on success\n", id);
  136. rt_system_scheduler_start();
  137. }
  138. void rt_hw_secondary_cpu_idle_exec(void)
  139. {
  140. __WFE();
  141. }
  142. #endif