blob: 88b4aaa1c0e9454aef2663abdd6963ce1ea41b32 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Rick Chene76b8042017-12-26 13:55:48 +08002/*
3 * Startup Code for RISC-V Core
4 *
5 * Copyright (c) 2017 Microsemi Corporation.
6 * Copyright (c) 2017 Padmarao Begari <Padmarao.Begari@microsemi.com>
7 *
8 * Copyright (C) 2017 Andes Technology Corporation
9 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
Rick Chene76b8042017-12-26 13:55:48 +080010 */
11
12#include <asm-offsets.h>
13#include <config.h>
14#include <common.h>
15#include <elf.h>
16#include <asm/encoding.h>
17
18#ifdef CONFIG_32BIT
Lukas Auer7cf43682018-11-22 11:26:24 +010019#define LREG lw
20#define SREG sw
21#define REGBYTES 4
Rick Chene76b8042017-12-26 13:55:48 +080022#define RELOC_TYPE R_RISCV_32
23#define SYM_INDEX 0x8
24#define SYM_SIZE 0x10
25#else
Lukas Auer7cf43682018-11-22 11:26:24 +010026#define LREG ld
27#define SREG sd
28#define REGBYTES 8
Rick Chene76b8042017-12-26 13:55:48 +080029#define RELOC_TYPE R_RISCV_64
30#define SYM_INDEX 0x20
31#define SYM_SIZE 0x18
32#endif
33
Lukas Auer7cf43682018-11-22 11:26:24 +010034.section .text
Rick Chene76b8042017-12-26 13:55:48 +080035.globl _start
36_start:
Lukas Auer7cf43682018-11-22 11:26:24 +010037 j handle_reset
Rick Chene76b8042017-12-26 13:55:48 +080038
39nmi_vector:
Lukas Auer7cf43682018-11-22 11:26:24 +010040 j nmi_vector
Rick Chene76b8042017-12-26 13:55:48 +080041
42trap_vector:
Lukas Auer7cf43682018-11-22 11:26:24 +010043 j trap_entry
Rick Chene76b8042017-12-26 13:55:48 +080044
Rick Chene76b8042017-12-26 13:55:48 +080045handle_reset:
Lukas Auer7cf43682018-11-22 11:26:24 +010046 li t0, CONFIG_SYS_SDRAM_BASE
47 SREG a2, 0(t0)
48 la t0, trap_entry
49 csrw mtvec, t0
50 csrwi mstatus, 0
51 csrwi mie, 0
Rick Chene76b8042017-12-26 13:55:48 +080052
53/*
54 * Do CPU critical regs init only at reboot,
55 * not when booting from ram
56 */
57#ifdef CONFIG_INIT_CRITICAL
Lukas Auer7cf43682018-11-22 11:26:24 +010058 jal cpu_init_crit /* Do CPU critical regs init */
Rick Chene76b8042017-12-26 13:55:48 +080059#endif
60
61/*
62 * Set stackpointer in internal/ex RAM to call board_init_f
63 */
64call_board_init_f:
Lukas Auer7cf43682018-11-22 11:26:24 +010065 li t0, -16
66 li t1, CONFIG_SYS_INIT_SP_ADDR
67 and sp, t1, t0 /* force 16 byte alignment */
Rick Chene76b8042017-12-26 13:55:48 +080068
69#ifdef CONFIG_DEBUG_UART
70 jal debug_uart_init
71#endif
72
73call_board_init_f_0:
74 mv a0, sp
75 jal board_init_f_alloc_reserve
76 mv sp, a0
77 jal board_init_f_init_reserve
78
Lukas Auer7cf43682018-11-22 11:26:24 +010079 mv a0, zero /* a0 <-- boot_flags = 0 */
80 la t5, board_init_f
81 jr t5 /* jump to board_init_f() */
Rick Chene76b8042017-12-26 13:55:48 +080082
83/*
84 * void relocate_code (addr_sp, gd, addr_moni)
85 *
86 * This "function" does not return, instead it continues in RAM
87 * after relocating the monitor code.
88 *
89 */
90.globl relocate_code
91relocate_code:
Lukas Auer7cf43682018-11-22 11:26:24 +010092 mv s2, a0 /* save addr_sp */
93 mv s3, a1 /* save addr of gd */
94 mv s4, a2 /* save addr of destination */
Rick Chene76b8042017-12-26 13:55:48 +080095
96/*
97 *Set up the stack
98 */
99stack_setup:
Lukas Auer7cf43682018-11-22 11:26:24 +0100100 mv sp, s2
101 la t0, _start
102 sub t6, s4, t0 /* t6 <- relocation offset */
103 beq t0, s4, clear_bss /* skip relocation */
Rick Chene76b8042017-12-26 13:55:48 +0800104
Lukas Auer7cf43682018-11-22 11:26:24 +0100105 mv t1, s4 /* t1 <- scratch for copy_loop */
106 la t3, __bss_start
107 sub t3, t3, t0 /* t3 <- __bss_start_ofs */
108 add t2, t0, t3 /* t2 <- source end address */
Rick Chene76b8042017-12-26 13:55:48 +0800109
110copy_loop:
Lukas Auer7cf43682018-11-22 11:26:24 +0100111 LREG t5, 0(t0)
112 addi t0, t0, REGBYTES
113 SREG t5, 0(t1)
114 addi t1, t1, REGBYTES
115 blt t0, t2, copy_loop
Rick Chene76b8042017-12-26 13:55:48 +0800116
117/*
118 * Update dynamic relocations after board_init_f
119 */
120fix_rela_dyn:
Lukas Auer7cf43682018-11-22 11:26:24 +0100121 la t1, __rel_dyn_start
122 la t2, __rel_dyn_end
123 beq t1, t2, clear_bss
124 add t1, t1, t6 /* t1 <- rela_dyn_start in RAM */
125 add t2, t2, t6 /* t2 <- rela_dyn_end in RAM */
Rick Chene76b8042017-12-26 13:55:48 +0800126
127/*
128 * skip first reserved entry: address, type, addend
129 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100130 bne t1, t2, 7f
Rick Chene76b8042017-12-26 13:55:48 +0800131
1326:
Lukas Auer7cf43682018-11-22 11:26:24 +0100133 LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
134 li t3, R_RISCV_RELATIVE /* reloc type R_RISCV_RELATIVE */
135 bne t5, t3, 8f /* skip non-RISCV_RELOC entries */
136 LREG t3, -(REGBYTES*3)(t1)
137 LREG t5, -(REGBYTES)(t1) /* t5 <-- addend */
138 add t5, t5, t6 /* t5 <-- location to fix up in RAM */
139 add t3, t3, t6 /* t3 <-- location to fix up in RAM */
140 SREG t5, 0(t3)
Rick Chene76b8042017-12-26 13:55:48 +08001417:
Lukas Auer7cf43682018-11-22 11:26:24 +0100142 addi t1, t1, (REGBYTES*3)
143 ble t1, t2, 6b
Rick Chene76b8042017-12-26 13:55:48 +0800144
1458:
Lukas Auer7cf43682018-11-22 11:26:24 +0100146 la t4, __dyn_sym_start
147 add t4, t4, t6
Rick Chene76b8042017-12-26 13:55:48 +0800148
1499:
Lukas Auer7cf43682018-11-22 11:26:24 +0100150 LREG t5, -(REGBYTES*2)(t1) /* t5 <-- relocation info:type */
151 srli t0, t5, SYM_INDEX /* t0 <--- sym table index */
152 andi t5, t5, 0xFF /* t5 <--- relocation type */
153 li t3, RELOC_TYPE
154 bne t5, t3, 10f /* skip non-addned entries */
Rick Chene76b8042017-12-26 13:55:48 +0800155
Lukas Auer7cf43682018-11-22 11:26:24 +0100156 LREG t3, -(REGBYTES*3)(t1)
157 li t5, SYM_SIZE
158 mul t0, t0, t5
159 add s1, t4, t0
160 LREG t5, REGBYTES(s1)
161 add t5, t5, t6 /* t5 <-- location to fix up in RAM */
162 add t3, t3, t6 /* t3 <-- location to fix up in RAM */
163 SREG t5, 0(t3)
Rick Chene76b8042017-12-26 13:55:48 +080016410:
Lukas Auer7cf43682018-11-22 11:26:24 +0100165 addi t1, t1, (REGBYTES*3)
166 ble t1, t2, 9b
Rick Chene76b8042017-12-26 13:55:48 +0800167
168/*
169 * trap update
170*/
Lukas Auer7cf43682018-11-22 11:26:24 +0100171 la t0, trap_entry
172 add t0, t0, t6
173 csrw mtvec, t0
Rick Chene76b8042017-12-26 13:55:48 +0800174
175clear_bss:
Lukas Auer7cf43682018-11-22 11:26:24 +0100176 la t0, __bss_start /* t0 <- rel __bss_start in FLASH */
177 add t0, t0, t6 /* t0 <- rel __bss_start in RAM */
178 la t1, __bss_end /* t1 <- rel __bss_end in FLASH */
179 add t1, t1, t6 /* t1 <- rel __bss_end in RAM */
180 li t2, 0x00000000 /* clear */
181 beq t0, t1, call_board_init_r
Rick Chene76b8042017-12-26 13:55:48 +0800182
183clbss_l:
Lukas Auer7cf43682018-11-22 11:26:24 +0100184 SREG t2, 0(t0) /* clear loop... */
185 addi t0, t0, REGBYTES
186 bne t0, t1, clbss_l
Rick Chene76b8042017-12-26 13:55:48 +0800187
188/*
189 * We are done. Do not return, instead branch to second part of board
190 * initialization, now running from RAM.
191 */
192call_board_init_r:
Lukas Auer7cf43682018-11-22 11:26:24 +0100193 la t0, board_init_r
194 mv t4, t0 /* offset of board_init_r() */
195 add t4, t4, t6 /* real address of board_init_r() */
Rick Chene76b8042017-12-26 13:55:48 +0800196/*
197 * setup parameters for board_init_r
198 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100199 mv a0, s3 /* gd_t */
200 mv a1, s4 /* dest_addr */
Rick Chene76b8042017-12-26 13:55:48 +0800201
202/*
203 * jump to it ...
204 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100205 jr t4 /* jump to board_init_r() */
Rick Chene76b8042017-12-26 13:55:48 +0800206
207/*
208 * trap entry
209 */
Lukas Aueraf512852018-11-22 11:26:25 +0100210.align 2
Rick Chene76b8042017-12-26 13:55:48 +0800211trap_entry:
Lukas Auer7cf43682018-11-22 11:26:24 +0100212 addi sp, sp, -32*REGBYTES
213 SREG x1, 1*REGBYTES(sp)
214 SREG x2, 2*REGBYTES(sp)
215 SREG x3, 3*REGBYTES(sp)
216 SREG x4, 4*REGBYTES(sp)
217 SREG x5, 5*REGBYTES(sp)
218 SREG x6, 6*REGBYTES(sp)
219 SREG x7, 7*REGBYTES(sp)
220 SREG x8, 8*REGBYTES(sp)
221 SREG x9, 9*REGBYTES(sp)
222 SREG x10, 10*REGBYTES(sp)
223 SREG x11, 11*REGBYTES(sp)
224 SREG x12, 12*REGBYTES(sp)
225 SREG x13, 13*REGBYTES(sp)
226 SREG x14, 14*REGBYTES(sp)
227 SREG x15, 15*REGBYTES(sp)
228 SREG x16, 16*REGBYTES(sp)
229 SREG x17, 17*REGBYTES(sp)
230 SREG x18, 18*REGBYTES(sp)
231 SREG x19, 19*REGBYTES(sp)
232 SREG x20, 20*REGBYTES(sp)
233 SREG x21, 21*REGBYTES(sp)
234 SREG x22, 22*REGBYTES(sp)
235 SREG x23, 23*REGBYTES(sp)
236 SREG x24, 24*REGBYTES(sp)
237 SREG x25, 25*REGBYTES(sp)
238 SREG x26, 26*REGBYTES(sp)
239 SREG x27, 27*REGBYTES(sp)
240 SREG x28, 28*REGBYTES(sp)
241 SREG x29, 29*REGBYTES(sp)
242 SREG x30, 30*REGBYTES(sp)
243 SREG x31, 31*REGBYTES(sp)
244 csrr a0, mcause
245 csrr a1, mepc
246 mv a2, sp
247 jal handle_trap
248 csrw mepc, a0
Rick Chene76b8042017-12-26 13:55:48 +0800249
250/*
251 * Remain in M-mode after mret
252 */
Lukas Auer7cf43682018-11-22 11:26:24 +0100253 li t0, MSTATUS_MPP
254 csrs mstatus, t0
255 LREG x1, 1*REGBYTES(sp)
256 LREG x2, 2*REGBYTES(sp)
257 LREG x3, 3*REGBYTES(sp)
258 LREG x4, 4*REGBYTES(sp)
259 LREG x5, 5*REGBYTES(sp)
260 LREG x6, 6*REGBYTES(sp)
261 LREG x7, 7*REGBYTES(sp)
262 LREG x8, 8*REGBYTES(sp)
263 LREG x9, 9*REGBYTES(sp)
264 LREG x10, 10*REGBYTES(sp)
265 LREG x11, 11*REGBYTES(sp)
266 LREG x12, 12*REGBYTES(sp)
267 LREG x13, 13*REGBYTES(sp)
268 LREG x14, 14*REGBYTES(sp)
269 LREG x15, 15*REGBYTES(sp)
270 LREG x16, 16*REGBYTES(sp)
271 LREG x17, 17*REGBYTES(sp)
272 LREG x18, 18*REGBYTES(sp)
273 LREG x19, 19*REGBYTES(sp)
274 LREG x20, 20*REGBYTES(sp)
275 LREG x21, 21*REGBYTES(sp)
276 LREG x22, 22*REGBYTES(sp)
277 LREG x23, 23*REGBYTES(sp)
278 LREG x24, 24*REGBYTES(sp)
279 LREG x25, 25*REGBYTES(sp)
280 LREG x26, 26*REGBYTES(sp)
281 LREG x27, 27*REGBYTES(sp)
282 LREG x28, 28*REGBYTES(sp)
283 LREG x29, 29*REGBYTES(sp)
284 LREG x30, 30*REGBYTES(sp)
285 LREG x31, 31*REGBYTES(sp)
286 addi sp, sp, 32*REGBYTES
Rick Chene76b8042017-12-26 13:55:48 +0800287 mret
288
289#ifdef CONFIG_INIT_CRITICAL
290cpu_init_crit:
291 ret
292#endif