blob: 69ef8aa61fa79ffe56a5bfc9068f1c11eca1ee6a [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
Michael Schwingen7ddf55e2011-05-23 00:00:00 +020068_start:
69 ldr pc, _reset
wdenkbd1575f2003-10-14 19:43:55 +000070 ldr pc, _undefined_instruction
71 ldr pc, _software_interrupt
72 ldr pc, _prefetch_abort
73 ldr pc, _data_abort
74 ldr pc, _not_used
75 ldr pc, _irq
76 ldr pc, _fiq
77
Michael Schwingen7ddf55e2011-05-23 00:00:00 +020078_reset: .word reset
wdenkbd1575f2003-10-14 19:43:55 +000079_undefined_instruction: .word undefined_instruction
80_software_interrupt: .word software_interrupt
81_prefetch_abort: .word prefetch_abort
82_data_abort: .word data_abort
83_not_used: .word not_used
84_irq: .word irq
85_fiq: .word fiq
86
87 .balignl 16,0xdeadbeef
88
89
90/*
91 * Startup Code (reset vector)
92 *
93 * do important init only if we don't start from memory!
94 * - relocate armboot to ram
95 * - setup stack
96 * - jump to second stage
97 */
98
Heiko Schocherb2b37f02010-09-17 13:10:47 +020099.globl _TEXT_BASE
wdenkbd1575f2003-10-14 19:43:55 +0000100_TEXT_BASE:
Benoît Thébaudeaua402da32013-04-11 09:35:42 +0000101#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_TEXT_BASE)
102 .word CONFIG_SPL_TEXT_BASE
103#else
Wolfgang Denk0708bc62010-10-07 21:51:12 +0200104 .word CONFIG_SYS_TEXT_BASE
Benoît Thébaudeaua402da32013-04-11 09:35:42 +0000105#endif
wdenkbd1575f2003-10-14 19:43:55 +0000106
wdenkbd1575f2003-10-14 19:43:55 +0000107/*
wdenk927034e2004-02-08 19:38:38 +0000108 * These are defined in the board-specific linker script.
Albert Aribaud126897e2010-11-25 22:45:02 +0100109 * Subtracting _start from them lets the linker put their
110 * relative position in the executable instead of leaving
111 * them null.
wdenkbd1575f2003-10-14 19:43:55 +0000112 */
Albert Aribaud126897e2010-11-25 22:45:02 +0100113.globl _bss_start_ofs
114_bss_start_ofs:
115 .word __bss_start - _start
wdenkbd1575f2003-10-14 19:43:55 +0000116
Benoît Thébaudeau03bae032013-04-11 09:35:46 +0000117.globl _image_copy_end_ofs
118_image_copy_end_ofs:
119 .word __image_copy_end - _start
120
Albert Aribaud126897e2010-11-25 22:45:02 +0100121.globl _bss_end_ofs
122_bss_end_ofs:
Simon Glassed70c8f2013-03-14 06:54:53 +0000123 .word __bss_end - _start
wdenkbd1575f2003-10-14 19:43:55 +0000124
Po-Yu Chuang1864b002011-03-01 23:02:04 +0000125.globl _end_ofs
126_end_ofs:
127 .word _end - _start
128
wdenkbd1575f2003-10-14 19:43:55 +0000129#ifdef CONFIG_USE_IRQ
130/* IRQ stack memory (calculated at run-time) */
131.globl IRQ_STACK_START
132IRQ_STACK_START:
133 .word 0x0badc0de
134
135/* IRQ stack memory (calculated at run-time) */
136.globl FIQ_STACK_START
137FIQ_STACK_START:
138 .word 0x0badc0de
139#endif
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200140
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200141/* IRQ stack memory (calculated at run-time) + 8 bytes */
142.globl IRQ_STACK_START_IN
143IRQ_STACK_START_IN:
144 .word 0x0badc0de
145
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200146/*
147 * the actual reset code
148 */
149
150reset:
151 /* disable mmu, set big-endian */
152 mov r0, #0xf8
153 mcr p15, 0, r0, c1, c0, 0
154 CPWAIT r0
155
156 /* invalidate I & D caches & BTB */
157 mcr p15, 0, r0, c7, c7, 0
158 CPWAIT r0
159
160 /* invalidate I & Data TLB */
161 mcr p15, 0, r0, c8, c7, 0
162 CPWAIT r0
163
164 /* drain write and fill buffers */
165 mcr p15, 0, r0, c7, c10, 4
166 CPWAIT r0
167
168 /* disable write buffer coalescing */
169 mrc p15, 0, r0, c1, c0, 1
170 orr r0, r0, #1
171 mcr p15, 0, r0, c1, c0, 1
172 CPWAIT r0
173
174 /* set EXP CS0 to the optimum timing */
175 ldr r1, =CONFIG_SYS_EXP_CS0
176 ldr r2, =IXP425_EXP_CS0
177 str r1, [r2]
178
179 /* make sure flash is visible at 0 */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200180 mov r1, #CONFIG_SYS_SDR_CONFIG
181 ldr r2, =IXP425_SDR_CONFIG
182 str r1, [r2]
183
184 /* disable refresh cycles */
185 mov r1, #0
186 ldr r3, =IXP425_SDR_REFRESH
187 str r1, [r3]
188
189 /* send nop command */
190 mov r1, #3
191 ldr r4, =IXP425_SDR_IR
192 str r1, [r4]
193 DELAY_FOR 0x4000, r0
194
195 /* set SDRAM internal refresh val */
196 ldr r1, =CONFIG_SYS_SDRAM_REFRESH_CNT
197 str r1, [r3]
198 DELAY_FOR 0x4000, r0
199
200 /* send precharge-all command to close all open banks */
201 mov r1, #2
202 str r1, [r4]
203 DELAY_FOR 0x4000, r0
204
205 /* provide 8 auto-refresh cycles */
206 mov r1, #4
207 mov r5, #8
208111: str r1, [r4]
209 DELAY_FOR 0x100, r0
210 subs r5, r5, #1
211 bne 111b
wdenkbd1575f2003-10-14 19:43:55 +0000212
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200213 /* set mode register in sdram */
214 mov r1, #CONFIG_SYS_SDR_MODE_CONFIG
215 str r1, [r4]
216 DELAY_FOR 0x4000, r0
217
218 /* send normal operation command */
219 mov r1, #6
220 str r1, [r4]
221 DELAY_FOR 0x4000, r0
222
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200223 /* invalidate I & D caches & BTB */
224 mcr p15, 0, r0, c7, c7, 0
225 CPWAIT r0
226
227 /* invalidate I & Data TLB */
228 mcr p15, 0, r0, c8, c7, 0
229 CPWAIT r0
230
231 /* drain write and fill buffers */
232 mcr p15, 0, r0, c7, c10, 4
233 CPWAIT r0
234
Michael Schwingen7ddf55e2011-05-23 00:00:00 +0200235 /* remove flash mirror at 0x00000000 */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200236 ldr r2, =IXP425_EXP_CFG0
237 ldr r1, [r2]
238 bic r1, r1, #0x80000000
239 str r1, [r2]
240
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200241 /* invalidate I & Data TLB */
242 mcr p15, 0, r0, c8, c7, 0
243 CPWAIT r0
244
245 /* enable I cache */
246 mrc p15, 0, r0, c1, c0, 0
247 orr r0, r0, #MMU_Control_I
248 mcr p15, 0, r0, c1, c0, 0
249 CPWAIT r0
250
251 mrs r0,cpsr /* set the cpu to SVC32 mode */
252 bic r0,r0,#0x1f /* (superviser mode, M=10011) */
253 orr r0,r0,#0x13
254 msr cpsr,r0
255
Albert ARIBAUDfacdae52013-01-08 10:18:02 +0000256 bl _main
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200257
258/*------------------------------------------------------------------------------*/
259
260/*
Benoît Thébaudeaua0436612013-04-11 09:35:53 +0000261 * void relocate_code(addr_moni)
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200262 *
Benoît Thébaudeau9039c102013-04-11 09:35:43 +0000263 * This function relocates the monitor code.
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200264 */
265 .globl relocate_code
266relocate_code:
Benoît Thébaudeaua0436612013-04-11 09:35:53 +0000267 mov r6, r0 /* save addr of destination */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200268
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200269 adr r0, _start
Benoît Thébaudeaua18f3232013-04-11 09:35:45 +0000270 subs r9, r6, r0 /* r9 <- relocation offset */
Albert ARIBAUDfacdae52013-01-08 10:18:02 +0000271 beq relocate_done /* skip relocation */
Andreas Bießmann8cfbda92010-12-01 00:58:33 +0100272 mov r1, r6 /* r1 <- scratch for copy_loop */
Benoît Thébaudeau03bae032013-04-11 09:35:46 +0000273 ldr r3, _image_copy_end_ofs
Albert Aribaud126897e2010-11-25 22:45:02 +0100274 add r2, r0, r3 /* r2 <- source end address */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200275
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200276copy_loop:
Benoît Thébaudeaua18f3232013-04-11 09:35:45 +0000277 ldmia r0!, {r10-r11} /* copy from source address [r0] */
278 stmia r1!, {r10-r11} /* copy to target address [r1] */
Albert Aribaud0668d162010-10-05 16:06:39 +0200279 cmp r0, r2 /* until source end address [r2] */
280 blo copy_loop
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200281
Aneesh V552a3192011-07-13 05:11:07 +0000282#ifndef CONFIG_SPL_BUILD
Albert Aribaud126897e2010-11-25 22:45:02 +0100283 /*
284 * fix .rel.dyn relocations
285 */
286 ldr r0, _TEXT_BASE /* r0 <- Text base */
Albert Aribaud126897e2010-11-25 22:45:02 +0100287 ldr r10, _dynsym_start_ofs /* r10 <- sym table ofs */
288 add r10, r10, r0 /* r10 <- sym table in FLASH */
289 ldr r2, _rel_dyn_start_ofs /* r2 <- rel dyn start ofs */
290 add r2, r2, r0 /* r2 <- rel dyn start in FLASH */
291 ldr r3, _rel_dyn_end_ofs /* r3 <- rel dyn end ofs */
292 add r3, r3, r0 /* r3 <- rel dyn end in FLASH */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200293fixloop:
Albert Aribaud126897e2010-11-25 22:45:02 +0100294 ldr r0, [r2] /* r0 <- location to fix up, IN FLASH! */
295 add r0, r0, r9 /* r0 <- location to fix up in RAM */
296 ldr r1, [r2, #4]
Andreas Bießmann318cea12010-12-01 00:58:35 +0100297 and r7, r1, #0xff
298 cmp r7, #23 /* relative fixup? */
Albert Aribaud126897e2010-11-25 22:45:02 +0100299 beq fixrel
Andreas Bießmann318cea12010-12-01 00:58:35 +0100300 cmp r7, #2 /* absolute fixup? */
Albert Aribaud126897e2010-11-25 22:45:02 +0100301 beq fixabs
302 /* ignore unknown type of fixup */
303 b fixnext
304fixabs:
305 /* absolute fix: set location to (offset) symbol value */
306 mov r1, r1, LSR #4 /* r1 <- symbol index in .dynsym */
307 add r1, r10, r1 /* r1 <- address of symbol in table */
308 ldr r1, [r1, #4] /* r1 <- symbol value */
Wolfgang Denk899cdd12010-12-09 11:26:24 +0100309 add r1, r1, r9 /* r1 <- relocated sym addr */
Albert Aribaud126897e2010-11-25 22:45:02 +0100310 b fixnext
311fixrel:
312 /* relative fix: increase location by offset */
313 ldr r1, [r0]
314 add r1, r1, r9
315fixnext:
316 str r1, [r0]
317 add r2, r2, #8 /* each rel.dyn entry is 8 bytes */
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200318 cmp r2, r3
Wolfgang Denk98dd07c2010-10-23 23:22:38 +0200319 blo fixloop
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200320#endif
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200321
Albert ARIBAUDfacdae52013-01-08 10:18:02 +0000322relocate_done:
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200323
Albert ARIBAUDfacdae52013-01-08 10:18:02 +0000324 bx lr
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200325
Albert Aribaud126897e2010-11-25 22:45:02 +0100326_rel_dyn_start_ofs:
327 .word __rel_dyn_start - _start
328_rel_dyn_end_ofs:
329 .word __rel_dyn_end - _start
330_dynsym_start_ofs:
331 .word __dynsym_start - _start
wdenkbd1575f2003-10-14 19:43:55 +0000332
Albert ARIBAUDfacdae52013-01-08 10:18:02 +0000333 .globl c_runtime_cpu_setup
334c_runtime_cpu_setup:
335
336 bx lr
337
wdenkbd1575f2003-10-14 19:43:55 +0000338/****************************************************************************/
339/* */
340/* Interrupt handling */
341/* */
342/****************************************************************************/
343
344/* IRQ stack frame */
345
346#define S_FRAME_SIZE 72
347
348#define S_OLD_R0 68
349#define S_PSR 64
350#define S_PC 60
351#define S_LR 56
352#define S_SP 52
353
354#define S_IP 48
355#define S_FP 44
356#define S_R10 40
357#define S_R9 36
358#define S_R8 32
359#define S_R7 28
360#define S_R6 24
361#define S_R5 20
362#define S_R4 16
363#define S_R3 12
364#define S_R2 8
365#define S_R1 4
366#define S_R0 0
367
368#define MODE_SVC 0x13
369
370 /* use bad_save_user_regs for abort/prefetch/undef/swi ... */
371
372 .macro bad_save_user_regs
373 sub sp, sp, #S_FRAME_SIZE
374 stmia sp, {r0 - r12} /* Calling r0-r12 */
375 add r8, sp, #S_PC
376
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200377 ldr r2, IRQ_STACK_START_IN
wdenkbd1575f2003-10-14 19:43:55 +0000378 ldmia r2, {r2 - r4} /* get pc, cpsr, old_r0 */
379 add r0, sp, #S_FRAME_SIZE /* restore sp_SVC */
380
381 add r5, sp, #S_SP
382 mov r1, lr
383 stmia r5, {r0 - r4} /* save sp_SVC, lr_SVC, pc, cpsr, old_r */
384 mov r0, sp
385 .endm
386
387
388 /* use irq_save_user_regs / irq_restore_user_regs for */
389 /* IRQ/FIQ handling */
390
391 .macro irq_save_user_regs
392 sub sp, sp, #S_FRAME_SIZE
393 stmia sp, {r0 - r12} /* Calling r0-r12 */
394 add r8, sp, #S_PC
395 stmdb r8, {sp, lr}^ /* Calling SP, LR */
396 str lr, [r8, #0] /* Save calling PC */
397 mrs r6, spsr
398 str r6, [r8, #4] /* Save CPSR */
399 str r0, [r8, #8] /* Save OLD_R0 */
400 mov r0, sp
401 .endm
402
403 .macro irq_restore_user_regs
404 ldmia sp, {r0 - lr}^ @ Calling r0 - lr
405 mov r0, r0
406 ldr lr, [sp, #S_PC] @ Get PC
407 add sp, sp, #S_FRAME_SIZE
408 subs pc, lr, #4 @ return & move spsr_svc into cpsr
409 .endm
410
411 .macro get_bad_stack
Heiko Schocherb2b37f02010-09-17 13:10:47 +0200412 ldr r13, IRQ_STACK_START_IN @ setup our mode stack
wdenkbd1575f2003-10-14 19:43:55 +0000413
414 str lr, [r13] @ save caller lr / spsr
415 mrs lr, spsr
416 str lr, [r13, #4]
417
418 mov r13, #MODE_SVC @ prepare SVC-Mode
419 msr spsr_c, r13
420 mov lr, pc
421 movs pc, lr
422 .endm
423
424 .macro get_irq_stack @ setup IRQ stack
425 ldr sp, IRQ_STACK_START
426 .endm
427
428 .macro get_fiq_stack @ setup FIQ stack
429 ldr sp, FIQ_STACK_START
430 .endm
431
432
433/****************************************************************************/
434/* */
435/* exception handlers */
436/* */
437/****************************************************************************/
438
439 .align 5
440undefined_instruction:
441 get_bad_stack
442 bad_save_user_regs
443 bl do_undefined_instruction
444
445 .align 5
446software_interrupt:
447 get_bad_stack
448 bad_save_user_regs
449 bl do_software_interrupt
450
451 .align 5
452prefetch_abort:
453 get_bad_stack
454 bad_save_user_regs
455 bl do_prefetch_abort
456
457 .align 5
458data_abort:
459 get_bad_stack
460 bad_save_user_regs
461 bl do_data_abort
462
463 .align 5
464not_used:
465 get_bad_stack
466 bad_save_user_regs
467 bl do_not_used
468
469#ifdef CONFIG_USE_IRQ
470
471 .align 5
472irq:
473 get_irq_stack
474 irq_save_user_regs
475 bl do_irq
476 irq_restore_user_regs
477
478 .align 5
479fiq:
480 get_fiq_stack
481 irq_save_user_regs /* someone ought to write a more */
482 bl do_fiq /* effiction fiq_save_user_regs */
483 irq_restore_user_regs
484
485#else
486
487 .align 5
488irq:
489 get_bad_stack
490 bad_save_user_regs
491 bl do_irq
492
493 .align 5
494fiq:
495 get_bad_stack
496 bad_save_user_regs
497 bl do_fiq
498
499#endif
500
501/****************************************************************************/
502/* */
503/* Reset function: Use Watchdog to reset */
504/* */
505/****************************************************************************/
506
507 .align 5
508.globl reset_cpu
509
510reset_cpu:
Wolfgang Denka1be4762008-05-20 16:00:29 +0200511 ldr r1, =0x482e
wdenkbd1575f2003-10-14 19:43:55 +0000512 ldr r2, =IXP425_OSWK
513 str r1, [r2]
Wolfgang Denka1be4762008-05-20 16:00:29 +0200514 ldr r1, =0x0fff
wdenkbd1575f2003-10-14 19:43:55 +0000515 ldr r2, =IXP425_OSWT
516 str r1, [r2]
Wolfgang Denka1be4762008-05-20 16:00:29 +0200517 ldr r1, =0x5
wdenkbd1575f2003-10-14 19:43:55 +0000518 ldr r2, =IXP425_OSWE
519 str r1, [r2]
520 b reset_endless
521
wdenkbd1575f2003-10-14 19:43:55 +0000522reset_endless:
wdenkbd1575f2003-10-14 19:43:55 +0000523 b reset_endless