xref: /rk3399_rockchip-uboot/arch/arm/lib/crt0_64.S (revision 3a1a9b814dddc7e73dce113623f72dde032d7bd5)
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_A | CR_SA | CR_I)
75#else
76	mov x1, #(CR_A | CR_SA)
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	msr	daifclr, #4			/* SCR_EL3.EA=1 was already set in start.S */
83	b	0f
842:	mrs	x0, sctlr_el2
85	orr	x0, x0, x1
86	msr	sctlr_el2, x0
87	b	0f
881:	mrs	x0, sctlr_el1
89	orr	x0, x0, x1
90	msr	sctlr_el1, x0
910:
92	isb
93
94/*
95 * Set up initial C runtime environment and call board_init_f(0).
96 */
97#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
98	ldr	x0, =(CONFIG_TPL_STACK)
99#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
100	ldr	x0, =(CONFIG_SPL_STACK)
101#else
102	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
103#endif
104	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
105	mov	x0, sp
106	bl	board_init_f_alloc_reserve
107	mov	sp, x0
108	/* set up gd here, outside any C code */
109	mov	x18, x0
110	bl	board_init_f_init_reserve
111	bl	board_init_f_boot_flags
112
113	bl	board_init_f
114
115#if (defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_SKIP_RELOCATE)) || \
116	!defined(CONFIG_SPL_BUILD)
117/*
118 * Set up intermediate environment (new sp and gd) and call
119 * relocate_code(addr_moni). Trick here is that we'll return
120 * 'here' but relocated.
121 */
122	ldr	x0, [x18, #GD_START_ADDR_SP]	/* x0 <- gd->start_addr_sp */
123	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
124	ldr	x18, [x18, #GD_NEW_GD]		/* x18 <- gd->new_gd */
125
126#ifndef CONFIG_SKIP_RELOCATE_UBOOT
127	adr	lr, relocation_return
128#if CONFIG_POSITION_INDEPENDENT
129	/* Add in link-vs-runtime offset */
130	adr	x0, _start		/* x0 <- Runtime value of _start */
131	ldr	x9, _TEXT_BASE		/* x9 <- Linked value of _start */
132	sub	x9, x9, x0		/* x9 <- Run-vs-link offset */
133	add	lr, lr, x9
134#endif
135	/* Add in link-vs-relocation offset */
136	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
137	add	lr, lr, x9	/* new return address after relocation */
138	ldr	x0, [x18, #GD_RELOCADDR]	/* x0 <- gd->relocaddr */
139	b	relocate_code
140#endif
141
142relocation_return:
143
144/*
145 * Set up final (full) environment
146 */
147	bl	c_runtime_cpu_setup		/* still call old routine */
148#endif /* !CONFIG_SPL_BUILD */
149#if defined(CONFIG_SPL_BUILD)
150	bl	spl_relocate_stack_gd           /* may return NULL */
151	/* set up gd here, outside any C code, if new stack is returned */
152	cmp	x0, #0
153	csel	x18, x0, x18, ne
154	/*
155	 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
156	 * around the constraint that conditional moves can not
157	 * have 'sp' as an operand
158	 */
159	mov	x1, sp
160	cmp	x0, #0
161	csel	x0, x0, x1, ne
162	mov	sp, x0
163#endif
164
165/*
166 * Clear BSS section
167 */
168	ldr	x0, =__bss_start		/* this is auto-relocated! */
169	ldr	x1, =__bss_end			/* this is auto-relocated! */
170clear_loop:
171	str	xzr, [x0], #8
172	cmp	x0, x1
173	b.lo	clear_loop
174
175	/* call board_init_r(gd_t *id, ulong dest_addr) */
176	mov	x0, x18				/* gd_t */
177	ldr	x1, [x18, #GD_RELOCADDR]	/* dest_addr */
178	b	board_init_r			/* PC relative jump */
179
180	/* NOTREACHED - board_init_r() does not return */
181
182ENDPROC(_main)
183