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
40 #include <asm-offsets.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 /* We don't want the MMU yet.
57 /* FP, Machine Check and Recoverable Interr. */
58 #define MSR_KERNEL ( MSR_FP | MSR_ME | MSR_RI )
61 * Set up GOT: Global Offset Table
63 * Use r12 to access the GOT
66 GOT_ENTRY(_GOT2_TABLE_)
67 GOT_ENTRY(_FIXUP_TABLE_)
70 GOT_ENTRY(_start_of_vectors)
71 GOT_ENTRY(_end_of_vectors)
72 GOT_ENTRY(transfer_to_handler)
75 GOT_ENTRY(__bss_end__)
76 GOT_ENTRY(__bss_start)
77 #if defined(CONFIG_FADS)
78 GOT_ENTRY(environment)
83 * r3 - 1st arg to board_init(): IMMP pointer
84 * r4 - 2nd arg to board_init(): boot flag
87 .long 0x27051956 /* U-Boot Magic Number */
90 .ascii U_BOOT_VERSION_STRING, "\0"
95 /* Initialize machine status; enable machine check interrupt */
96 /*----------------------------------------------------------------------*/
97 li r3, MSR_KERNEL /* Set FP, ME, RI flags */
99 mtspr SRR1, r3 /* Make SRR1 match MSR */
101 addis r0,0,0x0000 /* lets make sure that r0 is really 0 */
102 mtspr HID0, r0 /* disable I and D caches */
104 mfspr r3, ICR /* clear Interrupt Cause Register */
106 mfmsr r3 /* turn off address translation */
112 sync /* the MMU should be off... */
117 * Setup BATs - cannot be done in C since we don't have a stack yet
124 ori r3, r3, (MSR_IR | MSR_DR)
127 /* Enable and invalidate data cache.
131 ori r3, r3, HID0_DCE | HID0_DCI
138 /* Allocate Initial RAM in data cache.
140 lis r3, CONFIG_SYS_INIT_RAM_ADDR@h
141 ori r3, r3, CONFIG_SYS_INIT_RAM_ADDR@l
149 /* Lock way0 in data cache.
160 * Thisk the stack pointer *somewhere* sensible. Doesnt
161 * matter much where as we'll move it when we relocate
163 lis r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@h
164 ori r1, r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@l
166 li r0, 0 /* Make room for stack frame header and */
167 stwu r0, -4(r1) /* clear final stack frame so that */
168 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
170 /* let the C-code set up the rest */
172 /* Be careful to keep code relocatable ! */
173 /*----------------------------------------------------------------------*/
175 GET_GOT /* initialize GOT access */
178 bl cpu_init_f /* run low-level CPU init code (from Flash) */
180 bl board_init_f /* run 1st part of board init code (from Flash) */
182 /* NOTREACHED - board_init_f() does not return */
185 .globl _start_of_vectors
189 STD_EXCEPTION(EXC_OFF_MACH_CHCK, MachineCheck, MachineCheckException)
191 /* Data Storage exception. "Never" generated on the 860. */
192 STD_EXCEPTION(EXC_OFF_DATA_STOR, DataStorage, UnknownException)
194 /* Instruction Storage exception. "Never" generated on the 860. */
195 STD_EXCEPTION(EXC_OFF_INS_STOR, InstStorage, UnknownException)
197 /* External Interrupt exception. */
198 STD_EXCEPTION(EXC_OFF_EXTERNAL, ExtInterrupt, external_interrupt)
200 /* Alignment exception. */
203 EXCEPTION_PROLOG(SRR0, SRR1)
208 addi r3,r1,STACK_FRAME_OVERHEAD
209 EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
211 /* Program check exception */
214 EXCEPTION_PROLOG(SRR0, SRR1)
215 addi r3,r1,STACK_FRAME_OVERHEAD
216 EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
219 /* No FPU on MPC8xx. This exception is not supposed to happen.
221 STD_EXCEPTION(EXC_OFF_FPUNAVAIL, FPUnavailable, UnknownException)
223 /* I guess we could implement decrementer, and may have
224 * to someday for timekeeping.
226 STD_EXCEPTION(EXC_OFF_DECR, Decrementer, timer_interrupt)
227 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
228 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
229 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
231 STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException)
233 STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException)
234 STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException)
236 STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException)
237 STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException)
238 STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException)
239 STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, DebugException)
240 STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException)
241 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
242 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
243 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
244 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
245 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
246 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
247 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
248 STD_EXCEPTION(0x1c00, ReservedC, UnknownException)
249 STD_EXCEPTION(0x1d00, ReservedD, UnknownException)
250 STD_EXCEPTION(0x1e00, ReservedE, UnknownException)
251 STD_EXCEPTION(0x1f00, ReservedF, UnknownException)
253 STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException)
255 .globl _end_of_vectors
262 * This code finishes saving the registers to the exception frame
263 * and jumps to the appropriate handler for the exception.
264 * Register r21 is pointer into trap frame, r1 has new stack pointer.
266 .globl transfer_to_handler
278 mfspr r23,SPRG3 /* if from user, fix up tss.regs */
280 addi r24,r1,STACK_FRAME_OVERHEAD
282 2: addi r2,r23,-TSS /* set r2 to current */
286 andi. r24,r23,0x3f00 /* get vector offset */
290 mtspr SPRG2,r22 /* r1 is now kernel sp */
292 addi r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */
296 bgt stack_ovf /* if r2 < r1 < r2+TASK_STRUCT_SIZE */
298 lwz r24,0(r23) /* virtual address of handler */
299 lwz r23,4(r23) /* where to go when done */
301 ori r20,r20,0x30 /* enable IR, DR */
305 rfi /* jump to handler, enable MMU */
308 mfmsr r28 /* Disable interrupts */
312 SYNC /* Some chip revs need this... */
327 lwz r2,_NIP(r1) /* Restore environment */
341 mfspr r5,HID0 /* turn on the I cache. */
342 ori r5,r5,0x8800 /* Instruction cache only! */
345 and r6,r5,r6 /* clear the invalidate bit */
353 .globl icache_disable
368 srwi r3, r3, 15 /* >>15 & 1=> select bit 16 */
374 mfspr r5,HID0 /* turn on the D cache. */
375 ori r5,r5,0x4400 /* Data cache only! */
376 mfspr r4, PVR /* read PVR */
377 srawi r3, r4, 16 /* shift off the least 16 bits */
378 cmpi 0, 0, r3, 0xC /* Check for Max pvr */
380 ori r5,r5,0x0040 /* setting the DCFA bit, for Max rev 1 errata */
384 and r6,r5,r6 /* clear the invalidate bit */
392 .globl dcache_disable
407 srwi r3, r3, 14 /* >>14 & 1=> select bit 17 */
413 /*TODO : who uses this, what should it do?
424 /*------------------------------------------------------------------------------*/
427 * void relocate_code (addr_sp, gd, addr_moni)
429 * This "function" does not return, instead it continues in RAM
430 * after relocating the monitor code.
434 * r5 = length in bytes
440 mr r1, r3 /* Set new stack pointer */
441 mr r9, r4 /* Save copy of Global Data pointer */
442 mr r10, r5 /* Save copy of Destination Address */
445 mr r3, r5 /* Destination Address */
446 #ifdef CONFIG_SYS_RAMBOOT
447 lis r4, CONFIG_SYS_SDRAM_BASE@h /* Source Address */
448 ori r4, r4, CONFIG_SYS_SDRAM_BASE@l
450 lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
451 ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
453 lwz r5, GOT(__init_end)
455 li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
460 * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
466 /* First our own GOT */
468 /* the the one used by the C code */
478 beq cr1,4f /* In place copy is not necessary */
479 beq 7f /* Protect against 0 count */
498 /* Unlock the data cache and invalidate locked area */
501 lis r4, CONFIG_SYS_INIT_RAM_ADDR@h
502 ori r4, r4, CONFIG_SYS_INIT_RAM_ADDR@l
511 * Now flush the cache: note that we must start from a cache aligned
512 * address. Otherwise we might miss one cache line.
516 beq 7f /* Always flush prefetch queue in any case */
524 sync /* Wait for all dcbst to complete on bus */
530 7: sync /* Wait for all icbi to complete on bus */
534 * We are done. Do not return, instead branch to second part of board
535 * initialization, now running from RAM.
538 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
545 * Relocation Function, r12 point to got2+0x8000
547 * Adjust got2 pointers, no need to check for 0, this code
548 * already puts a few entries in the table.
550 li r0,__got2_entries@sectoff@l
551 la r3,GOT(_GOT2_TABLE_)
552 lwz r11,GOT(_GOT2_TABLE_)
564 * Now adjust the fixups and the pointers to the fixups
565 * in case we need to move ourselves again.
567 li r0,__fixup_entries@sectoff@l
568 lwz r3,GOT(_FIXUP_TABLE_)
584 * Now clear BSS segment
586 lwz r3,GOT(__bss_start)
587 lwz r4,GOT(__bss_end__)
600 mr r3, r9 /* Global Data pointer */
601 mr r4, r10 /* Destination Address */
605 * Copy exception vector code to low memory
608 * r7: source address, r8: end address, r9: target address
612 mflr r4 /* save link register */
615 lwz r8, GOT(_end_of_vectors)
617 li r9, 0x100 /* reset vector always at 0x100 */
620 bgelr /* return if r7>=r8 - just in case */
630 * relocate `hdlr' and `int_return' entries
632 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
633 li r8, Alignment - _start + EXC_OFF_SYS_RESET
636 addi r7, r7, 0x100 /* next exception vector */
640 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
643 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
646 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
647 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
650 addi r7, r7, 0x100 /* next exception vector */
654 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
655 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
658 addi r7, r7, 0x100 /* next exception vector */
662 mtlr r4 /* restore link register */
665 /* Setup the BAT registers.
668 lis r4, CONFIG_SYS_IBAT0L@h
669 ori r4, r4, CONFIG_SYS_IBAT0L@l
670 lis r3, CONFIG_SYS_IBAT0U@h
671 ori r3, r3, CONFIG_SYS_IBAT0U@l
676 lis r4, CONFIG_SYS_DBAT0L@h
677 ori r4, r4, CONFIG_SYS_DBAT0L@l
678 lis r3, CONFIG_SYS_DBAT0U@h
679 ori r3, r3, CONFIG_SYS_DBAT0U@l
684 lis r4, CONFIG_SYS_IBAT1L@h
685 ori r4, r4, CONFIG_SYS_IBAT1L@l
686 lis r3, CONFIG_SYS_IBAT1U@h
687 ori r3, r3, CONFIG_SYS_IBAT1U@l
692 lis r4, CONFIG_SYS_DBAT1L@h
693 ori r4, r4, CONFIG_SYS_DBAT1L@l
694 lis r3, CONFIG_SYS_DBAT1U@h
695 ori r3, r3, CONFIG_SYS_DBAT1U@l
700 lis r4, CONFIG_SYS_IBAT2L@h
701 ori r4, r4, CONFIG_SYS_IBAT2L@l
702 lis r3, CONFIG_SYS_IBAT2U@h
703 ori r3, r3, CONFIG_SYS_IBAT2U@l
708 lis r4, CONFIG_SYS_DBAT2L@h
709 ori r4, r4, CONFIG_SYS_DBAT2L@l
710 lis r3, CONFIG_SYS_DBAT2U@h
711 ori r3, r3, CONFIG_SYS_DBAT2U@l
716 lis r4, CONFIG_SYS_IBAT3L@h
717 ori r4, r4, CONFIG_SYS_IBAT3L@l
718 lis r3, CONFIG_SYS_IBAT3U@h
719 ori r3, r3, CONFIG_SYS_IBAT3U@l
724 lis r4, CONFIG_SYS_DBAT3L@h
725 ori r4, r4, CONFIG_SYS_DBAT3L@l
726 lis r3, CONFIG_SYS_DBAT3U@h
727 ori r3, r3, CONFIG_SYS_DBAT3U@l
733 * -> for (val = 0; val < 0x20000; val+=0x1000)