1 /**************************************************************************//**
2 * @file system_MBRZA1H.c
3 * @brief CMSIS Device System Source File for
10 ******************************************************************************/
11 /* Copyright (c) 2011 - 2013 ARM LIMITED
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16 - Redistributions of source code must retain the above copyright
17 notice, this list of conditions and the following disclaimer.
18 - Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21 - Neither the name of ARM nor the names of its contributors may be used
22 to endorse or promote products derived from this software without
23 specific prior written permission.
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36 ---------------------------------------------------------------------------*/
41 #include "RZ_A1_Init.h"
44 #if defined(__ARMCC_VERSION)
45 extern void $Super$$main(void);
46 __asm void FPUEnable(void);
52 uint32_t IRQNestLevel;
55 #if defined(__ARMCC_VERSION)
57 * Initialize the cache.
62 * @brief Initialise caches. Requires PL1, so implemented as an SVC in case threads are USR mode.
67 void InitMemorySubsystem(void) {
69 /* This SVC is specific for reset where data / tlb / btac may contain undefined data, therefore before
70 * enabling the cache you must invalidate the instruction cache, the data cache, TLB, and BTAC.
71 * You are not required to invalidate the main TLB, even though it is recommended for safety
72 * reasons. This ensures compatibility with future revisions of the processor. */
76 /* Invalidate undefined data */
78 __v7_inv_icache_all();
79 __v7_inv_dcache_all();
82 /* Don't use this function during runtime since caches may contain valid data. For a correct cache maintenance you may need to execute a clean and
83 * invalidate in order to flush the valid data to the next level cache.
87 /* After MMU is enabled and data has been invalidated, enable caches and BTAC */
91 /* If present, you may also need to Invalidate and Enable L2 cache here */
92 l2_id = PL310_GetID();
101 #elif defined(__GNUC__)
103 void InitMemorySubsystem(void) {
105 /* This SVC is specific for reset where data / tlb / btac may contain undefined data, therefore before
106 * enabling the cache you must invalidate the instruction cache, the data cache, TLB, and BTAC.
107 * You are not required to invalidate the main TLB, even though it is recommended for safety
108 * reasons. This ensures compatibility with future revisions of the processor. */
112 /* Invalidate undefined data */
113 __ca9u_inv_tlb_all();
114 __v7_inv_icache_all();
115 __v7_inv_dcache_all();
118 /* Don't use this function during runtime since caches may contain valid data. For a correct cache maintenance you may need to execute a clean and
119 * invalidate in order to flush the valid data to the next level cache.
123 /* After MMU is enabled and data has been invalidated, enable caches and BTAC */
127 /* If present, you may also need to Invalidate and Enable L2 cache here */
128 l2_id = PL310_GetID();
140 IRQHandler IRQTable[Renesas_RZ_A1_IRQ_MAX+1];
142 uint32_t IRQCount = sizeof IRQTable / 4;
144 uint32_t InterruptHandlerRegister (IRQn_Type irq, IRQHandler handler)
146 if (irq < IRQCount) {
147 IRQTable[irq] = handler;
155 uint32_t InterruptHandlerUnregister (IRQn_Type irq)
157 if (irq < IRQCount) {
167 * Initialize the system
172 * @brief Setup the microcontroller system.
173 * Initialize the System.
175 void SystemInit (void)
178 /* do not use global variables because this function is called before
179 reaching pre-main. RW section maybe overwritten afterwards. */
183 //Configure GIC ICDICFR GIC_SetICDICFR()
190 //Fault Status Register (IFSR/DFSR) definitions
191 #define FSR_ALIGNMENT_FAULT 0x01 //DFSR only. Fault on first lookup
192 #define FSR_INSTRUCTION_CACHE_MAINTAINANCE 0x04 //DFSR only - async/external
193 #define FSR_SYNC_EXT_TTB_WALK_FIRST 0x0c //sync/external
194 #define FSR_SYNC_EXT_TTB_WALK_SECOND 0x0e //sync/external
195 #define FSR_SYNC_PARITY_TTB_WALK_FIRST 0x1c //sync/external
196 #define FSR_SYNC_PARITY_TTB_WALK_SECOND 0x1e //sync/external
197 #define FSR_TRANSLATION_FAULT_FIRST 0x05 //MMU Fault - internal
198 #define FSR_TRANSLATION_FAULT_SECOND 0x07 //MMU Fault - internal
199 #define FSR_ACCESS_FLAG_FAULT_FIRST 0x03 //MMU Fault - internal
200 #define FSR_ACCESS_FLAG_FAULT_SECOND 0x06 //MMU Fault - internal
201 #define FSR_DOMAIN_FAULT_FIRST 0x09 //MMU Fault - internal
202 #define FSR_DOMAIN_FAULT_SECOND 0x0b //MMU Fault - internal
203 #define FSR_PERMISION_FAULT_FIRST 0x0f //MMU Fault - internal
204 #define FSR_PERMISION_FAULT_SECOND 0x0d //MMU Fault - internal
205 #define FSR_DEBUG_EVENT 0x02 //internal
206 #define FSR_SYNC_EXT_ABORT 0x08 //sync/external
207 #define FSR_TLB_CONFLICT_ABORT 0x10 //sync/external
208 #define FSR_LOCKDOWN 0x14 //internal
209 #define FSR_COPROCESSOR_ABORT 0x1a //internal
210 #define FSR_SYNC_PARITY_ERROR 0x19 //sync/external
211 #define FSR_ASYNC_EXTERNAL_ABORT 0x16 //DFSR only - async/external
212 #define FSR_ASYNC_PARITY_ERROR 0x18 //DFSR only - async/external
214 void CDAbtHandler(uint32_t DFSR, uint32_t DFAR, uint32_t LR) {
215 uint32_t FS = (DFSR & (1 << 10)) >> 6 | (DFSR & 0x0f); //Store Fault Status
218 //Synchronous parity errors - retry
219 case FSR_SYNC_PARITY_ERROR:
220 case FSR_SYNC_PARITY_TTB_WALK_FIRST:
221 case FSR_SYNC_PARITY_TTB_WALK_SECOND:
224 //Your code here. Value in DFAR is invalid for some fault statuses.
225 case FSR_ALIGNMENT_FAULT:
226 case FSR_INSTRUCTION_CACHE_MAINTAINANCE:
227 case FSR_SYNC_EXT_TTB_WALK_FIRST:
228 case FSR_SYNC_EXT_TTB_WALK_SECOND:
229 case FSR_TRANSLATION_FAULT_FIRST:
230 case FSR_TRANSLATION_FAULT_SECOND:
231 case FSR_ACCESS_FLAG_FAULT_FIRST:
232 case FSR_ACCESS_FLAG_FAULT_SECOND:
233 case FSR_DOMAIN_FAULT_FIRST:
234 case FSR_DOMAIN_FAULT_SECOND:
235 case FSR_PERMISION_FAULT_FIRST:
236 case FSR_PERMISION_FAULT_SECOND:
237 case FSR_DEBUG_EVENT:
238 case FSR_SYNC_EXT_ABORT:
239 case FSR_TLB_CONFLICT_ABORT:
241 case FSR_COPROCESSOR_ABORT:
242 case FSR_ASYNC_EXTERNAL_ABORT: //DFAR invalid
243 case FSR_ASYNC_PARITY_ERROR: //DFAR invalid
249 void CPAbtHandler(uint32_t IFSR, uint32_t IFAR, uint32_t LR) {
250 uint32_t FS = (IFSR & (1 << 10)) >> 6 | (IFSR & 0x0f); //Store Fault Status
253 //Synchronous parity errors - retry
254 case FSR_SYNC_PARITY_ERROR:
255 case FSR_SYNC_PARITY_TTB_WALK_FIRST:
256 case FSR_SYNC_PARITY_TTB_WALK_SECOND:
259 //Your code here. Value in IFAR is invalid for some fault statuses.
260 case FSR_SYNC_EXT_TTB_WALK_FIRST:
261 case FSR_SYNC_EXT_TTB_WALK_SECOND:
262 case FSR_TRANSLATION_FAULT_FIRST:
263 case FSR_TRANSLATION_FAULT_SECOND:
264 case FSR_ACCESS_FLAG_FAULT_FIRST:
265 case FSR_ACCESS_FLAG_FAULT_SECOND:
266 case FSR_DOMAIN_FAULT_FIRST:
267 case FSR_DOMAIN_FAULT_SECOND:
268 case FSR_PERMISION_FAULT_FIRST:
269 case FSR_PERMISION_FAULT_SECOND:
270 case FSR_DEBUG_EVENT: //IFAR invalid
271 case FSR_SYNC_EXT_ABORT:
272 case FSR_TLB_CONFLICT_ABORT:
274 case FSR_COPROCESSOR_ABORT:
280 //returns amount to decrement lr by
281 //this will be 0 when we have emulated the instruction and simply want to execute the next instruction
282 //this will be 2 when we have performed some maintenance and want to retry the instruction in thumb (state == 2)
283 //this will be 4 when we have performed some maintenance and want to retry the instruction in arm (state == 4)
284 uint32_t CUndefHandler(uint32_t opcode, uint32_t state, uint32_t LR) {
285 const unsigned int THUMB = 2;
286 const unsigned int ARM = 4;
287 //Lazy VFP/NEON initialisation and switching
288 if ((state == ARM && ((opcode & 0x0C000000)) >> 26 == 0x03) ||
289 (state == THUMB && ((opcode & 0xEC000000)) >> 26 == 0x3B)) {
290 if (((opcode & 0x00000E00) >> 9) == 5) { //fp instruction?
296 //Add code here for other Undef cases
300 #if defined(__ARMCC_VERSION)
303 //Critical section, called from undef handler, so systick is disabled
304 __asm void FPUEnable(void) {
307 //Permit access to VFP registers by modifying CPACR
309 ORR R1,R1,#0x00F00000
314 ORR R1,R1,#0x40000000
317 //Initialise VFP registers to 0
336 //Initialise FPSCR to a known state
338 LDR R3,=0x00086060 //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
346 #elif defined(__GNUC__)
349 __asm__ __volatile__ (
352 "mrc p15,0,r1,c1,c0,2 \n\t"
353 "orr r1,r1,#0x00f00000 \n\t"
354 "mcr p15,0,r1,c1,c0,2 \n\t"
356 "orr r1,r1,#0x40000000 \n\t"
359 "vmov d0, r2,r2 \n\t"
360 "vmov d1, r2,r2 \n\t"
361 "vmov d2, r2,r2 \n\t"
362 "vmov d3, r2,r2 \n\t"
363 "vmov d4, r2,r2 \n\t"
364 "vmov d5, r2,r2 \n\t"
365 "vmov d6, r2,r2 \n\t"
366 "vmov d7, r2,r2 \n\t"
367 "vmov d8, r2,r2 \n\t"
368 "vmov d9, r2,r2 \n\t"
369 "vmov d10,r2,r2 \n\t"
370 "vmov d11,r2,r2 \n\t"
371 "vmov d12,r2,r2 \n\t"
372 "vmov d13,r2,r2 \n\t"
373 "vmov d14,r2,r2 \n\t"
374 "vmov d15,r2,r2 \n\t"
376 "ldr r3,=0x00086060 \n\t"