1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
10 #include <asm/cacheops.h>
11 #ifdef CONFIG_MIPS_L2_CACHE
15 #include <asm/mipsregs.h>
16 #include <asm/system.h>
17 #include <linux/bug.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static void probe_l2(void)
23 #ifdef CONFIG_MIPS_L2_CACHE
24 unsigned long conf2, sl;
27 if (!(read_c0_config1() & MIPS_CONF_M))
30 conf2 = read_c0_config2();
32 if (__mips_isa_rev >= 6) {
33 l2c = conf2 & MIPS_CONF_M;
35 l2c = read_c0_config3() & MIPS_CONF_M;
37 l2c = read_c0_config4() & MIPS_CONF_M;
39 l2c = read_c0_config5() & MIPS_CONF5_L2C;
42 if (l2c && config_enabled(CONFIG_MIPS_CM)) {
43 gd->arch.l2_line_size = mips_cm_l2_line_size();
45 /* We don't know how to retrieve L2 config on this system */
48 sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF;
49 gd->arch.l2_line_size = sl ? (2 << sl) : 0;
54 void mips_cache_probe(void)
56 #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
57 unsigned long conf1, il, dl;
59 conf1 = read_c0_config1();
61 il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
62 dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
64 gd->arch.l1i_line_size = il ? (2 << il) : 0;
65 gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
70 static inline unsigned long icache_line_size(void)
72 #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
73 return gd->arch.l1i_line_size;
75 return CONFIG_SYS_ICACHE_LINE_SIZE;
79 static inline unsigned long dcache_line_size(void)
81 #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
82 return gd->arch.l1d_line_size;
84 return CONFIG_SYS_DCACHE_LINE_SIZE;
88 static inline unsigned long scache_line_size(void)
90 #ifdef CONFIG_MIPS_L2_CACHE
91 return gd->arch.l2_line_size;
93 return CONFIG_SYS_SCACHE_LINE_SIZE;
97 #define cache_loop(start, end, lsize, ops...) do { \
98 const void *addr = (const void *)(start & ~(lsize - 1)); \
99 const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
100 const unsigned int cache_ops[] = { ops }; \
106 for (; addr <= aend; addr += lsize) { \
107 for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
108 mips_cache(cache_ops[i], addr); \
112 void flush_cache(ulong start_addr, ulong size)
114 unsigned long ilsize = icache_line_size();
115 unsigned long dlsize = dcache_line_size();
116 unsigned long slsize = scache_line_size();
118 /* aend will be miscalculated when size is zero, so we return here */
122 if ((ilsize == dlsize) && !slsize) {
123 /* flush I-cache & D-cache simultaneously */
124 cache_loop(start_addr, start_addr + size, ilsize,
125 HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
130 cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
133 cache_loop(start_addr, start_addr + size, slsize, HIT_WRITEBACK_INV_SD);
136 cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
139 /* ensure cache ops complete before any further memory accesses */
142 /* ensure the pipeline doesn't contain now-invalid instructions */
143 instruction_hazard_barrier();
146 void __weak flush_dcache_range(ulong start_addr, ulong stop)
148 unsigned long lsize = dcache_line_size();
149 unsigned long slsize = scache_line_size();
151 /* aend will be miscalculated when size is zero, so we return here */
152 if (start_addr == stop)
155 cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
158 cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
160 /* ensure cache ops complete before any further memory accesses */
164 void invalidate_dcache_range(ulong start_addr, ulong stop)
166 unsigned long lsize = dcache_line_size();
167 unsigned long slsize = scache_line_size();
169 /* aend will be miscalculated when size is zero, so we return here */
170 if (start_addr == stop)
173 /* invalidate L2 cache */
174 cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
176 cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
178 /* ensure cache ops complete before any further memory accesses */
182 int dcache_status(void)
184 unsigned int cca = read_c0_config() & CONF_CM_CMASK;
185 return cca != CONF_CM_UNCACHED;
188 void dcache_enable(void)
190 puts("Not supported!\n");
193 void dcache_disable(void)
195 /* change CCA to uncached */
196 change_c0_config(CONF_CM_CMASK, CONF_CM_UNCACHED);
198 /* ensure the pipeline doesn't contain now-invalid instructions */
199 instruction_hazard_barrier();