1 @******************************************************************************
2 @
3 @ diag_entry.S - Init code routines
4 @
5 @******************************************************************************
6 @
7 @ Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
8 @
9 @
10 @ Redistribution and use in source and binary forms, with or without
11 @ modification, are permitted provided that the following conditions
12 @ are met:
13 @
14 @ Redistributions of source code must retain the above copyright
15 @ notice, this list of conditions and the following disclaimer.
16 @
17 @ Redistributions in binary form must reproduce the above copyright
18 @ notice, this list of conditions and the following disclaimer in the
19 @ documentation and/or other materials provided with the
20 @ distribution.
21 @
22 @ Neither the name of Texas Instruments Incorporated nor the names of
23 @ its contributors may be used to endorse or promote products derived
24 @ from this software without specific prior written permission.
25 @
26 @ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 @ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 @ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 @ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 @ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 @ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 @ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 @ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 @ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 @ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 @ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 @
38 @******************************************************************************
40 @****************************** Global Symbols*********************************
41 .global Entry
42 .global _stack
43 .global _bss_start
44 .global _bss_end
45 .global start_boot
46 .global IRQHandler
47 .global FIQHandler
48 .global AbortHandler
49 .global SVC_Handler
50 .global UndefInstHandler
51 .global INTCCommonIntrHandler
53 @************************ Internal Definitions ********************************
54 @
55 @ Define the stack sizes for different modes. The user/system mode will use
56 @ the rest of the total stack size
57 @
58 .set UND_STACK_SIZE, 0x8
59 .set ABT_STACK_SIZE, 0x8
60 .set FIQ_STACK_SIZE, 0x8
61 .set IRQ_STACK_SIZE, 0x1000
62 .set SVC_STACK_SIZE, 0x8
64 @
65 @ to set the mode bits in CPSR for different modes
66 @
67 .set MODE_USR, 0x10
68 .set MODE_FIQ, 0x11
69 .set MODE_IRQ, 0x12
70 .set MODE_SVC, 0x13
71 .set MODE_ABT, 0x17
72 .set MODE_UND, 0x1B
73 .set MODE_SYS, 0x1F
75 .equ I_F_BIT, 0xC0
76 .equ MASK_SVC_NUM, 0x1
78 @**************************** Code Seection ***********************************
79 .text
81 @
82 @ This code is assembled for ARM instructions
83 @
84 .code 32
86 @******************************************************************************
87 @
88 @******************************************************************************
89 @
90 @ The reset handler in StarterWare is named as 'Entry'.
91 @ The reset handler sets up the stack pointers for all the modes. The FIQ and
92 @ IRQ shall be disabled during this. Then clear the BSS sections and finally
93 @ switch to the function calling the main() function.
94 @
95 Entry:
96 @STMFD sp!, {lr} @ Store link reg
97 MOV r7, sp
98 MOV r8, lr
99 LDR r9, =0xfeedfeed
100 CMP r0, r9
101 BEQ Diag_jump
102 MOV r9, #0
104 Diag_jump:
106 @
107 @ Set up the Stack for Undefined mode
108 @
109 LDR r0, =_stack @ Read the stack address
110 MSR cpsr_c, #MODE_UND|I_F_BIT @ switch to undef mode
111 MOV sp,r0 @ write the stack pointer
112 SUB r0, r0, #UND_STACK_SIZE @ give stack space
113 @
114 @ Set up the Stack for abort mode
115 @
116 MSR cpsr_c, #MODE_ABT|I_F_BIT @ Change to abort mode
117 MOV sp, r0 @ write the stack pointer
118 SUB r0,r0, #ABT_STACK_SIZE @ give stack space
119 @
120 @ Set up the Stack for FIQ mode
121 @
122 MSR cpsr_c, #MODE_FIQ|I_F_BIT @ change to FIQ mode
123 MOV sp,r0 @ write the stack pointer
124 SUB r0,r0, #FIQ_STACK_SIZE @ give stack space
125 @
126 @ Set up the Stack for IRQ mode
127 @
128 MSR cpsr_c, #MODE_IRQ|I_F_BIT @ change to IRQ mode
129 MOV sp,r0 @ write the stack pointer
130 SUB r0,r0, #IRQ_STACK_SIZE @ give stack space
131 @
132 @ Set up the Stack for SVC mode
133 @
134 MSR cpsr_c, #MODE_SVC|I_F_BIT @ change to SVC mode
135 MOV sp,r0 @ write the stack pointer
136 SUB r0,r0, #SVC_STACK_SIZE @ give stack space
137 @
138 @ Set up the Stack for USer/System mode
139 @
140 MSR cpsr_c, #MODE_SYS|I_F_BIT @ change to system mode
141 MOV sp,r0 @ write the stack pointer
143 @ Invalidate and Enable Branch Prediction
144 MOV r0, #0
145 MCR p15, #0, r0, c7, c5, #6
146 ISB
147 MRC p15, #0, r0, c1, c0, #0
148 ORR r0, r0, #0x00000800
149 MCR p15, #0, r0, c1, c0, #0
151 @
152 @ Enable Neon/VFP Co-Processor
153 @
154 MRC p15, #0, r1, c1, c0, #2 @ r1 = Access Control Register
155 ORR r1, r1, #(0xf << 20) @ enable full access for p10,11
156 MCR p15, #0, r1, c1, c0, #2 @ Access Control Register = r1
157 MOV r1, #0
158 MCR p15, #0, r1, c7, c5, #4 @ flush prefetch buffer
159 MOV r0,#0x40000000
160 FMXR FPEXC, r0 @ Set Neon/VFP Enable bit
162 Diag_save_lr:
163 CMP r9, #0
164 BEQ Clear_Bss_Section
165 STMFD sp!, {r7, r8}
168 @
169 @ Clear the BSS section here
170 @
171 Clear_Bss_Section:
173 LDR r0, =_bss_start @ Start address of BSS
174 LDR r1, =(_bss_end - 0x04) @ End address of BSS
175 MOV r2, #0
176 Loop:
177 STR r2, [r0], #4 @ Clear one word in BSS
178 CMP r0, r1
179 BLE Loop @ Clear till BSS end
180 B Diag_return_main
182 @******************************************************************************
183 @* Function Definition of SWI Handler
184 @******************************************************************************
185 @
186 @ The SVC Handler switches to system mode if the SVC number is 1. If the
187 @ SVC number is different, no mode switching will be done. No other SVC are
188 @ handled here
189 @
190 SVC_Handler:
191 STMFD r13!, {r0-r1, r14} @ Save context in SVC stack
192 SUB r13, r13, #0x4 @ Adjust the stack pointer
193 LDR r0, [r14, #-4] @ R0 points to SWI instruction
194 BIC r0, r0, #MASK_SVC_NUM @ Get the SWI number
195 CMP r0, #458752
196 MRSEQ r1, spsr @ Copy SPSR
197 ORREQ r1, r1, #0x1F @ Change the mode to System
198 MSREQ spsr_cf, r1 @ Restore SPSR
199 ADD r13, r13, #0x4 @ Adjust the stack pointer
200 LDMFD r13!, {r0-r1, pc}^ @ Restore registers from IRQ stack
202 @******************************************************************************
203 @* Function Definition of IRQ Handler
204 @******************************************************************************
205 @
206 @ The IRQ handler saves the context and jumps to the common handler which calls
207 @ the handlers of highest priority pending IRQ.
208 @
209 IRQHandler:
210 SUB r14, r14, #4 @ Apply lr correction
211 STMFD r13!, {r0-r3, r12, r14} @ Save context
212 MRS r12, spsr @ Copy spsr
213 VMRS r1, FPSCR @ Copy fpscr
214 VMRS r2, FPEXC @ Copy fpexc
215 STMFD r13!, {r1-r2, r12} @ Save spsr, fpscr and fpexc
216 VSTMDB r13!, {d0-d7} @ Save D0-D7 Neon/VFP registers
217 VSTMDB r13!, {d16-d31} @ Save D16-D31 Neon/VFP registers
219 bl INTCCommonIntrHandler
221 VLDMIA r13!, {d16-d31} @ Restore D16-D31 Neon/VFP registers
222 VLDMIA r13!, {d0-d7} @ Restore D0-D7 Neon/VFP registers
223 LDMFD r13!, {r1-r2, r12} @ Get fpscr, fpexc and spsr
224 MSR spsr, r12 @ Restore spsr
225 VMSR fpscr, r1 @ Restore fpscr
226 VMSR fpexc, r2 @ Restore fpexc
228 LDMFD r13!, {r0-r3, r12, pc}^ @ Restore the context and return
230 @******************************************************************************
231 @* Function Definition of FIQ Handler
232 @******************************************************************************
233 @
234 @ FIQ is not supported in this version of software.
235 @
236 FIQHandler:
237 SUBS pc, r14, #0x4 @ Return to program before FIQ
239 @******************************************************************************
240 @* Function Definition of Abort/Undef Handler
241 @******************************************************************************
242 @
243 @ The Abort handler goes to the C handler of abort mode. Note that the undefined
244 @ instruction is not handled separately.
245 @ if nothing is done in the abort mode, the execution enters infinite loop.
246 @
247 AbortHandler:
248 UndefInstHandler:
249 @
250 @ Go to infinite loop.
251 @
252 loop0:
253 B loop0
255 Diag_return_main:
256 LDR r10, =main @ Get the address of main
257 MOV lr,pc @ Dummy return to main
258 BX r10 @ Branch to main
260 Diag_return:
261 MOV r9, r0
262 LDMFD sp!, {pc, sp}
264 @
265 @ End of the file
266 @
267 .end