1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Andes Technology Corporation
4 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
5 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
9 #if (!defined(CONFIG_SYS_ICACHE_OFF) || !defined(CONFIG_SYS_DCACHE_OFF))
10 static inline unsigned long CACHE_SET(unsigned char cache)
13 return 64 << ((GET_ICM_CFG() & ICM_CFG_MSK_ISET) \
16 return 64 << ((GET_DCM_CFG() & DCM_CFG_MSK_DSET) \
20 static inline unsigned long CACHE_WAY(unsigned char cache)
23 return 1 + ((GET_ICM_CFG() & ICM_CFG_MSK_IWAY) \
26 return 1 + ((GET_DCM_CFG() & DCM_CFG_MSK_DWAY) \
30 static inline unsigned long CACHE_LINE_SIZE(enum cache_t cache)
33 return 8 << (((GET_ICM_CFG() & ICM_CFG_MSK_ISZ) \
34 >> ICM_CFG_OFF_ISZ) - 1);
36 return 8 << (((GET_DCM_CFG() & DCM_CFG_MSK_DSZ) \
37 >> DCM_CFG_OFF_DSZ) - 1);
41 #ifndef CONFIG_SYS_ICACHE_OFF
42 void invalidate_icache_all(void)
44 unsigned long end, line_size;
45 line_size = CACHE_LINE_SIZE(ICACHE);
46 end = line_size * CACHE_WAY(ICACHE) * CACHE_SET(ICACHE);
49 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL" : : "r" (end));
52 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL" : : "r" (end));
55 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL" : : "r" (end));
57 __asm__ volatile ("\n\tcctl %0, L1I_IX_INVAL" : : "r" (end));
61 void invalidate_icache_range(unsigned long start, unsigned long end)
63 unsigned long line_size;
65 line_size = CACHE_LINE_SIZE(ICACHE);
68 "\n\tcctl %0, L1I_VA_INVAL"
76 void icache_enable(void)
80 "ori $p0, $p0, 0x01\n\t"
86 void icache_disable(void)
91 "and $p0, $p0, $p1\n\t"
97 int icache_status(void)
103 "andi %0, $p0, 0x01\n\t"
113 void invalidate_icache_all(void)
117 void invalidate_icache_range(unsigned long start, unsigned long end)
121 void icache_enable(void)
125 void icache_disable(void)
129 int icache_status(void)
136 #ifndef CONFIG_SYS_DCACHE_OFF
137 void dcache_wbinval_all(void)
139 unsigned long end, line_size;
140 line_size = CACHE_LINE_SIZE(DCACHE);
141 end = line_size * CACHE_WAY(DCACHE) * CACHE_SET(DCACHE);
144 __asm__ volatile ("\n\tcctl %0, L1D_IX_WB" : : "r" (end));
145 __asm__ volatile ("\n\tcctl %0, L1D_IX_INVAL" : : "r" (end));
147 __asm__ volatile ("\n\tcctl %0, L1D_IX_WB" : : "r" (end));
148 __asm__ volatile ("\n\tcctl %0, L1D_IX_INVAL" : : "r" (end));
150 __asm__ volatile ("\n\tcctl %0, L1D_IX_WB" : : "r" (end));
151 __asm__ volatile ("\n\tcctl %0, L1D_IX_INVAL" : : "r" (end));
153 __asm__ volatile ("\n\tcctl %0, L1D_IX_WB" : : "r" (end));
154 __asm__ volatile ("\n\tcctl %0, L1D_IX_INVAL" : : "r" (end));
159 void flush_dcache_range(unsigned long start, unsigned long end)
161 unsigned long line_size;
163 line_size = CACHE_LINE_SIZE(DCACHE);
165 while (end > start) {
167 "\n\tcctl %0, L1D_VA_WB"
168 "\n\tcctl %0, L1D_VA_INVAL" : : "r" (start)
174 void invalidate_dcache_range(unsigned long start, unsigned long end)
176 unsigned long line_size;
178 line_size = CACHE_LINE_SIZE(DCACHE);
179 while (end > start) {
181 "\n\tcctl %0, L1D_VA_INVAL" : : "r"(start)
187 void dcache_enable(void)
191 "ori $p0, $p0, 0x02\n\t"
197 void dcache_disable(void)
202 "and $p0, $p0, $p1\n\t"
208 int dcache_status(void)
213 "andi %0, $p0, 0x02\n\t"
222 void dcache_wbinval_all(void)
226 void flush_dcache_range(unsigned long start, unsigned long end)
230 void invalidate_dcache_range(unsigned long start, unsigned long end)
234 void dcache_enable(void)
238 void dcache_disable(void)
242 int dcache_status(void)
250 void flush_dcache_all(void)
252 dcache_wbinval_all();
255 void cache_flush(void)
258 invalidate_icache_all();
262 void flush_cache(unsigned long addr, unsigned long size)
264 flush_dcache_range(addr, addr + size);
265 invalidate_icache_range(addr, addr + size);