blob: 84c04bd43a2db565154cea2fb79f8f48b11baaba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
David Feng85fd5f12013-12-14 11:47:35 +08002/*
3 * crt0 - C-runtime startup Code for AArch64 U-Boot
4 *
5 * (C) Copyright 2013
6 * David Feng <fenghua@phytium.com.cn>
7 *
8 * (C) Copyright 2012
9 * Albert ARIBAUD <albert.u.boot@aribaud.net>
David Feng85fd5f12013-12-14 11:47:35 +080010 */
11
12#include <config.h>
13#include <asm-offsets.h>
14#include <asm/macro.h>
15#include <linux/linkage.h>
16
17/*
18 * This file handles the target-independent stages of the U-Boot
19 * start-up where a C runtime environment is needed. Its entry point
20 * is _main and is branched into from the target's start.S file.
21 *
22 * _main execution sequence is:
23 *
24 * 1. Set up initial environment for calling board_init_f().
25 * This environment only provides a stack and a place to store
26 * the GD ('global data') structure, both located in some readily
27 * available RAM (SRAM, locked cache...). In this context, VARIABLE
28 * global data, initialized or not (BSS), are UNAVAILABLE; only
Simon Glasscb60ae82015-08-01 08:55:46 -060029 * CONSTANT initialized data are available. GD should be zeroed
30 * before board_init_f() is called.
David Feng85fd5f12013-12-14 11:47:35 +080031 *
32 * 2. Call board_init_f(). This function prepares the hardware for
33 * execution from system RAM (DRAM, DDR...) As system RAM may not
34 * be available yet, , board_init_f() must use the current GD to
35 * store any data which must be passed on to later stages. These
36 * data include the relocation destination, the future stack, and
37 * the future GD location.
38 *
David Feng85fd5f12013-12-14 11:47:35 +080039 * 3. Set up intermediate environment where the stack and GD are the
40 * ones allocated by board_init_f() in system RAM, but BSS and
41 * initialized non-const data are still not available.
42 *
Simon Glasscb60ae82015-08-01 08:55:46 -060043 * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
44 * relocates U-Boot from its current location into the relocation
45 * destination computed by board_init_f().
46 *
47 * 4b.For SPL, board_init_f() just returns (to crt0). There is no
48 * code relocation in SPL.
David Feng85fd5f12013-12-14 11:47:35 +080049 *
50 * 5. Set up final environment for calling board_init_r(). This
51 * environment has BSS (initialized to 0), initialized non-const
52 * data (initialized to their intended value), and stack in system
Simon Glasscb60ae82015-08-01 08:55:46 -060053 * RAM (for SPL moving the stack and GD into RAM is optional - see
54 * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
55 *
56 * TODO: For SPL, implement stack relocation on AArch64.
David Feng85fd5f12013-12-14 11:47:35 +080057 *
Simon Glasscb60ae82015-08-01 08:55:46 -060058 * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
59 * at this point regarding memory, so call c_runtime_cpu_setup.
60 *
61 * 7. Branch to board_init_r().
62 *
63 * For more information see 'Board Initialisation Flow in README.
David Feng85fd5f12013-12-14 11:47:35 +080064 */
65
66ENTRY(_main)
67
68/*
69 * Set up initial C runtime environment and call board_init_f(0).
70 */
Philipp Tomsich44aa8302017-07-28 20:04:09 +020071#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
Philipp Tomsichd71b9012017-07-04 11:02:14 +020072 ldr x0, =(CONFIG_TPL_STACK)
73#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
Scott Wood8e728cd2015-03-24 13:25:02 -070074 ldr x0, =(CONFIG_SPL_STACK)
Masahiro Yamadabf4645c2019-06-26 13:51:46 +090075#elif defined(CONFIG_INIT_SP_RELATIVE)
Edgar E. Iglesias545474e2020-09-09 19:07:26 +020076#if CONFIG_POSITION_INDEPENDENT
77 adrp x0, __bss_start /* x0 <- Runtime &__bss_start */
78 add x0, x0, #:lo12:__bss_start
79#else
Stephen Warrenb80fe6d2017-12-19 18:30:36 -070080 adr x0, __bss_start
Edgar E. Iglesias545474e2020-09-09 19:07:26 +020081#endif
Stephen Warrenb80fe6d2017-12-19 18:30:36 -070082 add x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET
Scott Wood8e728cd2015-03-24 13:25:02 -070083#else
David Feng85fd5f12013-12-14 11:47:35 +080084 ldr x0, =(CONFIG_SYS_INIT_SP_ADDR)
Scott Wood8e728cd2015-03-24 13:25:02 -070085#endif
David Feng85fd5f12013-12-14 11:47:35 +080086 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010087 mov x0, sp
88 bl board_init_f_alloc_reserve
Simon Glass4f456512015-10-19 06:49:59 -060089 mov sp, x0
Stephen Warren159c0da2016-01-14 14:03:11 -070090 /* set up gd here, outside any C code */
91 mov x18, x0
Albert ARIBAUD6cb4c462015-11-25 17:56:32 +010092 bl board_init_f_init_reserve
Simon Glass4f456512015-10-19 06:49:59 -060093
Simon Glass1ed9bf92021-11-03 07:16:06 -060094#if defined(CONFIG_DEBUG_UART) && CONFIG_IS_ENABLED(SERIAL)
95 bl debug_uart_init
96#endif
97
David Feng85fd5f12013-12-14 11:47:35 +080098 mov x0, #0
99 bl board_init_f
100
Scott Wood8e728cd2015-03-24 13:25:02 -0700101#if !defined(CONFIG_SPL_BUILD)
David Feng85fd5f12013-12-14 11:47:35 +0800102/*
103 * Set up intermediate environment (new sp and gd) and call
104 * relocate_code(addr_moni). Trick here is that we'll return
105 * 'here' but relocated.
106 */
107 ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */
108 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
zijun_hufc4741f2017-09-22 14:39:13 +0800109 ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */
David Feng85fd5f12013-12-14 11:47:35 +0800110
Marek Vasut9ecb2662021-11-13 18:34:59 +0100111 /* Skip relocation in case gd->gd_flags & GD_FLG_SKIP_RELOC */
112 ldr x0, [x18, #GD_FLAGS] /* x0 <- gd->flags */
113 tbnz x0, 11, relocation_return /* GD_FLG_SKIP_RELOC is bit 11 */
114
David Feng85fd5f12013-12-14 11:47:35 +0800115 adr lr, relocation_return
Stephen Warren81c21372017-11-02 18:11:27 -0600116#if CONFIG_POSITION_INDEPENDENT
117 /* Add in link-vs-runtime offset */
Edgar E. Iglesias545474e2020-09-09 19:07:26 +0200118 adrp x0, _start /* x0 <- Runtime value of _start */
119 add x0, x0, #:lo12:_start
Stephen Warren81c21372017-11-02 18:11:27 -0600120 ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */
121 sub x9, x9, x0 /* x9 <- Run-vs-link offset */
122 add lr, lr, x9
Kunihiko Hayashia11ee782021-06-15 15:33:02 +0900123#if defined(CONFIG_SYS_RELOC_GD_ENV_ADDR)
124 ldr x0, [x18, #GD_ENV_ADDR] /* x0 <- gd->env_addr */
125 add x0, x0, x9
126 str x0, [x18, #GD_ENV_ADDR]
127#endif
Stephen Warren81c21372017-11-02 18:11:27 -0600128#endif
129 /* Add in link-vs-relocation offset */
David Feng85fd5f12013-12-14 11:47:35 +0800130 ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */
131 add lr, lr, x9 /* new return address after relocation */
132 ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */
133 b relocate_code
134
135relocation_return:
136
137/*
138 * Set up final (full) environment
139 */
140 bl c_runtime_cpu_setup /* still call old routine */
Jeremy Hunt6402f722016-07-18 12:01:02 -0400141#endif /* !CONFIG_SPL_BUILD */
Heiko Stuebner4a0da7c2019-07-16 10:12:02 +0200142#if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(FRAMEWORK)
Philipp Tomsich7d50c292017-03-01 21:04:15 +0100143#if defined(CONFIG_SPL_BUILD)
144 bl spl_relocate_stack_gd /* may return NULL */
York Sunb400f6e2017-12-07 13:16:07 -0800145 /* set up gd here, outside any C code, if new stack is returned */
146 cmp x0, #0
147 csel x18, x0, x18, ne
Philipp Tomsich7d50c292017-03-01 21:04:15 +0100148 /*
149 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
150 * around the constraint that conditional moves can not
151 * have 'sp' as an operand
152 */
153 mov x1, sp
154 cmp x0, #0
155 csel x0, x0, x1, ne
156 mov sp, x0
157#endif
Simon Glasscb60ae82015-08-01 08:55:46 -0600158
David Feng85fd5f12013-12-14 11:47:35 +0800159/*
160 * Clear BSS section
161 */
162 ldr x0, =__bss_start /* this is auto-relocated! */
163 ldr x1, =__bss_end /* this is auto-relocated! */
David Feng85fd5f12013-12-14 11:47:35 +0800164clear_loop:
Masahiro Yamada48042d82017-01-27 16:15:30 +0900165 str xzr, [x0], #8
David Feng85fd5f12013-12-14 11:47:35 +0800166 cmp x0, x1
167 b.lo clear_loop
168
169 /* call board_init_r(gd_t *id, ulong dest_addr) */
170 mov x0, x18 /* gd_t */
171 ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */
172 b board_init_r /* PC relative jump */
173
174 /* NOTREACHED - board_init_r() does not return */
Heiko Stuebner4a0da7c2019-07-16 10:12:02 +0200175#endif
David Feng85fd5f12013-12-14 11:47:35 +0800176
David Feng85fd5f12013-12-14 11:47:35 +0800177ENDPROC(_main)