David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 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 <linux/linkage.h> |
| 17 | |
| 18 | /* |
| 19 | * This file handles the target-independent stages of the U-Boot |
| 20 | * start-up where a C runtime environment is needed. Its entry point |
| 21 | * is _main and is branched into from the target's start.S file. |
| 22 | * |
| 23 | * _main execution sequence is: |
| 24 | * |
| 25 | * 1. Set up initial environment for calling board_init_f(). |
| 26 | * This environment only provides a stack and a place to store |
| 27 | * the GD ('global data') structure, both located in some readily |
| 28 | * available RAM (SRAM, locked cache...). In this context, VARIABLE |
| 29 | * global data, initialized or not (BSS), are UNAVAILABLE; only |
Simon Glass | ed64190 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 30 | * CONSTANT initialized data are available. GD should be zeroed |
| 31 | * before board_init_f() is called. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 32 | * |
| 33 | * 2. Call board_init_f(). This function prepares the hardware for |
| 34 | * execution from system RAM (DRAM, DDR...) As system RAM may not |
| 35 | * be available yet, , board_init_f() must use the current GD to |
| 36 | * store any data which must be passed on to later stages. These |
| 37 | * data include the relocation destination, the future stack, and |
| 38 | * the future GD location. |
| 39 | * |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 40 | * 3. Set up intermediate environment where the stack and GD are the |
| 41 | * ones allocated by board_init_f() in system RAM, but BSS and |
| 42 | * initialized non-const data are still not available. |
| 43 | * |
Simon Glass | ed64190 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 44 | * 4a.For U-Boot proper (not SPL), call relocate_code(). This function |
| 45 | * relocates U-Boot from its current location into the relocation |
| 46 | * destination computed by board_init_f(). |
| 47 | * |
| 48 | * 4b.For SPL, board_init_f() just returns (to crt0). There is no |
| 49 | * code relocation in SPL. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 50 | * |
| 51 | * 5. Set up final environment for calling board_init_r(). This |
| 52 | * environment has BSS (initialized to 0), initialized non-const |
| 53 | * data (initialized to their intended value), and stack in system |
Simon Glass | ed64190 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 54 | * RAM (for SPL moving the stack and GD into RAM is optional - see |
| 55 | * CONFIG_SPL_STACK_R). GD has retained values set by board_init_f(). |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 56 | * |
Simon Glass | ed64190 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 57 | * TODO: For SPL, implement stack relocation on AArch64. |
| 58 | * |
| 59 | * 6. For U-Boot proper (not SPL), some CPUs have some work left to do |
| 60 | * at this point regarding memory, so call c_runtime_cpu_setup. |
| 61 | * |
| 62 | * 7. Branch to board_init_r(). |
| 63 | * |
| 64 | * For more information see 'Board Initialisation Flow in README. |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 65 | */ |
| 66 | |
| 67 | ENTRY(_main) |
| 68 | |
| 69 | /* |
| 70 | * Set up initial C runtime environment and call board_init_f(0). |
| 71 | */ |
Philipp Tomsich | faa18db | 2017-07-28 20:04:09 +0200 | [diff] [blame] | 72 | #if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK) |
Philipp Tomsich | c3be619 | 2017-07-04 11:02:14 +0200 | [diff] [blame] | 73 | ldr x0, =(CONFIG_TPL_STACK) |
| 74 | #elif defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK) |
Scott Wood | b2d5ac5 | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 75 | ldr x0, =(CONFIG_SPL_STACK) |
| 76 | #else |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 77 | ldr x0, =(CONFIG_SYS_INIT_SP_ADDR) |
Scott Wood | b2d5ac5 | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 78 | #endif |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 79 | bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ |
Albert ARIBAUD | ecc3066 | 2015-11-25 17:56:32 +0100 | [diff] [blame] | 80 | mov x0, sp |
| 81 | bl board_init_f_alloc_reserve |
Simon Glass | 931bec3 | 2015-10-19 06:49:59 -0600 | [diff] [blame] | 82 | mov sp, x0 |
Stephen Warren | a737028 | 2016-01-14 14:03:11 -0700 | [diff] [blame] | 83 | /* set up gd here, outside any C code */ |
| 84 | mov x18, x0 |
Albert ARIBAUD | ecc3066 | 2015-11-25 17:56:32 +0100 | [diff] [blame] | 85 | bl board_init_f_init_reserve |
Simon Glass | 931bec3 | 2015-10-19 06:49:59 -0600 | [diff] [blame] | 86 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 87 | mov x0, #0 |
| 88 | bl board_init_f |
| 89 | |
Scott Wood | b2d5ac5 | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 90 | #if !defined(CONFIG_SPL_BUILD) |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 91 | /* |
| 92 | * Set up intermediate environment (new sp and gd) and call |
| 93 | * relocate_code(addr_moni). Trick here is that we'll return |
| 94 | * 'here' but relocated. |
| 95 | */ |
| 96 | ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */ |
| 97 | bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ |
| 98 | ldr x18, [x18, #GD_BD] /* x18 <- gd->bd */ |
| 99 | sub x18, x18, #GD_SIZE /* new GD is below bd */ |
| 100 | |
| 101 | adr lr, relocation_return |
| 102 | ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ |
| 103 | add lr, lr, x9 /* new return address after relocation */ |
| 104 | ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ |
| 105 | b relocate_code |
| 106 | |
| 107 | relocation_return: |
| 108 | |
| 109 | /* |
| 110 | * Set up final (full) environment |
| 111 | */ |
| 112 | bl c_runtime_cpu_setup /* still call old routine */ |
Jeremy Hunt | b8cb51d | 2016-07-18 12:01:02 -0400 | [diff] [blame] | 113 | #endif /* !CONFIG_SPL_BUILD */ |
Philipp Tomsich | 7a70c99 | 2017-03-01 21:04:15 +0100 | [diff] [blame] | 114 | #if defined(CONFIG_SPL_BUILD) |
| 115 | bl spl_relocate_stack_gd /* may return NULL */ |
| 116 | /* |
| 117 | * Perform 'sp = (x0 != NULL) ? x0 : sp' while working |
| 118 | * around the constraint that conditional moves can not |
| 119 | * have 'sp' as an operand |
| 120 | */ |
| 121 | mov x1, sp |
| 122 | cmp x0, #0 |
| 123 | csel x0, x0, x1, ne |
| 124 | mov sp, x0 |
| 125 | #endif |
Simon Glass | ed64190 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 126 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 127 | /* |
| 128 | * Clear BSS section |
| 129 | */ |
| 130 | ldr x0, =__bss_start /* this is auto-relocated! */ |
| 131 | ldr x1, =__bss_end /* this is auto-relocated! */ |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 132 | clear_loop: |
Masahiro Yamada | 07a63c7 | 2017-01-27 16:15:30 +0900 | [diff] [blame] | 133 | str xzr, [x0], #8 |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 134 | cmp x0, x1 |
| 135 | b.lo clear_loop |
| 136 | |
| 137 | /* call board_init_r(gd_t *id, ulong dest_addr) */ |
| 138 | mov x0, x18 /* gd_t */ |
| 139 | ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ |
| 140 | b board_init_r /* PC relative jump */ |
| 141 | |
| 142 | /* NOTREACHED - board_init_r() does not return */ |
| 143 | |
David Feng | 0ae7653 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 144 | ENDPROC(_main) |