xref: /openbmc/u-boot/arch/arm/lib/crt0_64.S (revision c3be6190)
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)
680ae76531SDavid Feng
690ae76531SDavid Feng/*
700ae76531SDavid Feng * Set up initial C runtime environment and call board_init_f(0).
710ae76531SDavid Feng */
72*c3be6190SPhilipp Tomsich#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_STACK)
73*c3be6190SPhilipp Tomsich	ldr	x0, =(CONFIG_TPL_STACK)
74*c3be6190SPhilipp Tomsich#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
75b2d5ac59SScott Wood	ldr	x0, =(CONFIG_SPL_STACK)
76b2d5ac59SScott Wood#else
770ae76531SDavid Feng	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
78b2d5ac59SScott Wood#endif
790ae76531SDavid Feng	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
80ecc30663SAlbert ARIBAUD	mov	x0, sp
81ecc30663SAlbert ARIBAUD	bl	board_init_f_alloc_reserve
82931bec31SSimon Glass	mov	sp, x0
83a737028eSStephen Warren	/* set up gd here, outside any C code */
84a737028eSStephen Warren	mov	x18, x0
85ecc30663SAlbert ARIBAUD	bl	board_init_f_init_reserve
86931bec31SSimon Glass
870ae76531SDavid Feng	mov	x0, #0
880ae76531SDavid Feng	bl	board_init_f
890ae76531SDavid Feng
90b2d5ac59SScott Wood#if !defined(CONFIG_SPL_BUILD)
910ae76531SDavid Feng/*
920ae76531SDavid Feng * Set up intermediate environment (new sp and gd) and call
930ae76531SDavid Feng * relocate_code(addr_moni). Trick here is that we'll return
940ae76531SDavid Feng * 'here' but relocated.
950ae76531SDavid Feng */
960ae76531SDavid Feng	ldr	x0, [x18, #GD_START_ADDR_SP]	/* x0 <- gd->start_addr_sp */
970ae76531SDavid Feng	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
980ae76531SDavid Feng	ldr	x18, [x18, #GD_BD]		/* x18 <- gd->bd */
990ae76531SDavid Feng	sub	x18, x18, #GD_SIZE		/* new GD is below bd */
1000ae76531SDavid Feng
1010ae76531SDavid Feng	adr	lr, relocation_return
1020ae76531SDavid Feng	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
1030ae76531SDavid Feng	add	lr, lr, x9	/* new return address after relocation */
1040ae76531SDavid Feng	ldr	x0, [x18, #GD_RELOCADDR]	/* x0 <- gd->relocaddr */
1050ae76531SDavid Feng	b	relocate_code
1060ae76531SDavid Feng
1070ae76531SDavid Fengrelocation_return:
1080ae76531SDavid Feng
1090ae76531SDavid Feng/*
1100ae76531SDavid Feng * Set up final (full) environment
1110ae76531SDavid Feng */
1120ae76531SDavid Feng	bl	c_runtime_cpu_setup		/* still call old routine */
113b8cb51d0SJeremy Hunt#endif /* !CONFIG_SPL_BUILD */
1147a70c998SPhilipp Tomsich#if defined(CONFIG_SPL_BUILD)
1157a70c998SPhilipp Tomsich	bl	spl_relocate_stack_gd           /* may return NULL */
1167a70c998SPhilipp Tomsich	/*
1177a70c998SPhilipp Tomsich	 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working
1187a70c998SPhilipp Tomsich	 * around the constraint that conditional moves can not
1197a70c998SPhilipp Tomsich	 * have 'sp' as an operand
1207a70c998SPhilipp Tomsich	 */
1217a70c998SPhilipp Tomsich	mov	x1, sp
1227a70c998SPhilipp Tomsich	cmp	x0, #0
1237a70c998SPhilipp Tomsich	csel	x0, x0, x1, ne
1247a70c998SPhilipp Tomsich	mov	sp, x0
1257a70c998SPhilipp Tomsich#endif
126ed64190fSSimon Glass
1270ae76531SDavid Feng/*
1280ae76531SDavid Feng * Clear BSS section
1290ae76531SDavid Feng */
1300ae76531SDavid Feng	ldr	x0, =__bss_start		/* this is auto-relocated! */
1310ae76531SDavid Feng	ldr	x1, =__bss_end			/* this is auto-relocated! */
1320ae76531SDavid Fengclear_loop:
13307a63c7eSMasahiro Yamada	str	xzr, [x0], #8
1340ae76531SDavid Feng	cmp	x0, x1
1350ae76531SDavid Feng	b.lo	clear_loop
1360ae76531SDavid Feng
1370ae76531SDavid Feng	/* call board_init_r(gd_t *id, ulong dest_addr) */
1380ae76531SDavid Feng	mov	x0, x18				/* gd_t */
1390ae76531SDavid Feng	ldr	x1, [x18, #GD_RELOCADDR]	/* dest_addr */
1400ae76531SDavid Feng	b	board_init_r			/* PC relative jump */
1410ae76531SDavid Feng
1420ae76531SDavid Feng	/* NOTREACHED - board_init_r() does not return */
1430ae76531SDavid Feng
1440ae76531SDavid FengENDPROC(_main)
145