summaryrefslogtreecommitdiff
path: root/ext/TARGET_LPC1768/core_ca_mmu.h
blob: 3ce170dc4054c76ac075910d56fa55dbc0f474bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
;/**************************************************************************//**
; * @file     core_ca_mmu.h
; * @brief    MMU Startup File for A9_MP Device Series
; * @version  V1.01
; * @date     10 Sept 2014
; *
; * @note
; *
; ******************************************************************************/
;/* Copyright (c) 2012-2014 ARM LIMITED
;
;   All rights reserved.
;   Redistribution and use in source and binary forms, with or without
;   modification, are permitted provided that the following conditions are met:
;   - Redistributions of source code must retain the above copyright
;     notice, this list of conditions and the following disclaimer.
;   - Redistributions in binary form must reproduce the above copyright
;     notice, this list of conditions and the following disclaimer in the
;     documentation and/or other materials provided with the distribution.
;   - Neither the name of ARM nor the names of its contributors may be used
;     to endorse or promote products derived from this software without
;     specific prior written permission.
;   *
;   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
;   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;   POSSIBILITY OF SUCH DAMAGE.
;   ---------------------------------------------------------------------------*/

#ifdef __cplusplus
 extern "C" {
#endif

#ifndef _MMU_FUNC_H
#define _MMU_FUNC_H

#define SECTION_DESCRIPTOR      (0x2)
#define SECTION_MASK            (0xFFFFFFFC)

#define SECTION_TEXCB_MASK      (0xFFFF8FF3)
#define SECTION_B_SHIFT         (2)
#define SECTION_C_SHIFT         (3)
#define SECTION_TEX0_SHIFT      (12)
#define SECTION_TEX1_SHIFT      (13)
#define SECTION_TEX2_SHIFT      (14)

#define SECTION_XN_MASK         (0xFFFFFFEF)
#define SECTION_XN_SHIFT        (4)

#define SECTION_DOMAIN_MASK     (0xFFFFFE1F)
#define SECTION_DOMAIN_SHIFT    (5)

#define SECTION_P_MASK          (0xFFFFFDFF)
#define SECTION_P_SHIFT         (9)

#define SECTION_AP_MASK         (0xFFFF73FF)
#define SECTION_AP_SHIFT        (10)
#define SECTION_AP2_SHIFT       (15)

#define SECTION_S_MASK          (0xFFFEFFFF)
#define SECTION_S_SHIFT         (16)

#define SECTION_NG_MASK         (0xFFFDFFFF)
#define SECTION_NG_SHIFT        (17)

#define SECTION_NS_MASK         (0xFFF7FFFF)
#define SECTION_NS_SHIFT        (19)


#define PAGE_L1_DESCRIPTOR      (0x1)
#define PAGE_L1_MASK            (0xFFFFFFFC)

#define PAGE_L2_4K_DESC         (0x2)
#define PAGE_L2_4K_MASK         (0xFFFFFFFD)

#define PAGE_L2_64K_DESC        (0x1)
#define PAGE_L2_64K_MASK        (0xFFFFFFFC)

#define PAGE_4K_TEXCB_MASK      (0xFFFFFE33)
#define PAGE_4K_B_SHIFT         (2)
#define PAGE_4K_C_SHIFT         (3)
#define PAGE_4K_TEX0_SHIFT      (6)
#define PAGE_4K_TEX1_SHIFT      (7)
#define PAGE_4K_TEX2_SHIFT      (8)

#define PAGE_64K_TEXCB_MASK     (0xFFFF8FF3)
#define PAGE_64K_B_SHIFT        (2)
#define PAGE_64K_C_SHIFT        (3)
#define PAGE_64K_TEX0_SHIFT     (12)
#define PAGE_64K_TEX1_SHIFT     (13)
#define PAGE_64K_TEX2_SHIFT     (14)

#define PAGE_TEXCB_MASK         (0xFFFF8FF3)
#define PAGE_B_SHIFT            (2)
#define PAGE_C_SHIFT            (3)
#define PAGE_TEX_SHIFT          (12)

#define PAGE_XN_4K_MASK         (0xFFFFFFFE)
#define PAGE_XN_4K_SHIFT        (0)
#define PAGE_XN_64K_MASK        (0xFFFF7FFF)
#define PAGE_XN_64K_SHIFT       (15)


#define PAGE_DOMAIN_MASK        (0xFFFFFE1F)
#define PAGE_DOMAIN_SHIFT       (5)

#define PAGE_P_MASK             (0xFFFFFDFF)
#define PAGE_P_SHIFT            (9)

#define PAGE_AP_MASK            (0xFFFFFDCF)
#define PAGE_AP_SHIFT           (4)
#define PAGE_AP2_SHIFT          (9)

#define PAGE_S_MASK             (0xFFFFFBFF)
#define PAGE_S_SHIFT            (10)

#define PAGE_NG_MASK            (0xFFFFF7FF)
#define PAGE_NG_SHIFT           (11)

#define PAGE_NS_MASK            (0xFFFFFFF7)
#define PAGE_NS_SHIFT           (3)

#define OFFSET_1M               (0x00100000)
#define OFFSET_64K              (0x00010000)
#define OFFSET_4K               (0x00001000)

#define DESCRIPTOR_FAULT        (0x00000000)

/* ###########################  MMU Function Access  ########################### */
/** \ingroup  MMU_FunctionInterface
    \defgroup MMU_Functions MMU Functions Interface
  @{
 */

/* Attributes enumerations */

/* Region size attributes */
typedef enum
{
   SECTION,
   PAGE_4k,
   PAGE_64k,
} mmu_region_size_Type;

/* Region type attributes */
typedef enum
{
   NORMAL,
   DEVICE,
   SHARED_DEVICE,
   NON_SHARED_DEVICE,
   STRONGLY_ORDERED
} mmu_memory_Type;

/* Region cacheability attributes */
typedef enum
{
   NON_CACHEABLE,
   WB_WA,
   WT,
   WB_NO_WA,
} mmu_cacheability_Type;

/* Region parity check attributes */
typedef enum
{
   ECC_DISABLED,
   ECC_ENABLED,
} mmu_ecc_check_Type;

/* Region execution attributes */
typedef enum
{
   EXECUTE,
   NON_EXECUTE,
} mmu_execute_Type;

/* Region global attributes */
typedef enum
{
   GLOBAL,
   NON_GLOBAL,
} mmu_global_Type;

/* Region shareability attributes */
typedef enum
{
   NON_SHARED,
   SHARED,
} mmu_shared_Type;

/* Region security attributes */
typedef enum
{
   SECURE,
   NON_SECURE,
} mmu_secure_Type;

/* Region access attributes */
typedef enum
{
   NO_ACCESS,
   RW,
   READ,
} mmu_access_Type;

/* Memory Region definition */
typedef struct RegionStruct {
    mmu_region_size_Type rg_t;
    mmu_memory_Type mem_t;
    uint8_t domain;
    mmu_cacheability_Type inner_norm_t;
    mmu_cacheability_Type outer_norm_t;
    mmu_ecc_check_Type e_t;
    mmu_execute_Type xn_t;
    mmu_global_Type g_t;
    mmu_secure_Type sec_t;
    mmu_access_Type priv_t;
    mmu_access_Type user_t;
    mmu_shared_Type sh_t;

} mmu_region_attributes_Type;

/** \brief  Set section execution-never attribute

    The function sets section execution-never attribute

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]                xn  Section execution-never attribute : EXECUTE , NON_EXECUTE.

    \return          0  
 */
__STATIC_INLINE int __xn_section(uint32_t *descriptor_l1, mmu_execute_Type xn)
{
    *descriptor_l1 &= SECTION_XN_MASK;
    *descriptor_l1 |= ((xn & 0x1) << SECTION_XN_SHIFT);
    return 0;
}

/** \brief  Set section domain

    The function sets section domain

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]            domain  Section domain

    \return          0  
 */
__STATIC_INLINE int __domain_section(uint32_t *descriptor_l1, uint8_t domain)
{
    *descriptor_l1 &= SECTION_DOMAIN_MASK;
    *descriptor_l1 |= ((domain & 0xF) << SECTION_DOMAIN_SHIFT);
    return 0;
}

/** \brief  Set section parity check

    The function sets section parity check

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]              p_bit Parity check: ECC_DISABLED, ECC_ENABLED

    \return          0  
 */
__STATIC_INLINE int __p_section(uint32_t *descriptor_l1, mmu_ecc_check_Type p_bit)
{
    *descriptor_l1 &= SECTION_P_MASK;
    *descriptor_l1 |= ((p_bit & 0x1) << SECTION_P_SHIFT);
    return 0;
}

/** \brief  Set section access privileges

    The function sets section access privileges

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]              user  User Level Access: NO_ACCESS, RW, READ
    \param [in]              priv  Privilege Level Access: NO_ACCESS, RW, READ
    \param [in]               afe  Access flag enable

    \return          0  
 */
__STATIC_INLINE int __ap_section(uint32_t *descriptor_l1, mmu_access_Type user, mmu_access_Type priv,  uint32_t afe)
{
    uint32_t ap = 0;

    if (afe == 0) { //full access
        if ((priv == NO_ACCESS) && (user == NO_ACCESS)) { ap = 0x0; }
        else if ((priv == RW) && (user == NO_ACCESS))   { ap = 0x1; }
        else if ((priv == RW) && (user == READ))        { ap = 0x2; }
        else if ((priv == RW) && (user == RW))          { ap = 0x3; }
        else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
        else if ((priv == READ) && (user == READ))      { ap = 0x7; }
    }

    else { //Simplified access
        if ((priv == RW) && (user == NO_ACCESS))        { ap = 0x1; }
        else if ((priv == RW) && (user == RW))          { ap = 0x3; }
        else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
        else if ((priv == READ) && (user == READ))      { ap = 0x7; }
    }

    *descriptor_l1 &= SECTION_AP_MASK;
    *descriptor_l1 |= (ap & 0x3) << SECTION_AP_SHIFT;
    *descriptor_l1 |= ((ap & 0x4)>>2) << SECTION_AP2_SHIFT;

    return 0;
}

/** \brief  Set section shareability

    The function sets section shareability

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]             s_bit  Section shareability: NON_SHARED, SHARED

    \return          0  
 */
__STATIC_INLINE int __shared_section(uint32_t *descriptor_l1, mmu_shared_Type s_bit)
{
    *descriptor_l1 &= SECTION_S_MASK;
    *descriptor_l1 |= ((s_bit & 0x1) << SECTION_S_SHIFT);
    return 0;
}

/** \brief  Set section Global attribute

    The function sets section Global attribute

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]             g_bit  Section attribute: GLOBAL, NON_GLOBAL

    \return          0  
 */
__STATIC_INLINE int __global_section(uint32_t *descriptor_l1, mmu_global_Type g_bit)
{
    *descriptor_l1 &= SECTION_NG_MASK;
    *descriptor_l1 |= ((g_bit & 0x1) << SECTION_NG_SHIFT);
    return 0;
}

/** \brief  Set section Security attribute

    The function sets section Global attribute

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]             s_bit  Section Security attribute: SECURE, NON_SECURE

    \return          0  
 */
__STATIC_INLINE int __secure_section(uint32_t *descriptor_l1, mmu_secure_Type s_bit)
{
    *descriptor_l1 &= SECTION_NS_MASK;
    *descriptor_l1 |= ((s_bit & 0x1) << SECTION_NS_SHIFT);
    return 0;
}

/* Page 4k or 64k */
/** \brief  Set 4k/64k page execution-never attribute

    The function sets 4k/64k page execution-never attribute

    \param [out]    descriptor_l2  L2 descriptor.
    \param [in]                xn  Page execution-never attribute : EXECUTE , NON_EXECUTE.
    \param [in]              page  Page size: PAGE_4k, PAGE_64k,
   
    \return          0  
 */
__STATIC_INLINE int __xn_page(uint32_t *descriptor_l2, mmu_execute_Type xn, mmu_region_size_Type page)
{
    if (page == PAGE_4k)
    {
        *descriptor_l2 &= PAGE_XN_4K_MASK;
        *descriptor_l2 |= ((xn & 0x1) << PAGE_XN_4K_SHIFT);
    }
    else
    {
        *descriptor_l2 &= PAGE_XN_64K_MASK;
        *descriptor_l2 |= ((xn & 0x1) << PAGE_XN_64K_SHIFT);
    }
    return 0;
}

/** \brief  Set 4k/64k page domain

    The function sets 4k/64k page domain

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]            domain  Page domain

    \return          0  
 */
__STATIC_INLINE int __domain_page(uint32_t *descriptor_l1, uint8_t domain)
{
    *descriptor_l1 &= PAGE_DOMAIN_MASK;
    *descriptor_l1 |= ((domain & 0xf) << PAGE_DOMAIN_SHIFT);
    return 0;
}

/** \brief  Set 4k/64k page parity check

    The function sets 4k/64k page parity check

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]              p_bit Parity check: ECC_DISABLED, ECC_ENABLED

    \return          0  
 */
__STATIC_INLINE int __p_page(uint32_t *descriptor_l1, mmu_ecc_check_Type p_bit)
{
    *descriptor_l1 &= SECTION_P_MASK;
    *descriptor_l1 |= ((p_bit & 0x1) << SECTION_P_SHIFT);
    return 0;
}

/** \brief  Set 4k/64k page access privileges

    The function sets 4k/64k page access privileges

    \param [out]    descriptor_l2  L2 descriptor.
    \param [in]              user  User Level Access: NO_ACCESS, RW, READ
    \param [in]              priv  Privilege Level Access: NO_ACCESS, RW, READ
    \param [in]               afe  Access flag enable

    \return          0  
 */
__STATIC_INLINE int __ap_page(uint32_t *descriptor_l2, mmu_access_Type user, mmu_access_Type priv,  uint32_t afe)
{
    uint32_t ap = 0;

    if (afe == 0) { //full access
        if ((priv == NO_ACCESS) && (user == NO_ACCESS)) { ap = 0x0; }
        else if ((priv == RW) && (user == NO_ACCESS))   { ap = 0x1; }
        else if ((priv == RW) && (user == READ))        { ap = 0x2; }
        else if ((priv == RW) && (user == RW))          { ap = 0x3; }
        else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
        else if ((priv == READ) && (user == READ))      { ap = 0x6; }
    }

    else { //Simplified access
        if ((priv == RW) && (user == NO_ACCESS))        { ap = 0x1; }
        else if ((priv == RW) && (user == RW))          { ap = 0x3; }
        else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
        else if ((priv == READ) && (user == READ))      { ap = 0x7; }
    }

    *descriptor_l2 &= PAGE_AP_MASK;
    *descriptor_l2 |= (ap & 0x3) << PAGE_AP_SHIFT;
    *descriptor_l2 |= ((ap & 0x4)>>2) << PAGE_AP2_SHIFT;

    return 0;
}

/** \brief  Set 4k/64k page shareability

    The function sets 4k/64k page shareability

    \param [out]    descriptor_l2  L2 descriptor.
    \param [in]             s_bit  4k/64k page shareability: NON_SHARED, SHARED

    \return          0  
 */
__STATIC_INLINE int __shared_page(uint32_t *descriptor_l2, mmu_shared_Type s_bit)
{
    *descriptor_l2 &= PAGE_S_MASK;
    *descriptor_l2 |= ((s_bit & 0x1) << PAGE_S_SHIFT);
    return 0;
}

/** \brief  Set 4k/64k page Global attribute

    The function sets 4k/64k page Global attribute

    \param [out]    descriptor_l2  L2 descriptor.
    \param [in]             g_bit  4k/64k page attribute: GLOBAL, NON_GLOBAL

    \return          0  
 */
__STATIC_INLINE int __global_page(uint32_t *descriptor_l2, mmu_global_Type g_bit)
{
    *descriptor_l2 &= PAGE_NG_MASK;
    *descriptor_l2 |= ((g_bit & 0x1) << PAGE_NG_SHIFT);
    return 0;
}

/** \brief  Set 4k/64k page Security attribute

    The function sets 4k/64k page Global attribute

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]             s_bit  4k/64k page Security attribute: SECURE, NON_SECURE

    \return          0  
 */
__STATIC_INLINE int __secure_page(uint32_t *descriptor_l1, mmu_secure_Type s_bit)
{
    *descriptor_l1 &= PAGE_NS_MASK;
    *descriptor_l1 |= ((s_bit & 0x1) << PAGE_NS_SHIFT);
    return 0;
}


/** \brief  Set Section memory attributes

    The function sets section memory attributes

    \param [out]    descriptor_l1  L1 descriptor.
    \param [in]               mem  Section memory type: NORMAL, DEVICE, SHARED_DEVICE, NON_SHARED_DEVICE, STRONGLY_ORDERED
    \param [in]             outer  Outer cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
    \param [in]             inner  Inner cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,

    \return          0  
 */
__STATIC_INLINE int __memory_section(uint32_t *descriptor_l1, mmu_memory_Type mem, mmu_cacheability_Type outer, mmu_cacheability_Type inner)
{
    *descriptor_l1 &= SECTION_TEXCB_MASK;

    if (STRONGLY_ORDERED == mem)
    {
        return 0;
    }
    else if (SHARED_DEVICE == mem)
    {
        *descriptor_l1 |= (1 << SECTION_B_SHIFT);
    }
    else if (NON_SHARED_DEVICE == mem)
    {
        *descriptor_l1 |= (1 << SECTION_TEX1_SHIFT);
    }
    else if (NORMAL == mem)
    {
           *descriptor_l1 |= 1 << SECTION_TEX2_SHIFT;
           switch(inner)
           {
            case NON_CACHEABLE:
            break;
            case WB_WA:
                *descriptor_l1 |= (1 << SECTION_B_SHIFT);
                break;
            case WT:
                *descriptor_l1 |= 1 << SECTION_C_SHIFT;
                break;
            case WB_NO_WA:
                *descriptor_l1 |= (1 << SECTION_B_SHIFT) | (1 << SECTION_C_SHIFT);
                break;
        }
        switch(outer)
        {
            case NON_CACHEABLE:
             break;
            case WB_WA:
                *descriptor_l1 |= (1 << SECTION_TEX0_SHIFT);
                break;
            case WT:
                *descriptor_l1 |= 1 << SECTION_TEX1_SHIFT;
                break;
            case WB_NO_WA:
                *descriptor_l1 |= (1 << SECTION_TEX0_SHIFT) | (1 << SECTION_TEX0_SHIFT);
                break;
        }
    }

    return 0;
}

/** \brief  Set 4k/64k page memory attributes

    The function sets 4k/64k page memory attributes

    \param [out]    descriptor_l2  L2 descriptor.
    \param [in]               mem  4k/64k page memory type: NORMAL, DEVICE, SHARED_DEVICE, NON_SHARED_DEVICE, STRONGLY_ORDERED
    \param [in]             outer  Outer cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
    \param [in]             inner  Inner cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,

    \return          0  
 */
__STATIC_INLINE int __memory_page(uint32_t *descriptor_l2, mmu_memory_Type mem, mmu_cacheability_Type outer, mmu_cacheability_Type inner, mmu_region_size_Type page)
{
    *descriptor_l2 &= PAGE_4K_TEXCB_MASK;

    if (page == PAGE_64k)
    {
        //same as section
        __memory_section(descriptor_l2, mem, outer, inner);
    }
    else
    {
        if (STRONGLY_ORDERED == mem)
        {
            return 0;
        }
        else if (SHARED_DEVICE == mem)
        {
            *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT);
        }
        else if (NON_SHARED_DEVICE == mem)
        {
             *descriptor_l2 |= (1 << PAGE_4K_TEX1_SHIFT);
        }
        else if (NORMAL == mem)
        {
            *descriptor_l2 |= 1 << PAGE_4K_TEX2_SHIFT;
            switch(inner)
            {
                case NON_CACHEABLE:
                break;
                case WB_WA:
                     *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT);
                     break;
                case WT:
                    *descriptor_l2 |= 1 << PAGE_4K_C_SHIFT;
                     break;
                case WB_NO_WA:
                    *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT) | (1 << PAGE_4K_C_SHIFT);
                    break;
            }
            switch(outer)
            {
                case NON_CACHEABLE:
                break;
                case WB_WA:
                      *descriptor_l2 |= (1 << PAGE_4K_TEX0_SHIFT);
                    break;
                case WT:
                     *descriptor_l2 |= 1 << PAGE_4K_TEX1_SHIFT;
                    break;
                case WB_NO_WA:
                    *descriptor_l2 |= (1 << PAGE_4K_TEX0_SHIFT) | (1 << PAGE_4K_TEX0_SHIFT);
                    break;
            }
        }
    }

    return 0;
}

