]> rtime.felk.cvut.cz Git - arc.git/blob - arch/arm/arm_cr4/kernel/startup_cr4.s
Fixed assembler code bug causing erronous startup sequence.
[arc.git] / arch / arm / arm_cr4 / kernel / startup_cr4.s
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15     \r
16   .syntax unified\r
17         .cpu cortex-r4\r
18         .fpu softvfp\r
19         .thumb\r
20 \r
21 .global g_pfnVectors\r
22 .global Default_Handler\r
23 \r
24 .word   _sidata\r
25 .word   _sdata\r
26 .word   _edata\r
27 .word   _sbss\r
28 .word   _ebss\r
29 \r
30 .word _estack\r
31 \r
32 \r
33 /**\r
34  * @brief  This is the code that gets called when the processor first\r
35  *          starts execution following a reset event. Only the absolutely\r
36  *          necessary set is performed, after which the application\r
37  *          supplied main() routine is called. \r
38  * @param  None\r
39  * @retval : None\r
40 */\r
41     .section    .text.Reset_Handler\r
42         .weak   Reset_Handler\r
43         .type   Reset_Handler, %function\r
44 Reset_Handler:  \r
45 \r
46 /* Set big endian state */\r
47         SETEND BE\r
48 \r
49 /* Copy the data segment initializers from flash to SRAM */\r
50         movs    r1, #0\r
51 \r
52 \r
53 Init_Registers:\r
54 \r
55         mov   r0,         #0x0000\r
56         mov   r1,         #0x0000\r
57         mov   r2,         #0x0000\r
58         mov   r3,         #0x0000\r
59         mov   r4,         #0x0000\r
60         mov   r5,         #0x0000\r
61         mov   r6,         #0x0000\r
62         mov   r7,         #0x0000\r
63         mov   r8,         #0x0000\r
64         mov   r9,         #0x0000\r
65         mov   r10,        #0x0000\r
66         mov   r11,        #0x0000\r
67         mov   r12,        #0x0000\r
68         mov   r1,         #0x03D0\r
69         orr   r2,        r1,     #0x0001\r
70         msr   cpsr_cxsf,  r2\r
71         msr   spsr_cxsf,  r2\r
72         mov   r8,         #0x0000\r
73         mov   r9,         #0x0000\r
74         mov   r10,        #0x0000\r
75         mov   r11,        #0x0000\r
76         mov   r12,        #0x0000\r
77         orr   r12,        r1,     #0x0002\r
78         msr   cpsr_c,     r12\r
79         msr   spsr_cxsf,  r12\r
80         orr   r12,        r1,     #0x0007\r
81         msr   cpsr_c,     r12\r
82         msr   spsr_cxsf,  r12\r
83         orr   r12,        r1,     #0x000B\r
84         msr   cpsr_c,     r12\r
85         msr   spsr_cxsf,  r12\r
86         orr   r12,        r1,     #0x0003\r
87         msr   cpsr_c,     r12\r
88         msr   spsr_cxsf,  r12\r
89 \r
90         /* System level configuration */\r
91         mrc   p15,0,r11,c1,c0,0       /* Read current system configuration */\r
92         mov   r12,                #0x40000000 /* Set THUMB instruction set mode for interrupts and exceptions */\r
93         orr   r12, r12, r11\r
94         mcr   p15,0,r12,c1,c0,0       /* Write new configuration */\r
95 \r
96 \r
97 Init_Stack_Pointers:\r
98 \r
99                 mov   r2,               #0xD1\r
100         msr   cpsr_c,   r2\r
101         ldr   sp,               =_estack\r
102 \r
103         mov   r2,               #0xD2\r
104         msr   cpsr_c,   r2\r
105         ldr   sp,               =_estack\r
106 \r
107         mov   r2,               #0xD7\r
108         msr   cpsr_c,   r2\r
109         ldr   sp,               =_estack\r
110 \r
111         mov   r2,               #0xDB\r
112         msr   cpsr_c,   r2\r
113         ldr   sp,               =_estack\r
114 \r
115         mov   r2,               #0xDF\r
116         msr   cpsr_c,   r2\r
117         ldr   sp,               =_estack\r
118 \r
119         mov   r2,               #0xD3\r
120         msr   cpsr_c,   r2\r
121         ldr   sp,               =_estack\r
122 \r
123 \r
124 CopyInitializedData:\r
125         ldr     r0, =_sdata       /* r0 holds start of data in ram */\r
126         ldr     r3, =_edata       /* r3 holds end of data in ram */\r
127         ldr     r5, =_sidata      /* r5 start of data in flash */\r
128         movs  r1,       #0    /* r1 is the counter */\r
129         b       LoopCopyDataInit\r
130 \r
131 CopyDataInit:\r
132         ldr     r4, [r5, r1]          /* read current position in flash */\r
133         str     r4, [r0, r1]          /* store current position in ram */\r
134         adds    r1, r1, #4        /* increment counter */\r
135     \r
136 LoopCopyDataInit:\r
137         adds    r2, r0, r1        /* are we at the final position? */\r
138         cmp     r2, r3                /* ... */\r
139         bcc     CopyDataInit          /* nope, continue */\r
140 \r
141 /* Fill zero areas */\r
142         ldr     r2, =_sbss            /* r2 holds the start address */\r
143         ldr r5, =_ebss            /* r5 holds the end address */\r
144         bl      LoopFillZero\r
145 \r
146         ldr     r2, =_sstack            /* r2 holds the start address */\r
147         ldr r5, =_estack            /* r5 holds the end address */\r
148         bl      LoopFillZero\r
149 \r
150 /* Call the application's entry point.*/\r
151         mov   r2,               #0xDF\r
152     msr   cpsr_c,   r2\r
153         bl      main\r
154         bx      lr\r
155 \r
156 /* Zero fill the bss segment. */  \r
157 FillZero:\r
158         movs    r3, #0\r
159         str         r3, [r2], #4\r
160     \r
161 LoopFillZero:\r
162         cmp     r2, r5\r
163         bcc     FillZero\r
164         bx  lr\r
165 \r
166 Dummy_Irq:\r
167         /* Go back to sys mode for easier debugging.\r
168          Save link register*/\r
169         mov   r3, lr\r
170         /* We don't want to use the IRQ mode\r
171            so swich back to sys mode. */\r
172         mov   r2,               #0xDF\r
173     msr   cpsr_c,   r2\r
174     /* Restore link register again */\r
175     mov   lr, r3\r
176         b Dummy_Irq\r
177 \r
178 .size   Reset_Handler, .-Reset_Handler\r
179 \r
180 /**\r
181  * @brief  This is the code that gets called when the processor receives an \r
182  *         unexpected interrupt.  This simply enters an infinite loop, preserving\r
183  *         the system state for examination by a debugger.\r
184  *\r
185  * @param  None     \r
186  * @retval : None       \r
187 */\r
188     .section    .text.Default_Handler,"ax",%progbits\r
189 Default_Handler:\r
190 Infinite_Loop:\r
191         b       Infinite_Loop\r
192         .size   Default_Handler, .-Default_Handler\r
193 \r
194 \r
195 /******************************************************************************\r
196 * Interrupt and exception vectors. Vectors start at addr 0x0.\r
197 ******************************************************************************/    \r
198         .section        .int_vecs,"ax",%progbits\r
199         .extern Irq_Handler\r
200 \r
201         b   Reset_Handler      /* Reset? */\r
202         b   Dummy_Irq          /* Undef? */\r
203         b   Irq_Handler        /* SVC, to be able to use SVC instruction. */\r
204         b   Dummy_Irq          /* Prefetch */\r
205         b   Dummy_Irq          /* data */\r
206         b   Dummy_Irq          /* ? */\r
207         b   Irq_Handler        /* IRQ */\r
208             b   Irq_Handler        /* FIR */\r
209 \r