crt0.S 5.3 KB

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