blob: 72b0f33547313f6d776d461852b64fa366cd012e [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 Simeka8e5d752022-06-24 14:15:00 +020013#define SYM_ADDR(reg, reg_add, symbol) \
14 addi reg, reg_add, symbol
15
wdenk12490652004-04-18 21:13:41 +000016 .text
17 .global _start
18_start:
Michal Simek922ce202007-03-11 13:48:24 +010019 mts rmsr, r0 /* disable cache */
Michal Simeke7d1e442022-06-24 14:15:00 +020020 mfs r20, rpc
21 addi r20, r20, -4
Michal Simek26acb3e2014-01-21 07:30:37 +010022
Michal Simek2d92f872022-06-24 14:14:59 +020023 mts rslr, r0
Michal Simeke7d1e442022-06-24 14:15:00 +020024 mts rshr, r20
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030025
Michal Simek26acb3e2014-01-21 07:30:37 +010026#if defined(CONFIG_SPL_BUILD)
27 addi r1, r0, CONFIG_SPL_STACK_ADDR
Michal Simek9ea67442015-01-30 15:46:43 +010028#else
Michal Simeke7d1e442022-06-24 14:15:00 +020029 add r1, r0, r20
Michal Simek9ea67442015-01-30 15:46:43 +010030#endif
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030031
Michal Simek1f0c40c2007-03-26 01:39:07 +020032 addi r1, r1, -4 /* Decrement SP to top of memory */
Michal Simekead124a2010-08-12 11:47:11 +020033
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030034 /* Call board_init_f_alloc_reserve with the current stack pointer as
35 * parameter. */
36 add r5, r0, r1
Michal Simek8f57aec2022-06-24 14:14:59 +020037 brlid r15, board_init_f_alloc_reserve
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030038 nop
39
40 /* board_init_f_alloc_reserve returns a pointer to the allocated area
41 * in r3. Set the new stack pointer below this area. */
42 add r1, r0, r3
43 mts rshr, r1
44 addi r1, r1, -4
45
46 /* Call board_init_f_init_reserve with the address returned by
47 * board_init_f_alloc_reserve as parameter. */
48 add r5, r0, r3
Michal Simek8f57aec2022-06-24 14:14:59 +020049 brlid r15, board_init_f_init_reserve
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030050 nop
51
52#if !defined(CONFIG_SPL_BUILD)
Ovidiu Panait98558352020-09-24 11:54:36 +030053 /* Setup vectors with pre-relocation symbols */
54 or r5, r0, r0
Michal Simek8f57aec2022-06-24 14:14:59 +020055 brlid r15, __setup_exceptions
Ovidiu Panait98558352020-09-24 11:54:36 +030056 nop
Ovidiu Panaite6dbb8b2020-09-24 11:54:37 +030057#endif
Ovidiu Panait98558352020-09-24 11:54:36 +030058
59 /* Flush cache before enable cache */
60 addik r5, r0, 0
61 addik r6, r0, XILINX_DCACHE_BYTE_SIZE
Michal Simek8f57aec2022-06-24 14:14:59 +020062 brlid r15, flush_cache
Ovidiu Panait98558352020-09-24 11:54:36 +030063 nop
64
65 /* enable instruction and data cache */
66 mfs r12, rmsr
67 ori r12, r12, 0x1a0
68 mts rmsr, r12
69
Ovidiu Panait98558352020-09-24 11:54:36 +030070clear_bss:
71 /* clear BSS segments */
Michal Simeka8e5d752022-06-24 14:15:00 +020072 SYM_ADDR(r5, r0, __bss_start)
73 SYM_ADDR(r4, r0, __bss_end)
Ovidiu Panait98558352020-09-24 11:54:36 +030074 cmp r6, r5, r4
75 beqi r6, 3f
762:
77 swi r0, r5, 0 /* write zero to loc */
78 addi r5, r5, 4 /* increment to next loc */
79 cmp r6, r5, r4 /* check if we have reach the end */
80 bnei r6, 2b
813: /* jumping to board_init */
82#ifdef CONFIG_DEBUG_UART
Michal Simek8f57aec2022-06-24 14:14:59 +020083 brlid r15, debug_uart_init
Ovidiu Panait98558352020-09-24 11:54:36 +030084 nop
85#endif
86#ifndef CONFIG_SPL_BUILD
87 or r5, r0, r0 /* flags - empty */
Michal Simek8f57aec2022-06-24 14:14:59 +020088 bri board_init_f
Ovidiu Panait98558352020-09-24 11:54:36 +030089#else
Michal Simek8f57aec2022-06-24 14:14:59 +020090 bri board_init_r
Ovidiu Panait98558352020-09-24 11:54:36 +030091#endif
921: bri 1b
93
Ovidiu Panait98558352020-09-24 11:54:36 +030094#ifndef CONFIG_SPL_BUILD
95 .text
96 .ent __setup_exceptions
97 .align 2
98/*
99 * Set up reset, interrupt, user exception and hardware exception vectors.
100 *
101 * Parameters:
102 * r5 - relocation offset (zero when setting up vectors before
103 * relocation, and gd->reloc_off when setting up vectors after
104 * relocation)
105 * - the relocation offset is added to the _exception_handler,
106 * _interrupt_handler and _hw_exception_handler symbols to reflect the
107 * post-relocation memory addresses
108 *
109 * Reserve registers:
110 * r10: Stores little/big endian offset for vectors
111 * r2: Stores imm opcode
112 * r3: Stores brai opcode
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200113 * r4: Stores the vector base address
Ovidiu Panait98558352020-09-24 11:54:36 +0300114 */
115__setup_exceptions:
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200116 addik r1, r1, -32
Ovidiu Panait98558352020-09-24 11:54:36 +0300117 swi r2, r1, 4
118 swi r3, r1, 8
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200119 swi r4, r1, 12
120 swi r6, r1, 16
121 swi r7, r1, 20
122 swi r8, r1, 24
123 swi r10, r1, 28
Ovidiu Panait98558352020-09-24 11:54:36 +0300124
Michal Simekead124a2010-08-12 11:47:11 +0200125 /* Find-out if u-boot is running on BIG/LITTLE endian platform
126 * There are some steps which is necessary to keep in mind:
127 * 1. Setup offset value to r6
128 * 2. Store word offset value to address 0x0
129 * 3. Load just byte from address 0x0
130 * 4a) LITTLE endian - r10 contains 0x2 because it is the smallest
131 * value that's why is on address 0x0
132 * 4b) BIG endian - r10 contains 0x0 because 0x2 offset is on addr 0x3
133 */
134 addik r6, r0, 0x2 /* BIG/LITTLE endian offset */
Ovidiu Panaitff759b32021-11-30 18:33:52 +0200135 sw r6, r1, r0
136 lbu r10, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200137
Michal Simek4a30db92011-07-21 10:47:21 +0200138 /* add opcode instruction for 32bit jump - 2 instruction imm & brai */
139 addi r2, r0, 0xb0000000 /* hex b000 opcode imm */
140 addi r3, r0, 0xb8080000 /* hew b808 opcode brai */
wdenk12490652004-04-18 21:13:41 +0000141
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200142 /* Store the vector base address in r4 */
143 addi r4, r0, CONFIG_XILINX_MICROBLAZE0_VECTOR_BASE_ADDR
144
Michal Simek922ce202007-03-11 13:48:24 +0100145 /* reset address */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200146 swi r2, r4, 0x0 /* reset address - imm opcode */
147 swi r3, r4, 0x4 /* reset address - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200148
Michal Simeka8e5d752022-06-24 14:15:00 +0200149 SYM_ADDR(r6, r0, _start)
Michal Simekfa43ada2022-06-24 14:15:00 +0200150 /* Intentionally keep reset vector back to origin u-boot location */
Michal Simek922ce202007-03-11 13:48:24 +0100151 sw r6, r1, r0
Michal Simek8daf0c32011-08-30 15:22:24 +0200152 lhu r7, r1, r10
153 rsubi r8, r10, 0x2
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200154 sh r7, r4, r8
Michal Simek8daf0c32011-08-30 15:22:24 +0200155 rsubi r8, r10, 0x6
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200156 sh r6, r4, r8
Michal Simek922ce202007-03-11 13:48:24 +0100157
Ovidiu Panait39415f72021-11-30 18:33:54 +0200158#if CONFIG_IS_ENABLED(XILINX_MICROBLAZE0_USR_EXCEP)
Michal Simek922ce202007-03-11 13:48:24 +0100159 /* user_vector_exception */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200160 swi r2, r4, 0x8 /* user vector exception - imm opcode */
161 swi r3, r4, 0xC /* user vector exception - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200162
Michal Simeka8e5d752022-06-24 14:15:00 +0200163 SYM_ADDR(r6, r5, _exception_handler)
Michal Simek922ce202007-03-11 13:48:24 +0100164 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200165 /*
166 * BIG ENDIAN memory map for user exception
167 * 0x8: 0xB000XXXX
168 * 0xC: 0xB808XXXX
169 *
170 * then it is necessary to count address for storing the most significant
Wolfgang Denk1136f692010-10-27 22:48:30 +0200171 * 16bits from _exception_handler address and copy it to
Michal Simekead124a2010-08-12 11:47:11 +0200172 * 0xa address. Big endian use offset in r10=0 that's why is it just
173 * 0xa address. The same is done for the least significant 16 bits
174 * for 0xe address.
175 *
176 * LITTLE ENDIAN memory map for user exception
177 * 0x8: 0xXXXX00B0
178 * 0xC: 0xXXXX08B8
179 *
180 * Offset is for little endian setup to 0x2. rsubi instruction decrease
181 * address value to ensure that points to proper place which is
182 * 0x8 for the most significant 16 bits and
183 * 0xC for the least significant 16 bits
184 */
185 lhu r7, r1, r10
186 rsubi r8, r10, 0xa
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200187 sh r7, r4, r8
Michal Simekead124a2010-08-12 11:47:11 +0200188 rsubi r8, r10, 0xe
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200189 sh r6, r4, r8
Michal Simek922ce202007-03-11 13:48:24 +0100190#endif
191
Michal Simek922ce202007-03-11 13:48:24 +0100192 /* interrupt_handler */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200193 swi r2, r4, 0x10 /* interrupt - imm opcode */
194 swi r3, r4, 0x14 /* interrupt - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200195
Michal Simeka8e5d752022-06-24 14:15:00 +0200196 SYM_ADDR(r6, r5, _interrupt_handler)
Michal Simek922ce202007-03-11 13:48:24 +0100197 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200198 lhu r7, r1, r10
199 rsubi r8, r10, 0x12
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200200 sh r7, r4, r8
Michal Simekead124a2010-08-12 11:47:11 +0200201 rsubi r8, r10, 0x16
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200202 sh r6, r4, r8
wdenk12490652004-04-18 21:13:41 +0000203
Michal Simek922ce202007-03-11 13:48:24 +0100204 /* hardware exception */
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200205 swi r2, r4, 0x20 /* hardware exception - imm opcode */
206 swi r3, r4, 0x24 /* hardware exception - brai opcode */
Michal Simek4a30db92011-07-21 10:47:21 +0200207
Michal Simeka8e5d752022-06-24 14:15:00 +0200208 SYM_ADDR(r6, r5, _hw_exception_handler)
Michal Simek922ce202007-03-11 13:48:24 +0100209 sw r6, r1, r0
Michal Simekead124a2010-08-12 11:47:11 +0200210 lhu r7, r1, r10
211 rsubi r8, r10, 0x22
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200212 sh r7, r4, r8
Michal Simekead124a2010-08-12 11:47:11 +0200213 rsubi r8, r10, 0x26
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200214 sh r6, r4, r8
Michal Simek922ce202007-03-11 13:48:24 +0100215
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200216 lwi r10, r1, 28
217 lwi r8, r1, 24
218 lwi r7, r1, 20
219 lwi r6, r1, 16
220 lwi r4, r1, 12
Ovidiu Panait98558352020-09-24 11:54:36 +0300221 lwi r3, r1, 8
222 lwi r2, r1, 4
Ovidiu Panaite165fa52021-11-30 18:33:56 +0200223 addik r1, r1, 32
Michal Simeke3aa3d52012-09-25 10:13:35 +0200224
Ovidiu Panait98558352020-09-24 11:54:36 +0300225 rtsd r15, 8
226 or r0, r0, r0
227 .end __setup_exceptions
Michal Simeka79d6582015-01-30 15:45:02 +0100228
Michal Simek04ae75f2007-04-21 21:02:40 +0200229/*
230 * Read 16bit little endian
231 */
232 .text
233 .global in16
234 .ent in16
235 .align 2
236in16: lhu r3, r0, r5
237 bslli r4, r3, 8
238 bsrli r3, r3, 8
239 andi r4, r4, 0xffff
240 or r3, r3, r4
241 rtsd r15, 8
242 sext16 r3, r3
243 .end in16
244
245/*
246 * Write 16bit little endian
247 * first parameter(r5) - address, second(r6) - short value
248 */
249 .text
250 .global out16
251 .ent out16
252 .align 2
253out16: bslli r3, r6, 8
254 bsrli r6, r6, 8
255 andi r3, r3, 0xffff
256 or r3, r3, r6
257 sh r3, r0, r5
258 rtsd r15, 8
259 or r0, r0, r0
260 .end out16
Michal Simek65e915c2014-05-08 16:08:44 +0200261
262/*
263 * Relocate u-boot
264 */
265 .text
266 .global relocate_code
267 .ent relocate_code
268 .align 2
269relocate_code:
270 /*
271 * r5 - start_addr_sp
272 * r6 - new_gd
273 * r7 - reloc_addr
274 */
275 addi r1, r5, 0 /* Start to use new SP */
Michal Simek32b80be2022-06-24 14:15:00 +0200276 mts rshr, r1
Michal Simek65e915c2014-05-08 16:08:44 +0200277 addi r31, r6, 0 /* Start to use new GD */
278
Michal Simek65e915c2014-05-08 16:08:44 +0200279 /* Relocate text and data - r12 temp value */
Michal Simeka8e5d752022-06-24 14:15:00 +0200280 SYM_ADDR(r21, r0, _start)
281 SYM_ADDR(r22, r0, _end) /* Include BSS too */
Michal Simek98730082022-06-24 14:15:00 +0200282 addi r22, r22, -4
Michal Simeka9228f62015-01-27 15:10:37 +0100283
284 rsub r6, r21, r22
285 or r5, r0, r0
2861: lw r12, r21, r5 /* Load u-boot data */
Michal Simekca0fe052022-06-24 14:15:00 +0200287 sw r12, r7, r5 /* Write zero to loc */
Michal Simeka9228f62015-01-27 15:10:37 +0100288 cmp r12, r5, r6 /* Check if we have reach the end */
Michal Simek65e915c2014-05-08 16:08:44 +0200289 bneid r12, 1b
Michal Simeka9228f62015-01-27 15:10:37 +0100290 addi r5, r5, 4 /* Increment to next loc - relocate code */
Michal Simek65e915c2014-05-08 16:08:44 +0200291
Michal Simek55df7da2019-10-21 12:20:16 +0200292 /* R23 points to the base address. */
Michal Simekca0fe052022-06-24 14:15:00 +0200293 rsub r23, r21, r7 /* keep - this is already here gd->reloc_off */
Michal Simek65e915c2014-05-08 16:08:44 +0200294
Ovidiu Panait98558352020-09-24 11:54:36 +0300295 /* Setup vectors with post-relocation symbols */
296 add r5, r0, r23 /* load gd->reloc_off to r5 */
Michal Simek8f57aec2022-06-24 14:14:59 +0200297 brlid r15, __setup_exceptions
Ovidiu Panait98558352020-09-24 11:54:36 +0300298 nop
Michal Simek65e915c2014-05-08 16:08:44 +0200299
300 /* Check if GOT exist */
301 addik r21, r23, _got_start
302 addik r22, r23, _got_end
303 cmpu r12, r21, r22
304 beqi r12, 2f /* No GOT table - jump over */
305
306 /* Skip last 3 entries plus 1 because of loop boundary below */
307 addik r22, r22, -0x10
308
309 /* Relocate the GOT. */
3103: lw r12, r21, r0 /* Load entry */
311 addk r12, r12, r23 /* Add reloc offset */
312 sw r12, r21, r0 /* Save entry back */
313
314 cmpu r12, r21, r22 /* Check if this cross boundary */
315 bneid r12, 3b
316 addik r21. r21, 4
317
Michal Simek65e915c2014-05-08 16:08:44 +0200318 /* Flush caches to ensure consistency */
319 addik r5, r0, 0
320 addik r6, r0, XILINX_DCACHE_BYTE_SIZE
Michal Simek8f57aec2022-06-24 14:14:59 +0200321 brlid r15, flush_cache
Michal Simek65e915c2014-05-08 16:08:44 +0200322 nop
323
3242: addi r5, r31, 0 /* gd is initialized in board_r.c */
Michal Simeka8e5d752022-06-24 14:15:00 +0200325 SYM_ADDR(r6, r0, _start)
326 SYM_ADDR(r12, r23, board_init_r)
Michal Simek65e915c2014-05-08 16:08:44 +0200327 bra r12 /* Jump to relocated code */
328
329 .end relocate_code
Michal Simek26acb3e2014-01-21 07:30:37 +0100330#endif