blob: 7079d9e17047aad4bf4d483022a4c496a1c38ddb [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
Michal Simekf942ebb2022-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 Simeka8e5d752022-06-24 14:15:00 +020020#define SYM_ADDR(reg, reg_add, symbol) \
21 addi reg, reg_add, symbol
Michal Simekf942ebb2022-06-24 14:15:01 +020022#endif
Michal Simeka8e5d752022-06-24 14:15:00 +020023
wdenk12490652004-04-18 21:13:41 +000024 .text
25 .global _start
26_start:
Michal Simek922ce202007-03-11 13:48:24 +010027 mts rmsr, r0 /* disable cache */
Michal Simeke7d1e442022-06-24 14:15:00 +020028 mfs r20, rpc
29 addi r20, r20, -4
Michal Simek26acb3e2014-01-21 07:30:37 +010030
Michal Simek2d92f872022-06-24 14:14:59 +020031 mts rslr, r0
Michal Simeke7d1e442022-06-24 14:15:00 +020032 mts rshr, r20
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030033
Michal Simek26acb3e2014-01-21 07:30:37 +010034#if defined(CONFIG_SPL_BUILD)
Tom Rini8a14ac42022-05-26 13:13:21 -040035 addi r1, r0, CONFIG_SPL_STACK
Michal Simek9ea67442015-01-30 15:46:43 +010036#else
Michal Simeke7d1e442022-06-24 14:15:00 +020037 add r1, r0, r20
Michal Simekf942ebb2022-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
Simon Glass72cc5382022-10-20 18:22:39 -060058 addi r7, r7, -CONFIG_TEXT_BASE
Michal Simekf942ebb2022-06-24 14:15:01 +020059 add r7, r7, r5
60 lwi r8, r20, ALIGNMENT_ADDR + 0x4
Simon Glass72cc5382022-10-20 18:22:39 -060061 addi r8, r8, -CONFIG_TEXT_BASE
Michal Simekf942ebb2022-06-24 14:15:01 +020062 add r8, r8, r5
63 lwi r9, r20, ALIGNMENT_ADDR + 0x8
Simon Glass72cc5382022-10-20 18:22:39 -060064 addi r9, r9, -CONFIG_TEXT_BASE
Michal Simekf942ebb2022-06-24 14:15:01 +020065 add r9, r9, r5
Simon Glass72cc5382022-10-20 18:22:39 -060066 addi r10, r0, CONFIG_TEXT_BASE
Michal Simekf942ebb2022-06-24 14:15:01 +020067
68 brlid r15, mb_fix_rela
69 nop
70#endif
Michal Simek9ea67442015-01-30 15:46:43 +010071#endif
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030072
Michal Simek1f0c40c2007-03-26 01:39:07 +020073 addi r1, r1, -4 /* Decrement SP to top of memory */
Michal Simekead124a2010-08-12 11:47:11 +020074
Ovidiu Panaite6dbb8b2020-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 Simek8f57aec2022-06-24 14:14:59 +020078 brlid r15, board_init_f_alloc_reserve
Ovidiu Panaite6dbb8b2020-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 Simek8f57aec2022-06-24 14:14:59 +020090 brlid r15, board_init_f_init_reserve
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030091 nop
92
93#if !defined(CONFIG_SPL_BUILD)
Ovidiu Panait98558352020-09-24 11:54:36 +030094 /* Setup vectors with pre-relocation symbols */
95 or r5, r0, r0
Michal Simek8f57aec2022-06-24 14:14:59 +020096 brlid r15, __setup_exceptions
Ovidiu Panait98558352020-09-24 11:54:36 +030097 nop
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030098#endif
Ovidiu Panait98558352020-09-24 11:54:36 +030099
Ovidiu Panait87a739e2022-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
Ovidiu Panait98558352020-09-24 11:54:36 +0300107 /* Flush cache before enable cache */
Ovidiu Panaitbc159c12022-05-31 21:14:30 +0300108 brlid r15, flush_cache_all
Ovidiu Panait98558352020-09-24 11:54:36 +0300109 nop
110
111 /* enable instruction and data cache */
112 mfs r12, rmsr
113 ori r12, r12, 0x1a0
114 mts rmsr, r12
115
Ovidiu Panait98558352020-09-24 11:54:36 +0300116clear_bss:
117 /* clear BSS segments */
Michal Simeka8e5d752022-06-24 14:15:00 +0200118 SYM_ADDR(r5, r0, __bss_start)
119 SYM_ADDR(r4, r0, __bss_end)
Ovidiu Panait98558352020-09-24 11:54:36 +0300120 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 */
128#ifdef CONFIG_DEBUG_UART
Michal Simek8f57aec2022-06-24 14:14:59 +0200129 brlid r15, debug_uart_init
Ovidiu Panait98558352020-09-24 11:54:36 +0300130 nop
131#endif
132#ifndef CONFIG_SPL_BUILD
133 or r5, r0, r0 /* flags - empty */
Michal Simek8f57aec2022-06-24 14:14:59 +0200134 bri board_init_f
Ovidiu Panait98558352020-09-24 11:54:36 +0300135#else
Michal Simek8f57aec2022-06-24 14:14:59 +0200136 bri board_init_r
Ovidiu Panait98558352020-09-24 11:54:36 +0300137#endif
1381: bri 1b
139
Ovidiu Panait98558352020-09-24 11:54:36 +0300140#ifndef CONFIG_SPL_BUILD
141 .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 Panaite165fa52021-11-30 18:33:56 +0200159 * r4: Stores the vector base address
Ovidiu Panait98558352020-09-24 11:54:36 +0300160 */
161__setup_exceptions:
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200162 addik r1, r1, -32
Ovidiu Panait98558352020-09-24 11:54:36 +0300163 swi r2, r1, 4
164 swi r3, r1, 8
Ovidiu Panaite165fa52021-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 Panait98558352020-09-24 11:54:36 +0300170
Michal Simekead124a2010-08-12 11:47:11 +0200171 /* 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 Panaitff759b32021-11-30 18:33:52 +0200181 sw r6, r1, r0
182 lbu r10, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200183
Michal Simek4a30db92011-07-21 10:47:21 +0200184 /* 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 */
wdenk12490652004-04-18 21:13:41 +0000187
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200188 /* Store the vector base address in r4 */
189 addi r4, r0, CONFIG_XILINX_MICROBLAZE0_VECTOR_BASE_ADDR
190
Michal Simek922ce202007-03-11 13:48:24 +0100191 /* reset address */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200192 swi r2, r4, 0x0 /* reset address - imm opcode */
193 swi r3, r4, 0x4 /* reset address - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200194
Michal Simeka8e5d752022-06-24 14:15:00 +0200195 SYM_ADDR(r6, r0, _start)
Michal Simekfa43ada2022-06-24 14:15:00 +0200196 /* Intentionally keep reset vector back to origin u-boot location */
Michal Simek922ce202007-03-11 13:48:24 +0100197 sw r6, r1, r0
Michal Simek8daf0c32011-08-30 15:22:24 +0200198 lhu r7, r1, r10
199 rsubi r8, r10, 0x2
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200200 sh r7, r4, r8
Michal Simek8daf0c32011-08-30 15:22:24 +0200201 rsubi r8, r10, 0x6
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200202 sh r6, r4, r8
Michal Simek922ce202007-03-11 13:48:24 +0100203
Ovidiu Panait39415f72021-11-30 18:33:54 +0200204#if CONFIG_IS_ENABLED(XILINX_MICROBLAZE0_USR_EXCEP)
Michal Simek922ce202007-03-11 13:48:24 +0100205 /* user_vector_exception */
Ovidiu Panaite165fa52021-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 */
Michal Simek4a30db92011-07-21 10:47:21 +0200208
Michal Simeka8e5d752022-06-24 14:15:00 +0200209 SYM_ADDR(r6, r5, _exception_handler)
Michal Simek922ce202007-03-11 13:48:24 +0100210 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200211 /*
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
Wolfgang Denk1136f692010-10-27 22:48:30 +0200217 * 16bits from _exception_handler address and copy it to
Michal Simekead124a2010-08-12 11:47:11 +0200218 * 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 Panaite165fa52021-11-30 18:33:56 +0200233 sh r7, r4, r8
Michal Simekead124a2010-08-12 11:47:11 +0200234 rsubi r8, r10, 0xe
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200235 sh r6, r4, r8
Michal Simek922ce202007-03-11 13:48:24 +0100236#endif
237
Michal Simek922ce202007-03-11 13:48:24 +0100238 /* interrupt_handler */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200239 swi r2, r4, 0x10 /* interrupt - imm opcode */
240 swi r3, r4, 0x14 /* interrupt - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200241
Michal Simeka8e5d752022-06-24 14:15:00 +0200242 SYM_ADDR(r6, r5, _interrupt_handler)
Michal Simek922ce202007-03-11 13:48:24 +0100243 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200244 lhu r7, r1, r10
245 rsubi r8, r10, 0x12
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200246 sh r7, r4, r8
Michal Simekead124a2010-08-12 11:47:11 +0200247 rsubi r8, r10, 0x16
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200248 sh r6, r4, r8
wdenk12490652004-04-18 21:13:41 +0000249
Michal Simek922ce202007-03-11 13:48:24 +0100250 /* hardware exception */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200251 swi r2, r4, 0x20 /* hardware exception - imm opcode */
252 swi r3, r4, 0x24 /* hardware exception - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200253
Michal Simeka8e5d752022-06-24 14:15:00 +0200254 SYM_ADDR(r6, r5, _hw_exception_handler)
Michal Simek922ce202007-03-11 13:48:24 +0100255 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200256 lhu r7, r1, r10
257 rsubi r8, r10, 0x22
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200258 sh r7, r4, r8
Michal Simekead124a2010-08-12 11:47:11 +0200259 rsubi r8, r10, 0x26
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200260 sh r6, r4, r8
Michal Simek922ce202007-03-11 13:48:24 +0100261
Ovidiu Panaite165fa52021-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 Panait98558352020-09-24 11:54:36 +0300267 lwi r3, r1, 8
268 lwi r2, r1, 4
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200269 addik r1, r1, 32
Michal Simeke3aa3d52012-09-25 10:13:35 +0200270
Ovidiu Panait98558352020-09-24 11:54:36 +0300271 rtsd r15, 8
272 or r0, r0, r0
273 .end __setup_exceptions
Michal Simeka79d6582015-01-30 15:45:02 +0100274
Michal Simek04ae75f2007-04-21 21:02:40 +0200275/*
Michal Simek65e915c2014-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 Simek32b80be2022-06-24 14:15:00 +0200289 mts rshr, r1
Michal Simek65e915c2014-05-08 16:08:44 +0200290 addi r31, r6, 0 /* Start to use new GD */
291
Michal Simek65e915c2014-05-08 16:08:44 +0200292 /* Relocate text and data - r12 temp value */
Michal Simeka8e5d752022-06-24 14:15:00 +0200293 SYM_ADDR(r21, r0, _start)
294 SYM_ADDR(r22, r0, _end) /* Include BSS too */
Michal Simek98730082022-06-24 14:15:00 +0200295 addi r22, r22, -4
Michal Simeka9228f62015-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 Simekca0fe052022-06-24 14:15:00 +0200300 sw r12, r7, r5 /* Write zero to loc */
Michal Simeka9228f62015-01-27 15:10:37 +0100301 cmp r12, r5, r6 /* Check if we have reach the end */
Michal Simek65e915c2014-05-08 16:08:44 +0200302 bneid r12, 1b
Michal Simeka9228f62015-01-27 15:10:37 +0100303 addi r5, r5, 4 /* Increment to next loc - relocate code */
Michal Simek65e915c2014-05-08 16:08:44 +0200304
Michal Simek55df7da2019-10-21 12:20:16 +0200305 /* R23 points to the base address. */
Michal Simekca0fe052022-06-24 14:15:00 +0200306 rsub r23, r21, r7 /* keep - this is already here gd->reloc_off */
Michal Simek65e915c2014-05-08 16:08:44 +0200307
Ovidiu Panait98558352020-09-24 11:54:36 +0300308 /* Setup vectors with post-relocation symbols */
309 add r5, r0, r23 /* load gd->reloc_off to r5 */
Michal Simek8f57aec2022-06-24 14:14:59 +0200310 brlid r15, __setup_exceptions
Ovidiu Panait98558352020-09-24 11:54:36 +0300311 nop
Michal Simek65e915c2014-05-08 16:08:44 +0200312
Michal Simekf942ebb2022-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 Simek65e915c2014-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 Simekf942ebb2022-06-24 14:15:01 +0200354#endif
Michal Simek65e915c2014-05-08 16:08:44 +0200355
356 /* Flush caches to ensure consistency */
Ovidiu Panaitbc159c12022-05-31 21:14:30 +0300357 brlid r15, flush_cache_all
Michal Simek65e915c2014-05-08 16:08:44 +0200358 nop
359
3602: addi r5, r31, 0 /* gd is initialized in board_r.c */
Michal Simeka8e5d752022-06-24 14:15:00 +0200361 SYM_ADDR(r6, r0, _start)
362 SYM_ADDR(r12, r23, board_init_r)
Michal Simek65e915c2014-05-08 16:08:44 +0200363 bra r12 /* Jump to relocated code */
364
365 .end relocate_code
Michal Simek26acb3e2014-01-21 07:30:37 +0100366#endif