Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * crt0 - C-runtime startup Code for AArch64 U-Boot |
| 4 | * |
| 5 | * (C) Copyright 2013 |
| 6 | * David Feng <fenghua@phytium.com.cn> |
| 7 | * |
| 8 | * (C) Copyright 2012 |
| 9 | * Albert ARIBAUD <albert.u.boot@aribaud.net> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <config.h> |
| 13 | #include <asm-offsets.h> |
| 14 | #include <asm/macro.h> |
| 15 | #include <linux/linkage.h> |
Tom Rini | 4ddbade | 2022-05-25 12:16:03 -0400 | [diff] [blame] | 16 | #include <system-constants.h> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 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 | cb60ae8 | 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 | 85fd5f1 | 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 | 85fd5f1 | 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 | cb60ae8 | 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 | 85fd5f1 | 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 | cb60ae8 | 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(). |
| 56 | * |
| 57 | * TODO: For SPL, implement stack relocation on AArch64. |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 58 | * |
Simon Glass | cb60ae8 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 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 | 85fd5f1 | 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 | 44aa830 | 2017-07-28 20:04:09 +0200 | [diff] [blame] | 72 | #if defined(CONFIG_TPL_BUILD) && defined(CONFIG_TPL_NEEDS_SEPARATE_STACK) |
Philipp Tomsich | d71b901 | 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 | 8e728cd | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 75 | ldr x0, =(CONFIG_SPL_STACK) |
Masahiro Yamada | bf4645c | 2019-06-26 13:51:46 +0900 | [diff] [blame] | 76 | #elif defined(CONFIG_INIT_SP_RELATIVE) |
Edgar E. Iglesias | 545474e | 2020-09-09 19:07:26 +0200 | [diff] [blame] | 77 | #if CONFIG_POSITION_INDEPENDENT |
| 78 | adrp x0, __bss_start /* x0 <- Runtime &__bss_start */ |
| 79 | add x0, x0, #:lo12:__bss_start |
| 80 | #else |
Stephen Warren | b80fe6d | 2017-12-19 18:30:36 -0700 | [diff] [blame] | 81 | adr x0, __bss_start |
Edgar E. Iglesias | 545474e | 2020-09-09 19:07:26 +0200 | [diff] [blame] | 82 | #endif |
Stephen Warren | b80fe6d | 2017-12-19 18:30:36 -0700 | [diff] [blame] | 83 | add x0, x0, #CONFIG_SYS_INIT_SP_BSS_OFFSET |
Scott Wood | 8e728cd | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 84 | #else |
Tom Rini | 4ddbade | 2022-05-25 12:16:03 -0400 | [diff] [blame] | 85 | ldr x0, =(SYS_INIT_SP_ADDR) |
Scott Wood | 8e728cd | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 86 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 87 | bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ |
Albert ARIBAUD | 6cb4c46 | 2015-11-25 17:56:32 +0100 | [diff] [blame] | 88 | mov x0, sp |
| 89 | bl board_init_f_alloc_reserve |
Simon Glass | 4f45651 | 2015-10-19 06:49:59 -0600 | [diff] [blame] | 90 | mov sp, x0 |
Stephen Warren | 159c0da | 2016-01-14 14:03:11 -0700 | [diff] [blame] | 91 | /* set up gd here, outside any C code */ |
| 92 | mov x18, x0 |
Albert ARIBAUD | 6cb4c46 | 2015-11-25 17:56:32 +0100 | [diff] [blame] | 93 | bl board_init_f_init_reserve |
Simon Glass | 4f45651 | 2015-10-19 06:49:59 -0600 | [diff] [blame] | 94 | |
Simon Glass | 1ed9bf9 | 2021-11-03 07:16:06 -0600 | [diff] [blame] | 95 | #if defined(CONFIG_DEBUG_UART) && CONFIG_IS_ENABLED(SERIAL) |
| 96 | bl debug_uart_init |
| 97 | #endif |
| 98 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 99 | mov x0, #0 |
| 100 | bl board_init_f |
| 101 | |
Scott Wood | 8e728cd | 2015-03-24 13:25:02 -0700 | [diff] [blame] | 102 | #if !defined(CONFIG_SPL_BUILD) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 103 | /* |
| 104 | * Set up intermediate environment (new sp and gd) and call |
| 105 | * relocate_code(addr_moni). Trick here is that we'll return |
| 106 | * 'here' but relocated. |
| 107 | */ |
| 108 | ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */ |
| 109 | bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */ |
zijun_hu | fc4741f | 2017-09-22 14:39:13 +0800 | [diff] [blame] | 110 | ldr x18, [x18, #GD_NEW_GD] /* x18 <- gd->new_gd */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 111 | |
Marek Vasut | 9ecb266 | 2021-11-13 18:34:59 +0100 | [diff] [blame] | 112 | /* Skip relocation in case gd->gd_flags & GD_FLG_SKIP_RELOC */ |
| 113 | ldr x0, [x18, #GD_FLAGS] /* x0 <- gd->flags */ |
| 114 | tbnz x0, 11, relocation_return /* GD_FLG_SKIP_RELOC is bit 11 */ |
| 115 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 116 | adr lr, relocation_return |
Stephen Warren | 81c2137 | 2017-11-02 18:11:27 -0600 | [diff] [blame] | 117 | #if CONFIG_POSITION_INDEPENDENT |
| 118 | /* Add in link-vs-runtime offset */ |
Edgar E. Iglesias | 545474e | 2020-09-09 19:07:26 +0200 | [diff] [blame] | 119 | adrp x0, _start /* x0 <- Runtime value of _start */ |
| 120 | add x0, x0, #:lo12:_start |
Stephen Warren | 81c2137 | 2017-11-02 18:11:27 -0600 | [diff] [blame] | 121 | ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */ |
| 122 | sub x9, x9, x0 /* x9 <- Run-vs-link offset */ |
| 123 | add lr, lr, x9 |
Kunihiko Hayashi | a11ee78 | 2021-06-15 15:33:02 +0900 | [diff] [blame] | 124 | #if defined(CONFIG_SYS_RELOC_GD_ENV_ADDR) |
| 125 | ldr x0, [x18, #GD_ENV_ADDR] /* x0 <- gd->env_addr */ |
| 126 | add x0, x0, x9 |
| 127 | str x0, [x18, #GD_ENV_ADDR] |
| 128 | #endif |
Stephen Warren | 81c2137 | 2017-11-02 18:11:27 -0600 | [diff] [blame] | 129 | #endif |
| 130 | /* Add in link-vs-relocation offset */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 131 | ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ |
| 132 | add lr, lr, x9 /* new return address after relocation */ |
| 133 | ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */ |
| 134 | b relocate_code |
| 135 | |
| 136 | relocation_return: |
| 137 | |
| 138 | /* |
| 139 | * Set up final (full) environment |
| 140 | */ |
| 141 | bl c_runtime_cpu_setup /* still call old routine */ |
Jeremy Hunt | 6402f72 | 2016-07-18 12:01:02 -0400 | [diff] [blame] | 142 | #endif /* !CONFIG_SPL_BUILD */ |
Heiko Stuebner | 4a0da7c | 2019-07-16 10:12:02 +0200 | [diff] [blame] | 143 | #if !defined(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(FRAMEWORK) |
Philipp Tomsich | 7d50c29 | 2017-03-01 21:04:15 +0100 | [diff] [blame] | 144 | #if defined(CONFIG_SPL_BUILD) |
| 145 | bl spl_relocate_stack_gd /* may return NULL */ |
York Sun | b400f6e | 2017-12-07 13:16:07 -0800 | [diff] [blame] | 146 | /* set up gd here, outside any C code, if new stack is returned */ |
| 147 | cmp x0, #0 |
| 148 | csel x18, x0, x18, ne |
Philipp Tomsich | 7d50c29 | 2017-03-01 21:04:15 +0100 | [diff] [blame] | 149 | /* |
| 150 | * Perform 'sp = (x0 != NULL) ? x0 : sp' while working |
| 151 | * around the constraint that conditional moves can not |
| 152 | * have 'sp' as an operand |
| 153 | */ |
| 154 | mov x1, sp |
| 155 | cmp x0, #0 |
| 156 | csel x0, x0, x1, ne |
| 157 | mov sp, x0 |
| 158 | #endif |
Simon Glass | cb60ae8 | 2015-08-01 08:55:46 -0600 | [diff] [blame] | 159 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 160 | /* |
| 161 | * Clear BSS section |
| 162 | */ |
| 163 | ldr x0, =__bss_start /* this is auto-relocated! */ |
| 164 | ldr x1, =__bss_end /* this is auto-relocated! */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 165 | clear_loop: |
Masahiro Yamada | 48042d8 | 2017-01-27 16:15:30 +0900 | [diff] [blame] | 166 | str xzr, [x0], #8 |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 167 | cmp x0, x1 |
| 168 | b.lo clear_loop |
| 169 | |
| 170 | /* call board_init_r(gd_t *id, ulong dest_addr) */ |
| 171 | mov x0, x18 /* gd_t */ |
| 172 | ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */ |
| 173 | b board_init_r /* PC relative jump */ |
| 174 | |
| 175 | /* NOTREACHED - board_init_r() does not return */ |
Heiko Stuebner | 4a0da7c | 2019-07-16 10:12:02 +0200 | [diff] [blame] | 176 | #endif |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 177 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 178 | ENDPROC(_main) |