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,2001,2002 Wolfgang Denk <wd@denx.de>
6 * SPDX-License-Identifier: GPL-2.0+
9 /* U-Boot - Startup Code for PowerPC based Embedded Boards
12 * The processor starts at 0x00000100 and the code is executed
13 * from flash. The code is organized to be at an other address
14 * in memory, but as long we don't jump around before relocating.
15 * board_init lies at a quite high address and when the cpu has
16 * jumped there, everything is ok.
17 * This works because the cpu gives the FLASH (CS0) the whole
18 * address space at startup, and board_init lies as a echo of
19 * the flash somewhere up there in the memorymap.
21 * board_init will change CS0 to be positioned at the correct
22 * address and (s)dram will be positioned at address 0
24 #include <asm-offsets.h>
29 #include <ppc_asm.tmpl>
32 #include <asm/cache.h>
34 #include <asm/u-boot.h>
36 /* We don't want the MMU yet.
39 /* FP, Machine Check and Recoverable Interr. */
40 #define MSR_KERNEL ( MSR_FP | MSR_ME | MSR_RI )
43 * Set up GOT: Global Offset Table
45 * Use r12 to access the GOT
48 GOT_ENTRY(_GOT2_TABLE_)
49 GOT_ENTRY(_FIXUP_TABLE_)
52 GOT_ENTRY(_start_of_vectors)
53 GOT_ENTRY(_end_of_vectors)
54 GOT_ENTRY(transfer_to_handler)
58 GOT_ENTRY(__bss_start)
62 * r3 - 1st arg to board_init(): IMMP pointer
63 * r4 - 2nd arg to board_init(): boot flag
66 .long 0x27051956 /* U-Boot Magic Number */
69 .ascii U_BOOT_VERSION_STRING, "\0"
74 /* Initialize machine status; enable machine check interrupt */
75 /*----------------------------------------------------------------------*/
76 li r3, MSR_KERNEL /* Set FP, ME, RI flags */
78 mtspr SRR1, r3 /* Make SRR1 match MSR */
80 addis r0,0,0x0000 /* lets make sure that r0 is really 0 */
81 mtspr HID0, r0 /* disable I and D caches */
83 mfspr r3, ICR /* clear Interrupt Cause Register */
85 mfmsr r3 /* turn off address translation */
91 sync /* the MMU should be off... */
96 * Setup BATs - cannot be done in C since we don't have a stack yet
103 ori r3, r3, (MSR_IR | MSR_DR)
106 /* Enable and invalidate data cache.
110 ori r3, r3, HID0_DCE | HID0_DCI
117 /* Allocate Initial RAM in data cache.
119 lis r3, CONFIG_SYS_INIT_RAM_ADDR@h
120 ori r3, r3, CONFIG_SYS_INIT_RAM_ADDR@l
128 /* Lock way0 in data cache.
139 * Thisk the stack pointer *somewhere* sensible. Doesnt
140 * matter much where as we'll move it when we relocate
142 lis r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@h
143 ori r1, r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@l
145 li r0, 0 /* Make room for stack frame header and */
146 stwu r0, -4(r1) /* clear final stack frame so that */
147 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
149 /* let the C-code set up the rest */
151 /* Be careful to keep code relocatable ! */
152 /*----------------------------------------------------------------------*/
154 GET_GOT /* initialize GOT access */
157 bl cpu_init_f /* run low-level CPU init code (from Flash) */
159 bl board_init_f /* run 1st part of board init code (from Flash) */
161 /* NOTREACHED - board_init_f() does not return */
164 .globl _start_of_vectors
168 STD_EXCEPTION(EXC_OFF_MACH_CHCK, MachineCheck, MachineCheckException)
170 /* Data Storage exception. "Never" generated on the 860. */
171 STD_EXCEPTION(EXC_OFF_DATA_STOR, DataStorage, UnknownException)
173 /* Instruction Storage exception. "Never" generated on the 860. */
174 STD_EXCEPTION(EXC_OFF_INS_STOR, InstStorage, UnknownException)
176 /* External Interrupt exception. */
177 STD_EXCEPTION(EXC_OFF_EXTERNAL, ExtInterrupt, external_interrupt)
179 /* Alignment exception. */
182 EXCEPTION_PROLOG(SRR0, SRR1)
187 addi r3,r1,STACK_FRAME_OVERHEAD
188 EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
190 /* Program check exception */
193 EXCEPTION_PROLOG(SRR0, SRR1)
194 addi r3,r1,STACK_FRAME_OVERHEAD
195 EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
198 /* No FPU on MPC8xx. This exception is not supposed to happen.
200 STD_EXCEPTION(EXC_OFF_FPUNAVAIL, FPUnavailable, UnknownException)
202 /* I guess we could implement decrementer, and may have
203 * to someday for timekeeping.
205 STD_EXCEPTION(EXC_OFF_DECR, Decrementer, timer_interrupt)
206 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
207 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
208 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
210 STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException)
212 STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException)
213 STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException)
215 STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException)
216 STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException)
217 STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException)
218 STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, DebugException)
219 STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException)
220 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
221 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
222 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
223 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
224 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
225 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
226 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
227 STD_EXCEPTION(0x1c00, ReservedC, UnknownException)
228 STD_EXCEPTION(0x1d00, ReservedD, UnknownException)
229 STD_EXCEPTION(0x1e00, ReservedE, UnknownException)
230 STD_EXCEPTION(0x1f00, ReservedF, UnknownException)
232 STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException)
234 .globl _end_of_vectors
241 * This code finishes saving the registers to the exception frame
242 * and jumps to the appropriate handler for the exception.
243 * Register r21 is pointer into trap frame, r1 has new stack pointer.
245 .globl transfer_to_handler
257 mfspr r23,SPRG3 /* if from user, fix up tss.regs */
259 addi r24,r1,STACK_FRAME_OVERHEAD
261 2: addi r2,r23,-TSS /* set r2 to current */
265 andi. r24,r23,0x3f00 /* get vector offset */
269 mtspr SPRG2,r22 /* r1 is now kernel sp */
271 addi r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */
275 bgt stack_ovf /* if r2 < r1 < r2+TASK_STRUCT_SIZE */
277 lwz r24,0(r23) /* virtual address of handler */
278 lwz r23,4(r23) /* where to go when done */
280 ori r20,r20,0x30 /* enable IR, DR */
284 rfi /* jump to handler, enable MMU */
287 mfmsr r28 /* Disable interrupts */
291 SYNC /* Some chip revs need this... */
306 lwz r2,_NIP(r1) /* Restore environment */
320 mfspr r5,HID0 /* turn on the I cache. */
321 ori r5,r5,0x8800 /* Instruction cache only! */
324 and r6,r5,r6 /* clear the invalidate bit */
332 .globl icache_disable
347 srwi r3, r3, 15 /* >>15 & 1=> select bit 16 */
353 mfspr r5,HID0 /* turn on the D cache. */
354 ori r5,r5,0x4400 /* Data cache only! */
355 mfspr r4, PVR /* read PVR */
356 srawi r3, r4, 16 /* shift off the least 16 bits */
357 cmpi 0, 0, r3, 0xC /* Check for Max pvr */
359 ori r5,r5,0x0040 /* setting the DCFA bit, for Max rev 1 errata */
363 and r6,r5,r6 /* clear the invalidate bit */
371 .globl dcache_disable
386 srwi r3, r3, 14 /* >>14 & 1=> select bit 17 */
392 /*TODO : who uses this, what should it do?
403 /*------------------------------------------------------------------------------*/
406 * void relocate_code (addr_sp, gd, addr_moni)
408 * This "function" does not return, instead it continues in RAM
409 * after relocating the monitor code.
413 * r5 = length in bytes
419 mr r1, r3 /* Set new stack pointer */
420 mr r9, r4 /* Save copy of Global Data pointer */
421 mr r10, r5 /* Save copy of Destination Address */
424 mr r3, r5 /* Destination Address */
425 #ifdef CONFIG_SYS_RAMBOOT
426 lis r4, CONFIG_SYS_SDRAM_BASE@h /* Source Address */
427 ori r4, r4, CONFIG_SYS_SDRAM_BASE@l
429 lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
430 ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
432 lwz r5, GOT(__init_end)
434 li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
439 * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
445 /* First our own GOT */
447 /* the the one used by the C code */
457 beq cr1,4f /* In place copy is not necessary */
458 beq 7f /* Protect against 0 count */
477 /* Unlock the data cache and invalidate locked area */
480 lis r4, CONFIG_SYS_INIT_RAM_ADDR@h
481 ori r4, r4, CONFIG_SYS_INIT_RAM_ADDR@l
490 * Now flush the cache: note that we must start from a cache aligned
491 * address. Otherwise we might miss one cache line.
495 beq 7f /* Always flush prefetch queue in any case */
503 sync /* Wait for all dcbst to complete on bus */
509 7: sync /* Wait for all icbi to complete on bus */
513 * We are done. Do not return, instead branch to second part of board
514 * initialization, now running from RAM.
517 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
524 * Relocation Function, r12 point to got2+0x8000
526 * Adjust got2 pointers, no need to check for 0, this code
527 * already puts a few entries in the table.
529 li r0,__got2_entries@sectoff@l
530 la r3,GOT(_GOT2_TABLE_)
531 lwz r11,GOT(_GOT2_TABLE_)
543 * Now adjust the fixups and the pointers to the fixups
544 * in case we need to move ourselves again.
546 li r0,__fixup_entries@sectoff@l
547 lwz r3,GOT(_FIXUP_TABLE_)
563 * Now clear BSS segment
565 lwz r3,GOT(__bss_start)
566 lwz r4,GOT(__bss_end)
579 mr r3, r9 /* Global Data pointer */
580 mr r4, r10 /* Destination Address */
584 * Copy exception vector code to low memory
587 * r7: source address, r8: end address, r9: target address
591 mflr r4 /* save link register */
594 lwz r8, GOT(_end_of_vectors)
596 li r9, 0x100 /* reset vector always at 0x100 */
599 bgelr /* return if r7>=r8 - just in case */
609 * relocate `hdlr' and `int_return' entries
611 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
612 li r8, Alignment - _start + EXC_OFF_SYS_RESET
615 addi r7, r7, 0x100 /* next exception vector */
619 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
622 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
625 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
626 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
629 addi r7, r7, 0x100 /* next exception vector */
633 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
634 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
637 addi r7, r7, 0x100 /* next exception vector */
641 mtlr r4 /* restore link register */
644 /* Setup the BAT registers.
647 lis r4, CONFIG_SYS_IBAT0L@h
648 ori r4, r4, CONFIG_SYS_IBAT0L@l
649 lis r3, CONFIG_SYS_IBAT0U@h
650 ori r3, r3, CONFIG_SYS_IBAT0U@l
655 lis r4, CONFIG_SYS_DBAT0L@h
656 ori r4, r4, CONFIG_SYS_DBAT0L@l
657 lis r3, CONFIG_SYS_DBAT0U@h
658 ori r3, r3, CONFIG_SYS_DBAT0U@l
663 lis r4, CONFIG_SYS_IBAT1L@h
664 ori r4, r4, CONFIG_SYS_IBAT1L@l
665 lis r3, CONFIG_SYS_IBAT1U@h
666 ori r3, r3, CONFIG_SYS_IBAT1U@l
671 lis r4, CONFIG_SYS_DBAT1L@h
672 ori r4, r4, CONFIG_SYS_DBAT1L@l
673 lis r3, CONFIG_SYS_DBAT1U@h
674 ori r3, r3, CONFIG_SYS_DBAT1U@l
679 lis r4, CONFIG_SYS_IBAT2L@h
680 ori r4, r4, CONFIG_SYS_IBAT2L@l
681 lis r3, CONFIG_SYS_IBAT2U@h
682 ori r3, r3, CONFIG_SYS_IBAT2U@l
687 lis r4, CONFIG_SYS_DBAT2L@h
688 ori r4, r4, CONFIG_SYS_DBAT2L@l
689 lis r3, CONFIG_SYS_DBAT2U@h
690 ori r3, r3, CONFIG_SYS_DBAT2U@l
695 lis r4, CONFIG_SYS_IBAT3L@h
696 ori r4, r4, CONFIG_SYS_IBAT3L@l
697 lis r3, CONFIG_SYS_IBAT3U@h
698 ori r3, r3, CONFIG_SYS_IBAT3U@l
703 lis r4, CONFIG_SYS_DBAT3L@h
704 ori r4, r4, CONFIG_SYS_DBAT3L@l
705 lis r3, CONFIG_SYS_DBAT3U@h
706 ori r3, r3, CONFIG_SYS_DBAT3U@l
712 * -> for (val = 0; val < 0x20000; val+=0x1000)