1/* 2 * crt0 - C-runtime startup Code for AArch64 U-Boot 3 * 4 * (C) Copyright 2013 5 * David Feng <fenghua@phytium.com.cn> 6 * 7 * (C) Copyright 2012 8 * Albert ARIBAUD <albert.u.boot@aribaud.net> 9 * 10 * SPDX-License-Identifier: GPL-2.0+ 11 */ 12 13#include <config.h> 14#include <asm-offsets.h> 15#include <asm/macro.h> 16#include <asm/system.h> 17#include <linux/linkage.h> 18 19/* 20 * This file handles the target-independent stages of the U-Boot 21 * start-up where a C runtime environment is needed. Its entry point 22 * is _main and is branched into from the target's start.S file. 23 * 24 * _main execution sequence is: 25 * 26 * 1. Set up initial environment for calling board_init_f(). 27 * This environment only provides a stack and a place to store 28 * the GD ('global data') structure, both located in some readily 29 * available RAM (SRAM, locked cache...). In this context, VARIABLE 30 * global data, initialized or not (BSS), are UNAVAILABLE; only 31 * CONSTANT initialized data are available. GD should be zeroed 32 * before board_init_f() is called. 33 * 34 * 2. Call board_init_f(). This function prepares the hardware for 35 * execution from system RAM (DRAM, DDR...) As system RAM may not 36 * be available yet, , board_init_f() must use the current GD to 37 * store any data which must be passed on to later stages. These 38 * data include the relocation destination, the future stack, and 39 * the future GD location. 40 * 41 * 3. Set up intermediate environment where the stack and GD are the 42 * ones allocated by board_init_f() in system RAM, but BSS and 43 * initialized non-const data are still not available. 44 * 45 * 4a.For U-Boot proper (not SPL), call relocate_code(). This function 46 * relocates U-Boot from its current location into the relocation 47 * destination computed by board_init_f(). 48 * 49 * 4b.For SPL, board_init_f() just returns (to crt0). There is no 50 * code relocation in SPL. 51 * 52 * 5. Set up final environment for calling board_init_r(). This 53 * environment has BSS (initialized to 0), initialized non-const 54 * data (initialized to their intended value), and stack in system 55 * RAM (for SPL moving the stack and GD into RAM is optional - see 56 * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f(). 57 * 58 * TODO: For SPL, implement stack relocation on AArch64. 59 * 60 * 6. For U-Boot proper (not SPL), some CPUs have some work left to do 61 * at this point regarding memory, so call c_runtime_cpu_setup. 62 * 63 * 7. Branch to board_init_r(). 64 * 65 * For more information see 'Board Initialisation Flow in README. 66 */ 67 68ENTRY(_main) 69 70/* 71 * Set up initial C runtime environment and call board_init_f(0). 72 */ 73#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK) 74 ldr x0, =(CONFIG_TPL_STACK) 75#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) 76 ldr x0, =(CONFIG_SPL_STACK) 77#else 78 ldr x0, =(CONFIG_SYS_INIT_SP_ADDR) 79#endif 80 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 81 mov x0, sp 82 bl board_init_f_alloc_reserve 83 mov sp, x0 84 /* set up gd here, outside any C code */ 85 mov x18, x0 86 bl board_init_f_init_reserve 87 bl board_init_f_boot_flags 88 89 bl board_init_f 90 91#if (defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_SKIP_RELOCATE)) || \ 92 !defined(CONFIG_SPL_BUILD) 93/* 94 * Set up intermediate environment (new sp and gd) and call 95 * relocate_code(addr_moni). Trick here is that we'll return 96 * 'here' but relocated. 97 */ 98 ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */ 99 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 100 ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */ 101 102#ifndef CONFIG_SKIP_RELOCATE_UBOOT 103 adr lr, relocation_return 104#if CONFIG_POSITION_INDEPENDENT 105 /* Add in link-vs-runtime offset */ 106 adr x0, _start /* x0 <- Runtime value of _start */ 107 ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */ 108 sub x9, x9, x0 /* x9 <- Run-vs-link offset */ 109 add lr, lr, x9 110#endif 111 /* Add in link-vs-relocation offset */ 112 ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ 113 add lr, lr, x9 /* new return address after relocation */ 114 ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ 115 b relocate_code 116#endif 117 118relocation_return: 119 120/* 121 * Set up final (full) environment 122 */ 123 bl c_runtime_cpu_setup /* still call old routine */ 124#endif /* !CONFIG_SPL_BUILD */ 125#if defined(CONFIG_SPL_BUILD) 126 bl spl_relocate_stack_gd /* may return NULL */ 127 /* set up gd here, outside any C code, if new stack is returned */ 128 cmp x0, #0 129 csel x18, x0, x18, ne 130 /* 131 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working 132 * around the constraint that conditional moves can not 133 * have 'sp' as an operand 134 */ 135 mov x1, sp 136 cmp x0, #0 137 csel x0, x0, x1, ne 138 mov sp, x0 139#endif 140 141/* 142 * Clear BSS section 143 */ 144 ldr x0, =__bss_start /* this is auto-relocated! */ 145 ldr x1, =__bss_end /* this is auto-relocated! */ 146clear_loop: 147 str xzr, [x0], #8 148 cmp x0, x1 149 b.lo clear_loop 150 151 /* call board_init_r(gd_t *id, ulong dest_addr) */ 152 mov x0, x18 /* gd_t */ 153 ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ 154 b board_init_r /* PC relative jump */ 155 156 /* NOTREACHED - board_init_r() does not return */ 157 158ENDPROC(_main) 159