blob: 98a906ee111c6110bf0b5aa2b839bc2143ddd892 [file] [log] [blame]
David Feng0ae76532013-12-14 11:47:35 +08001/*
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
30 * CONSTANT initialized data are available.
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 *
39 * (the following applies only to non-SPL builds)
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 * 4. Call relocate_code(). This function relocates U-Boot from its
46 * current location into the relocation destination computed by
47 * board_init_f().
48 *
49 * 5. Set up final environment for calling board_init_r(). This
50 * environment has BSS (initialized to 0), initialized non-const
51 * data (initialized to their intended value), and stack in system
52 * RAM. GD has retained values set by board_init_f(). Some CPUs
53 * have some work left to do at this point regarding memory, so
54 * call c_runtime_cpu_setup.
55 *
56 * 6. Branch to board_init_r().
57 */
58
59ENTRY(_main)
60
61/*
62 * Set up initial C runtime environment and call board_init_f(0).
63 */
Scott Woodb2d5ac52015-03-24 13:25:02 -070064#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
65 ldr x0, =(CONFIG_SPL_STACK)
66#else
David Feng0ae76532013-12-14 11:47:35 +080067 ldr x0, =(CONFIG_SYS_INIT_SP_ADDR)
Scott Woodb2d5ac52015-03-24 13:25:02 -070068#endif
David Fengb2633022015-01-31 11:55:28 +080069 sub x18, x0, #GD_SIZE /* allocate one GD above SP */
70 bic x18, x18, #0x7 /* 8-byte alignment for GD */
71zero_gd:
72 sub x0, x0, #0x8
73 str xzr, [x0]
74 cmp x0, x18
75 b.gt zero_gd
76#if defined(CONFIG_SYS_MALLOC_F_LEN)
Thierry Reding502a2af2015-07-22 16:44:32 -060077 ldr x0, =CONFIG_SYS_MALLOC_F_LEN
78 sub x0, x18, x0
David Fengb2633022015-01-31 11:55:28 +080079 str x0, [x18, #GD_MALLOC_BASE]
80#endif
David Feng0ae76532013-12-14 11:47:35 +080081 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
David Feng0ae76532013-12-14 11:47:35 +080082 mov x0, #0
83 bl board_init_f
84
Scott Woodb2d5ac52015-03-24 13:25:02 -070085#if !defined(CONFIG_SPL_BUILD)
David Feng0ae76532013-12-14 11:47:35 +080086/*
87 * Set up intermediate environment (new sp and gd) and call
88 * relocate_code(addr_moni). Trick here is that we'll return
89 * 'here' but relocated.
90 */
91 ldr x0, [x18, #GD_START_ADDR_SP] /* x0 <- gd->start_addr_sp */
92 bic sp, x0, #0xf /* 16-byte alignment for ABI compliance */
93 ldr x18, [x18, #GD_BD] /* x18 <- gd->bd */
94 sub x18, x18, #GD_SIZE /* new GD is below bd */
95
96 adr lr, relocation_return
97 ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */
98 add lr, lr, x9 /* new return address after relocation */
99 ldr x0, [x18, #GD_RELOCADDR] /* x0 <- gd->relocaddr */
100 b relocate_code
101
102relocation_return:
103
104/*
105 * Set up final (full) environment
106 */
107 bl c_runtime_cpu_setup /* still call old routine */
108
109/*
110 * Clear BSS section
111 */
112 ldr x0, =__bss_start /* this is auto-relocated! */
113 ldr x1, =__bss_end /* this is auto-relocated! */
114 mov x2, #0
115clear_loop:
116 str x2, [x0]
117 add x0, x0, #8
118 cmp x0, x1
119 b.lo clear_loop
120
121 /* call board_init_r(gd_t *id, ulong dest_addr) */
122 mov x0, x18 /* gd_t */
123 ldr x1, [x18, #GD_RELOCADDR] /* dest_addr */
124 b board_init_r /* PC relative jump */
125
126 /* NOTREACHED - board_init_r() does not return */
127
Scott Woodb2d5ac52015-03-24 13:25:02 -0700128#endif /* !CONFIG_SPL_BUILD */
129
David Feng0ae76532013-12-14 11:47:35 +0800130ENDPROC(_main)