]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/lib/cache-cp15.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / arch / arm / lib / cache-cp15.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23 #include <common.h>
24 #include <asm/system.h>
25
26 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
27
28 #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
29 #define CACHE_SETUP     0x1a
30 #else
31 #define CACHE_SETUP     0x1e
32 #endif
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 void __arm_init_before_mmu(void)
37 {
38 }
39 void arm_init_before_mmu(void)
40         __attribute__((weak, alias("__arm_init_before_mmu")));
41
42 static inline void dram_bank_mmu_setup(int bank)
43 {
44         u32 *page_table = (u32 *)gd->tlb_addr;
45         bd_t *bd = gd->bd;
46         int     i;
47
48         debug("%s: bank: %d\n", __func__, bank);
49         for (i = bd->bi_dram[bank].start >> 20;
50              i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
51              i++) {
52                 page_table[i] = i << 20 | (3 << 10) | CACHE_SETUP;
53         }
54 }
55
56 /* to activate the MMU we need to set up virtual memory: use 1M areas */
57 static inline void mmu_setup(void)
58 {
59         u32 *page_table = (u32 *)gd->tlb_addr;
60         int i;
61         u32 reg;
62
63         arm_init_before_mmu();
64         /* Set up an identity-mapping for all 4GB, rw for everyone */
65         for (i = 0; i < 4096; i++)
66                 page_table[i] = i << 20 | (3 << 10) | 0x12;
67
68         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
69                 dram_bank_mmu_setup(i);
70         }
71
72         asm volatile(
73                 /* Copy the page table address to cp15 */
74                 "mcr p15, 0, %0, c2, c0, 0\n"
75                 /* Set the access control to all-supervisor */
76                 "mcr p15, 0, %1, c3, c0, 0\n"
77                 :
78                 : "r"(page_table), "r"(~0)
79                 );
80         /* and enable the mmu */
81         reg = get_cr(); /* get control reg. */
82         set_cr(reg | CR_M);
83 }
84
85 static int mmu_enabled(void)
86 {
87         return get_cr() & CR_M;
88 }
89
90 /* cache_bit must be either CR_I or CR_C */
91 static void cache_enable(uint32_t cache_bit)
92 {
93         uint32_t reg;
94
95         /* The data cache is not active unless the mmu is enabled too */
96         if ((cache_bit == CR_C) && !mmu_enabled())
97                 mmu_setup();
98         reg = get_cr(); /* get control reg. */
99         set_cr(reg | cache_bit);
100 }
101
102 /* cache_bit must be either CR_I or CR_C */
103 static void cache_disable(uint32_t cache_bit)
104 {
105         uint32_t reg;
106
107         if (cache_bit == CR_C) {
108                 /* if cache isn;t enabled no need to disable */
109                 reg = get_cr();
110                 if ((reg & CR_C) != CR_C)
111                         return;
112                 /* if disabling data cache, disable mmu too */
113                 cache_bit |= CR_M;
114                 flush_dcache_all();
115         }
116         reg = get_cr();
117         set_cr(reg & ~cache_bit);
118 }
119 #endif
120
121 #ifdef CONFIG_SYS_ICACHE_OFF
122 void icache_enable (void)
123 {
124         return;
125 }
126
127 void icache_disable (void)
128 {
129         return;
130 }
131
132 int icache_status (void)
133 {
134         return 0;                                       /* always off */
135 }
136 #else
137 void icache_enable(void)
138 {
139         cache_enable(CR_I);
140 }
141
142 void icache_disable(void)
143 {
144         cache_disable(CR_I);
145 }
146
147 int icache_status(void)
148 {
149         return (get_cr() & CR_I) != 0;
150 }
151 #endif
152
153 #ifdef CONFIG_SYS_DCACHE_OFF
154 void dcache_enable (void)
155 {
156         return;
157 }
158
159 void dcache_disable (void)
160 {
161         return;
162 }
163
164 int dcache_status (void)
165 {
166         return 0;                                       /* always off */
167 }
168 #else
169 void dcache_enable(void)
170 {
171         cache_enable(CR_C);
172 }
173
174 void dcache_disable(void)
175 {
176         cache_disable(CR_C);
177 }
178
179 int dcache_status(void)
180 {
181         return (get_cr() & CR_C) != 0;
182 }
183 #endif