xref: /rk3399_rockchip-uboot/arch/arm/lib/crt0_64.S (revision e55dfbd47140353ad2ac122e706d44b699c8162a)
1/*
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 <asm/system.h>
17#include <linux/linkage.h>
18
19/*
20 * This file handles the target-independent stages of the U-Boot
21 * start-up where a C runtime environment is needed. Its entry point
22 * is _main and is branched into from the target's start.S file.
23 *
24 * _main execution sequence is:
25 *
26 * 1. Set up initial environment for calling board_init_f().
27 *    This environment only provides a stack and a place to store
28 *    the GD ('global data') structure, both located in some readily
29 *    available RAM (SRAM, locked cache...). In this context, VARIABLE
30 *    global data, initialized or not (BSS), are UNAVAILABLE; only
31 *    CONSTANT initialized data are available. GD should be zeroed
32 *    before board_init_f() is called.
33 *
34 * 2. Call board_init_f(). This function prepares the hardware for
35 *    execution from system RAM (DRAM, DDR...) As system RAM may not
36 *    be available yet, , board_init_f() must use the current GD to
37 *    store any data which must be passed on to later stages. These
38 *    data include the relocation destination, the future stack, and
39 *    the future GD location.
40 *
41 * 3. Set up intermediate environment where the stack and GD are the
42 *    ones allocated by board_init_f() in system RAM, but BSS and
43 *    initialized non-const data are still not available.
44 *
45 * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
46 *    relocates U-Boot from its current location into the relocation
47 *    destination computed by board_init_f().
48 *
49 * 4b.For SPL, board_init_f() just returns (to crt0). There is no
50 *    code relocation in SPL.
51 *
52 * 5. Set up final environment for calling board_init_r(). This
53 *    environment has BSS (initialized to 0), initialized non-const
54 *    data (initialized to their intended value), and stack in system
55 *    RAM (for SPL moving the stack and GD into RAM is optional - see
56 *    CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
57 *
58 * TODO: For SPL, implement stack relocation on AArch64.
59 *
60 * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
61 *    at this point regarding memory, so call c_runtime_cpu_setup.
62 *
63 * 7. Branch to board_init_r().
64 *
65 * For more information see 'Board Initialisation Flow in README.
66 */
67
68ENTRY(_main)
69	/*
70	 * Enable instruction cache (if required), stack pointer,
71	 * data access alignment checks and SError.
72	 */
73#ifdef CONFIG_SPL_BUILD
74	mov x1, #CR_I
75#else
76	mov x1, #0
77#endif
78	switch_el x2, 3f, 2f, 1f
793:	mrs	x0, sctlr_el3
80	orr	x0, x0, x1
81	msr	sctlr_el3, x0
82#ifndef CONFIG_SUPPORT_USBPLUG
83	msr	daifclr, #4			/* Enable SError. SCR_EL3.EA=1 was already set in start.S */
84#endif
85	b	0f
862:	mrs	x0, sctlr_el2
87	orr	x0, x0, x1
88	msr	sctlr_el2, x0
89
90	mrs	x0, hcr_el2
91	orr	x0, x0, #HCR_EL2_TGE
92	orr	x0, x0, #HCR_EL2_AMO
93#if CONFIG_IS_ENABLED(IRQ)
94	orr	x0, x0, #HCR_EL2_IMO
95#endif
96	msr	hcr_el2, x0
97	msr	daifclr, #4
98	b	0f
991:	mrs	x0, sctlr_el1
100	orr	x0, x0, x1
101	msr	sctlr_el1, x0
102	msr	daifclr, #4
1030:
104	isb
105
106/*
107 * Set up initial C runtime environment and call board_init_f(0).
108 */
109#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
110	ldr	x0, =(CONFIG_TPL_STACK)
111#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
112	ldr	x0, =(CONFIG_SPL_STACK)
113#else
114	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
115#endif
116	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
117	mov	x0, sp
118	bl	board_init_f_alloc_reserve
119	mov	sp, x0
120	/* set up gd here, outside any C code */
121	mov	x18, x0
122	bl	board_init_f_init_reserve
123	bl	board_init_f_boot_flags
124
125	bl	board_init_f
126
127#if (defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_SKIP_RELOCATE)) || \
128	!defined(CONFIG_SPL_BUILD)
129/*
130 * Set up intermediate environment (new sp and gd) and call
131 * relocate_code(addr_moni). Trick here is that we'll return
132 * 'here' but relocated.
133 */
134	ldr	x0, [x18, #GD_START_ADDR_SP]	/* x0 <- gd->start_addr_sp */
135	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
136	ldr	x18, [x18, #GD_NEW_GD]		/* x18 <- gd->new_gd */
137
138#ifndef CONFIG_SKIP_RELOCATE_UBOOT
139	adr	lr, relocation_return
140#if CONFIG_POSITION_INDEPENDENT
141	/* Add in link-vs-runtime offset */
142	adr	x0, _start		/* x0 <- Runtime value of _start */
143	ldr	x9, _TEXT_BASE		/* x9 <- Linked value of _start */
144	sub	x9, x9, x0		/* x9 <- Run-vs-link offset */
145	add	lr, lr, x9
146#endif
147	/* Add in link-vs-relocation offset */
148	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
149	add	lr, lr, x9	/* new return address after relocation */
150	ldr	x0, [x18, #GD_RELOCADDR]	/* x0 <- gd->relocaddr */
151	b	relocate_code
152#endif
153
154relocation_return:
155
156/*
157 * Set up final (full) environment
158 */
159	bl	c_runtime_cpu_setup		/* still call old routine */
160#endif /* !CONFIG_SPL_BUILD */
161#if defined(CONFIG_SPL_BUILD)
162	bl	spl_relocate_stack_gd           /* may return NULL */
163	/* set up gd here, outside any C code, if new stack is returned */
164	cmp	x0, #0
165	csel	x18, x0, x18, ne
166	/*
167	 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
168	 * around the constraint that conditional moves can not
169	 * have 'sp' as an operand
170	 */
171	mov	x1, sp
172	cmp	x0, #0
173	csel	x0, x0, x1, ne
174	mov	sp, x0
175#endif
176
177/*
178 * Clear BSS section
179 */
180	ldr	x0, =__bss_start		/* this is auto-relocated! */
181	ldr	x1, =__bss_end			/* this is auto-relocated! */
182clear_loop:
183	str	xzr, [x0], #8
184	cmp	x0, x1
185	b.lo	clear_loop
186
187	/* call board_init_r(gd_t *id, ulong dest_addr) */
188	mov	x0, x18				/* gd_t */
189	ldr	x1, [x18, #GD_RELOCADDR]	/* dest_addr */
190	b	board_init_r			/* PC relative jump */
191
192	/* NOTREACHED - board_init_r() does not return */
193
194ENDPROC(_main)
195