/** \brief  Create a L1 section descriptor

    The function creates a section descriptor.
    
    Assumptions:
    - 16MB super sections not supported
    - TEX remap disabled, so memory type and attributes are described directly by bits in the descriptor
    - Functions always return 0

    \param [out]       descriptor  L1 descriptor
    \param [out]      descriptor2  L2 descriptor
    \param [in]               reg  Section attributes

    \return          0  
 */
__STATIC_INLINE int __get_section_descriptor(uint32_t *descriptor, mmu_region_attributes_Type reg)
{
    *descriptor  = 0;

   __memory_section(descriptor, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t);
   __xn_section(descriptor,reg.xn_t);
   __domain_section(descriptor, reg.domain);
   __p_section(descriptor, reg.e_t);
   __ap_section(descriptor, reg.priv_t, reg.user_t, 1);
   __shared_section(descriptor,reg.sh_t);
   __global_section(descriptor,reg.g_t);
   __secure_section(descriptor,reg.sec_t);
   *descriptor &= SECTION_MASK;
   *descriptor |= SECTION_DESCRIPTOR;

   return 0;

}


/** \brief  Create a L1 and L2 4k/64k page descriptor

    The function creates a 4k/64k page descriptor.
    Assumptions:
    - TEX remap disabled, so memory type and attributes are described directly by bits in the descriptor
    - Functions always return 0

    \param [out]       descriptor  L1 descriptor
    \param [out]      descriptor2  L2 descriptor
    \param [in]               reg  4k/64k page attributes

    \return          0  
 */
