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
44 #define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */
46 #include <ppc_asm.tmpl>
49 #include <asm/cache.h>
52 #ifndef CONFIG_IDENT_STRING
53 #define CONFIG_IDENT_STRING ""
56 /* We don't want the MMU yet.
59 /* FP, Machine Check and Recoverable Interr. */
60 #define MSR_KERNEL ( MSR_FP | MSR_ME | MSR_RI )
63 * Set up GOT: Global Offset Table
65 * Use r14 to access the GOT
68 GOT_ENTRY(_GOT2_TABLE_)
69 GOT_ENTRY(_FIXUP_TABLE_)
72 GOT_ENTRY(_start_of_vectors)
73 GOT_ENTRY(_end_of_vectors)
74 GOT_ENTRY(transfer_to_handler)
78 GOT_ENTRY(__bss_start)
79 #if defined(CONFIG_FADS)
80 GOT_ENTRY(environment)
85 * r3 - 1st arg to board_init(): IMMP pointer
86 * r4 - 2nd arg to board_init(): boot flag
89 .long 0x27051956 /* U-Boot Magic Number */
93 .ascii " (", __DATE__, " - ", __TIME__, ")"
94 .ascii CONFIG_IDENT_STRING, "\0"
99 li r21, BOOTFLAG_COLD /* Normal Power-On: Boot from FLASH */
102 . = EXC_OFF_SYS_RESET + 0x10
106 li r21, BOOTFLAG_WARM /* Software reboot */
112 /* Initialize machine status; enable machine check interrupt */
113 /*----------------------------------------------------------------------*/
114 li r3, MSR_KERNEL /* Set FP, ME, RI flags */
116 mtspr SRR1, r3 /* Make SRR1 match MSR */
118 addis r0,0,0x0000 /* lets make sure that r0 is really 0 */
119 mtspr HID0, r0 /* disable I and D caches */
121 mfspr r3, ICR /* clear Interrupt Cause Register */
123 mfmsr r3 /* turn off address translation */
129 sync /* the MMU should be off... */
133 #if defined(CONFIG_BMW)
134 bl early_init_f /* Must be ASM: no stack yet! */
137 * Setup BATs - cannot be done in C since we don't have a stack yet
144 ori r3, r3, (MSR_IR | MSR_DR)
146 #if !defined(CONFIG_BMW)
147 /* Enable and invalidate data cache.
151 ori r3, r3, HID0_DCE | HID0_DCI
158 /* Allocate Initial RAM in data cache.
160 lis r3, CFG_INIT_RAM_ADDR@h
161 ori r3, r3, CFG_INIT_RAM_ADDR@l
169 /* Lock way0 in data cache.
178 #endif /* !CONFIG_BMW */
180 * Thisk the stack pointer *somewhere* sensible. Doesnt
181 * matter much where as we'll move it when we relocate
183 lis r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@h
184 ori r1, r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@l
186 li r0, 0 /* Make room for stack frame header and */
187 stwu r0, -4(r1) /* clear final stack frame so that */
188 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
190 /* let the C-code set up the rest */
192 /* Be careful to keep code relocatable ! */
193 /*----------------------------------------------------------------------*/
195 GET_GOT /* initialize GOT access */
198 bl cpu_init_f /* run low-level CPU init code (from Flash) */
202 bl board_init_f /* run 1st part of board init code (from Flash) */
205 .globl _start_of_vectors
209 STD_EXCEPTION(EXC_OFF_MACH_CHCK, MachineCheck, MachineCheckException)
211 /* Data Storage exception. "Never" generated on the 860. */
212 STD_EXCEPTION(EXC_OFF_DATA_STOR, DataStorage, UnknownException)
214 /* Instruction Storage exception. "Never" generated on the 860. */
215 STD_EXCEPTION(EXC_OFF_INS_STOR, InstStorage, UnknownException)
217 /* External Interrupt exception. */
218 STD_EXCEPTION(EXC_OFF_EXTERNAL, ExtInterrupt, external_interrupt)
220 /* Alignment exception. */
228 addi r3,r1,STACK_FRAME_OVERHEAD
230 rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
231 lwz r6,GOT(transfer_to_handler)
235 .long AlignmentException - _start + EXC_OFF_SYS_RESET
236 .long int_return - _start + EXC_OFF_SYS_RESET
238 /* Program check exception */
242 addi r3,r1,STACK_FRAME_OVERHEAD
244 rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
245 lwz r6,GOT(transfer_to_handler)
249 .long ProgramCheckException - _start + EXC_OFF_SYS_RESET
250 .long int_return - _start + EXC_OFF_SYS_RESET
252 /* No FPU on MPC8xx. This exception is not supposed to happen.
254 STD_EXCEPTION(EXC_OFF_FPUNAVAIL, FPUnavailable, UnknownException)
256 /* I guess we could implement decrementer, and may have
257 * to someday for timekeeping.
259 STD_EXCEPTION(EXC_OFF_DECR, Decrementer, timer_interrupt)
260 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
261 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
265 * r0 - SYSCALL number
269 addis r11,r0,0 /* get functions table addr */
270 ori r11,r11,0 /* Note: this code is patched in trap_init */
271 addis r12,r0,0 /* get number of functions */
277 rlwinm r0,r0,2,0,31 /* fn_addr = fn_tbl[r0] */
281 li r20,0xd00-4 /* Get stack pointer */
283 subi r12,r12,12 /* Adjust stack pointer */
284 li r0,0xc00+_end_back-SystemCall
285 cmplw 0, r0, r12 /* Check stack overflow */
296 li r12,0xc00+_back-SystemCall
305 mfmsr r11 /* Disable interrupts */
309 SYNC /* Some chip revs need this... */
313 li r12,0xd00-4 /* restore regs */
323 addi r12,r12,12 /* Adjust stack pointer */
331 STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException)
333 STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException)
334 STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException)
336 STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException)
337 STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException)
338 STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException)
339 STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, DebugException)
340 STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException)
341 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
342 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
343 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
344 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
345 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
346 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
347 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
348 STD_EXCEPTION(0x1c00, ReservedC, UnknownException)
349 STD_EXCEPTION(0x1d00, ReservedD, UnknownException)
350 STD_EXCEPTION(0x1e00, ReservedE, UnknownException)
351 STD_EXCEPTION(0x1f00, ReservedF, UnknownException)
353 STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException)
355 .globl _end_of_vectors
362 * This code finishes saving the registers to the exception frame
363 * and jumps to the appropriate handler for the exception.
364 * Register r21 is pointer into trap frame, r1 has new stack pointer.
366 .globl transfer_to_handler
378 mfspr r23,SPRG3 /* if from user, fix up tss.regs */
380 addi r24,r1,STACK_FRAME_OVERHEAD
382 2: addi r2,r23,-TSS /* set r2 to current */
386 andi. r24,r23,0x3f00 /* get vector offset */
390 mtspr SPRG2,r22 /* r1 is now kernel sp */
392 addi r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */
396 bgt stack_ovf /* if r2 < r1 < r2+TASK_STRUCT_SIZE */
398 lwz r24,0(r23) /* virtual address of handler */
399 lwz r23,4(r23) /* where to go when done */
401 ori r20,r20,0x30 /* enable IR, DR */
405 rfi /* jump to handler, enable MMU */
408 mfmsr r28 /* Disable interrupts */
412 SYNC /* Some chip revs need this... */
427 lwz r2,_NIP(r1) /* Restore environment */
441 mfspr r5,HID0 /* turn on the I cache. */
442 ori r5,r5,0x8800 /* Instruction cache only! */
445 and r6,r5,r6 /* clear the invalidate bit */
453 .globl icache_disable
468 srwi r3, r3, 15 /* >>15 & 1=> select bit 16 */
474 mfspr r5,HID0 /* turn on the D cache. */
475 ori r5,r5,0x4400 /* Data cache only! */
476 mfspr r4, PVR /* read PVR */
477 srawi r3, r4, 16 /* shift off the least 16 bits */
478 cmpi 0, 0, r3, 0xC /* Check for Max pvr */
480 ori r5,r5,0x0040 /* setting the DCFA bit, for Max rev 1 errata */
484 and r6,r5,r6 /* clear the invalidate bit */
492 .globl dcache_disable
507 srwi r3, r3, 14 /* >>14 & 1=> select bit 17 */
513 /*TODO : who uses this, what should it do?
524 /*------------------------------------------------------------------------------*/
527 * void relocate_code (addr_sp, gd, addr_moni)
529 * This "function" does not return, instead it continues in RAM
530 * after relocating the monitor code.
534 * r5 = length in bytes
540 mr r1, r3 /* Set new stack pointer */
541 mr r9, r4 /* Save copy of Global Data pointer */
542 mr r10, r5 /* Save copy of Destination Address */
544 mr r3, r5 /* Destination Address */
546 lis r4, CFG_SDRAM_BASE@h /* Source Address */
547 ori r4, r4, CFG_SDRAM_BASE@l
549 lis r4, CFG_MONITOR_BASE@h /* Source Address */
550 ori r4, r4, CFG_MONITOR_BASE@l
552 lwz r5, GOT(__init_end)
554 li r6, CFG_CACHELINE_SIZE /* Cache Line Size */
559 * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
565 /* First our own GOT */
567 /* the 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 */
610 sync /* Wait for all dcbst to complete on bus */
616 7: sync /* Wait for all icbi to complete on bus */
620 * We are done. Do not return, instead branch to second part of board
621 * initialization, now running from RAM.
624 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
631 * Relocation Function, r14 point to got2+0x8000
633 * Adjust got2 pointers, no need to check for 0, this code
634 * already puts a few entries in the table.
636 li r0,__got2_entries@sectoff@l
637 la r3,GOT(_GOT2_TABLE_)
638 lwz r11,GOT(_GOT2_TABLE_)
648 * Now adjust the fixups and the pointers to the fixups
649 * in case we need to move ourselves again.
651 2: li r0,__fixup_entries@sectoff@l
652 lwz r3,GOT(_FIXUP_TABLE_)
666 * Now clear BSS segment
668 lwz r3,GOT(__bss_start)
682 mr r3, r9 /* Global Data pointer */
683 mr r4, r10 /* Destination Address */
687 * Copy exception vector code to low memory
690 * r7: source address, r8: end address, r9: target address
695 lwz r8, GOT(_end_of_vectors)
697 li r9, 0x100 /* reset vector always at 0x100 */
700 bgelr /* return if r7>=r8 - just in case */
702 mflr r4 /* save link register */
712 * relocate `hdlr' and `int_return' entries
714 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
715 li r8, Alignment - _start + EXC_OFF_SYS_RESET
718 addi r7, r7, 0x100 /* next exception vector */
722 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
725 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
728 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
729 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
732 addi r7, r7, 0x100 /* next exception vector */
736 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
737 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
740 addi r7, r7, 0x100 /* next exception vector */
744 mtlr r4 /* restore link register */
748 * Function: relocate entries for one exception vector
751 lwz r0, 0(r7) /* hdlr ... */
752 add r0, r0, r3 /* ... += dest_addr */
755 lwz r0, 4(r7) /* int_return ... */
756 add r0, r0, r3 /* ... += dest_addr */
761 /* Setup the BAT registers.
765 ori r4, r4, CFG_IBAT0L@l
767 ori r3, r3, CFG_IBAT0U@l
773 ori r4, r4, CFG_DBAT0L@l
775 ori r3, r3, CFG_DBAT0U@l
781 ori r4, r4, CFG_IBAT1L@l
783 ori r3, r3, CFG_IBAT1U@l
789 ori r4, r4, CFG_DBAT1L@l
791 ori r3, r3, CFG_DBAT1U@l
797 ori r4, r4, CFG_IBAT2L@l
799 ori r3, r3, CFG_IBAT2U@l
805 ori r4, r4, CFG_DBAT2L@l
807 ori r3, r3, CFG_DBAT2U@l
813 ori r4, r4, CFG_IBAT3L@l
815 ori r3, r3, CFG_IBAT3U@l
821 ori r4, r4, CFG_DBAT3L@l
823 ori r3, r3, CFG_DBAT3U@l
829 * -> for (val = 0; val < 0x20000; val+=0x1000)