startup.S 3.8 KB

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