1*0ae76531SDavid Feng/* 2*0ae76531SDavid Feng * crt0 - C-runtime startup Code for AArch64 U-Boot 3*0ae76531SDavid Feng * 4*0ae76531SDavid Feng * (C) Copyright 2013 5*0ae76531SDavid Feng * David Feng <fenghua@phytium.com.cn> 6*0ae76531SDavid Feng * 7*0ae76531SDavid Feng * (C) Copyright 2012 8*0ae76531SDavid Feng * Albert ARIBAUD <albert.u.boot@aribaud.net> 9*0ae76531SDavid Feng * 10*0ae76531SDavid Feng * SPDX-License-Identifier: GPL-2.0+ 11*0ae76531SDavid Feng */ 12*0ae76531SDavid Feng 13*0ae76531SDavid Feng#include <config.h> 14*0ae76531SDavid Feng#include <asm-offsets.h> 15*0ae76531SDavid Feng#include <asm/macro.h> 16*0ae76531SDavid Feng#include <linux/linkage.h> 17*0ae76531SDavid Feng 18*0ae76531SDavid Feng/* 19*0ae76531SDavid Feng * This file handles the target-independent stages of the U-Boot 20*0ae76531SDavid Feng * start-up where a C runtime environment is needed. Its entry point 21*0ae76531SDavid Feng * is _main and is branched into from the target's start.S file. 22*0ae76531SDavid Feng * 23*0ae76531SDavid Feng * _main execution sequence is: 24*0ae76531SDavid Feng * 25*0ae76531SDavid Feng * 1. Set up initial environment for calling board_init_f(). 26*0ae76531SDavid Feng * This environment only provides a stack and a place to store 27*0ae76531SDavid Feng * the GD ('global data') structure, both located in some readily 28*0ae76531SDavid Feng * available RAM (SRAM, locked cache...). In this context, VARIABLE 29*0ae76531SDavid Feng * global data, initialized or not (BSS), are UNAVAILABLE; only 30*0ae76531SDavid Feng * CONSTANT initialized data are available. 31*0ae76531SDavid Feng * 32*0ae76531SDavid Feng * 2. Call board_init_f(). This function prepares the hardware for 33*0ae76531SDavid Feng * execution from system RAM (DRAM, DDR...) As system RAM may not 34*0ae76531SDavid Feng * be available yet, , board_init_f() must use the current GD to 35*0ae76531SDavid Feng * store any data which must be passed on to later stages. These 36*0ae76531SDavid Feng * data include the relocation destination, the future stack, and 37*0ae76531SDavid Feng * the future GD location. 38*0ae76531SDavid Feng * 39*0ae76531SDavid Feng * (the following applies only to non-SPL builds) 40*0ae76531SDavid Feng * 41*0ae76531SDavid Feng * 3. Set up intermediate environment where the stack and GD are the 42*0ae76531SDavid Feng * ones allocated by board_init_f() in system RAM, but BSS and 43*0ae76531SDavid Feng * initialized non-const data are still not available. 44*0ae76531SDavid Feng * 45*0ae76531SDavid Feng * 4. Call relocate_code(). This function relocates U-Boot from its 46*0ae76531SDavid Feng * current location into the relocation destination computed by 47*0ae76531SDavid Feng * board_init_f(). 48*0ae76531SDavid Feng * 49*0ae76531SDavid Feng * 5. Set up final environment for calling board_init_r(). This 50*0ae76531SDavid Feng * environment has BSS (initialized to 0), initialized non-const 51*0ae76531SDavid Feng * data (initialized to their intended value), and stack in system 52*0ae76531SDavid Feng * RAM. GD has retained values set by board_init_f(). Some CPUs 53*0ae76531SDavid Feng * have some work left to do at this point regarding memory, so 54*0ae76531SDavid Feng * call c_runtime_cpu_setup. 55*0ae76531SDavid Feng * 56*0ae76531SDavid Feng * 6. Branch to board_init_r(). 57*0ae76531SDavid Feng */ 58*0ae76531SDavid Feng 59*0ae76531SDavid FengENTRY(_main) 60*0ae76531SDavid Feng 61*0ae76531SDavid Feng/* 62*0ae76531SDavid Feng * Set up initial C runtime environment and call board_init_f(0). 63*0ae76531SDavid Feng */ 64*0ae76531SDavid Feng ldr x0, =(CONFIG_SYS_INIT_SP_ADDR) 65*0ae76531SDavid Feng sub x0, x0, #GD_SIZE /* allocate one GD above SP */ 66*0ae76531SDavid Feng bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 67*0ae76531SDavid Feng mov x18, sp /* GD is above SP */ 68*0ae76531SDavid Feng mov x0, #0 69*0ae76531SDavid Feng bl board_init_f 70*0ae76531SDavid Feng 71*0ae76531SDavid Feng/* 72*0ae76531SDavid Feng * Set up intermediate environment (new sp and gd) and call 73*0ae76531SDavid Feng * relocate_code(addr_moni). Trick here is that we'll return 74*0ae76531SDavid Feng * 'here' but relocated. 75*0ae76531SDavid Feng */ 76*0ae76531SDavid Feng ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */ 77*0ae76531SDavid Feng bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 78*0ae76531SDavid Feng ldr x18, [x18, #GD_BD] /* x18 <- gd->bd */ 79*0ae76531SDavid Feng sub x18, x18, #GD_SIZE /* new GD is below bd */ 80*0ae76531SDavid Feng 81*0ae76531SDavid Feng adr lr, relocation_return 82*0ae76531SDavid Feng ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ 83*0ae76531SDavid Feng add lr, lr, x9 /* new return address after relocation */ 84*0ae76531SDavid Feng ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ 85*0ae76531SDavid Feng b relocate_code 86*0ae76531SDavid Feng 87*0ae76531SDavid Fengrelocation_return: 88*0ae76531SDavid Feng 89*0ae76531SDavid Feng/* 90*0ae76531SDavid Feng * Set up final (full) environment 91*0ae76531SDavid Feng */ 92*0ae76531SDavid Feng bl c_runtime_cpu_setup /* still call old routine */ 93*0ae76531SDavid Feng 94*0ae76531SDavid Feng/* 95*0ae76531SDavid Feng * Clear BSS section 96*0ae76531SDavid Feng */ 97*0ae76531SDavid Feng ldr x0, =__bss_start /* this is auto-relocated! */ 98*0ae76531SDavid Feng ldr x1, =__bss_end /* this is auto-relocated! */ 99*0ae76531SDavid Feng mov x2, #0 100*0ae76531SDavid Fengclear_loop: 101*0ae76531SDavid Feng str x2, [x0] 102*0ae76531SDavid Feng add x0, x0, #8 103*0ae76531SDavid Feng cmp x0, x1 104*0ae76531SDavid Feng b.lo clear_loop 105*0ae76531SDavid Feng 106*0ae76531SDavid Feng /* call board_init_r(gd_t *id, ulong dest_addr) */ 107*0ae76531SDavid Feng mov x0, x18 /* gd_t */ 108*0ae76531SDavid Feng ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ 109*0ae76531SDavid Feng b board_init_r /* PC relative jump */ 110*0ae76531SDavid Feng 111*0ae76531SDavid Feng /* NOTREACHED - board_init_r() does not return */ 112*0ae76531SDavid Feng 113*0ae76531SDavid FengENDPROC(_main) 114