1*83d290c5STom Rini/* SPDX-License-Identifier: GPL-2.0+ */ 20ae76531SDavid Feng/* 30ae76531SDavid Feng * crt0 - C-runtime startup Code for AArch64 U-Boot 40ae76531SDavid Feng * 50ae76531SDavid Feng * (C) Copyright 2013 60ae76531SDavid Feng * David Feng <fenghua@phytium.com.cn> 70ae76531SDavid Feng * 80ae76531SDavid Feng * (C) Copyright 2012 90ae76531SDavid Feng * Albert ARIBAUD <albert.u.boot@aribaud.net> 100ae76531SDavid Feng */ 110ae76531SDavid Feng 120ae76531SDavid Feng#include <config.h> 130ae76531SDavid Feng#include <asm-offsets.h> 140ae76531SDavid Feng#include <asm/macro.h> 150ae76531SDavid Feng#include <linux/linkage.h> 160ae76531SDavid Feng 170ae76531SDavid Feng/* 180ae76531SDavid Feng * This file handles the target-independent stages of the U-Boot 190ae76531SDavid Feng * start-up where a C runtime environment is needed. Its entry point 200ae76531SDavid Feng * is _main and is branched into from the target's start.S file. 210ae76531SDavid Feng * 220ae76531SDavid Feng * _main execution sequence is: 230ae76531SDavid Feng * 240ae76531SDavid Feng * 1. Set up initial environment for calling board_init_f(). 250ae76531SDavid Feng * This environment only provides a stack and a place to store 260ae76531SDavid Feng * the GD ('global data') structure, both located in some readily 270ae76531SDavid Feng * available RAM (SRAM, locked cache...). In this context, VARIABLE 280ae76531SDavid Feng * global data, initialized or not (BSS), are UNAVAILABLE; only 29ed64190fSSimon Glass * CONSTANT initialized data are available. GD should be zeroed 30ed64190fSSimon Glass * before board_init_f() is called. 310ae76531SDavid Feng * 320ae76531SDavid Feng * 2. Call board_init_f(). This function prepares the hardware for 330ae76531SDavid Feng * execution from system RAM (DRAM, DDR...) As system RAM may not 340ae76531SDavid Feng * be available yet, , board_init_f() must use the current GD to 350ae76531SDavid Feng * store any data which must be passed on to later stages. These 360ae76531SDavid Feng * data include the relocation destination, the future stack, and 370ae76531SDavid Feng * the future GD location. 380ae76531SDavid Feng * 390ae76531SDavid Feng * 3. Set up intermediate environment where the stack and GD are the 400ae76531SDavid Feng * ones allocated by board_init_f() in system RAM, but BSS and 410ae76531SDavid Feng * initialized non-const data are still not available. 420ae76531SDavid Feng * 43ed64190fSSimon Glass * 4a.For U-Boot proper (not SPL), call relocate_code(). This function 44ed64190fSSimon Glass * relocates U-Boot from its current location into the relocation 45ed64190fSSimon Glass * destination computed by board_init_f(). 46ed64190fSSimon Glass * 47ed64190fSSimon Glass * 4b.For SPL, board_init_f() just returns (to crt0). There is no 48ed64190fSSimon Glass * code relocation in SPL. 490ae76531SDavid Feng * 500ae76531SDavid Feng * 5. Set up final environment for calling board_init_r(). This 510ae76531SDavid Feng * environment has BSS (initialized to 0), initialized non-const 520ae76531SDavid Feng * data (initialized to their intended value), and stack in system 53ed64190fSSimon Glass * RAM (for SPL moving the stack and GD into RAM is optional - see 54ed64190fSSimon Glass * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f(). 550ae76531SDavid Feng * 56ed64190fSSimon Glass * TODO: For SPL, implement stack relocation on AArch64. 57ed64190fSSimon Glass * 58ed64190fSSimon Glass * 6. For U-Boot proper (not SPL), some CPUs have some work left to do 59ed64190fSSimon Glass * at this point regarding memory, so call c_runtime_cpu_setup. 60ed64190fSSimon Glass * 61ed64190fSSimon Glass * 7. Branch to board_init_r(). 62ed64190fSSimon Glass * 63ed64190fSSimon Glass * For more information see 'Board Initialisation Flow in README. 640ae76531SDavid Feng */ 650ae76531SDavid Feng 660ae76531SDavid FengENTRY(_main) 670ae76531SDavid Feng 680ae76531SDavid Feng/* 690ae76531SDavid Feng * Set up initial C runtime environment and call board_init_f(0). 700ae76531SDavid Feng */ 71faa18dbeSPhilipp Tomsich#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK) 72c3be6190SPhilipp Tomsich ldr x0, =(CONFIG_TPL_STACK) 73c3be6190SPhilipp Tomsich#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) 74b2d5ac59SScott Wood ldr x0, =(CONFIG_SPL_STACK) 75e6c90448SStephen Warren#elif defined(CONFIG_SYS_INIT_SP_BSS_OFFSET) 76e6c90448SStephen Warren adr x0, __bss_start 77e6c90448SStephen Warren add x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET 78b2d5ac59SScott Wood#else 790ae76531SDavid Feng ldr x0, =(CONFIG_SYS_INIT_SP_ADDR) 80b2d5ac59SScott Wood#endif 810ae76531SDavid Feng bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 82ecc30663SAlbert ARIBAUD mov x0, sp 83ecc30663SAlbert ARIBAUD bl board_init_f_alloc_reserve 84931bec31SSimon Glass mov sp, x0 85a737028eSStephen Warren /* set up gd here, outside any C code */ 86a737028eSStephen Warren mov x18, x0 87ecc30663SAlbert ARIBAUD bl board_init_f_init_reserve 88931bec31SSimon Glass 890ae76531SDavid Feng mov x0, #0 900ae76531SDavid Feng bl board_init_f 910ae76531SDavid Feng 92b2d5ac59SScott Wood#if !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 */ 10001a83599Szijun_hu ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */ 1010ae76531SDavid Feng 1020ae76531SDavid Feng adr lr, relocation_return 10349e93875SStephen Warren#if CONFIG_POSITION_INDEPENDENT 10449e93875SStephen Warren /* Add in link-vs-runtime offset */ 10549e93875SStephen Warren adr x0, _start /* x0 <- Runtime value of _start */ 10649e93875SStephen Warren ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */ 10749e93875SStephen Warren sub x9, x9, x0 /* x9 <- Run-vs-link offset */ 10849e93875SStephen Warren add lr, lr, x9 10949e93875SStephen Warren#endif 11049e93875SStephen Warren /* Add in link-vs-relocation offset */ 1110ae76531SDavid Feng ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ 1120ae76531SDavid Feng add lr, lr, x9 /* new return address after relocation */ 1130ae76531SDavid Feng ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ 1140ae76531SDavid Feng b relocate_code 1150ae76531SDavid Feng 1160ae76531SDavid Fengrelocation_return: 1170ae76531SDavid Feng 1180ae76531SDavid Feng/* 1190ae76531SDavid Feng * Set up final (full) environment 1200ae76531SDavid Feng */ 1210ae76531SDavid Feng bl c_runtime_cpu_setup /* still call old routine */ 122b8cb51d0SJeremy Hunt#endif /* !CONFIG_SPL_BUILD */ 1237a70c998SPhilipp Tomsich#if defined(CONFIG_SPL_BUILD) 1247a70c998SPhilipp Tomsich bl spl_relocate_stack_gd /* may return NULL */ 125e421b646SYork Sun /* set up gd here, outside any C code, if new stack is returned */ 126e421b646SYork Sun cmp x0, #0 127e421b646SYork Sun csel x18, x0, x18, ne 1287a70c998SPhilipp Tomsich /* 1297a70c998SPhilipp Tomsich * Perform 'sp = (x0 != NULL) ? x0 : sp' while working 1307a70c998SPhilipp Tomsich * around the constraint that conditional moves can not 1317a70c998SPhilipp Tomsich * have 'sp' as an operand 1327a70c998SPhilipp Tomsich */ 1337a70c998SPhilipp Tomsich mov x1, sp 1347a70c998SPhilipp Tomsich cmp x0, #0 1357a70c998SPhilipp Tomsich csel x0, x0, x1, ne 1367a70c998SPhilipp Tomsich mov sp, x0 1377a70c998SPhilipp Tomsich#endif 138ed64190fSSimon Glass 1390ae76531SDavid Feng/* 1400ae76531SDavid Feng * Clear BSS section 1410ae76531SDavid Feng */ 1420ae76531SDavid Feng ldr x0, =__bss_start /* this is auto-relocated! */ 1430ae76531SDavid Feng ldr x1, =__bss_end /* this is auto-relocated! */ 1440ae76531SDavid Fengclear_loop: 14507a63c7eSMasahiro Yamada str xzr, [x0], #8 1460ae76531SDavid Feng cmp x0, x1 1470ae76531SDavid Feng b.lo clear_loop 1480ae76531SDavid Feng 1490ae76531SDavid Feng /* call board_init_r(gd_t *id, ulong dest_addr) */ 1500ae76531SDavid Feng mov x0, x18 /* gd_t */ 1510ae76531SDavid Feng ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ 1520ae76531SDavid Feng b board_init_r /* PC relative jump */ 1530ae76531SDavid Feng 1540ae76531SDavid Feng /* NOTREACHED - board_init_r() does not return */ 1550ae76531SDavid Feng 1560ae76531SDavid FengENDPROC(_main) 157