__STATIC_INLINE int __get_page_descriptor(uint32_t *descriptor, uint32_t *descriptor2, mmu_region_attributes_Type reg)
{
    *descriptor  = 0;
    *descriptor2 = 0;

    switch (reg.rg_t)
    {
        case PAGE_4k:
            __memory_page(descriptor2, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t, PAGE_4k);
            __xn_page(descriptor2, reg.xn_t, PAGE_4k);
            __domain_page(descriptor, reg.domain);
            __p_page(descriptor, reg.e_t);
            __ap_page(descriptor2, reg.priv_t, reg.user_t, 1);
            __shared_page(descriptor2,reg.sh_t);
            __global_page(descriptor2,reg.g_t);
            __secure_page(descriptor,reg.sec_t);
            *descriptor &= PAGE_L1_MASK;
            *descriptor |= PAGE_L1_DESCRIPTOR;
            *descriptor2 &= PAGE_L2_4K_MASK;
            *descriptor2 |= PAGE_L2_4K_DESC;
            break;

        case PAGE_64k:
            __memory_page(descriptor2, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t, PAGE_64k);
            __xn_page(descriptor2, reg.xn_t, PAGE_64k);
            __domain_page(descriptor, reg.domain);
            __p_page(descriptor, reg.e_t);
            __ap_page(descriptor2, reg.priv_t, reg.user_t, 1);
            __shared_page(descriptor2,reg.sh_t);
            __global_page(descriptor2,reg.g_t);
            __secure_page(descriptor,reg.sec_t);
            *descriptor &= PAGE_L1_MASK;
            *descriptor |= PAGE_L1_DESCRIPTOR;
            *descriptor2 &= PAGE_L2_64K_MASK;
            *descriptor2 |= PAGE_L2_64K_DESC;
            break;

        case SECTION:
            //error
            break;    

    }

   return 0;

}

