blob: f8bfed7cc61074962f53389d2d4b4437e639b44c [file] [log] [blame]
wdenk2d5b5612003-10-14 19:43:55 +00001/* vi: set ts=8 sw=8 noet: */
2/*
3 * u-boot - Startup Code for XScale IXP
4 *
5 * Copyright (C) 2003 Kyle Harris <kharris@nexus-tech.net>
6 *
7 * Based on startup code example contained in the
8 * Intel IXP4xx Programmer's Guide and past u-boot Start.S
9 * samples.
10 *
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 */
29
Wolfgang Denk25ddd1f2010-10-26 14:34:52 +020030#include <asm-offsets.h>
wdenk2d5b5612003-10-14 19:43:55 +000031#include <config.h>
32#include <version.h>
33#include <asm/arch/ixp425.h>
34
wdenk42d1f032003-10-15 23:53:47 +000035#define MMU_Control_M 0x001 /* Enable MMU */
36#define MMU_Control_A 0x002 /* Enable address alignment faults */
37#define MMU_Control_C 0x004 /* Enable cache */
38#define MMU_Control_W 0x008 /* Enable write-buffer */
39#define MMU_Control_P 0x010 /* Compatability: 32 bit code */
40#define MMU_Control_D 0x020 /* Compatability: 32 bit data */
41#define MMU_Control_L 0x040 /* Compatability: */
42#define MMU_Control_B 0x080 /* Enable Big-Endian */
43#define MMU_Control_S 0x100 /* Enable system protection */
44#define MMU_Control_R 0x200 /* Enable ROM protection */
45#define MMU_Control_I 0x1000 /* Enable Instruction cache */
46#define MMU_Control_X 0x2000 /* Set interrupt vectors at 0xFFFF0000 */
wdenk2d5b5612003-10-14 19:43:55 +000047#define MMU_Control_Init (MMU_Control_P|MMU_Control_D|MMU_Control_L)
48
49
50/*
51 * Macro definitions
52 */
wdenk42d1f032003-10-15 23:53:47 +000053 /* Delay a bit */
54 .macro DELAY_FOR cycles, reg0
55 ldr \reg0, =\cycles
56 subs \reg0, \reg0, #1
57 subne pc, pc, #0xc
58 .endm
wdenk2d5b5612003-10-14 19:43:55 +000059
wdenk42d1f032003-10-15 23:53:47 +000060 /* wait for coprocessor write complete */
61 .macro CPWAIT reg
62 mrc p15,0,\reg,c2,c0,0
63 mov \reg,\reg
64 sub pc,pc,#4
65 .endm
wdenk2d5b5612003-10-14 19:43:55 +000066
67.globl _start
68_start: b reset
69 ldr pc, _undefined_instruction
70 ldr pc, _software_interrupt
71 ldr pc, _prefetch_abort
72 ldr pc, _data_abort
73 ldr pc, _not_used
74 ldr pc, _irq
75 ldr pc, _fiq
76
77_undefined_instruction: .word undefined_instruction
78_software_interrupt: .word software_interrupt
79_prefetch_abort: .word prefetch_abort
80_data_abort: .word data_abort
81_not_used: .word not_used
82_irq: .word irq
83_fiq: .word fiq
84
85 .balignl 16,0xdeadbeef
86
87
88/*
89 * Startup Code (reset vector)
90 *
91 * do important init only if we don't start from memory!
92 * - relocate armboot to ram
93 * - setup stack
94 * - jump to second stage
95 */
96
Heiko Schocher2af0a092010-09-17 13:10:47 +020097.globl _TEXT_BASE
wdenk2d5b5612003-10-14 19:43:55 +000098_TEXT_BASE:
Wolfgang Denk14d0a022010-10-07 21:51:12 +020099 .word CONFIG_SYS_TEXT_BASE
wdenk2d5b5612003-10-14 19:43:55 +0000100
wdenk2d5b5612003-10-14 19:43:55 +0000101/*
wdenkf6e20fc2004-02-08 19:38:38 +0000102 * These are defined in the board-specific linker script.
Albert Aribaud3336ca62010-11-25 22:45:02 +0100103 * Subtracting _start from them lets the linker put their
104 * relative position in the executable instead of leaving
105 * them null.
wdenk2d5b5612003-10-14 19:43:55 +0000106 */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100107.globl _bss_start_ofs
108_bss_start_ofs:
109 .word __bss_start - _start
wdenk2d5b5612003-10-14 19:43:55 +0000110
Albert Aribaud3336ca62010-11-25 22:45:02 +0100111.globl _bss_end_ofs
112_bss_end_ofs:
Po-Yu Chuang44c6e652011-03-01 22:59:59 +0000113 .word __bss_end__ - _start
wdenk2d5b5612003-10-14 19:43:55 +0000114
115#ifdef CONFIG_USE_IRQ
116/* IRQ stack memory (calculated at run-time) */
117.globl IRQ_STACK_START
118IRQ_STACK_START:
119 .word 0x0badc0de
120
121/* IRQ stack memory (calculated at run-time) */
122.globl FIQ_STACK_START
123FIQ_STACK_START:
124 .word 0x0badc0de
125#endif
126
Heiko Schocher2af0a092010-09-17 13:10:47 +0200127/* IRQ stack memory (calculated at run-time) + 8 bytes */
128.globl IRQ_STACK_START_IN
129IRQ_STACK_START_IN:
130 .word 0x0badc0de
131
Heiko Schocher2af0a092010-09-17 13:10:47 +0200132/*
133 * the actual reset code
134 */
135
136reset:
137 /* disable mmu, set big-endian */
138 mov r0, #0xf8
139 mcr p15, 0, r0, c1, c0, 0
140 CPWAIT r0
141
142 /* invalidate I & D caches & BTB */
143 mcr p15, 0, r0, c7, c7, 0
144 CPWAIT r0
145
146 /* invalidate I & Data TLB */
147 mcr p15, 0, r0, c8, c7, 0
148 CPWAIT r0
149
150 /* drain write and fill buffers */
151 mcr p15, 0, r0, c7, c10, 4
152 CPWAIT r0
153
154 /* disable write buffer coalescing */
155 mrc p15, 0, r0, c1, c0, 1
156 orr r0, r0, #1
157 mcr p15, 0, r0, c1, c0, 1
158 CPWAIT r0
159
160 /* set EXP CS0 to the optimum timing */
161 ldr r1, =CONFIG_SYS_EXP_CS0
162 ldr r2, =IXP425_EXP_CS0
163 str r1, [r2]
164
165 /* make sure flash is visible at 0 */
166#if 0
167 ldr r2, =IXP425_EXP_CFG0
168 ldr r1, [r2]
169 orr r1, r1, #0x80000000
170 str r1, [r2]
171#endif
172 mov r1, #CONFIG_SYS_SDR_CONFIG
173 ldr r2, =IXP425_SDR_CONFIG
174 str r1, [r2]
175
176 /* disable refresh cycles */
177 mov r1, #0
178 ldr r3, =IXP425_SDR_REFRESH
179 str r1, [r3]
180
181 /* send nop command */
182 mov r1, #3
183 ldr r4, =IXP425_SDR_IR
184 str r1, [r4]
185 DELAY_FOR 0x4000, r0
186
187 /* set SDRAM internal refresh val */
188 ldr r1, =CONFIG_SYS_SDRAM_REFRESH_CNT
189 str r1, [r3]
190 DELAY_FOR 0x4000, r0
191
192 /* send precharge-all command to close all open banks */
193 mov r1, #2
194 str r1, [r4]
195 DELAY_FOR 0x4000, r0
196
197 /* provide 8 auto-refresh cycles */
198 mov r1, #4
199 mov r5, #8
200111: str r1, [r4]
201 DELAY_FOR 0x100, r0
202 subs r5, r5, #1
203 bne 111b
204
205 /* set mode register in sdram */
206 mov r1, #CONFIG_SYS_SDR_MODE_CONFIG
207 str r1, [r4]
208 DELAY_FOR 0x4000, r0
209
210 /* send normal operation command */
211 mov r1, #6
212 str r1, [r4]
213 DELAY_FOR 0x4000, r0
214
215 /* copy */
216 mov r0, #0
217 mov r4, r0
218 add r2, r0, #CONFIG_SYS_MONITOR_LEN
219 mov r1, #0x10000000
220 mov r5, r1
221
222 30:
223 ldr r3, [r0], #4
224 str r3, [r1], #4
225 cmp r0, r2
226 bne 30b
227
228 /* invalidate I & D caches & BTB */
229 mcr p15, 0, r0, c7, c7, 0
230 CPWAIT r0
231
232 /* invalidate I & Data TLB */
233 mcr p15, 0, r0, c8, c7, 0
234 CPWAIT r0
235
236 /* drain write and fill buffers */
237 mcr p15, 0, r0, c7, c10, 4
238 CPWAIT r0
239
240 /* move flash to 0x50000000 */
241 ldr r2, =IXP425_EXP_CFG0
242 ldr r1, [r2]
243 bic r1, r1, #0x80000000
244 str r1, [r2]
245
246 nop
247 nop
248 nop
249 nop
250 nop
251 nop
252
253 /* invalidate I & Data TLB */
254 mcr p15, 0, r0, c8, c7, 0
255 CPWAIT r0
256
257 /* enable I cache */
258 mrc p15, 0, r0, c1, c0, 0
259 orr r0, r0, #MMU_Control_I
260 mcr p15, 0, r0, c1, c0, 0
261 CPWAIT r0
262
263 mrs r0,cpsr /* set the cpu to SVC32 mode */
264 bic r0,r0,#0x1f /* (superviser mode, M=10011) */
265 orr r0,r0,#0x13
266 msr cpsr,r0
267
268/* Set stackpointer in internal RAM to call board_init_f */
269call_board_init_f:
270 ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
Heiko Schocher296cae72010-11-12 07:53:55 +0100271 bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200272 ldr r0,=0x00000000
273 bl board_init_f
274
275/*------------------------------------------------------------------------------*/
276
277/*
278 * void relocate_code (addr_sp, gd, addr_moni)
279 *
280 * This "function" does not return, instead it continues in RAM
281 * after relocating the monitor code.
282 *
283 */
284 .globl relocate_code
285relocate_code:
286 mov r4, r0 /* save addr_sp */
287 mov r5, r1 /* save addr of gd */
288 mov r6, r2 /* save addr of destination */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200289
290 /* Set up the stack */
291stack_setup:
292 mov sp, r4
293
294 adr r0, _start
Andreas Bießmanna1a47d32010-12-01 00:58:34 +0100295 cmp r0, r6
296 beq clear_bss /* skip relocation */
Andreas Bießmanna78fb682010-12-01 00:58:33 +0100297 mov r1, r6 /* r1 <- scratch for copy_loop */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100298 ldr r3, _bss_start_ofs
299 add r2, r0, r3 /* r2 <- source end address */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200300
Heiko Schocher2af0a092010-09-17 13:10:47 +0200301copy_loop:
302 ldmia r0!, {r9-r10} /* copy from source address [r0] */
Andreas Bießmanna78fb682010-12-01 00:58:33 +0100303 stmia r1!, {r9-r10} /* copy to target address [r1] */
Albert Aribaudda90d4c2010-10-05 16:06:39 +0200304 cmp r0, r2 /* until source end address [r2] */
305 blo copy_loop
Heiko Schocher2af0a092010-09-17 13:10:47 +0200306
307#ifndef CONFIG_PRELOADER
Albert Aribaud3336ca62010-11-25 22:45:02 +0100308 /*
309 * fix .rel.dyn relocations
310 */
311 ldr r0, _TEXT_BASE /* r0 <- Text base */
Andreas Bießmanna78fb682010-12-01 00:58:33 +0100312 sub r9, r6, r0 /* r9 <- relocation offset */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100313 ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
314 add r10, r10, r0 /* r10 <- sym table in FLASH */
315 ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
316 add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
317 ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
318 add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200319fixloop:
Albert Aribaud3336ca62010-11-25 22:45:02 +0100320 ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
321 add r0, r0, r9 /* r0 <- location to fix up in RAM */
322 ldr r1, [r2, #4]
Andreas Bießmann1f52d892010-12-01 00:58:35 +0100323 and r7, r1, #0xff
324 cmp r7, #23 /* relative fixup? */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100325 beq fixrel
Andreas Bießmann1f52d892010-12-01 00:58:35 +0100326 cmp r7, #2 /* absolute fixup? */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100327 beq fixabs
328 /* ignore unknown type of fixup */
329 b fixnext
330fixabs:
331 /* absolute fix: set location to (offset) symbol value */
332 mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
333 add r1, r10, r1 /* r1 <- address of symbol in table */
334 ldr r1, [r1, #4] /* r1 <- symbol value */
Wolfgang Denk36009452010-12-09 11:26:24 +0100335 add r1, r1, r9 /* r1 <- relocated sym addr */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100336 b fixnext
337fixrel:
338 /* relative fix: increase location by offset */
339 ldr r1, [r0]
340 add r1, r1, r9
341fixnext:
342 str r1, [r0]
343 add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200344 cmp r2, r3
Wolfgang Denk79e63132010-10-23 23:22:38 +0200345 blo fixloop
Heiko Schocher2af0a092010-09-17 13:10:47 +0200346#endif
Heiko Schocher2af0a092010-09-17 13:10:47 +0200347
348clear_bss:
349#ifndef CONFIG_PRELOADER
Albert Aribaud3336ca62010-11-25 22:45:02 +0100350 ldr r0, _bss_start_ofs
351 ldr r1, _bss_end_ofs
Andreas Bießmanna78fb682010-12-01 00:58:33 +0100352 mov r4, r6 /* reloc addr */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200353 add r0, r0, r4
Heiko Schocher2af0a092010-09-17 13:10:47 +0200354 add r1, r1, r4
355 mov r2, #0x00000000 /* clear */
356
357clbss_l:str r2, [r0] /* clear loop... */
358 add r0, r0, #4
359 cmp r0, r1
360 bne clbss_l
361
362 bl coloured_LED_init
363 bl red_LED_on
364#endif
365
366/*
367 * We are done. Do not return, instead branch to second part of board
368 * initialization, now running from RAM.
369 */
Albert Aribaud3336ca62010-11-25 22:45:02 +0100370 ldr r0, _board_init_r_ofs
371 adr r1, _start
372 add lr, r0, r1
373 add lr, lr, r9
Heiko Schocher2af0a092010-09-17 13:10:47 +0200374 /* setup parameters for board_init_r */
375 mov r0, r5 /* gd_t */
Andreas Bießmanna78fb682010-12-01 00:58:33 +0100376 mov r1, r6 /* dest_addr */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200377 /* jump to it ... */
Heiko Schocher2af0a092010-09-17 13:10:47 +0200378 mov pc, lr
379
Albert Aribaud3336ca62010-11-25 22:45:02 +0100380_board_init_r_ofs:
381 .word board_init_r - _start
Heiko Schocher2af0a092010-09-17 13:10:47 +0200382
Albert Aribaud3336ca62010-11-25 22:45:02 +0100383_rel_dyn_start_ofs:
384 .word __rel_dyn_start - _start
385_rel_dyn_end_ofs:
386 .word __rel_dyn_end - _start
387_dynsym_start_ofs:
388 .word __dynsym_start - _start
wdenk2d5b5612003-10-14 19:43:55 +0000389
wdenk2d5b5612003-10-14 19:43:55 +0000390/****************************************************************************/
391/* */
392/* Interrupt handling */
393/* */
394/****************************************************************************/
395
396/* IRQ stack frame */
397
398#define S_FRAME_SIZE 72
399
400#define S_OLD_R0 68
401#define S_PSR 64
402#define S_PC 60
403#define S_LR 56
404#define S_SP 52
405
406#define S_IP 48
407#define S_FP 44
408#define S_R10 40
409#define S_R9 36
410#define S_R8 32
411#define S_R7 28
412#define S_R6 24
413#define S_R5 20
414#define S_R4 16
415#define S_R3 12
416#define S_R2 8
417#define S_R1 4
418#define S_R0 0
419
420#define MODE_SVC 0x13
421
422 /* use bad_save_user_regs for abort/prefetch/undef/swi ... */
423
424 .macro bad_save_user_regs
425 sub sp, sp, #S_FRAME_SIZE
426 stmia sp, {r0 - r12} /* Calling r0-r12 */
427 add r8, sp, #S_PC
428
Heiko Schocher2af0a092010-09-17 13:10:47 +0200429 ldr r2, IRQ_STACK_START_IN
wdenk2d5b5612003-10-14 19:43:55 +0000430 ldmia r2, {r2 - r4} /* get pc, cpsr, old_r0 */
431 add r0, sp, #S_FRAME_SIZE /* restore sp_SVC */
432
433 add r5, sp, #S_SP
434 mov r1, lr
435 stmia r5, {r0 - r4} /* save sp_SVC, lr_SVC, pc, cpsr, old_r */
436 mov r0, sp
437 .endm
438
439
440 /* use irq_save_user_regs / irq_restore_user_regs for */
441 /* IRQ/FIQ handling */
442
443 .macro irq_save_user_regs
444 sub sp, sp, #S_FRAME_SIZE
445 stmia sp, {r0 - r12} /* Calling r0-r12 */
446 add r8, sp, #S_PC
447 stmdb r8, {sp, lr}^ /* Calling SP, LR */
448 str lr, [r8, #0] /* Save calling PC */
449 mrs r6, spsr
450 str r6, [r8, #4] /* Save CPSR */
451 str r0, [r8, #8] /* Save OLD_R0 */
452 mov r0, sp
453 .endm
454
455 .macro irq_restore_user_regs
456 ldmia sp, {r0 - lr}^ @ Calling r0 - lr
457 mov r0, r0
458 ldr lr, [sp, #S_PC] @ Get PC
459 add sp, sp, #S_FRAME_SIZE
460 subs pc, lr, #4 @ return & move spsr_svc into cpsr
461 .endm
462
463 .macro get_bad_stack
Heiko Schocher2af0a092010-09-17 13:10:47 +0200464 ldr r13, IRQ_STACK_START_IN @ setup our mode stack
wdenk2d5b5612003-10-14 19:43:55 +0000465
466 str lr, [r13] @ save caller lr / spsr
467 mrs lr, spsr
468 str lr, [r13, #4]
469
470 mov r13, #MODE_SVC @ prepare SVC-Mode
471 msr spsr_c, r13
472 mov lr, pc
473 movs pc, lr
474 .endm
475
476 .macro get_irq_stack @ setup IRQ stack
477 ldr sp, IRQ_STACK_START
478 .endm
479
480 .macro get_fiq_stack @ setup FIQ stack
481 ldr sp, FIQ_STACK_START
482 .endm
483
484
485/****************************************************************************/
486/* */
487/* exception handlers */
488/* */
489/****************************************************************************/
490
491 .align 5
492undefined_instruction:
493 get_bad_stack
494 bad_save_user_regs
495 bl do_undefined_instruction
496
497 .align 5
498software_interrupt:
499 get_bad_stack
500 bad_save_user_regs
501 bl do_software_interrupt
502
503 .align 5
504prefetch_abort:
505 get_bad_stack
506 bad_save_user_regs
507 bl do_prefetch_abort
508
509 .align 5
510data_abort:
511 get_bad_stack
512 bad_save_user_regs
513 bl do_data_abort
514
515 .align 5
516not_used:
517 get_bad_stack
518 bad_save_user_regs
519 bl do_not_used
520
521#ifdef CONFIG_USE_IRQ
522
523 .align 5
524irq:
525 get_irq_stack
526 irq_save_user_regs
527 bl do_irq
528 irq_restore_user_regs
529
530 .align 5
531fiq:
532 get_fiq_stack
533 irq_save_user_regs /* someone ought to write a more */
534 bl do_fiq /* effiction fiq_save_user_regs */
535 irq_restore_user_regs
536
537#else
538
539 .align 5
540irq:
541 get_bad_stack
542 bad_save_user_regs
543 bl do_irq
544
545 .align 5
546fiq:
547 get_bad_stack
548 bad_save_user_regs
549 bl do_fiq
550
551#endif
552
553/****************************************************************************/
554/* */
555/* Reset function: Use Watchdog to reset */
556/* */
557/****************************************************************************/
558
559 .align 5
560.globl reset_cpu
561
562reset_cpu:
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200563 ldr r1, =0x482e
wdenk2d5b5612003-10-14 19:43:55 +0000564 ldr r2, =IXP425_OSWK
565 str r1, [r2]
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200566 ldr r1, =0x0fff
wdenk2d5b5612003-10-14 19:43:55 +0000567 ldr r2, =IXP425_OSWT
568 str r1, [r2]
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200569 ldr r1, =0x5
wdenk2d5b5612003-10-14 19:43:55 +0000570 ldr r2, =IXP425_OSWE
571 str r1, [r2]
572 b reset_endless
573
574
575reset_endless:
576
577 b reset_endless
Wolfgang Denkba94a1b2006-05-30 15:56:48 +0200578
579#ifdef CONFIG_USE_IRQ
580
581.LC0: .word loops_per_jiffy
582
583/*
584 * 0 <= r0 <= 2000
585 */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100586.globl __udelay
587__udelay:
Wolfgang Denkba94a1b2006-05-30 15:56:48 +0200588 mov r2, #0x6800
589 orr r2, r2, #0x00db
590 mul r0, r2, r0
591 ldr r2, .LC0
592 ldr r2, [r2] @ max = 0x0fffffff
593 mov r0, r0, lsr #11 @ max = 0x00003fff
594 mov r2, r2, lsr #11 @ max = 0x0003ffff
595 mul r0, r2, r0 @ max = 2^32-1
596 movs r0, r0, lsr #6
597
598delay_loop:
599 subs r0, r0, #1
600 bne delay_loop
601 mov pc, lr
602
603#endif /* CONFIG_USE_IRQ */