crt0.S 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 r0, =(CONFIG_SPL_STACK)
  69. #else
  70. ldr r0, =(CONFIG_SYS_INIT_SP_ADDR)
  71. #endif
  72. bic r0, r0, #7 /* 8-byte alignment for ABI compliance */
  73. mov sp, r0
  74. bl board_init_f_alloc_reserve
  75. mov sp, r0
  76. /* set up gd here, outside any C code */
  77. mov r9, r0
  78. bl board_init_f_init_reserve
  79. mov r0, #0
  80. bl board_init_f
  81. #if ! defined(CONFIG_SPL_BUILD)
  82. /*
  83. * Set up intermediate environment (new sp and gd) and call
  84. * relocate_code(addr_moni). Trick here is that we'll return
  85. * 'here' but relocated.
  86. */
  87. ldr r0, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
  88. bic r0, r0, #7 /* 8-byte alignment for ABI compliance */
  89. mov sp, r0
  90. ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
  91. sub r9, r9, #GD_SIZE /* new GD is below bd */
  92. adr lr, here
  93. ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
  94. add lr, lr, r0
  95. #if defined(CONFIG_CPU_V7M)
  96. orr lr, #1 /* As required by Thumb-only */
  97. #endif
  98. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  99. b relocate_code
  100. here:
  101. /*
  102. * now relocate vectors
  103. */
  104. bl relocate_vectors
  105. /* Set up final (full) environment */
  106. bl c_runtime_cpu_setup /* we still call old routine here */
  107. #endif
  108. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK)
  109. # ifdef CONFIG_SPL_BUILD
  110. /* Use a DRAM stack for the rest of SPL, if requested */
  111. bl spl_relocate_stack_gd
  112. cmp r0, #0
  113. movne sp, r0
  114. movne r9, r0
  115. # endif
  116. ldr r0, =__bss_start /* this is auto-relocated! */
  117. #ifdef CONFIG_USE_ARCH_MEMSET
  118. ldr r3, =__bss_end /* this is auto-relocated! */
  119. mov r1, #0x00000000 /* prepare zero to clear BSS */
  120. subs r2, r3, r0 /* r2 = memset len */
  121. bl memset
  122. #else
  123. ldr r1, =__bss_end /* this is auto-relocated! */
  124. mov r2, #0x00000000 /* prepare zero to clear BSS */
  125. clbss_l:cmp r0, r1 /* while not at end of BSS */
  126. #if defined(CONFIG_CPU_V7M)
  127. itt lo
  128. #endif
  129. strlo r2, [r0] /* clear 32-bit BSS word */
  130. addlo r0, r0, #4 /* move to next */
  131. blo clbss_l
  132. #endif
  133. #if ! defined(CONFIG_SPL_BUILD)
  134. bl coloured_LED_init
  135. bl red_led_on
  136. #endif
  137. /* call board_init_r(gd_t *id, ulong dest_addr) */
  138. mov r0, r9 /* gd_t */
  139. ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */
  140. /* call board_init_r */
  141. #if CONFIG_IS_ENABLED(SYS_THUMB_BUILD)
  142. ldr lr, =board_init_r /* this is auto-relocated! */
  143. bx lr
  144. #else
  145. ldr pc, =board_init_r /* this is auto-relocated! */
  146. #endif
  147. /* we should not return here. */
  148. #endif
  149. ENDPROC(_main)