/** \brief  Create a 1MB Section

    \param [in]               ttb  Translation table base address
    \param [in]      base_address  Section base address
    \param [in]             count  Number of sections to create
    \param [in]     descriptor_l1  L1 descriptor (region attributes) 

 */
__STATIC_INLINE void __TTSection(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1)
{
    uint32_t offset;
    uint32_t entry;
    uint32_t i;

    offset = base_address >> 20;
    entry  = (base_address & 0xFFF00000) | descriptor_l1;

    //4 bytes aligned
    ttb = ttb + offset;

    for (i = 0; i < count; i++ )
    {
        //4 bytes aligned
       *ttb++ = entry;
       entry += OFFSET_1M;
    }
}

/** \brief  Create a 4k page entry

    \param [in]               ttb  L1 table base address
    \param [in]      base_address  4k base address
    \param [in]             count  Number of 4k pages to create
    \param [in]     descriptor_l1  L1 descriptor (region attributes) 
    \param [in]            ttb_l2  L2 table base address
    \param [in]     descriptor_l2  L2 descriptor (region attributes) 

 */
__STATIC_INLINE void __TTPage_4k(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1, uint32_t *ttb_l2, uint32_t descriptor_l2 )
{

    uint32_t offset, offset2;
    uint32_t entry, entry2;
    uint32_t i;


    offset = base_address >> 20;
    entry  = ((int)ttb_l2 & 0xFFFFFC00) | descriptor_l1;

    //4 bytes aligned
    ttb += offset;
    //create l1_entry
    *ttb = entry;

    offset2 = (base_address & 0xff000) >> 12;
    ttb_l2 += offset2;
    entry2 = (base_address & 0xFFFFF000) | descriptor_l2;
    for (i = 0; i < count; i++ )
    {
        //4 bytes aligned
       *ttb_l2++ = entry2;
       entry2 += OFFSET_4K;
    }
}

