blob: 561c1f4795d7cacff637c603f4af23e8e28f9a43 [file] [log] [blame]
wdenkbd1575f2003-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 Denk0191e472010-10-26 14:34:52 +020030#include <asm-offsets.h>
wdenkbd1575f2003-10-14 19:43:55 +000031#include <config.h>
32#include <version.h>
33#include <asm/arch/ixp425.h>
34
wdenk9c53f402003-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 */
wdenkbd1575f2003-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 */
wdenk9c53f402003-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
wdenkbd1575f2003-10-14 19:43:55 +000059
wdenk9c53f402003-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
wdenkbd1575f2003-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 Schocherb2b37f02010-09-17 13:10:47 +020097.globl _TEXT_BASE
wdenkbd1575f2003-10-14 19:43:55 +000098_TEXT_BASE:
Wolfgang Denk0708bc62010-10-07 21:51:12 +020099 .word CONFIG_SYS_TEXT_BASE
wdenkbd1575f2003-10-14 19:43:55 +0000100
wdenkbd1575f2003-10-14 19:43:55 +0000101/*
wdenk927034e2004-02-08 19:38:38 +0000102 * These are defined in the board-specific linker script.
Albert Aribaud126897e2010-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.
wdenkbd1575f2003-10-14 19:43:55 +0000106 */
Albert Aribaud126897e2010-11-25 22:45:02 +0100107.globl _bss_start_ofs
108_bss_start_ofs:
109 .word __bss_start - _start
wdenkbd1575f2003-10-14 19:43:55 +0000110
Albert Aribaud126897e2010-11-25 22:45:02 +0100111.globl _bss_end_ofs
112_bss_end_ofs:
Po-Yu Chuangcedbf4b2011-03-01 22:59:59 +0000113 .word __bss_end__ - _start
wdenkbd1575f2003-10-14 19:43:55 +0000114
Po-Yu Chuang1864b002011-03-01 23:02:04 +0000115.globl _end_ofs
116_end_ofs:
117 .word _end - _start
118
wdenkbd1575f2003-10-14 19:43:55 +0000119#ifdef CONFIG_USE_IRQ
120/* IRQ stack memory (calculated at run-time) */
121.globl IRQ_STACK_START
122IRQ_STACK_START:
123 .word 0x0badc0de
124
125/* IRQ stack memory (calculated at run-time) */
126.globl FIQ_STACK_START
127FIQ_STACK_START:
128 .word 0x0badc0de
129#endif
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200130
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200131/* IRQ stack memory (calculated at run-time) + 8 bytes */
132.globl IRQ_STACK_START_IN
133IRQ_STACK_START_IN:
134 .word 0x0badc0de
135
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200136/*
137 * the actual reset code
138 */
139
140reset:
141 /* disable mmu, set big-endian */
142 mov r0, #0xf8
143 mcr p15, 0, r0, c1, c0, 0
144 CPWAIT r0
145
146 /* invalidate I & D caches & BTB */
147 mcr p15, 0, r0, c7, c7, 0
148 CPWAIT r0
149
150 /* invalidate I & Data TLB */
151 mcr p15, 0, r0, c8, c7, 0
152 CPWAIT r0
153
154 /* drain write and fill buffers */
155 mcr p15, 0, r0, c7, c10, 4
156 CPWAIT r0
157
158 /* disable write buffer coalescing */
159 mrc p15, 0, r0, c1, c0, 1
160 orr r0, r0, #1
161 mcr p15, 0, r0, c1, c0, 1
162 CPWAIT r0
163
164 /* set EXP CS0 to the optimum timing */
165 ldr r1, =CONFIG_SYS_EXP_CS0
166 ldr r2, =IXP425_EXP_CS0
167 str r1, [r2]
168
169 /* make sure flash is visible at 0 */
170#if 0
171 ldr r2, =IXP425_EXP_CFG0
172 ldr r1, [r2]
173 orr r1, r1, #0x80000000
174 str r1, [r2]
175#endif
176 mov r1, #CONFIG_SYS_SDR_CONFIG
177 ldr r2, =IXP425_SDR_CONFIG
178 str r1, [r2]
179
180 /* disable refresh cycles */
181 mov r1, #0
182 ldr r3, =IXP425_SDR_REFRESH
183 str r1, [r3]
184
185 /* send nop command */
186 mov r1, #3
187 ldr r4, =IXP425_SDR_IR
188 str r1, [r4]
189 DELAY_FOR 0x4000, r0
190
191 /* set SDRAM internal refresh val */
192 ldr r1, =CONFIG_SYS_SDRAM_REFRESH_CNT
193 str r1, [r3]
194 DELAY_FOR 0x4000, r0
195
196 /* send precharge-all command to close all open banks */
197 mov r1, #2
198 str r1, [r4]
199 DELAY_FOR 0x4000, r0
200
201 /* provide 8 auto-refresh cycles */
202 mov r1, #4
203 mov r5, #8
204111: str r1, [r4]
205 DELAY_FOR 0x100, r0
206 subs r5, r5, #1
207 bne 111b
wdenkbd1575f2003-10-14 19:43:55 +0000208
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200209 /* set mode register in sdram */
210 mov r1, #CONFIG_SYS_SDR_MODE_CONFIG
211 str r1, [r4]
212 DELAY_FOR 0x4000, r0
213
214 /* send normal operation command */
215 mov r1, #6
216 str r1, [r4]
217 DELAY_FOR 0x4000, r0
218
219 /* copy */
220 mov r0, #0
221 mov r4, r0
222 add r2, r0, #CONFIG_SYS_MONITOR_LEN
223 mov r1, #0x10000000
224 mov r5, r1
225
226 30:
227 ldr r3, [r0], #4
228 str r3, [r1], #4
229 cmp r0, r2
230 bne 30b
231
232 /* invalidate I & D caches & BTB */
233 mcr p15, 0, r0, c7, c7, 0
234 CPWAIT r0
235
236 /* invalidate I & Data TLB */
237 mcr p15, 0, r0, c8, c7, 0
238 CPWAIT r0
239
240 /* drain write and fill buffers */
241 mcr p15, 0, r0, c7, c10, 4
242 CPWAIT r0
243
244 /* move flash to 0x50000000 */
245 ldr r2, =IXP425_EXP_CFG0
246 ldr r1, [r2]
247 bic r1, r1, #0x80000000
248 str r1, [r2]
249
250 nop
251 nop
252 nop
253 nop
254 nop
255 nop
256
257 /* invalidate I & Data TLB */
258 mcr p15, 0, r0, c8, c7, 0
259 CPWAIT r0
260
261 /* enable I cache */
262 mrc p15, 0, r0, c1, c0, 0
263 orr r0, r0, #MMU_Control_I
264 mcr p15, 0, r0, c1, c0, 0
265 CPWAIT r0
266
267 mrs r0,cpsr /* set the cpu to SVC32 mode */
268 bic r0,r0,#0x1f /* (superviser mode, M=10011) */
269 orr r0,r0,#0x13
270 msr cpsr,r0
271
272/* Set stackpointer in internal RAM to call board_init_f */
273call_board_init_f:
274 ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
Heiko Schocher17f288a2010-11-12 07:53:55 +0100275 bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200276 ldr r0,=0x00000000
277 bl board_init_f
278
279/*------------------------------------------------------------------------------*/
280
281/*
282 * void relocate_code (addr_sp, gd, addr_moni)
283 *
284 * This "function" does not return, instead it continues in RAM
285 * after relocating the monitor code.
286 *
287 */
288 .globl relocate_code
289relocate_code:
290 mov r4, r0 /* save addr_sp */
291 mov r5, r1 /* save addr of gd */
292 mov r6, r2 /* save addr of destination */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200293
294 /* Set up the stack */
295stack_setup:
296 mov sp, r4
297
298 adr r0, _start
Andreas Bießmann007b38f2010-12-01 00:58:34 +0100299 cmp r0, r6
300 beq clear_bss /* skip relocation */
Andreas Bießmann8cfbda92010-12-01 00:58:33 +0100301 mov r1, r6 /* r1 <- scratch for copy_loop */
Albert Aribaud126897e2010-11-25 22:45:02 +0100302 ldr r3, _bss_start_ofs
303 add r2, r0, r3 /* r2 <- source end address */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200304
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200305copy_loop:
306 ldmia r0!, {r9-r10} /* copy from source address [r0] */
Andreas Bießmann8cfbda92010-12-01 00:58:33 +0100307 stmia r1!, {r9-r10} /* copy to target address [r1] */
Albert Aribaud0668d162010-10-05 16:06:39 +0200308 cmp r0, r2 /* until source end address [r2] */
309 blo copy_loop
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200310
311#ifndef CONFIG_PRELOADER
Albert Aribaud126897e2010-11-25 22:45:02 +0100312 /*
313 * fix .rel.dyn relocations
314 */
315 ldr r0, _TEXT_BASE /* r0 <- Text base */
Andreas Bießmann8cfbda92010-12-01 00:58:33 +0100316 sub r9, r6, r0 /* r9 <- relocation offset */
Albert Aribaud126897e2010-11-25 22:45:02 +0100317 ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
318 add r10, r10, r0 /* r10 <- sym table in FLASH */
319 ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
320 add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
321 ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
322 add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200323fixloop:
Albert Aribaud126897e2010-11-25 22:45:02 +0100324 ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
325 add r0, r0, r9 /* r0 <- location to fix up in RAM */
326 ldr r1, [r2, #4]
Andreas Bießmann318cea12010-12-01 00:58:35 +0100327 and r7, r1, #0xff
328 cmp r7, #23 /* relative fixup? */
Albert Aribaud126897e2010-11-25 22:45:02 +0100329 beq fixrel
Andreas Bießmann318cea12010-12-01 00:58:35 +0100330 cmp r7, #2 /* absolute fixup? */
Albert Aribaud126897e2010-11-25 22:45:02 +0100331 beq fixabs
332 /* ignore unknown type of fixup */
333 b fixnext
334fixabs:
335 /* absolute fix: set location to (offset) symbol value */
336 mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
337 add r1, r10, r1 /* r1 <- address of symbol in table */
338 ldr r1, [r1, #4] /* r1 <- symbol value */
Wolfgang Denk899cdd12010-12-09 11:26:24 +0100339 add r1, r1, r9 /* r1 <- relocated sym addr */
Albert Aribaud126897e2010-11-25 22:45:02 +0100340 b fixnext
341fixrel:
342 /* relative fix: increase location by offset */
343 ldr r1, [r0]
344 add r1, r1, r9
345fixnext:
346 str r1, [r0]
347 add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200348 cmp r2, r3
Wolfgang Denk98dd07c2010-10-23 23:22:38 +0200349 blo fixloop
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200350#endif
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200351
352clear_bss:
353#ifndef CONFIG_PRELOADER
Albert Aribaud126897e2010-11-25 22:45:02 +0100354 ldr r0, _bss_start_ofs
355 ldr r1, _bss_end_ofs
Andreas Bießmann8cfbda92010-12-01 00:58:33 +0100356 mov r4, r6 /* reloc addr */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200357 add r0, r0, r4
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200358 add r1, r1, r4
359 mov r2, #0x00000000 /* clear */
360
361clbss_l:str r2, [r0] /* clear loop... */
362 add r0, r0, #4
363 cmp r0, r1
364 bne clbss_l
365
366 bl coloured_LED_init
367 bl red_LED_on
368#endif
369
370/*
371 * We are done. Do not return, instead branch to second part of board
372 * initialization, now running from RAM.
373 */
Albert Aribaud126897e2010-11-25 22:45:02 +0100374 ldr r0, _board_init_r_ofs
375 adr r1, _start
376 add lr, r0, r1
377 add lr, lr, r9
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200378 /* setup parameters for board_init_r */
379 mov r0, r5 /* gd_t */
Andreas Bießmann8cfbda92010-12-01 00:58:33 +0100380 mov r1, r6 /* dest_addr */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200381 /* jump to it ... */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200382 mov pc, lr
383
Albert Aribaud126897e2010-11-25 22:45:02 +0100384_board_init_r_ofs:
385 .word board_init_r - _start
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200386
Albert Aribaud126897e2010-11-25 22:45:02 +0100387_rel_dyn_start_ofs:
388 .word __rel_dyn_start - _start
389_rel_dyn_end_ofs:
390 .word __rel_dyn_end - _start
391_dynsym_start_ofs:
392 .word __dynsym_start - _start
wdenkbd1575f2003-10-14 19:43:55 +0000393
wdenkbd1575f2003-10-14 19:43:55 +0000394/****************************************************************************/
395/* */
396/* Interrupt handling */
397/* */
398/****************************************************************************/
399
400/* IRQ stack frame */
401
402#define S_FRAME_SIZE 72
403
404#define S_OLD_R0 68
405#define S_PSR 64
406#define S_PC 60
407#define S_LR 56
408#define S_SP 52
409
410#define S_IP 48
411#define S_FP 44
412#define S_R10 40
413#define S_R9 36
414#define S_R8 32
415#define S_R7 28
416#define S_R6 24
417#define S_R5 20
418#define S_R4 16
419#define S_R3 12
420#define S_R2 8
421#define S_R1 4
422#define S_R0 0
423
424#define MODE_SVC 0x13
425
426 /* use bad_save_user_regs for abort/prefetch/undef/swi ... */
427
428 .macro bad_save_user_regs
429 sub sp, sp, #S_FRAME_SIZE
430 stmia sp, {r0 - r12} /* Calling r0-r12 */
431 add r8, sp, #S_PC
432
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200433 ldr r2, IRQ_STACK_START_IN
wdenkbd1575f2003-10-14 19:43:55 +0000434 ldmia r2, {r2 - r4} /* get pc, cpsr, old_r0 */
435 add r0, sp, #S_FRAME_SIZE /* restore sp_SVC */
436
437 add r5, sp, #S_SP
438 mov r1, lr
439 stmia r5, {r0 - r4} /* save sp_SVC, lr_SVC, pc, cpsr, old_r */
440 mov r0, sp
441 .endm
442
443
444 /* use irq_save_user_regs / irq_restore_user_regs for */
445 /* IRQ/FIQ handling */
446
447 .macro irq_save_user_regs
448 sub sp, sp, #S_FRAME_SIZE
449 stmia sp, {r0 - r12} /* Calling r0-r12 */
450 add r8, sp, #S_PC
451 stmdb r8, {sp, lr}^ /* Calling SP, LR */
452 str lr, [r8, #0] /* Save calling PC */
453 mrs r6, spsr
454 str r6, [r8, #4] /* Save CPSR */
455 str r0, [r8, #8] /* Save OLD_R0 */
456 mov r0, sp
457 .endm
458
459 .macro irq_restore_user_regs
460 ldmia sp, {r0 - lr}^ @ Calling r0 - lr
461 mov r0, r0
462 ldr lr, [sp, #S_PC] @ Get PC
463 add sp, sp, #S_FRAME_SIZE
464 subs pc, lr, #4 @ return & move spsr_svc into cpsr
465 .endm
466
467 .macro get_bad_stack
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200468 ldr r13, IRQ_STACK_START_IN @ setup our mode stack
wdenkbd1575f2003-10-14 19:43:55 +0000469
470 str lr, [r13] @ save caller lr / spsr
471 mrs lr, spsr
472 str lr, [r13, #4]
473
474 mov r13, #MODE_SVC @ prepare SVC-Mode
475 msr spsr_c, r13
476 mov lr, pc
477 movs pc, lr
478 .endm
479
480 .macro get_irq_stack @ setup IRQ stack
481 ldr sp, IRQ_STACK_START
482 .endm
483
484 .macro get_fiq_stack @ setup FIQ stack
485 ldr sp, FIQ_STACK_START
486 .endm
487
488
489/****************************************************************************/
490/* */
491/* exception handlers */
492/* */
493/****************************************************************************/
494
495 .align 5
496undefined_instruction:
497 get_bad_stack
498 bad_save_user_regs
499 bl do_undefined_instruction
500
501 .align 5
502software_interrupt:
503 get_bad_stack
504 bad_save_user_regs
505 bl do_software_interrupt
506
507 .align 5
508prefetch_abort:
509 get_bad_stack
510 bad_save_user_regs
511 bl do_prefetch_abort
512
513 .align 5
514data_abort:
515 get_bad_stack
516 bad_save_user_regs
517 bl do_data_abort
518
519 .align 5
520not_used:
521 get_bad_stack
522 bad_save_user_regs
523 bl do_not_used
524
525#ifdef CONFIG_USE_IRQ
526
527 .align 5
528irq:
529 get_irq_stack
530 irq_save_user_regs
531 bl do_irq
532 irq_restore_user_regs
533
534 .align 5
535fiq:
536 get_fiq_stack
537 irq_save_user_regs /* someone ought to write a more */
538 bl do_fiq /* effiction fiq_save_user_regs */
539 irq_restore_user_regs
540
541#else
542
543 .align 5
544irq:
545 get_bad_stack
546 bad_save_user_regs
547 bl do_irq
548
549 .align 5
550fiq:
551 get_bad_stack
552 bad_save_user_regs
553 bl do_fiq
554
555#endif
556
557/****************************************************************************/
558/* */
559/* Reset function: Use Watchdog to reset */
560/* */
561/****************************************************************************/
562
563 .align 5
564.globl reset_cpu
565
566reset_cpu:
Wolfgang Denka1be4762008-05-20 16:00:29 +0200567 ldr r1, =0x482e
wdenkbd1575f2003-10-14 19:43:55 +0000568 ldr r2, =IXP425_OSWK
569 str r1, [r2]
Wolfgang Denka1be4762008-05-20 16:00:29 +0200570 ldr r1, =0x0fff
wdenkbd1575f2003-10-14 19:43:55 +0000571 ldr r2, =IXP425_OSWT
572 str r1, [r2]
Wolfgang Denka1be4762008-05-20 16:00:29 +0200573 ldr r1, =0x5
wdenkbd1575f2003-10-14 19:43:55 +0000574 ldr r2, =IXP425_OSWE
575 str r1, [r2]
576 b reset_endless
577
578
579reset_endless:
580
581 b reset_endless
Wolfgang Denk4646d2a2006-05-30 15:56:48 +0200582
583#ifdef CONFIG_USE_IRQ
584
585.LC0: .word loops_per_jiffy
586
587/*
588 * 0 <= r0 <= 2000
589 */
Ingo van Lilf0f778a2009-11-24 14:09:21 +0100590.globl __udelay
591__udelay:
Wolfgang Denk4646d2a2006-05-30 15:56:48 +0200592 mov r2, #0x6800
593 orr r2, r2, #0x00db
594 mul r0, r2, r0
595 ldr r2, .LC0
596 ldr r2, [r2] @ max = 0x0fffffff
597 mov r0, r0, lsr #11 @ max = 0x00003fff
598 mov r2, r2, lsr #11 @ max = 0x0003ffff
599 mul r0, r2, r0 @ max = 2^32-1
600 movs r0, r0, lsr #6
601
602delay_loop:
603 subs r0, r0, #1
604 bne delay_loop
605 mov pc, lr
606
607#endif /* CONFIG_USE_IRQ */