xref: /rk3399_rockchip-uboot/arch/arm/lib/crt0_64.S (revision 618713d1f2e2bca0ca94522aa5a34b2806d2dfe0)
10ae76531SDavid Feng/*
20ae76531SDavid Feng * crt0 - C-runtime startup Code for AArch64 U-Boot
30ae76531SDavid Feng *
40ae76531SDavid Feng * (C) Copyright 2013
50ae76531SDavid Feng * David Feng <fenghua@phytium.com.cn>
60ae76531SDavid Feng *
70ae76531SDavid Feng * (C) Copyright 2012
80ae76531SDavid Feng * Albert ARIBAUD <albert.u.boot@aribaud.net>
90ae76531SDavid Feng *
100ae76531SDavid Feng * SPDX-License-Identifier:	GPL-2.0+
110ae76531SDavid Feng */
120ae76531SDavid Feng
130ae76531SDavid Feng#include <config.h>
140ae76531SDavid Feng#include <asm-offsets.h>
150ae76531SDavid Feng#include <asm/macro.h>
160ae76531SDavid Feng#include <linux/linkage.h>
170ae76531SDavid Feng
180ae76531SDavid Feng/*
190ae76531SDavid Feng * This file handles the target-independent stages of the U-Boot
200ae76531SDavid Feng * start-up where a C runtime environment is needed. Its entry point
210ae76531SDavid Feng * is _main and is branched into from the target's start.S file.
220ae76531SDavid Feng *
230ae76531SDavid Feng * _main execution sequence is:
240ae76531SDavid Feng *
250ae76531SDavid Feng * 1. Set up initial environment for calling board_init_f().
260ae76531SDavid Feng *    This environment only provides a stack and a place to store
270ae76531SDavid Feng *    the GD ('global data') structure, both located in some readily
280ae76531SDavid Feng *    available RAM (SRAM, locked cache...). In this context, VARIABLE
290ae76531SDavid Feng *    global data, initialized or not (BSS), are UNAVAILABLE; only
30ed64190fSSimon Glass *    CONSTANT initialized data are available. GD should be zeroed
31ed64190fSSimon Glass *    before board_init_f() is called.
320ae76531SDavid Feng *
330ae76531SDavid Feng * 2. Call board_init_f(). This function prepares the hardware for
340ae76531SDavid Feng *    execution from system RAM (DRAM, DDR...) As system RAM may not
350ae76531SDavid Feng *    be available yet, , board_init_f() must use the current GD to
360ae76531SDavid Feng *    store any data which must be passed on to later stages. These
370ae76531SDavid Feng *    data include the relocation destination, the future stack, and
380ae76531SDavid Feng *    the future GD location.
390ae76531SDavid Feng *
400ae76531SDavid Feng * 3. Set up intermediate environment where the stack and GD are the
410ae76531SDavid Feng *    ones allocated by board_init_f() in system RAM, but BSS and
420ae76531SDavid Feng *    initialized non-const data are still not available.
430ae76531SDavid Feng *
44ed64190fSSimon Glass * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
45ed64190fSSimon Glass *    relocates U-Boot from its current location into the relocation
46ed64190fSSimon Glass *    destination computed by board_init_f().
47ed64190fSSimon Glass *
48ed64190fSSimon Glass * 4b.For SPL, board_init_f() just returns (to crt0). There is no
49ed64190fSSimon Glass *    code relocation in SPL.
500ae76531SDavid Feng *
510ae76531SDavid Feng * 5. Set up final environment for calling board_init_r(). This
520ae76531SDavid Feng *    environment has BSS (initialized to 0), initialized non-const
530ae76531SDavid Feng *    data (initialized to their intended value), and stack in system
54ed64190fSSimon Glass *    RAM (for SPL moving the stack and GD into RAM is optional - see
55ed64190fSSimon Glass *    CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
560ae76531SDavid Feng *
57ed64190fSSimon Glass * TODO: For SPL, implement stack relocation on AArch64.
58ed64190fSSimon Glass *
59ed64190fSSimon Glass * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
60ed64190fSSimon Glass *    at this point regarding memory, so call c_runtime_cpu_setup.
61ed64190fSSimon Glass *
62ed64190fSSimon Glass * 7. Branch to board_init_r().
63ed64190fSSimon Glass *
64ed64190fSSimon Glass * For more information see 'Board Initialisation Flow in README.
650ae76531SDavid Feng */
660ae76531SDavid Feng
670ae76531SDavid FengENTRY(_main)
68*618713d1SKever Yang#if defined(CONFIG_SPL_BUILD) && defined(DEBUG)
69*618713d1SKever Yang	.equ SCTLR_A_BIT,		(1 << 1)
70*618713d1SKever Yang	.equ SCTLR_SA_BIT,		(1 << 3)
71*618713d1SKever Yang	.equ SCTLR_I_BIT,		(1 << 12)
720ae76531SDavid Feng
730ae76531SDavid Feng/*
74*618713d1SKever Yang * Enable the instruction cache, stack pointer
75*618713d1SKever Yang * and data access alignment checks
76*618713d1SKever Yang */
77*618713d1SKever Yang	mov	x1, #(SCTLR_I_BIT | SCTLR_A_BIT | SCTLR_SA_BIT)
78*618713d1SKever Yang	mrs	x0, sctlr_el3
79*618713d1SKever Yang	orr	x0, x0, x1
80*618713d1SKever Yang	msr	sctlr_el3, x0
81*618713d1SKever Yang	isb
82*618713d1SKever Yang/*
83*618713d1SKever Yang * Enable External Abort and SError
84*618713d1SKever Yang */
85*618713d1SKever Yang	msr	daifclr, #4
86*618713d1SKever Yang#endif
87*618713d1SKever Yang/*
880ae76531SDavid Feng * Set up initial C runtime environment and call board_init_f(0).
890ae76531SDavid Feng */
90faa18dbeSPhilipp Tomsich#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK)
91c3be6190SPhilipp Tomsich	ldr	x0, =(CONFIG_TPL_STACK)
92c3be6190SPhilipp Tomsich#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
93b2d5ac59SScott Wood	ldr	x0, =(CONFIG_SPL_STACK)
94b2d5ac59SScott Wood#else
950ae76531SDavid Feng	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
96b2d5ac59SScott Wood#endif
970ae76531SDavid Feng	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
98ecc30663SAlbert ARIBAUD	mov	x0, sp
99ecc30663SAlbert ARIBAUD	bl	board_init_f_alloc_reserve
100931bec31SSimon Glass	mov	sp, x0
101a737028eSStephen Warren	/* set up gd here, outside any C code */
102a737028eSStephen Warren	mov	x18, x0
103ecc30663SAlbert ARIBAUD	bl	board_init_f_init_reserve
104064eb493SJoseph Chen	bl	board_init_f_init_serial
105931bec31SSimon Glass
1060ae76531SDavid Feng	mov	x0, #0
1070ae76531SDavid Feng	bl	board_init_f
1080ae76531SDavid Feng
109b2d5ac59SScott Wood#if !defined(CONFIG_SPL_BUILD)
1100ae76531SDavid Feng/*
1110ae76531SDavid Feng * Set up intermediate environment (new sp and gd) and call
1120ae76531SDavid Feng * relocate_code(addr_moni). Trick here is that we'll return
1130ae76531SDavid Feng * 'here' but relocated.
1140ae76531SDavid Feng */
1150ae76531SDavid Feng	ldr	x0, [x18, #GD_START_ADDR_SP]	/* x0 <- gd->start_addr_sp */
1160ae76531SDavid Feng	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
11751380c3bSzijun_hu	ldr	x18, [x18, #GD_NEW_GD]		/* x18 <- gd->new_gd */
1180ae76531SDavid Feng
119645a442dSJoseph Chen#ifndef CONFIG_SKIP_RELOCATE_UBOOT
1200ae76531SDavid Feng	adr	lr, relocation_return
121f00ac1e5SStephen Warren#if CONFIG_POSITION_INDEPENDENT
122f00ac1e5SStephen Warren	/* Add in link-vs-runtime offset */
123f00ac1e5SStephen Warren	adr	x0, _start		/* x0 <- Runtime value of _start */
124f00ac1e5SStephen Warren	ldr	x9, _TEXT_BASE		/* x9 <- Linked value of _start */
125f00ac1e5SStephen Warren	sub	x9, x9, x0		/* x9 <- Run-vs-link offset */
126f00ac1e5SStephen Warren	add	lr, lr, x9
127f00ac1e5SStephen Warren#endif
128f00ac1e5SStephen Warren	/* Add in link-vs-relocation offset */
1290ae76531SDavid Feng	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
1300ae76531SDavid Feng	add	lr, lr, x9	/* new return address after relocation */
1310ae76531SDavid Feng	ldr	x0, [x18, #GD_RELOCADDR]	/* x0 <- gd->relocaddr */
1320ae76531SDavid Feng	b	relocate_code
133645a442dSJoseph Chen#endif
1340ae76531SDavid Feng
1350ae76531SDavid Fengrelocation_return:
1360ae76531SDavid Feng
1370ae76531SDavid Feng/*
1380ae76531SDavid Feng * Set up final (full) environment
1390ae76531SDavid Feng */
1400ae76531SDavid Feng	bl	c_runtime_cpu_setup		/* still call old routine */
141b8cb51d0SJeremy Hunt#endif /* !CONFIG_SPL_BUILD */
1427a70c998SPhilipp Tomsich#if defined(CONFIG_SPL_BUILD)
1437a70c998SPhilipp Tomsich	bl	spl_relocate_stack_gd           /* may return NULL */
144995085a9SYork Sun	/* set up gd here, outside any C code, if new stack is returned */
145995085a9SYork Sun	cmp	x0, #0
146995085a9SYork Sun	csel	x18, x0, x18, ne
1477a70c998SPhilipp Tomsich	/*
1487a70c998SPhilipp Tomsich	 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
1497a70c998SPhilipp Tomsich	 * around the constraint that conditional moves can not
1507a70c998SPhilipp Tomsich	 * have 'sp' as an operand
1517a70c998SPhilipp Tomsich	 */
1527a70c998SPhilipp Tomsich	mov	x1, sp
1537a70c998SPhilipp Tomsich	cmp	x0, #0
1547a70c998SPhilipp Tomsich	csel	x0, x0, x1, ne
1557a70c998SPhilipp Tomsich	mov	sp, x0
1567a70c998SPhilipp Tomsich#endif
157ed64190fSSimon Glass
1580ae76531SDavid Feng/*
1590ae76531SDavid Feng * Clear BSS section
1600ae76531SDavid Feng */
1610ae76531SDavid Feng	ldr	x0, =__bss_start		/* this is auto-relocated! */
1620ae76531SDavid Feng	ldr	x1, =__bss_end			/* this is auto-relocated! */
1630ae76531SDavid Fengclear_loop:
16407a63c7eSMasahiro Yamada	str	xzr, [x0], #8
1650ae76531SDavid Feng	cmp	x0, x1
1660ae76531SDavid Feng	b.lo	clear_loop
1670ae76531SDavid Feng
1680ae76531SDavid Feng	/* call board_init_r(gd_t *id, ulong dest_addr) */
1690ae76531SDavid Feng	mov	x0, x18				/* gd_t */
1700ae76531SDavid Feng	ldr	x1, [x18, #GD_RELOCADDR]	/* dest_addr */
1710ae76531SDavid Feng	b	board_init_r			/* PC relative jump */
1720ae76531SDavid Feng
1730ae76531SDavid Feng	/* NOTREACHED - board_init_r() does not return */
1740ae76531SDavid Feng
1750ae76531SDavid FengENDPROC(_main)
176