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 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 /* U-Boot - Startup Code for PowerPC based Embedded Boards
28 * The processor starts at 0x00000100 and the code is executed
29 * from flash. The code is organized to be at an other address
30 * in memory, but as long we don't jump around before relocating.
31 * board_init lies at a quite high address and when the cpu has
32 * jumped there, everything is ok.
33 * This works because the cpu gives the FLASH (CS0) the whole
34 * address space at startup, and board_init lies as a echo of
35 * the flash somewhere up there in the memorymap.
37 * board_init will change CS0 to be positioned at the correct
38 * address and (s)dram will be positioned at address 0
42 #include <timestamp.h>
45 #define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */
47 #include <ppc_asm.tmpl>
50 #include <asm/cache.h>
52 #include <asm/u-boot.h>
54 #ifndef CONFIG_IDENT_STRING
55 #define CONFIG_IDENT_STRING ""
58 /* We don't want the MMU yet.
61 /* FP, Machine Check and Recoverable Interr. */
62 #define MSR_KERNEL ( MSR_FP | MSR_ME | MSR_RI )
65 * Set up GOT: Global Offset Table
67 * Use r12 to access the GOT
70 GOT_ENTRY(_GOT2_TABLE_)
71 GOT_ENTRY(_FIXUP_TABLE_)
74 GOT_ENTRY(_start_of_vectors)
75 GOT_ENTRY(_end_of_vectors)
76 GOT_ENTRY(transfer_to_handler)
80 GOT_ENTRY(__bss_start)
81 #if defined(CONFIG_FADS)
82 GOT_ENTRY(environment)
87 * r3 - 1st arg to board_init(): IMMP pointer
88 * r4 - 2nd arg to board_init(): boot flag
91 .long 0x27051956 /* U-Boot Magic Number */
95 .ascii " (", U_BOOT_DATE, " - ", U_BOOT_TIME, ")"
96 .ascii CONFIG_IDENT_STRING, "\0"
101 /* Initialize machine status; enable machine check interrupt */
102 /*----------------------------------------------------------------------*/
103 li r3, MSR_KERNEL /* Set FP, ME, RI flags */
105 mtspr SRR1, r3 /* Make SRR1 match MSR */
107 addis r0,0,0x0000 /* lets make sure that r0 is really 0 */
108 mtspr HID0, r0 /* disable I and D caches */
110 mfspr r3, ICR /* clear Interrupt Cause Register */
112 mfmsr r3 /* turn off address translation */
118 sync /* the MMU should be off... */
122 #if defined(CONFIG_BMW)
123 bl early_init_f /* Must be ASM: no stack yet! */
126 * Setup BATs - cannot be done in C since we don't have a stack yet
133 ori r3, r3, (MSR_IR | MSR_DR)
135 #if !defined(CONFIG_BMW)
136 /* Enable and invalidate data cache.
140 ori r3, r3, HID0_DCE | HID0_DCI
147 /* Allocate Initial RAM in data cache.
149 lis r3, CONFIG_SYS_INIT_RAM_ADDR@h
150 ori r3, r3, CONFIG_SYS_INIT_RAM_ADDR@l
158 /* Lock way0 in data cache.
167 #endif /* !CONFIG_BMW */
169 * Thisk the stack pointer *somewhere* sensible. Doesnt
170 * matter much where as we'll move it when we relocate
172 lis r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@h
173 ori r1, r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@l
175 li r0, 0 /* Make room for stack frame header and */
176 stwu r0, -4(r1) /* clear final stack frame so that */
177 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
179 /* let the C-code set up the rest */
181 /* Be careful to keep code relocatable ! */
182 /*----------------------------------------------------------------------*/
184 GET_GOT /* initialize GOT access */
187 bl cpu_init_f /* run low-level CPU init code (from Flash) */
189 bl board_init_f /* run 1st part of board init code (from Flash) */
191 /* NOTREACHED - board_init_f() does not return */
194 .globl _start_of_vectors
198 STD_EXCEPTION(EXC_OFF_MACH_CHCK, MachineCheck, MachineCheckException)
200 /* Data Storage exception. "Never" generated on the 860. */
201 STD_EXCEPTION(EXC_OFF_DATA_STOR, DataStorage, UnknownException)
203 /* Instruction Storage exception. "Never" generated on the 860. */
204 STD_EXCEPTION(EXC_OFF_INS_STOR, InstStorage, UnknownException)
206 /* External Interrupt exception. */
207 STD_EXCEPTION(EXC_OFF_EXTERNAL, ExtInterrupt, external_interrupt)
209 /* Alignment exception. */
212 EXCEPTION_PROLOG(SRR0, SRR1)
217 addi r3,r1,STACK_FRAME_OVERHEAD
218 EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
220 /* Program check exception */
223 EXCEPTION_PROLOG(SRR0, SRR1)
224 addi r3,r1,STACK_FRAME_OVERHEAD
225 EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
228 /* No FPU on MPC8xx. This exception is not supposed to happen.
230 STD_EXCEPTION(EXC_OFF_FPUNAVAIL, FPUnavailable, UnknownException)
232 /* I guess we could implement decrementer, and may have
233 * to someday for timekeeping.
235 STD_EXCEPTION(EXC_OFF_DECR, Decrementer, timer_interrupt)
236 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
237 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
238 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
240 STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException)
242 STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException)
243 STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException)
245 STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException)
246 STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException)
247 STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException)
248 STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, DebugException)
249 STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException)
250 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
251 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
252 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
253 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
254 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
255 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
256 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
257 STD_EXCEPTION(0x1c00, ReservedC, UnknownException)
258 STD_EXCEPTION(0x1d00, ReservedD, UnknownException)
259 STD_EXCEPTION(0x1e00, ReservedE, UnknownException)
260 STD_EXCEPTION(0x1f00, ReservedF, UnknownException)
262 STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException)
264 .globl _end_of_vectors
271 * This code finishes saving the registers to the exception frame
272 * and jumps to the appropriate handler for the exception.
273 * Register r21 is pointer into trap frame, r1 has new stack pointer.
275 .globl transfer_to_handler
287 mfspr r23,SPRG3 /* if from user, fix up tss.regs */
289 addi r24,r1,STACK_FRAME_OVERHEAD
291 2: addi r2,r23,-TSS /* set r2 to current */
295 andi. r24,r23,0x3f00 /* get vector offset */
299 mtspr SPRG2,r22 /* r1 is now kernel sp */
301 addi r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */
305 bgt stack_ovf /* if r2 < r1 < r2+TASK_STRUCT_SIZE */
307 lwz r24,0(r23) /* virtual address of handler */
308 lwz r23,4(r23) /* where to go when done */
310 ori r20,r20,0x30 /* enable IR, DR */
314 rfi /* jump to handler, enable MMU */
317 mfmsr r28 /* Disable interrupts */
321 SYNC /* Some chip revs need this... */
336 lwz r2,_NIP(r1) /* Restore environment */
350 mfspr r5,HID0 /* turn on the I cache. */
351 ori r5,r5,0x8800 /* Instruction cache only! */
354 and r6,r5,r6 /* clear the invalidate bit */
362 .globl icache_disable
377 srwi r3, r3, 15 /* >>15 & 1=> select bit 16 */
383 mfspr r5,HID0 /* turn on the D cache. */
384 ori r5,r5,0x4400 /* Data cache only! */
385 mfspr r4, PVR /* read PVR */
386 srawi r3, r4, 16 /* shift off the least 16 bits */
387 cmpi 0, 0, r3, 0xC /* Check for Max pvr */
389 ori r5,r5,0x0040 /* setting the DCFA bit, for Max rev 1 errata */
393 and r6,r5,r6 /* clear the invalidate bit */
401 .globl dcache_disable
416 srwi r3, r3, 14 /* >>14 & 1=> select bit 17 */
422 /*TODO : who uses this, what should it do?
433 /*------------------------------------------------------------------------------*/
436 * void relocate_code (addr_sp, gd, addr_moni)
438 * This "function" does not return, instead it continues in RAM
439 * after relocating the monitor code.
443 * r5 = length in bytes
449 mr r1, r3 /* Set new stack pointer */
450 mr r9, r4 /* Save copy of Global Data pointer */
451 mr r10, r5 /* Save copy of Destination Address */
454 mr r3, r5 /* Destination Address */
455 #ifdef CONFIG_SYS_RAMBOOT
456 lis r4, CONFIG_SYS_SDRAM_BASE@h /* Source Address */
457 ori r4, r4, CONFIG_SYS_SDRAM_BASE@l
459 lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
460 ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
462 lwz r5, GOT(__init_end)
464 li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
469 * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
475 /* First our own GOT */
477 /* the the one used by the C code */
487 beq cr1,4f /* In place copy is not necessary */
488 beq 7f /* Protect against 0 count */
507 #if !defined(CONFIG_BMW)
508 /* Unlock the data cache and invalidate locked area */
511 lis r4, CONFIG_SYS_INIT_RAM_ADDR@h
512 ori r4, r4, CONFIG_SYS_INIT_RAM_ADDR@l
522 * Now flush the cache: note that we must start from a cache aligned
523 * address. Otherwise we might miss one cache line.
527 beq 7f /* Always flush prefetch queue in any case */
535 sync /* Wait for all dcbst to complete on bus */
541 7: sync /* Wait for all icbi to complete on bus */
545 * We are done. Do not return, instead branch to second part of board
546 * initialization, now running from RAM.
549 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
556 * Relocation Function, r12 point to got2+0x8000
558 * Adjust got2 pointers, no need to check for 0, this code
559 * already puts a few entries in the table.
561 li r0,__got2_entries@sectoff@l
562 la r3,GOT(_GOT2_TABLE_)
563 lwz r11,GOT(_GOT2_TABLE_)
575 * Now adjust the fixups and the pointers to the fixups
576 * in case we need to move ourselves again.
578 li r0,__fixup_entries@sectoff@l
579 lwz r3,GOT(_FIXUP_TABLE_)
595 * Now clear BSS segment
597 lwz r3,GOT(__bss_start)
611 mr r3, r9 /* Global Data pointer */
612 mr r4, r10 /* Destination Address */
616 * Copy exception vector code to low memory
619 * r7: source address, r8: end address, r9: target address
623 mflr r4 /* save link register */
626 lwz r8, GOT(_end_of_vectors)
628 li r9, 0x100 /* reset vector always at 0x100 */
631 bgelr /* return if r7>=r8 - just in case */
641 * relocate `hdlr' and `int_return' entries
643 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
644 li r8, Alignment - _start + EXC_OFF_SYS_RESET
647 addi r7, r7, 0x100 /* next exception vector */
651 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
654 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
657 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
658 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
661 addi r7, r7, 0x100 /* next exception vector */
665 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
666 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
669 addi r7, r7, 0x100 /* next exception vector */
673 mtlr r4 /* restore link register */
676 /* Setup the BAT registers.
679 lis r4, CONFIG_SYS_IBAT0L@h
680 ori r4, r4, CONFIG_SYS_IBAT0L@l
681 lis r3, CONFIG_SYS_IBAT0U@h
682 ori r3, r3, CONFIG_SYS_IBAT0U@l
687 lis r4, CONFIG_SYS_DBAT0L@h
688 ori r4, r4, CONFIG_SYS_DBAT0L@l
689 lis r3, CONFIG_SYS_DBAT0U@h
690 ori r3, r3, CONFIG_SYS_DBAT0U@l
695 lis r4, CONFIG_SYS_IBAT1L@h
696 ori r4, r4, CONFIG_SYS_IBAT1L@l
697 lis r3, CONFIG_SYS_IBAT1U@h
698 ori r3, r3, CONFIG_SYS_IBAT1U@l
703 lis r4, CONFIG_SYS_DBAT1L@h
704 ori r4, r4, CONFIG_SYS_DBAT1L@l
705 lis r3, CONFIG_SYS_DBAT1U@h
706 ori r3, r3, CONFIG_SYS_DBAT1U@l
711 lis r4, CONFIG_SYS_IBAT2L@h
712 ori r4, r4, CONFIG_SYS_IBAT2L@l
713 lis r3, CONFIG_SYS_IBAT2U@h
714 ori r3, r3, CONFIG_SYS_IBAT2U@l
719 lis r4, CONFIG_SYS_DBAT2L@h
720 ori r4, r4, CONFIG_SYS_DBAT2L@l
721 lis r3, CONFIG_SYS_DBAT2U@h
722 ori r3, r3, CONFIG_SYS_DBAT2U@l
727 lis r4, CONFIG_SYS_IBAT3L@h
728 ori r4, r4, CONFIG_SYS_IBAT3L@l
729 lis r3, CONFIG_SYS_IBAT3U@h
730 ori r3, r3, CONFIG_SYS_IBAT3U@l
735 lis r4, CONFIG_SYS_DBAT3L@h
736 ori r4, r4, CONFIG_SYS_DBAT3L@l
737 lis r3, CONFIG_SYS_DBAT3U@h
738 ori r3, r3, CONFIG_SYS_DBAT3U@l
744 * -> for (val = 0; val < 0x20000; val+=0x1000)