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 * Enable instruction cache (if required), stack pointer, 71 * data access alignment checks and SError. 72 */ 73#ifdef CONFIG_SPL_BUILD 74 mov x1, #(CR_A | CR_SA | CR_I) 75#else 76 mov x1, #(CR_A | CR_SA) 77#endif 78 switch_el x2, 3f, 2f, 1f 793: mrs x0, sctlr_el3 80 orr x0, x0, x1 81 msr sctlr_el3, x0 82 msr daifclr, #4 /* SCR_EL3.EA=1 was already set in start.S */ 83 b 0f 842: mrs x0, sctlr_el2 85 orr x0, x0, x1 86 msr sctlr_el2, x0 87 88 mrs x0, hcr_el2 89 orr x0, x0, #HCR_EL2_TGE 90 orr x0, x0, #HCR_EL2_AMO 91#if CONFIG_IS_ENABLED(IRQ) 92 orr x0, x0, #HCR_EL2_IMO 93#endif 94 msr hcr_el2, x0 95 msr daifclr, #4 96 b 0f 971: mrs x0, sctlr_el1 98 orr x0, x0, x1 99 msr sctlr_el1, x0 100 msr daifclr, #4 1010: 102 isb 103 104/* 105 * Set up initial C runtime environment and call board_init_f(0). 106 */ 107#if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK) 108 ldr x0, =(CONFIG_TPL_STACK) 109#elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) 110 ldr x0, =(CONFIG_SPL_STACK) 111#else 112 ldr x0, =(CONFIG_SYS_INIT_SP_ADDR) 113#endif 114 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 115 mov x0, sp 116 bl board_init_f_alloc_reserve 117 mov sp, x0 118 /* set up gd here, outside any C code */ 119 mov x18, x0 120 bl board_init_f_init_reserve 121 bl board_init_f_boot_flags 122 123 bl board_init_f 124 125#if (defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) && !defined(CONFIG_SPL_SKIP_RELOCATE)) || \ 126 !defined(CONFIG_SPL_BUILD) 127/* 128 * Set up intermediate environment (new sp and gd) and call 129 * relocate_code(addr_moni). Trick here is that we'll return 130 * 'here' but relocated. 131 */ 132 ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */ 133 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ 134 ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */ 135 136#ifndef CONFIG_SKIP_RELOCATE_UBOOT 137 adr lr, relocation_return 138#if CONFIG_POSITION_INDEPENDENT 139 /* Add in link-vs-runtime offset */ 140 adr x0, _start /* x0 <- Runtime value of _start */ 141 ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */ 142 sub x9, x9, x0 /* x9 <- Run-vs-link offset */ 143 add lr, lr, x9 144#endif 145 /* Add in link-vs-relocation offset */ 146 ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ 147 add lr, lr, x9 /* new return address after relocation */ 148 ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ 149 b relocate_code 150#endif 151 152relocation_return: 153 154/* 155 * Set up final (full) environment 156 */ 157 bl c_runtime_cpu_setup /* still call old routine */ 158#endif /* !CONFIG_SPL_BUILD */ 159#if defined(CONFIG_SPL_BUILD) 160 bl spl_relocate_stack_gd /* may return NULL */ 161 /* set up gd here, outside any C code, if new stack is returned */ 162 cmp x0, #0 163 csel x18, x0, x18, ne 164 /* 165 * Perform 'sp = (x0 != NULL) ? x0 : sp' while working 166 * around the constraint that conditional moves can not 167 * have 'sp' as an operand 168 */ 169 mov x1, sp 170 cmp x0, #0 171 csel x0, x0, x1, ne 172 mov sp, x0 173#endif 174 175/* 176 * Clear BSS section 177 */ 178 ldr x0, =__bss_start /* this is auto-relocated! */ 179 ldr x1, =__bss_end /* this is auto-relocated! */ 180clear_loop: 181 str xzr, [x0], #8 182 cmp x0, x1 183 b.lo clear_loop 184 185 /* call board_init_r(gd_t *id, ulong dest_addr) */ 186 mov x0, x18 /* gd_t */ 187 ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ 188 b board_init_r /* PC relative jump */ 189 190 /* NOTREACHED - board_init_r() does not return */ 191 192ENDPROC(_main) 193