blob: 4d7c71644f0fc10851f27ad34246d967935cfaae [file] [log] [blame]
wdenk0db5bca2003-03-31 17:27:09 +00001/*
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 * Copyright (C) 2003 Martin Winistoerfer, martinwinistoerfer@gmx.ch.
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26/*
27 * File: start.S
28 *
29 * Discription: startup code
30 *
31 */
32
33#include <config.h>
34#include <mpc5xx.h>
35#include <version.h>
36
37#define CONFIG_5xx 1 /* needed for Linux kernel header files */
38#define _LINUX_CONFIG_H 1 /* avoid reading Linux autoconf.h file */
39
40#include <ppc_asm.tmpl>
41#include <ppc_defs.h>
42
43#include <linux/config.h>
44#include <asm/processor.h>
45
46#ifndef CONFIG_IDENT_STRING
47#define CONFIG_IDENT_STRING ""
48#endif
49
50/* We don't have a MMU.
51*/
52#undef MSR_KERNEL
53#define MSR_KERNEL ( MSR_ME | MSR_RI ) /* Machine Check and Recoverable Interr. */
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
wdenk3b57fe02003-05-30 12:48:29 +000069 GOT_ENTRY(__init_end)
wdenk0db5bca2003-03-31 17:27:09 +000070 GOT_ENTRY(_end)
wdenk5d232d02003-05-22 22:52:13 +000071 GOT_ENTRY(__bss_start)
wdenk0db5bca2003-03-31 17:27:09 +000072 END_GOT
73
74/*
75 * r3 - 1st arg to board_init(): IMMP pointer
76 * r4 - 2nd arg to board_init(): boot flag
77 */
78 .text
79 .long 0x27051956 /* U-Boot Magic Number */
80 .globl version_string
81version_string:
82 .ascii U_BOOT_VERSION
83 .ascii " (", __DATE__, " - ", __TIME__, ")"
84 .ascii CONFIG_IDENT_STRING, "\0"
85
86 . = EXC_OFF_SYS_RESET
87 .globl _start
88_start:
89 mfspr r3, 638
90 li r4, CFG_ISB /* Set ISB bit */
91 or r3, r3, r4
92 mtspr 638, r3
93 li r21, BOOTFLAG_COLD /* Normal Power-On: Boot from FLASH */
94 b boot_cold
95
96 . = EXC_OFF_SYS_RESET + 0x20
97
98 .globl _start_warm
99_start_warm:
100 li r21, BOOTFLAG_WARM /* Software reboot */
101 b boot_warm
102
103boot_cold:
104boot_warm:
105
106 /* Initialize machine status; enable machine check interrupt */
107 /*----------------------------------------------------------------------*/
108 li r3, MSR_KERNEL /* Set ME, RI flags */
109 mtmsr r3
110 mtspr SRR1, r3 /* Make SRR1 match MSR */
111
112 /* Initialize debug port registers */
113 /*----------------------------------------------------------------------*/
114 xor r0, r0, r0 /* Clear R0 */
115 mtspr LCTRL1, r0 /* Initialize debug port regs */
116 mtspr LCTRL2, r0
117 mtspr COUNTA, r0
118 mtspr COUNTB, r0
119
120 /*
121 * Calculate absolute address in FLASH and jump there
122 *----------------------------------------------------------------------*/
123
124 lis r3, CFG_MONITOR_BASE@h
125 ori r3, r3, CFG_MONITOR_BASE@l
126 addi r3, r3, in_flash - _start + EXC_OFF_SYS_RESET
127 mtlr r3
128 blr
129
130in_flash:
131
132 /* Initialize some SPRs that are hard to access from C */
133 /*----------------------------------------------------------------------*/
134
135 lis r3, CFG_IMMR@h /* Pass IMMR as arg1 to C routine */
136 lis r2, CFG_INIT_SP_ADDR@h
137 ori r1, r2, CFG_INIT_SP_ADDR@l /* Set up the stack in internal SRAM */
138 /* Note: R0 is still 0 here */
139 stwu r0, -4(r1) /* Clear final stack frame so that */
140 stwu r0, -4(r1) /* stack backtraces terminate cleanly */
141
142 /*
143 * Disable serialized ifetch and show cycles
144 * (i.e. set processor to normal mode) for maximum
145 * performance.
146 */
147
148 li r2, 0x0007
149 mtspr ICTRL, r2
150
151 /* Set up debug mode entry */
152
153 lis r2, CFG_DER@h
154 ori r2, r2, CFG_DER@l
155 mtspr DER, r2
156
157 /* Let the C-code set up the rest */
158 /* */
159 /* Be careful to keep code relocatable ! */
160 /*----------------------------------------------------------------------*/
161
162 GET_GOT /* initialize GOT access */
163
164 /* r3: IMMR */
165 bl cpu_init_f /* run low-level CPU init code (from Flash) */
166
167 mr r3, r21
168 /* r3: BOOTFLAG */
169 bl board_init_f /* run 1st part of board init code (from Flash) */
170
171
172
173 .globl _start_of_vectors
174_start_of_vectors:
175
176/* Machine check */
177 STD_EXCEPTION(0x200, MachineCheck, MachineCheckException)
178
179/* Data Storage exception. "Never" generated on the 860. */
180 STD_EXCEPTION(0x300, DataStorage, UnknownException)
181
182/* Instruction Storage exception. "Never" generated on the 860. */
183 STD_EXCEPTION(0x400, InstStorage, UnknownException)
184
185/* External Interrupt exception. */
186 STD_EXCEPTION(0x500, ExtInterrupt, external_interrupt)
187
188/* Alignment exception. */
189 . = 0x600
190Alignment:
191 EXCEPTION_PROLOG
192 mfspr r4,DAR
193 stw r4,_DAR(r21)
194 mfspr r5,DSISR
195 stw r5,_DSISR(r21)
196 addi r3,r1,STACK_FRAME_OVERHEAD
197 li r20,MSR_KERNEL
198 rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
199 lwz r6,GOT(transfer_to_handler)
200 mtlr r6
201 blrl
202.L_Alignment:
203 .long AlignmentException - _start + EXC_OFF_SYS_RESET
204 .long int_return - _start + EXC_OFF_SYS_RESET
205
206/* Program check exception */
207 . = 0x700
208ProgramCheck:
209 EXCEPTION_PROLOG
210 addi r3,r1,STACK_FRAME_OVERHEAD
211 li r20,MSR_KERNEL
212 rlwimi r20,r23,0,16,16 /* copy EE bit from saved MSR */
213 lwz r6,GOT(transfer_to_handler)
214 mtlr r6
215 blrl
216.L_ProgramCheck:
217 .long ProgramCheckException - _start + EXC_OFF_SYS_RESET
218 .long int_return - _start + EXC_OFF_SYS_RESET
219
220 /* FPU on MPC5xx available. We will use it later.
221 */
222 STD_EXCEPTION(0x800, FPUnavailable, UnknownException)
223
224 /* I guess we could implement decrementer, and may have
225 * to someday for timekeeping.
226 */
227 STD_EXCEPTION(0x900, Decrementer, timer_interrupt)
228 STD_EXCEPTION(0xa00, Trap_0a, UnknownException)
229 STD_EXCEPTION(0xb00, Trap_0b, UnknownException)
230
231 . = 0xc00
232/*
233 * r0 - SYSCALL number
234 * r3-... arguments
235 */
236SystemCall:
237 addis r11,r0,0 /* get functions table addr */
238 ori r11,r11,0 /* Note: this code is patched in trap_init */
239 addis r12,r0,0 /* get number of functions */
240 ori r12,r12,0
241
242 cmplw 0, r0, r12
243 bge 1f
244
245 rlwinm r0,r0,2,0,31 /* fn_addr = fn_tbl[r0] */
246 add r11,r11,r0
247 lwz r11,0(r11)
248
249 li r20,0xd00-4 /* Get stack pointer */
250 lwz r12,0(r20)
251 subi r12,r12,12 /* Adjust stack pointer */
252 li r0,0xc00+_end_back-SystemCall
253 cmplw 0, r0, r12 /* Check stack overflow */
254 bgt 1f
255 stw r12,0(r20)
256
257 mflr r0
258 stw r0,0(r12)
259 mfspr r0,SRR0
260 stw r0,4(r12)
261 mfspr r0,SRR1
262 stw r0,8(r12)
263
264 li r12,0xc00+_back-SystemCall
265 mtlr r12
266 mtspr SRR0,r11
267
2681: SYNC
269 rfi
270
271_back:
272
273 mfmsr r11 /* Disable interrupts */
274 li r12,0
275 ori r12,r12,MSR_EE
276 andc r11,r11,r12
277 SYNC /* Some chip revs need this... */
278 mtmsr r11
279 SYNC
280
281 li r12,0xd00-4 /* restore regs */
282 lwz r12,0(r12)
283
284 lwz r11,0(r12)
285 mtlr r11
286 lwz r11,4(r12)
287 mtspr SRR0,r11
288 lwz r11,8(r12)
289 mtspr SRR1,r11
290
291 addi r12,r12,12 /* Adjust stack pointer */
292 li r20,0xd00-4
293 stw r12,0(r20)
294
295 SYNC
296 rfi
297_end_back:
298
299 STD_EXCEPTION(0xd00, SingleStep, UnknownException)
300
301 STD_EXCEPTION(0xe00, Trap_0e, UnknownException)
302 STD_EXCEPTION(0xf00, Trap_0f, UnknownException)
303
304 /* On the MPC8xx, this is a software emulation interrupt. It occurs
305 * for all unimplemented and illegal instructions.
306 */
307 STD_EXCEPTION(0x1000, SoftEmu, SoftEmuException)
308 STD_EXCEPTION(0x1100, InstructionTLBMiss, UnknownException)
309 STD_EXCEPTION(0x1200, DataTLBMiss, UnknownException)
310 STD_EXCEPTION(0x1300, InstructionTLBError, UnknownException)
311 STD_EXCEPTION(0x1400, DataTLBError, UnknownException)
312
313 STD_EXCEPTION(0x1500, Reserved5, UnknownException)
314 STD_EXCEPTION(0x1600, Reserved6, UnknownException)
315 STD_EXCEPTION(0x1700, Reserved7, UnknownException)
316 STD_EXCEPTION(0x1800, Reserved8, UnknownException)
317 STD_EXCEPTION(0x1900, Reserved9, UnknownException)
318 STD_EXCEPTION(0x1a00, ReservedA, UnknownException)
319 STD_EXCEPTION(0x1b00, ReservedB, UnknownException)
320
321 STD_EXCEPTION(0x1c00, DataBreakpoint, UnknownException)
322 STD_EXCEPTION(0x1d00, InstructionBreakpoint, DebugException)
323 STD_EXCEPTION(0x1e00, PeripheralBreakpoint, UnknownException)
324 STD_EXCEPTION(0x1f00, DevPortBreakpoint, UnknownException)
325
326
327 .globl _end_of_vectors
328_end_of_vectors:
329
330
331 . = 0x2000
332
333/*
334 * This code finishes saving the registers to the exception frame
335 * and jumps to the appropriate handler for the exception.
336 * Register r21 is pointer into trap frame, r1 has new stack pointer.
337 */
338 .globl transfer_to_handler
339transfer_to_handler:
340 stw r22,_NIP(r21)
341 lis r22,MSR_POW@h
342 andc r23,r23,r22
343 stw r23,_MSR(r21)
344 SAVE_GPR(7, r21)
345 SAVE_4GPRS(8, r21)
346 SAVE_8GPRS(12, r21)
347 SAVE_8GPRS(24, r21)
348 mflr r23
349 andi. r24,r23,0x3f00 /* get vector offset */
350 stw r24,TRAP(r21)
351 li r22,0
352 stw r22,RESULT(r21)
353 mtspr SPRG2,r22 /* r1 is now kernel sp */
354 lwz r24,0(r23) /* virtual address of handler */
355 lwz r23,4(r23) /* where to go when done */
356 mtspr SRR0,r24
357 mtspr SRR1,r20
358 mtlr r23
359 SYNC
360 rfi /* jump to handler, enable MMU */
361
362int_return:
363 mfmsr r28 /* Disable interrupts */
364 li r4,0
365 ori r4,r4,MSR_EE
366 andc r28,r28,r4
367 SYNC /* Some chip revs need this... */
368 mtmsr r28
369 SYNC
370 lwz r2,_CTR(r1)
371 lwz r0,_LINK(r1)
372 mtctr r2
373 mtlr r0
374 lwz r2,_XER(r1)
375 lwz r0,_CCR(r1)
376 mtspr XER,r2
377 mtcrf 0xFF,r0
378 REST_10GPRS(3, r1)
379 REST_10GPRS(13, r1)
380 REST_8GPRS(23, r1)
381 REST_GPR(31, r1)
382 lwz r2,_NIP(r1) /* Restore environment */
383 lwz r0,_MSR(r1)
384 mtspr SRR0,r2
385 mtspr SRR1,r0
386 lwz r0,GPR0(r1)
387 lwz r2,GPR2(r1)
388 lwz r1,GPR1(r1)
389 SYNC
390 rfi
391
392
393/*
394 * unsigned int get_immr (unsigned int mask)
395 *
396 * return (mask ? (IMMR & mask) : IMMR);
397 */
398 .globl get_immr
399get_immr:
400 mr r4,r3 /* save mask */
401 mfspr r3, IMMR /* IMMR */
402 cmpwi 0,r4,0 /* mask != 0 ? */
403 beq 4f
404 and r3,r3,r4 /* IMMR & mask */
4054:
406 blr
407
408 .globl get_pvr
409get_pvr:
410 mfspr r3, PVR
411 blr
412
413
414/*------------------------------------------------------------------------------*/
415
416/*
417 * void relocate_code (addr_sp, gd, addr_moni)
418 *
419 * This "function" does not return, instead it continues in RAM
420 * after relocating the monitor code.
421 *
422 * r3 = dest
423 * r4 = src
424 * r5 = length in bytes
425 * r6 = cachelinesize
426 */
427 .globl relocate_code
428relocate_code:
429 mr r1, r3 /* Set new stack pointer in SRAM */
430 mr r9, r4 /* Save copy of global data pointer in SRAM */
431 mr r10, r5 /* Save copy of monitor destination Address in SRAM */
432
433 mr r3, r5 /* Destination Address */
434 lis r4, CFG_MONITOR_BASE@h /* Source Address */
435 ori r4, r4, CFG_MONITOR_BASE@l
wdenk3b57fe02003-05-30 12:48:29 +0000436 lwz r5, GOT(__init_end)
437 sub r5, r5, r4
wdenk0db5bca2003-03-31 17:27:09 +0000438
439 /*
440 * Fix GOT pointer:
441 *
442 * New GOT-PTR = (old GOT-PTR - CFG_MONITOR_BASE) + Destination Address
443 *
444 * Offset:
445 */
446 sub r15, r10, r4
447
448 /* First our own GOT */
449 add r14, r14, r15
450 /* the the one used by the C code */
451 add r30, r30, r15
452
453 /*
454 * Now relocate code
455 */
456
457 cmplw cr1,r3,r4
458 addi r0,r5,3
459 srwi. r0,r0,2
460 beq cr1,4f /* In place copy is not necessary */
461 beq 4f /* Protect against 0 count */
462 mtctr r0
463 bge cr1,2f
464
465 la r8,-4(r4)
466 la r7,-4(r3)
4671: lwzu r0,4(r8)
468 stwu r0,4(r7)
469 bdnz 1b
470 b 4f
471
4722: slwi r0,r0,2
473 add r8,r4,r0
474 add r7,r3,r0
4753: lwzu r0,-4(r8)
476 stwu r0,-4(r7)
477 bdnz 3b
478
4794: sync
480 isync
481
482/*
483 * We are done. Do not return, instead branch to second part of board
484 * initialization, now running from RAM.
485 */
486
487 addi r0, r10, in_ram - _start + EXC_OFF_SYS_RESET
488 mtlr r0
489 blr
490
491in_ram:
492
493 /*
494 * Relocation Function, r14 point to got2+0x8000
495 *
496 * Adjust got2 pointers, no need to check for 0, this code
497 * already puts a few entries in the table.
498 */
499 li r0,__got2_entries@sectoff@l
500 la r3,GOT(_GOT2_TABLE_)
501 lwz r11,GOT(_GOT2_TABLE_)
502 mtctr r0
503 sub r11,r3,r11
504 addi r3,r3,-4
5051: lwzu r0,4(r3)
506 add r0,r0,r11
507 stw r0,0(r3)
508 bdnz 1b
509
510 /*
511 * Now adjust the fixups and the pointers to the fixups
512 * in case we need to move ourselves again.
513 */
5142: li r0,__fixup_entries@sectoff@l
515 lwz r3,GOT(_FIXUP_TABLE_)
516 cmpwi r0,0
517 mtctr r0
518 addi r3,r3,-4
519 beq 4f
5203: lwzu r4,4(r3)
521 lwzux r0,r4,r11
522 add r0,r0,r11
523 stw r10,0(r3)
524 stw r0,0(r4)
525 bdnz 3b
5264:
527clear_bss:
528 /*
529 * Now clear BSS segment
530 */
wdenk5d232d02003-05-22 22:52:13 +0000531 lwz r3,GOT(__bss_start)
wdenk0db5bca2003-03-31 17:27:09 +0000532 lwz r4,GOT(_end)
533 cmplw 0, r3, r4
534 beq 6f
535
536 li r0, 0
5375:
538 stw r0, 0(r3)
539 addi r3, r3, 4
540 cmplw 0, r3, r4
541 bne 5b
5426:
543
544 mr r3, r9 /* Global Data pointer */
545 mr r4, r10 /* Destination Address */
546 bl board_init_r
547
wdenk0db5bca2003-03-31 17:27:09 +0000548 /*
549 * Copy exception vector code to low memory
550 *
551 * r3: dest_addr
552 * r7: source address, r8: end address, r9: target address
553 */
554 .globl trap_init
555trap_init:
556 lwz r7, GOT(_start)
557 lwz r8, GOT(_end_of_vectors)
558
559 rlwinm r9, r7, 0, 22, 31 /* _start & 0x3FF */
560
561 cmplw 0, r7, r8
562 bgelr /* return if r7>=r8 - just in case */
563
564 mflr r4 /* save link register */
5651:
566 lwz r0, 0(r7)
567 stw r0, 0(r9)
568 addi r7, r7, 4
569 addi r9, r9, 4
570 cmplw 0, r7, r8
571 bne 1b
572
573 /*
574 * relocate `hdlr' and `int_return' entries
575 */
576 li r7, .L_MachineCheck - _start + EXC_OFF_SYS_RESET
577 li r8, Alignment - _start + EXC_OFF_SYS_RESET
5782:
579 bl trap_reloc
580 addi r7, r7, 0x100 /* next exception vector */
581 cmplw 0, r7, r8
582 blt 2b
583
584 li r7, .L_Alignment - _start + EXC_OFF_SYS_RESET
585 bl trap_reloc
586
587 li r7, .L_ProgramCheck - _start + EXC_OFF_SYS_RESET
588 bl trap_reloc
589
590 li r7, .L_FPUnavailable - _start + EXC_OFF_SYS_RESET
591 li r8, SystemCall - _start + EXC_OFF_SYS_RESET
5923:
593 bl trap_reloc
594 addi r7, r7, 0x100 /* next exception vector */
595 cmplw 0, r7, r8
596 blt 3b
597
598 li r7, .L_SingleStep - _start + EXC_OFF_SYS_RESET
599 li r8, _end_of_vectors - _start + EXC_OFF_SYS_RESET
6004:
601 bl trap_reloc
602 addi r7, r7, 0x100 /* next exception vector */
603 cmplw 0, r7, r8
604 blt 4b
605
606 mtlr r4 /* restore link register */
607 blr
608
609 /*
610 * Function: relocate entries for one exception vector
611 */
612trap_reloc:
613 lwz r0, 0(r7) /* hdlr ... */
614 add r0, r0, r3 /* ... += dest_addr */
615 stw r0, 0(r7)
616
617 lwz r0, 4(r7) /* int_return ... */
618 add r0, r0, r3 /* ... += dest_addr */
619 stw r0, 4(r7)
620
621 sync
622 isync
623
624 blr