]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/lib/cache-cp15.c
TX6 Release 2013-04-22
[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
24 #include <common.h>
25 #include <asm/system.h>
26
27 #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 void __arm_init_before_mmu(void)
32 {
33 }
34 void arm_init_before_mmu(void)
35         __attribute__((weak, alias("__arm_init_before_mmu")));
36
37 void set_section_dcache(int section, enum dcache_option option)
38 {
39         u32 *page_table = (u32 *)gd->arch.tlb_addr;
40         u32 value;
41
42         value = (section << MMU_SECTION_SHIFT) | (3 << 10);
43         value |= option;
44         page_table[section] = value;
45 }
46
47 void __mmu_page_table_flush(unsigned long start, unsigned long stop)
48 {
49         debug("%s: Warning: not implemented\n", __func__);
50 }
51
52 void mmu_page_table_flush(unsigned long start, unsigned long stop)
53         __attribute__((weak, alias("__mmu_page_table_flush")));
54
55 void mmu_set_region_dcache_behaviour(u32 start, int size,
56                                      enum dcache_option option)
57 {
58         u32 *page_table = (u32 *)gd->arch.tlb_addr;
59         u32 upto, end;
60
61         end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
62         start = start >> MMU_SECTION_SHIFT;
63         debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
64               option);
65         for (upto = start; upto < end; upto++)
66                 set_section_dcache(upto, option);
67         mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
68 }
69
70 static inline void dram_bank_mmu_setup(int bank)
71 {
72         bd_t *bd = gd->bd;
73         int     i;
74
75         debug("%s: bank: %d\n", __func__, bank);
76         for (i = bd->bi_dram[bank].start >> 20;
77              i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
78              i++) {
79 #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
80                 set_section_dcache(i, DCACHE_WRITETHROUGH);
81 #else
82                 set_section_dcache(i, DCACHE_WRITEBACK);
83 #endif
84         }
85 }
86
87 /* to activate the MMU we need to set up virtual memory: use 1M areas */
88 static inline void mmu_setup(void)
89 {
90         u32 *page_table = (u32 *)gd->arch.tlb_addr;
91         int i;
92         u32 reg;
93
94         arm_init_before_mmu();
95         /* Set up an identity-mapping for all 4GB, rw for everyone */
96         for (i = 0; i < 4096; i++)
97                 set_section_dcache(i, DCACHE_OFF);
98
99         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
100                 dram_bank_mmu_setup(i);
101         }
102
103         asm volatile(
104                 /* Copy the page table address to cp15 */
105                 "mcr p15, 0, %0, c2, c0, 0\n"
106                 /* Set the access control to all-supervisor */
107                 "mcr p15, 0, %1, c3, c0, 0\n"
108                 :
109                 : "r"(page_table), "r"(~0)
110                 );
111         /* and enable the mmu */
112         reg = get_cr(); /* get control reg. */
113         set_cr(reg | CR_M);
114 }
115
116 static int mmu_enabled(void)
117 {
118         return get_cr() & CR_M;
119 }
120
121 /* cache_bit must be either CR_I or CR_C */
122 static void cache_enable(uint32_t cache_bit)
123 {
124         uint32_t reg;
125
126         /* The data cache is not active unless the mmu is enabled too */
127         if ((cache_bit == CR_C) && !mmu_enabled())
128                 mmu_setup();
129         reg = get_cr(); /* get control reg. */
130         set_cr(reg | cache_bit);
131 }
132
133 /* cache_bit must be either CR_I or CR_C */
134 static void cache_disable(uint32_t cache_bit)
135 {
136         uint32_t reg;
137
138         reg = get_cr();
139
140         if (cache_bit == CR_C) {
141                 /* if cache isn;t enabled no need to disable */
142                 if ((reg & CR_C) != CR_C)
143                         return;
144                 /* if disabling data cache, disable mmu too */
145                 cache_bit |= CR_M;
146         }
147         reg = get_cr();
148         if (cache_bit == (CR_C | CR_M))
149                 flush_dcache_all();
150         set_cr(reg & ~cache_bit);
151 }
152 #endif
153
154 #ifdef CONFIG_SYS_ICACHE_OFF
155 void icache_enable (void)
156 {
157         return;
158 }
159
160 void icache_disable (void)
161 {
162         return;
163 }
164
165 int icache_status (void)
166 {
167         return 0;                                       /* always off */
168 }
169 #else
170 void icache_enable(void)
171 {
172         cache_enable(CR_I);
173 }
174
175 void icache_disable(void)
176 {
177         cache_disable(CR_I);
178 }
179
180 int icache_status(void)
181 {
182         return (get_cr() & CR_I) != 0;
183 }
184 #endif
185
186 #ifdef CONFIG_SYS_DCACHE_OFF
187 void dcache_enable (void)
188 {
189         return;
190 }
191
192 void dcache_disable (void)
193 {
194         return;
195 }
196
197 int dcache_status (void)
198 {
199         return 0;                                       /* always off */
200 }
201 #else
202 void dcache_enable(void)
203 {
204         cache_enable(CR_C);
205 }
206
207 void dcache_disable(void)
208 {
209         cache_disable(CR_C);
210 }
211
212 int dcache_status(void)
213 {
214         return (get_cr() & CR_C) != 0;
215 }
216 #endif