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