2 * Copyright (C) 1998 Dan Malek <dmalek@jlc.net>
3 * Copyright (C) 1999 Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se>
4 * Copyright (C) 2000 - 2003 Wolfgang Denk <wd@denx.de>
6 * SPDX-License-Identifier: GPL-2.0+
10 * U-Boot - Startup Code for MPC5xxx CPUs
12 #include <asm-offsets.h>
17 #define CONFIG_MPC5xxx 1 /* needed for Linux kernel header files */
18 #define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */
20 #include <ppc_asm.tmpl>
23 #include <asm/cache.h>
25 #include <asm/u-boot.h>
27 /* We don't want the MMU yet.
30 /* Floating Point enable, Machine Check and Recoverable Interr. */
32 #define MSR_KERNEL (MSR_FP|MSR_RI)
34 #define MSR_KERNEL (MSR_FP|MSR_ME|MSR_RI)
37 #ifndef CONFIG_SPL_BUILD
39 * Set up GOT: Global Offset Table
41 * Use r12 to access the GOT
44 GOT_ENTRY(_GOT2_TABLE_)
45 GOT_ENTRY(_FIXUP_TABLE_)
48 GOT_ENTRY(_start_of_vectors)
49 GOT_ENTRY(_end_of_vectors)
50 GOT_ENTRY(transfer_to_handler)
54 GOT_ENTRY(__bss_start)
64 .ascii U_BOOT_VERSION_STRING, "\0"
74 #if defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD)
76 * This is the entry of the real U-Boot from a board port
77 * that supports SPL booting on the MPC5200. We only need
78 * to call board_init_f() here. Everything else has already
79 * been done in the SPL u-boot version.
81 GET_GOT /* initialize GOT access */
82 bl board_init_f /* run 1st part of board init code (in Flash)*/
83 /* NOTREACHED - board_init_f() does not return */
85 mfmsr r5 /* save msr contents */
87 /* Move CSBoot and adjust instruction pointer */
88 /*--------------------------------------------------------------*/
90 #if defined(CONFIG_SYS_LOWBOOT)
91 # if defined(CONFIG_SYS_RAMBOOT)
92 # error CONFIG_SYS_LOWBOOT is incompatible with CONFIG_SYS_RAMBOOT
93 # endif /* CONFIG_SYS_RAMBOOT */
94 lis r4, CONFIG_SYS_DEFAULT_MBAR@h
95 lis r3, START_REG(CONFIG_SYS_BOOTCS_START)@h
96 ori r3, r3, START_REG(CONFIG_SYS_BOOTCS_START)@l
97 stw r3, 0x4(r4) /* CS0 start */
98 lis r3, STOP_REG(CONFIG_SYS_BOOTCS_START, CONFIG_SYS_BOOTCS_SIZE)@h
99 ori r3, r3, STOP_REG(CONFIG_SYS_BOOTCS_START, CONFIG_SYS_BOOTCS_SIZE)@l
100 stw r3, 0x8(r4) /* CS0 stop */
102 ori r3, r3, 0x02010000@l
103 stw r3, 0x54(r4) /* CS0 and Boot enable */
105 lis r3, lowboot_reentry@h /* jump from bootlow address space (0x0000xxxx) */
106 ori r3, r3, lowboot_reentry@l /* to the address space the linker used */
111 lis r3, START_REG(CONFIG_SYS_BOOTCS_START)@h
112 ori r3, r3, START_REG(CONFIG_SYS_BOOTCS_START)@l
113 stw r3, 0x4c(r4) /* Boot start */
114 lis r3, STOP_REG(CONFIG_SYS_BOOTCS_START, CONFIG_SYS_BOOTCS_SIZE)@h
115 ori r3, r3, STOP_REG(CONFIG_SYS_BOOTCS_START, CONFIG_SYS_BOOTCS_SIZE)@l
116 stw r3, 0x50(r4) /* Boot stop */
118 ori r3, r3, 0x02000001@l
119 stw r3, 0x54(r4) /* Boot enable, CS0 disable */
120 #endif /* CONFIG_SYS_LOWBOOT */
122 #if defined(CONFIG_SYS_DEFAULT_MBAR) && !defined(CONFIG_SYS_RAMBOOT)
123 lis r3, CONFIG_SYS_MBAR@h
124 ori r3, r3, CONFIG_SYS_MBAR@l
125 /* MBAR is mirrored into the MBAR SPR */
127 rlwinm r3, r3, 16, 16, 31
128 lis r4, CONFIG_SYS_DEFAULT_MBAR@h
130 #endif /* CONFIG_SYS_DEFAULT_MBAR */
132 /* Initialise the MPC5xxx processor core */
133 /*--------------------------------------------------------------*/
137 /* initialize some things that are hard to access from C */
138 /*--------------------------------------------------------------*/
140 /* set up stack in on-chip SRAM */
141 lis r3, CONFIG_SYS_INIT_RAM_ADDR@h
142 ori r3, r3, CONFIG_SYS_INIT_RAM_ADDR@l
143 ori r1, r3, CONFIG_SYS_INIT_SP_OFFSET
144 li r0, 0 /* Make room for stack frame header and */
145 stwu r0, -4(r1) /* clear final stack frame so that */
146 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
148 /* let the C-code set up the rest */
150 /* Be careful to keep code relocatable ! */
151 /*--------------------------------------------------------------*/
153 #ifndef CONFIG_SPL_BUILD
154 GET_GOT /* initialize GOT access */
158 bl cpu_init_f /* run low-level CPU init code (in Flash)*/
160 bl board_init_f /* run 1st part of board init code (in Flash)*/
162 /* NOTREACHED - board_init_f() does not return */
165 #ifndef CONFIG_SPL_BUILD
170 .globl _start_of_vectors
174 STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
176 /* Data Storage exception. */
177 STD_EXCEPTION(0x300, DataStorage, UnknownException)
179 /* Instruction Storage exception. */
180 STD_EXCEPTION(0x400, InstStorage, UnknownException)
182 /* External Interrupt exception. */
183 STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
185 /* Alignment exception. */
188 EXCEPTION_PROLOG(SRR0, SRR1)
193 addi r3,r1,STACK_FRAME_OVERHEAD
194 EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
196 /* Program check exception */
199 EXCEPTION_PROLOG(SRR0, SRR1)
200 addi r3,r1,STACK_FRAME_OVERHEAD
201 EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
204 STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
206 /* I guess we could implement decrementer, and may have
207 * to someday for timekeeping.
209 STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
211 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
212 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
213 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
214 STD_EXCEPTION(0xd00, SingleStep, UnknownException)
216 STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
217 STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
219 STD_EXCEPTION(0x1000, InstructionTLBMiss, UnknownException)
220 STD_EXCEPTION(0x1100, DataLoadTLBMiss, UnknownException)
221 STD_EXCEPTION(0x1200, DataStoreTLBMiss, UnknownException)
225 * This exception occurs when the program counter matches the
226 * Instruction Address Breakpoint Register (IABR).
228 * I want the cpu to halt if this occurs so I can hunt around
229 * with the debugger and look at things.
231 * When DEBUG is defined, both machine check enable (in the MSR)
232 * and checkstop reset enable (in the reset mode register) are
233 * turned off and so a checkstop condition will result in the cpu
236 * I force the cpu into a checkstop condition by putting an illegal
237 * instruction here (at least this is the theory).
239 * well - that didnt work, so just do an infinite loop!
243 STD_EXCEPTION(0x1300, InstructionBreakpoint, DebugException)
245 STD_EXCEPTION(0x1400, SMI, UnknownException)
247 STD_EXCEPTION(0x1500, Trap_15, UnknownException)
248 STD_EXCEPTION(0x1600, Trap_16, UnknownException)
249 STD_EXCEPTION(0x1700, Trap_17, UnknownException)
250 STD_EXCEPTION(0x1800, Trap_18, UnknownException)
251 STD_EXCEPTION(0x1900, Trap_19, UnknownException)
252 STD_EXCEPTION(0x1a00, Trap_1a, UnknownException)
253 STD_EXCEPTION(0x1b00, Trap_1b, UnknownException)
254 STD_EXCEPTION(0x1c00, Trap_1c, UnknownException)
255 STD_EXCEPTION(0x1d00, Trap_1d, UnknownException)
256 STD_EXCEPTION(0x1e00, Trap_1e, UnknownException)
257 STD_EXCEPTION(0x1f00, Trap_1f, UnknownException)
258 STD_EXCEPTION(0x2000, Trap_20, UnknownException)
259 STD_EXCEPTION(0x2100, Trap_21, UnknownException)
260 STD_EXCEPTION(0x2200, Trap_22, UnknownException)
261 STD_EXCEPTION(0x2300, Trap_23, UnknownException)
262 STD_EXCEPTION(0x2400, Trap_24, UnknownException)
263 STD_EXCEPTION(0x2500, Trap_25, UnknownException)
264 STD_EXCEPTION(0x2600, Trap_26, UnknownException)
265 STD_EXCEPTION(0x2700, Trap_27, UnknownException)
266 STD_EXCEPTION(0x2800, Trap_28, UnknownException)
267 STD_EXCEPTION(0x2900, Trap_29, UnknownException)
268 STD_EXCEPTION(0x2a00, Trap_2a, UnknownException)
269 STD_EXCEPTION(0x2b00, Trap_2b, UnknownException)
270 STD_EXCEPTION(0x2c00, Trap_2c, UnknownException)
271 STD_EXCEPTION(0x2d00, Trap_2d, UnknownException)
272 STD_EXCEPTION(0x2e00, Trap_2e, UnknownException)
273 STD_EXCEPTION(0x2f00, Trap_2f, UnknownException)
276 .globl _end_of_vectors
282 * This code finishes saving the registers to the exception frame
283 * and jumps to the appropriate handler for the exception.
284 * Register r21 is pointer into trap frame, r1 has new stack pointer.
286 .globl transfer_to_handler
297 andi. r24,r23,0x3f00 /* get vector offset */
301 lwz r24,0(r23) /* virtual address of handler */
302 lwz r23,4(r23) /* where to go when done */
307 rfi /* jump to handler, enable MMU */
310 mfmsr r28 /* Disable interrupts */
314 SYNC /* Some chip revs need this... */
329 lwz r2,_NIP(r1) /* Restore environment */
338 #endif /* CONFIG_SPL_BUILD */
341 * This code initialises the MPC5xxx processor core
342 * (conforms to PowerPC 603e spec)
343 * Note: expects original MSR contents to be in r5.
349 /* Initialize machine status; enable machine check interrupt */
350 /*--------------------------------------------------------------*/
352 li r3, MSR_KERNEL /* Set ME and RI flags */
353 rlwimi r3, r5, 0, 25, 25 /* preserve IP bit set by HRCW */
355 rlwimi r3, r5, 0, 21, 22 /* debugger might set SE & BE bits */
357 SYNC /* Some chip revs need this... */
360 mtspr SRR1, r3 /* Make SRR1 match MSR */
362 /* Initialize the Hardware Implementation-dependent Registers */
363 /* HID0 also contains cache control */
364 /*--------------------------------------------------------------*/
366 lis r3, CONFIG_SYS_HID0_INIT@h
367 ori r3, r3, CONFIG_SYS_HID0_INIT@l
371 lis r3, CONFIG_SYS_HID0_FINAL@h
372 ori r3, r3, CONFIG_SYS_HID0_FINAL@l
376 /* clear all BAT's */
377 /*--------------------------------------------------------------*/
414 /* invalidate all tlb's */
416 /* From the 603e User Manual: "The 603e provides the ability to */
417 /* invalidate a TLB entry. The TLB Invalidate Entry (tlbie) */
418 /* instruction invalidates the TLB entry indexed by the EA, and */
419 /* operates on both the instruction and data TLBs simultaneously*/
420 /* invalidating four TLB entries (both sets in each TLB). The */
421 /* index corresponds to bits 15-19 of the EA. To invalidate all */
422 /* entries within both TLBs, 32 tlbie instructions should be */
423 /* issued, incrementing this field by one each time." */
425 /* "Note that the tlbia instruction is not implemented on the */
428 /* bits 15-19 correspond to addresses 0x00000000 to 0x0001F000 */
429 /* incrementing by 0x1000 each time. The code below is sort of */
430 /* based on code in "flush_tlbs" from arch/powerpc/kernel/head.S */
432 /*--------------------------------------------------------------*/
443 /*--------------------------------------------------------------*/
449 * Note: requires that all cache bits in
450 * HID0 are in the low half word.
457 ori r4, r4, HID0_ILOCK
459 ori r4, r3, HID0_ICFI
461 mtspr HID0, r4 /* sets enable and invalidate, clears lock */
463 mtspr HID0, r3 /* clears invalidate */
466 .globl icache_disable
470 ori r4, r4, HID0_ICE|HID0_ILOCK
472 ori r4, r3, HID0_ICFI
474 mtspr HID0, r4 /* sets invalidate, clears enable and lock */
476 mtspr HID0, r3 /* clears invalidate */
482 rlwinm r3, r3, HID0_ICE_BITPOS + 1, 31, 31
490 ori r4, r4, HID0_DLOCK
494 mtspr HID0, r4 /* sets enable and invalidate, clears lock */
496 mtspr HID0, r3 /* clears invalidate */
499 .globl dcache_disable
503 ori r4, r4, HID0_DCE|HID0_DLOCK
507 mtspr HID0, r4 /* sets invalidate, clears enable and lock */
509 mtspr HID0, r3 /* clears invalidate */
515 rlwinm r3, r3, HID0_DCE_BITPOS + 1, 31, 31
528 #ifndef CONFIG_SPL_BUILD
529 /*------------------------------------------------------------------------------*/
532 * void relocate_code (addr_sp, gd, addr_moni)
534 * This "function" does not return, instead it continues in RAM
535 * after relocating the monitor code.
539 * r5 = length in bytes
544 mr r1, r3 /* Set new stack pointer */
545 mr r9, r4 /* Save copy of Global Data pointer */
546 mr r10, r5 /* Save copy of Destination Address */
549 mr r3, r5 /* Destination Address */
550 lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
551 ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
552 lwz r5, GOT(__init_end)
554 li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
559 * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
565 /* First our own GOT */
567 /* then the one used by the C code */
577 beq cr1,4f /* In place copy is not necessary */
578 beq 7f /* Protect against 0 count */
597 * Now flush the cache: note that we must start from a cache aligned
598 * address. Otherwise we might miss one cache line.
602 beq 7f /* Always flush prefetch queue in any case */
605 mfspr r7,HID0 /* don't do dcbst if dcache is disabled */
606 rlwinm r7,r7,HID0_DCE_BITPOS+1,31,31
614 sync /* Wait for all dcbst to complete on bus */
615 9: mfspr r7,HID0 /* don't do icbi if icache is disabled */
616 rlwinm r7,r7,HID0_ICE_BITPOS+1,31,31
624 7: sync /* Wait for all icbi to complete on bus */
628 * We are done. Do not return, instead branch to second part of board
629 * initialization, now running from RAM.
632 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
639 * Relocation Function, r12 point to got2+0x8000
641 * Adjust got2 pointers, no need to check for 0, this code
642 * already puts a few entries in the table.
644 li r0,__got2_entries@sectoff@l
645 la r3,GOT(_GOT2_TABLE_)
646 lwz r11,GOT(_GOT2_TABLE_)
658 * Now adjust the fixups and the pointers to the fixups
659 * in case we need to move ourselves again.
661 li r0,__fixup_entries@sectoff@l
662 lwz r3,GOT(_FIXUP_TABLE_)
678 * Now clear BSS segment
680 lwz r3,GOT(__bss_start)
681 lwz r4,GOT(__bss_end)
694 mr r3, r9 /* Global Data pointer */
695 mr r4, r10 /* Destination Address */
699 * Copy exception vector code to low memory
702 * r7: source address, r8: end address, r9: target address
706 mflr r4 /* save link register */
709 lwz r8, GOT(_end_of_vectors)
711 li r9, 0x100 /* reset vector always at 0x100 */
714 bgelr /* return if r7>=r8 - just in case */
724 * relocate `hdlr' and `int_return' entries
726 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
727 li r8, Alignment - _start + EXC_OFF_SYS_RESET
730 addi r7, r7, 0x100 /* next exception vector */
734 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
737 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
740 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
741 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
744 addi r7, r7, 0x100 /* next exception vector */
748 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
749 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
752 addi r7, r7, 0x100 /* next exception vector */
756 mfmsr r3 /* now that the vectors have */
757 lis r7, MSR_IP@h /* relocated into low memory */
758 ori r7, r7, MSR_IP@l /* MSR[IP] can be turned off */
759 andc r3, r3, r7 /* (if it was on) */
760 SYNC /* Some chip revs need this... */
764 mtlr r4 /* restore link register */
767 #endif /* CONFIG_SPL_BUILD */