de33e8b8fe527562b55058f6302eaab580644db9
[oweals/u-boot.git] / cpu / ixp / start.S
1 /* 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
30 #include <config.h>
31 #include <version.h>
32 #include <asm/arch/ixp425.h>
33
34 #define MMU_Control_M  0x001    // Enable MMU
35 #define MMU_Control_A  0x002    // Enable address alignment faults
36 #define MMU_Control_C  0x004    // Enable cache
37 #define MMU_Control_W  0x008    // Enable write-buffer
38 #define MMU_Control_P  0x010    // Compatability: 32 bit code
39 #define MMU_Control_D  0x020    // Compatability: 32 bit data
40 #define MMU_Control_L  0x040    // Compatability:
41 #define MMU_Control_B  0x080    // Enable Big-Endian
42 #define MMU_Control_S  0x100    // Enable system protection
43 #define MMU_Control_R  0x200    // Enable ROM protection
44 #define MMU_Control_I  0x1000   // Enable Instruction cache
45 #define MMU_Control_X  0x2000   // Set interrupt vectors at 0xFFFF0000
46 #define MMU_Control_Init (MMU_Control_P|MMU_Control_D|MMU_Control_L)
47
48
49 /*
50  * Macro definitions
51  */
52         // Delay a bit
53         .macro DELAY_FOR cycles, reg0
54         ldr     \reg0, =\cycles
55         subs    \reg0, \reg0, #1
56         subne   pc,  pc, #0xc
57         .endm
58
59         // wait for coprocessor write complete
60         .macro CPWAIT reg
61         mrc  p15,0,\reg,c2,c0,0
62         mov  \reg,\reg
63         sub  pc,pc,#4
64         .endm
65
66 .globl _start
67 _start: b       reset
68         ldr     pc, _undefined_instruction
69         ldr     pc, _software_interrupt
70         ldr     pc, _prefetch_abort
71         ldr     pc, _data_abort
72         ldr     pc, _not_used
73         ldr     pc, _irq
74         ldr     pc, _fiq
75
76 _undefined_instruction: .word undefined_instruction
77 _software_interrupt:    .word software_interrupt
78 _prefetch_abort:        .word prefetch_abort
79 _data_abort:            .word data_abort
80 _not_used:              .word not_used
81 _irq:                   .word irq
82 _fiq:                   .word fiq
83
84         .balignl 16,0xdeadbeef
85
86
87 /*
88  * Startup Code (reset vector)
89  *
90  * do important init only if we don't start from memory!
91  * - relocate armboot to ram
92  * - setup stack
93  * - jump to second stage
94  */
95
96 _TEXT_BASE:
97         .word   TEXT_BASE
98
99 .globl _armboot_start
100 _armboot_start:
101         .word _start
102
103 /*
104  * Note: _armboot_end_data and _armboot_end are defined
105  * by the (board-dependent) linker script.
106  * _armboot_end_data is the first usable FLASH address after armboot
107  */
108 .globl _armboot_end_data
109 _armboot_end_data:
110         .word armboot_end_data
111 .globl _armboot_end
112 _armboot_end:
113         .word armboot_end
114
115 /*
116  * This is defined in the board specific linker script
117  */
118 .globl _bss_start
119 _bss_start:
120         .word bss_start
121
122 .globl _bss_end
123 _bss_end:
124         .word bss_end
125
126 /*
127  * _armboot_real_end is the first usable RAM address behind armboot
128  * and the various stacks
129  */
130 .globl _armboot_real_end
131 _armboot_real_end:
132         .word 0x0badc0de
133
134 /*
135  * We relocate uboot to this address (end of RAM - 128 KiB)
136  */
137 .globl _uboot_reloc
138 _uboot_reloc:
139         .word TEXT_BASE
140
141 #ifdef CONFIG_USE_IRQ
142 /* IRQ stack memory (calculated at run-time) */
143 .globl IRQ_STACK_START
144 IRQ_STACK_START:
145         .word   0x0badc0de
146
147 /* IRQ stack memory (calculated at run-time) */
148 .globl FIQ_STACK_START
149 FIQ_STACK_START:
150         .word 0x0badc0de
151 #endif
152
153 /****************************************************************************/
154 /*                                                                          */
155 /* the actual reset code                                                    */
156 /*                                                                          */
157 /****************************************************************************/
158
159 reset:
160         /* disable mmu, set big-endian */
161         mov     r0, #0xf8
162         mcr     p15, 0, r0, c1, c0, 0
163         CPWAIT  r0
164
165         /* invalidate I & D caches & BTB */
166         mcr     p15, 0, r0, c7, c7, 0
167         CPWAIT  r0
168
169         /* invalidate I & Data TLB */
170         mcr     p15, 0, r0, c8, c7, 0
171         CPWAIT r0
172
173         /* drain write and fill buffers */
174         mcr     p15, 0, r0, c7, c10, 4
175         CPWAIT  r0
176
177         /* disable write buffer coalescing */
178         mrc     p15, 0, r0, c1, c0, 1
179         orr     r0, r0, #1
180         mcr     p15, 0, r0, c1, c0, 1
181         CPWAIT  r0
182
183         /* set EXP CS0 to the optimum timing */
184         ldr     r1, =CFG_EXP_CS0
185         ldr     r2, =IXP425_EXP_CS0
186         str     r1, [r2]
187
188         /* make sure flash is visible at 0 */
189         ldr     r2, =IXP425_EXP_CFG0
190         ldr     r1, [r2]
191         orr     r1, r1, #0x80000000
192         str     r1, [r2]
193
194         mov     r1, #CFG_SDR_CONFIG
195         ldr     r2, =IXP425_SDR_CONFIG
196         str     r1, [r2]
197
198         /* disable refresh cycles */
199         mov     r1, #0
200         ldr     r3, =IXP425_SDR_REFRESH
201         str     r1, [r3]
202
203         /* send nop command */
204         mov     r1, #3
205         ldr     r4, =IXP425_SDR_IR
206         str     r1, [r4]
207         DELAY_FOR 0x4000, r0
208
209         /* set SDRAM internal refresh val */
210         ldr     r1, =CFG_SDRAM_REFRESH_CNT
211         str     r1, [r3]
212         DELAY_FOR 0x4000, r0
213
214         /* send precharge-all command to close all open banks */
215         mov     r1, #2
216         str     r1, [r4]
217         DELAY_FOR 0x4000, r0
218
219         /* provide 8 auto-refresh cycles */
220         mov     r1, #4
221         mov     r5, #8
222 111:    str     r1, [r4]
223         DELAY_FOR 0x100, r0
224         subs    r5, r5, #1
225         bne     111b
226
227         /* set mode register in sdram */
228         mov     r1, #1
229         str     r1, [r4]
230         DELAY_FOR 0x4000, r0
231
232         /* send normal operation command */
233         mov     r1, #6
234         str     r1, [r4]
235         DELAY_FOR 0x4000, r0
236
237         /* copy */
238         mov     r0, #0
239         mov     r4, r0
240         add     r2, r0, #0x40000
241         mov     r1, #0x10000000
242         mov     r5, r1
243
244     30:
245         ldr     r3, [r0], #4
246         str     r3, [r1], #4
247         cmp     r0, r2
248         bne     30b
249
250         /* invalidate I & D caches & BTB */
251         mcr     p15, 0, r0, c7, c7, 0
252         CPWAIT  r0
253
254         /* invalidate I & Data TLB */
255         mcr     p15, 0, r0, c8, c7, 0
256         CPWAIT r0
257
258         /* drain write and fill buffers */
259         mcr     p15, 0, r0, c7, c10, 4
260         CPWAIT  r0
261
262         /* move flash to 0x50000000 */
263         ldr     r2, =IXP425_EXP_CFG0
264         ldr     r1, [r2]
265         bic     r1, r1, #0x80000000
266         str     r1, [r2]
267
268         nop
269         nop
270         nop
271         nop
272         nop
273         nop
274
275         /* invalidate I & Data TLB */
276         mcr     p15, 0, r0, c8, c7, 0
277         CPWAIT r0
278
279         /* enable I cache */
280         mrc     p15, 0, r0, c1, c0, 0
281         orr     r0, r0, #MMU_Control_I
282         mcr     p15, 0, r0, c1, c0, 0
283         CPWAIT  r0
284
285         mrs     r0,cpsr                 /* set the cpu to SVC32 mode        */
286         bic     r0,r0,#0x1f             /* (superviser mode, M=10011)       */
287         orr     r0,r0,#0x13
288         msr     cpsr,r0
289
290 relocate:                               /* relocate U-Boot to RAM           */
291         adr     r0, _start              /* r0 <- current position of code   */
292         ldr     r1, _TEXT_BASE          /* test if we run from flash or RAM */
293         cmp     r0, r1                  /* don't reloc during debug         */
294         beq     stack_setup
295
296         ldr     r2, _armboot_start
297         ldr     r3, _armboot_end
298         sub     r2, r3, r2              /* r2 <- size of armboot            */
299         add     r2, r0, r2              /* r2 <- source end address         */
300
301 copy_loop:
302         ldmia   r0!, {r3-r10}           /* copy from source address [r0]    */
303         stmia   r1!, {r3-r10}           /* copy to   target address [r1]    */
304         cmp     r0, r2                  /* until source end addreee [r2]    */
305         ble     copy_loop
306
307         /* Set up the stack                                                 */
308
309 stack_setup:
310
311         ldr     r0, _uboot_reloc        /* upper 128 KiB: relocated uboot   */
312         sub     r0, r0, #CFG_MALLOC_LEN /* malloc area                      */
313                                         /* FIXME: bdinfo should be here     */
314         sub     sp, r0, #12             /* leave 3 words for abort-stack    */
315
316 clear_bss:
317
318         ldr     r0, _bss_start          /* find start of bss segment        */
319         add     r0, r0, #4              /* start at first byte of bss       */
320         ldr     r1, _bss_end            /* stop here                        */
321         mov     r2, #0x00000000         /* clear                            */
322
323 clbss_l:str     r2, [r0]                /* clear loop...                    */
324         add     r0, r0, #4
325         cmp     r0, r1
326         bne     clbss_l
327
328
329         ldr     pc, _start_armboot
330
331 _start_armboot: .word start_armboot
332
333
334
335
336 /****************************************************************************/
337 /*                                                                          */
338 /* Interrupt handling                                                       */
339 /*                                                                          */
340 /****************************************************************************/
341
342 /* IRQ stack frame                                                          */
343
344 #define S_FRAME_SIZE    72
345
346 #define S_OLD_R0        68
347 #define S_PSR           64
348 #define S_PC            60
349 #define S_LR            56
350 #define S_SP            52
351
352 #define S_IP            48
353 #define S_FP            44
354 #define S_R10           40
355 #define S_R9            36
356 #define S_R8            32
357 #define S_R7            28
358 #define S_R6            24
359 #define S_R5            20
360 #define S_R4            16
361 #define S_R3            12
362 #define S_R2            8
363 #define S_R1            4
364 #define S_R0            0
365
366 #define MODE_SVC 0x13
367
368         /* use bad_save_user_regs for abort/prefetch/undef/swi ...          */
369
370         .macro  bad_save_user_regs
371         sub     sp, sp, #S_FRAME_SIZE
372         stmia   sp, {r0 - r12}                  /* Calling r0-r12           */
373         add     r8, sp, #S_PC
374
375         ldr     r2, _armboot_end
376         add     r2, r2, #CONFIG_STACKSIZE
377         sub     r2, r2, #8
378         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
412         ldr     r13, _armboot_end               @ setup our mode stack
413         add     r13, r13, #CONFIG_STACKSIZE     @ resides at top of normal stack
414         sub     r13, r13, #8
415
416         str     lr, [r13]                       @ save caller lr / spsr
417         mrs     lr, spsr
418         str     lr, [r13, #4]
419
420         mov     r13, #MODE_SVC                  @ prepare SVC-Mode
421         msr     spsr_c, r13
422         mov     lr, pc
423         movs    pc, lr
424         .endm
425
426         .macro get_irq_stack                    @ setup IRQ stack
427         ldr     sp, IRQ_STACK_START
428         .endm
429
430         .macro get_fiq_stack                    @ setup FIQ stack
431         ldr     sp, FIQ_STACK_START
432         .endm
433
434
435 /****************************************************************************/
436 /*                                                                          */
437 /* exception handlers                                                       */
438 /*                                                                          */
439 /****************************************************************************/
440
441         .align  5
442 undefined_instruction:
443         get_bad_stack
444         bad_save_user_regs
445         bl      do_undefined_instruction
446
447         .align  5
448 software_interrupt:
449         get_bad_stack
450         bad_save_user_regs
451         bl      do_software_interrupt
452
453         .align  5
454 prefetch_abort:
455         get_bad_stack
456         bad_save_user_regs
457         bl      do_prefetch_abort
458
459         .align  5
460 data_abort:
461         get_bad_stack
462         bad_save_user_regs
463         bl      do_data_abort
464
465         .align  5
466 not_used:
467         get_bad_stack
468         bad_save_user_regs
469         bl      do_not_used
470
471 #ifdef CONFIG_USE_IRQ
472
473         .align  5
474 irq:
475         get_irq_stack
476         irq_save_user_regs
477         bl      do_irq
478         irq_restore_user_regs
479
480         .align  5
481 fiq:
482         get_fiq_stack
483         irq_save_user_regs              /* someone ought to write a more    */
484         bl      do_fiq                  /* effiction fiq_save_user_regs     */
485         irq_restore_user_regs
486
487 #else
488
489         .align  5
490 irq:
491         get_bad_stack
492         bad_save_user_regs
493         bl      do_irq
494
495         .align  5
496 fiq:
497         get_bad_stack
498         bad_save_user_regs
499         bl      do_fiq
500
501 #endif
502
503 /****************************************************************************/
504 /*                                                                          */
505 /* Reset function: Use Watchdog to reset                                    */
506 /*                                                                          */
507 /****************************************************************************/
508
509         .align  5
510 .globl reset_cpu
511
512 reset_cpu:
513         ldr     r1, =0x482e
514         ldr     r2, =IXP425_OSWK
515         str     r1, [r2]
516         ldr     r1, =0x0fff
517         ldr     r2, =IXP425_OSWT
518         str     r1, [r2]
519         ldr     r1, =0x5
520         ldr     r2, =IXP425_OSWE
521         str     r1, [r2]
522         b       reset_endless
523
524
525 reset_endless:
526
527         b       reset_endless