blob: 9a661e785f6d7e6edf6ad9a7cf6c9eba343b1960 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk507bbe32004-04-18 21:13:41 +00002/*
Michal Simekcfc67112007-03-11 13:48:24 +01003 * (C) Copyright 2007 Michal Simek
wdenk507bbe32004-04-18 21:13:41 +00004 * (C) Copyright 2004 Atmark Techno, Inc.
5 *
Michal Simekcfc67112007-03-11 13:48:24 +01006 * Michal SIMEK <monstr@monstr.eu>
wdenk507bbe32004-04-18 21:13:41 +00007 * Yasushi SHOJI <yashi@atmark-techno.com>
wdenk507bbe32004-04-18 21:13:41 +00008 */
9
Wolfgang Denk25ddd1f2010-10-26 14:34:52 +020010#include <asm-offsets.h>
wdenk507bbe32004-04-18 21:13:41 +000011#include <config.h>
12
Michal Simekd58c0072022-06-24 14:15:01 +020013#if defined(CONFIG_STATIC_RELA)
14#define SYM_ADDR(reg, reg_add, symbol) \
15 mfs r20, rpc; \
16 addik r20, r20, _GLOBAL_OFFSET_TABLE_ + 8; \
17 lwi reg, r20, symbol@GOT; \
18 addk reg, reg reg_add;
19#else
Michal Simek07c052b2022-06-24 14:15:00 +020020#define SYM_ADDR(reg, reg_add, symbol) \
21 addi reg, reg_add, symbol
Michal Simekd58c0072022-06-24 14:15:01 +020022#endif
Michal Simek07c052b2022-06-24 14:15:00 +020023
wdenk507bbe32004-04-18 21:13:41 +000024 .text
25 .global _start
26_start:
Michal Simekcfc67112007-03-11 13:48:24 +010027 mts rmsr, r0 /* disable cache */
Michal Simekb6fe10a2022-06-24 14:15:00 +020028 mfs r20, rpc
29 addi r20, r20, -4
Michal Simek9d242742014-01-21 07:30:37 +010030
Michal Simek16a18472022-06-24 14:14:59 +020031 mts rslr, r0
Michal Simekb6fe10a2022-06-24 14:15:00 +020032 mts rshr, r20
Ovidiu Panaitf5d8b1a2020-09-24 11:54:37 +030033
Michal Simek9d242742014-01-21 07:30:37 +010034#if defined(CONFIG_SPL_BUILD)
35 addi r1, r0, CONFIG_SPL_STACK_ADDR
Michal Simek405e6512015-01-30 15:46:43 +010036#else
Michal Simekb6fe10a2022-06-24 14:15:00 +020037 add r1, r0, r20
Michal Simekd58c0072022-06-24 14:15:01 +020038#if defined(CONFIG_STATIC_RELA)
39 bri 1f
40
41 /* Force alignment for easier ASM code below */
42#define ALIGNMENT_ADDR 0x20
43 .align 4
44uboot_dyn_start:
45 .word __rel_dyn_start
46
47uboot_dyn_end:
48 .word __rel_dyn_end
49
50uboot_sym_start:
51 .word __dyn_sym_start
521:
53
54 addi r5, r20, 0
55 add r6, r0, r0
56
57 lwi r7, r20, ALIGNMENT_ADDR
58 addi r7, r7, -CONFIG_SYS_TEXT_BASE
59 add r7, r7, r5
60 lwi r8, r20, ALIGNMENT_ADDR + 0x4
61 addi r8, r8, -CONFIG_SYS_TEXT_BASE
62 add r8, r8, r5
63 lwi r9, r20, ALIGNMENT_ADDR + 0x8
64 addi r9, r9, -CONFIG_SYS_TEXT_BASE
65 add r9, r9, r5
66 addi r10, r0, CONFIG_SYS_TEXT_BASE
67
68 brlid r15, mb_fix_rela
69 nop
70#endif
Michal Simek405e6512015-01-30 15:46:43 +010071#endif
Ovidiu Panaitf5d8b1a2020-09-24 11:54:37 +030072
Michal Simek17980492007-03-26 01:39:07 +020073 addi r1, r1, -4 /* Decrement SP to top of memory */
Michal Simekb98cba02010-08-12 11:47:11 +020074
Ovidiu Panaitf5d8b1a2020-09-24 11:54:37 +030075 /* Call board_init_f_alloc_reserve with the current stack pointer as
76 * parameter. */
77 add r5, r0, r1
Michal Simek7cf236c2022-06-24 14:14:59 +020078 brlid r15, board_init_f_alloc_reserve
Ovidiu Panaitf5d8b1a2020-09-24 11:54:37 +030079 nop
80
81 /* board_init_f_alloc_reserve returns a pointer to the allocated area
82 * in r3. Set the new stack pointer below this area. */
83 add r1, r0, r3
84 mts rshr, r1
85 addi r1, r1, -4
86
87 /* Call board_init_f_init_reserve with the address returned by
88 * board_init_f_alloc_reserve as parameter. */
89 add r5, r0, r3
Michal Simek7cf236c2022-06-24 14:14:59 +020090 brlid r15, board_init_f_init_reserve
Ovidiu Panaitf5d8b1a2020-09-24 11:54:37 +030091 nop
92
93#if !defined(CONFIG_SPL_BUILD)
Ovidiu Panait627085e2020-09-24 11:54:36 +030094 /* Setup vectors with pre-relocation symbols */
95 or r5, r0, r0
Michal Simek7cf236c2022-06-24 14:14:59 +020096 brlid r15, __setup_exceptions
Ovidiu Panait627085e2020-09-24 11:54:36 +030097 nop
Ovidiu Panaitf5d8b1a2020-09-24 11:54:37 +030098#endif
Michal Simekcfc67112007-03-11 13:48:24 +010099
Michal Simek58118302012-09-25 10:13:35 +0200100 /* Flush cache before enable cache */
101 addik r5, r0, 0
102 addik r6, r0, XILINX_DCACHE_BYTE_SIZE
Michal Simek7cf236c2022-06-24 14:14:59 +0200103 brlid r15, flush_cache
Michal Simek58118302012-09-25 10:13:35 +0200104 nop
105
Michal Simekcfc67112007-03-11 13:48:24 +0100106 /* enable instruction and data cache */
107 mfs r12, rmsr
Michal Simek822d43a2014-11-04 13:27:52 +0100108 ori r12, r12, 0x1a0
Michal Simekcfc67112007-03-11 13:48:24 +0100109 mts rmsr, r12
110
Michal Simek17980492007-03-26 01:39:07 +0200111clear_bss:
112 /* clear BSS segments */
Michal Simek07c052b2022-06-24 14:15:00 +0200113 SYM_ADDR(r5, r0, __bss_start)
114 SYM_ADDR(r4, r0, __bss_end)
Michal Simek17980492007-03-26 01:39:07 +0200115 cmp r6, r5, r4
116 beqi r6, 3f
1172:
118 swi r0, r5, 0 /* write zero to loc */
119 addi r5, r5, 4 /* increment to next loc */
120 cmp r6, r5, r4 /* check if we have reach the end */
121 bnei r6, 2b
1223: /* jumping to board_init */
Michal Simek48470b72015-12-10 12:55:39 +0100123#ifdef CONFIG_DEBUG_UART
Michal Simek7cf236c2022-06-24 14:14:59 +0200124 brlid r15, debug_uart_init
Michal Simek48470b72015-12-10 12:55:39 +0100125 nop
126#endif
Michal Simek9d242742014-01-21 07:30:37 +0100127#ifndef CONFIG_SPL_BUILD
Michal Simeke945f6d2014-05-08 16:08:44 +0200128 or r5, r0, r0 /* flags - empty */
Michal Simek7cf236c2022-06-24 14:14:59 +0200129 bri board_init_f
Michal Simek9d242742014-01-21 07:30:37 +0100130#else
Michal Simek7cf236c2022-06-24 14:14:59 +0200131 bri board_init_r
Michal Simek9d242742014-01-21 07:30:37 +0100132#endif
wdenk507bbe32004-04-18 21:13:41 +00001331: bri 1b
Michal Simek06436312007-04-21 21:02:40 +0200134
Michal Simek9d242742014-01-21 07:30:37 +0100135#ifndef CONFIG_SPL_BUILD
Ovidiu Panait627085e2020-09-24 11:54:36 +0300136 .text
137 .ent __setup_exceptions
138 .align 2
139/*
140 * Set up reset, interrupt, user exception and hardware exception vectors.
141 *
142 * Parameters:
143 * r5 - relocation offset (zero when setting up vectors before
144 * relocation, and gd->reloc_off when setting up vectors after
145 * relocation)
146 * - the relocation offset is added to the _exception_handler,
147 * _interrupt_handler and _hw_exception_handler symbols to reflect the
148 * post-relocation memory addresses
149 *
150 * Reserve registers:
151 * r10: Stores little/big endian offset for vectors
152 * r2: Stores imm opcode
153 * r3: Stores brai opcode
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200154 * r4: Stores the vector base address
Ovidiu Panait627085e2020-09-24 11:54:36 +0300155 */
156__setup_exceptions:
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200157 addik r1, r1, -32
Ovidiu Panait627085e2020-09-24 11:54:36 +0300158 swi r2, r1, 4
159 swi r3, r1, 8
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200160 swi r4, r1, 12
161 swi r6, r1, 16
162 swi r7, r1, 20
163 swi r8, r1, 24
164 swi r10, r1, 28
Ovidiu Panait627085e2020-09-24 11:54:36 +0300165
166 /* Find-out if u-boot is running on BIG/LITTLE endian platform
167 * There are some steps which is necessary to keep in mind:
168 * 1. Setup offset value to r6
169 * 2. Store word offset value to address 0x0
170 * 3. Load just byte from address 0x0
171 * 4a) LITTLE endian - r10 contains 0x2 because it is the smallest
172 * value that's why is on address 0x0
173 * 4b) BIG endian - r10 contains 0x0 because 0x2 offset is on addr 0x3
174 */
175 addik r6, r0, 0x2 /* BIG/LITTLE endian offset */
Ovidiu Panait48039c32021-11-30 18:33:52 +0200176 sw r6, r1, r0
177 lbu r10, r1, r0
Ovidiu Panait627085e2020-09-24 11:54:36 +0300178
179 /* add opcode instruction for 32bit jump - 2 instruction imm & brai */
180 addi r2, r0, 0xb0000000 /* hex b000 opcode imm */
181 addi r3, r0, 0xb8080000 /* hew b808 opcode brai */
182
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200183 /* Store the vector base address in r4 */
184 addi r4, r0, CONFIG_XILINX_MICROBLAZE0_VECTOR_BASE_ADDR
185
Ovidiu Panait627085e2020-09-24 11:54:36 +0300186 /* reset address */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200187 swi r2, r4, 0x0 /* reset address - imm opcode */
188 swi r3, r4, 0x4 /* reset address - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300189
Michal Simek07c052b2022-06-24 14:15:00 +0200190 SYM_ADDR(r6, r0, _start)
Michal Simek81169ae2022-06-24 14:15:00 +0200191 /* Intentionally keep reset vector back to origin u-boot location */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300192 sw r6, r1, r0
193 lhu r7, r1, r10
194 rsubi r8, r10, 0x2
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200195 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300196 rsubi r8, r10, 0x6
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200197 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300198
Ovidiu Panait83b175b2021-11-30 18:33:54 +0200199#if CONFIG_IS_ENABLED(XILINX_MICROBLAZE0_USR_EXCEP)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300200 /* user_vector_exception */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200201 swi r2, r4, 0x8 /* user vector exception - imm opcode */
202 swi r3, r4, 0xC /* user vector exception - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300203
Michal Simek07c052b2022-06-24 14:15:00 +0200204 SYM_ADDR(r6, r5, _exception_handler)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300205 sw r6, r1, r0
206 /*
207 * BIG ENDIAN memory map for user exception
208 * 0x8: 0xB000XXXX
209 * 0xC: 0xB808XXXX
210 *
211 * then it is necessary to count address for storing the most significant
212 * 16bits from _exception_handler address and copy it to
213 * 0xa address. Big endian use offset in r10=0 that's why is it just
214 * 0xa address. The same is done for the least significant 16 bits
215 * for 0xe address.
216 *
217 * LITTLE ENDIAN memory map for user exception
218 * 0x8: 0xXXXX00B0
219 * 0xC: 0xXXXX08B8
220 *
221 * Offset is for little endian setup to 0x2. rsubi instruction decrease
222 * address value to ensure that points to proper place which is
223 * 0x8 for the most significant 16 bits and
224 * 0xC for the least significant 16 bits
225 */
226 lhu r7, r1, r10
227 rsubi r8, r10, 0xa
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200228 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300229 rsubi r8, r10, 0xe
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200230 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300231#endif
232
233 /* interrupt_handler */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200234 swi r2, r4, 0x10 /* interrupt - imm opcode */
235 swi r3, r4, 0x14 /* interrupt - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300236
Michal Simek07c052b2022-06-24 14:15:00 +0200237 SYM_ADDR(r6, r5, _interrupt_handler)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300238 sw r6, r1, r0
239 lhu r7, r1, r10
240 rsubi r8, r10, 0x12
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200241 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300242 rsubi r8, r10, 0x16
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200243 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300244
245 /* hardware exception */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200246 swi r2, r4, 0x20 /* hardware exception - imm opcode */
247 swi r3, r4, 0x24 /* hardware exception - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300248
Michal Simek07c052b2022-06-24 14:15:00 +0200249 SYM_ADDR(r6, r5, _hw_exception_handler)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300250 sw r6, r1, r0
251 lhu r7, r1, r10
252 rsubi r8, r10, 0x22
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200253 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300254 rsubi r8, r10, 0x26
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200255 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300256
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200257 lwi r10, r1, 28
258 lwi r8, r1, 24
259 lwi r7, r1, 20
260 lwi r6, r1, 16
261 lwi r4, r1, 12
Ovidiu Panait627085e2020-09-24 11:54:36 +0300262 lwi r3, r1, 8
263 lwi r2, r1, 4
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200264 addik r1, r1, 32
Ovidiu Panait627085e2020-09-24 11:54:36 +0300265
266 rtsd r15, 8
267 or r0, r0, r0
268 .end __setup_exceptions
269
Michal Simek06436312007-04-21 21:02:40 +0200270/*
271 * Read 16bit little endian
272 */
273 .text
274 .global in16
275 .ent in16
276 .align 2
277in16: lhu r3, r0, r5
278 bslli r4, r3, 8
279 bsrli r3, r3, 8
280 andi r4, r4, 0xffff
281 or r3, r3, r4
282 rtsd r15, 8
283 sext16 r3, r3
284 .end in16
285
286/*
287 * Write 16bit little endian
288 * first parameter(r5) - address, second(r6) - short value
289 */
290 .text
291 .global out16
292 .ent out16
293 .align 2
294out16: bslli r3, r6, 8
295 bsrli r6, r6, 8
296 andi r3, r3, 0xffff
297 or r3, r3, r6
298 sh r3, r0, r5
299 rtsd r15, 8
300 or r0, r0, r0
301 .end out16
Michal Simeke945f6d2014-05-08 16:08:44 +0200302
303/*
304 * Relocate u-boot
305 */
306 .text
307 .global relocate_code
308 .ent relocate_code
309 .align 2
310relocate_code:
311 /*
312 * r5 - start_addr_sp
313 * r6 - new_gd
314 * r7 - reloc_addr
315 */
316 addi r1, r5, 0 /* Start to use new SP */
Michal Simek532ad5f2022-06-24 14:15:00 +0200317 mts rshr, r1
Michal Simeke945f6d2014-05-08 16:08:44 +0200318 addi r31, r6, 0 /* Start to use new GD */
319
Michal Simeke945f6d2014-05-08 16:08:44 +0200320 /* Relocate text and data - r12 temp value */
Michal Simek07c052b2022-06-24 14:15:00 +0200321 SYM_ADDR(r21, r0, _start)
322 SYM_ADDR(r22, r0, _end) /* Include BSS too */
Michal Simek1918c412022-06-24 14:15:00 +0200323 addi r22, r22, -4
Michal Simek7c4dd542015-01-27 15:10:37 +0100324
325 rsub r6, r21, r22
326 or r5, r0, r0
3271: lw r12, r21, r5 /* Load u-boot data */
Michal Simek3041b512022-06-24 14:15:00 +0200328 sw r12, r7, r5 /* Write zero to loc */
Michal Simek7c4dd542015-01-27 15:10:37 +0100329 cmp r12, r5, r6 /* Check if we have reach the end */
Michal Simeke945f6d2014-05-08 16:08:44 +0200330 bneid r12, 1b
Michal Simek7c4dd542015-01-27 15:10:37 +0100331 addi r5, r5, 4 /* Increment to next loc - relocate code */
Michal Simeke945f6d2014-05-08 16:08:44 +0200332
Michal Simek3ad95ed2019-10-21 12:20:16 +0200333 /* R23 points to the base address. */
Michal Simek3041b512022-06-24 14:15:00 +0200334 rsub r23, r21, r7 /* keep - this is already here gd->reloc_off */
Michal Simeke945f6d2014-05-08 16:08:44 +0200335
Ovidiu Panait627085e2020-09-24 11:54:36 +0300336 /* Setup vectors with post-relocation symbols */
337 add r5, r0, r23 /* load gd->reloc_off to r5 */
Michal Simek7cf236c2022-06-24 14:14:59 +0200338 brlid r15, __setup_exceptions
Ovidiu Panait627085e2020-09-24 11:54:36 +0300339 nop
Michal Simeke945f6d2014-05-08 16:08:44 +0200340
Michal Simekd58c0072022-06-24 14:15:01 +0200341#if defined(CONFIG_STATIC_RELA)
342 /* reloc_offset is current location */
343 SYM_ADDR(r10, r0, _start)
344
345 /* r5 new address where I should copy code */
346 add r5, r0, r7 /* Move reloc addr to r5 */
347
348 /* Verbose message */
349 addi r6, r0, 0
350
351 SYM_ADDR(r7, r0, __rel_dyn_start)
352 rsub r7, r10, r7
353 add r7, r7, r5
354 SYM_ADDR(r8, r0, __rel_dyn_end)
355 rsub r8, r10, r8
356 add r8, r8, r5
357 SYM_ADDR(r9, r0, __dyn_sym_start)
358 rsub r9, r10, r9
359 add r9, r9, r5
360 brlid r15, mb_fix_rela
361 nop
362
363 /* end of code which does relocation */
364#else
Michal Simeke945f6d2014-05-08 16:08:44 +0200365 /* Check if GOT exist */
366 addik r21, r23, _got_start
367 addik r22, r23, _got_end
368 cmpu r12, r21, r22
369 beqi r12, 2f /* No GOT table - jump over */
370
371 /* Skip last 3 entries plus 1 because of loop boundary below */
372 addik r22, r22, -0x10
373
374 /* Relocate the GOT. */
3753: lw r12, r21, r0 /* Load entry */
376 addk r12, r12, r23 /* Add reloc offset */
377 sw r12, r21, r0 /* Save entry back */
378
379 cmpu r12, r21, r22 /* Check if this cross boundary */
380 bneid r12, 3b
381 addik r21. r21, 4
Michal Simekd58c0072022-06-24 14:15:01 +0200382#endif
Michal Simeke945f6d2014-05-08 16:08:44 +0200383
Michal Simeke945f6d2014-05-08 16:08:44 +0200384 /* Flush caches to ensure consistency */
385 addik r5, r0, 0
386 addik r6, r0, XILINX_DCACHE_BYTE_SIZE
Michal Simek7cf236c2022-06-24 14:14:59 +0200387 brlid r15, flush_cache
Michal Simeke945f6d2014-05-08 16:08:44 +0200388 nop
389
3902: addi r5, r31, 0 /* gd is initialized in board_r.c */
Michal Simek07c052b2022-06-24 14:15:00 +0200391 SYM_ADDR(r6, r0, _start)
392 SYM_ADDR(r12, r23, board_init_r)
Michal Simeke945f6d2014-05-08 16:08:44 +0200393 bra r12 /* Jump to relocated code */
394
395 .end relocate_code
Michal Simek9d242742014-01-21 07:30:37 +0100396#endif