TEXT_BASE is in board/sandpoint/config.mk so say so...
[u-boot.git] / cpu / mpc824x / start.S
1 /*
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>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
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.
13  *
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.
18  *
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,
22  * MA 02111-1307 USA
23  */
24
25 /* U-Boot - Startup Code for PowerPC based Embedded Boards
26  *
27  *
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.
36  *
37  * board_init will change CS0 to be positioned at the correct
38  * address and (s)dram will be positioned at address 0
39  */
40 #include <config.h>
41 #include <mpc824x.h>
42 #include <version.h>
43
44 #define _LINUX_CONFIG_H 1       /* avoid reading Linux autoconf.h file  */
45
46 #include <ppc_asm.tmpl>
47 #include <ppc_defs.h>
48
49 #include <asm/cache.h>
50 #include <asm/mmu.h>
51
52 #ifndef CONFIG_IDENT_STRING
53 #define CONFIG_IDENT_STRING ""
54 #endif
55
56 /* We don't want the MMU yet.
57 */
58 #undef  MSR_KERNEL
59 /* FP, Machine Check and Recoverable Interr. */
60 #define MSR_KERNEL ( MSR_FP | MSR_ME | MSR_RI )
61
62 /*
63  * Set up GOT: Global Offset Table
64  *
65  * Use r14 to access the GOT
66  */
67         START_GOT
68         GOT_ENTRY(_GOT2_TABLE_)
69         GOT_ENTRY(_FIXUP_TABLE_)
70
71         GOT_ENTRY(_start)
72         GOT_ENTRY(_start_of_vectors)
73         GOT_ENTRY(_end_of_vectors)
74         GOT_ENTRY(transfer_to_handler)
75
76         GOT_ENTRY(__init_end)
77         GOT_ENTRY(_end)
78         GOT_ENTRY(__bss_start)
79 #if defined(CONFIG_FADS)
80         GOT_ENTRY(environment)
81 #endif
82         END_GOT
83
84 /*
85  * r3 - 1st arg to board_init(): IMMP pointer
86  * r4 - 2nd arg to board_init(): boot flag
87  */
88         .text
89         .long   0x27051956              /* U-Boot Magic Number                  */
90         .globl  version_string
91 version_string:
92         .ascii U_BOOT_VERSION
93         .ascii " (", __DATE__, " - ", __TIME__, ")"
94         .ascii CONFIG_IDENT_STRING, "\0"
95
96         . = EXC_OFF_SYS_RESET
97         .globl  _start
98 _start:
99         li      r21, BOOTFLAG_COLD      /* Normal Power-On: Boot from FLASH     */
100         b       boot_cold
101
102         . = EXC_OFF_SYS_RESET + 0x10
103
104         .globl  _start_warm
105 _start_warm:
106         li      r21, BOOTFLAG_WARM      /* Software reboot                      */
107         b       boot_warm
108
109 boot_cold:
110 boot_warm:
111
112         /* Initialize machine status; enable machine check interrupt            */
113         /*----------------------------------------------------------------------*/
114         li      r3, MSR_KERNEL          /* Set FP, ME, RI flags */
115         mtmsr   r3
116         mtspr   SRR1, r3                /* Make SRR1 match MSR */
117
118         addis   r0,0,0x0000             /* lets make sure that r0 is really 0 */
119         mtspr   HID0, r0                /* disable I and D caches */
120
121         mfspr   r3, ICR                 /* clear Interrupt Cause Register */
122
123         mfmsr   r3                      /* turn off address translation */
124         addis   r4,0,0xffff
125         ori     r4,r4,0xffcf
126         and     r3,r3,r4
127         mtmsr   r3
128         isync
129         sync                            /* the MMU should be off... */
130
131
132 in_flash:
133 #if defined(CONFIG_BMW) || defined(CONFIG_LINKSTATION)
134         bl early_init_f /* Must be ASM: no stack yet! */
135 #endif
136         /*
137          * Setup BATs - cannot be done in C since we don't have a stack yet
138          */
139         bl      setup_bats
140
141         /* Enable MMU.
142          */
143         mfmsr   r3
144         ori     r3, r3, (MSR_IR | MSR_DR)
145         mtmsr   r3
146 #if !defined(CONFIG_BMW)
147         /* Enable and invalidate data cache.
148          */
149         mfspr   r3, HID0
150         mr      r2, r3
151         ori     r3, r3, HID0_DCE | HID0_DCI
152         ori     r2, r2, HID0_DCE
153         sync
154         mtspr   HID0, r3
155         mtspr   HID0, r2
156         sync
157
158 #if !defined(CONFIG_LINKSTATION)
159         /* Allocate Initial RAM in data cache.
160          */
161         lis     r3, CFG_INIT_RAM_ADDR@h
162         ori     r3, r3, CFG_INIT_RAM_ADDR@l
163         li      r2, 128
164         mtctr   r2
165 1:
166         dcbz    r0, r3
167         addi    r3, r3, 32
168         bdnz    1b
169
170         /* Lock way0 in data cache.
171          */
172         mfspr   r3, 1011
173         lis     r2, 0xffff
174         ori     r2, r2, 0xff1f
175         and     r3, r3, r2
176         ori     r3, r3, 0x0080
177         sync
178         mtspr   1011, r3
179 #endif /* !CONFIG_LINKSTATION */
180 #endif /* !CONFIG_BMW */
181         /*
182          * Thisk the stack pointer *somewhere* sensible. Doesnt
183          * matter much where as we'll move it when we relocate
184          */
185         lis     r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@h
186         ori     r1, r1, (CFG_INIT_RAM_ADDR + CFG_GBL_DATA_OFFSET)@l
187
188         li      r0, 0                   /* Make room for stack frame header and */
189         stwu    r0, -4(r1)              /* clear final stack frame so that      */
190         stwu    r0, -4(r1)              /* stack backtraces terminate cleanly   */
191
192         /* let the C-code set up the rest                                       */
193         /*                                                                      */
194         /* Be careful to keep code relocatable !                                */
195         /*----------------------------------------------------------------------*/
196
197         GET_GOT                 /* initialize GOT access                        */
198
199         /* r3: IMMR */
200 #if !defined(CONFIG_LINKSTATION)
201         bl      cpu_init_f      /* run low-level CPU init code     (from Flash) */
202 #endif
203
204         mr      r3, r21
205         /* r3: BOOTFLAG */
206         bl      board_init_f    /* run 1st part of board init code (from Flash) */
207
208
209         .globl  _start_of_vectors
210 _start_of_vectors:
211
212 /* Machine check */
213         STD_EXCEPTION(EXC_OFF_MACH_CHCK, MachineCheck, MachineCheckException)
214
215 /* Data Storage exception.  "Never" generated on the 860. */
216         STD_EXCEPTION(EXC_OFF_DATA_STOR, DataStorage, UnknownException)
217
218 /* Instruction Storage exception.  "Never" generated on the 860. */
219         STD_EXCEPTION(EXC_OFF_INS_STOR, InstStorage, UnknownException)
220
221 /* External Interrupt exception. */
222         STD_EXCEPTION(EXC_OFF_EXTERNAL, ExtInterrupt, external_interrupt)
223
224 /* Alignment exception. */
225         . = EXC_OFF_ALIGN
226 Alignment:
227         EXCEPTION_PROLOG
228         mfspr   r4,DAR
229         stw     r4,_DAR(r21)
230         mfspr   r5,DSISR
231         stw     r5,_DSISR(r21)
232         addi    r3,r1,STACK_FRAME_OVERHEAD
233         li      r20,MSR_KERNEL
234         rlwimi  r20,r23,0,16,16         /* copy EE bit from saved MSR */
235         lwz     r6,GOT(transfer_to_handler)
236         mtlr    r6
237         blrl
238 .L_Alignment:
239         .long   AlignmentException - _start + EXC_OFF_SYS_RESET
240         .long   int_return - _start + EXC_OFF_SYS_RESET
241
242 /* Program check exception */
243         . = EXC_OFF_PROGRAM
244 ProgramCheck:
245         EXCEPTION_PROLOG
246         addi    r3,r1,STACK_FRAME_OVERHEAD
247         li      r20,MSR_KERNEL
248         rlwimi  r20,r23,0,16,16         /* copy EE bit from saved MSR */
249         lwz     r6,GOT(transfer_to_handler)
250         mtlr    r6
251         blrl
252 .L_ProgramCheck:
253         .long   ProgramCheckException - _start + EXC_OFF_SYS_RESET
254         .long   int_return - _start + EXC_OFF_SYS_RESET
255
256         /* No FPU on MPC8xx. This exception is not supposed to happen.
257         */
258         STD_EXCEPTION(EXC_OFF_FPUNAVAIL, FPUnavailable, UnknownException)
259
260         /* I guess we could implement decrementer, and may have
261          * to someday for timekeeping.
262          */
263         STD_EXCEPTION(EXC_OFF_DECR, Decrementer, timer_interrupt)
264         STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
265         STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
266         STD_EXCEPTION(0xc00, SystemCall, UnknownException)
267
268         STD_EXCEPTION(EXC_OFF_TRACE, SingleStep, UnknownException)
269
270         STD_EXCEPTION(EXC_OFF_FPUNASSIST, Trap_0e, UnknownException)
271         STD_EXCEPTION(EXC_OFF_PMI, Trap_0f, UnknownException)
272
273         STD_EXCEPTION(EXC_OFF_ITME, InstructionTransMiss, UnknownException)
274         STD_EXCEPTION(EXC_OFF_DLTME, DataLoadTransMiss, UnknownException)
275         STD_EXCEPTION(EXC_OFF_DSTME, DataStoreTransMiss, UnknownException)
276         STD_EXCEPTION(EXC_OFF_IABE, InstructionBreakpoint, DebugException)
277         STD_EXCEPTION(EXC_OFF_SMIE, SysManageInt, UnknownException)
278         STD_EXCEPTION(0x1500, Reserved5, UnknownException)
279         STD_EXCEPTION(0x1600, Reserved6, UnknownException)
280         STD_EXCEPTION(0x1700, Reserved7, UnknownException)
281         STD_EXCEPTION(0x1800, Reserved8, UnknownException)
282         STD_EXCEPTION(0x1900, Reserved9, UnknownException)
283         STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
284         STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
285         STD_EXCEPTION(0x1c00, ReservedC, UnknownException)
286         STD_EXCEPTION(0x1d00, ReservedD, UnknownException)
287         STD_EXCEPTION(0x1e00, ReservedE, UnknownException)
288         STD_EXCEPTION(0x1f00, ReservedF, UnknownException)
289
290         STD_EXCEPTION(EXC_OFF_RMTE, RunModeTrace, UnknownException)
291
292         .globl  _end_of_vectors
293 _end_of_vectors:
294
295
296         . = 0x3000
297
298 /*
299  * This code finishes saving the registers to the exception frame
300  * and jumps to the appropriate handler for the exception.
301  * Register r21 is pointer into trap frame, r1 has new stack pointer.
302  */
303         .globl  transfer_to_handler
304 transfer_to_handler:
305         stw     r22,_NIP(r21)
306         lis     r22,MSR_POW@h
307         andc    r23,r23,r22
308         stw     r23,_MSR(r21)
309         SAVE_GPR(7, r21)
310         SAVE_4GPRS(8, r21)
311         SAVE_8GPRS(12, r21)
312         SAVE_8GPRS(24, r21)
313 #if 0
314         andi.   r23,r23,MSR_PR
315         mfspr   r23,SPRG3               /* if from user, fix up tss.regs */
316         beq     2f
317         addi    r24,r1,STACK_FRAME_OVERHEAD
318         stw     r24,PT_REGS(r23)
319 2:      addi    r2,r23,-TSS             /* set r2 to current */
320         tovirt(r2,r2,r23)
321 #endif
322         mflr    r23
323         andi.   r24,r23,0x3f00          /* get vector offset */
324         stw     r24,TRAP(r21)
325         li      r22,0
326         stw     r22,RESULT(r21)
327         mtspr   SPRG2,r22               /* r1 is now kernel sp */
328 #if 0
329         addi    r24,r2,TASK_STRUCT_SIZE /* check for kernel stack overflow */
330         cmplw   0,r1,r2
331         cmplw   1,r1,r24
332         crand   1,1,4
333         bgt     stack_ovf               /* if r2 < r1 < r2+TASK_STRUCT_SIZE */
334 #endif
335         lwz     r24,0(r23)              /* virtual address of handler */
336         lwz     r23,4(r23)              /* where to go when done */
337         mtspr   SRR0,r24
338         ori     r20,r20,0x30            /* enable IR, DR */
339         mtspr   SRR1,r20
340         mtlr    r23
341         SYNC
342         rfi                             /* jump to handler, enable MMU */
343
344 int_return:
345         mfmsr   r28             /* Disable interrupts */
346         li      r4,0
347         ori     r4,r4,MSR_EE
348         andc    r28,r28,r4
349         SYNC                    /* Some chip revs need this... */
350         mtmsr   r28
351         SYNC
352         lwz     r2,_CTR(r1)
353         lwz     r0,_LINK(r1)
354         mtctr   r2
355         mtlr    r0
356         lwz     r2,_XER(r1)
357         lwz     r0,_CCR(r1)
358         mtspr   XER,r2
359         mtcrf   0xFF,r0
360         REST_10GPRS(3, r1)
361         REST_10GPRS(13, r1)
362         REST_8GPRS(23, r1)
363         REST_GPR(31, r1)
364         lwz     r2,_NIP(r1)     /* Restore environment */
365         lwz     r0,_MSR(r1)
366         mtspr   SRR0,r2
367         mtspr   SRR1,r0
368         lwz     r0,GPR0(r1)
369         lwz     r2,GPR2(r1)
370         lwz     r1,GPR1(r1)
371         SYNC
372         rfi
373
374 /* Cache functions.
375 */
376         .globl  icache_enable
377 icache_enable:
378         mfspr   r5,HID0         /* turn on the I cache. */
379         ori     r5,r5,0x8800    /* Instruction cache only! */
380         addis   r6,0,0xFFFF
381         ori     r6,r6,0xF7FF
382         and     r6,r5,r6        /* clear the invalidate bit */
383         sync
384         mtspr   HID0,r5
385         mtspr   HID0,r6
386         isync
387         sync
388         blr
389
390         .globl  icache_disable
391 icache_disable:
392         mfspr   r5,HID0
393         addis   r6,0,0xFFFF
394         ori     r6,r6,0x7FFF
395         and     r5,r5,r6
396         sync
397         mtspr   HID0,r5
398         isync
399         sync
400         blr
401
402         .globl  icache_status
403 icache_status:
404         mfspr   r3, HID0
405         srwi    r3, r3, 15      /* >>15 & 1=> select bit 16 */
406         andi.   r3, r3, 1
407         blr
408
409         .globl  dcache_enable
410 dcache_enable:
411         mfspr   r5,HID0         /* turn on the D cache. */
412         ori     r5,r5,0x4400    /* Data cache only! */
413         mfspr   r4, PVR         /* read PVR */
414         srawi   r3, r4, 16      /* shift off the least 16 bits */
415         cmpi    0, 0, r3, 0xC   /* Check for Max pvr */
416         bne     NotMax
417         ori     r5,r5,0x0040    /* setting the DCFA bit, for Max rev 1 errata */
418 NotMax:
419         addis   r6,0,0xFFFF
420         ori     r6,r6,0xFBFF
421         and     r6,r5,r6        /* clear the invalidate bit */
422         sync
423         mtspr   HID0,r5
424         mtspr   HID0,r6
425         isync
426         sync
427         blr
428
429         .globl  dcache_disable
430 dcache_disable:
431         mfspr   r5,HID0
432         addis   r6,0,0xFFFF
433         ori     r6,r6,0xBFFF
434         and     r5,r5,r6
435         sync
436         mtspr   HID0,r5
437         isync
438         sync
439         blr
440
441         .globl  dcache_status
442 dcache_status:
443         mfspr   r3, HID0
444         srwi    r3, r3, 14      /* >>14 & 1=> select bit 17 */
445         andi.   r3, r3, 1
446         blr
447
448         .globl  dc_read
449 dc_read:
450 /*TODO : who uses this, what should it do?
451 */
452         blr
453
454
455         .globl get_pvr
456 get_pvr:
457         mfspr   r3, PVR
458         blr
459
460
461 /*------------------------------------------------------------------------------*/
462
463 /*
464  * void relocate_code (addr_sp, gd, addr_moni)
465  *
466  * This "function" does not return, instead it continues in RAM
467  * after relocating the monitor code.
468  *
469  * r3 = dest
470  * r4 = src
471  * r5 = length in bytes
472  * r6 = cachelinesize
473  */
474         .globl  relocate_code
475 relocate_code:
476
477         mr      r1,  r3         /* Set new stack pointer                */
478         mr      r9,  r4         /* Save copy of Global Data pointer     */
479         mr      r10, r5         /* Save copy of Destination Address     */
480
481         mr      r3,  r5                         /* Destination Address  */
482 #if defined(CFG_RAMBOOT) && !defined(CONFIG_LINKSTATION)
483         lis     r4, CFG_SDRAM_BASE@h            /* Source      Address  */
484         ori     r4, r4, CFG_SDRAM_BASE@l
485 #else
486         lis     r4, CFG_MONITOR_BASE@h          /* Source      Address  */
487         ori     r4, r4, CFG_MONITOR_BASE@l
488 #endif
489         lwz     r5, GOT(__init_end)
490         sub     r5, r5, r4
491         li      r6, CFG_CACHELINE_SIZE          /* Cache Line Size      */
492
493         /*
494          * Fix GOT pointer:
495          *
496          * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
497          *
498          * Offset:
499          */
500         sub     r15, r10, r4
501
502         /* First our own GOT */
503         add     r14, r14, r15
504         /* the the one used by the C code */
505         add     r30, r30, r15
506
507         /*
508          * Now relocate code
509          */
510
511         cmplw   cr1,r3,r4
512         addi    r0,r5,3
513         srwi.   r0,r0,2
514         beq     cr1,4f          /* In place copy is not necessary       */
515         beq     7f              /* Protect against 0 count              */
516         mtctr   r0
517         bge     cr1,2f
518
519         la      r8,-4(r4)
520         la      r7,-4(r3)
521 1:      lwzu    r0,4(r8)
522         stwu    r0,4(r7)
523         bdnz    1b
524         b       4f
525
526 2:      slwi    r0,r0,2
527         add     r8,r4,r0
528         add     r7,r3,r0
529 3:      lwzu    r0,-4(r8)
530         stwu    r0,-4(r7)
531         bdnz    3b
532
533 4:
534 #if !defined(CONFIG_BMW)
535 /* Unlock the data cache and invalidate locked area */
536         xor     r0, r0, r0
537         mtspr   1011, r0
538         lis     r4, CFG_INIT_RAM_ADDR@h
539         ori     r4, r4, CFG_INIT_RAM_ADDR@l
540         li      r0, 128
541         mtctr   r0
542 41:
543         dcbi    r0, r4
544         addi    r4, r4, 32
545         bdnz    41b
546 #endif
547
548 /*
549  * Now flush the cache: note that we must start from a cache aligned
550  * address. Otherwise we might miss one cache line.
551  */
552         cmpwi   r6,0
553         add     r5,r3,r5
554         beq     7f              /* Always flush prefetch queue in any case */
555         subi    r0,r6,1
556         andc    r3,r3,r0
557         mr      r4,r3
558 5:      dcbst   0,r4
559         add     r4,r4,r6
560         cmplw   r4,r5
561         blt     5b
562         sync                    /* Wait for all dcbst to complete on bus */
563         mr      r4,r3
564 6:      icbi    0,r4
565         add     r4,r4,r6
566         cmplw   r4,r5
567         blt     6b
568 7:      sync                    /* Wait for all icbi to complete on bus */
569         isync
570
571 /*
572  * We are done. Do not return, instead branch to second part of board
573  * initialization, now running from RAM.
574  */
575
576         addi    r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
577         mtlr    r0
578         blr
579
580 in_ram:
581
582         /*
583          * Relocation Function, r14 point to got2+0x8000
584          *
585          * Adjust got2 pointers, no need to check for 0, this code
586          * already puts a few entries in the table.
587          */
588         li      r0,__got2_entries@sectoff@l
589         la      r3,GOT(_GOT2_TABLE_)
590         lwz     r11,GOT(_GOT2_TABLE_)
591         mtctr   r0
592         sub     r11,r3,r11
593         addi    r3,r3,-4
594 1:      lwzu    r0,4(r3)
595         add     r0,r0,r11
596         stw     r0,0(r3)
597         bdnz    1b
598
599         /*
600          * Now adjust the fixups and the pointers to the fixups
601          * in case we need to move ourselves again.
602          */
603 2:      li      r0,__fixup_entries@sectoff@l
604         lwz     r3,GOT(_FIXUP_TABLE_)
605         cmpwi   r0,0
606         mtctr   r0
607         addi    r3,r3,-4
608         beq     4f
609 3:      lwzu    r4,4(r3)
610         lwzux   r0,r4,r11
611         add     r0,r0,r11
612         stw     r10,0(r3)
613         stw     r0,0(r4)
614         bdnz    3b
615 4:
616 clear_bss:
617         /*
618          * Now clear BSS segment
619          */
620         lwz     r3,GOT(__bss_start)
621         lwz     r4,GOT(_end)
622
623         cmplw   0, r3, r4
624         beq     6f
625
626         li      r0, 0
627 5:
628         stw     r0, 0(r3)
629         addi    r3, r3, 4
630         cmplw   0, r3, r4
631         blt     5b
632 6:
633
634         mr      r3, r9          /* Global Data pointer          */
635         mr      r4, r10         /* Destination Address          */
636         bl      board_init_r
637
638         /*
639          * Copy exception vector code to low memory
640          *
641          * r3: dest_addr
642          * r7: source address, r8: end address, r9: target address
643          */
644         .globl  trap_init
645 trap_init:
646         lwz     r7, GOT(_start)
647         lwz     r8, GOT(_end_of_vectors)
648
649         li      r9, 0x100               /* reset vector always at 0x100 */
650
651         cmplw   0, r7, r8
652         bgelr                           /* return if r7>=r8 - just in case */
653
654         mflr    r4                      /* save link register           */
655 1:
656         lwz     r0, 0(r7)
657         stw     r0, 0(r9)
658         addi    r7, r7, 4
659         addi    r9, r9, 4
660         cmplw   0, r7, r8
661         bne     1b
662
663         /*
664          * relocate `hdlr' and `int_return' entries
665          */
666         li      r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
667         li      r8, Alignment - _start + EXC_OFF_SYS_RESET
668 2:
669         bl      trap_reloc
670         addi    r7, r7, 0x100           /* next exception vector        */
671         cmplw   0, r7, r8
672         blt     2b
673
674         li      r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
675         bl      trap_reloc
676
677         li      r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
678         bl      trap_reloc
679
680         li      r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
681         li      r8, SystemCall - _start + EXC_OFF_SYS_RESET
682 3:
683         bl      trap_reloc
684         addi    r7, r7, 0x100           /* next exception vector        */
685         cmplw   0, r7, r8
686         blt     3b
687
688         li      r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
689         li      r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
690 4:
691         bl      trap_reloc
692         addi    r7, r7, 0x100           /* next exception vector        */
693         cmplw   0, r7, r8
694         blt     4b
695
696         mfmsr   r7                      /* Exception prefix 0x000 */
697         li      r8,0
698         ori     r8,r8,MSR_IP
699         andc    r7,r7,r8
700         SYNC
701         mtmsr   r7
702         SYNC
703
704         mtlr    r4                      /* restore link register        */
705         blr
706
707         /*
708          * Function: relocate entries for one exception vector
709          */
710 trap_reloc:
711         lwz     r0, 0(r7)               /* hdlr ...                     */
712         add     r0, r0, r3              /*  ... += dest_addr            */
713         stw     r0, 0(r7)
714
715         lwz     r0, 4(r7)               /* int_return ...               */
716         add     r0, r0, r3              /*  ... += dest_addr            */
717         stw     r0, 4(r7)
718
719         blr
720
721         /* Setup the BAT registers.
722          */
723 setup_bats:
724         lis     r4, CFG_IBAT0L@h
725         ori     r4, r4, CFG_IBAT0L@l
726         lis     r3, CFG_IBAT0U@h
727         ori     r3, r3, CFG_IBAT0U@l
728         mtspr   IBAT0L, r4
729         mtspr   IBAT0U, r3
730         isync
731
732         lis     r4, CFG_DBAT0L@h
733         ori     r4, r4, CFG_DBAT0L@l
734         lis     r3, CFG_DBAT0U@h
735         ori     r3, r3, CFG_DBAT0U@l
736         mtspr   DBAT0L, r4
737         mtspr   DBAT0U, r3
738         isync
739
740         lis     r4, CFG_IBAT1L@h
741         ori     r4, r4, CFG_IBAT1L@l
742         lis     r3, CFG_IBAT1U@h
743         ori     r3, r3, CFG_IBAT1U@l
744         mtspr   IBAT1L, r4
745         mtspr   IBAT1U, r3
746         isync
747
748         lis     r4, CFG_DBAT1L@h
749         ori     r4, r4, CFG_DBAT1L@l
750         lis     r3, CFG_DBAT1U@h
751         ori     r3, r3, CFG_DBAT1U@l
752         mtspr   DBAT1L, r4
753         mtspr   DBAT1U, r3
754         isync
755
756         lis     r4, CFG_IBAT2L@h
757         ori     r4, r4, CFG_IBAT2L@l
758         lis     r3, CFG_IBAT2U@h
759         ori     r3, r3, CFG_IBAT2U@l
760         mtspr   IBAT2L, r4
761         mtspr   IBAT2U, r3
762         isync
763
764         lis     r4, CFG_DBAT2L@h
765         ori     r4, r4, CFG_DBAT2L@l
766         lis     r3, CFG_DBAT2U@h
767         ori     r3, r3, CFG_DBAT2U@l
768         mtspr   DBAT2L, r4
769         mtspr   DBAT2U, r3
770         isync
771
772         lis     r4, CFG_IBAT3L@h
773         ori     r4, r4, CFG_IBAT3L@l
774         lis     r3, CFG_IBAT3U@h
775         ori     r3, r3, CFG_IBAT3U@l
776         mtspr   IBAT3L, r4
777         mtspr   IBAT3U, r3
778         isync
779
780         lis     r4, CFG_DBAT3L@h
781         ori     r4, r4, CFG_DBAT3L@l
782         lis     r3, CFG_DBAT3U@h
783         ori     r3, r3, CFG_DBAT3U@l
784         mtspr   DBAT3L, r4
785         mtspr   DBAT3U, r3
786         isync
787
788         /* Invalidate TLBs.
789          * -> for (val = 0; val < 0x20000; val+=0x1000)
790          * ->   tlbie(val);
791          */
792         lis     r3, 0
793         lis     r5, 2
794
795 1:
796         tlbie   r3
797         addi    r3, r3, 0x1000
798         cmp     0, 0, r3, r5
799         blt     1b
800
801         blr