microblaze: migrate CONFIG_SYS_USR_EXCEP to Kconfig
[platform/kernel/u-boot.git] / arch / microblaze / cpu / start.S
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2007 Michal Simek
4  * (C) Copyright 2004 Atmark Techno, Inc.
5  *
6  * Michal  SIMEK <monstr@monstr.eu>
7  * Yasushi SHOJI <yashi@atmark-techno.com>
8  */
9
10 #include <asm-offsets.h>
11 #include <config.h>
12
13         .text
14         .global _start
15 _start:
16         mts     rmsr, r0        /* disable cache */
17
18         addi    r8, r0, _end
19         mts     rslr, r8
20
21 #if defined(CONFIG_SPL_BUILD)
22         addi    r1, r0, CONFIG_SPL_STACK_ADDR
23 #else
24         addi    r1, r0, CONFIG_SYS_INIT_SP_OFFSET
25 #endif
26
27         addi    r1, r1, -4      /* Decrement SP to top of memory */
28
29         /* 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)
48         /* Setup vectors with pre-relocation symbols */
49         or      r5, r0, r0
50         bralid  r15, __setup_exceptions
51         nop
52 #endif
53
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
65 clear_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
71 2:
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
76 3:      /* 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 */
83         brai    board_init_f
84 #else
85         brai    board_init_r
86 #endif
87 1:      bri     1b
88
89 #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
118         /* 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 */
128         sw      r6, r1, r0
129         lbu     r10, r1, r0
130
131         /* 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 */
134
135         /* reset address */
136         swi     r2, r0, 0x0     /* reset address - imm opcode */
137         swi     r3, r0, 0x4     /* reset address - brai opcode */
138
139         addik   r6, r0, CONFIG_SYS_TEXT_BASE
140         sw      r6, r1, r0
141         lhu     r7, r1, r10
142         rsubi   r8, r10, 0x2
143         sh      r7, r0, r8
144         rsubi   r8, r10, 0x6
145         sh      r6, r0, r8
146
147 #if CONFIG_IS_ENABLED(XILINX_MICROBLAZE0_USR_EXCEP)
148         /* user_vector_exception */
149         swi     r2, r0, 0x8     /* user vector exception - imm opcode */
150         swi     r3, r0, 0xC     /* user vector exception - brai opcode */
151
152         addik   r6, r5, _exception_handler
153         sw      r6, r1, r0
154         /*
155          * BIG ENDIAN memory map for user exception
156          * 0x8: 0xB000XXXX
157          * 0xC: 0xB808XXXX
158          *
159          * then it is necessary to count address for storing the most significant
160          * 16bits from _exception_handler address and copy it to
161          * 0xa address. Big endian use offset in r10=0 that's why is it just
162          * 0xa address. The same is done for the least significant 16 bits
163          * for 0xe address.
164          *
165          * LITTLE ENDIAN memory map for user exception
166          * 0x8: 0xXXXX00B0
167          * 0xC: 0xXXXX08B8
168          *
169          * Offset is for little endian setup to 0x2. rsubi instruction decrease
170          * address value to ensure that points to proper place which is
171          * 0x8 for the most significant 16 bits and
172          * 0xC for the least significant 16 bits
173          */
174         lhu     r7, r1, r10
175         rsubi   r8, r10, 0xa
176         sh      r7, r0, r8
177         rsubi   r8, r10, 0xe
178         sh      r6, r0, r8
179 #endif
180
181         /* interrupt_handler */
182         swi     r2, r0, 0x10    /* interrupt - imm opcode */
183         swi     r3, r0, 0x14    /* interrupt - brai opcode */
184
185         addik   r6, r5, _interrupt_handler
186         sw      r6, r1, r0
187         lhu     r7, r1, r10
188         rsubi   r8, r10, 0x12
189         sh      r7, r0, r8
190         rsubi   r8, r10, 0x16
191         sh      r6, r0, r8
192
193         /* hardware exception */
194         swi     r2, r0, 0x20    /* hardware exception - imm opcode */
195         swi     r3, r0, 0x24    /* hardware exception - brai opcode */
196
197         addik   r6, r5, _hw_exception_handler
198         sw      r6, r1, r0
199         lhu     r7, r1, r10
200         rsubi   r8, r10, 0x22
201         sh      r7, r0, r8
202         rsubi   r8, r10, 0x26
203         sh      r6, r0, r8
204
205         lwi     r10, r1, 24
206         lwi     r8, r1, 20
207         lwi     r7, r1, 16
208         lwi     r6, r1, 12
209         lwi     r3, r1, 8
210         lwi     r2, r1, 4
211         addik   r1, r1, 28
212
213         rtsd    r15, 8
214         or      r0, r0, r0
215         .end    __setup_exceptions
216
217 /*
218  * Read 16bit little endian
219  */
220         .text
221         .global in16
222         .ent    in16
223         .align  2
224 in16:   lhu     r3, r0, r5
225         bslli   r4, r3, 8
226         bsrli   r3, r3, 8
227         andi    r4, r4, 0xffff
228         or      r3, r3, r4
229         rtsd    r15, 8
230         sext16  r3, r3
231         .end    in16
232
233 /*
234  * Write 16bit little endian
235  * first parameter(r5) - address, second(r6) - short value
236  */
237         .text
238         .global out16
239         .ent    out16
240         .align  2
241 out16:  bslli   r3, r6, 8
242         bsrli   r6, r6, 8
243         andi    r3, r3, 0xffff
244         or      r3, r3, r6
245         sh      r3, r0, r5
246         rtsd    r15, 8
247         or      r0, r0, r0
248         .end    out16
249
250 /*
251  * Relocate u-boot
252  */
253         .text
254         .global relocate_code
255         .ent    relocate_code
256         .align  2
257 relocate_code:
258         /*
259          * r5 - start_addr_sp
260          * r6 - new_gd
261          * r7 - reloc_addr
262          */
263         addi    r1, r5, 0 /* Start to use new SP */
264         addi    r31, r6, 0 /* Start to use new GD */
265
266         add     r23, r0, r7 /* Move reloc addr to r23 */
267         /* Relocate text and data - r12 temp value */
268         addi    r21, r0, _start
269         addi    r22, r0, _end - 4 /* Include BSS too */
270
271         rsub    r6, r21, r22
272         or      r5, r0, r0
273 1:      lw      r12, r21, r5 /* Load u-boot data */
274         sw      r12, r23, r5 /* Write zero to loc */
275         cmp     r12, r5, r6 /* Check if we have reach the end */
276         bneid   r12, 1b
277         addi    r5, r5, 4 /* Increment to next loc - relocate code */
278
279         /* R23 points to the base address. */
280         add     r23, r0, r7 /* Move reloc addr to r23 */
281         addi    r24, r0, CONFIG_SYS_TEXT_BASE /* Get reloc offset */
282         rsub    r23, r24, r23 /* keep - this is already here gd->reloc_off */
283
284         /* Setup vectors with post-relocation symbols */
285         add     r5, r0, r23 /* load gd->reloc_off to r5 */
286         bralid  r15, __setup_exceptions
287         nop
288
289         /* Check if GOT exist */
290         addik   r21, r23, _got_start
291         addik   r22, r23, _got_end
292         cmpu    r12, r21, r22
293         beqi    r12, 2f /* No GOT table - jump over */
294
295         /* Skip last 3 entries plus 1 because of loop boundary below */
296         addik   r22, r22, -0x10
297
298         /* Relocate the GOT. */
299 3:      lw      r12, r21, r0 /* Load entry */
300         addk    r12, r12, r23 /* Add reloc offset */
301         sw      r12, r21, r0 /* Save entry back */
302
303         cmpu    r12, r21, r22 /* Check if this cross boundary */
304         bneid   r12, 3b
305         addik   r21. r21, 4
306
307         /* Update pointer to GOT */
308         mfs     r20, rpc
309         addik   r20, r20, _GLOBAL_OFFSET_TABLE_ + 8
310         addk    r20, r20, r23
311
312         /* Flush caches to ensure consistency */
313         addik   r5, r0, 0
314         addik   r6, r0, XILINX_DCACHE_BYTE_SIZE
315         bralid  r15, flush_cache
316         nop
317
318 2:      addi    r5, r31, 0 /* gd is initialized in board_r.c */
319         addi    r6, r0, CONFIG_SYS_TEXT_BASE
320         addi    r12, r23, board_init_r
321         bra     r12 /* Jump to relocated code */
322
323         .end    relocate_code
324 #endif