Merge branch 'for-2023.04' of https://source.denx.de/u-boot/custodians/u-boot-mpc8xx
[platform/kernel/u-boot.git] / arch / powerpc / cpu / mpc8xx / start.S
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  Copyright (C) 1998  Dan Malek <dmalek@jlc.net>
4  *  Copyright (C) 1999  Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se>
5  *  Copyright (C) 2000,2001,2002 Wolfgang Denk <wd@denx.de>
6  */
7
8 /*  U-Boot - Startup Code for PowerPC based Embedded Boards
9  *
10  *
11  *  The processor starts at 0x00000100 and the code is executed
12  *  from flash. The code is organized to be at an other address
13  *  in memory, but as long we don't jump around before relocating,
14  *  board_init lies at a quite high address and when the cpu has
15  *  jumped there, everything is ok.
16  *  This works because the cpu gives the FLASH (CS0) the whole
17  *  address space at startup, and board_init lies as a echo of
18  *  the flash somewhere up there in the memory map.
19  *
20  *  board_init will change CS0 to be positioned at the correct
21  *  address and (s)dram will be positioned at address 0
22  */
23 #include <asm-offsets.h>
24 #include <config.h>
25 #include <mpc8xx.h>
26
27 #include <ppc_asm.tmpl>
28 #include <ppc_defs.h>
29
30 #include <asm/cache.h>
31 #include <asm/mmu.h>
32 #include <asm/u-boot.h>
33
34 /* We don't want the  MMU yet.
35 */
36 #undef  MSR_KERNEL
37 #define MSR_KERNEL ( MSR_ME | MSR_RI )  /* Machine Check and Recoverable Interr. */
38
39 /*
40  * Set up GOT: Global Offset Table
41  *
42  * Use r12 to access the GOT
43  */
44         START_GOT
45         GOT_ENTRY(_GOT2_TABLE_)
46         GOT_ENTRY(_FIXUP_TABLE_)
47
48         GOT_ENTRY(_start)
49         GOT_ENTRY(_start_of_vectors)
50         GOT_ENTRY(_end_of_vectors)
51         GOT_ENTRY(transfer_to_handler)
52
53         GOT_ENTRY(__init_end)
54         GOT_ENTRY(__bss_end)
55         GOT_ENTRY(__bss_start)
56         END_GOT
57
58 /*
59  * r3 - 1st arg to board_init(): IMMP pointer
60  * r4 - 2nd arg to board_init(): boot flag
61  */
62         .text
63         .long   0x27051956              /* U-Boot Magic Number                  */
64
65         . = EXC_OFF_SYS_RESET
66         .globl  _start
67 _start:
68         lis     r3, CONFIG_SYS_IMMR@h           /* position IMMR */
69         mtspr   638, r3
70
71         /* Initialize machine status; enable machine check interrupt            */
72         /*----------------------------------------------------------------------*/
73         li      r3, MSR_KERNEL          /* Set ME, RI flags */
74         mtmsr   r3
75         mtspr   SRR1, r3                /* Make SRR1 match MSR */
76
77         mfspr   r3, ICR                 /* clear Interrupt Cause Register */
78
79         /* Initialize debug port registers                                      */
80         /*----------------------------------------------------------------------*/
81         xor     r0, r0, r0              /* Clear R0 */
82         mtspr   LCTRL1, r0              /* Initialize debug port regs */
83         mtspr   LCTRL2, r0
84         mtspr   COUNTA, r0
85         mtspr   COUNTB, r0
86
87         /* Reset the caches                                                     */
88         /*----------------------------------------------------------------------*/
89
90         mfspr   r3, IC_CST              /* Clear error bits */
91         mfspr   r3, DC_CST
92
93         lis     r3, IDC_UNALL@h         /* Unlock all */
94         mtspr   IC_CST, r3
95         mtspr   DC_CST, r3
96
97         lis     r3, IDC_INVALL@h        /* Invalidate all */
98         mtspr   IC_CST, r3
99         mtspr   DC_CST, r3
100
101         lis     r3, IDC_DISABLE@h       /* Disable data cache */
102         mtspr   DC_CST, r3
103
104         lis     r3, IDC_ENABLE@h        /* Enable instruction cache */
105         mtspr   IC_CST, r3
106
107         /* invalidate all tlb's                                                 */
108         /*----------------------------------------------------------------------*/
109
110         tlbia
111         isync
112
113         /*
114          * Calculate absolute address in FLASH and jump there
115          *----------------------------------------------------------------------*/
116
117         lis     r3, CONFIG_SYS_MONITOR_BASE@h
118         ori     r3, r3, CONFIG_SYS_MONITOR_BASE@l
119         addi    r3, r3, in_flash - _start + EXC_OFF_SYS_RESET
120         mtlr    r3
121         blr
122
123 in_flash:
124
125         /* initialize some SPRs that are hard to access from C                  */
126         /*----------------------------------------------------------------------*/
127
128         /*
129          * Disable serialized ifetch and show cycles
130          * (i.e. set processor to normal mode).
131          * This is also a silicon bug workaround, see errata
132          */
133
134         li      r2, 0x0007
135         mtspr   ICTRL, r2
136
137         /* Set up debug mode entry */
138
139         lis     r2, CONFIG_SYS_DER@h
140         ori     r2, r2, CONFIG_SYS_DER@l
141         mtspr   DER, r2
142
143         /* set up the stack on top of internal DPRAM */
144         lis     r3, (CFG_SYS_INIT_RAM_ADDR + CFG_SYS_INIT_RAM_SIZE)@h
145         ori     r3, r3, (CFG_SYS_INIT_RAM_ADDR + CFG_SYS_INIT_RAM_SIZE)@l
146         stw     r0, -4(r3)
147         stw     r0, -8(r3)
148         addi    r1, r3, -8
149
150         bl      board_init_f_alloc_reserve
151         addi    r1, r3, -8
152
153         /* Zeroise the CPM dpram */
154         lis     r4, CONFIG_SYS_IMMR@h
155         ori     r4, r4, (0x2000 - 4)
156         li      r0, (0x2000 / 4)
157         mtctr   r0
158         li      r0, 0
159 1:      stwu    r0, 4(r4)
160         bdnz    1b
161
162         bl      board_init_f_init_reserve
163
164         /* let the C-code set up the rest                                       */
165         /*                                                                      */
166         /* Be careful to keep code relocatable !                                */
167         /*----------------------------------------------------------------------*/
168
169         GET_GOT                 /* initialize GOT access                        */
170
171         lis     r3, CONFIG_SYS_IMMR@h
172         bl      cpu_init_f      /* run low-level CPU init code     (from Flash) */
173
174         li      r3, 0           /* clear boot_flag for calling board_init_f */
175         bl      board_init_f    /* run 1st part of board init code (from Flash) */
176
177         /* NOTREACHED - board_init_f() does not return */
178
179
180         .globl  _start_of_vectors
181 _start_of_vectors:
182
183 /* Machine check */
184         STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
185
186 /* Data Storage exception.  "Never" generated on the 860. */
187         STD_EXCEPTION(0x300, DataStorage, UnknownException)
188
189 /* Instruction Storage exception.  "Never" generated on the 860. */
190         STD_EXCEPTION(0x400, InstStorage, UnknownException)
191
192 /* External Interrupt exception. */
193         STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
194
195 /* Alignment exception. */
196         . = 0x600
197 Alignment:
198         EXCEPTION_PROLOG(SRR0, SRR1)
199         mfspr   r4,DAR
200         stw     r4,_DAR(r21)
201         mfspr   r5,DSISR
202         stw     r5,_DSISR(r21)
203         addi    r3,r1,STACK_FRAME_OVERHEAD
204         EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
205
206 /* Program check exception */
207         . = 0x700
208 ProgramCheck:
209         EXCEPTION_PROLOG(SRR0, SRR1)
210         addi    r3,r1,STACK_FRAME_OVERHEAD
211         EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
212                 MSR_KERNEL, COPY_EE)
213
214         /* No FPU on MPC8xx.  This exception is not supposed to happen.
215         */
216         STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
217
218         /* I guess we could implement decrementer, and may have
219          * to someday for timekeeping.
220          */
221         STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
222         STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
223         STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
224         STD_EXCEPTION(0xc00, SystemCall, UnknownException)
225         STD_EXCEPTION(0xd00, SingleStep, UnknownException)
226
227         STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
228         STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
229
230         /* On the MPC8xx, this is a software emulation interrupt.  It occurs
231          * for all unimplemented and illegal instructions.
232          */
233         STD_EXCEPTION(0x1000, SoftEmu, SoftEmuException)
234
235         STD_EXCEPTION(0x1100, InstructionTLBMiss, UnknownException)
236         STD_EXCEPTION(0x1200, DataTLBMiss, UnknownException)
237         STD_EXCEPTION(0x1300, InstructionTLBError, UnknownException)
238         STD_EXCEPTION(0x1400, DataTLBError, UnknownException)
239
240         STD_EXCEPTION(0x1500, Reserved5, UnknownException)
241         STD_EXCEPTION(0x1600, Reserved6, UnknownException)
242         STD_EXCEPTION(0x1700, Reserved7, UnknownException)
243         STD_EXCEPTION(0x1800, Reserved8, UnknownException)
244         STD_EXCEPTION(0x1900, Reserved9, UnknownException)
245         STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
246         STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
247
248         STD_EXCEPTION(0x1c00, DataBreakpoint, UnknownException)
249         STD_EXCEPTION(0x1d00, InstructionBreakpoint, DebugException)
250         STD_EXCEPTION(0x1e00, PeripheralBreakpoint, UnknownException)
251         STD_EXCEPTION(0x1f00, DevPortBreakpoint, UnknownException)
252
253
254         .globl  _end_of_vectors
255 _end_of_vectors:
256
257
258         . = 0x2000
259
260 /*
261  * This code finishes saving the registers to the exception frame
262  * and jumps to the appropriate handler for the exception.
263  * Register r21 is pointer into trap frame, r1 has new stack pointer.
264  */
265         .globl  transfer_to_handler
266 transfer_to_handler:
267         stw     r22,_NIP(r21)
268         lis     r22,MSR_POW@h
269         andc    r23,r23,r22
270         stw     r23,_MSR(r21)
271         SAVE_GPR(7, r21)
272         SAVE_4GPRS(8, r21)
273         SAVE_8GPRS(12, r21)
274         SAVE_8GPRS(24, r21)
275         mflr    r23
276         andi.   r24,r23,0x3f00          /* get vector offset */
277         stw     r24,TRAP(r21)
278         li      r22,0
279         stw     r22,RESULT(r21)
280         mtspr   SPRG2,r22               /* r1 is now kernel sp */
281         lwz     r24,0(r23)              /* virtual address of handler */
282         lwz     r23,4(r23)              /* where to go when done */
283         mtspr   SRR0,r24
284         mtspr   SRR1,r20
285         mtlr    r23
286         SYNC
287         rfi                             /* jump to handler, enable MMU */
288
289 int_return:
290         mfmsr   r28                     /* Disable interrupts */
291         li      r4,0
292         ori     r4,r4,MSR_EE
293         andc    r28,r28,r4
294         SYNC                            /* Some chip revs need this... */
295         mtmsr   r28
296         SYNC
297         lwz     r2,_CTR(r1)
298         lwz     r0,_LINK(r1)
299         mtctr   r2
300         mtlr    r0
301         lwz     r2,_XER(r1)
302         lwz     r0,_CCR(r1)
303         mtspr   XER,r2
304         mtcrf   0xFF,r0
305         REST_10GPRS(3, r1)
306         REST_10GPRS(13, r1)
307         REST_8GPRS(23, r1)
308         REST_GPR(31, r1)
309         lwz     r2,_NIP(r1)             /* Restore environment */
310         lwz     r0,_MSR(r1)
311         mtspr   SRR0,r2
312         mtspr   SRR1,r0
313         lwz     r0,GPR0(r1)
314         lwz     r2,GPR2(r1)
315         lwz     r1,GPR1(r1)
316         SYNC
317         rfi
318
319 /*------------------------------------------------------------------------------*/
320
321 /*
322  * void relocate_code(addr_sp, gd, addr_moni)
323  *
324  * This "function" does not return, instead it continues in RAM
325  * after relocating the monitor code.
326  *
327  * r3 = dest
328  * r4 = src
329  * r5 = length in bytes
330  * r6 = cachelinesize
331  */
332         .globl  relocate_code
333 relocate_code:
334         mr      r1,  r3         /* Set new stack pointer                */
335         mr      r9,  r4         /* Save copy of Global Data pointer     */
336         mr      r10, r5         /* Save copy of Destination Address     */
337
338         GET_GOT
339         mr      r3,  r5                         /* Destination Address  */
340         lis     r4, CONFIG_SYS_MONITOR_BASE@h           /* Source      Address  */
341         ori     r4, r4, CONFIG_SYS_MONITOR_BASE@l
342         lwz     r5, GOT(__init_end)
343         sub     r5, r5, r4
344         li      r6, CONFIG_SYS_CACHELINE_SIZE           /* Cache Line Size      */
345
346         /*
347          * Fix GOT pointer:
348          *
349          * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
350          *
351          * Offset:
352          */
353         sub     r15, r10, r4
354
355         /* First our own GOT */
356         add     r12, r12, r15
357         /* then the one used by the C code */
358         add     r30, r30, r15
359
360         /*
361          * Now relocate code
362          */
363
364         cmplw   cr1,r3,r4
365         addi    r0,r5,3
366         srwi.   r0,r0,2
367         beq     cr1,4f          /* In place copy is not necessary       */
368         beq     7f              /* Protect against 0 count              */
369         mtctr   r0
370         bge     cr1,2f
371
372         la      r8,-4(r4)
373         la      r7,-4(r3)
374 1:      lwzu    r0,4(r8)
375         stwu    r0,4(r7)
376         bdnz    1b
377         b       4f
378
379 2:      slwi    r0,r0,2
380         add     r8,r4,r0
381         add     r7,r3,r0
382 3:      lwzu    r0,-4(r8)
383         stwu    r0,-4(r7)
384         bdnz    3b
385
386 /*
387  * Now flush the cache: note that we must start from a cache aligned
388  * address. Otherwise we might miss one cache line.
389  */
390 4:      cmpwi   r6,0
391         add     r5,r3,r5
392         beq     7f              /* Always flush prefetch queue in any case */
393         subi    r0,r6,1
394         andc    r3,r3,r0
395         mr      r4,r3
396 5:      dcbst   0,r4
397         add     r4,r4,r6
398         cmplw   r4,r5
399         blt     5b
400         sync                    /* Wait for all dcbst to complete on bus */
401         mr      r4,r3
402 6:      icbi    0,r4
403         add     r4,r4,r6
404         cmplw   r4,r5
405         blt     6b
406 7:      sync                    /* Wait for all icbi to complete on bus */
407         isync
408
409 /*
410  * We are done. Do not return, instead branch to second part of board
411  * initialization, now running from RAM.
412  */
413
414         addi    r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
415         mtlr    r0
416         blr
417
418 in_ram:
419
420         /*
421          * Relocation Function, r12 point to got2+0x8000
422          *
423          * Adjust got2 pointers, no need to check for 0, this code
424          * already puts a few entries in the table.
425          */
426         li      r0,__got2_entries@sectoff@l
427         la      r3,GOT(_GOT2_TABLE_)
428         lwz     r11,GOT(_GOT2_TABLE_)
429         mtctr   r0
430         sub     r11,r3,r11
431         addi    r3,r3,-4
432 1:      lwzu    r0,4(r3)
433         cmpwi   r0,0
434         beq-    2f
435         add     r0,r0,r11
436         stw     r0,0(r3)
437 2:      bdnz    1b
438
439         /*
440          * Now adjust the fixups and the pointers to the fixups
441          * in case we need to move ourselves again.
442          */
443         li      r0,__fixup_entries@sectoff@l
444         lwz     r3,GOT(_FIXUP_TABLE_)
445         cmpwi   r0,0
446         mtctr   r0
447         addi    r3,r3,-4
448         beq     4f
449 3:      lwzu    r4,4(r3)
450         lwzux   r0,r4,r11
451         cmpwi   r0,0
452         add     r0,r0,r11
453         stw     r4,0(r3)
454         beq-    5f
455         stw     r0,0(r4)
456 5:      bdnz    3b
457 4:
458 clear_bss:
459         /*
460          * Now clear BSS segment
461          */
462         lwz     r3,GOT(__bss_start)
463         lwz     r4,GOT(__bss_end)
464
465         cmplw   0, r3, r4
466         beq     6f
467
468         li      r0, 0
469 5:
470         stw     r0, 0(r3)
471         addi    r3, r3, 4
472         cmplw   0, r3, r4
473         bne     5b
474 6:
475
476         mr      r3, r9          /* Global Data pointer          */
477         mr      r4, r10         /* Destination Address          */
478         bl      board_init_r
479
480         /*
481          * Copy exception vector code to low memory
482          *
483          * r3: dest_addr
484          * r7: source address, r8: end address, r9: target address
485          */
486         .globl  trap_init
487 trap_init:
488         mflr    r4                      /* save link register           */
489         GET_GOT
490         lwz     r7, GOT(_start)
491         lwz     r8, GOT(_end_of_vectors)
492
493         li      r9, 0x100               /* reset vector always at 0x100 */
494
495         cmplw   0, r7, r8
496         bgelr                           /* return if r7>=r8 - just in case */
497 1:
498         lwz     r0, 0(r7)
499         stw     r0, 0(r9)
500         addi    r7, r7, 4
501         addi    r9, r9, 4
502         cmplw   0, r7, r8
503         bne     1b
504
505         /*
506          * relocate `hdlr' and `int_return' entries
507          */
508         li      r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
509         li      r8, Alignment - _start + EXC_OFF_SYS_RESET
510 2:
511         bl      trap_reloc
512         addi    r7, r7, 0x100           /* next exception vector        */
513         cmplw   0, r7, r8
514         blt     2b
515
516         li      r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
517         bl      trap_reloc
518
519         li      r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
520         bl      trap_reloc
521
522         li      r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
523         li      r8, SystemCall - _start + EXC_OFF_SYS_RESET
524 3:
525         bl      trap_reloc
526         addi    r7, r7, 0x100           /* next exception vector        */
527         cmplw   0, r7, r8
528         blt     3b
529
530         li      r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
531         li      r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
532 4:
533         bl      trap_reloc
534         addi    r7, r7, 0x100           /* next exception vector        */
535         cmplw   0, r7, r8
536         blt     4b
537
538         mtlr    r4                      /* restore link register        */
539         blr