3 * Copyright (C) 1999 Magnus Damm <kieraypc01.p.y.kie.era.ericsson.se>
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)
262 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
264 STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException)
266 STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException)
267 STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException)
269 STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException)
270 STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException)
271 STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException)
272 STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, DebugException)
273 STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException)
274 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
275 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
276 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
277 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
278 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
279 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
280 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
281 STD_EXCEPTION(0x1c00, ReservedC, UnknownException)
282 STD_EXCEPTION(0x1d00, ReservedD, UnknownException)
283 STD_EXCEPTION(0x1e00, ReservedE, UnknownException)
284 STD_EXCEPTION(0x1f00, ReservedF, UnknownException)
286 STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException)
288 .globl _end_of_vectors
295 * This code finishes saving the registers to the exception frame
296 * and jumps to the appropriate handler for the exception.
297 * Register r21 is pointer into trap frame, r1 has new stack pointer.
299 .globl transfer_to_handler
311 mfspr r23,SPRG3 /* if from user, fix up tss.regs */
313 addi r24,r1,STACK_FRAME_OVERHEAD
315 2: addi r2,r23,-TSS /* set r2 to current */
319 andi. r24,r23,0x3f00 /* get vector offset */
323 mtspr SPRG2,r22 /* r1 is now kernel sp */
325 addi r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */
329 bgt stack_ovf /* if r2 < r1 < r2+TASK_STRUCT_SIZE */
331 lwz r24,0(r23) /* virtual address of handler */
332 lwz r23,4(r23) /* where to go when done */
334 ori r20,r20,0x30 /* enable IR, DR */
338 rfi /* jump to handler, enable MMU */
341 mfmsr r28 /* Disable interrupts */
345 SYNC /* Some chip revs need this... */
360 lwz r2,_NIP(r1) /* Restore environment */
374 mfspr r5,HID0 /* turn on the I cache. */
375 ori r5,r5,0x8800 /* Instruction cache only! */
378 and r6,r5,r6 /* clear the invalidate bit */
386 .globl icache_disable
401 srwi r3, r3, 15 /* >>15 & 1=> select bit 16 */
407 mfspr r5,HID0 /* turn on the D cache. */
408 ori r5,r5,0x4400 /* Data cache only! */
409 mfspr r4, PVR /* read PVR */
410 srawi r3, r4, 16 /* shift off the least 16 bits */
411 cmpi 0, 0, r3, 0xC /* Check for Max pvr */
413 ori r5,r5,0x0040 /* setting the DCFA bit, for Max rev 1 errata */
417 and r6,r5,r6 /* clear the invalidate bit */
425 .globl dcache_disable
440 srwi r3, r3, 14 /* >>14 & 1=> select bit 17 */
446 /*TODO : who uses this, what should it do?
457 /*------------------------------------------------------------------------------*/
460 * void relocate_code (addr_sp, gd, addr_moni)
462 * This "function" does not return, instead it continues in RAM
463 * after relocating the monitor code.
467 * r5 = length in bytes
473 mr r1, r3 /* Set new stack pointer */
474 mr r9, r4 /* Save copy of Global Data pointer */
475 mr r10, r5 /* Save copy of Destination Address */
477 mr r3, r5 /* Destination Address */
479 lis r4, CFG_SDRAM_BASE@h /* Source Address */
480 ori r4, r4, CFG_SDRAM_BASE@l
482 lis r4, CFG_MONITOR_BASE@h /* Source Address */
483 ori r4, r4, CFG_MONITOR_BASE@l
485 lwz r5, GOT(__init_end)
487 li r6, CFG_CACHELINE_SIZE /* Cache Line Size */
492 * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
498 /* First our own GOT */
500 /* the the one used by the C code */
510 beq cr1,4f /* In place copy is not necessary */
511 beq 7f /* Protect against 0 count */
530 #if !defined(CONFIG_BMW)
531 /* Unlock the data cache and invalidate locked area */
534 lis r4, CFG_INIT_RAM_ADDR@h
535 ori r4, r4, CFG_INIT_RAM_ADDR@l
545 * Now flush the cache: note that we must start from a cache aligned
546 * address. Otherwise we might miss one cache line.
550 beq 7f /* Always flush prefetch queue in any case */
558 sync /* Wait for all dcbst to complete on bus */
564 7: sync /* Wait for all icbi to complete on bus */
568 * We are done. Do not return, instead branch to second part of board
569 * initialization, now running from RAM.
572 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
579 * Relocation Function, r14 point to got2+0x8000
581 * Adjust got2 pointers, no need to check for 0, this code
582 * already puts a few entries in the table.
584 li r0,__got2_entries@sectoff@l
585 la r3,GOT(_GOT2_TABLE_)
586 lwz r11,GOT(_GOT2_TABLE_)
596 * Now adjust the fixups and the pointers to the fixups
597 * in case we need to move ourselves again.
599 2: li r0,__fixup_entries@sectoff@l
600 lwz r3,GOT(_FIXUP_TABLE_)
614 * Now clear BSS segment
616 lwz r3,GOT(__bss_start)
630 mr r3, r9 /* Global Data pointer */
631 mr r4, r10 /* Destination Address */
635 * Copy exception vector code to low memory
638 * r7: source address, r8: end address, r9: target address
643 lwz r8, GOT(_end_of_vectors)
645 li r9, 0x100 /* reset vector always at 0x100 */
648 bgelr /* return if r7>=r8 - just in case */
650 mflr r4 /* save link register */
660 * relocate `hdlr' and `int_return' entries
662 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
663 li r8, Alignment - _start + EXC_OFF_SYS_RESET
666 addi r7, r7, 0x100 /* next exception vector */
670 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
673 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
676 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
677 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
680 addi r7, r7, 0x100 /* next exception vector */
684 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
685 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
688 addi r7, r7, 0x100 /* next exception vector */
692 mtlr r4 /* restore link register */
696 * Function: relocate entries for one exception vector
699 lwz r0, 0(r7) /* hdlr ... */
700 add r0, r0, r3 /* ... += dest_addr */
703 lwz r0, 4(r7) /* int_return ... */
704 add r0, r0, r3 /* ... += dest_addr */
709 /* Setup the BAT registers.
713 ori r4, r4, CFG_IBAT0L@l
715 ori r3, r3, CFG_IBAT0U@l
721 ori r4, r4, CFG_DBAT0L@l
723 ori r3, r3, CFG_DBAT0U@l
729 ori r4, r4, CFG_IBAT1L@l
731 ori r3, r3, CFG_IBAT1U@l
737 ori r4, r4, CFG_DBAT1L@l
739 ori r3, r3, CFG_DBAT1U@l
745 ori r4, r4, CFG_IBAT2L@l
747 ori r3, r3, CFG_IBAT2U@l
753 ori r4, r4, CFG_DBAT2L@l
755 ori r3, r3, CFG_DBAT2U@l
761 ori r4, r4, CFG_IBAT3L@l
763 ori r3, r3, CFG_IBAT3U@l
769 ori r4, r4, CFG_DBAT3L@l
771 ori r3, r3, CFG_DBAT3U@l
777 * -> for (val = 0; val < 0x20000; val+=0x1000)