reset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "nds32_intrinsic.h"
  2. #include "nds32.h"
  3. #ifndef VECTOR_BASE
  4. #define VECTOR_BASE 0x00000000
  5. #endif
  6. #define PSW_MSK \
  7. (PSW_mskGIE | PSW_mskINTL | PSW_mskPOM | PSW_mskIFCON | PSW_mskCPL)
  8. #define PSW_INIT \
  9. (0x0UL << PSW_offGIE \
  10. | 0x0UL << PSW_offINTL \
  11. | 0x1UL << PSW_offPOM \
  12. | 0x0UL << PSW_offIFCON \
  13. | 0x7UL << PSW_offCPL)
  14. #define IVB_MSK \
  15. (IVB_mskEVIC | IVB_mskESZ | IVB_mskIVBASE)
  16. #define IVB_INIT \
  17. ((VECTOR_BASE >> IVB_offIVBASE) << IVB_offIVBASE\
  18. | 0x1UL << IVB_offESZ \
  19. | 0x0UL << IVB_offEVIC)
  20. #pragma weak c_startup = c_startup_common
  21. void c_startup(void);
  22. /*
  23. * Default c_startup() function which used for those relocation from LMA to VMA.
  24. */
  25. static void c_startup_common(void)
  26. {
  27. #ifdef XIP_MODE
  28. /* Data section initialization */
  29. #define MEMCPY(des, src, n) __builtin_memcpy ((des), (src), (n))
  30. extern char __rw_lma_start, __rw_lma_end, __rw_vma_start;
  31. unsigned int size = &__rw_lma_end - &__rw_lma_start;
  32. /* Copy data section from LMA to VMA */
  33. MEMCPY(&__rw_vma_start, &__rw_lma_start, size);
  34. #else
  35. /* We do nothing for those LMA equal to VMA */
  36. #endif
  37. }
  38. static void cpu_init(void)
  39. {
  40. unsigned int reg;
  41. /* Set PSW GIE/INTL to 0, superuser & CPL to 7 */
  42. reg = (__nds32__mfsr(NDS32_SR_PSW) & ~PSW_MSK) | PSW_INIT;
  43. __nds32__mtsr(reg, NDS32_SR_PSW);
  44. __nds32__isb();
  45. /* Set vector size: 16 byte, base: VECTOR_BASE, mode: IVIC */
  46. reg = (__nds32__mfsr(NDS32_SR_IVB) & ~IVB_MSK) | IVB_INIT;
  47. __nds32__mtsr(reg, NDS32_SR_IVB);
  48. /*
  49. * Check interrupt priority programmable (IVB.PROG_PRI_LVL)
  50. * 0: Fixed priority, 1: Programmable priority
  51. */
  52. if (reg & IVB_mskPROG_PRI_LVL)
  53. {
  54. /* Set PPL2FIX_EN to 0 to enable Programmable Priority Level */
  55. __nds32__mtsr(0x0, NDS32_SR_INT_CTRL);
  56. }
  57. /* Mask and clear hardware interrupts */
  58. if (reg & IVB_mskIVIC_VER)
  59. {
  60. /* IVB.IVIC_VER >= 1*/
  61. __nds32__mtsr(0x0, NDS32_SR_INT_MASK2);
  62. __nds32__mtsr(-1, NDS32_SR_INT_PEND2);
  63. }
  64. else
  65. {
  66. __nds32__mtsr(__nds32__mfsr(NDS32_SR_INT_MASK) & ~0xFFFF, NDS32_SR_INT_MASK);
  67. }
  68. }
  69. /*
  70. * Vectors initialization. This means to copy exception handler code to
  71. * vector entry base address.
  72. */
  73. static void vector_init(void)
  74. {
  75. extern unsigned int OS_Int_Vectors, OS_Int_Vectors_End;
  76. if ((unsigned int)&OS_Int_Vectors != VECTOR_BASE)
  77. {
  78. volatile unsigned int *vector_srcptr = &OS_Int_Vectors;
  79. volatile unsigned int *vector_dstptr = (unsigned int *)VECTOR_BASE;
  80. /* copy vector table to VECTOR_BASE */
  81. while (vector_srcptr != &OS_Int_Vectors_End)
  82. *vector_dstptr++ = *vector_srcptr++;
  83. }
  84. }
  85. /*
  86. * NDS32 reset handler to reset all devices sequentially and call application
  87. * entry function.
  88. */
  89. void reset(void)
  90. {
  91. extern void hardware_init(void);
  92. extern void bsp_init(void);
  93. extern void rtthread_startup(void);
  94. /*
  95. * Initialize CPU to a post-reset state, ensuring the ground doesn't
  96. * shift under us while we try to set things up.
  97. */
  98. cpu_init();
  99. /*
  100. * Initialize LMA/VMA sections.
  101. * Relocation for any sections that need to be copied from LMA to VMA.
  102. */
  103. c_startup();
  104. /* Copy vector table to vector base address */
  105. vector_init();
  106. /* Call platform specific hardware initialization */
  107. hardware_init();
  108. /* Setup the OS system required initialization */
  109. bsp_init();
  110. /* Application enrty function */
  111. rtthread_startup();
  112. /* Never go back here! */
  113. while(1);
  114. }