2 * Copyright 2004, 2007, 2011 Freescale Semiconductor.
3 * Srikanth Srinivasan <srikanth.srinivaan@freescale.com>
5 * SPDX-License-Identifier: GPL-2.0+
8 /* U-Boot - Startup Code for 86xx PowerPC based Embedded Boards
11 * The processor starts at 0xfff00100 and the code is executed
12 * from flash. The code is organized to be at an other address
13 * in memory, but as long we don't jump around before relocating.
14 * board_init lies at a quite high address and when the cpu has
15 * jumped there, everything is ok.
17 #include <asm-offsets.h>
22 #include <ppc_asm.tmpl>
25 #include <asm/cache.h>
27 #include <asm/u-boot.h>
30 * Need MSR_DR | MSR_IR enabled to access I/O (printf) in exceptions
34 * Set up GOT: Global Offset Table
36 * Use r12 to access the GOT
39 GOT_ENTRY(_GOT2_TABLE_)
40 GOT_ENTRY(_FIXUP_TABLE_)
43 GOT_ENTRY(_start_of_vectors)
44 GOT_ENTRY(_end_of_vectors)
45 GOT_ENTRY(transfer_to_handler)
49 GOT_ENTRY(__bss_start)
53 * r3 - 1st arg to board_init(): IMMP pointer
54 * r4 - 2nd arg to board_init(): boot flag
57 .long 0x27051956 /* U-Boot Magic Number */
60 .ascii U_BOOT_VERSION_STRING, "\0"
67 /* the boot code is located below the exception table */
69 .globl _start_of_vectors
73 STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
75 /* Data Storage exception. */
76 STD_EXCEPTION(0x300, DataStorage, UnknownException)
78 /* Instruction Storage exception. */
79 STD_EXCEPTION(0x400, InstStorage, UnknownException)
81 /* External Interrupt exception. */
82 STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
84 /* Alignment exception. */
87 EXCEPTION_PROLOG(SRR0, SRR1)
92 addi r3,r1,STACK_FRAME_OVERHEAD
93 EXC_XFER_TEMPLATE(Alignment, AlignmentException, MSR_KERNEL, COPY_EE)
95 /* Program check exception */
98 EXCEPTION_PROLOG(SRR0, SRR1)
99 addi r3,r1,STACK_FRAME_OVERHEAD
100 EXC_XFER_TEMPLATE(ProgramCheck, ProgramCheckException,
103 STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
105 /* I guess we could implement decrementer, and may have
106 * to someday for timekeeping.
108 STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
109 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
110 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
111 STD_EXCEPTION(0xc00, SystemCall, UnknownException)
112 STD_EXCEPTION(0xd00, SingleStep, UnknownException)
113 STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
114 STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
115 STD_EXCEPTION(0x1000, SoftEmu, SoftEmuException)
116 STD_EXCEPTION(0x1100, InstructionTLBMiss, UnknownException)
117 STD_EXCEPTION(0x1200, DataTLBMiss, UnknownException)
118 STD_EXCEPTION(0x1300, InstructionTLBError, UnknownException)
119 STD_EXCEPTION(0x1400, DataTLBError, UnknownException)
120 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
121 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
122 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
123 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
124 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
125 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
126 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
127 STD_EXCEPTION(0x1c00, DataBreakpoint, UnknownException)
128 STD_EXCEPTION(0x1d00, InstructionBreakpoint, UnknownException)
129 STD_EXCEPTION(0x1e00, PeripheralBreakpoint, UnknownException)
130 STD_EXCEPTION(0x1f00, DevPortBreakpoint, UnknownException)
132 .globl _end_of_vectors
139 * NOTE: Only Cpu 0 will ever come here. Other cores go to an
140 * address specified by the BPTR
143 #ifdef CONFIG_SYS_RAMBOOT
144 /* disable everything */
151 /* Invalidate BATs */
154 /* Invalidate all of TLB before MMU turn on */
159 /* init the L2 cache */
161 ori r3, r3, L2_INIT@l
163 /* invalidate the L2 cache */
164 bl l2cache_invalidate
169 * Calculate absolute address in FLASH and jump there
170 *------------------------------------------------------*/
171 lis r3, CONFIG_SYS_MONITOR_BASE_EARLY@h
172 ori r3, r3, CONFIG_SYS_MONITOR_BASE_EARLY@l
173 addi r3, r3, in_flash - _start + EXC_OFF_SYS_RESET
178 /* let the C-code set up the rest */
180 /* Be careful to keep code relocatable ! */
181 /*------------------------------------------------------*/
182 /* perform low-level init */
184 /* enable extended addressing */
191 * Cache must be enabled here for stack-in-cache trick.
192 * This means we need to enable the BATS.
193 * Cache should be turned on after BATs, since by default
194 * everything is write-through.
197 /* enable address translation */
199 ori r5, r5, (MSR_IR | MSR_DR)
200 lis r3,addr_trans_enabled@h
201 ori r3, r3, addr_trans_enabled@l
207 /* enable and invalidate the data cache */
208 /* bl l1dcache_enable */
216 #ifdef CONFIG_SYS_INIT_RAM_LOCK
221 #if (CONFIG_SYS_CCSRBAR_DEFAULT != CONFIG_SYS_CCSRBAR)
225 /* set up the stack pointer in our newly created
227 lis r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@h
228 ori r1, r1, (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET)@l
230 li r0, 0 /* Make room for stack frame header and */
231 stwu r0, -4(r1) /* clear final stack frame so that */
232 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
234 GET_GOT /* initialize GOT access */
236 /* run low-level CPU init code (from Flash) */
242 /* Load PX_AUX register address in r4 */
245 /* Load contents of PX_AUX in r3 bits 24 to 31*/
248 /* Mask and obtain the bit in r3 */
249 rlwinm. r3, r3, 0, 24, 24
250 /* If not zero, jump and continue with u-boot */
253 /* Load back contents of PX_AUX in r3 bits 24 to 31 */
255 /* Set the MSB of the register value */
257 /* Write value in r3 back to PX_AUX */
260 /* Get the address to jump to in r3*/
261 lis r3, CONFIG_SYS_DIAG_ADDR@h
262 ori r3, r3, CONFIG_SYS_DIAG_ADDR@l
264 /* Load the LR with the branch address */
267 /* Branch to diagnostic */
273 /* bl l2cache_enable */
275 /* run 1st part of board init code (from Flash) */
276 li r3, 0 /* clear boot_flag for calling board_init_f */
280 /* NOTREACHED - board_init_f() does not return */
282 .globl invalidate_bats
286 /* invalidate BATs */
310 #define CONFIG_BAT_PAIR(n) \
311 lis r4, CONFIG_SYS_IBAT##n##L@h; \
312 ori r4, r4, CONFIG_SYS_IBAT##n##L@l; \
313 lis r3, CONFIG_SYS_IBAT##n##U@h; \
314 ori r3, r3, CONFIG_SYS_IBAT##n##U@l; \
315 mtspr IBAT##n##L, r4; \
316 mtspr IBAT##n##U, r3; \
317 lis r4, CONFIG_SYS_DBAT##n##L@h; \
318 ori r4, r4, CONFIG_SYS_DBAT##n##L@l; \
319 lis r3, CONFIG_SYS_DBAT##n##U@h; \
320 ori r3, r3, CONFIG_SYS_DBAT##n##U@l; \
321 mtspr DBAT##n##L, r4; \
322 mtspr DBAT##n##U, r3;
327 * Set up the final BAT registers now that setup is done.
330 * 1) Address translation is enabled upon entry
331 * 2) The boot rom is still accessible via 1:1 translation
339 * When we disable address translation, we will get 1:1 (VA==PA)
340 * translation. The only place we know for sure is safe for that is
341 * the bootrom where we originally started out. Pop back into there.
343 lis r4, CONFIG_SYS_MONITOR_BASE_EARLY@h
344 ori r4, r4, CONFIG_SYS_MONITOR_BASE_EARLY@l
345 addi r4, r4, trans_disabled - _start + EXC_OFF_SYS_RESET
347 /* disable address translation */
349 rlwinm r3, r3, 0, 28, 25
355 #if defined(CONFIG_SYS_DBAT0U) && defined(CONFIG_SYS_DBAT0L) \
356 && defined(CONFIG_SYS_IBAT0U) && defined(CONFIG_SYS_IBAT0L)
370 /* Turn translation back on and return */
372 ori r3, r3, (MSR_IR | MSR_DR)
380 * Set up bats needed early on - this is usually the BAT for the
381 * stack-in-cache, the Flash, and CCSR space
386 lis r4, CONFIG_SYS_IBAT3L@h
387 ori r4, r4, CONFIG_SYS_IBAT3L@l
388 lis r3, CONFIG_SYS_IBAT3U@h
389 ori r3, r3, CONFIG_SYS_IBAT3U@l
395 lis r4, CONFIG_SYS_DBAT3L@h
396 ori r4, r4, CONFIG_SYS_DBAT3L@l
397 lis r3, CONFIG_SYS_DBAT3U@h
398 ori r3, r3, CONFIG_SYS_DBAT3U@l
404 lis r4, CONFIG_SYS_IBAT5L@h
405 ori r4, r4, CONFIG_SYS_IBAT5L@l
406 lis r3, CONFIG_SYS_IBAT5U@h
407 ori r3, r3, CONFIG_SYS_IBAT5U@l
413 lis r4, CONFIG_SYS_DBAT5L@h
414 ori r4, r4, CONFIG_SYS_DBAT5L@l
415 lis r3, CONFIG_SYS_DBAT5U@h
416 ori r3, r3, CONFIG_SYS_DBAT5U@l
422 lis r4, CONFIG_SYS_IBAT6L_EARLY@h
423 ori r4, r4, CONFIG_SYS_IBAT6L_EARLY@l
424 lis r3, CONFIG_SYS_IBAT6U_EARLY@h
425 ori r3, r3, CONFIG_SYS_IBAT6U_EARLY@l
431 lis r4, CONFIG_SYS_DBAT6L_EARLY@h
432 ori r4, r4, CONFIG_SYS_DBAT6L_EARLY@l
433 lis r3, CONFIG_SYS_DBAT6U_EARLY@h
434 ori r3, r3, CONFIG_SYS_DBAT6U_EARLY@l
439 #if(CONFIG_SYS_CCSRBAR_DEFAULT != CONFIG_SYS_CCSRBAR)
441 lis r4, CONFIG_SYS_CCSR_DEFAULT_IBATL@h
442 ori r4, r4, CONFIG_SYS_CCSR_DEFAULT_IBATL@l
443 lis r3, CONFIG_SYS_CCSR_DEFAULT_IBATU@h
444 ori r3, r3, CONFIG_SYS_CCSR_DEFAULT_IBATU@l
450 lis r4, CONFIG_SYS_CCSR_DEFAULT_DBATL@h
451 ori r4, r4, CONFIG_SYS_CCSR_DEFAULT_DBATL@l
452 lis r3, CONFIG_SYS_CCSR_DEFAULT_DBATU@h
453 ori r3, r3, CONFIG_SYS_CCSR_DEFAULT_DBATU@l
473 .globl disable_addr_trans
475 /* disable address translation */
478 andi. r0, r3, (MSR_IR | MSR_DR)
486 * This code finishes saving the registers to the exception frame
487 * and jumps to the appropriate handler for the exception.
488 * Register r21 is pointer into trap frame, r1 has new stack pointer.
490 .globl transfer_to_handler
501 andi. r24,r23,0x3f00 /* get vector offset */
505 mtspr SPRG2,r22 /* r1 is now kernel sp */
506 lwz r24,0(r23) /* virtual address of handler */
507 lwz r23,4(r23) /* where to go when done */
512 rfi /* jump to handler, enable MMU */
515 mfmsr r28 /* Disable interrupts */
519 SYNC /* Some chip revs need this... */
534 lwz r2,_NIP(r1) /* Restore environment */
551 * Description: Input 8 bits
560 * Description: Output 8 bits
569 * Description: Output 16 bits
578 * Description: Byte reverse and output 16 bits
587 * Description: Output 32 bits
596 * Description: Byte reverse and output 32 bits
605 * Description: Input 16 bits
614 * Description: Input 16 bits and byte reverse
623 * Description: Input 32 bits
632 * Description: Input 32 bits and byte reverse
640 * void relocate_code (addr_sp, gd, addr_moni)
642 * This "function" does not return, instead it continues in RAM
643 * after relocating the monitor code.
647 * r5 = length in bytes
653 mr r1, r3 /* Set new stack pointer */
654 mr r9, r4 /* Save copy of Global Data pointer */
655 mr r10, r5 /* Save copy of Destination Address */
658 mr r3, r5 /* Destination Address */
659 lis r4, CONFIG_SYS_MONITOR_BASE@h /* Source Address */
660 ori r4, r4, CONFIG_SYS_MONITOR_BASE@l
661 lwz r5, GOT(__init_end)
663 li r6, CONFIG_SYS_CACHELINE_SIZE /* Cache Line Size */
668 * New GOT-PTR = (old GOT-PTR - CONFIG_SYS_MONITOR_BASE) + Destination Address
674 /* First our own GOT */
676 /* then the one used by the C code */
685 beq cr1,4f /* In place copy is not necessary */
686 beq 7f /* Protect against 0 count */
704 * Now flush the cache: note that we must start from a cache aligned
705 * address. Otherwise we might miss one cache line.
709 beq 7f /* Always flush prefetch queue in any case */
717 sync /* Wait for all dcbst to complete on bus */
723 7: sync /* Wait for all icbi to complete on bus */
727 * We are done. Do not return, instead branch to second part of board
728 * initialization, now running from RAM.
730 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
736 * Relocation Function, r12 point to got2+0x8000
738 * Adjust got2 pointers, no need to check for 0, this code
739 * already puts a few entries in the table.
741 li r0,__got2_entries@sectoff@l
742 la r3,GOT(_GOT2_TABLE_)
743 lwz r11,GOT(_GOT2_TABLE_)
755 * Now adjust the fixups and the pointers to the fixups
756 * in case we need to move ourselves again.
758 li r0,__fixup_entries@sectoff@l
759 lwz r3,GOT(_FIXUP_TABLE_)
775 * Now clear BSS segment
777 lwz r3,GOT(__bss_start)
778 lwz r4,GOT(__bss_end)
790 mr r3, r9 /* Init Date pointer */
791 mr r4, r10 /* Destination Address */
794 /* not reached - end relocate_code */
795 /*-----------------------------------------------------------------------*/
798 * Copy exception vector code to low memory
801 * r7: source address, r8: end address, r9: target address
805 mflr r4 /* save link register */
808 lwz r8, GOT(_end_of_vectors)
810 li r9, 0x100 /* reset vector always at 0x100 */
813 bgelr /* return if r7>=r8 - just in case */
823 * relocate `hdlr' and `int_return' entries
825 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
826 li r8, Alignment - _start + EXC_OFF_SYS_RESET
829 addi r7, r7, 0x100 /* next exception vector */
833 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
836 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
839 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
840 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
843 addi r7, r7, 0x100 /* next exception vector */
847 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
848 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
851 addi r7, r7, 0x100 /* next exception vector */
855 /* enable execptions from RAM vectors */
859 ori r7,r7,MSR_ME /* Enable Machine Check */
862 mtlr r4 /* restore link register */
865 .globl enable_ext_addr
868 lis r0, (HID0_HIGH_BAT_EN | HID0_XBSEN | HID0_XAEN)@h
869 ori r0, r0, (HID0_HIGH_BAT_EN | HID0_XBSEN | HID0_XAEN)@l
875 #if (CONFIG_SYS_CCSRBAR_DEFAULT != CONFIG_SYS_CCSRBAR)
878 /* Special sequence needed to update CCSRBAR itself */
879 lis r4, CONFIG_SYS_CCSRBAR_DEFAULT@h
880 ori r4, r4, CONFIG_SYS_CCSRBAR_DEFAULT@l
882 lis r5, CONFIG_SYS_CCSRBAR_PHYS_LOW@h
883 ori r5, r5, CONFIG_SYS_CCSRBAR_PHYS_LOW@l
885 li r6, CONFIG_SYS_CCSRBAR_PHYS_HIGH@l
887 stw r5, 0(r4) /* Store physical value of CCSR */
890 lis r5, CONFIG_SYS_TEXT_BASE@h
891 ori r5,r5,CONFIG_SYS_TEXT_BASE@l
895 /* Use VA of CCSR to do read */
896 lis r3, CONFIG_SYS_CCSRBAR@h
897 lwz r5, CONFIG_SYS_CCSRBAR@l(r3)
903 #ifdef CONFIG_SYS_INIT_RAM_LOCK
905 /* Allocate Initial RAM in data cache.
907 lis r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@h
908 ori r3, r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@l
909 li r4, ((CONFIG_SYS_INIT_RAM_SIZE & ~31) + \
910 (CONFIG_SYS_INIT_RAM_ADDR & 31) + 31) / 32
917 /* Lock the data cache */
926 /* Lock the first way of the data cache */
929 #if defined(CONFIG_ALTIVEC)
939 .globl unlock_ram_in_cache
941 /* invalidate the INIT_RAM section */
942 lis r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@h
943 ori r3, r3, (CONFIG_SYS_INIT_RAM_ADDR & ~31)@l
944 li r4, ((CONFIG_SYS_INIT_RAM_SIZE & ~31) + \
945 (CONFIG_SYS_INIT_RAM_ADDR & 31) + 31) / 32
950 sync /* Wait for all icbi to complete on bus */
953 /* Unlock the data cache and invalidate it */
965 /* Unlock the first way of the data cache */
969 #ifdef CONFIG_ALTIVEC