summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/mmu-arm.c
blob: fff201cbfcdf1f3e824d4a4aa4ceac2005505130 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006,2007 by Greg White
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "cpu.h"
#include "mmu-arm.h"
#include "panic.h"

void __attribute__((naked)) ttb_init(void) {
    asm volatile
    (
        "mcr p15, 0, %[ttbB], c2, c0, 0 \n" /* Set the TTB base address */
        "mcr p15, 0, %[ffff], c3, c0, 0 \n" /* Set all domains to manager status */
        "bx  lr                         \n"
        : 
        :   [ttbB] "r" (TTB_BASE), 
            [ffff] "r" (0xFFFFFFFF)
    );
}

void __attribute__((naked)) map_section(unsigned int pa, unsigned int va, int mb, int flags) {
    asm volatile
    (
        /* pa &= (-1 << 20);   // align to 1MB */
        "mov  r0, r0, lsr #20   \n"
        "mov  r0, r0, lsl #20   \n"

        /* pa |= (flags | 0x412);
         * bit breakdown:
         *  10:     superuser - r/w, user - no access
         *  4:      should be "1"
         *  3,2:    Cache flags (flags (r3))
         *  1:      Section signature 
         */

        "orr  r0, r0, r3        \n"
        "orr  r0, r0, #0x410    \n"
        "orr  r0, r0, #0x2      \n"
        :
        :
    );

    register int *ttb_base asm ("r3") = TTB_BASE;   /* force in r3 */

    asm volatile
    (
        /* unsigned int* ttbPtr = TTB_BASE + (va >> 20);
         * sections are 1MB size 
         */

        "mov  r1, r1, lsr #20           \n"
        "add  r1, %[ttbB], r1, lsl #0x2 \n" 

        /* Add MB to pa, flags are already present in pa, but addition 
         *  should not effect them
         *
         * #define MB (1 << 20)
         * for( ; mb>0; mb--, pa += MB)
         * {
         *     *(ttbPtr++) = pa;
         * }
         * #undef MB
         */

        "cmp  r2, #0                    \n"
        "bxle lr                        \n"   
        "mov  r3, #0x0                  \n"
        "loop:                          \n"
        "str  r0, [r1], #4              \n"
        "add  r0, r0, #0x100000         \n"
        "add  r3, r3, #0x1                 \n"
        "cmp  r2, r3                    \n"
        "bne  loop                      \n"
        "bx   lr                        \n"
        : 
        :   [ttbB] "r" (ttb_base)   /* This /HAS/ to be in r3 */
    );
    (void) pa;
    (void) va;
    (void) mb;
    (void) flags;
}

void __attribute__((naked)) enable_mmu(void) {
    asm volatile(
        "mov r0, #0                 \n"
        "mcr p15, 0, r0, c8, c7, 0  \n" /* invalidate TLB */
        "mcr p15, 0, r0, c7, c7,0   \n" /* invalidate both icache and dcache */
        "mrc p15, 0, r0, c1, c0, 0  \n"
        "orr r0, r0, #1             \n" /* enable mmu bit, icache and dcache */
        "orr r0, r0, #1<<2          \n" /* enable dcache */
        "orr r0, r0, #1<<12         \n" /* enable icache */
        "mcr p15, 0, r0, c1, c0, 0  \n"
        "nop                        \n" 
        "nop                        \n"
        "nop                        \n"
        "nop                        \n"
        "bx  lr                     \n"
        : 
        : 
        : "r0"
    );
}

#if CONFIG_CPU == IMX31L
void __attribute__((naked)) invalidate_dcache_range(const void *base, unsigned int size)
{
    asm volatile(
        "add    r1, r1, r0             \n"
        "mov    r2, #0                 \n"
        "mcrr   p15, 0, r1, r0, c14    \n" /* Clean and invalidate dcache range */
        "mcr    p15, 0, r2, c7, c10, 4 \n" /* Data synchronization barrier */
        "bx     lr                     \n"
    );
    (void)base; (void)size;
}
#else
/* Invalidate DCache for this range  */
/* Will do write back */
void invalidate_dcache_range(const void *base, unsigned int size) {
    unsigned int addr = (((int) base) & ~31);    /* Align start to cache line*/
    unsigned int end = ((addr+size) & ~31)+64;  /* Align end to cache line, pad */
    asm volatile(
        "inv_start: \n"
        "mcr p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "add %0, %0, #32 \n"
        "cmp %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "mcrne p15, 0, %0, c7, c14, 1 \n" /* Clean and invalidate this line */
        "addne %0, %0, #32 \n"
        "cmpne %0, %1 \n"
        "bne inv_start \n"
        "mov %0, #0\n"
        "mcr p15,0,%0,c7,c10,4\n"    /* Drain write buffer */
        : : "r" (addr), "r" (end)
    );
}
#endif


