blob: a1c06108d72332ee2e4866ab7603642926fb19e6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk12490652004-04-18 21:13:41 +00002/*
Michal Simek922ce202007-03-11 13:48:24 +01003 * (C) Copyright 2007 Michal Simek
wdenk12490652004-04-18 21:13:41 +00004 * (C) Copyright 2004 Atmark Techno, Inc.
5 *
Michal Simek922ce202007-03-11 13:48:24 +01006 * Michal SIMEK <monstr@monstr.eu>
wdenk12490652004-04-18 21:13:41 +00007 * Yasushi SHOJI <yashi@atmark-techno.com>
wdenk12490652004-04-18 21:13:41 +00008 */
9
Wolfgang Denk0191e472010-10-26 14:34:52 +020010#include <asm-offsets.h>
wdenk12490652004-04-18 21:13:41 +000011#include <config.h>
12
13 .text
14 .global _start
15_start:
Michal Simek922ce202007-03-11 13:48:24 +010016 mts rmsr, r0 /* disable cache */
Michal Simek26acb3e2014-01-21 07:30:37 +010017
Ovidiu Panait76e51132021-11-30 18:33:49 +020018 addi r8, r0, _end
Michal Simeka5e9d6e2014-11-04 13:30:14 +010019 mts rslr, r8
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030020
Michal Simek26acb3e2014-01-21 07:30:37 +010021#if defined(CONFIG_SPL_BUILD)
22 addi r1, r0, CONFIG_SPL_STACK_ADDR
Michal Simek9ea67442015-01-30 15:46:43 +010023#else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020024 addi r1, r0, CONFIG_SYS_INIT_SP_OFFSET
Michal Simek9ea67442015-01-30 15:46:43 +010025#endif
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030026
Michal Simek1f0c40c2007-03-26 01:39:07 +020027 addi r1, r1, -4 /* Decrement SP to top of memory */
Michal Simekead124a2010-08-12 11:47:11 +020028
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030029 /* Call board_init_f_alloc_reserve with the current stack pointer as
30 * parameter. */
31 add r5, r0, r1
32 bralid r15, board_init_f_alloc_reserve
33 nop
34
35 /* board_init_f_alloc_reserve returns a pointer to the allocated area
36 * in r3. Set the new stack pointer below this area. */
37 add r1, r0, r3
38 mts rshr, r1
39 addi r1, r1, -4
40
41 /* Call board_init_f_init_reserve with the address returned by
42 * board_init_f_alloc_reserve as parameter. */
43 add r5, r0, r3
44 bralid r15, board_init_f_init_reserve
45 nop
46
47#if !defined(CONFIG_SPL_BUILD)
Ovidiu Panait98558352020-09-24 11:54:36 +030048 /* Setup vectors with pre-relocation symbols */
49 or r5, r0, r0
50 bralid r15, __setup_exceptions
51 nop
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030052#endif
Ovidiu Panait98558352020-09-24 11:54:36 +030053
54 /* Flush cache before enable cache */
55 addik r5, r0, 0
56 addik r6, r0, XILINX_DCACHE_BYTE_SIZE
57 bralid r15, flush_cache
58 nop
59
60 /* enable instruction and data cache */
61 mfs r12, rmsr
62 ori r12, r12, 0x1a0
63 mts rmsr, r12
64
Ovidiu Panait98558352020-09-24 11:54:36 +030065clear_bss:
66 /* clear BSS segments */
67 addi r5, r0, __bss_start
68 addi r4, r0, __bss_end
69 cmp r6, r5, r4
70 beqi r6, 3f
712:
72 swi r0, r5, 0 /* write zero to loc */
73 addi r5, r5, 4 /* increment to next loc */
74 cmp r6, r5, r4 /* check if we have reach the end */
75 bnei r6, 2b
763: /* jumping to board_init */
77#ifdef CONFIG_DEBUG_UART
78 bralid r15, debug_uart_init
79 nop
80#endif
81#ifndef CONFIG_SPL_BUILD
82 or r5, r0, r0 /* flags - empty */
Ovidiu Panait98558352020-09-24 11:54:36 +030083 brai board_init_f
84#else
Ovidiu Panait98558352020-09-24 11:54:36 +030085 brai board_init_r
86#endif
871: bri 1b
88
Ovidiu Panait98558352020-09-24 11:54:36 +030089#ifndef CONFIG_SPL_BUILD
90 .text
91 .ent __setup_exceptions
92 .align 2
93/*
94 * Set up reset, interrupt, user exception and hardware exception vectors.
95 *
96 * Parameters:
97 * r5 - relocation offset (zero when setting up vectors before
98 * relocation, and gd->reloc_off when setting up vectors after
99 * relocation)
100 * - the relocation offset is added to the _exception_handler,
101 * _interrupt_handler and _hw_exception_handler symbols to reflect the
102 * post-relocation memory addresses
103 *
104 * Reserve registers:
105 * r10: Stores little/big endian offset for vectors
106 * r2: Stores imm opcode
107 * r3: Stores brai opcode
108 */
109__setup_exceptions:
110 addik r1, r1, -28
111 swi r2, r1, 4
112 swi r3, r1, 8
113 swi r6, r1, 12
114 swi r7, r1, 16
115 swi r8, r1, 20
116 swi r10, r1, 24
117
Michal Simekead124a2010-08-12 11:47:11 +0200118 /* Find-out if u-boot is running on BIG/LITTLE endian platform
119 * There are some steps which is necessary to keep in mind:
120 * 1. Setup offset value to r6
121 * 2. Store word offset value to address 0x0
122 * 3. Load just byte from address 0x0
123 * 4a) LITTLE endian - r10 contains 0x2 because it is the smallest
124 * value that's why is on address 0x0
125 * 4b) BIG endian - r10 contains 0x0 because 0x2 offset is on addr 0x3
126 */
127 addik r6, r0, 0x2 /* BIG/LITTLE endian offset */
Ovidiu Panaitff759b32021-11-30 18:33:52 +0200128 sw r6, r1, r0
129 lbu r10, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200130
Michal Simek4a30db92011-07-21 10:47:21 +0200131 /* add opcode instruction for 32bit jump - 2 instruction imm & brai */
132 addi r2, r0, 0xb0000000 /* hex b000 opcode imm */
133 addi r3, r0, 0xb8080000 /* hew b808 opcode brai */
wdenk12490652004-04-18 21:13:41 +0000134
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200135#ifdef CONFIG_SYS_RESET_ADDRESS
Michal Simek922ce202007-03-11 13:48:24 +0100136 /* reset address */
Michal Simek4a30db92011-07-21 10:47:21 +0200137 swi r2, r0, 0x0 /* reset address - imm opcode */
138 swi r3, r0, 0x4 /* reset address - brai opcode */
139
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200140 addik r6, r0, CONFIG_SYS_RESET_ADDRESS
Michal Simek922ce202007-03-11 13:48:24 +0100141 sw r6, r1, r0
Michal Simek8daf0c32011-08-30 15:22:24 +0200142 lhu r7, r1, r10
143 rsubi r8, r10, 0x2
144 sh r7, r0, r8
145 rsubi r8, r10, 0x6
146 sh r6, r0, r8
Michal Simek922ce202007-03-11 13:48:24 +0100147#endif
148
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200149#ifdef CONFIG_SYS_USR_EXCEP
Michal Simek922ce202007-03-11 13:48:24 +0100150 /* user_vector_exception */
Michal Simek4a30db92011-07-21 10:47:21 +0200151 swi r2, r0, 0x8 /* user vector exception - imm opcode */
152 swi r3, r0, 0xC /* user vector exception - brai opcode */
153
Ovidiu Panait98558352020-09-24 11:54:36 +0300154 addik r6, r5, _exception_handler
Michal Simek922ce202007-03-11 13:48:24 +0100155 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200156 /*
157 * BIG ENDIAN memory map for user exception
158 * 0x8: 0xB000XXXX
159 * 0xC: 0xB808XXXX
160 *
161 * then it is necessary to count address for storing the most significant
Wolfgang Denk1136f692010-10-27 22:48:30 +0200162 * 16bits from _exception_handler address and copy it to
Michal Simekead124a2010-08-12 11:47:11 +0200163 * 0xa address. Big endian use offset in r10=0 that's why is it just
164 * 0xa address. The same is done for the least significant 16 bits
165 * for 0xe address.
166 *
167 * LITTLE ENDIAN memory map for user exception
168 * 0x8: 0xXXXX00B0
169 * 0xC: 0xXXXX08B8
170 *
171 * Offset is for little endian setup to 0x2. rsubi instruction decrease
172 * address value to ensure that points to proper place which is
173 * 0x8 for the most significant 16 bits and
174 * 0xC for the least significant 16 bits
175 */
176 lhu r7, r1, r10
177 rsubi r8, r10, 0xa
178 sh r7, r0, r8
179 rsubi r8, r10, 0xe
180 sh r6, r0, r8
Michal Simek922ce202007-03-11 13:48:24 +0100181#endif
182
Michal Simek922ce202007-03-11 13:48:24 +0100183 /* interrupt_handler */
Michal Simek4a30db92011-07-21 10:47:21 +0200184 swi r2, r0, 0x10 /* interrupt - imm opcode */
185 swi r3, r0, 0x14 /* interrupt - brai opcode */
186
Ovidiu Panait98558352020-09-24 11:54:36 +0300187 addik r6, r5, _interrupt_handler
Michal Simek922ce202007-03-11 13:48:24 +0100188 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200189 lhu r7, r1, r10
190 rsubi r8, r10, 0x12
191 sh r7, r0, r8
192 rsubi r8, r10, 0x16
193 sh r6, r0, r8
wdenk12490652004-04-18 21:13:41 +0000194
Michal Simek922ce202007-03-11 13:48:24 +0100195 /* hardware exception */
Michal Simek4a30db92011-07-21 10:47:21 +0200196 swi r2, r0, 0x20 /* hardware exception - imm opcode */
197 swi r3, r0, 0x24 /* hardware exception - brai opcode */
198
Ovidiu Panait98558352020-09-24 11:54:36 +0300199 addik r6, r5, _hw_exception_handler
Michal Simek922ce202007-03-11 13:48:24 +0100200 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200201 lhu r7, r1, r10
202 rsubi r8, r10, 0x22
203 sh r7, r0, r8
204 rsubi r8, r10, 0x26
205 sh r6, r0, r8
Michal Simek922ce202007-03-11 13:48:24 +0100206
Ovidiu Panait98558352020-09-24 11:54:36 +0300207 lwi r10, r1, 24
208 lwi r8, r1, 20
209 lwi r7, r1, 16
210 lwi r6, r1, 12
211 lwi r3, r1, 8
212 lwi r2, r1, 4
213 addik r1, r1, 28
Michal Simeke3aa3d52012-09-25 10:13:35 +0200214
Ovidiu Panait98558352020-09-24 11:54:36 +0300215 rtsd r15, 8
216 or r0, r0, r0
217 .end __setup_exceptions
Michal Simeka79d6582015-01-30 15:45:02 +0100218
Michal Simek04ae75f2007-04-21 21:02:40 +0200219/*
220 * Read 16bit little endian
221 */
222 .text
223 .global in16
224 .ent in16
225 .align 2
226in16: lhu r3, r0, r5
227 bslli r4, r3, 8
228 bsrli r3, r3, 8
229 andi r4, r4, 0xffff
230 or r3, r3, r4
231 rtsd r15, 8
232 sext16 r3, r3
233 .end in16
234
235/*
236 * Write 16bit little endian
237 * first parameter(r5) - address, second(r6) - short value
238 */
239 .text
240 .global out16
241 .ent out16
242 .align 2
243out16: bslli r3, r6, 8
244 bsrli r6, r6, 8
245 andi r3, r3, 0xffff
246 or r3, r3, r6
247 sh r3, r0, r5
248 rtsd r15, 8
249 or r0, r0, r0
250 .end out16
Michal Simek65e915c2014-05-08 16:08:44 +0200251
252/*
253 * Relocate u-boot
254 */
255 .text
256 .global relocate_code
257 .ent relocate_code
258 .align 2
259relocate_code:
260 /*
261 * r5 - start_addr_sp
262 * r6 - new_gd
263 * r7 - reloc_addr
264 */
265 addi r1, r5, 0 /* Start to use new SP */
266 addi r31, r6, 0 /* Start to use new GD */
267
268 add r23, r0, r7 /* Move reloc addr to r23 */
269 /* Relocate text and data - r12 temp value */
270 addi r21, r0, _start
Ovidiu Panait76e51132021-11-30 18:33:49 +0200271 addi r22, r0, _end - 4 /* Include BSS too */
Michal Simeka9228f62015-01-27 15:10:37 +0100272
273 rsub r6, r21, r22
274 or r5, r0, r0
2751: lw r12, r21, r5 /* Load u-boot data */
276 sw r12, r23, r5 /* Write zero to loc */
277 cmp r12, r5, r6 /* Check if we have reach the end */
Michal Simek65e915c2014-05-08 16:08:44 +0200278 bneid r12, 1b
Michal Simeka9228f62015-01-27 15:10:37 +0100279 addi r5, r5, 4 /* Increment to next loc - relocate code */
Michal Simek65e915c2014-05-08 16:08:44 +0200280
Michal Simek55df7da2019-10-21 12:20:16 +0200281 /* R23 points to the base address. */
Michal Simek65e915c2014-05-08 16:08:44 +0200282 add r23, r0, r7 /* Move reloc addr to r23 */
283 addi r24, r0, CONFIG_SYS_TEXT_BASE /* Get reloc offset */
284 rsub r23, r24, r23 /* keep - this is already here gd->reloc_off */
285
Ovidiu Panait98558352020-09-24 11:54:36 +0300286 /* Setup vectors with post-relocation symbols */
287 add r5, r0, r23 /* load gd->reloc_off to r5 */
288 bralid r15, __setup_exceptions
289 nop
Michal Simek65e915c2014-05-08 16:08:44 +0200290
291 /* Check if GOT exist */
292 addik r21, r23, _got_start
293 addik r22, r23, _got_end
294 cmpu r12, r21, r22
295 beqi r12, 2f /* No GOT table - jump over */
296
297 /* Skip last 3 entries plus 1 because of loop boundary below */
298 addik r22, r22, -0x10
299
300 /* Relocate the GOT. */
3013: lw r12, r21, r0 /* Load entry */
302 addk r12, r12, r23 /* Add reloc offset */
303 sw r12, r21, r0 /* Save entry back */
304
305 cmpu r12, r21, r22 /* Check if this cross boundary */
306 bneid r12, 3b
307 addik r21. r21, 4
308
309 /* Update pointer to GOT */
310 mfs r20, rpc
311 addik r20, r20, _GLOBAL_OFFSET_TABLE_ + 8
312 addk r20, r20, r23
313
314 /* Flush caches to ensure consistency */
315 addik r5, r0, 0
316 addik r6, r0, XILINX_DCACHE_BYTE_SIZE
317 bralid r15, flush_cache
318 nop
319
3202: addi r5, r31, 0 /* gd is initialized in board_r.c */
321 addi r6, r0, CONFIG_SYS_TEXT_BASE
322 addi r12, r23, board_init_r
323 bra r12 /* Jump to relocated code */
324
325 .end relocate_code
Michal Simek26acb3e2014-01-21 07:30:37 +0100326#endif