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