crt0.S 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. GD should be zeroed
  27. * before board_init_f() is called.
  28. *
  29. * 2. Call board_init_f(). This function prepares the hardware for
  30. * execution from system RAM (DRAM, DDR...) As system RAM may not
  31. * be available yet, , board_init_f() must use the current GD to
  32. * store any data which must be passed on to later stages. These
  33. * data include the relocation destination, the future stack, and
  34. * the future GD location.
  35. *
  36. * 3. Set up intermediate environment where the stack and GD are the
  37. * ones allocated by board_init_f() in system RAM, but BSS and
  38. * initialized non-const data are still not available.
  39. *
  40. * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
  41. * relocates U-Boot from its current location into the relocation
  42. * destination computed by board_init_f().
  43. *
  44. * 4b.For SPL, board_init_f() just returns (to crt0). There is no
  45. * code relocation in SPL.
  46. *
  47. * 5. Set up final environment for calling board_init_r(). This
  48. * environment has BSS (initialized to 0), initialized non-const
  49. * data (initialized to their intended value), and stack in system
  50. * RAM (for SPL moving the stack and GD into RAM is optional - see
  51. * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
  52. *
  53. * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
  54. * at this point regarding memory, so call c_runtime_cpu_setup.
  55. *
  56. * 7. Branch to board_init_r().
  57. *
  58. * For more information see 'Board Initialisation Flow in README.
  59. */
  60. /*
  61. * entry point of crt0 sequence
  62. */
  63. ENTRY(_main)
  64. /*
  65. * Set up initial C runtime environment and call board_init_f(0).
  66. */
  67. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
  68. ldr sp, =(CONFIG_SPL_STACK)
  69. #else
  70. ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
  71. #endif
  72. #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */
  73. mov r3, sp
  74. bic r3, r3, #7
  75. mov sp, r3
  76. #else
  77. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  78. #endif
  79. mov r0, sp
  80. bl board_init_f_alloc_reserve
  81. mov sp, r0
  82. /* set up gd here, outside any C code */
  83. mov r9, r0
  84. bl board_init_f_init_reserve
  85. mov r0, #0
  86. bl board_init_f
  87. #if ! defined(CONFIG_SPL_BUILD)
  88. /*
  89. * Set up intermediate environment (new sp and gd) and call
  90. * relocate_code(addr_moni). Trick here is that we'll return
  91. * 'here' but relocated.
  92. */
  93. ldr sp, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
  94. #if defined(CONFIG_CPU_V7M) /* v7M forbids using SP as BIC destination */
  95. mov r3, sp
  96. bic r3, r3, #7
  97. mov sp, r3
  98. #else
  99. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  100. #endif
  101. ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
  102. sub r9, r9, #GD_SIZE /* new GD is below bd */
  103. adr lr, here
  104. ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
  105. add lr, lr, r0
  106. #if defined(CONFIG_CPU_V7M)
  107. orr lr, #1 /* As required by Thumb-only */
  108. #endif
  109. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  110. b relocate_code
  111. here:
  112. /*
  113. * now relocate vectors
  114. */
  115. bl relocate_vectors
  116. /* Set up final (full) environment */
  117. bl c_runtime_cpu_setup /* we still call old routine here */
  118. #endif
  119. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK)
  120. # ifdef CONFIG_SPL_BUILD
  121. /* Use a DRAM stack for the rest of SPL, if requested */
  122. bl spl_relocate_stack_gd
  123. cmp r0, #0
  124. movne sp, r0
  125. movne r9, r0
  126. # endif
  127. ldr r0, =__bss_start /* this is auto-relocated! */
  128. #ifdef CONFIG_USE_ARCH_MEMSET
  129. ldr r3, =__bss_end /* this is auto-relocated! */
  130. mov r1, #0x00000000 /* prepare zero to clear BSS */
  131. subs r2, r3, r0 /* r2 = memset len */
  132. bl memset
  133. #else
  134. ldr r1, =__bss_end /* this is auto-relocated! */
  135. mov r2, #0x00000000 /* prepare zero to clear BSS */
  136. clbss_l:cmp r0, r1 /* while not at end of BSS */
  137. #if defined(CONFIG_CPU_V7M)
  138. itt lo
  139. #endif
  140. strlo r2, [r0] /* clear 32-bit BSS word */
  141. addlo r0, r0, #4 /* move to next */
  142. blo clbss_l
  143. #endif
  144. #if ! defined(CONFIG_SPL_BUILD)
  145. bl coloured_LED_init
  146. bl red_led_on
  147. #endif
  148. /* call board_init_r(gd_t *id, ulong dest_addr) */
  149. mov r0, r9 /* gd_t */
  150. ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */
  151. /* call board_init_r */
  152. #if defined(CONFIG_SYS_THUMB_BUILD)
  153. ldr lr, =board_init_r /* this is auto-relocated! */
  154. bx lr
  155. #else
  156. ldr pc, =board_init_r /* this is auto-relocated! */
  157. #endif
  158. /* we should not return here. */
  159. #endif
  160. ENDPROC(_main)