/** \brief  Create a 64k page entry

    \param [in]               ttb  L1 table base address
    \param [in]      base_address  64k base address
    \param [in]             count  Number of 64k pages to create
    \param [in]     descriptor_l1  L1 descriptor (region attributes) 
    \param [in]            ttb_l2  L2 table base address
    \param [in]     descriptor_l2  L2 descriptor (region attributes) 

 */
__STATIC_INLINE void __TTPage_64k(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1, uint32_t *ttb_l2, uint32_t descriptor_l2 )
{
    uint32_t offset, offset2;
    uint32_t entry, entry2;
    uint32_t i,j;


    offset = base_address >> 20;
    entry  = ((int)ttb_l2 & 0xFFFFFC00) | descriptor_l1;

    //4 bytes aligned
    ttb += offset;
    //create l1_entry
    *ttb = entry;

    offset2 = (base_address & 0xff000) >> 12;
    ttb_l2 += offset2;
    entry2 = (base_address & 0xFFFF0000) | descriptor_l2;
    for (i = 0; i < count; i++ )
    {
        //create 16 entries
        for (j = 0; j < 16; j++)
            //4 bytes aligned
            *ttb_l2++ = entry2;
        entry2 += OFFSET_64K;
    }
}

/*@} end of MMU_Functions */
#endif

#ifdef __cplusplus
}
#endif