blob: a877db305e4ca61cd68a4003fde307e5d1e6b819 [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)
Tom Rinif113d7d2022-05-26 13:13:21 -040035 addi r1, r0, CONFIG_SPL_STACK
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
Ovidiu Panait95b7a8f2022-05-31 21:14:31 +0300100 /*
101 * Initialize global data cpuinfo with default values (cache
102 * size, cache line size, etc).
103 */
104 brlid r15, microblaze_early_cpuinfo_init
105 nop
106
Michal Simek58118302012-09-25 10:13:35 +0200107 /* Flush cache before enable cache */
Ovidiu Panaitb1951342022-05-31 21:14:30 +0300108 brlid r15, flush_cache_all
Michal Simek58118302012-09-25 10:13:35 +0200109 nop
110
Michal Simekcfc67112007-03-11 13:48:24 +0100111 /* enable instruction and data cache */
112 mfs r12, rmsr
Michal Simek822d43a2014-11-04 13:27:52 +0100113 ori r12, r12, 0x1a0
Michal Simekcfc67112007-03-11 13:48:24 +0100114 mts rmsr, r12
115
Michal Simek17980492007-03-26 01:39:07 +0200116clear_bss:
117 /* clear BSS segments */
Michal Simek07c052b2022-06-24 14:15:00 +0200118 SYM_ADDR(r5, r0, __bss_start)
119 SYM_ADDR(r4, r0, __bss_end)
Michal Simek17980492007-03-26 01:39:07 +0200120 cmp r6, r5, r4
121 beqi r6, 3f
1222:
123 swi r0, r5, 0 /* write zero to loc */
124 addi r5, r5, 4 /* increment to next loc */
125 cmp r6, r5, r4 /* check if we have reach the end */
126 bnei r6, 2b
1273: /* jumping to board_init */
Michal Simek48470b72015-12-10 12:55:39 +0100128#ifdef CONFIG_DEBUG_UART
Michal Simek7cf236c2022-06-24 14:14:59 +0200129 brlid r15, debug_uart_init
Michal Simek48470b72015-12-10 12:55:39 +0100130 nop
131#endif
Michal Simek9d242742014-01-21 07:30:37 +0100132#ifndef CONFIG_SPL_BUILD
Michal Simeke945f6d2014-05-08 16:08:44 +0200133 or r5, r0, r0 /* flags - empty */
Michal Simek7cf236c2022-06-24 14:14:59 +0200134 bri board_init_f
Michal Simek9d242742014-01-21 07:30:37 +0100135#else
Michal Simek7cf236c2022-06-24 14:14:59 +0200136 bri board_init_r
Michal Simek9d242742014-01-21 07:30:37 +0100137#endif
wdenk507bbe32004-04-18 21:13:41 +00001381: bri 1b
Michal Simek06436312007-04-21 21:02:40 +0200139
Michal Simek9d242742014-01-21 07:30:37 +0100140#ifndef CONFIG_SPL_BUILD
Ovidiu Panait627085e2020-09-24 11:54:36 +0300141 .text
142 .ent __setup_exceptions
143 .align 2
144/*
145 * Set up reset, interrupt, user exception and hardware exception vectors.
146 *
147 * Parameters:
148 * r5 - relocation offset (zero when setting up vectors before
149 * relocation, and gd->reloc_off when setting up vectors after
150 * relocation)
151 * - the relocation offset is added to the _exception_handler,
152 * _interrupt_handler and _hw_exception_handler symbols to reflect the
153 * post-relocation memory addresses
154 *
155 * Reserve registers:
156 * r10: Stores little/big endian offset for vectors
157 * r2: Stores imm opcode
158 * r3: Stores brai opcode
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200159 * r4: Stores the vector base address
Ovidiu Panait627085e2020-09-24 11:54:36 +0300160 */
161__setup_exceptions:
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200162 addik r1, r1, -32
Ovidiu Panait627085e2020-09-24 11:54:36 +0300163 swi r2, r1, 4
164 swi r3, r1, 8
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200165 swi r4, r1, 12
166 swi r6, r1, 16
167 swi r7, r1, 20
168 swi r8, r1, 24
169 swi r10, r1, 28
Ovidiu Panait627085e2020-09-24 11:54:36 +0300170
171 /* Find-out if u-boot is running on BIG/LITTLE endian platform
172 * There are some steps which is necessary to keep in mind:
173 * 1. Setup offset value to r6
174 * 2. Store word offset value to address 0x0
175 * 3. Load just byte from address 0x0
176 * 4a) LITTLE endian - r10 contains 0x2 because it is the smallest
177 * value that's why is on address 0x0
178 * 4b) BIG endian - r10 contains 0x0 because 0x2 offset is on addr 0x3
179 */
180 addik r6, r0, 0x2 /* BIG/LITTLE endian offset */
Ovidiu Panait48039c32021-11-30 18:33:52 +0200181 sw r6, r1, r0
182 lbu r10, r1, r0
Ovidiu Panait627085e2020-09-24 11:54:36 +0300183
184 /* add opcode instruction for 32bit jump - 2 instruction imm & brai */
185 addi r2, r0, 0xb0000000 /* hex b000 opcode imm */
186 addi r3, r0, 0xb8080000 /* hew b808 opcode brai */
187
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200188 /* Store the vector base address in r4 */
189 addi r4, r0, CONFIG_XILINX_MICROBLAZE0_VECTOR_BASE_ADDR
190
Ovidiu Panait627085e2020-09-24 11:54:36 +0300191 /* reset address */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200192 swi r2, r4, 0x0 /* reset address - imm opcode */
193 swi r3, r4, 0x4 /* reset address - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300194
Michal Simek07c052b2022-06-24 14:15:00 +0200195 SYM_ADDR(r6, r0, _start)
Michal Simek81169ae2022-06-24 14:15:00 +0200196 /* Intentionally keep reset vector back to origin u-boot location */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300197 sw r6, r1, r0
198 lhu r7, r1, r10
199 rsubi r8, r10, 0x2
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200200 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300201 rsubi r8, r10, 0x6
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200202 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300203
Ovidiu Panait83b175b2021-11-30 18:33:54 +0200204#if CONFIG_IS_ENABLED(XILINX_MICROBLAZE0_USR_EXCEP)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300205 /* user_vector_exception */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200206 swi r2, r4, 0x8 /* user vector exception - imm opcode */
207 swi r3, r4, 0xC /* user vector exception - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300208
Michal Simek07c052b2022-06-24 14:15:00 +0200209 SYM_ADDR(r6, r5, _exception_handler)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300210 sw r6, r1, r0
211 /*
212 * BIG ENDIAN memory map for user exception
213 * 0x8: 0xB000XXXX
214 * 0xC: 0xB808XXXX
215 *
216 * then it is necessary to count address for storing the most significant
217 * 16bits from _exception_handler address and copy it to
218 * 0xa address. Big endian use offset in r10=0 that's why is it just
219 * 0xa address. The same is done for the least significant 16 bits
220 * for 0xe address.
221 *
222 * LITTLE ENDIAN memory map for user exception
223 * 0x8: 0xXXXX00B0
224 * 0xC: 0xXXXX08B8
225 *
226 * Offset is for little endian setup to 0x2. rsubi instruction decrease
227 * address value to ensure that points to proper place which is
228 * 0x8 for the most significant 16 bits and
229 * 0xC for the least significant 16 bits
230 */
231 lhu r7, r1, r10
232 rsubi r8, r10, 0xa
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200233 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300234 rsubi r8, r10, 0xe
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200235 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300236#endif
237
238 /* interrupt_handler */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200239 swi r2, r4, 0x10 /* interrupt - imm opcode */
240 swi r3, r4, 0x14 /* interrupt - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300241
Michal Simek07c052b2022-06-24 14:15:00 +0200242 SYM_ADDR(r6, r5, _interrupt_handler)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300243 sw r6, r1, r0
244 lhu r7, r1, r10
245 rsubi r8, r10, 0x12
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200246 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300247 rsubi r8, r10, 0x16
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200248 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300249
250 /* hardware exception */
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200251 swi r2, r4, 0x20 /* hardware exception - imm opcode */
252 swi r3, r4, 0x24 /* hardware exception - brai opcode */
Ovidiu Panait627085e2020-09-24 11:54:36 +0300253
Michal Simek07c052b2022-06-24 14:15:00 +0200254 SYM_ADDR(r6, r5, _hw_exception_handler)
Ovidiu Panait627085e2020-09-24 11:54:36 +0300255 sw r6, r1, r0
256 lhu r7, r1, r10
257 rsubi r8, r10, 0x22
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200258 sh r7, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300259 rsubi r8, r10, 0x26
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200260 sh r6, r4, r8
Ovidiu Panait627085e2020-09-24 11:54:36 +0300261
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200262 lwi r10, r1, 28
263 lwi r8, r1, 24
264 lwi r7, r1, 20
265 lwi r6, r1, 16
266 lwi r4, r1, 12
Ovidiu Panait627085e2020-09-24 11:54:36 +0300267 lwi r3, r1, 8
268 lwi r2, r1, 4
Ovidiu Panaitf149ee42021-11-30 18:33:56 +0200269 addik r1, r1, 32
Ovidiu Panait627085e2020-09-24 11:54:36 +0300270
271 rtsd r15, 8
272 or r0, r0, r0
273 .end __setup_exceptions
274
Michal Simek06436312007-04-21 21:02:40 +0200275/*
Michal Simeke945f6d2014-05-08 16:08:44 +0200276 * Relocate u-boot
277 */
278 .text
279 .global relocate_code
280 .ent relocate_code
281 .align 2
282relocate_code:
283 /*
284 * r5 - start_addr_sp
285 * r6 - new_gd
286 * r7 - reloc_addr
287 */
288 addi r1, r5, 0 /* Start to use new SP */
Michal Simek532ad5f2022-06-24 14:15:00 +0200289 mts rshr, r1
Michal Simeke945f6d2014-05-08 16:08:44 +0200290 addi r31, r6, 0 /* Start to use new GD */
291
Michal Simeke945f6d2014-05-08 16:08:44 +0200292 /* Relocate text and data - r12 temp value */
Michal Simek07c052b2022-06-24 14:15:00 +0200293 SYM_ADDR(r21, r0, _start)
294 SYM_ADDR(r22, r0, _end) /* Include BSS too */
Michal Simek1918c412022-06-24 14:15:00 +0200295 addi r22, r22, -4
Michal Simek7c4dd542015-01-27 15:10:37 +0100296
297 rsub r6, r21, r22
298 or r5, r0, r0
2991: lw r12, r21, r5 /* Load u-boot data */
Michal Simek3041b512022-06-24 14:15:00 +0200300 sw r12, r7, r5 /* Write zero to loc */
Michal Simek7c4dd542015-01-27 15:10:37 +0100301 cmp r12, r5, r6 /* Check if we have reach the end */
Michal Simeke945f6d2014-05-08 16:08:44 +0200302 bneid r12, 1b
Michal Simek7c4dd542015-01-27 15:10:37 +0100303 addi r5, r5, 4 /* Increment to next loc - relocate code */
Michal Simeke945f6d2014-05-08 16:08:44 +0200304
Michal Simek3ad95ed2019-10-21 12:20:16 +0200305 /* R23 points to the base address. */
Michal Simek3041b512022-06-24 14:15:00 +0200306 rsub r23, r21, r7 /* keep - this is already here gd->reloc_off */
Michal Simeke945f6d2014-05-08 16:08:44 +0200307
Ovidiu Panait627085e2020-09-24 11:54:36 +0300308 /* Setup vectors with post-relocation symbols */
309 add r5, r0, r23 /* load gd->reloc_off to r5 */
Michal Simek7cf236c2022-06-24 14:14:59 +0200310 brlid r15, __setup_exceptions
Ovidiu Panait627085e2020-09-24 11:54:36 +0300311 nop
Michal Simeke945f6d2014-05-08 16:08:44 +0200312
Michal Simekd58c0072022-06-24 14:15:01 +0200313#if defined(CONFIG_STATIC_RELA)
314 /* reloc_offset is current location */
315 SYM_ADDR(r10, r0, _start)
316
317 /* r5 new address where I should copy code */
318 add r5, r0, r7 /* Move reloc addr to r5 */
319
320 /* Verbose message */
321 addi r6, r0, 0
322
323 SYM_ADDR(r7, r0, __rel_dyn_start)
324 rsub r7, r10, r7
325 add r7, r7, r5
326 SYM_ADDR(r8, r0, __rel_dyn_end)
327 rsub r8, r10, r8
328 add r8, r8, r5
329 SYM_ADDR(r9, r0, __dyn_sym_start)
330 rsub r9, r10, r9
331 add r9, r9, r5
332 brlid r15, mb_fix_rela
333 nop
334
335 /* end of code which does relocation */
336#else
Michal Simeke945f6d2014-05-08 16:08:44 +0200337 /* Check if GOT exist */
338 addik r21, r23, _got_start
339 addik r22, r23, _got_end
340 cmpu r12, r21, r22
341 beqi r12, 2f /* No GOT table - jump over */
342
343 /* Skip last 3 entries plus 1 because of loop boundary below */
344 addik r22, r22, -0x10
345
346 /* Relocate the GOT. */
3473: lw r12, r21, r0 /* Load entry */
348 addk r12, r12, r23 /* Add reloc offset */
349 sw r12, r21, r0 /* Save entry back */
350
351 cmpu r12, r21, r22 /* Check if this cross boundary */
352 bneid r12, 3b
353 addik r21. r21, 4
Michal Simekd58c0072022-06-24 14:15:01 +0200354#endif
Michal Simeke945f6d2014-05-08 16:08:44 +0200355
356 /* Flush caches to ensure consistency */
Ovidiu Panaitb1951342022-05-31 21:14:30 +0300357 brlid r15, flush_cache_all
Michal Simeke945f6d2014-05-08 16:08:44 +0200358 nop
359
3602: addi r5, r31, 0 /* gd is initialized in board_r.c */
Michal Simek07c052b2022-06-24 14:15:00 +0200361 SYM_ADDR(r6, r0, _start)
362 SYM_ADDR(r12, r23, board_init_r)
Michal Simeke945f6d2014-05-08 16:08:44 +0200363 bra r12 /* Jump to relocated code */
364
365 .end relocate_code
Michal Simek9d242742014-01-21 07:30:37 +0100366#endif