]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/lh7a40x/start.S
002116a401055e397d39928cc4e6003f149bc016
[karo-tx-uboot.git] / arch / arm / cpu / lh7a40x / start.S
1 /*
2  *  armboot - Startup Code for ARM920 CPU-core
3  *
4  *  Copyright (c) 2001  Marius Gröger <mag@sysgo.de>
5  *  Copyright (c) 2002  Alex Züpke <azu@sysgo.de>
6  *  Copyright (c) 2002  Gary Jennejohn <garyj@denx.de>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27
28 #include <config.h>
29 #include <version.h>
30
31
32 /*
33  *************************************************************************
34  *
35  * Jump vector table as in table 3.1 in [1]
36  *
37  *************************************************************************
38  */
39
40
41 .globl _start
42 _start: b       reset
43         ldr     pc, _undefined_instruction
44         ldr     pc, _software_interrupt
45         ldr     pc, _prefetch_abort
46         ldr     pc, _data_abort
47         ldr     pc, _not_used
48         ldr     pc, _irq
49         ldr     pc, _fiq
50
51 _undefined_instruction: .word undefined_instruction
52 _software_interrupt:    .word software_interrupt
53 _prefetch_abort:        .word prefetch_abort
54 _data_abort:            .word data_abort
55 _not_used:              .word not_used
56 _irq:                   .word irq
57 _fiq:                   .word fiq
58
59         .balignl 16,0xdeadbeef
60
61
62 /*
63  *************************************************************************
64  *
65  * Startup Code (reset vector)
66  *
67  * do important init only if we don't start from memory!
68  * relocate armboot to ram
69  * setup stack
70  * jump to second stage
71  *
72  *************************************************************************
73  */
74
75 .globl _TEXT_BASE
76 _TEXT_BASE:
77         .word   TEXT_BASE
78
79 #if defined(CONFIG_SYS_ARM_WITHOUT_RELOC)
80 .globl _armboot_start
81 _armboot_start:
82         .word _start
83 #endif
84
85 /*
86  * These are defined in the board-specific linker script.
87  */
88 .globl _bss_start
89 _bss_start:
90         .word __bss_start
91
92 .globl _bss_end
93 _bss_end:
94         .word _end
95
96 #ifdef CONFIG_USE_IRQ
97 /* IRQ stack memory (calculated at run-time) */
98 .globl IRQ_STACK_START
99 IRQ_STACK_START:
100         .word   0x0badc0de
101
102 /* IRQ stack memory (calculated at run-time) */
103 .globl FIQ_STACK_START
104 FIQ_STACK_START:
105         .word 0x0badc0de
106 #endif
107
108 #if !defined(CONFIG_SYS_ARM_WITHOUT_RELOC)
109 /* IRQ stack memory (calculated at run-time) + 8 bytes */
110 .globl IRQ_STACK_START_IN
111 IRQ_STACK_START_IN:
112         .word   0x0badc0de
113
114 .globl _datarel_start
115 _datarel_start:
116         .word __datarel_start
117
118 .globl _datarelrolocal_start
119 _datarelrolocal_start:
120         .word __datarelrolocal_start
121
122 .globl _datarellocal_start
123 _datarellocal_start:
124         .word __datarellocal_start
125
126 .globl _datarelro_start
127 _datarelro_start:
128         .word __datarelro_start
129
130 .globl _got_start
131 _got_start:
132         .word __got_start
133
134 .globl _got_end
135 _got_end:
136         .word __got_end
137
138 /*
139  * the actual reset code
140  */
141
142 reset:
143         /*
144          * set the cpu to SVC32 mode
145          */
146         mrs     r0,cpsr
147         bic     r0,r0,#0x1f
148         orr     r0,r0,#0xd3
149         msr     cpsr,r0
150
151 #define pWDTCTL         0x80001400  /* Watchdog Timer control register */
152 #define pINTENC         0x8000050C  /* Interupt-Controller enable clear register */
153 #define pCLKSET         0x80000420  /* clock divisor register */
154
155         /* disable watchdog, set watchdog control register to
156          * all zeros (default reset)
157          */
158         ldr     r0, =pWDTCTL
159         mov     r1, #0x0
160         str     r1, [r0]
161
162         /*
163          * mask all IRQs by setting all bits in the INTENC register (default)
164          */
165         mov     r1, #0xffffffff
166         ldr     r0, =pINTENC
167         str     r1, [r0]
168
169         /* FCLK:HCLK:PCLK = 1:2:2 */
170         /* default FCLK is 200 MHz, using 14.7456 MHz fin */
171         ldr     r0, =pCLKSET
172         ldr r1, =0x0004ee39
173 @       ldr r1, =0x0005ee39     @ 1: 2: 4
174         str     r1, [r0]
175
176         /*
177          * we do sys-critical inits only at reboot,
178          * not when booting from ram!
179          */
180 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
181         bl      cpu_init_crit
182 #endif
183
184 /* Set stackpointer in internal RAM to call board_init_f */
185 call_board_init_f:
186         ldr     sp, =(CONFIG_SYS_INIT_SP_ADDR)
187         ldr     r0,=0x00000000
188         bl      board_init_f
189
190 /*------------------------------------------------------------------------------*/
191
192 /*
193  * void relocate_code (addr_sp, gd, addr_moni)
194  *
195  * This "function" does not return, instead it continues in RAM
196  * after relocating the monitor code.
197  *
198  */
199         .globl  relocate_code
200 relocate_code:
201         mov     r4, r0  /* save addr_sp */
202         mov     r5, r1  /* save addr of gd */
203         mov     r6, r2  /* save addr of destination */
204         mov     r7, r2  /* save addr of destination */
205
206         /* Set up the stack                                                 */
207 stack_setup:
208         mov     sp, r4
209
210         adr     r0, _start
211         ldr     r2, _TEXT_BASE
212         ldr     r3, _bss_start
213         sub     r2, r3, r2              /* r2 <- size of armboot            */
214         add     r2, r0, r2              /* r2 <- source end address         */
215         cmp     r0, r6
216         beq     clear_bss
217
218 #ifndef CONFIG_SKIP_RELOCATE_UBOOT
219 copy_loop:
220         ldmia   r0!, {r9-r10}           /* copy from source address [r0]    */
221         stmia   r6!, {r9-r10}           /* copy to   target address [r1]    */
222         cmp     r0, r2                  /* until source end addreee [r2]    */
223         ble     copy_loop
224
225 #ifndef CONFIG_PRELOADER
226         /* fix got entries */
227         ldr     r1, _TEXT_BASE          /* Text base */
228         mov     r0, r7                  /* reloc addr */
229         ldr     r2, _got_start          /* addr in Flash */
230         ldr     r3, _got_end            /* addr in Flash */
231         sub     r3, r3, r1
232         add     r3, r3, r0
233         sub     r2, r2, r1
234         add     r2, r2, r0
235
236 fixloop:
237         ldr     r4, [r2]
238         sub     r4, r4, r1
239         add     r4, r4, r0
240         str     r4, [r2]
241         add     r2, r2, #4
242         cmp     r2, r3
243         bne     fixloop
244 #endif
245 #endif  /* #ifndef CONFIG_SKIP_RELOCATE_UBOOT */
246
247 clear_bss:
248 #ifndef CONFIG_PRELOADER
249         ldr     r0, _bss_start
250         ldr     r1, _bss_end
251         ldr     r3, _TEXT_BASE          /* Text base */
252         mov     r4, r7                  /* reloc addr */
253         sub     r0, r0, r3
254         add     r0, r0, r4
255         sub     r1, r1, r3
256         add     r1, r1, r4
257         mov     r2, #0x00000000         /* clear                            */
258
259 clbss_l:str     r2, [r0]                /* clear loop...                    */
260         add     r0, r0, #4
261         cmp     r0, r1
262         bne     clbss_l
263 #endif
264
265 /*
266  * We are done. Do not return, instead branch to second part of board
267  * initialization, now running from RAM.
268  */
269         ldr     r0, _TEXT_BASE
270         ldr     r2, _board_init_r
271         sub     r2, r2, r0
272         add     r2, r2, r7      /* position from board_init_r in RAM */
273         /* setup parameters for board_init_r */
274         mov     r0, r5          /* gd_t */
275         mov     r1, r7          /* dest_addr */
276         /* jump to it ... */
277         mov     lr, r2
278         mov     pc, lr
279
280 _board_init_r: .word board_init_r
281
282 #else /* #if !defined(CONFIG_SYS_ARM_WITHOUT_RELOC) */
283 /*
284  * the actual reset code
285  */
286
287 reset:
288         /*
289          * set the cpu to SVC32 mode
290          */
291         mrs     r0,cpsr
292         bic     r0,r0,#0x1f
293         orr     r0,r0,#0xd3
294         msr     cpsr,r0
295
296 #define pWDTCTL         0x80001400  /* Watchdog Timer control register */
297 #define pINTENC         0x8000050C  /* Interupt-Controller enable clear register */
298 #define pCLKSET         0x80000420  /* clock divisor register */
299
300         /* disable watchdog, set watchdog control register to
301          * all zeros (default reset)
302          */
303         ldr     r0, =pWDTCTL
304         mov     r1, #0x0
305         str     r1, [r0]
306
307         /*
308          * mask all IRQs by setting all bits in the INTENC register (default)
309          */
310         mov     r1, #0xffffffff
311         ldr     r0, =pINTENC
312         str     r1, [r0]
313
314         /* FCLK:HCLK:PCLK = 1:2:2 */
315         /* default FCLK is 200 MHz, using 14.7456 MHz fin */
316         ldr     r0, =pCLKSET
317         ldr r1, =0x0004ee39
318 @       ldr r1, =0x0005ee39     @ 1: 2: 4
319         str     r1, [r0]
320
321         /*
322          * we do sys-critical inits only at reboot,
323          * not when booting from ram!
324          */
325 #ifndef CONFIG_SKIP_LOWLEVEL_INIT
326         bl      cpu_init_crit
327 #endif
328
329 #ifndef CONFIG_SKIP_RELOCATE_UBOOT
330 relocate:                               /* relocate U-Boot to RAM           */
331         adr     r0, _start              /* r0 <- current position of code   */
332         ldr     r1, _TEXT_BASE          /* test if we run from flash or RAM */
333         cmp     r0, r1                  /* don't reloc during debug         */
334         beq     stack_setup
335
336         ldr     r2, _armboot_start
337         ldr     r3, _bss_start
338         sub     r2, r3, r2              /* r2 <- size of armboot            */
339         add     r2, r0, r2              /* r2 <- source end address         */
340
341 copy_loop:
342         ldmia   r0!, {r3-r10}           /* copy from source address [r0]    */
343         stmia   r1!, {r3-r10}           /* copy to   target address [r1]    */
344         cmp     r0, r2                  /* until source end addreee [r2]    */
345         blt     copy_loop               /* a 'ble' here actually copies     */
346                                         /*   four bytes of bss              */
347 #endif  /* CONFIG_SKIP_RELOCATE_UBOOT */
348
349         /* Set up the stack                                                 */
350 stack_setup:
351         ldr     r0, _TEXT_BASE          /* upper 128 KiB: relocated uboot   */
352         sub     r0, r0, #CONFIG_SYS_MALLOC_LEN  /* malloc area                      */
353         sub     r0, r0, #CONFIG_SYS_GBL_DATA_SIZE /* bdinfo                        */
354 #ifdef CONFIG_USE_IRQ
355         sub     r0, r0, #(CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ)
356 #endif
357         sub     sp, r0, #12             /* leave 3 words for abort-stack    */
358         bic     sp, sp, #7              /* 8-byte alignment for ABI compliance */
359
360 clear_bss:
361         ldr     r0, _bss_start          /* find start of bss segment        */
362         @add    r0, r0, #4              /* start at first byte of bss       */
363                                         /*   why inc. 4 bytes past then?    */
364         ldr     r1, _bss_end            /* stop here                        */
365         mov     r2, #0x00000000         /* clear                            */
366
367 clbss_l:str     r2, [r0]                /* clear loop...                    */
368         add     r0, r0, #4
369         cmp     r0, r1
370         ble     clbss_l
371
372         ldr     pc, _start_armboot
373
374 _start_armboot: .word start_armboot
375 #endif /* #if !defined(CONFIG_SYS_ARM_WITHOUT_RELOC) */
376
377 /*
378  *************************************************************************
379  *
380  * CPU_init_critical registers
381  *
382  * setup important registers
383  * setup memory timing
384  *
385  *************************************************************************
386  */
387
388
389 cpu_init_crit:
390         /*
391          * flush v4 I/D caches
392          */
393         mov     r0, #0
394         mcr     p15, 0, r0, c7, c7, 0   /* flush v3/v4 cache */
395         mcr     p15, 0, r0, c8, c7, 0   /* flush v4 TLB */
396
397         /*
398          * disable MMU stuff and caches
399          */
400         mrc     p15, 0, r0, c1, c0, 0
401         bic     r0, r0, #0x00002300     @ clear bits 13, 9:8 (--V- --RS)
402         bic     r0, r0, #0x00000087     @ clear bits 7, 2:0 (B--- -CAM)
403         orr     r0, r0, #0x00000002     @ set bit 2 (A) Align
404         orr     r0, r0, #0x00001000     @ set bit 12 (I) I-Cache
405         orr     r0, r0, #0x40000000     @ set bit 30 (nF) notFastBus
406         mcr     p15, 0, r0, c1, c0, 0
407
408
409         /*
410          * before relocating, we have to setup RAM timing
411          * because memory timing is board-dependend, you will
412          * find a lowlevel_init.S in your board directory.
413          */
414         mov     ip, lr
415         bl      lowlevel_init
416         mov     lr, ip
417
418         mov     pc, lr
419
420
421 /*
422  *************************************************************************
423  *
424  * Interrupt handling
425  *
426  *************************************************************************
427  */
428
429 @
430 @ IRQ stack frame.
431 @
432 #define S_FRAME_SIZE    72
433
434 #define S_OLD_R0        68
435 #define S_PSR           64
436 #define S_PC            60
437 #define S_LR            56
438 #define S_SP            52
439
440 #define S_IP            48
441 #define S_FP            44
442 #define S_R10           40
443 #define S_R9            36
444 #define S_R8            32
445 #define S_R7            28
446 #define S_R6            24
447 #define S_R5            20
448 #define S_R4            16
449 #define S_R3            12
450 #define S_R2            8
451 #define S_R1            4
452 #define S_R0            0
453
454 #define MODE_SVC 0x13
455 #define I_BIT    0x80
456
457 /*
458  * use bad_save_user_regs for abort/prefetch/undef/swi ...
459  * use irq_save_user_regs / irq_restore_user_regs for IRQ/FIQ handling
460  */
461
462         .macro  bad_save_user_regs
463         sub     sp, sp, #S_FRAME_SIZE
464         stmia   sp, {r0 - r12}                  @ Calling r0-r12
465 #if defined(CONFIG_SYS_ARM_WITHOUT_RELOC)
466         ldr     r2, _armboot_start
467         sub     r2, r2, #(CONFIG_STACKSIZE+CONFIG_SYS_MALLOC_LEN)
468         sub     r2, r2, #(CONFIG_SYS_GBL_DATA_SIZE+8)  @ set base 2 words into abort stack
469 #else
470         ldr     r2, IRQ_STACK_START_IN
471 #endif
472         ldmia   r2, {r2 - r3}                   @ get pc, cpsr
473         add     r0, sp, #S_FRAME_SIZE           @ restore sp_SVC
474
475         add     r5, sp, #S_SP
476         mov     r1, lr
477         stmia   r5, {r0 - r3}                   @ save sp_SVC, lr_SVC, pc, cpsr
478         mov     r0, sp
479         .endm
480
481         .macro  irq_save_user_regs
482         sub     sp, sp, #S_FRAME_SIZE
483         stmia   sp, {r0 - r12}                  @ Calling r0-r12
484         add     r8, sp, #S_PC
485         stmdb   r8, {sp, lr}^                   @ Calling SP, LR
486         str     lr, [r8, #0]                    @ Save calling PC
487         mrs     r6, spsr
488         str     r6, [r8, #4]                    @ Save CPSR
489         str     r0, [r8, #8]                    @ Save OLD_R0
490         mov     r0, sp
491         .endm
492
493         .macro  irq_restore_user_regs
494         ldmia   sp, {r0 - lr}^                  @ Calling r0 - lr
495         mov     r0, r0
496         ldr     lr, [sp, #S_PC]                 @ Get PC
497         add     sp, sp, #S_FRAME_SIZE
498         subs    pc, lr, #4                      @ return & move spsr_svc into cpsr
499         .endm
500
501         .macro get_bad_stack
502 #if defined(CONFIG_SYS_ARM_WITHOUT_RELOC)
503         ldr     r13, _armboot_start             @ setup our mode stack
504         sub     r13, r13, #(CONFIG_STACKSIZE+CONFIG_SYS_MALLOC_LEN)
505         sub     r13, r13, #(CONFIG_SYS_GBL_DATA_SIZE+8) @ reserved a couple spots in abort stack
506 #else
507         ldr     r13, IRQ_STACK_START_IN         @ setup our mode stack
508 #endif
509
510         str     lr, [r13]                       @ save caller lr / spsr
511         mrs     lr, spsr
512         str     lr, [r13, #4]
513
514         mov     r13, #MODE_SVC                  @ prepare SVC-Mode
515         @ msr   spsr_c, r13
516         msr     spsr, r13
517         mov     lr, pc
518         movs    pc, lr
519         .endm
520
521         .macro get_irq_stack                    @ setup IRQ stack
522         ldr     sp, IRQ_STACK_START
523         .endm
524
525         .macro get_fiq_stack                    @ setup FIQ stack
526         ldr     sp, FIQ_STACK_START
527         .endm
528
529 /*
530  * exception handlers
531  */
532         .align  5
533 undefined_instruction:
534         get_bad_stack
535         bad_save_user_regs
536         bl      do_undefined_instruction
537
538         .align  5
539 software_interrupt:
540         get_bad_stack
541         bad_save_user_regs
542         bl      do_software_interrupt
543
544         .align  5
545 prefetch_abort:
546         get_bad_stack
547         bad_save_user_regs
548         bl      do_prefetch_abort
549
550         .align  5
551 data_abort:
552         get_bad_stack
553         bad_save_user_regs
554         bl      do_data_abort
555
556         .align  5
557 not_used:
558         get_bad_stack
559         bad_save_user_regs
560         bl      do_not_used
561
562 #ifdef CONFIG_USE_IRQ
563
564         .align  5
565 irq:
566         get_irq_stack
567         irq_save_user_regs
568         bl      do_irq
569         irq_restore_user_regs
570
571         .align  5
572 fiq:
573         get_fiq_stack
574         /* someone ought to write a more effiction fiq_save_user_regs */
575         irq_save_user_regs
576         bl      do_fiq
577         irq_restore_user_regs
578
579 #else
580
581         .align  5
582 irq:
583         get_bad_stack
584         bad_save_user_regs
585         bl      do_irq
586
587         .align  5
588 fiq:
589         get_bad_stack
590         bad_save_user_regs
591         bl      do_fiq
592
593 #endif
594
595         .align  5
596 .globl reset_cpu
597 reset_cpu:
598         bl      disable_interrupts
599
600         /* Disable watchdog */
601         ldr     r1, =pWDTCTL
602         mov     r3, #0
603         str     r3, [r1]
604
605         /* reset counter */
606         ldr     r3, =0x00001984
607         str     r3, [r1, #4]
608
609         /* Enable the watchdog */
610         mov     r3, #1
611         str     r3, [r1]
612
613 _loop_forever:
614         b       _loop_forever