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> 16*b25dd131SJoseph Chen#include <asm/system.h> 170ae76531SDavid Feng#include <linux/linkage.h> 180ae76531SDavid Feng 190ae76531SDavid Feng/* 200ae76531SDavid Feng * This file handles the target-independent stages of the U-Boot 210ae76531SDavid Feng * start-up where a C runtime environment is needed. Its entry point 220ae76531SDavid Feng * is _main and is branched into from the target's start.S file. 230ae76531SDavid Feng * 240ae76531SDavid Feng * _main execution sequence is: 250ae76531SDavid Feng * 260ae76531SDavid Feng * 1. Set up initial environment for calling board_init_f(). 270ae76531SDavid Feng * This environment only provides a stack and a place to store 280ae76531SDavid Feng * the GD ('global data') structure, both located in some readily 290ae76531SDavid Feng * available RAM (SRAM, locked cache...). In this context, VARIABLE 300ae76531SDavid Feng * global data, initialized or not (BSS), are UNAVAILABLE; only 31ed64190fSSimon Glass * CONSTANT initialized data are available. GD should be zeroed 32ed64190fSSimon Glass * before board_init_f() is called. 330ae76531SDavid Feng * 340ae76531SDavid Feng * 2. Call board_init_f(). This function prepares the hardware for 350ae76531SDavid Feng * execution from system RAM (DRAM, DDR...) As system RAM may not 360ae76531SDavid Feng * be available yet, , board_init_f() must use the current GD to 370ae76531SDavid Feng * store any data which must be passed on to later stages. These 380ae76531SDavid Feng * data include the relocation destination, the future stack, and 390ae76531SDavid Feng * the future GD location. 400ae76531SDavid Feng * 410ae76531SDavid Feng * 3. Set up intermediate environment where the stack and GD are the 420ae76531SDavid Feng * ones allocated by board_init_f() in system RAM, but BSS and 430ae76531SDavid Feng * initialized non-const data are still not available. 440ae76531SDavid Feng * 45ed64190fSSimon Glass * 4a.For U-Boot proper (not SPL), call relocate_code(). This function 46ed64190fSSimon Glass * relocates U-Boot from its current location into the relocation 47ed64190fSSimon Glass * destination computed by board_init_f(). 48ed64190fSSimon Glass * 49ed64190fSSimon Glass * 4b.For SPL, board_init_f() just returns (to crt0). There is no 50ed64190fSSimon Glass * code relocation in SPL. 510ae76531SDavid Feng * 520ae76531SDavid Feng * 5. Set up final environment for calling board_init_r(). This 530ae76531SDavid Feng * environment has BSS (initialized to 0), initialized non-const 540ae76531SDavid Feng * data (initialized to their intended value), and stack in system 55ed64190fSSimon Glass * RAM (for SPL moving the stack and GD into RAM is optional - see 56ed64190fSSimon Glass * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f(). 570ae76531SDavid Feng * 58ed64190fSSimon Glass * TODO: For SPL, implement stack relocation on AArch64. 59ed64190fSSimon Glass * 60ed64190fSSimon Glass * 6. For U-Boot proper (not SPL), some CPUs have some work left to do 61ed64190fSSimon Glass * at this point regarding memory, so call c_runtime_cpu_setup. 62ed64190fSSimon Glass * 63ed64190fSSimon Glass * 7. Branch to board_init_r(). 64ed64190fSSimon Glass * 65ed64190fSSimon Glass * For more information see 'Board Initialisation Flow in README. 660ae76531SDavid Feng */ 670ae76531SDavid Feng 680ae76531SDavid FengENTRY(_main) 69*b25dd131SJoseph Chen 70618713d1SKever Yang/* 710ae76531SDavid Feng * Set up initial C runtime environment and call board_init_f(0). 720ae76531SDavid Feng */ 73faa18dbeSPhilipp Tomsich#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK) 74c3be6190SPhilipp Tomsich ldr x0, =(CONFIG_TPL_STACK) 75c3be6190SPhilipp Tomsich#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) 76b2d5ac59SScott Wood ldr x0, =(CONFIG_SPL_STACK) 77b2d5ac59SScott Wood#else 780ae76531SDavid Feng ldr x0, =(CONFIG_SYS_INIT_SP_ADDR) 79b2d5ac59SScott Wood#endif 800ae76531SDavid Feng bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 81ecc30663SAlbert ARIBAUD mov x0, sp 82ecc30663SAlbert ARIBAUD bl board_init_f_alloc_reserve 83931bec31SSimon Glass mov sp, x0 84a737028eSStephen Warren /* set up gd here, outside any C code */ 85a737028eSStephen Warren mov x18, x0 86ecc30663SAlbert ARIBAUD bl board_init_f_init_reserve 87034db995SJoseph Chen bl board_init_f_boot_flags 88931bec31SSimon Glass 890ae76531SDavid Feng bl board_init_f 900ae76531SDavid Feng 916184121cSAndy Yan#if (defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_SKIP_RELOCATE)) || \ 926184121cSAndy Yan !defined(CONFIG_SPL_BUILD) 930ae76531SDavid Feng/* 940ae76531SDavid Feng * Set up intermediate environment (new sp and gd) and call 950ae76531SDavid Feng * relocate_code(addr_moni). Trick here is that we'll return 960ae76531SDavid Feng * 'here' but relocated. 970ae76531SDavid Feng */ 980ae76531SDavid Feng ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */ 990ae76531SDavid Feng bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 10051380c3bSzijun_hu ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */ 1010ae76531SDavid Feng 102645a442dSJoseph Chen#ifndef CONFIG_SKIP_RELOCATE_UBOOT 1030ae76531SDavid Feng adr lr, relocation_return 104f00ac1e5SStephen Warren#if CONFIG_POSITION_INDEPENDENT 105f00ac1e5SStephen Warren /* Add in link-vs-runtime offset */ 106f00ac1e5SStephen Warren adr x0, _start /* x0 <- Runtime value of _start */ 107f00ac1e5SStephen Warren ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */ 108f00ac1e5SStephen Warren sub x9, x9, x0 /* x9 <- Run-vs-link offset */ 109f00ac1e5SStephen Warren add lr, lr, x9 110f00ac1e5SStephen Warren#endif 111f00ac1e5SStephen Warren /* Add in link-vs-relocation offset */ 1120ae76531SDavid Feng ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ 1130ae76531SDavid Feng add lr, lr, x9 /* new return address after relocation */ 1140ae76531SDavid Feng ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ 1150ae76531SDavid Feng b relocate_code 116645a442dSJoseph Chen#endif 1170ae76531SDavid Feng 1180ae76531SDavid Fengrelocation_return: 1190ae76531SDavid Feng 1200ae76531SDavid Feng/* 1210ae76531SDavid Feng * Set up final (full) environment 1220ae76531SDavid Feng */ 1230ae76531SDavid Feng bl c_runtime_cpu_setup /* still call old routine */ 124b8cb51d0SJeremy Hunt#endif /* !CONFIG_SPL_BUILD */ 1257a70c998SPhilipp Tomsich#if defined(CONFIG_SPL_BUILD) 1267a70c998SPhilipp Tomsich bl spl_relocate_stack_gd /* may return NULL */ 127995085a9SYork Sun /* set up gd here, outside any C code, if new stack is returned */ 128995085a9SYork Sun cmp x0, #0 129995085a9SYork Sun csel x18, x0, x18, ne 1307a70c998SPhilipp Tomsich /* 1317a70c998SPhilipp Tomsich * Perform 'sp = (x0 != NULL) ? x0 : sp' while working 1327a70c998SPhilipp Tomsich * around the constraint that conditional moves can not 1337a70c998SPhilipp Tomsich * have 'sp' as an operand 1347a70c998SPhilipp Tomsich */ 1357a70c998SPhilipp Tomsich mov x1, sp 1367a70c998SPhilipp Tomsich cmp x0, #0 1377a70c998SPhilipp Tomsich csel x0, x0, x1, ne 1387a70c998SPhilipp Tomsich mov sp, x0 1397a70c998SPhilipp Tomsich#endif 140ed64190fSSimon Glass 1410ae76531SDavid Feng/* 1420ae76531SDavid Feng * Clear BSS section 1430ae76531SDavid Feng */ 1440ae76531SDavid Feng ldr x0, =__bss_start /* this is auto-relocated! */ 1450ae76531SDavid Feng ldr x1, =__bss_end /* this is auto-relocated! */ 1460ae76531SDavid Fengclear_loop: 14707a63c7eSMasahiro Yamada str xzr, [x0], #8 1480ae76531SDavid Feng cmp x0, x1 1490ae76531SDavid Feng b.lo clear_loop 1500ae76531SDavid Feng 1510ae76531SDavid Feng /* call board_init_r(gd_t *id, ulong dest_addr) */ 1520ae76531SDavid Feng mov x0, x18 /* gd_t */ 1530ae76531SDavid Feng ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ 1540ae76531SDavid Feng b board_init_r /* PC relative jump */ 1550ae76531SDavid Feng 1560ae76531SDavid Feng /* NOTREACHED - board_init_r() does not return */ 1570ae76531SDavid Feng 1580ae76531SDavid FengENDPROC(_main) 159