]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - cpu/mpc8260/start.S
Initial revision
[karo-tx-uboot.git] / cpu / mpc8260 / 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 /*
26  *  U-Boot - Startup Code for MPC8260 PowerPC based Embedded Boards
27  */
28 #include <config.h>
29 #include <mpc8260.h>
30 #include <version.h>
31
32 #define CONFIG_8260 1           /* needed for Linux kernel header files */
33 #define _LINUX_CONFIG_H 1       /* avoid reading Linux autoconf.h file  */
34
35 #include <ppc_asm.tmpl>
36 #include <ppc_defs.h>
37
38 #include <asm/cache.h>
39 #include <asm/mmu.h>
40
41 #ifndef  CONFIG_IDENT_STRING
42 #define  CONFIG_IDENT_STRING ""
43 #endif
44
45 /* We don't want the  MMU yet.
46 */
47 #undef  MSR_KERNEL
48 /* Floating Point enable, Machine Check and Recoverable Interr. */
49 #ifdef DEBUG
50 #define MSR_KERNEL (MSR_FP|MSR_RI)
51 #else
52 #define MSR_KERNEL (MSR_FP|MSR_ME|MSR_RI)
53 #endif
54
55 /*
56  * Set up GOT: Global Offset Table
57  *
58  * Use r14 to access the GOT
59  */
60         START_GOT
61         GOT_ENTRY(_GOT2_TABLE_)
62         GOT_ENTRY(_FIXUP_TABLE_)
63
64         GOT_ENTRY(_start)
65         GOT_ENTRY(_start_of_vectors)
66         GOT_ENTRY(_end_of_vectors)
67         GOT_ENTRY(transfer_to_handler)
68
69         GOT_ENTRY(_end)
70         GOT_ENTRY(.bss)
71 #if defined(CONFIG_HYMOD)
72         GOT_ENTRY(environment)
73 #endif
74         END_GOT
75
76 /*
77  * Version string - must be in data segment because MPC8260 uses the first
78  * 256 bytes for the Hard Reset Configuration Word table (see below).
79  * Similarly, can't have the U-Boot Magic Number as the first thing in
80  * the image - don't know how this will affect the image tools, but I guess
81  * I'll find out soon
82  */
83         .data
84         .globl  version_string
85 version_string:
86         .ascii U_BOOT_VERSION
87         .ascii " (", __DATE__, " - ", __TIME__, ")"
88         .ascii CONFIG_IDENT_STRING, "\0"
89
90 /*
91  *  Hard Reset Configuration Word (HRCW) table
92  *
93  *  The Hard Reset Configuration Word (HRCW) sets a number of useful things
94  *  such as whether there is an external memory controller, whether the
95  *  PowerPC core is disabled (i.e. only the communications processor is
96  *  active, accessed by another CPU on the bus), whether using external
97  *  arbitration, external bus mode, boot port size, core initial prefix,
98  *  internal space base, boot memory space, etc.
99  *
100  *  These things dictate where the processor begins execution, where the
101  *  boot ROM appears in memory, the memory controller setup when access
102  *  boot ROM, etc. The HRCW is *extremely* important.
103  *
104  *  The HRCW is read from the bus during reset. One CPU on the bus will
105  *  be a hard reset configuration master, any others will be hard reset
106  *  configuration slaves. The master reads eight HRCWs from flash during
107  *  reset - the first it uses for itself, the other 7 it communicates to
108  *  up to 7 configuration slaves by some complicated mechanism, which is
109  *  not really important here.
110  *
111  *  The configuration master performs 32 successive reads starting at address
112  *  0 and incrementing by 8 each read (i.e. on 64 bit boundaries) but only 8
113  *  bits is read, and always from byte lane D[0-7] (so that port size of the
114  *  boot device does not matter). The first four reads form the 32 bit HRCW
115  *  for the master itself. The second four reads form the HRCW for the first
116  *  slave, and so on, up to seven slaves. The 32 bit HRCW is formed by
117  *  concatenating the four bytes, with the first read placed in byte 0 (the
118  *  most significant byte), and so on with the fourth read placed in byte 3
119  *  (the least significant byte).
120  */
121 #define _HRCW_TABLE_ENTRY(w)            \
122         .fill   8,1,(((w)>>24)&0xff);   \
123         .fill   8,1,(((w)>>16)&0xff);   \
124         .fill   8,1,(((w)>> 8)&0xff);   \
125         .fill   8,1,(((w)    )&0xff)
126         .text
127         .globl  _hrcw_table
128 _hrcw_table:
129         _HRCW_TABLE_ENTRY(CFG_HRCW_MASTER)
130         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE1)
131         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE2)
132         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE3)
133         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE4)
134         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE5)
135         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE6)
136         _HRCW_TABLE_ENTRY(CFG_HRCW_SLAVE7)
137 /*
138  *  After configuration, a system reset exception is executed using the
139  *  vector at offset 0x100 relative to the base set by MSR[IP]. If MSR[IP]
140  *  is 0, the base address is 0x00000000. If MSR[IP] is 1, the base address
141  *  is 0xfff00000. In the case of a Power On Reset or Hard Reset, the value
142  *  of MSR[IP] is determined by the CIP field in the HRCW.
143  *
144  *  Other bits in the HRCW set up the Base Address and Port Size in BR0.
145  *  This determines the location of the boot ROM (flash or EPROM) in the
146  *  processor's address space at boot time. As long as the HRCW is set up
147  *  so that we eventually end up executing the code below when the processor
148  *  executes the reset exception, the actual values used should not matter.
149  *
150  *  Once we have got here, the address mask in OR0 is cleared so that the
151  *  bottom 32K of the boot ROM is effectively repeated all throughout the
152  *  processor's address space, after which we can jump to the absolute
153  *  address at which the boot ROM was linked at compile time, and proceed
154  *  to initialise the memory controller without worrying if the rug will be
155  *  pulled out from under us, so to speak (it will be fine as long as we
156  *  configure BR0 with the same boot ROM link address).
157  */
158         . = EXC_OFF_SYS_RESET
159
160         .globl  _start
161 _start:
162         li      r21, BOOTFLAG_COLD      /* Normal Power-On: Boot from FLASH*/
163         b       boot_cold
164
165         . = EXC_OFF_SYS_RESET + 0x10
166
167         .globl  _start_warm
168 _start_warm:
169         li      r21, BOOTFLAG_WARM      /* Software reboot              */
170         b       boot_warm
171
172 boot_cold:
173 boot_warm:
174         mfmsr   r5                      /* save msr contents            */
175
176 #if defined(CONFIG_COGENT)
177         /* this is what the cogent EPROM does */
178         li      r0, 0
179         mtmsr   r0
180         isync
181         bl      cogent_init_8260
182 #endif  /* CONFIG_COGENT */
183
184 #if defined(CFG_DEFAULT_IMMR)
185         lis     r3, CFG_IMMR@h
186         ori     r3, r3, CFG_IMMR@l
187         lis     r4, CFG_DEFAULT_IMMR@h
188         stw     r3, 0x1A8(r4)
189 #endif /* CFG_DEFAULT_IMMR */
190
191         /* Initialise the MPC8260 processor core                        */
192         /*--------------------------------------------------------------*/
193
194         bl      init_8260_core
195
196 #ifndef CFG_RAMBOOT
197         /* When booting from ROM (Flash or EPROM), clear the            */
198         /* Address Mask in OR0 so ROM appears everywhere                */
199         /*--------------------------------------------------------------*/
200
201         lis     r3, (CFG_IMMR+IM_REGBASE)@h
202         lwz     r4, IM_OR0@l(r3)
203         li      r5, 0x7fff
204         and     r4, r4, r5
205         stw     r4, IM_OR0@l(r3)
206
207         /* Calculate absolute address in FLASH and jump there           */
208         /*--------------------------------------------------------------*/
209
210         lis     r3, CFG_MONITOR_BASE@h
211         ori     r3, r3, CFG_MONITOR_BASE@l
212         addi    r3, r3, in_flash - _start + EXC_OFF_SYS_RESET
213         mtlr    r3
214         blr
215
216 in_flash:
217 #endif  /* CFG_RAMBOOT */
218
219         /* initialize some things that are hard to access from C        */
220         /*--------------------------------------------------------------*/
221
222         lis     r3, CFG_IMMR@h          /* set up stack in internal DPRAM */
223         ori     r1, r3, CFG_INIT_SP_OFFSET
224         li      r0, 0                   /* Make room for stack frame header and */
225         stwu    r0, -4(r1)              /* clear final stack frame so that      */
226         stwu    r0, -4(r1)              /* stack backtraces terminate cleanly   */
227
228         /* let the C-code set up the rest                               */
229         /*                                                              */
230         /* Be careful to keep code relocatable !                        */
231         /*--------------------------------------------------------------*/
232
233         GET_GOT                 /* initialize GOT access                */
234
235         /* r3: IMMR */
236         bl      cpu_init_f      /* run low-level CPU init code (in Flash)*/
237
238 #ifdef DEBUG
239         bl      init_debug      /* set up debugging stuff               */
240 #endif
241
242         mr      r3, r21
243         /* r3: BOOTFLAG */
244         bl      board_init_f    /* run 1st part of board init code (in Flash)*/
245
246 /*
247  * Vector Table
248  */
249
250         .globl  _start_of_vectors
251 _start_of_vectors:
252
253 /* Machine check */
254         STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
255
256 /* Data Storage exception. */
257         STD_EXCEPTION(0x300, DataStorage, UnknownException)
258
259 /* Instruction Storage exception. */
260         STD_EXCEPTION(0x400, InstStorage, UnknownException)
261
262 /* External Interrupt exception. */
263         STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
264
265 /* Alignment exception. */
266         . = 0x600
267 Alignment:
268         EXCEPTION_PROLOG
269         mfspr   r4,DAR
270         stw     r4,_DAR(r21)
271         mfspr   r5,DSISR
272         stw     r5,_DSISR(r21)
273         addi    r3,r1,STACK_FRAME_OVERHEAD
274         li      r20,MSR_KERNEL
275         rlwimi  r20,r23,0,16,16         /* copy EE bit from saved MSR */
276         rlwimi  r20,r23,0,25,25         /* copy IP bit from saved MSR */
277         lwz     r6,GOT(transfer_to_handler)
278         mtlr    r6
279         blrl
280 .L_Alignment:
281         .long   AlignmentException - _start + EXC_OFF_SYS_RESET
282         .long   int_return - _start + EXC_OFF_SYS_RESET
283
284 /* Program check exception */
285         . = 0x700
286 ProgramCheck:
287         EXCEPTION_PROLOG
288         addi    r3,r1,STACK_FRAME_OVERHEAD
289         li      r20,MSR_KERNEL
290         rlwimi  r20,r23,0,16,16         /* copy EE bit from saved MSR */
291         rlwimi  r20,r23,0,25,25         /* copy IP bit from saved MSR */
292         lwz     r6,GOT(transfer_to_handler)
293         mtlr    r6
294         blrl
295 .L_ProgramCheck:
296         .long   ProgramCheckException - _start + EXC_OFF_SYS_RESET
297         .long   int_return - _start + EXC_OFF_SYS_RESET
298
299         STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
300
301         /* I guess we could implement decrementer, and may have
302          * to someday for timekeeping.
303          */
304         STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
305
306         STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
307         STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
308
309         . = 0xc00
310 /*
311  * r0 - SYSCALL number
312  * r3-... arguments
313  */
314 SystemCall:
315         addis   r11,r0,0                /* get functions table addr */
316         ori     r11,r11,0               /* Note: this code is patched in trap_init */
317         addis   r12,r0,0                /* get number of functions */
318         ori     r12,r12,0
319
320         cmplw   0, r0, r12
321         bge     1f
322
323         rlwinm  r0,r0,2,0,31            /* fn_addr = fn_tbl[r0] */
324         add     r11,r11,r0
325         lwz     r11,0(r11)
326
327         li      r12,0xd00-4*3           /* save LR & SRRx */
328         mflr    r0
329         stw     r0,0(r12)
330         mfspr   r0,SRR0
331         stw     r0,4(r12)
332         mfspr   r0,SRR1
333         stw     r0,8(r12)
334
335         li      r12,0xc00+_back-SystemCall
336         mtlr    r12
337         mtspr   SRR0,r11
338
339 1:      SYNC
340         rfi
341
342 _back:
343
344         mfmsr   r11                     /* Disable interrupts */
345         li      r12,0
346         ori     r12,r12,MSR_EE
347         andc    r11,r11,r12
348         SYNC                            /* Some chip revs need this... */
349         mtmsr   r11
350         SYNC
351
352         li      r12,0xd00-4*3           /* restore regs */
353         lwz     r11,0(r12)
354         mtlr    r11
355         lwz     r11,4(r12)
356         mtspr   SRR0,r11
357         lwz     r11,8(r12)
358         mtspr   SRR1,r11
359
360         SYNC
361         rfi
362
363         STD_EXCEPTION(0xd00, SingleStep, UnknownException)
364
365         STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
366         STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
367
368         STD_EXCEPTION(0x1000, InstructionTLBMiss, UnknownException)
369         STD_EXCEPTION(0x1100, DataLoadTLBMiss, UnknownException)
370         STD_EXCEPTION(0x1200, DataStoreTLBMiss, UnknownException)
371 #ifdef DEBUG
372         . = 0x1300
373         /*
374          * This exception occurs when the program counter matches the
375          * Instruction Address Breakpoint Register (IABR).
376          *
377          * I want the cpu to halt if this occurs so I can hunt around
378          * with the debugger and look at things.
379          *
380          * When DEBUG is defined, both machine check enable (in the MSR)
381          * and checkstop reset enable (in the reset mode register) are
382          * turned off and so a checkstop condition will result in the cpu
383          * halting.
384          *
385          * I force the cpu into a checkstop condition by putting an illegal
386          * instruction here (at least this is the theory).
387          *
388          * well - that didnt work, so just do an infinite loop!
389          */
390 1:      b       1b
391 #else
392         STD_EXCEPTION(0x1300, InstructionBreakpoint, DebugException)
393 #endif
394         STD_EXCEPTION(0x1400, SMI, UnknownException)
395
396         STD_EXCEPTION(0x1500, Trap_15, UnknownException)
397         STD_EXCEPTION(0x1600, Trap_16, UnknownException)
398         STD_EXCEPTION(0x1700, Trap_17, UnknownException)
399         STD_EXCEPTION(0x1800, Trap_18, UnknownException)
400         STD_EXCEPTION(0x1900, Trap_19, UnknownException)
401         STD_EXCEPTION(0x1a00, Trap_1a, UnknownException)
402         STD_EXCEPTION(0x1b00, Trap_1b, UnknownException)
403         STD_EXCEPTION(0x1c00, Trap_1c, UnknownException)
404         STD_EXCEPTION(0x1d00, Trap_1d, UnknownException)
405         STD_EXCEPTION(0x1e00, Trap_1e, UnknownException)
406         STD_EXCEPTION(0x1f00, Trap_1f, UnknownException)
407         STD_EXCEPTION(0x2000, Trap_20, UnknownException)
408         STD_EXCEPTION(0x2100, Trap_21, UnknownException)
409         STD_EXCEPTION(0x2200, Trap_22, UnknownException)
410         STD_EXCEPTION(0x2300, Trap_23, UnknownException)
411         STD_EXCEPTION(0x2400, Trap_24, UnknownException)
412         STD_EXCEPTION(0x2500, Trap_25, UnknownException)
413         STD_EXCEPTION(0x2600, Trap_26, UnknownException)
414         STD_EXCEPTION(0x2700, Trap_27, UnknownException)
415         STD_EXCEPTION(0x2800, Trap_28, UnknownException)
416         STD_EXCEPTION(0x2900, Trap_29, UnknownException)
417         STD_EXCEPTION(0x2a00, Trap_2a, UnknownException)
418         STD_EXCEPTION(0x2b00, Trap_2b, UnknownException)
419         STD_EXCEPTION(0x2c00, Trap_2c, UnknownException)
420         STD_EXCEPTION(0x2d00, Trap_2d, UnknownException)
421         STD_EXCEPTION(0x2e00, Trap_2e, UnknownException)
422         STD_EXCEPTION(0x2f00, Trap_2f, UnknownException)
423
424
425         .globl  _end_of_vectors
426 _end_of_vectors:
427
428         . = 0x3000
429
430 /*
431  * This code finishes saving the registers to the exception frame
432  * and jumps to the appropriate handler for the exception.
433  * Register r21 is pointer into trap frame, r1 has new stack pointer.
434  */
435         .globl  transfer_to_handler
436 transfer_to_handler:
437         stw     r22,_NIP(r21)
438         lis     r22,MSR_POW@h
439         andc    r23,r23,r22
440         stw     r23,_MSR(r21)
441         SAVE_GPR(7, r21)
442         SAVE_4GPRS(8, r21)
443         SAVE_8GPRS(12, r21)
444         SAVE_8GPRS(24, r21)
445         mflr    r23
446         andi.   r24,r23,0x3f00          /* get vector offset */
447         stw     r24,TRAP(r21)
448         li      r22,0
449         stw     r22,RESULT(r21)
450         lwz     r24,0(r23)              /* virtual address of handler */
451         lwz     r23,4(r23)              /* where to go when done */
452         mtspr   SRR0,r24
453         mtspr   SRR1,r20
454         mtlr    r23
455         SYNC
456         rfi                             /* jump to handler, enable MMU */
457
458 int_return:
459         mfmsr   r28             /* Disable interrupts */
460         li      r4,0
461         ori     r4,r4,MSR_EE
462         andc    r28,r28,r4
463         SYNC                    /* Some chip revs need this... */
464         mtmsr   r28
465         SYNC
466         lwz     r2,_CTR(r1)
467         lwz     r0,_LINK(r1)
468         mtctr   r2
469         mtlr    r0
470         lwz     r2,_XER(r1)
471         lwz     r0,_CCR(r1)
472         mtspr   XER,r2
473         mtcrf   0xFF,r0
474         REST_10GPRS(3, r1)
475         REST_10GPRS(13, r1)
476         REST_8GPRS(23, r1)
477         REST_GPR(31, r1)
478         lwz     r2,_NIP(r1)     /* Restore environment */
479         lwz     r0,_MSR(r1)
480         mtspr   SRR0,r2
481         mtspr   SRR1,r0
482         lwz     r0,GPR0(r1)
483         lwz     r2,GPR2(r1)
484         lwz     r1,GPR1(r1)
485         SYNC
486         rfi
487
488 #if defined(CONFIG_COGENT)
489
490 /*
491  * This code initialises the MPC8260 processor core
492  * (conforms to PowerPC 603e spec)
493  */
494
495         .globl  cogent_init_8260
496 cogent_init_8260:
497
498         /* Taken from page 14 of CMA282 manual                          */
499         /*--------------------------------------------------------------*/
500
501         lis     r4, (CFG_IMMR+IM_REGBASE)@h
502         lis     r3, CFG_IMMR@h
503         stw     r3, IM_IMMR@l(r4)
504         lwz     r3, IM_IMMR@l(r4)
505         stw     r3, 0(r0)
506         lis     r3, CFG_SYPCR@h
507         ori     r3, r3, CFG_SYPCR@l
508         stw     r3, IM_SYPCR@l(r4)
509         lwz     r3, IM_SYPCR@l(r4)
510         stw     r3, 4(r0)
511         lis     r3, CFG_SCCR@h
512         ori     r3, r3, CFG_SCCR@l
513         stw     r3, IM_SCCR@l(r4)
514         lwz     r3, IM_SCCR@l(r4)
515         stw     r3, 8(r0)
516
517         /* the rest of this was disassembled from the                   */
518         /* EPROM code that came with my CMA282 CPU module               */
519         /*--------------------------------------------------------------*/
520
521         lis     r1, 0x1234
522         ori     r1, r1, 0x5678
523         stw     r1, 0x20(r0)
524         lwz     r1, 0x20(r0)
525         stw     r1, 0x24(r0)
526         lwz     r1, 0x24(r0)
527         lis     r3, 0x0e80
528         ori     r3, r3, 0
529         stw     r1, 4(r3)
530         lwz     r1, 4(r3)
531
532         /* Done!                                                        */
533         /*--------------------------------------------------------------*/
534
535         blr
536
537 #endif  /* CONFIG_COGENT */
538
539 /*
540  * This code initialises the MPC8260 processor core
541  * (conforms to PowerPC 603e spec)
542  * Note: expects original MSR contents to be in r5.
543  */
544
545         .globl  init_8260_core
546 init_8260_core:
547
548         /* Initialize machine status; enable machine check interrupt    */
549         /*--------------------------------------------------------------*/
550
551         li      r3, MSR_KERNEL          /* Set ME and RI flags */
552         rlwimi  r3, r5, 0, 25, 25       /* preserve IP bit set by HRCW */
553 #ifdef DEBUG
554         rlwimi  r3, r5, 0, 21, 22       /* debugger might set SE & BE bits */
555 #endif
556         SYNC                            /* Some chip revs need this... */
557         mtmsr   r3
558         SYNC
559         mtspr   SRR1, r3                /* Make SRR1 match MSR */
560
561         /* Initialise the SYPCR early, and reset the watchdog (if req)  */
562         /*--------------------------------------------------------------*/
563
564         lis     r3, (CFG_IMMR+IM_REGBASE)@h
565 #if !defined(CONFIG_COGENT)
566         lis     r4, CFG_SYPCR@h
567         ori     r4, r4, CFG_SYPCR@l
568         stw     r4, IM_SYPCR@l(r3)
569 #endif /* !CONFIG_COGENT */
570 #if defined(CONFIG_WATCHDOG)
571         li      r4, 21868               /* = 0x556c */
572         sth     r4, IM_SWSR@l(r3)
573         li      r4, -21959              /* = 0xaa39 */
574         sth     r4, IM_SWSR@l(r3)
575 #endif /* CONFIG_WATCHDOG */
576
577         /* Initialize the Hardware Implementation-dependent Registers   */
578         /* HID0 also contains cache control                             */
579         /*--------------------------------------------------------------*/
580
581         lis     r3, CFG_HID0_INIT@h
582         ori     r3, r3, CFG_HID0_INIT@l
583         SYNC
584         mtspr   HID0, r3
585
586         lis     r3, CFG_HID0_FINAL@h
587         ori     r3, r3, CFG_HID0_FINAL@l
588         SYNC
589         mtspr   HID0, r3
590
591         lis     r3, CFG_HID2@h
592         ori     r3, r3, CFG_HID2@l
593         mtspr   HID2, r3
594
595         /* clear all BAT's                                              */
596         /*--------------------------------------------------------------*/
597
598         li      r0, 0
599         mtspr   DBAT0U, r0
600         mtspr   DBAT0L, r0
601         mtspr   DBAT1U, r0
602         mtspr   DBAT1L, r0
603         mtspr   DBAT2U, r0
604         mtspr   DBAT2L, r0
605         mtspr   DBAT3U, r0
606         mtspr   DBAT3L, r0
607         mtspr   IBAT0U, r0
608         mtspr   IBAT0L, r0
609         mtspr   IBAT1U, r0
610         mtspr   IBAT1L, r0
611         mtspr   IBAT2U, r0
612         mtspr   IBAT2L, r0
613         mtspr   IBAT3U, r0
614         mtspr   IBAT3L, r0
615         SYNC
616
617         /* invalidate all tlb's                                         */
618         /*                                                              */
619         /* From the 603e User Manual: "The 603e provides the ability to */
620         /* invalidate a TLB entry. The TLB Invalidate Entry (tlbie)     */
621         /* instruction invalidates the TLB entry indexed by the EA, and */
622         /* operates on both the instruction and data TLBs simultaneously*/
623         /* invalidating four TLB entries (both sets in each TLB). The   */
624         /* index corresponds to bits 15-19 of the EA. To invalidate all */
625         /* entries within both TLBs, 32 tlbie instructions should be    */
626         /* issued, incrementing this field by one each time."           */
627         /*                                                              */
628         /* "Note that the tlbia instruction is not implemented on the   */
629         /* 603e."                                                       */
630         /*                                                              */
631         /* bits 15-19 correspond to addresses 0x00000000 to 0x0001F000  */
632         /* incrementing by 0x1000 each time. The code below is sort of  */
633         /* based on code in "flush_tlbs" from arch/ppc/kernel/head.S    */
634         /*                                                              */
635         /*--------------------------------------------------------------*/
636
637         li      r3, 32
638         mtctr   r3
639         li      r3, 0
640 1:      tlbie   r3
641         addi    r3, r3, 0x1000
642         bdnz    1b
643         SYNC
644
645         /* Done!                                                        */
646         /*--------------------------------------------------------------*/
647
648         blr
649
650 #ifdef DEBUG
651
652 /*
653  * initialise things related to debugging.
654  *
655  * must be called after the global offset table (GOT) is initialised
656  * (GET_GOT) and after cpu_init_f() has executed.
657  */
658
659         .globl  init_debug
660 init_debug:
661
662         lis     r3, (CFG_IMMR+IM_REGBASE)@h
663
664         /* Quick and dirty hack to enable the RAM and copy the          */
665         /* vectors so that we can take exceptions.                      */
666         /*--------------------------------------------------------------*/
667         /* write Memory Refresh Prescaler */
668         li      r4, CFG_MPTPR
669         sth     r4, IM_MPTPR@l(r3)
670         /* write 60x Refresh Timer */
671         li      r4, CFG_PSRT
672         stb     r4, IM_PSRT@l(r3)
673         /* init the 60x SDRAM Mode Register */
674         lis     r4, (CFG_PSDMR|PSDMR_OP_NORM)@h
675         ori     r4, r4, (CFG_PSDMR|PSDMR_OP_NORM)@l
676         stw     r4, IM_PSDMR@l(r3)
677         /* write Precharge All Banks command */
678         lis     r4, (CFG_PSDMR|PSDMR_OP_PREA)@h
679         ori     r4, r4, (CFG_PSDMR|PSDMR_OP_PREA)@l
680         stw     r4, IM_PSDMR@l(r3)
681         stb     r0, 0(0)
682         /* write eight CBR Refresh commands */
683         lis     r4, (CFG_PSDMR|PSDMR_OP_CBRR)@h
684         ori     r4, r4, (CFG_PSDMR|PSDMR_OP_CBRR)@l
685         stw     r4, IM_PSDMR@l(r3)
686         stb     r0, 0(0)
687         stb     r0, 0(0)
688         stb     r0, 0(0)
689         stb     r0, 0(0)
690         stb     r0, 0(0)
691         stb     r0, 0(0)
692         stb     r0, 0(0)
693         stb     r0, 0(0)
694         /* write Mode Register Write command */
695         lis     r4, (CFG_PSDMR|PSDMR_OP_MRW)@h
696         ori     r4, r4, (CFG_PSDMR|PSDMR_OP_MRW)@l
697         stw     r4, IM_PSDMR@l(r3)
698         stb     r0, 0(0)
699         /* write Normal Operation command and enable Refresh */
700         lis     r4, (CFG_PSDMR|PSDMR_OP_NORM|PSDMR_RFEN)@h
701         ori     r4, r4, (CFG_PSDMR|PSDMR_OP_NORM|PSDMR_RFEN)@l
702         stw     r4, IM_PSDMR@l(r3)
703         stb     r0, 0(0)
704         /* RAM should now be operational */
705
706 #define VEC_WRD_CNT     ((_end_of_vectors - _start + EXC_OFF_SYS_RESET) / 4)
707
708         lwz     r3, GOT(_end_of_vectors)
709         rlwinm  r4, r3, 0, 18, 31       /* _end_of_vectors & 0x3FFF     */
710         lis     r5, VEC_WRD_CNT@h
711         ori     r5, r5, VEC_WRD_CNT@l
712         mtctr   r5
713 1:
714         lwzu    r5, -4(r3)
715         stwu    r5, -4(r4)
716         bdnz    1b
717
718         /* Load the Instruction Address Breakpoint Register (IABR).     */
719         /*                                                              */
720         /* The address to load is stored in the first word of dual port */
721         /* ram and should be preserved while the power is on, so you    */
722         /* can plug addresses into that location then reset the cpu and */
723         /* this code will load that address into the IABR after the     */
724         /* reset.                                                       */
725         /*                                                              */
726         /* When the program counter matches the contents of the IABR,   */
727         /* an exception is generated (before the instruction at that    */
728         /* location completes). The vector for this exception is 0x1300 */
729         /*--------------------------------------------------------------*/
730         lis     r3, CFG_IMMR@h
731         lwz     r3, 0(r3)
732         mtspr   IABR, r3
733
734         /* Set the entire dual port RAM (where the initial stack        */
735         /* resides) to a known value - makes it easier to see where     */
736         /* the stack has been written                                   */
737         /*--------------------------------------------------------------*/
738         lis     r3, (CFG_IMMR + CFG_INIT_SP_OFFSET)@h
739         ori     r3, r3, (CFG_IMMR + CFG_INIT_SP_OFFSET)@l
740         li      r4, ((CFG_INIT_SP_OFFSET - 4) / 4)
741         mtctr   r4
742         lis     r4, 0xdeadbeaf@h
743         ori     r4, r4, 0xdeadbeaf@l
744 1:
745         stwu    r4, -4(r3)
746         bdnz    1b
747
748         /* Done!                                                        */
749         /*--------------------------------------------------------------*/
750
751         blr
752 #endif
753
754 /* Cache functions.
755  *
756  * Note: requires that all cache bits in
757  * HID0 are in the low half word.
758  */
759         .globl  icache_enable
760 icache_enable:
761         mfspr   r3, HID0
762         ori     r3, r3, HID0_ICE
763         lis     r4, 0
764         ori     r4, r4, HID0_ILOCK
765         andc    r3, r3, r4
766         ori     r4, r3, HID0_ICFI
767         isync
768         mtspr   HID0, r4        /* sets enable and invalidate, clears lock */
769         isync
770         mtspr   HID0, r3        /* clears invalidate */
771         blr
772
773         .globl  icache_disable
774 icache_disable:
775         mfspr   r3, HID0
776         lis     r4, 0
777         ori     r4, r4, HID0_ICE|HID0_ILOCK
778         andc    r3, r3, r4
779         ori     r4, r3, HID0_ICFI
780         isync
781         mtspr   HID0, r4        /* sets invalidate, clears enable and lock */
782         isync
783         mtspr   HID0, r3        /* clears invalidate */
784         blr
785
786         .globl  icache_status
787 icache_status:
788         mfspr   r3, HID0
789         rlwinm  r3, r3, HID0_ICE_BITPOS + 1, 31, 31
790         blr
791
792         .globl  dcache_enable
793 dcache_enable:
794         mfspr   r3, HID0
795         ori     r3, r3, HID0_DCE
796         lis     r4, 0
797         ori     r4, r4, HID0_DLOCK
798         andc    r3, r3, r4
799         ori     r4, r3, HID0_DCI
800         sync
801         mtspr   HID0, r4        /* sets enable and invalidate, clears lock */
802         sync
803         mtspr   HID0, r3        /* clears invalidate */
804         blr
805
806         .globl  dcache_disable
807 dcache_disable:
808         mfspr   r3, HID0
809         lis     r4, 0
810         ori     r4, r4, HID0_DCE|HID0_DLOCK
811         andc    r3, r3, r4
812         ori     r4, r3, HID0_DCI
813         sync
814         mtspr   HID0, r4        /* sets invalidate, clears enable and lock */
815         sync
816         mtspr   HID0, r3        /* clears invalidate */
817         blr
818
819         .globl  dcache_status
820 dcache_status:
821         mfspr   r3, HID0
822         rlwinm  r3, r3, HID0_DCE_BITPOS + 1, 31, 31
823         blr
824
825         .globl get_pvr
826 get_pvr:
827         mfspr   r3, PVR
828         blr
829
830 /*------------------------------------------------------------------------------*/
831
832 /*
833  * void relocate_code (addr_sp, gd, addr_moni)
834  *
835  * This "function" does not return, instead it continues in RAM
836  * after relocating the monitor code.
837  *
838  * r3 = dest
839  * r4 = src
840  * r5 = length in bytes
841  * r6 = cachelinesize
842  */
843         .globl  relocate_code
844 relocate_code:
845         mr      r1,  r3         /* Set new stack pointer                */
846         mr      r9,  r4         /* Save copy of Global Data pointer     */
847         mr      r10, r5         /* Save copy of Destination Address     */
848
849         mr      r3,  r5                         /* Destination Address  */
850         lis     r4, CFG_MONITOR_BASE@h          /* Source      Address  */
851         ori     r4, r4, CFG_MONITOR_BASE@l
852         lis     r5, CFG_MONITOR_LEN@h           /* Length in Bytes      */
853         ori     r5, r5, CFG_MONITOR_LEN@l
854         li      r6, CFG_CACHELINE_SIZE          /* Cache Line Size      */
855
856         /*
857          * Fix GOT pointer:
858          *
859          * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
860          *
861          * Offset:
862          */
863         sub     r15, r10, r4
864
865         /* First our own GOT */
866         add     r14, r14, r15
867         /* then the one used by the C code */
868         add     r30, r30, r15
869
870         /*
871          * Now relocate code
872          */
873
874         cmplw   cr1,r3,r4
875         addi    r0,r5,3
876         srwi.   r0,r0,2
877         beq     cr1,4f          /* In place copy is not necessary       */
878         beq     7f              /* Protect against 0 count              */
879         mtctr   r0
880         bge     cr1,2f
881
882         la      r8,-4(r4)
883         la      r7,-4(r3)
884 1:      lwzu    r0,4(r8)
885         stwu    r0,4(r7)
886         bdnz    1b
887         b       4f
888
889 2:      slwi    r0,r0,2
890         add     r8,r4,r0
891         add     r7,r3,r0
892 3:      lwzu    r0,-4(r8)
893         stwu    r0,-4(r7)
894         bdnz    3b
895
896 /*
897  * Now flush the cache: note that we must start from a cache aligned
898  * address. Otherwise we might miss one cache line.
899  */
900 4:      cmpwi   r6,0
901         add     r5,r3,r5
902         beq     7f              /* Always flush prefetch queue in any case */
903         subi    r0,r6,1
904         andc    r3,r3,r0
905         mfspr   r7,HID0         /* don't do dcbst if dcache is disabled */
906         rlwinm  r7,r7,HID0_DCE_BITPOS+1,31,31
907         cmpwi   r7,0
908         beq     9f
909         mr      r4,r3
910 5:      dcbst   0,r4
911         add     r4,r4,r6
912         cmplw   r4,r5
913         blt     5b
914         sync                    /* Wait for all dcbst to complete on bus */
915 9:      mfspr   r7,HID0         /* don't do icbi if icache is disabled */
916         rlwinm  r7,r7,HID0_ICE_BITPOS+1,31,31
917         cmpwi   r7,0
918         beq     7f
919         mr      r4,r3
920 6:      icbi    0,r4
921         add     r4,r4,r6
922         cmplw   r4,r5
923         blt     6b
924 7:      sync                    /* Wait for all icbi to complete on bus */
925         isync
926
927 /*
928  * We are done. Do not return, instead branch to second part of board
929  * initialization, now running from RAM.
930  */
931
932         addi    r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
933         mtlr    r0
934         blr
935
936 in_ram:
937
938         /*
939          * Relocation Function, r14 point to got2+0x8000
940          *
941          * Adjust got2 pointers, no need to check for 0, this code
942          * already puts a few entries in the table.
943          */
944         li      r0,__got2_entries@sectoff@l
945         la      r3,GOT(_GOT2_TABLE_)
946         lwz     r11,GOT(_GOT2_TABLE_)
947         mtctr   r0
948         sub     r11,r3,r11
949         addi    r3,r3,-4
950 1:      lwzu    r0,4(r3)
951         add     r0,r0,r11
952         stw     r0,0(r3)
953         bdnz    1b
954
955         /*
956          * Now adjust the fixups and the pointers to the fixups
957          * in case we need to move ourselves again.
958          */
959 2:      li      r0,__fixup_entries@sectoff@l
960         lwz     r3,GOT(_FIXUP_TABLE_)
961         cmpwi   r0,0
962         mtctr   r0
963         addi    r3,r3,-4
964         beq     4f
965 3:      lwzu    r4,4(r3)
966         lwzux   r0,r4,r11
967         add     r0,r0,r11
968         stw     r10,0(r3)
969         stw     r0,0(r4)
970         bdnz    3b
971 4:
972 clear_bss:
973         /*
974          * Now clear BSS segment
975          */
976         lwz     r3,GOT(.bss)
977 #if defined(CONFIG_HYMOD)
978         /*
979          * For HYMOD - the environment is the very last item in flash.
980          * The real .bss stops just before environment starts, so only
981          * clear up to that point.
982          *
983          * taken from mods for FADS board
984          */
985         lwz     r4,GOT(environment)
986 #else
987         lwz     r4,GOT(_end)
988 #endif
989
990         cmplw   0, r3, r4
991         beq     6f
992
993         li      r0, 0
994 5:
995         stw     r0, 0(r3)
996         addi    r3, r3, 4
997         cmplw   0, r3, r4
998         bne     5b
999 6:
1000
1001         mr      r3, r9          /* Global Data pointer          */
1002         mr      r4, r10         /* Destination Address          */
1003         bl      board_init_r
1004
1005         /* Problems accessing "end" in C, so do it here */
1006         .globl  get_endaddr
1007 get_endaddr:
1008         lwz     r3,GOT(_end)
1009         blr
1010
1011         /*
1012          * Copy exception vector code to low memory
1013          *
1014          * r3: dest_addr
1015          * r7: source address, r8: end address, r9: target address
1016          */
1017         .globl  trap_init
1018 trap_init:
1019         lwz     r7, GOT(_start)
1020         lwz     r8, GOT(_end_of_vectors)
1021
1022         rlwinm  r9, r7, 0, 18, 31       /* _start & 0x3FFF      */
1023
1024         cmplw   0, r7, r8
1025         bgelr                           /* return if r7>=r8 - just in case */
1026
1027         mflr    r4                      /* save link register           */
1028 1:
1029         lwz     r0, 0(r7)
1030         stw     r0, 0(r9)
1031         addi    r7, r7, 4
1032         addi    r9, r9, 4
1033         cmplw   0, r7, r8
1034         bne     1b
1035
1036         /*
1037          * relocate `hdlr' and `int_return' entries
1038          */
1039         li      r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
1040         li      r8, Alignment - _start + EXC_OFF_SYS_RESET
1041 2:
1042         bl      trap_reloc
1043         addi    r7, r7, 0x100           /* next exception vector        */
1044         cmplw   0, r7, r8
1045         blt     2b
1046
1047         li      r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
1048         bl      trap_reloc
1049
1050         li      r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
1051         bl      trap_reloc
1052
1053         li      r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
1054         li      r8, SystemCall - _start + EXC_OFF_SYS_RESET
1055 3:
1056         bl      trap_reloc
1057         addi    r7, r7, 0x100           /* next exception vector        */
1058         cmplw   0, r7, r8
1059         blt     3b
1060
1061         li      r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
1062         li      r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
1063 4:
1064         bl      trap_reloc
1065         addi    r7, r7, 0x100           /* next exception vector        */
1066         cmplw   0, r7, r8
1067         blt     4b
1068
1069         mfmsr   r3                      /* now that the vectors have    */
1070         lis     r7, MSR_IP@h            /* relocated into low memory    */
1071         ori     r7, r7, MSR_IP@l        /* MSR[IP] can be turned off    */
1072         andc    r3, r3, r7              /* (if it was on)               */
1073         SYNC                            /* Some chip revs need this... */
1074         mtmsr   r3
1075         SYNC
1076
1077         mtlr    r4                      /* restore link register    */
1078         blr
1079
1080         /*
1081          * Function: relocate entries for one exception vector
1082          */
1083 trap_reloc:
1084         lwz     r0, 0(r7)               /* hdlr ...                     */
1085         add     r0, r0, r3              /*  ... += dest_addr            */
1086         stw     r0, 0(r7)
1087
1088         lwz     r0, 4(r7)               /* int_return ...               */
1089         add     r0, r0, r3              /*  ... += dest_addr            */
1090         stw     r0, 4(r7)
1091
1092         blr