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