startup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2021-2025 HPMicro
  3. *
  4. *
  5. */
  6. #include "hpm_common.h"
  7. #include "hpm_soc.h"
  8. #include "hpm_l1c_drv.h"
  9. #include <rtthread.h>
  10. void system_init(void);
  11. extern int entry(void);
  12. extern void __libc_init_array(void);
  13. extern void __libc_fini_array(void);
  14. void system_init(void)
  15. {
  16. disable_global_irq(CSR_MSTATUS_MIE_MASK);
  17. disable_irq_from_intc();
  18. enable_irq_from_intc();
  19. enable_global_irq(CSR_MSTATUS_MIE_MASK);
  20. #ifndef CONFIG_NOT_ENABLE_ICACHE
  21. l1c_ic_enable();
  22. #endif
  23. #ifndef CONFIG_NOT_ENABLE_DCACHE
  24. l1c_dc_enable();
  25. #endif
  26. }
  27. __attribute__((weak)) void c_startup(void)
  28. {
  29. #ifndef __SES_RISCV
  30. uint32_t i, size;
  31. #ifdef FLASH_XIP
  32. extern uint8_t __vector_ram_start__[], __vector_ram_end__[], __vector_load_addr__[];
  33. size = __vector_ram_end__ - __vector_ram_start__;
  34. for (i = 0; i < size; i++) {
  35. *(__vector_ram_start__ + i) = *(__vector_load_addr__ + i);
  36. }
  37. #endif
  38. extern uint8_t __etext[];
  39. extern uint8_t __bss_start__[], __bss_end__[];
  40. extern uint8_t __tbss_start__[], __tbss_end__[];
  41. extern uint8_t __tdata_start__[], __tdata_end__[];
  42. extern uint8_t __fast_load_addr__[];
  43. extern uint8_t __noncacheable_init_load_addr__[];
  44. extern uint8_t __data_load_addr__[];
  45. extern uint8_t __tdata_load_addr__[];
  46. extern uint8_t __data_start__[], __data_end__[];
  47. extern uint8_t __noncacheable_bss_start__[], __noncacheable_bss_end__[];
  48. extern uint8_t __ramfunc_start__[], __ramfunc_end__[];
  49. extern uint8_t __noncacheable_init_start__[], __noncacheable_init_end__[];
  50. /* tbss section */
  51. size = __tbss_end__ - __tbss_start__;
  52. for (i = 0; i < size; i++) {
  53. *(__tbss_start__ + i) = 0;
  54. }
  55. /* bss section */
  56. size = __bss_end__ - __bss_start__;
  57. for (i = 0; i < size; i++) {
  58. *(__bss_start__ + i) = 0;
  59. }
  60. /* noncacheable bss section */
  61. size = __noncacheable_bss_end__ - __noncacheable_bss_start__;
  62. for (i = 0; i < size; i++) {
  63. *(__noncacheable_bss_start__ + i) = 0;
  64. }
  65. /* tdata section LMA: etext */
  66. size = __tdata_end__ - __tdata_start__;
  67. for (i = 0; i < size; i++) {
  68. *(__tdata_start__ + i) = *(__tdata_load_addr__ + i);
  69. }
  70. /* data section LMA: etext */
  71. size = __data_end__ - __data_start__;
  72. for (i = 0; i < size; i++) {
  73. *(__data_start__ + i) = *(__data_load_addr__ + i);
  74. }
  75. /* ramfunc section LMA: etext + data length */
  76. size = __ramfunc_end__ - __ramfunc_start__;
  77. for (i = 0; i < size; i++) {
  78. *(__ramfunc_start__ + i) = *(__fast_load_addr__ + i);
  79. }
  80. /* noncacheable init section LMA: etext + data length + ramfunc length */
  81. size = __noncacheable_init_end__ - __noncacheable_init_start__;
  82. for (i = 0; i < size; i++) {
  83. *(__noncacheable_init_start__ + i) = *(__noncacheable_init_load_addr__ + i);
  84. }
  85. /* NOTE: Invalid I-Cache in case the I-Cache was filled with incorrect data during prefetch before copying the ram
  86. * function code to its destination.
  87. */
  88. fencei();
  89. #endif
  90. }
  91. __attribute__((weak)) int main(void)
  92. {
  93. while(1);
  94. }
  95. void reset_handler(void)
  96. {
  97. /**
  98. * Disable preemptive interrupt
  99. */
  100. HPM_PLIC->FEATURE = 0;
  101. /*
  102. * Initialize LMA/VMA sections.
  103. * Relocation for any sections that need to be copied from LMA to VMA.
  104. */
  105. c_startup();
  106. /* Call platform specific hardware initialization */
  107. system_init();
  108. #ifndef __SES_RISCV
  109. /* Do global constructors */
  110. __libc_init_array();
  111. #endif
  112. /* Entry function */
  113. entry();
  114. }
  115. __attribute__((weak)) void _init(void)
  116. {
  117. }