xref: /openbmc/u-boot/arch/arm/lib/crt0_64.S (revision 931bec31)
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 */
72b2d5ac59SScott Wood#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
73b2d5ac59SScott Wood	ldr	x0, =(CONFIG_SPL_STACK)
74b2d5ac59SScott Wood#else
750ae76531SDavid Feng	ldr	x0, =(CONFIG_SYS_INIT_SP_ADDR)
76b2d5ac59SScott Wood#endif
770ae76531SDavid Feng	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
78*931bec31SSimon Glass	bl	board_init_f_mem
79*931bec31SSimon Glass	mov	sp, x0
80*931bec31SSimon Glass
810ae76531SDavid Feng	mov	x0, #0
820ae76531SDavid Feng	bl	board_init_f
830ae76531SDavid Feng
84b2d5ac59SScott Wood#if !defined(CONFIG_SPL_BUILD)
850ae76531SDavid Feng/*
860ae76531SDavid Feng * Set up intermediate environment (new sp and gd) and call
870ae76531SDavid Feng * relocate_code(addr_moni). Trick here is that we'll return
880ae76531SDavid Feng * 'here' but relocated.
890ae76531SDavid Feng */
900ae76531SDavid Feng	ldr	x0, [x18, #GD_START_ADDR_SP]	/* x0 <- gd->start_addr_sp */
910ae76531SDavid Feng	bic	sp, x0, #0xf	/* 16-byte alignment for ABI compliance */
920ae76531SDavid Feng	ldr	x18, [x18, #GD_BD]		/* x18 <- gd->bd */
930ae76531SDavid Feng	sub	x18, x18, #GD_SIZE		/* new GD is below bd */
940ae76531SDavid Feng
950ae76531SDavid Feng	adr	lr, relocation_return
960ae76531SDavid Feng	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
970ae76531SDavid Feng	add	lr, lr, x9	/* new return address after relocation */
980ae76531SDavid Feng	ldr	x0, [x18, #GD_RELOCADDR]	/* x0 <- gd->relocaddr */
990ae76531SDavid Feng	b	relocate_code
1000ae76531SDavid Feng
1010ae76531SDavid Fengrelocation_return:
1020ae76531SDavid Feng
1030ae76531SDavid Feng/*
1040ae76531SDavid Feng * Set up final (full) environment
1050ae76531SDavid Feng */
1060ae76531SDavid Feng	bl	c_runtime_cpu_setup		/* still call old routine */
1070ae76531SDavid Feng
108ed64190fSSimon Glass/* TODO: For SPL, call spl_relocate_stack_gd() to alloc stack relocation */
109ed64190fSSimon Glass
1100ae76531SDavid Feng/*
1110ae76531SDavid Feng * Clear BSS section
1120ae76531SDavid Feng */
1130ae76531SDavid Feng	ldr	x0, =__bss_start		/* this is auto-relocated! */
1140ae76531SDavid Feng	ldr	x1, =__bss_end			/* this is auto-relocated! */
1150ae76531SDavid Feng	mov	x2, #0
1160ae76531SDavid Fengclear_loop:
1170ae76531SDavid Feng	str	x2, [x0]
1180ae76531SDavid Feng	add	x0, x0, #8
1190ae76531SDavid Feng	cmp	x0, x1
1200ae76531SDavid Feng	b.lo	clear_loop
1210ae76531SDavid Feng
1220ae76531SDavid Feng	/* call board_init_r(gd_t *id, ulong dest_addr) */
1230ae76531SDavid Feng	mov	x0, x18				/* gd_t */
1240ae76531SDavid Feng	ldr	x1, [x18, #GD_RELOCADDR]	/* dest_addr */
1250ae76531SDavid Feng	b	board_init_r			/* PC relative jump */
1260ae76531SDavid Feng
1270ae76531SDavid Feng	/* NOTREACHED - board_init_r() does not return */
1280ae76531SDavid Feng
129b2d5ac59SScott Wood#endif /* !CONFIG_SPL_BUILD */
130b2d5ac59SScott Wood
1310ae76531SDavid FengENDPROC(_main)
132