crt0.S 5.0 KB
Newer Older
1 2 3 4 5
/*
 *  crt0 - C-runtime startup Code for ARM U-Boot
 *
 *  Copyright (c) 2012  Albert ARIBAUD <albert.u.boot@aribaud.net>
 *
6
 * SPDX-License-Identifier:	GPL-2.0+
7 8 9 10
 */

#include <config.h>
#include <asm-offsets.h>
11
#include <linux/linkage.h>
R
rev13@wp.pl 已提交
12 13 14
#ifdef CONFIG_CPU_V7M
#include <asm/armv7m.h>
#endif
15 16 17 18 19 20 21 22 23 24 25 26 27

/*
 * This file handles the target-independent stages of the U-Boot
 * start-up where a C runtime environment is needed. Its entry point
 * is _main and is branched into from the target's start.S file.
 *
 * _main execution sequence is:
 *
 * 1. Set up initial environment for calling board_init_f().
 *    This environment only provides a stack and a place to store
 *    the GD ('global data') structure, both located in some readily
 *    available RAM (SRAM, locked cache...). In this context, VARIABLE
 *    global data, initialized or not (BSS), are UNAVAILABLE; only
28 29
 *    CONSTANT initialized data are available. GD should be zeroed
 *    before board_init_f() is called.
30 31 32 33 34 35 36 37 38 39 40 41
 *
 * 2. Call board_init_f(). This function prepares the hardware for
 *    execution from system RAM (DRAM, DDR...) As system RAM may not
 *    be available yet, , board_init_f() must use the current GD to
 *    store any data which must be passed on to later stages. These
 *    data include the relocation destination, the future stack, and
 *    the future GD location.
 *
 * 3. Set up intermediate environment where the stack and GD are the
 *    ones allocated by board_init_f() in system RAM, but BSS and
 *    initialized non-const data are still not available.
 *
42 43 44 45 46 47
 * 4a.For U-Boot proper (not SPL), call relocate_code(). This function
 *    relocates U-Boot from its current location into the relocation
 *    destination computed by board_init_f().
 *
 * 4b.For SPL, board_init_f() just returns (to crt0). There is no
 *    code relocation in SPL.
48 49 50 51
 *
 * 5. Set up final environment for calling board_init_r(). This
 *    environment has BSS (initialized to 0), initialized non-const
 *    data (initialized to their intended value), and stack in system
52 53 54 55 56 57 58
 *    RAM (for SPL moving the stack and GD into RAM is optional - see
 *    CONFIG_SPL_STACK_R). GD has retained values set by board_init_f().
 *
 * 6. For U-Boot proper (not SPL), some CPUs have some work left to do
 *    at this point regarding memory, so call c_runtime_cpu_setup.
 *
 * 7. Branch to board_init_r().
59
 *
60
 * For more information see 'Board Initialisation Flow in README.
61 62 63 64 65 66
 */

/*
 * entry point of crt0 sequence
 */

67
ENTRY(_main)
68 69 70 71 72

/*
 * Set up initial C runtime environment and call board_init_f(0).
 */

73
#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
74 75 76 77
	ldr	sp, =(CONFIG_SPL_STACK)
#else
	ldr	sp, =(CONFIG_SYS_INIT_SP_ADDR)
#endif
R
rev13@wp.pl 已提交
78 79 80 81 82
#if defined(CONFIG_CPU_V7M)	/* v7M forbids using SP as BIC destination */
	mov	r3, sp
	bic	r3, r3, #7
	mov	sp, r3
#else
83
	bic	sp, sp, #7	/* 8-byte alignment for ABI compliance */
R
rev13@wp.pl 已提交
84
#endif
85
	mov	r0, sp
86
	bl	board_init_f_alloc_reserve
87
	mov	sp, r0
88 89
	/* set up gd here, outside any C code */
	mov	r9, r0
90
	bl	board_init_f_init_reserve
91

92 93 94 95 96 97 98
	mov	r0, #0
	bl	board_init_f

#if ! defined(CONFIG_SPL_BUILD)

/*
 * Set up intermediate environment (new sp and gd) and call
99 100
 * relocate_code(addr_moni). Trick here is that we'll return
 * 'here' but relocated.
101 102
 */

J
Jeroen Hofstee 已提交
103
	ldr	sp, [r9, #GD_START_ADDR_SP]	/* sp = gd->start_addr_sp */
R
rev13@wp.pl 已提交
104 105 106 107 108
#if defined(CONFIG_CPU_V7M)	/* v7M forbids using SP as BIC destination */
	mov	r3, sp
	bic	r3, r3, #7
	mov	sp, r3
#else
109
	bic	sp, sp, #7	/* 8-byte alignment for ABI compliance */
R
rev13@wp.pl 已提交
110
#endif
J
Jeroen Hofstee 已提交
111 112
	ldr	r9, [r9, #GD_BD]		/* r9 = gd->bd */
	sub	r9, r9, #GD_SIZE		/* new GD is below bd */
113 114

	adr	lr, here
J
Jeroen Hofstee 已提交
115
	ldr	r0, [r9, #GD_RELOC_OFF]		/* r0 = gd->reloc_off */
116
	add	lr, lr, r0
R
rev13@wp.pl 已提交
117 118 119
#if defined(CONFIG_CPU_V7M)
	orr	lr, #1				/* As required by Thumb-only */
#endif
J
Jeroen Hofstee 已提交
120
	ldr	r0, [r9, #GD_RELOCADDR]		/* r0 = gd->relocaddr */
121 122
	b	relocate_code
here:
123 124 125 126 127
/*
 * now relocate vectors
 */

	bl	relocate_vectors
128 129 130 131

/* Set up final (full) environment */

	bl	c_runtime_cpu_setup	/* we still call old routine here */
132 133 134 135 136 137 138
#endif
#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FRAMEWORK)
# ifdef CONFIG_SPL_BUILD
	/* Use a DRAM stack for the rest of SPL, if requested */
	bl	spl_relocate_stack_gd
	cmp	r0, #0
	movne	sp, r0
139
	movne	r9, r0
140
# endif
141 142
	ldr	r0, =__bss_start	/* this is auto-relocated! */

143 144 145 146 147 148 149 150
#ifdef CONFIG_USE_ARCH_MEMSET
	ldr	r3, =__bss_end		/* this is auto-relocated! */
	mov	r1, #0x00000000		/* prepare zero to clear BSS */

	subs	r2, r3, r0		/* r2 = memset len */
	bl	memset
#else
	ldr	r1, =__bss_end		/* this is auto-relocated! */
151 152 153
	mov	r2, #0x00000000		/* prepare zero to clear BSS */

clbss_l:cmp	r0, r1			/* while not at end of BSS */
R
rev13@wp.pl 已提交
154 155 156
#if defined(CONFIG_CPU_V7M)
	itt	lo
#endif
157 158 159
	strlo	r2, [r0]		/* clear 32-bit BSS word */
	addlo	r0, r0, #4		/* move to next */
	blo	clbss_l
160
#endif
161

162
#if ! defined(CONFIG_SPL_BUILD)
163 164
	bl coloured_LED_init
	bl red_led_on
165
#endif
166
	/* call board_init_r(gd_t *id, ulong dest_addr) */
J
Jeroen Hofstee 已提交
167 168
	mov     r0, r9                  /* gd_t */
	ldr	r1, [r9, #GD_RELOCADDR]	/* dest_addr */
169 170 171 172 173
	/* call board_init_r */
	ldr	pc, =board_init_r	/* this is auto-relocated! */

	/* we should not return here. */
#endif
174 175

ENDPROC(_main)