* Patch by Mark Jonas, 08 June 2004:
[oweals/u-boot.git] / cpu / at91rm9200 / start.S
1 /*
2  *  armboot - Startup Code for ARM720 CPU-core
3  *
4  *  Copyright (c) 2001  Marius Gröger <mag@sysgo.de>
5  *  Copyright (c) 2002  Alex Züpke <azu@sysgo.de>
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 #include "config.h"
28 #include "version.h"
29
30
31 /*
32  *************************************************************************
33  *
34  * Jump vector table as in table 3.1 in [1]
35  *
36  *************************************************************************
37  */
38
39
40 .globl _start
41 _start: b       reset
42         ldr     pc, _undefined_instruction
43         ldr     pc, _software_interrupt
44         ldr     pc, _prefetch_abort
45         ldr     pc, _data_abort
46         ldr     pc, _not_used
47         ldr     pc, _irq
48         ldr     pc, _fiq
49
50 _undefined_instruction: .word undefined_instruction
51 _software_interrupt:    .word software_interrupt
52 _prefetch_abort:        .word prefetch_abort
53 _data_abort:            .word data_abort
54 _not_used:              .word not_used
55 _irq:                   .word irq
56 _fiq:                   .word fiq
57
58         .balignl 16,0xdeadbeef
59
60
61 /*
62  *************************************************************************
63  *
64  * Startup Code (reset vector)
65  *
66  * do important init only if we don't start from memory!
67  * relocate armboot to ram
68  * setup stack
69  * jump to second stage
70  *
71  *************************************************************************
72  */
73
74 _TEXT_BASE:
75         .word   TEXT_BASE
76
77 .globl _armboot_start
78 _armboot_start:
79         .word _start
80
81 /*
82  * These are defined in the board-specific linker script.
83  */
84 .globl _bss_start
85 _bss_start:
86         .word __bss_start
87
88 .globl _bss_end
89 _bss_end:
90         .word _end
91
92 #ifdef CONFIG_USE_IRQ
93 /* IRQ stack memory (calculated at run-time) */
94 .globl IRQ_STACK_START
95 IRQ_STACK_START:
96         .word   0x0badc0de
97
98 /* IRQ stack memory (calculated at run-time) */
99 .globl FIQ_STACK_START
100 FIQ_STACK_START:
101         .word 0x0badc0de
102 #endif
103
104
105 /*
106  * the actual reset code
107  */
108
109 reset:
110         /*
111          * set the cpu to SVC32 mode
112          */
113         mrs     r0,cpsr
114         bic     r0,r0,#0x1f
115         orr     r0,r0,#0x13
116         msr     cpsr,r0
117
118         /*
119          * relocate exeception table
120          */
121         ldr     r0, =_start
122         ldr     r1, =0x0
123         mov     r2, #16
124 copyex:
125         subs    r2, r2, #1
126         ldr     r3, [r0], #4
127         str     r3, [r1], #4
128         bne     copyex
129
130         /*
131          * we do sys-critical inits only at reboot,
132          * not when booting from ram!
133          */
134 #ifdef CONFIG_INIT_CRITICAL
135         bl      cpu_init_crit
136 #endif
137
138         /* Set up the stack                                                 */
139 stack_setup:
140         ldr     r0, _TEXT_BASE          /* upper 128 KiB: relocated uboot   */
141         sub     r0, r0, #CFG_MALLOC_LEN /* malloc area                      */
142         sub     r0, r0, #CFG_GBL_DATA_SIZE /* bdinfo                        */
143 #ifdef CONFIG_USE_IRQ
144         sub     r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)
145 #endif
146         sub     sp, r0, #12             /* leave 3 words for abort-stack    */
147
148 clear_bss:
149         ldr     r0, _bss_start          /* find start of bss segment        */
150         ldr     r1, _bss_end            /* stop here                        */
151         mov     r2, #0x00000000         /* clear                            */
152
153 clbss_l:str     r2, [r0]                /* clear loop...                    */
154         add     r0, r0, #4
155         cmp     r0, r1
156         bne     clbss_l
157
158         ldr pc,_start_armboot
159
160 _start_armboot: .word start_armboot
161
162 /*
163  *************************************************************************
164  *
165  * CPU_init_critical registers
166  *
167  *************************************************************************
168  */
169
170 cpu_init_crit:
171         # actually do nothing for now!
172         mov     pc, lr
173
174
175 /*
176  *************************************************************************
177  *
178  * Interrupt handling
179  *
180  *************************************************************************
181  */
182
183 @
184 @ IRQ stack frame.
185 @
186 #define S_FRAME_SIZE    72
187
188 #define S_OLD_R0        68
189 #define S_PSR           64
190 #define S_PC            60
191 #define S_LR            56
192 #define S_SP            52
193
194 #define S_IP            48
195 #define S_FP            44
196 #define S_R10           40
197 #define S_R9            36
198 #define S_R8            32
199 #define S_R7            28
200 #define S_R6            24
201 #define S_R5            20
202 #define S_R4            16
203 #define S_R3            12
204 #define S_R2            8
205 #define S_R1            4
206 #define S_R0            0
207
208 #define MODE_SVC 0x13
209 #define I_BIT    0x80
210
211 /*
212  * use bad_save_user_regs for abort/prefetch/undef/swi ...
213  * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
214  */
215
216         .macro  bad_save_user_regs
217         sub     sp, sp, #S_FRAME_SIZE
218         stmia   sp, {r0 - r12}                  @ Calling r0-r12
219         add     r8, sp, #S_PC
220
221         ldr     r2, _armboot_start
222         sub     r2, r2, #(CONFIG_STACKSIZE+CFG_MALLOC_LEN)
223         sub     r2, r2, #(CFG_GBL_DATA_SIZE+8)  @ set base 2 words into abort stack
224         ldmia   r2, {r2 - r4}                   @ get pc, cpsr, old_r0
225         add     r0, sp, #S_FRAME_SIZE           @ restore sp_SVC
226
227         add     r5, sp, #S_SP
228         mov     r1, lr
229         stmia   r5, {r0 - r4}                   @ save sp_SVC, lr_SVC, pc, cpsr, old_r
230         mov     r0, sp
231         .endm
232
233         .macro  irq_save_user_regs
234         sub     sp, sp, #S_FRAME_SIZE
235         stmia   sp, {r0 - r12}                  @ Calling r0-r12
236         add     r8, sp, #S_PC
237         stmdb   r8, {sp, lr}^                   @ Calling SP, LR
238         str     lr, [r8, #0]                    @ Save calling PC
239         mrs     r6, spsr
240         str     r6, [r8, #4]                    @ Save CPSR
241         str     r0, [r8, #8]                    @ Save OLD_R0
242         mov     r0, sp
243         .endm
244
245         .macro  irq_restore_user_regs
246         ldmia   sp, {r0 - lr}^                  @ Calling r0 - lr
247         mov     r0, r0
248         ldr     lr, [sp, #S_PC]                 @ Get PC
249         add     sp, sp, #S_FRAME_SIZE
250         subs    pc, lr, #4                      @ return & move spsr_svc into cpsr
251         .endm
252
253         .macro get_bad_stack
254         ldr     r13, _armboot_start             @ setup our mode stack
255         sub     r13, r13, #(CONFIG_STACKSIZE+CFG_MALLOC_LEN)
256         sub     r13, r13, #(CFG_GBL_DATA_SIZE+8) @ reserved a couple spots in abort stack
257
258         str     lr, [r13]                       @ save caller lr / spsr
259         mrs     lr, spsr
260         str     lr, [r13, #4]
261
262         mov     r13, #MODE_SVC                  @ prepare SVC-Mode
263         msr     spsr_c, r13
264         mov     lr, pc
265         movs    pc, lr
266         .endm
267
268         .macro get_irq_stack                    @ setup IRQ stack
269         ldr     sp, IRQ_STACK_START
270         .endm
271
272         .macro get_fiq_stack                    @ setup FIQ stack
273         ldr     sp, FIQ_STACK_START
274         .endm
275
276 /*
277  * exception handlers
278  */
279         .align  5
280 undefined_instruction:
281         get_bad_stack
282         bad_save_user_regs
283         bl      do_undefined_instruction
284
285         .align  5
286 software_interrupt:
287         get_bad_stack
288         bad_save_user_regs
289         bl      do_software_interrupt
290
291         .align  5
292 prefetch_abort:
293         get_bad_stack
294         bad_save_user_regs
295         bl      do_prefetch_abort
296
297         .align  5
298 data_abort:
299         get_bad_stack
300         bad_save_user_regs
301         bl      do_data_abort
302
303         .align  5
304 not_used:
305         get_bad_stack
306         bad_save_user_regs
307         bl      do_not_used
308
309 #ifdef CONFIG_USE_IRQ
310
311         .align  5
312 irq:
313         get_irq_stack
314         irq_save_user_regs
315         bl      do_irq
316         irq_restore_user_regs
317
318         .align  5
319 fiq:
320         get_fiq_stack
321         /* someone ought to write a more effiction fiq_save_user_regs */
322         irq_save_user_regs
323         bl      do_fiq
324         irq_restore_user_regs
325
326 #else
327
328         .align  5
329 irq:
330         get_bad_stack
331         bad_save_user_regs
332         bl      do_irq
333
334         .align  5
335 fiq:
336         get_bad_stack
337         bad_save_user_regs
338         bl      do_fiq
339
340 #endif
341
342         .align  5
343 .globl reset_cpu
344 reset_cpu:
345         mov     pc, r0