3 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/cacheops.h>
11 #include <asm/mipsregs.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 static void probe_l2(void)
17 #ifdef CONFIG_MIPS_L2_CACHE
18 unsigned long conf2, sl;
21 if (!(read_c0_config1() & MIPS_CONF_M))
24 conf2 = read_c0_config2();
26 if (__mips_isa_rev >= 6) {
27 l2c = conf2 & MIPS_CONF_M;
29 l2c = read_c0_config3() & MIPS_CONF_M;
31 l2c = read_c0_config4() & MIPS_CONF_M;
33 l2c = read_c0_config5() & MIPS_CONF5_L2C;
36 if (l2c && config_enabled(CONFIG_MIPS_CM)) {
37 gd->arch.l2_line_size = mips_cm_l2_line_size();
39 /* We don't know how to retrieve L2 config on this system */
42 sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF;
43 gd->arch.l2_line_size = sl ? (2 << sl) : 0;
48 void mips_cache_probe(void)
50 #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
51 unsigned long conf1, il, dl;
53 conf1 = read_c0_config1();
55 il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
56 dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
58 gd->arch.l1i_line_size = il ? (2 << il) : 0;
59 gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
64 static inline unsigned long icache_line_size(void)
66 #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
67 return gd->arch.l1i_line_size;
69 return CONFIG_SYS_ICACHE_LINE_SIZE;
73 static inline unsigned long dcache_line_size(void)
75 #ifdef CONFIG_SYS_CACHE_SIZE_AUTO
76 return gd->arch.l1d_line_size;
78 return CONFIG_SYS_DCACHE_LINE_SIZE;
82 static inline unsigned long scache_line_size(void)
84 #ifdef CONFIG_MIPS_L2_CACHE
85 return gd->arch.l2_line_size;
91 #define cache_loop(start, end, lsize, ops...) do { \
92 const void *addr = (const void *)(start & ~(lsize - 1)); \
93 const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
94 const unsigned int cache_ops[] = { ops }; \
97 for (; addr <= aend; addr += lsize) { \
98 for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
99 mips_cache(cache_ops[i], addr); \
103 void flush_cache(ulong start_addr, ulong size)
105 unsigned long ilsize = icache_line_size();
106 unsigned long dlsize = dcache_line_size();
107 unsigned long slsize = scache_line_size();
109 /* aend will be miscalculated when size is zero, so we return here */
113 if ((ilsize == dlsize) && !slsize) {
114 /* flush I-cache & D-cache simultaneously */
115 cache_loop(start_addr, start_addr + size, ilsize,
116 HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
121 cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
125 cache_loop(start_addr, start_addr + size, slsize,
126 HIT_WRITEBACK_INV_SD);
129 cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
132 void flush_dcache_range(ulong start_addr, ulong stop)
134 unsigned long lsize = dcache_line_size();
135 unsigned long slsize = scache_line_size();
137 /* aend will be miscalculated when size is zero, so we return here */
138 if (start_addr == stop)
141 cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
145 cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
148 void invalidate_dcache_range(ulong start_addr, ulong stop)
150 unsigned long lsize = dcache_line_size();
151 unsigned long slsize = scache_line_size();
153 /* aend will be miscalculated when size is zero, so we return here */
154 if (start_addr == stop)
157 /* invalidate L2 cache */
159 cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
161 cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);