#if CONFIG_CPU == IMX31L
void __attribute__((naked)) clean_dcache_range(const void *base, unsigned int size)
{
    asm volatile(
        "add    r1, r1, r0             \n"
        "mov    r2, #0                 \n"
        "mcrr   p15, 0, r1, r0, c12    \n" /* Clean dcache range */
        "mcr    p15, 0, r2, c7, c10, 4 \n" /* Data synchronization barrier */
        "bx     lr                     \n"
    );
    (void)base; (void)size;
}
#else
/* clean DCache for this range  */
/* forces DCache writeback for the specified range */
void clean_dcache_range(const void *base, unsigned int size) {
    unsigned int addr = (int) base;
    unsigned int end = addr+size+32;
    asm      volatile(
    "bic %0, %0, #31 \n"
"clean_start: \n"
    "mcr p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "add %0, %0, #32 \n"
    "cmp %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "mcrlo p15, 0, %0, c7, c10, 1 \n" /* Clean this line */
    "addlo %0, %0, #32 \n"
    "cmplo %0, %1 \n"
    "blo clean_start \n"
    "mov %0, #0\n"
    "mcr p15,0,%0,c7,c10,4 \n"    /* Drain write buffer */
        : : "r" (addr), "r" (end));
}
#endif

#if CONFIG_CPU == IMX31L
void __attribute__((naked)) dump_dcache_range(const void *base, unsigned int size)
{
    asm volatile(
        "add    r1, r1, r0         \n"
        "mcrr   p15, 0, r1, r0, c6 \n"
        "bx     lr                 \n"
    );
    (void)base; (void)size;
}
#else
/* Dump DCache for this range  */
/* Will *NOT* do write back */
void dump_dcache_range(const void *base, unsigned int size) {
    unsigned int addr = (int) base;
    unsigned int end = addr+size;
    asm volatile(
    "tst %0, #31 \n"                    /* Check to see if low five bits are set */
    "bic %0, %0, #31 \n"                /* Clear them */
    "mcrne p15, 0, %0, c7, c14, 1 \n"     /* Clean and invalidate this line, if those bits were set */
    "add %0, %0, #32 \n"                /* Move to the next cache line */
    "tst %1, #31 \n"                    /* Check last line for bits set */
    "bic %1, %1, #31 \n"                /* Clear those bits */
    "mcrne p15, 0, %1, c7, c14, 1 \n"     /* Clean and invalidate this line, if not cache aligned */
"dump_start: \n"
    "mcr p15, 0, %0, c7, c6, 1 \n"         /* Invalidate this line */
    "add %0, %0, #32 \n"                /* Next cache line */
    "cmp %0, %1 \n"
    "bne dump_start \n"
"dump_end: \n"
    "mcr p15,0,%0,c7,c10,4 \n"    /* Drain write buffer */
        : : "r" (addr), "r" (end));
}
#endif

#if CONFIG_CPU == IMX31L
void __attribute__((naked)) clean_dcache(void)
{
    asm volatile (
        /* Clean entire data cache */
        "mov    r0, #0                 \n"
        "mcr    p15, 0, r0, c7, c10, 0 \n"
        "bx     lr                     \n"
    );
}
#else
/* Cleans entire DCache */
void clean_dcache(void)
{
    unsigned int index, addr, low;

    for(index = 0; index <= 63; index++) 
    {
        for(low = 0;low <= 7; low++)
        {
            addr = (index << 26) | (low << 5);
            asm volatile
            (
                "mcr p15, 0, %[addr], c7, c10, 2 \n" /* Clean this entry by index */
                : 
                : [addr] "r" (addr)
            );
        }
    }
}
#endif