board.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "board.h"
  14. #include <mmu.h>
  15. #include "drv_uart.h"
  16. void rt_hw_vector_init(void);
  17. static uint64_t tickval = 0;
  18. void rt_hw_timer_isr(int vector, void *parameter)
  19. {
  20. uint64_t cntvct_el0;
  21. do
  22. {
  23. tickval += 0xF424;
  24. __asm__ volatile ("msr CNTV_CVAL_EL0, %0"::"r"(tickval));
  25. __asm__ volatile ("mrs %0, CNTVCT_EL0":"=r"(cntvct_el0));
  26. }
  27. while (cntvct_el0 >= tickval);
  28. rt_tick_increase();
  29. }
  30. int rt_hw_timer_init()
  31. {
  32. uint64_t val;
  33. rt_hw_interrupt_install(27, rt_hw_timer_isr, RT_NULL, "tick");
  34. rt_hw_interrupt_umask(27);
  35. val = 0;
  36. __asm__ volatile ("msr CNTV_CTL_EL0, %0"::"r"(val));
  37. val = 0x03B9ACA0;
  38. __asm__ volatile ("msr CNTFRQ_EL0, %0"::"r"(val));
  39. tickval += 0xF424;
  40. __asm__ volatile ("msr CNTV_CVAL_EL0, %0"::"r"(tickval));
  41. val = 1;
  42. __asm__ volatile ("msr CNTV_CTL_EL0, %0"::"r"(val));
  43. return 0;
  44. }
  45. void idle_wfi(void)
  46. {
  47. asm volatile ("wfi");
  48. }
  49. /**
  50. * Initialize the Hardware related stuffs. Called from rtthread_startup()
  51. * after interrupt disabled.
  52. */
  53. void rt_hw_board_init(void)
  54. {
  55. uint64_t cont;
  56. mmu_init();
  57. cont = (uint64_t)RT_HW_HEAP_END + 0x1fffff;
  58. cont &= ~0x1fffff;
  59. cont -= 0x40000000;
  60. cont >>= 21;
  61. /* memory location */
  62. armv8_map_2M(0x40000000, 0x40000000, cont, MEM_ATTR_MEMORY);
  63. /* uart location*/
  64. armv8_map_2M(PL011_UART0_BASE, PL011_UART0_BASE, 0x1, MEM_ATTR_IO);
  65. /* gic location*/
  66. armv8_map_2M(GIC_PL390_DISTRIBUTOR_PPTR, GIC_PL390_DISTRIBUTOR_PPTR, 0x1, MEM_ATTR_IO);
  67. mmu_enable();
  68. /* initialize hardware interrupt */
  69. rt_hw_interrupt_init(); // in libcpu/interrupt.c. Set some data structures, no operation on device
  70. rt_hw_vector_init(); // in libcpu/interrupt.c. == rt_cpu_vector_set_base((rt_ubase_t)&system_vectors);
  71. /* initialize uart */
  72. rt_hw_uart_init(); // driver/drv_uart.c
  73. /* initialize timer for os tick */
  74. rt_hw_timer_init();
  75. rt_thread_idle_sethook(idle_wfi);
  76. #ifdef RT_USING_CONSOLE
  77. /* set console device */
  78. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  79. #endif /* RT_USING_CONSOLE */
  80. #ifdef RT_USING_HEAP
  81. /* initialize memory system */
  82. rt_kprintf("heap: [0x%08x - 0x%08x]\n", RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  83. rt_system_heap_init(RT_HW_HEAP_BEGIN, RT_HW_HEAP_END);
  84. #endif
  85. #ifdef RT_USING_COMPONENTS_INIT
  86. rt_components_board_init();
  87. #endif
  88. }