2 * crt0 - C-runtime startup Code for AArch64 U-Boot
5 * David Feng <fenghua@phytium.com.cn>
8 * Albert ARIBAUD <albert.u.boot@aribaud.net>
10 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm-offsets.h>
15 #include <asm/macro.h>
16 #include <linux/linkage.h>
19 * This file handles the target-independent stages of the U-Boot
20 * start-up where a C runtime environment is needed. Its entry point
21 * is _main and is branched into from the target's start.S file.
23 * _main execution sequence is:
25 * 1. Set up initial environment for calling board_init_f().
26 * This environment only provides a stack and a place to store
27 * the GD ('global data') structure, both located in some readily
28 * available RAM (SRAM, locked cache...). In this context, VARIABLE
29 * global data, initialized or not (BSS), are UNAVAILABLE; only
30 * CONSTANT initialized data are available. GD should be zeroed
31 * before board_init_f() is called.
33 * 2. Call board_init_f(). This function prepares the hardware for
34 * execution from system RAM (DRAM, DDR...) As system RAM may not
35 * be available yet, , board_init_f() must use the current GD to
36 * store any data which must be passed on to later stages. These
37 * data include the relocation destination, the future stack, and
38 * the future GD location.
40 * 3. Set up intermediate environment where the stack and GD are the
41 * ones allocated by board_init_f() in system RAM, but BSS and
42 * initialized non-const data are still not available.
44 * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
45 * relocates U-Boot from its current location into the relocation
46 * destination computed by board_init_f().
48 * 4b.For SPL, board_init_f() just returns (to crt0). There is no
49 * code relocation in SPL.
51 * 5. Set up final environment for calling board_init_r(). This
52 * environment has BSS (initialized to 0), initialized non-const
53 * data (initialized to their intended value), and stack in system
54 * RAM (for SPL moving the stack and GD into RAM is optional - see
55 * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
57 * TODO: For SPL, implement stack relocation on AArch64.
59 * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
60 * at this point regarding memory, so call c_runtime_cpu_setup.
62 * 7. Branch to board_init_r().
64 * For more information see 'Board Initialisation Flow in README.
70 * Set up initial C runtime environment and call board_init_f(0).
72 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
73 ldr x0, =(CONFIG_SPL_STACK)
75 ldr x0, =(CONFIG_SYS_INIT_SP_ADDR)
77 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
79 bl board_init_f_alloc_reserve
81 /* set up gd here, outside any C code */
83 bl board_init_f_init_reserve
88 #if !defined(CONFIG_SPL_BUILD)
90 * Set up intermediate environment (new sp and gd) and call
91 * relocate_code(addr_moni). Trick here is that we'll return
92 * 'here' but relocated.
94 ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */
95 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
96 ldr x18, [x18, #GD_BD] /* x18 <- gd->bd */
97 sub x18, x18, #GD_SIZE /* new GD is below bd */
99 adr lr, relocation_return
100 ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */
101 add lr, lr, x9 /* new return address after relocation */
102 ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */
108 * Set up final (full) environment
110 bl c_runtime_cpu_setup /* still call old routine */
111 #endif /* !CONFIG_SPL_BUILD */
113 /* TODO: For SPL, call spl_relocate_stack_gd() to alloc stack relocation */
118 ldr x0, =__bss_start /* this is auto-relocated! */
119 ldr x1, =__bss_end /* this is auto-relocated! */
127 /* call board_init_r(gd_t *id, ulong dest_addr) */
128 mov x0, x18 /* gd_t */
129 ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */
130 b board_init_r /* PC relative jump */
132 /* NOTREACHED - board_init_r() does not return */