crt0.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * crt0 - C-runtime startup Code for ARM U-Boot
  3. *
  4. * Copyright (c) 2012 Albert ARIBAUD <albert.u.boot@aribaud.net>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <asm-offsets.h>
  10. #include <linux/linkage.h>
  11. #ifdef CONFIG_CPU_V7M
  12. #include <asm/armv7m.h>
  13. #endif
  14. /*
  15. * This file handles the target-independent stages of the U-Boot
  16. * start-up where a C runtime environment is needed. Its entry point
  17. * is _main and is branched into from the target's start.S file.
  18. *
  19. * _main execution sequence is:
  20. *
  21. * 1. Set up initial environment for calling board_init_f().
  22. * This environment only provides a stack and a place to store
  23. * the GD ('global data') structure, both located in some readily
  24. * available RAM (SRAM, locked cache...). In this context, VARIABLE
  25. * global data, initialized or not (BSS), are UNAVAILABLE; only
  26. * CONSTANT initialized data are available.
  27. *
  28. * 2. Call board_init_f(). This function prepares the hardware for
  29. * execution from system RAM (DRAM, DDR...) As system RAM may not
  30. * be available yet, , board_init_f() must use the current GD to
  31. * store any data which must be passed on to later stages. These
  32. * data include the relocation destination, the future stack, and
  33. * the future GD location.
  34. *
  35. * (the following applies only to non-SPL builds)
  36. *
  37. * 3. Set up intermediate environment where the stack and GD are the
  38. * ones allocated by board_init_f() in system RAM, but BSS and
  39. * initialized non-const data are still not available.
  40. *
  41. * 4. Call relocate_code(). This function relocates U-Boot from its
  42. * current location into the relocation destination computed by
  43. * board_init_f().
  44. *
  45. * 5. Set up final environment for calling board_init_r(). This
  46. * environment has BSS (initialized to 0), initialized non-const
  47. * data (initialized to their intended value), and stack in system
  48. * RAM. GD has retained values set by board_init_f(). Some CPUs
  49. * have some work left to do at this point regarding memory, so
  50. * call c_runtime_cpu_setup.
  51. *
  52. * 6. Branch to board_init_r().
  53. */
  54. /*
  55. * entry point of crt0 sequence
  56. */
  57. ENTRY(_main)
  58. /*
  59. * Set up initial C runtime environment and call board_init_f(0).
  60. */
  61. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
  62. ldr sp, =(CONFIG_SPL_STACK)
  63. #else
  64. ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
  65. #endif
  66. #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */
  67. mov r3, sp
  68. bic r3, r3, #7
  69. mov sp, r3
  70. #else
  71. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  72. #endif
  73. mov r2, sp
  74. sub sp, sp, #GD_SIZE /* allocate one GD above SP */
  75. #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */
  76. mov r3, sp
  77. bic r3, r3, #7
  78. mov sp, r3
  79. #else
  80. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  81. #endif
  82. mov r9, sp /* GD is above SP */
  83. mov r1, sp
  84. mov r0, #0
  85. clr_gd:
  86. cmp r1, r2 /* while not at end of GD */
  87. #if defined(CONFIG_CPU_V7M)
  88. itt lo
  89. #endif
  90. strlo r0, [r1] /* clear 32-bit GD word */
  91. addlo r1, r1, #4 /* move to next */
  92. blo clr_gd
  93. #if defined(CONFIG_SYS_MALLOC_F_LEN)
  94. sub sp, sp, #CONFIG_SYS_MALLOC_F_LEN
  95. str sp, [r9, #GD_MALLOC_BASE]
  96. #endif
  97. /* mov r0, #0 not needed due to above code */
  98. bl board_init_f
  99. #if ! defined(CONFIG_SPL_BUILD)
  100. /*
  101. * Set up intermediate environment (new sp and gd) and call
  102. * relocate_code(addr_moni). Trick here is that we'll return
  103. * 'here' but relocated.
  104. */
  105. ldr sp, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
  106. #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */
  107. mov r3, sp
  108. bic r3, r3, #7
  109. mov sp, r3
  110. #else
  111. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  112. #endif
  113. ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
  114. sub r9, r9, #GD_SIZE /* new GD is below bd */
  115. adr lr, here
  116. ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
  117. add lr, lr, r0
  118. #if defined(CONFIG_CPU_V7M)
  119. orr lr, #1 /* As required by Thumb-only */
  120. #endif
  121. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  122. b relocate_code
  123. here:
  124. /*
  125. * now relocate vectors
  126. */
  127. bl relocate_vectors
  128. /* Set up final (full) environment */
  129. bl c_runtime_cpu_setup /* we still call old routine here */
  130. #endif
  131. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK)
  132. # ifdef CONFIG_SPL_BUILD
  133. /* Use a DRAM stack for the rest of SPL, if requested */
  134. bl spl_relocate_stack_gd
  135. cmp r0, #0
  136. movne sp, r0
  137. # endif
  138. ldr r0, =__bss_start /* this is auto-relocated! */
  139. #ifdef CONFIG_USE_ARCH_MEMSET
  140. ldr r3, =__bss_end /* this is auto-relocated! */
  141. mov r1, #0x00000000 /* prepare zero to clear BSS */
  142. subs r2, r3, r0 /* r2 = memset len */
  143. bl memset
  144. #else
  145. ldr r1, =__bss_end /* this is auto-relocated! */
  146. mov r2, #0x00000000 /* prepare zero to clear BSS */
  147. clbss_l:cmp r0, r1 /* while not at end of BSS */
  148. #if defined(CONFIG_CPU_V7M)
  149. itt lo
  150. #endif
  151. strlo r2, [r0] /* clear 32-bit BSS word */
  152. addlo r0, r0, #4 /* move to next */
  153. blo clbss_l
  154. #endif
  155. #if ! defined(CONFIG_SPL_BUILD)
  156. bl coloured_LED_init
  157. bl red_led_on
  158. #endif
  159. /* call board_init_r(gd_t *id, ulong dest_addr) */
  160. mov r0, r9 /* gd_t */
  161. ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */
  162. /* call board_init_r */
  163. ldr pc, =board_init_r /* this is auto-relocated! */
  164. /* we should not return here. */
  165. #endif
  166. ENDPROC(_main)