startup.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**************************************************
  2. *
  3. * System initialization.
  4. *
  5. * Copyright 2019-2022 IAR Systems AB.
  6. *
  7. **************************************************/
  8. #include "iarMacros.m"
  9. #include "iarCfi.m"
  10. #include "cpufeature.h"
  11. MODULE ?cstartup
  12. PUBWEAK __iar_program_start
  13. PUBLIC __iar_program_start_metal
  14. SECTION CSTACK:DATA:NOROOT(4)
  15. // --------------------------------------------------
  16. SECTION `.cstartup`:CODE:NOROOT(2)
  17. CfiCom ra,1,0
  18. CfiCom ra,1,1
  19. CfiCom ra,1,2
  20. CfiCom ra,1,3
  21. CfiCom ra,1,4
  22. CfiCom ra,1,6
  23. CfiBlk 0,__iar_program_start
  24. CALL_GRAPH_ROOT __iar_program_start, "Reset"
  25. CODE
  26. __iar_program_start:
  27. REQUIRE ?cstart_init_sp
  28. cfi ?RET Undefined
  29. nop // Avoid an empty section
  30. CfiEnd 0
  31. // ----------
  32. //Init gp (required in by the linker config file, if applicable)
  33. SECTION `.cstartup`:CODE:NOROOT(1)
  34. CfiBlk 1,__iar_program_start
  35. CODE
  36. PUBLIC __iar_cstart_init_gp
  37. __iar_cstart_init_gp:
  38. cfi ?RET Undefined
  39. EXTERN __iar_static_base$$GPREL
  40. .option push
  41. .option norelax
  42. ;; lui gp, %hi(__iar_static_base$$GPREL)
  43. ;; addi gp, gp, %lo(__iar_static_base$$GPREL)
  44. la gp, __iar_static_base$$GPREL
  45. .option pop
  46. REQUIRE ?cstart_init_sp
  47. CfiEnd 1
  48. // ----------
  49. // Init sp, note that this MAY be gp relaxed! (since if gp relaxations are
  50. // allowed, __iar_cstart_init_gp is already done
  51. SECTION `.cstartup`:CODE:NOROOT(1)
  52. #ifdef __riscv_flen
  53. REQUIRE __iar_program_start_metal
  54. #endif
  55. REQUIRE call_low_level_init
  56. CfiBlk 2,__iar_program_start
  57. CODE
  58. ?cstart_init_sp:
  59. cfi ?RET Undefined
  60. #if defined(SMP_CPU_CNT) && (SMP_CPU_CNT > 1)
  61. /* Set correct sp for each cpu
  62. * each stack size is __STACK_SIZE
  63. * defined in linker script */
  64. EXTERN __STACK_SIZE
  65. lui t0, %hi(__STACK_SIZE)
  66. addi t0, t0, %lo(__STACK_SIZE)
  67. la sp, SFE(CSTACK)
  68. csrr a0, mhartid
  69. andi a0, a0, 0xFF
  70. li a1, 0
  71. _per_cpu_init_sp_cont:
  72. beq a0, a1, _per_cpu_init_sp_fin
  73. sub sp, sp, t0
  74. addi a1, a1, 1
  75. j _per_cpu_init_sp_cont
  76. _per_cpu_init_sp_fin:
  77. #else
  78. /* Set correct sp for current cpu */
  79. la sp, SFE(CSTACK)
  80. #endif
  81. CfiEnd 2
  82. SECTION `.cstartup`:CODE:NOROOT(1)
  83. CfiBlk 6,__iar_program_start
  84. CODE
  85. __iar_program_start_metal:
  86. #ifdef __riscv_flen
  87. // Enable the floating-point unit by setting the "fs" field in
  88. // the "mstatus" register.
  89. lui a0, %hi(1 << 13)
  90. csrs mstatus, a0
  91. // Set rounding mode to "round to nearest" and clear
  92. // the floating-point exception flags.
  93. csrwi fcsr, 0
  94. #else
  95. nop // avoid empty sections
  96. #endif
  97. CfiEnd 6
  98. SECTION `.cstartup`:CODE:NOROOT(1)
  99. CfiBlk 4,__iar_program_start
  100. CODE
  101. EXTERN __low_level_init
  102. EXTERN __iar_data_init2
  103. call_low_level_init:
  104. cfi ?RET Undefined
  105. CfiCall __low_level_init
  106. // __iar_data_init2 is done directly in __low_level_init in Nuclei SDK
  107. // main function will be called directly in __low_level_init
  108. call __low_level_init
  109. EXTERN exit
  110. CfiCall exit
  111. call exit
  112. ?cstart_end:
  113. j ?cstart_end
  114. CfiEnd 4
  115. /* This section is required by some devices to handle HW reset */
  116. SECTION `.alias.hwreset`:CODE:NOROOT(2)
  117. PUBLIC __alias_hw_reset
  118. PUBLIC _reset_vector
  119. EXTERN exc_entry
  120. /* TODO this is used to handle vector table + reset vector + exception entry */
  121. _reset_vector:
  122. .option push
  123. .option norelax
  124. j __alias_hw_reset
  125. #ifdef CFG_HAS_EXCP
  126. j exc_entry
  127. #endif
  128. .option pop
  129. __alias_hw_reset:
  130. csrci mstatus, 0x08
  131. ;; lui a0, %hi(__iar_program_start)
  132. ;; addi a0, a0, %lo(__iar_program_start)
  133. la a0, __iar_program_start
  134. jr a0
  135. END