]> begriffs open source - cmsis/blob - CMSIS/Core_A/Include/core_ca.h
Added GIC set/clear SGI pending flags handling
[cmsis] / CMSIS / Core_A / Include / core_ca.h
1 /**************************************************************************//**
2  * @file     core_ca.h
3  * @brief    CMSIS Cortex-A Core Peripheral Access Layer Header File
4  * @version  V1.00
5  * @date     22. Feb 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2017 ARM Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 #if defined ( __ICCARM__ )
26  #pragma system_include  /* treat file as system include file for MISRA check */
27 #endif
28
29 #ifdef __cplusplus
30  extern "C" {
31 #endif
32
33 #ifndef __CORE_CA_H_GENERIC
34 #define __CORE_CA_H_GENERIC
35
36
37 /*******************************************************************************
38  *                 CMSIS definitions
39  ******************************************************************************/
40
41 /*  CMSIS CA definitions */
42 #define __CA_CMSIS_VERSION_MAIN  (1U)                                      /*!< \brief [31:16] CMSIS HAL main version   */
43 #define __CA_CMSIS_VERSION_SUB   (0U)                                      /*!< \brief [15:0]  CMSIS HAL sub version    */
44 #define __CA_CMSIS_VERSION       ((__CA_CMSIS_VERSION_MAIN << 16U) | \
45                                    __CA_CMSIS_VERSION_SUB          )       /*!< \brief CMSIS HAL version number         */
46
47 #if defined ( __CC_ARM )
48   #if defined __TARGET_FPU_VFP
49     #if (__FPU_PRESENT == 1)
50       #define __FPU_USED       1U
51     #else
52       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
53       #define __FPU_USED       0U
54     #endif
55   #else
56     #define __FPU_USED         0U
57   #endif
58
59 #elif defined ( __ICCARM__ )
60   #if defined __ARMVFP__
61     #if (__FPU_PRESENT == 1)
62       #define __FPU_USED       1U
63     #else
64       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
65       #define __FPU_USED       0U
66     #endif
67   #else
68     #define __FPU_USED         0U
69   #endif
70
71 #elif defined ( __TMS470__ )
72   #if defined __TI_VFP_SUPPORT__
73     #if (__FPU_PRESENT == 1)
74       #define __FPU_USED       1U
75     #else
76       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
77       #define __FPU_USED       0U
78     #endif
79   #else
80     #define __FPU_USED         0U
81   #endif
82
83 #elif defined ( __GNUC__ )
84   #if defined (__VFP_FP__) && !defined(__SOFTFP__)
85     #if (__FPU_PRESENT == 1)
86       #define __FPU_USED       1U
87     #else
88       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
89       #define __FPU_USED       0U
90     #endif
91   #else
92     #define __FPU_USED         0U
93   #endif
94
95 #elif defined ( __TASKING__ )
96   #if defined __FPU_VFP__
97     #if (__FPU_PRESENT == 1)
98       #define __FPU_USED       1U
99     #else
100       #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
101       #define __FPU_USED       0U
102     #endif
103   #else
104     #define __FPU_USED         0U
105   #endif
106 #endif
107
108 #include "cmsis_compiler.h"               /* CMSIS compiler specific defines */
109
110 #ifdef __cplusplus
111 }
112 #endif
113
114 #endif /* __CORE_CA_H_GENERIC */
115
116 #ifndef __CMSIS_GENERIC
117
118 #ifndef __CORE_CA_H_DEPENDANT
119 #define __CORE_CA_H_DEPENDANT
120
121 #ifdef __cplusplus
122  extern "C" {
123 #endif
124
125  /* check device defines and use defaults */
126 #if defined __CHECK_DEVICE_DEFINES
127   #ifndef __CA_REV
128     #define __CA_REV              0x0000U
129     #warning "__CA_REV not defined in device header file; using default!"
130   #endif
131   
132   #ifndef __FPU_PRESENT
133     #define __FPU_PRESENT             0U
134     #warning "__FPU_PRESENT not defined in device header file; using default!"
135   #endif
136   
137   #ifndef __MPU_PRESENT
138     #define __MPU_PRESENT             0U
139     #warning "__MPU_PRESENT not defined in device header file; using default!"
140   #endif
141   
142   #ifndef __GIC_PRESENT
143     #define __GIC_PRESENT             1U
144     #warning "__GIC_PRESENT not defined in device header file; using default!"
145   #endif
146   
147   #ifndef __TIM_PRESENT
148     #define __TIM_PRESENT             1U
149     #warning "__TIM_PRESENT not defined in device header file; using default!"
150   #endif
151   
152   #ifndef __L2C_PRESENT
153     #define __L2C_PRESENT             0U
154     #warning "__L2C_PRESENT not defined in device header file; using default!"
155   #endif
156 #endif
157
158 /* IO definitions (access restrictions to peripheral registers) */
159 #ifdef __cplusplus
160   #define   __I     volatile             /*!< \brief Defines 'read only' permissions */
161 #else
162   #define   __I     volatile const       /*!< \brief Defines 'read only' permissions */
163 #endif
164 #define     __O     volatile             /*!< \brief Defines 'write only' permissions */
165 #define     __IO    volatile             /*!< \brief Defines 'read / write' permissions */
166
167 /* following defines should be used for structure members */
168 #define     __IM     volatile const      /*!< \brief Defines 'read only' structure member permissions */
169 #define     __OM     volatile            /*!< \brief Defines 'write only' structure member permissions */
170 #define     __IOM    volatile            /*!< \brief Defines 'read / write' structure member permissions */
171
172
173  /*******************************************************************************
174   *                 Register Abstraction
175    Core Register contain:
176    - CPSR
177    - CP15 Registers
178    - L2C-310 Cache Controller
179    - Generic Interrupt Controller Distributor
180    - Generic Interrupt Controller Interface
181   ******************************************************************************/
182
183 /* Core Register CPSR */
184 typedef union
185 {
186   struct
187   {
188     uint32_t M:5;                        /*!< \brief bit:  0.. 4  Mode field */
189     uint32_t T:1;                        /*!< \brief bit:      5  Thumb execution state bit */
190     uint32_t F:1;                        /*!< \brief bit:      6  FIQ mask bit */
191     uint32_t I:1;                        /*!< \brief bit:      7  IRQ mask bit */
192     uint32_t A:1;                        /*!< \brief bit:      8  Asynchronous abort mask bit */
193     uint32_t E:1;                        /*!< \brief bit:      9  Endianness execution state bit */
194     uint32_t IT1:6;                      /*!< \brief bit: 10..15  If-Then execution state bits 2-7 */
195     uint32_t GE:4;                       /*!< \brief bit: 16..19  Greater than or Equal flags */
196     uint32_t _reserved0:4;               /*!< \brief bit: 20..23  Reserved */
197     uint32_t J:1;                        /*!< \brief bit:     24  Jazelle bit */
198     uint32_t IT0:2;                      /*!< \brief bit: 25..26  If-Then execution state bits 0-1 */
199     uint32_t Q:1;                        /*!< \brief bit:     27  Saturation condition flag */
200     uint32_t V:1;                        /*!< \brief bit:     28  Overflow condition code flag */
201     uint32_t C:1;                        /*!< \brief bit:     29  Carry condition code flag */
202     uint32_t Z:1;                        /*!< \brief bit:     30  Zero condition code flag */
203     uint32_t N:1;                        /*!< \brief bit:     31  Negative condition code flag */
204   } b;                                   /*!< \brief Structure used for bit  access */
205   uint32_t w;                            /*!< \brief Type      used for word access */
206 } CPSR_Type;
207
208 /* CPSR Register Definitions */
209 #define CPSR_N_Pos                       31U                                    /*!< \brief CPSR: N Position */
210 #define CPSR_N_Msk                       (1UL << CPSR_N_Pos)                    /*!< \brief CPSR: N Mask */
211
212 #define CPSR_Z_Pos                       30U                                    /*!< \brief CPSR: Z Position */
213 #define CPSR_Z_Msk                       (1UL << CPSR_Z_Pos)                    /*!< \brief CPSR: Z Mask */
214
215 #define CPSR_C_Pos                       29U                                    /*!< \brief CPSR: C Position */
216 #define CPSR_C_Msk                       (1UL << CPSR_C_Pos)                    /*!< \brief CPSR: C Mask */
217
218 #define CPSR_V_Pos                       28U                                    /*!< \brief CPSR: V Position */
219 #define CPSR_V_Msk                       (1UL << CPSR_V_Pos)                    /*!< \brief CPSR: V Mask */
220
221 #define CPSR_Q_Pos                       27U                                    /*!< \brief CPSR: Q Position */
222 #define CPSR_Q_Msk                       (1UL << CPSR_Q_Pos)                    /*!< \brief CPSR: Q Mask */
223
224 #define CPSR_IT0_Pos                     25U                                    /*!< \brief CPSR: IT0 Position */
225 #define CPSR_IT0_Msk                     (3UL << CPSR_IT0_Pos)                  /*!< \brief CPSR: IT0 Mask */
226
227 #define CPSR_J_Pos                       24U                                    /*!< \brief CPSR: J Position */
228 #define CPSR_J_Msk                       (1UL << CPSR_J_Pos)                    /*!< \brief CPSR: J Mask */
229
230 #define CPSR_GE_Pos                      16U                                    /*!< \brief CPSR: GE Position */
231 #define CPSR_GE_Msk                      (0xFUL << CPSR_GE_Pos)                 /*!< \brief CPSR: GE Mask */
232
233 #define CPSR_IT1_Pos                     10U                                    /*!< \brief CPSR: IT1 Position */
234 #define CPSR_IT1_Msk                     (0x3FUL << CPSR_IT1_Pos)               /*!< \brief CPSR: IT1 Mask */
235
236 #define CPSR_E_Pos                       9U                                     /*!< \brief CPSR: E Position */
237 #define CPSR_E_Msk                       (1UL << CPSR_E_Pos)                    /*!< \brief CPSR: E Mask */
238
239 #define CPSR_A_Pos                       8U                                     /*!< \brief CPSR: A Position */
240 #define CPSR_A_Msk                       (1UL << CPSR_A_Pos)                    /*!< \brief CPSR: A Mask */
241
242 #define CPSR_I_Pos                       7U                                     /*!< \brief CPSR: I Position */
243 #define CPSR_I_Msk                       (1UL << CPSR_I_Pos)                    /*!< \brief CPSR: I Mask */
244
245 #define CPSR_F_Pos                       6U                                     /*!< \brief CPSR: F Position */
246 #define CPSR_F_Msk                       (1UL << CPSR_F_Pos)                    /*!< \brief CPSR: F Mask */
247
248 #define CPSR_T_Pos                       5U                                     /*!< \brief CPSR: T Position */
249 #define CPSR_T_Msk                       (1UL << CPSR_T_Pos)                    /*!< \brief CPSR: T Mask */
250
251 #define CPSR_M_Pos                       0U                                     /*!< \brief CPSR: M Position */
252 #define CPSR_M_Msk                       (0x1FUL << CPSR_M_Pos)                 /*!< \brief CPSR: M Mask */
253
254 /* CP15 Register SCTLR */
255 typedef union
256 {
257   struct
258   {
259     uint32_t M:1;                        /*!< \brief bit:     0  MMU enable */
260     uint32_t A:1;                        /*!< \brief bit:     1  Alignment check enable */
261     uint32_t C:1;                        /*!< \brief bit:     2  Cache enable */
262     uint32_t _reserved0:2;               /*!< \brief bit: 3.. 4  Reserved */
263     uint32_t CP15BEN:1;                  /*!< \brief bit:     5  CP15 barrier enable */
264     uint32_t _reserved1:1;               /*!< \brief bit:     6  Reserved */
265     uint32_t B:1;                        /*!< \brief bit:     7  Endianness model */
266     uint32_t _reserved2:2;               /*!< \brief bit: 8.. 9  Reserved */
267     uint32_t SW:1;                       /*!< \brief bit:    10  SWP and SWPB enable */
268     uint32_t Z:1;                        /*!< \brief bit:    11  Branch prediction enable */
269     uint32_t I:1;                        /*!< \brief bit:    12  Instruction cache enable */
270     uint32_t V:1;                        /*!< \brief bit:    13  Vectors bit */
271     uint32_t RR:1;                       /*!< \brief bit:    14  Round Robin select */
272     uint32_t _reserved3:2;               /*!< \brief bit:15..16  Reserved */
273     uint32_t HA:1;                       /*!< \brief bit:    17  Hardware Access flag enable */
274     uint32_t _reserved4:1;               /*!< \brief bit:    18  Reserved */
275     uint32_t WXN:1;                      /*!< \brief bit:    19  Write permission implies XN */
276     uint32_t UWXN:1;                     /*!< \brief bit:    20  Unprivileged write permission implies PL1 XN */
277     uint32_t FI:1;                       /*!< \brief bit:    21  Fast interrupts configuration enable */
278     uint32_t U:1;                        /*!< \brief bit:    22  Alignment model */
279     uint32_t _reserved5:1;               /*!< \brief bit:    23  Reserved */
280     uint32_t VE:1;                       /*!< \brief bit:    24  Interrupt Vectors Enable */
281     uint32_t EE:1;                       /*!< \brief bit:    25  Exception Endianness */
282     uint32_t _reserved6:1;               /*!< \brief bit:    26  Reserved */
283     uint32_t NMFI:1;                     /*!< \brief bit:    27  Non-maskable FIQ (NMFI) support */
284     uint32_t TRE:1;                      /*!< \brief bit:    28  TEX remap enable. */
285     uint32_t AFE:1;                      /*!< \brief bit:    29  Access flag enable */
286     uint32_t TE:1;                       /*!< \brief bit:    30  Thumb Exception enable */
287     uint32_t _reserved7:1;               /*!< \brief bit:    31  Reserved */
288   } b;                                   /*!< \brief Structure used for bit  access */
289   uint32_t w;                            /*!< \brief Type      used for word access */
290 } SCTLR_Type;
291
292 #define SCTLR_TE_Pos                     30U                                    /*!< \brief SCTLR: TE Position */
293 #define SCTLR_TE_Msk                     (1UL << SCTLR_TE_Pos)                  /*!< \brief SCTLR: TE Mask */
294
295 #define SCTLR_AFE_Pos                    29U                                    /*!< \brief SCTLR: AFE Position */
296 #define SCTLR_AFE_Msk                    (1UL << SCTLR_AFE_Pos)                 /*!< \brief SCTLR: AFE Mask */
297
298 #define SCTLR_TRE_Pos                    28U                                    /*!< \brief SCTLR: TRE Position */
299 #define SCTLR_TRE_Msk                    (1UL << SCTLR_TRE_Pos)                 /*!< \brief SCTLR: TRE Mask */
300
301 #define SCTLR_NMFI_Pos                   27U                                    /*!< \brief SCTLR: NMFI Position */
302 #define SCTLR_NMFI_Msk                   (1UL << SCTLR_NMFI_Pos)                /*!< \brief SCTLR: NMFI Mask */
303
304 #define SCTLR_EE_Pos                     25U                                    /*!< \brief SCTLR: EE Position */
305 #define SCTLR_EE_Msk                     (1UL << SCTLR_EE_Pos)                  /*!< \brief SCTLR: EE Mask */
306
307 #define SCTLR_VE_Pos                     24U                                    /*!< \brief SCTLR: VE Position */
308 #define SCTLR_VE_Msk                     (1UL << SCTLR_VE_Pos)                  /*!< \brief SCTLR: VE Mask */
309
310 #define SCTLR_U_Pos                      22U                                    /*!< \brief SCTLR: U Position */
311 #define SCTLR_U_Msk                      (1UL << SCTLR_U_Pos)                   /*!< \brief SCTLR: U Mask */
312
313 #define SCTLR_FI_Pos                     21U                                    /*!< \brief SCTLR: FI Position */
314 #define SCTLR_FI_Msk                     (1UL << SCTLR_FI_Pos)                  /*!< \brief SCTLR: FI Mask */
315
316 #define SCTLR_UWXN_Pos                   20U                                    /*!< \brief SCTLR: UWXN Position */
317 #define SCTLR_UWXN_Msk                   (1UL << SCTLR_UWXN_Pos)                /*!< \brief SCTLR: UWXN Mask */
318
319 #define SCTLR_WXN_Pos                    19U                                    /*!< \brief SCTLR: WXN Position */
320 #define SCTLR_WXN_Msk                    (1UL << SCTLR_WXN_Pos)                 /*!< \brief SCTLR: WXN Mask */
321
322 #define SCTLR_HA_Pos                     17U                                    /*!< \brief SCTLR: HA Position */
323 #define SCTLR_HA_Msk                     (1UL << SCTLR_HA_Pos)                  /*!< \brief SCTLR: HA Mask */
324
325 #define SCTLR_RR_Pos                     14U                                    /*!< \brief SCTLR: RR Position */
326 #define SCTLR_RR_Msk                     (1UL << SCTLR_RR_Pos)                  /*!< \brief SCTLR: RR Mask */
327
328 #define SCTLR_V_Pos                      13U                                    /*!< \brief SCTLR: V Position */
329 #define SCTLR_V_Msk                      (1UL << SCTLR_V_Pos)                   /*!< \brief SCTLR: V Mask */
330
331 #define SCTLR_I_Pos                      12U                                    /*!< \brief SCTLR: I Position */
332 #define SCTLR_I_Msk                      (1UL << SCTLR_I_Pos)                   /*!< \brief SCTLR: I Mask */
333
334 #define SCTLR_Z_Pos                      11U                                    /*!< \brief SCTLR: Z Position */
335 #define SCTLR_Z_Msk                      (1UL << SCTLR_Z_Pos)                   /*!< \brief SCTLR: Z Mask */
336
337 #define SCTLR_SW_Pos                     10U                                    /*!< \brief SCTLR: SW Position */
338 #define SCTLR_SW_Msk                     (1UL << SCTLR_SW_Pos)                  /*!< \brief SCTLR: SW Mask */
339
340 #define SCTLR_B_Pos                      7U                                     /*!< \brief SCTLR: B Position */
341 #define SCTLR_B_Msk                      (1UL << SCTLR_B_Pos)                   /*!< \brief SCTLR: B Mask */
342
343 #define SCTLR_CP15BEN_Pos                5U                                     /*!< \brief SCTLR: CP15BEN Position */
344 #define SCTLR_CP15BEN_Msk                (1UL << SCTLR_CP15BEN_Pos)             /*!< \brief SCTLR: CP15BEN Mask */
345
346 #define SCTLR_C_Pos                      2U                                     /*!< \brief SCTLR: C Position */
347 #define SCTLR_C_Msk                      (1UL << SCTLR_C_Pos)                   /*!< \brief SCTLR: C Mask */
348
349 #define SCTLR_A_Pos                      1U                                     /*!< \brief SCTLR: A Position */
350 #define SCTLR_A_Msk                      (1UL << SCTLR_A_Pos)                   /*!< \brief SCTLR: A Mask */
351
352 #define SCTLR_M_Pos                      0U                                     /*!< \brief SCTLR: M Position */
353 #define SCTLR_M_Msk                      (1UL << SCTLR_M_Pos)                   /*!< \brief SCTLR: M Mask */
354
355 /* CP15 Register ACTLR */
356 typedef union
357 {
358   struct
359   {
360     uint32_t _reserved0:6;               /*!< bit: 0.. 5  Reserved */
361     uint32_t SMP:1;                      /*!< bit:     6  Enables coherent requests to the processor */
362     uint32_t _reserved1:3;               /*!< bit: 7.. 9  Reserved */
363     uint32_t DODMBS:1;                   /*!< bit:    10  Disable optimized data memory barrier behavior */
364     uint32_t L2RADIS:1;                  /*!< bit:    11  L2 Data Cache read-allocate mode disable */
365     uint32_t L1RADIS:1;                  /*!< bit:    12  L1 Data Cache read-allocate mode disable */
366     uint32_t L1PCTL:2;                   /*!< bit:13..14  L1 Data prefetch control */
367     uint32_t DDVM:1;                     /*!< bit:    15  Disable Distributed Virtual Memory (DVM) transactions */
368     uint32_t _reserved3:12;              /*!< bit:16..27  Reserved */
369     uint32_t DDI:1;                      /*!< bit:    28  Disable dual issue */
370     uint32_t _reserved7:3;               /*!< bit:29..31  Reserved */
371   } b;                                   /*!< Structure used for bit  access */
372   uint32_t w;                            /*!< Type      used for word access */
373 } ACTLR_Type;
374
375 #define ACTLR_DDI_Pos                    28U                                     /*!< ACTLR: DDI Position */
376 #define ACTLR_DDI_Msk                    (1UL << ACTLR_DDI_Pos)                  /*!< ACTLR: DDI Mask */
377
378 #define ACTLR_DDVM_Pos                   15U                                     /*!< ACTLR: DDVM Position */
379 #define ACTLR_DDVM_Msk                   (1UL << ACTLR_DDVM_Pos)                 /*!< ACTLR: DDVM Mask */
380
381 #define ACTLR_L1PCTL_Pos                 13U                                     /*!< ACTLR: L1PCTL Position */
382 #define ACTLR_L1PCTL_Msk                 (3UL << ACTLR_L1PCTL_Pos)               /*!< ACTLR: L1PCTL Mask */
383
384 #define ACTLR_L1RADIS_Pos                12U                                     /*!< ACTLR: L1RADIS Position */
385 #define ACTLR_L1RADIS_Msk                (1UL << ACTLR_L1RADIS_Pos)              /*!< ACTLR: L1RADIS Mask */
386
387 #define ACTLR_L2RADIS_Pos                11U                                     /*!< ACTLR: L2RADIS Position */
388 #define ACTLR_L2RADIS_Msk                (1UL << ACTLR_L2RADIS_Pos)              /*!< ACTLR: L2RADIS Mask */
389
390 #define ACTLR_DODMBS_Pos                 10U                                     /*!< ACTLR: DODMBS Position */
391 #define ACTLR_DODMBS_Msk                 (1UL << ACTLR_DODMBS_Pos)               /*!< ACTLR: DODMBS Mask */
392
393 #define ACTLR_SMP_Pos                    6U                                      /*!< ACTLR: SMP Position */
394 #define ACTLR_SMP_Msk                    (1UL << ACTLR_SMP_Pos)                  /*!< ACTLR: SMP Mask */
395
396 /* CP15 Register CPACR */
397 typedef union
398 {
399   struct
400   {
401     uint32_t _reserved0:20;              /*!< \brief bit: 0..19  Reserved */
402     uint32_t cp10:2;                     /*!< \brief bit:20..21  Access rights for coprocessor 10 */
403     uint32_t cp11:2;                     /*!< \brief bit:22..23  Access rights for coprocessor 11 */
404     uint32_t _reserved1:6;               /*!< \brief bit:24..29  Reserved */
405     uint32_t D32DIS:1;                   /*!< \brief bit:    30  Disable use of registers D16-D31 of the VFP register file */
406     uint32_t ASEDIS:1;                   /*!< \brief bit:    31  Disable Advanced SIMD Functionality */
407   } b;                                   /*!< \brief Structure used for bit  access */
408   uint32_t w;                            /*!< \brief Type      used for word access */
409 } CPACR_Type;
410
411 #define CPACR_ASEDIS_Pos                 31U                                    /*!< \brief CPACR: ASEDIS Position */
412 #define CPACR_ASEDIS_Msk                 (1UL << CPACR_ASEDIS_Pos)              /*!< \brief CPACR: ASEDIS Mask */
413
414 #define CPACR_D32DIS_Pos                 30U                                    /*!< \brief CPACR: D32DIS Position */
415 #define CPACR_D32DIS_Msk                 (1UL << CPACR_D32DIS_Pos)              /*!< \brief CPACR: D32DIS Mask */
416
417 #define CPACR_cp11_Pos                   22U                                    /*!< \brief CPACR: cp11 Position */
418 #define CPACR_cp11_Msk                   (3UL << CPACR_cp11_Pos)                /*!< \brief CPACR: cp11 Mask */
419
420 #define CPACR_cp10_Pos                   20U                                    /*!< \brief CPACR: cp10 Position */
421 #define CPACR_cp10_Msk                   (3UL << CPACR_cp10_Pos)                /*!< \brief CPACR: cp10 Mask */
422
423 /* CP15 Register DFSR */
424 typedef union
425 {
426   struct
427   {
428     uint32_t FS0:4;                      /*!< \brief bit: 0.. 3  Fault Status bits bit 0-3 */
429     uint32_t Domain:4;                   /*!< \brief bit: 4.. 7  Fault on which domain */
430     uint32_t _reserved0:2;               /*!< \brief bit: 8.. 9  Reserved */
431     uint32_t FS1:1;                      /*!< \brief bit:    10  Fault Status bits bit 4 */
432     uint32_t WnR:1;                      /*!< \brief bit:    11  Write not Read bit */
433     uint32_t ExT:1;                      /*!< \brief bit:    12  External abort type */
434     uint32_t CM:1;                       /*!< \brief bit:    13  Cache maintenance fault */
435     uint32_t _reserved1:18;              /*!< \brief bit:14..31  Reserved */
436   } b;                                   /*!< \brief Structure used for bit  access */
437   uint32_t w;                            /*!< \brief Type      used for word access */
438 } DFSR_Type;
439
440 #define DFSR_CM_Pos                      13U                                    /*!< \brief DFSR: CM Position */
441 #define DFSR_CM_Msk                      (1UL << DFSR_CM_Pos)                   /*!< \brief DFSR: CM Mask */
442
443 #define DFSR_Ext_Pos                     12U                                    /*!< \brief DFSR: Ext Position */
444 #define DFSR_Ext_Msk                     (1UL << DFSR_Ext_Pos)                  /*!< \brief DFSR: Ext Mask */
445
446 #define DFSR_WnR_Pos                     11U                                    /*!< \brief DFSR: WnR Position */
447 #define DFSR_WnR_Msk                     (1UL << DFSR_WnR_Pos)                  /*!< \brief DFSR: WnR Mask */
448
449 #define DFSR_FS1_Pos                     10U                                    /*!< \brief DFSR: FS1 Position */
450 #define DFSR_FS1_Msk                     (1UL << DFSR_FS1_Pos)                  /*!< \brief DFSR: FS1 Mask */
451
452 #define DFSR_Domain_Pos                  4U                                     /*!< \brief DFSR: Domain Position */
453 #define DFSR_Domain_Msk                  (0xFUL << DFSR_Domain_Pos)             /*!< \brief DFSR: Domain Mask */
454
455 #define DFSR_FS0_Pos                     0U                                     /*!< \brief DFSR: FS0 Position */
456 #define DFSR_FS0_Msk                     (0xFUL << DFSR_FS0_Pos)                /*!< \brief DFSR: FS0 Mask */
457
458 /* CP15 Register IFSR */
459 typedef union
460 {
461   struct
462   {
463     uint32_t FS0:4;                      /*!< \brief bit: 0.. 3  Fault Status bits bit 0-3 */
464     uint32_t _reserved0:6;               /*!< \brief bit: 4.. 9  Reserved */
465     uint32_t FS1:1;                      /*!< \brief bit:    10  Fault Status bits bit 4 */
466     uint32_t _reserved1:1;               /*!< \brief bit:    11  Reserved */
467     uint32_t ExT:1;                      /*!< \brief bit:    12  External abort type */
468     uint32_t _reserved2:19;              /*!< \brief bit:13..31  Reserved */
469   } b;                                   /*!< \brief Structure used for bit  access */
470   uint32_t w;                            /*!< \brief Type      used for word access */
471 } IFSR_Type;
472
473 #define IFSR_ExT_Pos                     12U                                    /*!< \brief IFSR: ExT Position */
474 #define IFSR_ExT_Msk                     (1UL << IFSR_ExT_Pos)                  /*!< \brief IFSR: ExT Mask */
475
476 #define IFSR_FS1_Pos                     10U                                    /*!< \brief IFSR: FS1 Position */
477 #define IFSR_FS1_Msk                     (1UL << IFSR_FS1_Pos)                  /*!< \brief IFSR: FS1 Mask */
478
479 #define IFSR_FS0_Pos                     0U                                     /*!< \brief IFSR: FS0 Position */
480 #define IFSR_FS0_Msk                     (0xFUL << IFSR_FS0_Pos)                /*!< \brief IFSR: FS0 Mask */
481
482 /* CP15 Register ISR */
483 typedef union
484 {
485   struct
486   {
487     uint32_t _reserved0:6;               /*!< \brief bit: 0.. 5  Reserved */
488     uint32_t F:1;                        /*!< \brief bit:     6  FIQ pending bit */
489     uint32_t I:1;                        /*!< \brief bit:     7  IRQ pending bit */
490     uint32_t A:1;                        /*!< \brief bit:     8  External abort pending bit */
491     uint32_t _reserved1:23;              /*!< \brief bit:14..31  Reserved */
492   } b;                                   /*!< \brief Structure used for bit  access */
493   uint32_t w;                            /*!< \brief Type      used for word access */
494 } ISR_Type;
495
496 #define ISR_A_Pos                        13U                                    /*!< \brief ISR: A Position */
497 #define ISR_A_Msk                        (1UL << ISR_A_Pos)                     /*!< \brief ISR: A Mask */
498
499 #define ISR_I_Pos                        12U                                    /*!< \brief ISR: I Position */
500 #define ISR_I_Msk                        (1UL << ISR_I_Pos)                     /*!< \brief ISR: I Mask */
501
502 #define ISR_F_Pos                        11U                                    /*!< \brief ISR: F Position */
503 #define ISR_F_Msk                        (1UL << ISR_F_Pos)                     /*!< \brief ISR: F Mask */
504
505
506 /**
507  \brief  Union type to access the L2C_310 Cache Controller.
508 */
509 #if (__L2C_PRESENT == 1U) || defined(DOXYGEN)
510 typedef struct
511 {
512   __IM  uint32_t CACHE_ID;                   /*!< \brief Offset: 0x0000 (R/ ) Cache ID Register               */
513   __IM  uint32_t CACHE_TYPE;                 /*!< \brief Offset: 0x0004 (R/ ) Cache Type Register             */
514         uint32_t RESERVED0[0x3e];
515   __IOM uint32_t CONTROL;                    /*!< \brief Offset: 0x0100 (R/W) Control Register                */
516   __IOM uint32_t AUX_CNT;                    /*!< \brief Offset: 0x0104 (R/W) Auxiliary Control               */
517         uint32_t RESERVED1[0x3e];
518   __IOM uint32_t EVENT_CONTROL;              /*!< \brief Offset: 0x0200 (R/W) Event Counter Control           */
519   __IOM uint32_t EVENT_COUNTER1_CONF;        /*!< \brief Offset: 0x0204 (R/W) Event Counter 1 Configuration   */
520   __IOM uint32_t EVENT_COUNTER0_CONF;        /*!< \brief Offset: 0x0208 (R/W) Event Counter 1 Configuration   */
521         uint32_t RESERVED2[0x2];
522   __IOM uint32_t INTERRUPT_MASK;             /*!< \brief Offset: 0x0214 (R/W) Interrupt Mask                  */
523   __IM  uint32_t MASKED_INT_STATUS;          /*!< \brief Offset: 0x0218 (R/ ) Masked Interrupt Status         */
524   __IM  uint32_t RAW_INT_STATUS;             /*!< \brief Offset: 0x021c (R/ ) Raw Interrupt Status            */
525   __OM  uint32_t INTERRUPT_CLEAR;            /*!< \brief Offset: 0x0220 ( /W) Interrupt Clear                 */
526         uint32_t RESERVED3[0x143];
527   __IOM uint32_t CACHE_SYNC;                 /*!< \brief Offset: 0x0730 (R/W) Cache Sync                      */
528         uint32_t RESERVED4[0xf];
529   __IOM uint32_t INV_LINE_PA;                /*!< \brief Offset: 0x0770 (R/W) Invalidate Line By PA           */
530         uint32_t RESERVED6[2];
531   __IOM uint32_t INV_WAY;                    /*!< \brief Offset: 0x077c (R/W) Invalidate by Way               */
532         uint32_t RESERVED5[0xc];
533   __IOM uint32_t CLEAN_LINE_PA;              /*!< \brief Offset: 0x07b0 (R/W) Clean Line by PA                */
534         uint32_t RESERVED7[1];
535   __IOM uint32_t CLEAN_LINE_INDEX_WAY;       /*!< \brief Offset: 0x07b8 (R/W) Clean Line by Index/Way         */
536   __IOM uint32_t CLEAN_WAY;                  /*!< \brief Offset: 0x07bc (R/W) Clean by Way                    */
537         uint32_t RESERVED8[0xc];
538   __IOM uint32_t CLEAN_INV_LINE_PA;          /*!< \brief Offset: 0x07f0 (R/W) Clean and Invalidate Line by PA  */
539         uint32_t RESERVED9[1];
540   __IOM uint32_t CLEAN_INV_LINE_INDEX_WAY;   /*!< \brief Offset: 0x07f8 (R/W) Clean and Invalidate Line by Index/Way  */
541   __IOM uint32_t CLEAN_INV_WAY;              /*!< \brief Offset: 0x07fc (R/W) Clean and Invalidate by Way     */
542         uint32_t RESERVED10[0x40];
543   __IOM uint32_t DATA_LOCK_0_WAY;            /*!< \brief Offset: 0x0900 (R/W) Data Lockdown 0 by Way          */
544   __IOM uint32_t INST_LOCK_0_WAY;            /*!< \brief Offset: 0x0904 (R/W) Instruction Lockdown 0 by Way   */
545   __IOM uint32_t DATA_LOCK_1_WAY;            /*!< \brief Offset: 0x0908 (R/W) Data Lockdown 1 by Way          */
546   __IOM uint32_t INST_LOCK_1_WAY;            /*!< \brief Offset: 0x090c (R/W) Instruction Lockdown 1 by Way   */
547   __IOM uint32_t DATA_LOCK_2_WAY;            /*!< \brief Offset: 0x0910 (R/W) Data Lockdown 2 by Way          */
548   __IOM uint32_t INST_LOCK_2_WAY;            /*!< \brief Offset: 0x0914 (R/W) Instruction Lockdown 2 by Way   */
549   __IOM uint32_t DATA_LOCK_3_WAY;            /*!< \brief Offset: 0x0918 (R/W) Data Lockdown 3 by Way          */
550   __IOM uint32_t INST_LOCK_3_WAY;            /*!< \brief Offset: 0x091c (R/W) Instruction Lockdown 3 by Way   */
551   __IOM uint32_t DATA_LOCK_4_WAY;            /*!< \brief Offset: 0x0920 (R/W) Data Lockdown 4 by Way          */
552   __IOM uint32_t INST_LOCK_4_WAY;            /*!< \brief Offset: 0x0924 (R/W) Instruction Lockdown 4 by Way   */
553   __IOM uint32_t DATA_LOCK_5_WAY;            /*!< \brief Offset: 0x0928 (R/W) Data Lockdown 5 by Way          */
554   __IOM uint32_t INST_LOCK_5_WAY;            /*!< \brief Offset: 0x092c (R/W) Instruction Lockdown 5 by Way   */
555   __IOM uint32_t DATA_LOCK_6_WAY;            /*!< \brief Offset: 0x0930 (R/W) Data Lockdown 5 by Way          */
556   __IOM uint32_t INST_LOCK_6_WAY;            /*!< \brief Offset: 0x0934 (R/W) Instruction Lockdown 5 by Way   */
557   __IOM uint32_t DATA_LOCK_7_WAY;            /*!< \brief Offset: 0x0938 (R/W) Data Lockdown 6 by Way          */
558   __IOM uint32_t INST_LOCK_7_WAY;            /*!< \brief Offset: 0x093c (R/W) Instruction Lockdown 6 by Way   */
559         uint32_t RESERVED11[0x4];
560   __IOM uint32_t LOCK_LINE_EN;               /*!< \brief Offset: 0x0950 (R/W) Lockdown by Line Enable         */
561   __IOM uint32_t UNLOCK_ALL_BY_WAY;          /*!< \brief Offset: 0x0954 (R/W) Unlock All Lines by Way         */
562         uint32_t RESERVED12[0xaa];
563   __IOM uint32_t ADDRESS_FILTER_START;       /*!< \brief Offset: 0x0c00 (R/W) Address Filtering Start         */
564   __IOM uint32_t ADDRESS_FILTER_END;         /*!< \brief Offset: 0x0c04 (R/W) Address Filtering End           */
565         uint32_t RESERVED13[0xce];
566   __IOM uint32_t DEBUG_CONTROL;              /*!< \brief Offset: 0x0f40 (R/W) Debug Control Register          */
567 } L2C_310_TypeDef;
568
569 #define L2C_310           ((L2C_310_TypeDef *)L2C_310_BASE) /*!< \brief L2C_310 Declaration */
570 #endif
571
572 #if (__GIC_PRESENT == 1U) || defined(DOXYGEN)
573 /** \brief  Structure type to access the Generic Interrupt Controller Distributor (GICD)
574 */
575 typedef struct
576 {
577   __IOM uint32_t D_CTLR;                 /*!< \brief  Offset: 0x000 (R/W) Distributor Control Register */
578   __IM  uint32_t D_TYPER;                /*!< \brief  Offset: 0x004 (R/ ) Interrupt Controller Type Register */
579   __IM  uint32_t D_IIDR;                 /*!< \brief  Offset: 0x008 (R/ ) Distributor Implementer Identification Register */
580         uint32_t RESERVED0;
581   __IOM uint32_t D_STATUSR;              /*!< \brief  Offset: 0x010 (R/W) Error Reporting Status Register, optional */
582         uint32_t RESERVED1[11];
583   __OM  uint32_t D_SETSPI_NSR;           /*!< \brief  Offset: 0x040 ( /W) Set SPI Register */
584         uint32_t RESERVED2;
585   __OM  uint32_t D_CLRSPI_NSR;           /*!< \brief  Offset: 0x048 ( /W) Clear SPI Register */
586         uint32_t RESERVED3;
587   __OM  uint32_t D_SETSPI_SR;            /*!< \brief  Offset: 0x050 ( /W) Set SPI, Secure Register */
588         uint32_t RESERVED4;
589   __OM  uint32_t D_CLRSPI_SR;            /*!< \brief  Offset: 0x058 ( /W) Clear SPI, Secure Register */
590         uint32_t RESERVED5[9];
591   __IOM uint32_t D_IGROUPR[32];          /*!< \brief  Offset: 0x080 (R/W) Interrupt Group Registers */
592   __IOM uint32_t D_ISENABLER[32];        /*!< \brief  Offset: 0x100 (R/W) Interrupt Set-Enable Registers */
593   __IOM uint32_t D_ICENABLER[32];        /*!< \brief  Offset: 0x180 (R/W) Interrupt Clear-Enable Registers */
594   __IOM uint32_t D_ISPENDR[32];          /*!< \brief  Offset: 0x200 (R/W) Interrupt Set-Pending Registers */
595   __IOM uint32_t D_ICPENDR[32];          /*!< \brief  Offset: 0x280 (R/W) Interrupt Clear-Pending Registers */
596   __IOM uint32_t D_ISACTIVER[32];        /*!< \brief  Offset: 0x300 (R/W) Interrupt Set-Active Registers */
597   __IOM uint32_t D_ICACTIVER[32];        /*!< \brief  Offset: 0x380 (R/W) Interrupt Clear-Active Registers */
598   __IOM uint8_t  D_IPRIORITYR[1020];     /*!< \brief  Offset: 0x400 (R/W) Interrupt Priority Registers */
599         uint32_t RESERVED9[1];
600   __IOM uint8_t  D_ITARGETSR[1020];      /*!< \brief  Offset: 0x800 (R/W) Interrupt Targets Registers */
601         uint32_t RESERVED10[1];
602   __IOM uint32_t D_ICFGR[64];            /*!< \brief  Offset: 0xC00 (R/W) Interrupt Configuration Registers */
603   __IOM uint32_t D_IGRPMODR[32];         /*!< \brief  Offset: 0xD00 (R/W) Interrupt Group Modifier Registers */
604         uint32_t RESERVED11[32];
605   __IOM uint32_t D_NSACR[64];            /*!< \brief  Offset: 0xE00 (R/W) Non-secure Access Control Registers */
606   __OM  uint32_t D_SGIR;                 /*!< \brief  Offset: 0xF00 ( /W) Software Generated Interrupt Register */
607         uint32_t RESERVED13[3];
608   __IOM uint8_t  D_CPENDSGIR[16];        /*!< \brief  Offset: 0xF10 (R/W) SGI Clear-Pending Registers */
609   __IOM uint8_t  D_SPENDSGIR[16];        /*!< \brief  Offset: 0xF20 (R/W) SGI Set-Pending Registers */
610         uint32_t RESERVED14[5236];
611   __IOM uint64_t D_IROUTER[988];         /*!< \brief  Offset: 0x6100(R/W) Interrupt Routing Registers */
612 }  GICDistributor_Type;
613
614 #define GICDistributor      ((GICDistributor_Type      *)     GIC_DISTRIBUTOR_BASE ) /*!< GIC Distributor configuration struct */
615
616 /** \brief  Structure type to access the Generic Interrupt Controller Interface (GICC)
617 */
618 typedef struct
619 {
620   __IOM uint32_t C_CTLR;                 /*!< \brief  Offset: 0x000 (R/W) CPU Interface Control Register */
621   __IOM uint32_t C_PMR;                  /*!< \brief  Offset: 0x004 (R/W) Interrupt Priority Mask Register */
622   __IOM uint32_t C_BPR;                  /*!< \brief  Offset: 0x008 (R/W) Binary Point Register */
623   __IM  uint32_t C_IAR;                  /*!< \brief  Offset: 0x00C (R/ ) Interrupt Acknowledge Register */
624   __OM  uint32_t C_EOIR;                 /*!< \brief  Offset: 0x010 ( /W) End Of Interrupt Register */
625   __IM  uint32_t C_RPR;                  /*!< \brief  Offset: 0x014 (R/ ) Running Priority Register */
626   __IM  uint32_t C_HPPIR;                /*!< \brief  Offset: 0x018 (R/ ) Highest Priority Pending Interrupt Register */
627   __IOM uint32_t C_ABPR;                 /*!< \brief  Offset: 0x01C (R/W) Aliased Binary Point Register */
628   __IM  uint32_t C_AIAR;                 /*!< \brief  Offset: 0x020 (R/ ) Aliased Interrupt Acknowledge Register */
629   __OM  uint32_t C_AEOIR;                /*!< \brief  Offset: 0x024 ( /W) Aliased End Of Interrupt Register */
630   __IM  uint32_t C_AHPPIR;               /*!< \brief  Offset: 0x028 (R/ ) Aliased Highest Priority Pending Interrupt Register */
631   __IOM uint32_t C_STATUSR;              /*!< \brief  Offset: 0x02C (R/W) Error Reporting Status Register, optional */
632         uint32_t RESERVED15[40];
633   __IOM uint32_t C_APR[4];               /*!< \brief  Offset: 0x0D0 (R/W) Active Priority Register */
634   __IOM uint32_t C_NSAPR[4];             /*!< \brief  Offset: 0x0E0 (R/W) Non-secure Active Priority Register */
635         uint32_t RESERVED17[3];
636   __IM  uint32_t C_IIDR;                 /*!< \brief  Offset: 0x0FC (R/ ) CPU Interface Identification Register */
637         uint32_t RESERVED18[960];  
638   __OM  uint32_t C_DIR;                  /*!< \brief  Offset: 0x1000( /W) Deactivate Interrupt Register */
639 }  GICInterface_Type;
640
641 #define GICInterface        ((GICInterface_Type        *)     GIC_INTERFACE_BASE )   /*!< GIC Interface configuration struct */
642 #endif
643
644 #if (__TIM_PRESENT == 1U) || defined(DOXYGEN)
645 #if ((__CORTEX_A == 5U) || (__CORTEX_A == 9U)) || defined(DOXYGEN)
646 /** \brief Structure type to access the Private Timer
647 */
648 typedef struct
649 {
650   __IOM uint32_t LOAD;            //!< \brief  Offset: 0x000 (R/W) Private Timer Load Register
651   __IOM uint32_t COUNTER;         //!< \brief  Offset: 0x004 (R/W) Private Timer Counter Register
652   __IOM uint32_t CONTROL;         //!< \brief  Offset: 0x008 (R/W) Private Timer Control Register
653   __IM  uint32_t ISR;             //!< \brief  Offset: 0x00C (R/ ) Private Timer Interrupt Status Register
654         uint32_t RESERVED[4];
655   __IOM uint32_t WLOAD;           //!< \brief  Offset: 0x020 (R/W) Watchdog Load Register
656   __IOM uint32_t WCOUNTER;        //!< \brief  Offset: 0x024 (R/W) Watchdog Counter Register
657   __IOM uint32_t WCONTROL;        //!< \brief  Offset: 0x028 (R/W) Watchdog Control Register
658   __IOM uint32_t WISR;            //!< \brief  Offset: 0x02C (R/W) Watchdog Interrupt Status Register
659   __IOM uint32_t WRESET;          //!< \brief  Offset: 0x030 (R/W) Watchdog Reset Status Register
660   __IM  uint32_t WDISABLE;        //!< \brief  Offset: 0x034 (R/ ) Watchdog Disable Register
661 } Timer_Type;
662 #define PTIM ((Timer_Type *) TIMER_BASE )   /*!< \brief Timer configuration struct */
663 #endif
664 #endif
665
666  /*******************************************************************************
667   *                Hardware Abstraction Layer
668    Core Function Interface contains:
669    - L1 Cache Functions
670    - L2C-310 Cache Controller Functions 
671    - PL1 Timer Functions
672    - GIC Functions
673    - MMU Functions
674   ******************************************************************************/
675  
676 /* ##########################  L1 Cache functions  ################################# */
677
678 /** \brief Enable Caches by setting I and C bits in \ref CMSIS_SCTLR "SCTLR" register.
679 */
680 __STATIC_INLINE void L1C_EnableCaches(void) {
681   __set_SCTLR( __get_SCTLR() | (1u << SCTLR_I_Pos) | (1u << SCTLR_C_Pos));
682   __ISB();
683 }
684
685 /** \brief Disable Caches by clearing I and C bits in \ref CMSIS_SCTLR "SCTLR" register.
686 */
687 __STATIC_INLINE void L1C_DisableCaches(void) {
688   __set_SCTLR( __get_SCTLR() & ~(1u << SCTLR_I_Pos) & ~(1u << SCTLR_C_Pos));
689   __ISB();
690 }
691
692 /** \brief  Enable Branch Prediction by setting Z bit in \ref CMSIS_SCTLR "SCTLR" register.
693 */
694 __STATIC_INLINE void L1C_EnableBTAC(void) {
695   __set_SCTLR( __get_SCTLR() | (1u << SCTLR_Z_Pos));
696   __ISB();
697 }
698
699 /** \brief  Disable Branch Prediction by clearing Z bit in \ref CMSIS_SCTLR "SCTLR" register.
700 */
701 __STATIC_INLINE void L1C_DisableBTAC(void) {
702   __set_SCTLR( __get_SCTLR() & ~(1u << SCTLR_Z_Pos));
703   __ISB();
704 }
705
706 /** \brief  Invalidate entire branch predictor array
707 */
708 __STATIC_INLINE void L1C_InvalidateBTAC(void) {
709   __set_BPIALL(0);
710   __DSB();     //ensure completion of the invalidation
711   __ISB();     //ensure instruction fetch path sees new state
712 }
713
714 /** \brief  Invalidate the whole instruction cache
715 */
716 __STATIC_INLINE void L1C_InvalidateICacheAll(void) {
717   __set_ICIALLU(0);
718   __DSB();     //ensure completion of the invalidation
719   __ISB();     //ensure instruction fetch path sees new I cache state
720 }
721
722 /** \brief  Clean data cache line by address.
723 * \param va Pointer to data to clear the cache for.
724 */
725 __STATIC_INLINE void L1C_CleanDCacheMVA(void *va) {
726   __set_DCCMVAC((uint32_t)va);
727   __DMB();     //ensure the ordering of data cache maintenance operations and their effects
728 }
729
730 /** \brief  Invalidate data cache line by address.
731 * \param va Pointer to data to invalidate the cache for.
732 */
733 __STATIC_INLINE void L1C_InvalidateDCacheMVA(void *va) {
734   __set_DCIMVAC((uint32_t)va);
735   __DMB();     //ensure the ordering of data cache maintenance operations and their effects
736 }
737
738 /** \brief  Clean and Invalidate data cache by address.
739 * \param va Pointer to data to invalidate the cache for.
740 */
741 __STATIC_INLINE void L1C_CleanInvalidateDCacheMVA(void *va) {
742   __set_DCCIMVAC((uint32_t)va);
743   __DMB();     //ensure the ordering of data cache maintenance operations and their effects
744 }
745
746 /** \brief  Clean and Invalidate the entire data or unified cache
747 * \param op 0 - invalidate, 1 - clean, otherwise - invalidate and clean
748 * \see __L1C_CleanInvalidateCache
749 */
750 __STATIC_INLINE void L1C_CleanInvalidateCache(uint32_t op) {
751   __L1C_CleanInvalidateCache(op);
752 }
753
754 /** \brief  Invalidate the whole data cache.
755 */
756 __STATIC_INLINE void L1C_InvalidateDCacheAll(void) {
757   L1C_CleanInvalidateCache(0);
758 }
759
760 /** \brief  Clean the whole data cache.
761  */
762 __STATIC_INLINE void L1C_CleanDCacheAll(void) {
763   L1C_CleanInvalidateCache(1);
764 }
765
766 /** \brief  Clean and invalidate the whole data cache.
767  */
768 __STATIC_INLINE void L1C_CleanInvalidateDCacheAll(void) {
769   L1C_CleanInvalidateCache(2);
770 }
771
772
773 /* ##########################  L2 Cache functions  ################################# */
774 #if (__L2C_PRESENT == 1U) || defined(DOXYGEN)
775 /** \brief Cache Sync operation by writing \ref L2C_310_TypeDef::CACHE_SYNC "CACHE_SYNC" register.
776 */
777 __STATIC_INLINE void L2C_Sync(void)
778 {
779   L2C_310->CACHE_SYNC = 0x0;
780 }
781
782 /** \brief Read cache controller cache ID from \ref L2C_310_TypeDef::CACHE_ID "CACHE_ID" register.
783  * \return L2C_310_TypeDef::CACHE_ID
784  */
785 __STATIC_INLINE int L2C_GetID (void)
786 {
787   return L2C_310->CACHE_ID;
788 }
789
790 /** \brief Read cache controller cache type from \ref L2C_310_TypeDef::CACHE_TYPE "CACHE_TYPE" register.
791 *  \return L2C_310_TypeDef::CACHE_TYPE
792 */
793 __STATIC_INLINE int L2C_GetType (void)
794 {
795   return L2C_310->CACHE_TYPE;
796 }
797
798 /** \brief Invalidate all cache by way
799 */
800 __STATIC_INLINE void L2C_InvAllByWay (void)
801 {
802   unsigned int assoc;
803
804   if (L2C_310->AUX_CNT & (1u << 16u)) {
805     assoc = 16u;
806   } else {
807     assoc =  8u;
808   }
809   
810   L2C_310->INV_WAY = (1u << assoc) - 1u;
811   while(L2C_310->INV_WAY & ((1u << assoc) - 1u)); //poll invalidate
812
813   L2C_Sync();
814 }
815
816 /** \brief Clean and Invalidate all cache by way
817 */
818 __STATIC_INLINE void L2C_CleanInvAllByWay (void)
819 {
820   unsigned int assoc;
821
822   if (L2C_310->AUX_CNT & (1u << 16u)) {
823     assoc = 16u;
824   } else {
825     assoc =  8u;
826   }
827
828   L2C_310->CLEAN_INV_WAY = (1u << assoc) - 1u;
829   while(L2C_310->CLEAN_INV_WAY & ((1u << assoc) - 1u)); //poll invalidate
830
831   L2C_Sync();
832 }
833
834 /** \brief Enable Level 2 Cache
835 */
836 __STATIC_INLINE void L2C_Enable(void)
837 {
838   L2C_310->CONTROL = 0;
839   L2C_310->INTERRUPT_CLEAR = 0x000001FFuL;
840   L2C_310->DEBUG_CONTROL = 0;
841   L2C_310->DATA_LOCK_0_WAY = 0;
842   L2C_310->CACHE_SYNC = 0;
843   L2C_310->CONTROL = 0x01;
844   L2C_Sync();
845 }
846
847 /** \brief Disable Level 2 Cache
848 */
849 __STATIC_INLINE void L2C_Disable(void)
850 {
851   L2C_310->CONTROL = 0x00;
852   L2C_Sync();
853 }
854
855 /** \brief Invalidate cache by physical address
856 * \param pa Pointer to data to invalidate cache for.
857 */
858 __STATIC_INLINE void L2C_InvPa (void *pa)
859 {
860   L2C_310->INV_LINE_PA = (unsigned int)pa;
861   L2C_Sync();
862 }
863
864 /** \brief Clean cache by physical address
865 * \param pa Pointer to data to invalidate cache for.
866 */
867 __STATIC_INLINE void L2C_CleanPa (void *pa)
868 {
869   L2C_310->CLEAN_LINE_PA = (unsigned int)pa;
870   L2C_Sync();
871 }
872
873 /** \brief Clean and invalidate cache by physical address
874 * \param pa Pointer to data to invalidate cache for.
875 */
876 __STATIC_INLINE void L2C_CleanInvPa (void *pa)
877 {
878   L2C_310->CLEAN_INV_LINE_PA = (unsigned int)pa;
879   L2C_Sync();
880 }
881 #endif
882
883 /* ##########################  GIC functions  ###################################### */
884 #if (__GIC_PRESENT == 1U) || defined(DOXYGEN)
885   
886 /** Enable the interrupt distributor using the GIC's \ref GICDistributor_Type::D_CTLR "D_CTLR" register.
887 */
888 __STATIC_INLINE void GIC_EnableDistributor(void)
889 {
890   GICDistributor->D_CTLR |= 1;
891 }
892
893 /** Disable the interrupt distributor using the GIC's \ref GICDistributor_Type::D_CTLR "D_CTLR" register.
894 */
895 __STATIC_INLINE void GIC_DisableDistributor(void)
896 {
897   GICDistributor->D_CTLR &=~1;
898 }
899
900 /** Read the GIC's \ref GICDistributor_Type.D_TYPER "D_TYPER" register.
901 * \return GICDistributor_Type::D_TYPER
902 */
903 __STATIC_INLINE uint32_t GIC_DistributorInfo(void)
904 {
905   return (uint32_t)(GICDistributor->D_TYPER);
906 }
907
908 /** Reads the GIC's \ref GICDistributor_Type.D_IIDR "D_IIDR" register.
909 * \return GICDistributor_Type::D_IIDR
910 */
911 __STATIC_INLINE uint32_t GIC_DistributorImplementer(void)
912 {
913   return (uint32_t)(GICDistributor->D_IIDR);
914 }
915
916 /** Sets the GIC's \ref GICDistributor_Type.D_ITARGETSR "D_ITARGETSR" register for the given interrupt.
917 * \param IRQn Interrupt to be configured.
918 * \param cpu_target CPU interfaces to assign this interrupt to.
919 */
920 __STATIC_INLINE void GIC_SetTarget(IRQn_Type IRQn, uint32_t cpu_target)
921 {
922   GICDistributor->D_ITARGETSR[((uint32_t)(int32_t)IRQn)] = (uint8_t)(cpu_target & 0x0f);
923 }
924
925 /** Read the GIC's \ref GICDistributor_Type.D_ITARGETSR "D_ITARGETSR" register.
926 * \param IRQn Interrupt to acquire the configuration for.
927 * \return GICDistributor_Type::D_ITARGETSR
928 */
929 __STATIC_INLINE uint32_t GIC_GetTarget(IRQn_Type IRQn)
930 {
931   return ((uint32_t) GICDistributor->D_ITARGETSR[((uint32_t)(int32_t)IRQn)] & 0x0f);
932 }
933
934 /** Enable the CPUs interrupt interface.
935 */
936 __STATIC_INLINE void GIC_EnableInterface(void)
937 {
938   GICInterface->C_CTLR |= 1; //enable interface
939 }
940
941 /** Disable the CPUs interrupt interface.
942 */
943 __STATIC_INLINE void GIC_DisableInterface(void)
944 {
945   GICInterface->C_CTLR &=~1; //disable distributor
946 }
947
948 /** Read the CPU's \ref GICInterface_Type::C_IAR "C_IAR" register.
949 * \return GICInterface_Type::C_IAR
950 */
951 __STATIC_INLINE IRQn_Type GIC_AcknowledgePending(void)
952 {
953   return (IRQn_Type)(GICInterface->C_IAR);
954 }
955
956 /** Writes the given interrupt number to the CPU's \ref GICInterface_Type::C_EOIR "C_EOIR" register.
957 * \param IRQn The interrupt to be signaled as finished.
958 */
959 __STATIC_INLINE void GIC_EndInterrupt(IRQn_Type IRQn)
960 {
961   GICInterface->C_EOIR = IRQn;
962 }
963
964 /** Enables the given interrupt using GIC's \ref GICDistributor_Type::D_ISENABLER "D_ISENABLER" register.
965 * \param IRQn The interrupt to be enabled.
966 */
967 __STATIC_INLINE void GIC_EnableIRQ(IRQn_Type IRQn)
968 {
969   GICDistributor->D_ISENABLER[IRQn / 32] = 1 << (IRQn % 32);
970 }
971
972 /** Disables the given interrupt using GIC's \ref GICDistributor_Type::D_ICENABLER "D_ICENABLER" register.
973 * \param IRQn The interrupt to be disabled.
974 */
975 __STATIC_INLINE void GIC_DisableIRQ(IRQn_Type IRQn)
976 {
977   GICDistributor->D_ICENABLER[IRQn / 32] = 1 << (IRQn % 32);
978 }
979
980 /** Sets the given interrupt as pending using GIC's \ref GICDistributor_Type::D_ISPENDR "D_ISPENDR" register.
981 * \param IRQn The interrupt to be enabled.
982 */
983 __STATIC_INLINE void GIC_SetPendingIRQ(IRQn_Type IRQn)
984 {
985   if (IRQn >= 16U) {
986     GICDistributor->D_ISPENDR[IRQn / 32] = 1 << (IRQn % 32);
987   } else {
988     // INTID 0-15 Software Generated Interrupt
989     GICDistributor->D_SPENDSGIR[IRQn] = 1U;
990     // Forward the interrupt to the CPU interface that requested it
991     GICDistributor->D_SGIR = (IRQn | 0x02000000U);
992   }
993 }
994
995 /** Clears the given interrupt from beeing pending using GIC's \ref GICDistributor_Type::D_ICPENDR "D_ICPENDR" register.
996 * \param IRQn The interrupt to be enabled.
997 */
998 __STATIC_INLINE void GIC_ClearPendingIRQ(IRQn_Type IRQn)
999 {
1000   if (IRQn >= 16U) {
1001     GICDistributor->D_ICPENDR[IRQn / 32] = 1 << (IRQn % 32);
1002   } else {
1003     // INTID 0-15 Software Generated Interrupt
1004     GICDistributor->D_CPENDSGIR[IRQn] = 1U;
1005   }
1006 }
1007
1008 /** Configures the interrupt egde and model using GIC's GICDistributor_Type::D_ICFGR "D_ICFGR" register.
1009 * \param IRQn The interrupt to be configured.
1010 * \param edge_level Signal sensitivity: 0 - level sensitive, 1 - edge triggered
1011 * \param model Handling mode: 0 - N-N model, 1 - 1-N model.
1012 */
1013 __STATIC_INLINE void GIC_SetLevelModel(IRQn_Type IRQn, uint8_t edge_level, uint8_t model)
1014 {   
1015   // Word-size read/writes must be used to access this register
1016   volatile uint32_t * field = &(GICDistributor->D_ICFGR[IRQn / 16]);
1017   unsigned bit_shift = (IRQn % 16)<<1;
1018   unsigned int save_word;
1019
1020   save_word = *field;
1021   save_word &= (~(3 << bit_shift));
1022
1023   *field = (save_word | ((((edge_level & 0x01u) << 1) | (model & 0x01u)) << bit_shift));
1024 }
1025
1026 /** Set the priority for the given interrupt in the GIC's \ref GICDistributor_Type::D_IPRIORITYR "D_IPRIORITYR" register.
1027 * \param IRQn The interrupt to be configured.
1028 * \param priority The priority for the interrupt, lower values denote higher priorities.
1029 */
1030 __STATIC_INLINE void GIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
1031 {
1032   GICDistributor->D_IPRIORITYR[((uint32_t)(int32_t)IRQn)] = (uint8_t)(priority);
1033 }
1034
1035 /** Read the current interrupt priority from GIC's \ref GICDistributor_Type::D_IPRIORITYR "D_IPRIORITYR" register.
1036 * \param IRQn The interrupt to be queried.
1037 */
1038 __STATIC_INLINE uint32_t GIC_GetPriority(IRQn_Type IRQn)
1039 {
1040   return((uint32_t)GICDistributor->D_IPRIORITYR[((uint32_t)(int32_t)IRQn)]);
1041 }
1042
1043 /** Set the interrupt priority mask using CPU's \ref GICInterface_Type::C_PMR "C_PMR" register.
1044 * \param priority Priority mask to be set.
1045 */
1046 __STATIC_INLINE void GIC_SetInterfacePriorityMask(uint32_t priority)
1047 {
1048   GICInterface->C_PMR = priority & 0xff; //set priority mask
1049 }
1050
1051 /** Read the current interrupt priority mask from CPU's \ref GICInterface_Type::C_PMR "C_PMR" register.
1052 * \result GICInterface_Type::C_PMR
1053 */
1054 __STATIC_INLINE uint32_t GIC_GetInterfacePriorityMask(void)
1055 {
1056   return (uint32_t)GICInterface->C_PMR;
1057 }
1058
1059 /** Configures the group priority and subpriority split point using CPU's \ref GICInterface_Type::C_BPR "C_BPR" register.
1060 * \param binary_point Amount of bits used as subpriority.
1061 */
1062 __STATIC_INLINE void GIC_SetBinaryPoint(uint32_t binary_point)
1063 {
1064   GICInterface->C_BPR = binary_point & 0x07; //set binary point
1065 }
1066
1067 /** Read the current group priority and subpriority split point from CPU's \ref GICInterface_Type::C_BPR "C_BPR" register.
1068 * \return GICInterface_Type::C_BPR
1069 */
1070 __STATIC_INLINE uint32_t GIC_GetBinaryPoint(void)
1071 {
1072   return (uint32_t)GICInterface->C_BPR;
1073 }
1074
1075 /** Get the status for a given interrupt.
1076 * \param IRQn The interrupt to get status for.
1077 * \return 0 - not pending/active, 1 - pending, 2 - active, 3 - pending and active
1078 */
1079 __STATIC_INLINE uint32_t GIC_GetIRQStatus(IRQn_Type IRQn)
1080 {
1081   uint32_t pending, active;
1082
1083   active = ((GICDistributor->D_ISACTIVER[IRQn / 32])  >> (IRQn % 32)) & 0x1;
1084   pending =((GICDistributor->D_ISPENDR[IRQn / 32]) >> (IRQn % 32)) & 0x1;
1085
1086   return ((active<<1) | pending);
1087 }
1088
1089 /** Generate a software interrupt using GIC's \ref GICDistributor_Type::D_SGIR "D_SGIR" register.
1090 * \param IRQn Software interrupt to be generated.
1091 * \param target_list List of CPUs the software interrupt should be forwarded to.
1092 * \param filter_list Filter to be applied to determin interrupt receivers.
1093 */
1094 __STATIC_INLINE void GIC_SendSGI(IRQn_Type IRQn, uint32_t target_list, uint32_t filter_list)
1095 {
1096   GICDistributor->D_SGIR = ((filter_list & 0x3) << 24) | ((target_list & 0xff) << 16) | (IRQn & 0xf);
1097 }
1098
1099 /** Get the interrupt number of the highest interrupt pending from CPU's \ref GICInterface_Type::C_HPPIR "C_HPPIR" register.
1100 * \return GICInterface_Type::C_HPPIR
1101 */
1102 __STATIC_INLINE uint32_t GIC_GetHighPendingIRQ(void) 
1103
1104   return GICInterface->C_HPPIR; 
1105 }
1106
1107 /** Provides information about the implementer and revision of the CPU interface.
1108 * \return GICInterface_Type::C_IIDR
1109 */
1110 __STATIC_INLINE uint32_t GIC_GetInterfaceId(void)
1111
1112   return GICInterface->C_IIDR; 
1113 }
1114
1115 /** Initialitze the interrupt distributor.
1116 */
1117 __STATIC_INLINE void GIC_DistInit(void)
1118 {
1119   IRQn_Type i;
1120   uint32_t num_irq = 0;
1121   uint32_t priority_field;
1122
1123   //A reset sets all bits in the D_IGROUPRs corresponding to the SPIs to 0,
1124   //configuring all of the interrupts as Secure.
1125
1126   //Disable interrupt forwarding
1127   GIC_DisableDistributor();
1128   //Get the maximum number of interrupts that the GIC supports
1129   num_irq = 32 * ((GIC_DistributorInfo() & 0x1f) + 1);
1130
1131   /* Priority level is implementation defined.
1132    To determine the number of priority bits implemented write 0xFF to an D_IPRIORITYR
1133    priority field and read back the value stored.*/
1134   GIC_SetPriority((IRQn_Type)0, 0xff);
1135   priority_field = GIC_GetPriority((IRQn_Type)0);
1136
1137   for (i = (IRQn_Type)32; i < num_irq; i++)
1138   {
1139       //Disable the SPI interrupt
1140       GIC_DisableIRQ(i);
1141       //Set level-sensitive and 1-N model
1142       GIC_SetLevelModel(i, 0, 1);
1143       //Set priority
1144       GIC_SetPriority(i, priority_field/2);
1145       //Set target list to CPU0
1146       GIC_SetTarget(i, 1);
1147   }
1148   //Enable distributor
1149   GIC_EnableDistributor();
1150 }
1151
1152 /** Initialize the CPU's interrupt interface
1153 */
1154 __STATIC_INLINE void GIC_CPUInterfaceInit(void)
1155 {
1156   IRQn_Type i;
1157   uint32_t priority_field;
1158
1159   //A reset sets all bits in the D_IGROUPRs corresponding to the SPIs to 0,
1160   //configuring all of the interrupts as Secure.
1161
1162   //Disable interrupt forwarding
1163   GIC_DisableInterface();
1164
1165   /* Priority level is implementation defined.
1166    To determine the number of priority bits implemented write 0xFF to an D_IPRIORITYR
1167    priority field and read back the value stored.*/
1168   GIC_SetPriority((IRQn_Type)0, 0xff);
1169   priority_field = GIC_GetPriority((IRQn_Type)0);
1170
1171   //SGI and PPI
1172   for (i = (IRQn_Type)0; i < 32; i++)
1173   {
1174       //Set level-sensitive and 1-N model for PPI
1175     if(i > 15)
1176           GIC_SetLevelModel(i, 0, 1);
1177       //Disable SGI and PPI interrupts
1178       GIC_DisableIRQ(i);
1179       //Set priority
1180       GIC_SetPriority(i, priority_field/2);
1181   }
1182   //Enable interface
1183   GIC_EnableInterface();
1184   //Set binary point to 0
1185   GIC_SetBinaryPoint(0);
1186   //Set priority mask
1187   GIC_SetInterfacePriorityMask(0xff);
1188 }
1189
1190 /** Initialize and enable the GIC
1191 */
1192 __STATIC_INLINE void GIC_Enable(void)
1193 {
1194   GIC_DistInit();
1195   GIC_CPUInterfaceInit(); //per CPU
1196 }
1197 #endif
1198
1199 /* ##########################  Generic Timer functions  ############################ */
1200 #if (__TIM_PRESENT == 1U) || defined(DOXYGEN)
1201   
1202 /* PL1 Physical Timer */
1203 #if (__CORTEX_A == 7U) || defined(DOXYGEN)
1204   
1205 /** \brief Physical Timer Control register */
1206 typedef union
1207 {
1208   struct
1209   {
1210     uint32_t ENABLE:1;      /*!< \brief bit: 0      Enables the timer. */
1211     uint32_t IMASK:1;       /*!< \brief bit: 1      Timer output signal mask bit. */
1212     uint32_t ISTATUS:1;     /*!< \brief bit: 2      The status of the timer. */
1213     uint32_t _reserved0:29; /*!< \brief bit: 3..31  Reserved */
1214   } b;                      /*!< \brief Structure used for bit  access */
1215   uint32_t w;               /*!< \brief Type      used for word access */
1216 } CNTP_CTL_Type;
1217
1218 /** Configures the frequency the timer shall run at.
1219 * \param value The timer frequency in Hz.
1220 */
1221 __STATIC_INLINE void PL1_SetCounterFrequency(uint32_t value) {
1222   __set_CNTFRQ(value);
1223   __ISB();
1224 }
1225
1226 /** Sets the reset value of the timer.
1227 * \param value The value the timer is loaded with.
1228 */
1229 __STATIC_INLINE void PL1_SetLoadValue(uint32_t value) {
1230   __set_CNTP_TVAL(value);
1231   __ISB();
1232 }
1233
1234 /** Get the current counter value.
1235 * \return Current counter value.
1236 */
1237 __STATIC_INLINE uint32_t PL1_GetCurrentValue() {
1238   return(__get_CNTP_TVAL());
1239 }
1240
1241 /** Configure the timer by setting the control value.
1242 * \param value New timer control value.
1243 */
1244 __STATIC_INLINE void PL1_SetControl(uint32_t value) {
1245   __set_CNTP_CTL(value);
1246   __ISB();
1247 }
1248
1249 /* Private Timer */
1250 #elif ((__CORTEX_A == 5U) || (__CORTEX_A == 9U)) || defined(DOXYGEN)
1251 /** Set the load value to timers \ref Timer_Type::LOAD "LOAD" register.
1252 * \param value The load value to be set.
1253 */
1254 __STATIC_INLINE void PTIM_SetLoadValue(uint32_t value) {
1255   PTIM->LOAD = value;
1256 }
1257
1258 /** Get the load value from timers \ref Timer_Type::LOAD "LOAD" register.
1259 * \return Timer_Type::LOAD
1260 */
1261 __STATIC_INLINE uint32_t PTIM_GetLoadValue() {
1262   return(PTIM->LOAD);
1263 }
1264
1265 /** Get current counter value from timers \ref Timer_Type::COUNTER "COUNTER" register.
1266 * \result Timer_Type::COUNTER
1267 */
1268 __STATIC_INLINE uint32_t PTIM_GetCurrentValue() {
1269   return(PTIM->COUNTER);
1270 }
1271
1272 /** Configure the timer using its \ref Timer_Type::CONTROL "CONTROL" register.
1273 * \param value The new configuration value to be set.
1274 */
1275 __STATIC_INLINE void PTIM_SetControl(uint32_t value) {
1276   PTIM->CONTROL = value;
1277 }
1278
1279 /** Get the current timer configuration from its \ref Timer_Type::CONTROL "CONTROL" register.
1280 * \return Timer_Type::CONTROL
1281 */
1282 __STATIC_INLINE uint32_t PTIM_GetControl(void) {
1283   return(PTIM->CONTROL);
1284 }
1285
1286 /** Clears the event flag in timers \ref Timer_Type::ISR "ISR" register.
1287 */
1288 __STATIC_INLINE void PTIM_ClearEventFlag(void) {
1289   PTIM->ISR = 1;
1290 }
1291 #endif
1292 #endif
1293
1294 /* ##########################  MMU functions  ###################################### */
1295
1296 #define SECTION_DESCRIPTOR      (0x2)
1297 #define SECTION_MASK            (0xFFFFFFFC)
1298
1299 #define SECTION_TEXCB_MASK      (0xFFFF8FF3)
1300 #define SECTION_B_SHIFT         (2)
1301 #define SECTION_C_SHIFT         (3)
1302 #define SECTION_TEX0_SHIFT      (12)
1303 #define SECTION_TEX1_SHIFT      (13)
1304 #define SECTION_TEX2_SHIFT      (14)
1305
1306 #define SECTION_XN_MASK         (0xFFFFFFEF)
1307 #define SECTION_XN_SHIFT        (4)
1308
1309 #define SECTION_DOMAIN_MASK     (0xFFFFFE1F)
1310 #define SECTION_DOMAIN_SHIFT    (5)
1311
1312 #define SECTION_P_MASK          (0xFFFFFDFF)
1313 #define SECTION_P_SHIFT         (9)
1314
1315 #define SECTION_AP_MASK         (0xFFFF73FF)
1316 #define SECTION_AP_SHIFT        (10)
1317 #define SECTION_AP2_SHIFT       (15)
1318
1319 #define SECTION_S_MASK          (0xFFFEFFFF)
1320 #define SECTION_S_SHIFT         (16)
1321
1322 #define SECTION_NG_MASK         (0xFFFDFFFF)
1323 #define SECTION_NG_SHIFT        (17)
1324
1325 #define SECTION_NS_MASK         (0xFFF7FFFF)
1326 #define SECTION_NS_SHIFT        (19)
1327
1328 #define PAGE_L1_DESCRIPTOR      (0x1)
1329 #define PAGE_L1_MASK            (0xFFFFFFFC)
1330
1331 #define PAGE_L2_4K_DESC         (0x2)
1332 #define PAGE_L2_4K_MASK         (0xFFFFFFFD)
1333
1334 #define PAGE_L2_64K_DESC        (0x1)
1335 #define PAGE_L2_64K_MASK        (0xFFFFFFFC)
1336
1337 #define PAGE_4K_TEXCB_MASK      (0xFFFFFE33)
1338 #define PAGE_4K_B_SHIFT         (2)
1339 #define PAGE_4K_C_SHIFT         (3)
1340 #define PAGE_4K_TEX0_SHIFT      (6)
1341 #define PAGE_4K_TEX1_SHIFT      (7)
1342 #define PAGE_4K_TEX2_SHIFT      (8)
1343
1344 #define PAGE_64K_TEXCB_MASK     (0xFFFF8FF3)
1345 #define PAGE_64K_B_SHIFT        (2)
1346 #define PAGE_64K_C_SHIFT        (3)
1347 #define PAGE_64K_TEX0_SHIFT     (12)
1348 #define PAGE_64K_TEX1_SHIFT     (13)
1349 #define PAGE_64K_TEX2_SHIFT     (14)
1350
1351 #define PAGE_TEXCB_MASK         (0xFFFF8FF3)
1352 #define PAGE_B_SHIFT            (2)
1353 #define PAGE_C_SHIFT            (3)
1354 #define PAGE_TEX_SHIFT          (12)
1355
1356 #define PAGE_XN_4K_MASK         (0xFFFFFFFE)
1357 #define PAGE_XN_4K_SHIFT        (0)
1358 #define PAGE_XN_64K_MASK        (0xFFFF7FFF)
1359 #define PAGE_XN_64K_SHIFT       (15)
1360
1361 #define PAGE_DOMAIN_MASK        (0xFFFFFE1F)
1362 #define PAGE_DOMAIN_SHIFT       (5)
1363
1364 #define PAGE_P_MASK             (0xFFFFFDFF)
1365 #define PAGE_P_SHIFT            (9)
1366
1367 #define PAGE_AP_MASK            (0xFFFFFDCF)
1368 #define PAGE_AP_SHIFT           (4)
1369 #define PAGE_AP2_SHIFT          (9)
1370
1371 #define PAGE_S_MASK             (0xFFFFFBFF)
1372 #define PAGE_S_SHIFT            (10)
1373
1374 #define PAGE_NG_MASK            (0xFFFFF7FF)
1375 #define PAGE_NG_SHIFT           (11)
1376
1377 #define PAGE_NS_MASK            (0xFFFFFFF7)
1378 #define PAGE_NS_SHIFT           (3)
1379
1380 #define OFFSET_1M               (0x00100000)
1381 #define OFFSET_64K              (0x00010000)
1382 #define OFFSET_4K               (0x00001000)
1383
1384 #define DESCRIPTOR_FAULT        (0x00000000)
1385
1386 /* Attributes enumerations */
1387
1388 /* Region size attributes */
1389 typedef enum
1390 {
1391    SECTION,
1392    PAGE_4k,
1393    PAGE_64k,
1394 } mmu_region_size_Type;
1395
1396 /* Region type attributes */
1397 typedef enum
1398 {
1399    NORMAL,
1400    DEVICE,
1401    SHARED_DEVICE,
1402    NON_SHARED_DEVICE,
1403    STRONGLY_ORDERED
1404 } mmu_memory_Type;
1405
1406 /* Region cacheability attributes */
1407 typedef enum
1408 {
1409    NON_CACHEABLE,
1410    WB_WA,
1411    WT,
1412    WB_NO_WA,
1413 } mmu_cacheability_Type;
1414
1415 /* Region parity check attributes */
1416 typedef enum
1417 {
1418    ECC_DISABLED,
1419    ECC_ENABLED,
1420 } mmu_ecc_check_Type;
1421
1422 /* Region execution attributes */
1423 typedef enum
1424 {
1425    EXECUTE,
1426    NON_EXECUTE,
1427 } mmu_execute_Type;
1428
1429 /* Region global attributes */
1430 typedef enum
1431 {
1432    GLOBAL,
1433    NON_GLOBAL,
1434 } mmu_global_Type;
1435
1436 /* Region shareability attributes */
1437 typedef enum
1438 {
1439    NON_SHARED,
1440    SHARED,
1441 } mmu_shared_Type;
1442
1443 /* Region security attributes */
1444 typedef enum
1445 {
1446    SECURE,
1447    NON_SECURE,
1448 } mmu_secure_Type;
1449
1450 /* Region access attributes */
1451 typedef enum
1452 {
1453    NO_ACCESS,
1454    RW,
1455    READ,
1456 } mmu_access_Type;
1457
1458 /* Memory Region definition */
1459 typedef struct RegionStruct {
1460     mmu_region_size_Type rg_t;
1461     mmu_memory_Type mem_t;
1462     uint8_t domain;
1463     mmu_cacheability_Type inner_norm_t;
1464     mmu_cacheability_Type outer_norm_t;
1465     mmu_ecc_check_Type e_t;
1466     mmu_execute_Type xn_t;
1467     mmu_global_Type g_t;
1468     mmu_secure_Type sec_t;
1469     mmu_access_Type priv_t;
1470     mmu_access_Type user_t;
1471     mmu_shared_Type sh_t;
1472
1473 } mmu_region_attributes_Type;
1474
1475 //Following macros define the descriptors and attributes
1476 //Sect_Normal. Outer & inner wb/wa, non-shareable, executable, rw, domain 0
1477 #define section_normal(descriptor_l1, region)     region.rg_t = SECTION; \
1478                                    region.domain = 0x0; \
1479                                    region.e_t = ECC_DISABLED; \
1480                                    region.g_t = GLOBAL; \
1481                                    region.inner_norm_t = WB_WA; \
1482                                    region.outer_norm_t = WB_WA; \
1483                                    region.mem_t = NORMAL; \
1484                                    region.sec_t = SECURE; \
1485                                    region.xn_t = EXECUTE; \
1486                                    region.priv_t = RW; \
1487                                    region.user_t = RW; \
1488                                    region.sh_t = NON_SHARED; \
1489                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1490
1491 //Sect_Normal_Cod. Outer & inner wb/wa, non-shareable, executable, ro, domain 0
1492 #define section_normal_cod(descriptor_l1, region) region.rg_t = SECTION; \
1493                                    region.domain = 0x0; \
1494                                    region.e_t = ECC_DISABLED; \
1495                                    region.g_t = GLOBAL; \
1496                                    region.inner_norm_t = WB_WA; \
1497                                    region.outer_norm_t = WB_WA; \
1498                                    region.mem_t = NORMAL; \
1499                                    region.sec_t = SECURE; \
1500                                    region.xn_t = EXECUTE; \
1501                                    region.priv_t = READ; \
1502                                    region.user_t = READ; \
1503                                    region.sh_t = NON_SHARED; \
1504                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1505
1506 //Sect_Normal_RO. Sect_Normal_Cod, but not executable
1507 #define section_normal_ro(descriptor_l1, region)  region.rg_t = SECTION; \
1508                                    region.domain = 0x0; \
1509                                    region.e_t = ECC_DISABLED; \
1510                                    region.g_t = GLOBAL; \
1511                                    region.inner_norm_t = WB_WA; \
1512                                    region.outer_norm_t = WB_WA; \
1513                                    region.mem_t = NORMAL; \
1514                                    region.sec_t = SECURE; \
1515                                    region.xn_t = NON_EXECUTE; \
1516                                    region.priv_t = READ; \
1517                                    region.user_t = READ; \
1518                                    region.sh_t = NON_SHARED; \
1519                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1520
1521 //Sect_Normal_RW. Sect_Normal_Cod, but writeable and not executable
1522 #define section_normal_rw(descriptor_l1, region) region.rg_t = SECTION; \
1523                                    region.domain = 0x0; \
1524                                    region.e_t = ECC_DISABLED; \
1525                                    region.g_t = GLOBAL; \
1526                                    region.inner_norm_t = WB_WA; \
1527                                    region.outer_norm_t = WB_WA; \
1528                                    region.mem_t = NORMAL; \
1529                                    region.sec_t = SECURE; \
1530                                    region.xn_t = NON_EXECUTE; \
1531                                    region.priv_t = RW; \
1532                                    region.user_t = RW; \
1533                                    region.sh_t = NON_SHARED; \
1534                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1535 //Sect_SO. Strongly-ordered (therefore shareable), not executable, rw, domain 0, base addr 0
1536 #define section_so(descriptor_l1, region) region.rg_t = SECTION; \
1537                                    region.domain = 0x0; \
1538                                    region.e_t = ECC_DISABLED; \
1539                                    region.g_t = GLOBAL; \
1540                                    region.inner_norm_t = NON_CACHEABLE; \
1541                                    region.outer_norm_t = NON_CACHEABLE; \
1542                                    region.mem_t = STRONGLY_ORDERED; \
1543                                    region.sec_t = SECURE; \
1544                                    region.xn_t = NON_EXECUTE; \
1545                                    region.priv_t = RW; \
1546                                    region.user_t = RW; \
1547                                    region.sh_t = NON_SHARED; \
1548                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1549
1550 //Sect_Device_RO. Device, non-shareable, non-executable, ro, domain 0, base addr 0
1551 #define section_device_ro(descriptor_l1, region) region.rg_t = SECTION; \
1552                                    region.domain = 0x0; \
1553                                    region.e_t = ECC_DISABLED; \
1554                                    region.g_t = GLOBAL; \
1555                                    region.inner_norm_t = NON_CACHEABLE; \
1556                                    region.outer_norm_t = NON_CACHEABLE; \
1557                                    region.mem_t = STRONGLY_ORDERED; \
1558                                    region.sec_t = SECURE; \
1559                                    region.xn_t = NON_EXECUTE; \
1560                                    region.priv_t = READ; \
1561                                    region.user_t = READ; \
1562                                    region.sh_t = NON_SHARED; \
1563                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1564
1565 //Sect_Device_RW. Sect_Device_RO, but writeable
1566 #define section_device_rw(descriptor_l1, region) region.rg_t = SECTION; \
1567                                    region.domain = 0x0; \
1568                                    region.e_t = ECC_DISABLED; \
1569                                    region.g_t = GLOBAL; \
1570                                    region.inner_norm_t = NON_CACHEABLE; \
1571                                    region.outer_norm_t = NON_CACHEABLE; \
1572                                    region.mem_t = STRONGLY_ORDERED; \
1573                                    region.sec_t = SECURE; \
1574                                    region.xn_t = NON_EXECUTE; \
1575                                    region.priv_t = RW; \
1576                                    region.user_t = RW; \
1577                                    region.sh_t = NON_SHARED; \
1578                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1579 //Page_4k_Device_RW.  Shared device, not executable, rw, domain 0
1580 #define page4k_device_rw(descriptor_l1, descriptor_l2, region) region.rg_t = PAGE_4k; \
1581                                    region.domain = 0x0; \
1582                                    region.e_t = ECC_DISABLED; \
1583                                    region.g_t = GLOBAL; \
1584                                    region.inner_norm_t = NON_CACHEABLE; \
1585                                    region.outer_norm_t = NON_CACHEABLE; \
1586                                    region.mem_t = SHARED_DEVICE; \
1587                                    region.sec_t = SECURE; \
1588                                    region.xn_t = NON_EXECUTE; \
1589                                    region.priv_t = RW; \
1590                                    region.user_t = RW; \
1591                                    region.sh_t = NON_SHARED; \
1592                                    MMU_GetPageDescriptor(&descriptor_l1, &descriptor_l2, region);
1593
1594 //Page_64k_Device_RW.  Shared device, not executable, rw, domain 0
1595 #define page64k_device_rw(descriptor_l1, descriptor_l2, region)  region.rg_t = PAGE_64k; \
1596                                    region.domain = 0x0; \
1597                                    region.e_t = ECC_DISABLED; \
1598                                    region.g_t = GLOBAL; \
1599                                    region.inner_norm_t = NON_CACHEABLE; \
1600                                    region.outer_norm_t = NON_CACHEABLE; \
1601                                    region.mem_t = SHARED_DEVICE; \
1602                                    region.sec_t = SECURE; \
1603                                    region.xn_t = NON_EXECUTE; \
1604                                    region.priv_t = RW; \
1605                                    region.user_t = RW; \
1606                                    region.sh_t = NON_SHARED; \
1607                                    MMU_GetPageDescriptor(&descriptor_l1, &descriptor_l2, region);
1608
1609 /** \brief  Set section execution-never attribute
1610
1611   \param [out]    descriptor_l1  L1 descriptor.
1612   \param [in]                xn  Section execution-never attribute : EXECUTE , NON_EXECUTE.
1613
1614   \return          0
1615 */
1616 __STATIC_INLINE int MMU_XNSection(uint32_t *descriptor_l1, mmu_execute_Type xn)
1617 {
1618   *descriptor_l1 &= SECTION_XN_MASK;
1619   *descriptor_l1 |= ((xn & 0x1) << SECTION_XN_SHIFT);
1620   return 0;
1621 }
1622
1623 /** \brief  Set section domain
1624
1625   \param [out]    descriptor_l1  L1 descriptor.
1626   \param [in]            domain  Section domain
1627
1628   \return          0
1629 */
1630 __STATIC_INLINE int MMU_DomainSection(uint32_t *descriptor_l1, uint8_t domain)
1631 {
1632   *descriptor_l1 &= SECTION_DOMAIN_MASK;
1633   *descriptor_l1 |= ((domain & 0xF) << SECTION_DOMAIN_SHIFT);
1634   return 0;
1635 }
1636
1637 /** \brief  Set section parity check
1638
1639   \param [out]    descriptor_l1  L1 descriptor.
1640   \param [in]              p_bit Parity check: ECC_DISABLED, ECC_ENABLED
1641
1642   \return          0
1643 */
1644 __STATIC_INLINE int MMU_PSection(uint32_t *descriptor_l1, mmu_ecc_check_Type p_bit)
1645 {
1646   *descriptor_l1 &= SECTION_P_MASK;
1647   *descriptor_l1 |= ((p_bit & 0x1) << SECTION_P_SHIFT);
1648   return 0;
1649 }
1650
1651 /** \brief  Set section access privileges
1652
1653   \param [out]    descriptor_l1  L1 descriptor.
1654   \param [in]              user  User Level Access: NO_ACCESS, RW, READ
1655   \param [in]              priv  Privilege Level Access: NO_ACCESS, RW, READ
1656   \param [in]               afe  Access flag enable
1657
1658   \return          0
1659 */
1660 __STATIC_INLINE int MMU_APSection(uint32_t *descriptor_l1, mmu_access_Type user, mmu_access_Type priv, uint32_t afe)
1661 {
1662   uint32_t ap = 0;
1663
1664   if (afe == 0) { //full access
1665     if ((priv == NO_ACCESS) && (user == NO_ACCESS)) { ap = 0x0; }
1666     else if ((priv == RW) && (user == NO_ACCESS))   { ap = 0x1; }
1667     else if ((priv == RW) && (user == READ))        { ap = 0x2; }
1668     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1669     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1670     else if ((priv == READ) && (user == READ))      { ap = 0x7; }
1671   }
1672
1673   else { //Simplified access
1674     if ((priv == RW) && (user == NO_ACCESS))        { ap = 0x1; }
1675     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1676     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1677     else if ((priv == READ) && (user == READ))      { ap = 0x7; }
1678   }
1679
1680   *descriptor_l1 &= SECTION_AP_MASK;
1681   *descriptor_l1 |= (ap & 0x3) << SECTION_AP_SHIFT;
1682   *descriptor_l1 |= ((ap & 0x4)>>2) << SECTION_AP2_SHIFT;
1683
1684   return 0;
1685 }
1686
1687 /** \brief  Set section shareability
1688
1689   \param [out]    descriptor_l1  L1 descriptor.
1690   \param [in]             s_bit  Section shareability: NON_SHARED, SHARED
1691
1692   \return          0
1693 */
1694 __STATIC_INLINE int MMU_SharedSection(uint32_t *descriptor_l1, mmu_shared_Type s_bit)
1695 {
1696   *descriptor_l1 &= SECTION_S_MASK;
1697   *descriptor_l1 |= ((s_bit & 0x1) << SECTION_S_SHIFT);
1698   return 0;
1699 }
1700
1701 /** \brief  Set section Global attribute
1702
1703   \param [out]    descriptor_l1  L1 descriptor.
1704   \param [in]             g_bit  Section attribute: GLOBAL, NON_GLOBAL
1705
1706   \return          0
1707 */
1708 __STATIC_INLINE int MMU_GlobalSection(uint32_t *descriptor_l1, mmu_global_Type g_bit)
1709 {
1710   *descriptor_l1 &= SECTION_NG_MASK;
1711   *descriptor_l1 |= ((g_bit & 0x1) << SECTION_NG_SHIFT);
1712   return 0;
1713 }
1714
1715 /** \brief  Set section Security attribute
1716
1717   \param [out]    descriptor_l1  L1 descriptor.
1718   \param [in]             s_bit  Section Security attribute: SECURE, NON_SECURE
1719
1720   \return          0
1721 */
1722 __STATIC_INLINE int MMU_SecureSection(uint32_t *descriptor_l1, mmu_secure_Type s_bit)
1723 {
1724   *descriptor_l1 &= SECTION_NS_MASK;
1725   *descriptor_l1 |= ((s_bit & 0x1) << SECTION_NS_SHIFT);
1726   return 0;
1727 }
1728
1729 /* Page 4k or 64k */
1730 /** \brief  Set 4k/64k page execution-never attribute
1731
1732   \param [out]    descriptor_l2  L2 descriptor.
1733   \param [in]                xn  Page execution-never attribute : EXECUTE , NON_EXECUTE.
1734   \param [in]              page  Page size: PAGE_4k, PAGE_64k,
1735
1736   \return          0
1737 */
1738 __STATIC_INLINE int MMU_XNPage(uint32_t *descriptor_l2, mmu_execute_Type xn, mmu_region_size_Type page)
1739 {
1740   if (page == PAGE_4k)
1741   {
1742       *descriptor_l2 &= PAGE_XN_4K_MASK;
1743       *descriptor_l2 |= ((xn & 0x1) << PAGE_XN_4K_SHIFT);
1744   }
1745   else
1746   {
1747       *descriptor_l2 &= PAGE_XN_64K_MASK;
1748       *descriptor_l2 |= ((xn & 0x1) << PAGE_XN_64K_SHIFT);
1749   }
1750   return 0;
1751 }
1752
1753 /** \brief  Set 4k/64k page domain
1754
1755   \param [out]    descriptor_l1  L1 descriptor.
1756   \param [in]            domain  Page domain
1757
1758   \return          0
1759 */
1760 __STATIC_INLINE int MMU_DomainPage(uint32_t *descriptor_l1, uint8_t domain)
1761 {
1762   *descriptor_l1 &= PAGE_DOMAIN_MASK;
1763   *descriptor_l1 |= ((domain & 0xf) << PAGE_DOMAIN_SHIFT);
1764   return 0;
1765 }
1766
1767 /** \brief  Set 4k/64k page parity check
1768
1769   \param [out]    descriptor_l1  L1 descriptor.
1770   \param [in]              p_bit Parity check: ECC_DISABLED, ECC_ENABLED
1771
1772   \return          0
1773 */
1774 __STATIC_INLINE int MMU_PPage(uint32_t *descriptor_l1, mmu_ecc_check_Type p_bit)
1775 {
1776   *descriptor_l1 &= SECTION_P_MASK;
1777   *descriptor_l1 |= ((p_bit & 0x1) << SECTION_P_SHIFT);
1778   return 0;
1779 }
1780
1781 /** \brief  Set 4k/64k page access privileges
1782
1783   \param [out]    descriptor_l2  L2 descriptor.
1784   \param [in]              user  User Level Access: NO_ACCESS, RW, READ
1785   \param [in]              priv  Privilege Level Access: NO_ACCESS, RW, READ
1786   \param [in]               afe  Access flag enable
1787
1788   \return          0
1789 */
1790 __STATIC_INLINE int MMU_APPage(uint32_t *descriptor_l2, mmu_access_Type user, mmu_access_Type priv, uint32_t afe)
1791 {
1792   uint32_t ap = 0;
1793
1794   if (afe == 0) { //full access
1795     if ((priv == NO_ACCESS) && (user == NO_ACCESS)) { ap = 0x0; }
1796     else if ((priv == RW) && (user == NO_ACCESS))   { ap = 0x1; }
1797     else if ((priv == RW) && (user == READ))        { ap = 0x2; }
1798     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1799     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1800     else if ((priv == READ) && (user == READ))      { ap = 0x6; }
1801   }
1802
1803   else { //Simplified access
1804     if ((priv == RW) && (user == NO_ACCESS))        { ap = 0x1; }
1805     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1806     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1807     else if ((priv == READ) && (user == READ))      { ap = 0x7; }
1808   }
1809
1810   *descriptor_l2 &= PAGE_AP_MASK;
1811   *descriptor_l2 |= (ap & 0x3) << PAGE_AP_SHIFT;
1812   *descriptor_l2 |= ((ap & 0x4)>>2) << PAGE_AP2_SHIFT;
1813
1814   return 0;
1815 }
1816
1817 /** \brief  Set 4k/64k page shareability
1818
1819   \param [out]    descriptor_l2  L2 descriptor.
1820   \param [in]             s_bit  4k/64k page shareability: NON_SHARED, SHARED
1821
1822   \return          0
1823 */
1824 __STATIC_INLINE int MMU_SharedPage(uint32_t *descriptor_l2, mmu_shared_Type s_bit)
1825 {
1826   *descriptor_l2 &= PAGE_S_MASK;
1827   *descriptor_l2 |= ((s_bit & 0x1) << PAGE_S_SHIFT);
1828   return 0;
1829 }
1830
1831 /** \brief  Set 4k/64k page Global attribute
1832
1833   \param [out]    descriptor_l2  L2 descriptor.
1834   \param [in]             g_bit  4k/64k page attribute: GLOBAL, NON_GLOBAL
1835
1836   \return          0
1837 */
1838 __STATIC_INLINE int MMU_GlobalPage(uint32_t *descriptor_l2, mmu_global_Type g_bit)
1839 {
1840   *descriptor_l2 &= PAGE_NG_MASK;
1841   *descriptor_l2 |= ((g_bit & 0x1) << PAGE_NG_SHIFT);
1842   return 0;
1843 }
1844
1845 /** \brief  Set 4k/64k page Security attribute
1846
1847   \param [out]    descriptor_l1  L1 descriptor.
1848   \param [in]             s_bit  4k/64k page Security attribute: SECURE, NON_SECURE
1849
1850   \return          0
1851 */
1852 __STATIC_INLINE int MMU_SecurePage(uint32_t *descriptor_l1, mmu_secure_Type s_bit)
1853 {
1854   *descriptor_l1 &= PAGE_NS_MASK;
1855   *descriptor_l1 |= ((s_bit & 0x1) << PAGE_NS_SHIFT);
1856   return 0;
1857 }
1858
1859 /** \brief  Set Section memory attributes
1860
1861   \param [out]    descriptor_l1  L1 descriptor.
1862   \param [in]               mem  Section memory type: NORMAL, DEVICE, SHARED_DEVICE, NON_SHARED_DEVICE, STRONGLY_ORDERED
1863   \param [in]             outer  Outer cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
1864   \param [in]             inner  Inner cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
1865
1866   \return          0
1867 */
1868 __STATIC_INLINE int MMU_MemorySection(uint32_t *descriptor_l1, mmu_memory_Type mem, mmu_cacheability_Type outer, mmu_cacheability_Type inner)
1869 {
1870   *descriptor_l1 &= SECTION_TEXCB_MASK;
1871
1872   if (STRONGLY_ORDERED == mem)
1873   {
1874     return 0;
1875   }
1876   else if (SHARED_DEVICE == mem)
1877   {
1878     *descriptor_l1 |= (1 << SECTION_B_SHIFT);
1879   }
1880   else if (NON_SHARED_DEVICE == mem)
1881   {
1882     *descriptor_l1 |= (1 << SECTION_TEX1_SHIFT);
1883   }
1884   else if (NORMAL == mem)
1885   {
1886    *descriptor_l1 |= 1 << SECTION_TEX2_SHIFT;
1887    switch(inner)
1888    {
1889       case NON_CACHEABLE:
1890         break;
1891       case WB_WA:
1892         *descriptor_l1 |= (1 << SECTION_B_SHIFT);
1893         break;
1894       case WT:
1895         *descriptor_l1 |= 1 << SECTION_C_SHIFT;
1896         break;
1897       case WB_NO_WA:
1898         *descriptor_l1 |= (1 << SECTION_B_SHIFT) | (1 << SECTION_C_SHIFT);
1899         break;
1900     }
1901     switch(outer)
1902     {
1903       case NON_CACHEABLE:
1904         break;
1905       case WB_WA:
1906         *descriptor_l1 |= (1 << SECTION_TEX0_SHIFT);
1907         break;
1908       case WT:
1909         *descriptor_l1 |= 1 << SECTION_TEX1_SHIFT;
1910         break;
1911       case WB_NO_WA:
1912         *descriptor_l1 |= (1 << SECTION_TEX0_SHIFT) | (1 << SECTION_TEX0_SHIFT);
1913         break;
1914     }
1915   }
1916   return 0;
1917 }
1918
1919 /** \brief  Set 4k/64k page memory attributes
1920
1921   \param [out]    descriptor_l2  L2 descriptor.
1922   \param [in]               mem  4k/64k page memory type: NORMAL, DEVICE, SHARED_DEVICE, NON_SHARED_DEVICE, STRONGLY_ORDERED
1923   \param [in]             outer  Outer cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
1924   \param [in]             inner  Inner cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
1925   \param [in]              page  Page size
1926
1927   \return          0
1928 */
1929 __STATIC_INLINE int MMU_MemoryPage(uint32_t *descriptor_l2, mmu_memory_Type mem, mmu_cacheability_Type outer, mmu_cacheability_Type inner, mmu_region_size_Type page)
1930 {
1931   *descriptor_l2 &= PAGE_4K_TEXCB_MASK;
1932
1933   if (page == PAGE_64k)
1934   {
1935     //same as section
1936     MMU_MemorySection(descriptor_l2, mem, outer, inner);
1937   }
1938   else
1939   {
1940     if (STRONGLY_ORDERED == mem)
1941     {
1942       return 0;
1943     }
1944     else if (SHARED_DEVICE == mem)
1945     {
1946       *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT);
1947     }
1948     else if (NON_SHARED_DEVICE == mem)
1949     {
1950       *descriptor_l2 |= (1 << PAGE_4K_TEX1_SHIFT);
1951     }
1952     else if (NORMAL == mem)
1953     {
1954       *descriptor_l2 |= 1 << PAGE_4K_TEX2_SHIFT;
1955       switch(inner)
1956       {
1957         case NON_CACHEABLE:
1958           break;
1959         case WB_WA:
1960           *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT);
1961           break;
1962         case WT:
1963           *descriptor_l2 |= 1 << PAGE_4K_C_SHIFT;
1964           break;
1965         case WB_NO_WA:
1966           *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT) | (1 << PAGE_4K_C_SHIFT);
1967           break;
1968       }
1969       switch(outer)
1970       {
1971         case NON_CACHEABLE:
1972           break;
1973         case WB_WA:
1974           *descriptor_l2 |= (1 << PAGE_4K_TEX0_SHIFT);
1975           break;
1976         case WT:
1977           *descriptor_l2 |= 1 << PAGE_4K_TEX1_SHIFT;
1978           break;
1979         case WB_NO_WA:
1980           *descriptor_l2 |= (1 << PAGE_4K_TEX0_SHIFT) | (1 << PAGE_4K_TEX0_SHIFT);
1981           break;
1982       }
1983     }
1984   }
1985
1986   return 0;
1987 }
1988
1989 /** \brief  Create a L1 section descriptor
1990
1991   \param [out]     descriptor  L1 descriptor
1992   \param [in]      reg  Section attributes
1993   
1994   \return          0
1995 */
1996 __STATIC_INLINE int MMU_GetSectionDescriptor(uint32_t *descriptor, mmu_region_attributes_Type reg)
1997 {
1998   *descriptor  = 0;
1999
2000   MMU_MemorySection(descriptor, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t);
2001   MMU_XNSection(descriptor,reg.xn_t);
2002   MMU_DomainSection(descriptor, reg.domain);
2003   MMU_PSection(descriptor, reg.e_t);
2004   MMU_APSection(descriptor, reg.priv_t, reg.user_t, 1);
2005   MMU_SharedSection(descriptor,reg.sh_t);
2006   MMU_GlobalSection(descriptor,reg.g_t);
2007   MMU_SecureSection(descriptor,reg.sec_t);
2008   *descriptor &= SECTION_MASK;
2009   *descriptor |= SECTION_DESCRIPTOR;
2010  
2011   return 0;
2012 }
2013
2014
2015 /** \brief  Create a L1 and L2 4k/64k page descriptor
2016
2017   \param [out]       descriptor  L1 descriptor
2018   \param [out]      descriptor2  L2 descriptor
2019   \param [in]               reg  4k/64k page attributes
2020
2021   \return          0
2022 */
2023 __STATIC_INLINE int MMU_GetPageDescriptor(uint32_t *descriptor, uint32_t *descriptor2, mmu_region_attributes_Type reg)
2024 {
2025   *descriptor  = 0;
2026   *descriptor2 = 0;
2027
2028   switch (reg.rg_t)
2029   {
2030     case PAGE_4k:
2031       MMU_MemoryPage(descriptor2, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t, PAGE_4k);
2032       MMU_XNPage(descriptor2, reg.xn_t, PAGE_4k);
2033       MMU_DomainPage(descriptor, reg.domain);
2034       MMU_PPage(descriptor, reg.e_t);
2035       MMU_APPage(descriptor2, reg.priv_t, reg.user_t, 1);
2036       MMU_SharedPage(descriptor2,reg.sh_t);
2037       MMU_GlobalPage(descriptor2,reg.g_t);
2038       MMU_SecurePage(descriptor,reg.sec_t);
2039       *descriptor &= PAGE_L1_MASK;
2040       *descriptor |= PAGE_L1_DESCRIPTOR;
2041       *descriptor2 &= PAGE_L2_4K_MASK;
2042       *descriptor2 |= PAGE_L2_4K_DESC;
2043       break;
2044
2045     case PAGE_64k:
2046       MMU_MemoryPage(descriptor2, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t, PAGE_64k);
2047       MMU_XNPage(descriptor2, reg.xn_t, PAGE_64k);
2048       MMU_DomainPage(descriptor, reg.domain);
2049       MMU_PPage(descriptor, reg.e_t);
2050       MMU_APPage(descriptor2, reg.priv_t, reg.user_t, 1);
2051       MMU_SharedPage(descriptor2,reg.sh_t);
2052       MMU_GlobalPage(descriptor2,reg.g_t);
2053       MMU_SecurePage(descriptor,reg.sec_t);
2054       *descriptor &= PAGE_L1_MASK;
2055       *descriptor |= PAGE_L1_DESCRIPTOR;
2056       *descriptor2 &= PAGE_L2_64K_MASK;
2057       *descriptor2 |= PAGE_L2_64K_DESC;
2058       break;
2059
2060     case SECTION:
2061       //error
2062       break;
2063   }
2064   
2065   return 0;
2066 }
2067
2068 /** \brief  Create a 1MB Section
2069
2070   \param [in]               ttb  Translation table base address
2071   \param [in]      base_address  Section base address
2072   \param [in]             count  Number of sections to create
2073   \param [in]     descriptor_l1  L1 descriptor (region attributes)
2074
2075 */
2076 __STATIC_INLINE void MMU_TTSection(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1)
2077 {
2078   uint32_t offset;
2079   uint32_t entry;
2080   uint32_t i;
2081
2082   offset = base_address >> 20;
2083   entry  = (base_address & 0xFFF00000) | descriptor_l1;
2084
2085   //4 bytes aligned
2086   ttb = ttb + offset;
2087
2088   for (i = 0; i < count; i++ )
2089   {
2090     //4 bytes aligned
2091     *ttb++ = entry;
2092     entry += OFFSET_1M;
2093   }
2094 }
2095
2096 /** \brief  Create a 4k page entry
2097
2098   \param [in]               ttb  L1 table base address
2099   \param [in]      base_address  4k base address
2100   \param [in]             count  Number of 4k pages to create
2101   \param [in]     descriptor_l1  L1 descriptor (region attributes)
2102   \param [in]            ttb_l2  L2 table base address
2103   \param [in]     descriptor_l2  L2 descriptor (region attributes)
2104
2105 */
2106 __STATIC_INLINE void MMU_TTPage4k(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1, uint32_t *ttb_l2, uint32_t descriptor_l2 )
2107 {
2108
2109   uint32_t offset, offset2;
2110   uint32_t entry, entry2;
2111   uint32_t i;
2112
2113   offset = base_address >> 20;
2114   entry  = ((int)ttb_l2 & 0xFFFFFC00) | descriptor_l1;
2115
2116   //4 bytes aligned
2117   ttb += offset;
2118   //create l1_entry
2119   *ttb = entry;
2120
2121   offset2 = (base_address & 0xff000) >> 12;
2122   ttb_l2 += offset2;
2123   entry2 = (base_address & 0xFFFFF000) | descriptor_l2;
2124   for (i = 0; i < count; i++ )
2125   {
2126     //4 bytes aligned
2127     *ttb_l2++ = entry2;
2128     entry2 += OFFSET_4K;
2129   }
2130 }
2131
2132 /** \brief  Create a 64k page entry
2133
2134   \param [in]               ttb  L1 table base address
2135   \param [in]      base_address  64k base address
2136   \param [in]             count  Number of 64k pages to create
2137   \param [in]     descriptor_l1  L1 descriptor (region attributes)
2138   \param [in]            ttb_l2  L2 table base address
2139   \param [in]     descriptor_l2  L2 descriptor (region attributes)
2140
2141 */
2142 __STATIC_INLINE void MMU_TTPage64k(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1, uint32_t *ttb_l2, uint32_t descriptor_l2 )
2143 {
2144   uint32_t offset, offset2;
2145   uint32_t entry, entry2;
2146   uint32_t i,j;
2147
2148
2149   offset = base_address >> 20;
2150   entry  = ((int)ttb_l2 & 0xFFFFFC00) | descriptor_l1;
2151
2152   //4 bytes aligned
2153   ttb += offset;
2154   //create l1_entry
2155   *ttb = entry;
2156
2157   offset2 = (base_address & 0xff000) >> 12;
2158   ttb_l2 += offset2;
2159   entry2 = (base_address & 0xFFFF0000) | descriptor_l2;
2160   for (i = 0; i < count; i++ )
2161   {
2162     //create 16 entries
2163     for (j = 0; j < 16; j++)
2164     {
2165       //4 bytes aligned
2166       *ttb_l2++ = entry2;
2167     }
2168     entry2 += OFFSET_64K;
2169   }
2170 }
2171
2172 /** \brief  Enable MMU
2173 */
2174 __STATIC_INLINE void MMU_Enable(void) {
2175   // Set M bit 0 to enable the MMU
2176   // Set AFE bit to enable simplified access permissions model
2177   // Clear TRE bit to disable TEX remap and A bit to disable strict alignment fault checking
2178   __set_SCTLR( (__get_SCTLR() & ~(1 << 28) & ~(1 << 1)) | 1 | (1 << 29));
2179   __ISB();
2180 }
2181
2182 /** \brief  Disable MMU
2183 */
2184 __STATIC_INLINE void MMU_Disable(void) {
2185   // Clear M bit 0 to disable the MMU
2186   __set_SCTLR( __get_SCTLR() & ~1);
2187   __ISB();
2188 }
2189
2190 /** \brief  Invalidate entire unified TLB
2191 */
2192
2193 __STATIC_INLINE void MMU_InvalidateTLB(void) {
2194   __set_TLBIALL(0);
2195   __DSB();     //ensure completion of the invalidation
2196   __ISB();     //ensure instruction fetch path sees new state
2197 }
2198
2199
2200 #ifdef __cplusplus
2201 }
2202 #endif
2203
2204 #endif /* __CORE_CA_H_DEPENDANT */
2205
2206 #endif /* __CMSIS_GENERIC */