blob: a181283e0fa9008162cfad63b5ddfd9adf98ed39 [file] [log] [blame]
David Feng0ae76532013-12-14 11:47:35 +08001/*
2 * crt0 - C-runtime startup Code for AArch64 U-Boot
3 *
4 * (C) Copyright 2013
5 * David Feng <fenghua@phytium.com.cn>
6 *
7 * (C) Copyright 2012
8 * Albert ARIBAUD <albert.u.boot@aribaud.net>
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13#include <config.h>
14#include <asm-offsets.h>
15#include <asm/macro.h>
16#include <linux/linkage.h>
17
18/*
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.
22 *
23 * _main execution sequence is:
24 *
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
Simon Glassed641902015-08-01 08:55:46 -060030 * CONSTANT initialized data are available. GD should be zeroed
31 * before board_init_f() is called.
David Feng0ae76532013-12-14 11:47:35 +080032 *
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.
39 *
David Feng0ae76532013-12-14 11:47:35 +080040 * 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.
43 *
Simon Glassed641902015-08-01 08:55:46 -060044 * 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().
47 *
48 * 4b.For SPL, board_init_f() just returns (to crt0). There is no
49 * code relocation in SPL.
David Feng0ae76532013-12-14 11:47:35 +080050 *
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
Simon Glassed641902015-08-01 08:55:46 -060054 * 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().
David Feng0ae76532013-12-14 11:47:35 +080056 *
Simon Glassed641902015-08-01 08:55:46 -060057 * TODO: For SPL, implement stack relocation on AArch64.
58 *
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.
61 *
62 * 7. Branch to board_init_r().
63 *
64 * For more information see 'Board Initialisation Flow in README.
David Feng0ae76532013-12-14 11:47:35 +080065 */
66
67ENTRY(_main)
68
69/*
70 * Set up initial C runtime environment and call board_init_f(0).
71 */
Philipp Tomsichfaa18db2017-07-28 20:04:09 +020072#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
Philipp Tomsichc3be6192017-07-04 11:02:14 +020073 ldr x0, =(CONFIG_TPL_STACK)
74#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
Scott Woodb2d5ac52015-03-24 13:25:02 -070075 ldr x0, =(CONFIG_SPL_STACK)
Stephen Warrene6c90442017-12-19 18:30:36 -070076#elif defined(CONFIG_SYS_INIT_SP_BSS_OFFSET)
77 adr x0, __bss_start
78 add x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET
Scott Woodb2d5ac52015-03-24 13:25:02 -070079#else
David Feng0ae76532013-12-14 11:47:35 +080080 ldr x0, =(CONFIG_SYS_INIT_SP_ADDR)
Scott Woodb2d5ac52015-03-24 13:25:02 -070081#endif
David Feng0ae76532013-12-14 11:47:35 +080082 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
Albert ARIBAUDecc30662015-11-25 17:56:32 +010083 mov x0, sp
84 bl board_init_f_alloc_reserve
Simon Glass931bec32015-10-19 06:49:59 -060085 mov sp, x0
Stephen Warrena7370282016-01-14 14:03:11 -070086 /* set up gd here, outside any C code */
87 mov x18, x0
Albert ARIBAUDecc30662015-11-25 17:56:32 +010088 bl board_init_f_init_reserve
Simon Glass931bec32015-10-19 06:49:59 -060089
David Feng0ae76532013-12-14 11:47:35 +080090 mov x0, #0
91 bl board_init_f
92
Scott Woodb2d5ac52015-03-24 13:25:02 -070093#if !defined(CONFIG_SPL_BUILD)
David Feng0ae76532013-12-14 11:47:35 +080094/*
95 * Set up intermediate environment (new sp and gd) and call
96 * relocate_code(addr_moni). Trick here is that we'll return
97 * 'here' but relocated.
98 */
99 ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */
100 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
zijun_hu01a83592017-09-22 14:39:13 +0800101 ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */
David Feng0ae76532013-12-14 11:47:35 +0800102
103 adr lr, relocation_return
Stephen Warren49e93872017-11-02 18:11:27 -0600104#if CONFIG_POSITION_INDEPENDENT
105 /* Add in link-vs-runtime offset */
106 adr x0, _start /* x0 <- Runtime value of _start */
107 ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */
108 sub x9, x9, x0 /* x9 <- Run-vs-link offset */
109 add lr, lr, x9
110#endif
111 /* Add in link-vs-relocation offset */
David Feng0ae76532013-12-14 11:47:35 +0800112 ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */
113 add lr, lr, x9 /* new return address after relocation */
114 ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */
115 b relocate_code
116
117relocation_return:
118
119/*
120 * Set up final (full) environment
121 */
122 bl c_runtime_cpu_setup /* still call old routine */
Jeremy Huntb8cb51d2016-07-18 12:01:02 -0400123#endif /* !CONFIG_SPL_BUILD */
Philipp Tomsich7a70c992017-03-01 21:04:15 +0100124#if defined(CONFIG_SPL_BUILD)
125 bl spl_relocate_stack_gd /* may return NULL */
York Sune421b642017-12-07 13:16:07 -0800126 /* set up gd here, outside any C code, if new stack is returned */
127 cmp x0, #0
128 csel x18, x0, x18, ne
Philipp Tomsich7a70c992017-03-01 21:04:15 +0100129 /*
130 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
131 * around the constraint that conditional moves can not
132 * have 'sp' as an operand
133 */
134 mov x1, sp
135 cmp x0, #0
136 csel x0, x0, x1, ne
137 mov sp, x0
138#endif
Simon Glassed641902015-08-01 08:55:46 -0600139
David Feng0ae76532013-12-14 11:47:35 +0800140/*
141 * Clear BSS section
142 */
143 ldr x0, =__bss_start /* this is auto-relocated! */
144 ldr x1, =__bss_end /* this is auto-relocated! */
David Feng0ae76532013-12-14 11:47:35 +0800145clear_loop:
Masahiro Yamada07a63c72017-01-27 16:15:30 +0900146 str xzr, [x0], #8
David Feng0ae76532013-12-14 11:47:35 +0800147 cmp x0, x1
148 b.lo clear_loop
149
150 /* call board_init_r(gd_t *id, ulong dest_addr) */
151 mov x0, x18 /* gd_t */
152 ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */
153 b board_init_r /* PC relative jump */
154
155 /* NOTREACHED - board_init_r() does not return */
156
David Feng0ae76532013-12-14 11:47:35 +0800157ENDPROC(_main)