start.S 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2021-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "hpm_csr_regs.h"
  8. .section .start, "ax"
  9. .global _start
  10. .type _start,@function
  11. _start:
  12. /* Initialize global pointer */
  13. .option push
  14. .option norelax
  15. la gp, __global_pointer$
  16. .option pop
  17. /* reset mstatus to 0*/
  18. csrrw x0, mstatus, x0
  19. #ifdef __riscv_flen
  20. /* Enable FPU */
  21. li t0, CSR_MSTATUS_FS_MASK
  22. csrrs t0, mstatus, t0
  23. /* Initialize FCSR */
  24. fscsr zero
  25. #endif
  26. #ifdef INIT_EXT_RAM_FOR_DATA
  27. la t0, _stack_safe
  28. mv sp, t0
  29. call _init_ext_ram
  30. #endif
  31. /* Initialize stack pointer */
  32. la t0, _stack
  33. mv sp, t0
  34. /*
  35. * Initialize LMA/VMA sections.
  36. * Relocation for any sections that need to be copied from LMA to VMA.
  37. */
  38. call c_startup
  39. #if defined(__SES_RISCV)
  40. /* Initialize the heap */
  41. la a0, __heap_start__
  42. la a1, __heap_end__
  43. sub a1, a1, a0
  44. la t1, __SEGGER_RTL_init_heap
  45. jalr t1
  46. #endif
  47. /* Do global constructors */
  48. call __libc_init_array
  49. #ifndef NO_CLEANUP_AT_START
  50. /* clean up */
  51. call _clean_up
  52. #endif
  53. #ifdef __nds_execit
  54. /* Initialize EXEC.IT table */
  55. la t0, _ITB_BASE_
  56. csrw uitb, t0
  57. #endif
  58. #if defined(CONFIG_FREERTOS) && CONFIG_FREERTOS
  59. #define HANDLER_TRAP freertos_risc_v_trap_handler
  60. /* Use mscratch to store isr level */
  61. csrw mscratch, 0
  62. #elif defined(CONFIG_UCOS_III) && CONFIG_UCOS_III
  63. #define HANDLER_TRAP ucos_risc_v_trap_handler
  64. /* Use mscratch to store isr level */
  65. csrw mscratch, 0
  66. #elif defined(CONFIG_THREADX) && CONFIG_THREADX
  67. #define HANDLER_TRAP tx_risc_v_trap_handler
  68. #define HANDLER_S_TRAP tx_risc_v_trap_handler
  69. /* Use mscratch to store isr level */
  70. csrw mscratch, 0
  71. #else
  72. #define HANDLER_TRAP irq_handler_trap
  73. #endif
  74. #ifndef USE_NONVECTOR_MODE
  75. /* Initial machine trap-vector Base */
  76. la t0, __vector_table
  77. csrw mtvec, t0
  78. /* Enable vectored external PLIC interrupt */
  79. csrsi CSR_MMISC_CTL, 2
  80. #else
  81. /* Initial machine trap-vector Base */
  82. la t0, HANDLER_TRAP
  83. csrw mtvec, t0
  84. /* Disable vectored external PLIC interrupt */
  85. csrci CSR_MMISC_CTL, 2
  86. #endif
  87. /* System reset handler */
  88. call reset_handler
  89. /* Infinite loop, if returned accidentally */
  90. 1: j 1b
  91. .weak exit
  92. exit:
  93. 1: j 1b
  94. .section .isr_vector, "ax"
  95. .weak nmi_handler
  96. nmi_handler:
  97. 1: j 1b
  98. #include "../vectors.h"