1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
4 * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
9 #include <asm/armv7m.h>
12 /* Cache maintenance operation registers */
14 #define V7M_CACHE_REG_ICIALLU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x00))
15 #define INVAL_ICACHE_POU 0
16 #define V7M_CACHE_REG_ICIMVALU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x08))
17 #define V7M_CACHE_REG_DCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x0C))
18 #define V7M_CACHE_REG_DCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x10))
19 #define V7M_CACHE_REG_DCCMVAU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x14))
20 #define V7M_CACHE_REG_DCCMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x18))
21 #define V7M_CACHE_REG_DCCSW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x1C))
22 #define V7M_CACHE_REG_DCCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x20))
23 #define V7M_CACHE_REG_DCCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x24))
27 /* armv7m processor feature registers */
29 #define V7M_PROC_REG_CLIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x00))
30 #define V7M_PROC_REG_CTR ((u32 *)(V7M_PROC_FTR_BASE + 0x04))
31 #define V7M_PROC_REG_CCSIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x08))
32 #define MASK_NUM_WAYS GENMASK(12, 3)
33 #define MASK_NUM_SETS GENMASK(27, 13)
34 #define CLINE_SIZE_MASK GENMASK(2, 0)
35 #define NUM_WAYS_SHIFT 3
36 #define NUM_SETS_SHIFT 13
37 #define V7M_PROC_REG_CSSELR ((u32 *)(V7M_PROC_FTR_BASE + 0x0C))
38 #define SEL_I_OR_D BIT(0)
45 /* PoU : Point of Unification, Poc: Point of Coherency */
47 INVALIDATE_POU, /* i-cache invalidate by address */
48 INVALIDATE_POC, /* d-cache invalidate by address */
49 INVALIDATE_SET_WAY, /* d-cache invalidate by sets/ways */
50 FLUSH_POU, /* d-cache clean by address to the PoU */
51 FLUSH_POC, /* d-cache clean by address to the PoC */
52 FLUSH_SET_WAY, /* d-cache clean by sets/ways */
53 FLUSH_INVAL_POC, /* d-cache clean & invalidate by addr to PoC */
54 FLUSH_INVAL_SET_WAY, /* d-cache clean & invalidate by set/ways */
57 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
58 struct dcache_config {
63 static void get_cache_ways_sets(struct dcache_config *cache)
65 u32 cache_size_id = readl(V7M_PROC_REG_CCSIDR);
67 cache->ways = (cache_size_id & MASK_NUM_WAYS) >> NUM_WAYS_SHIFT;
68 cache->sets = (cache_size_id & MASK_NUM_SETS) >> NUM_SETS_SHIFT;
72 * Return the io register to perform required cache action like clean or clean
73 * & invalidate by sets/ways.
75 static u32 *get_action_reg_set_ways(enum cache_action action)
78 case INVALIDATE_SET_WAY:
79 return V7M_CACHE_REG_DCISW;
81 return V7M_CACHE_REG_DCCSW;
82 case FLUSH_INVAL_SET_WAY:
83 return V7M_CACHE_REG_DCCISW;
92 * Return the io register to perform required cache action like clean or clean
93 * & invalidate by adddress or range.
95 static u32 *get_action_reg_range(enum cache_action action)
99 return V7M_CACHE_REG_ICIMVALU;
101 return V7M_CACHE_REG_DCIMVAC;
103 return V7M_CACHE_REG_DCCMVAU;
105 return V7M_CACHE_REG_DCCMVAC;
106 case FLUSH_INVAL_POC:
107 return V7M_CACHE_REG_DCCIMVAC;
115 static u32 get_cline_size(enum cache_type type)
120 clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
121 else if (type == ICACHE)
122 setbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
123 /* Make sure cache selection is effective for next memory access */
126 size = readl(V7M_PROC_REG_CCSIDR) & CLINE_SIZE_MASK;
127 /* Size enocoded as 2 less than log(no_of_words_in_cache_line) base 2 */
128 size = 1 << (size + 2);
129 debug("cache line size is %d\n", size);
134 /* Perform the action like invalidate/clean on a range of cache addresses */
135 static int action_cache_range(enum cache_action action, u32 start_addr,
140 enum cache_type type;
142 action_reg = get_action_reg_range(action);
145 if (action == INVALIDATE_POU)
150 /* Cache line size is minium size for the cache action */
151 cline_size = get_cline_size(type);
152 /* Align start address to cache line boundary */
153 start_addr &= ~(cline_size - 1);
154 debug("total size for cache action = %llx\n", size);
156 writel(start_addr, action_reg);
158 start_addr += cline_size;
159 } while (size > cline_size);
161 /* Make sure cache action is effective for next memory access */
163 isb(); /* Make sure instruction stream sees it */
164 debug("cache action on range done\n");
169 /* Perform the action like invalidate/clean on all cached addresses */
170 static int action_dcache_all(enum cache_action action)
172 struct dcache_config cache;
176 action_reg = get_action_reg_set_ways(action);
180 clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
181 /* Make sure cache selection is effective for next memory access */
184 get_cache_ways_sets(&cache); /* Get number of ways & sets */
185 debug("cache: ways= %d, sets= %d\n", cache.ways + 1, cache.sets + 1);
186 for (i = cache.sets; i >= 0; i--) {
187 for (j = cache.ways; j >= 0; j--) {
188 writel((j << WAYS_SHIFT) | (i << SETS_SHIFT),
193 /* Make sure cache action is effective for next memory access */
195 isb(); /* Make sure instruction stream sees it */
200 void dcache_enable(void)
202 if (dcache_status()) /* return if cache already enabled */
205 if (action_dcache_all(INVALIDATE_SET_WAY)) {
206 printf("ERR: D-cache not enabled\n");
210 setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
212 /* Make sure cache action is effective for next memory access */
214 isb(); /* Make sure instruction stream sees it */
217 void dcache_disable(void)
219 if (!dcache_status())
222 /* if dcache is enabled-> dcache disable & then flush */
223 if (action_dcache_all(FLUSH_SET_WAY)) {
224 printf("ERR: D-cache not flushed\n");
228 clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
230 /* Make sure cache action is effective for next memory access */
232 isb(); /* Make sure instruction stream sees it */
235 int dcache_status(void)
237 return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_DCACHE)) != 0;
240 void invalidate_dcache_range(unsigned long start, unsigned long stop)
242 if (action_cache_range(INVALIDATE_POC, start, stop - start)) {
243 printf("ERR: D-cache not invalidated\n");
248 void flush_dcache_range(unsigned long start, unsigned long stop)
250 if (action_cache_range(FLUSH_POC, start, stop - start)) {
251 printf("ERR: D-cache not flushed\n");
255 void flush_dcache_all(void)
257 if (action_dcache_all(FLUSH_SET_WAY)) {
258 printf("ERR: D-cache not flushed\n");
263 void invalidate_dcache_all(void)
265 if (action_dcache_all(INVALIDATE_SET_WAY)) {
266 printf("ERR: D-cache not invalidated\n");
271 void dcache_enable(void)
276 void dcache_disable(void)
281 int dcache_status(void)
286 void flush_dcache_all(void)
290 void invalidate_dcache_all(void)
295 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
297 void invalidate_icache_all(void)
299 writel(INVAL_ICACHE_POU, V7M_CACHE_REG_ICIALLU);
301 /* Make sure cache action is effective for next memory access */
303 isb(); /* Make sure instruction stream sees it */
306 void icache_enable(void)
311 invalidate_icache_all();
312 setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
314 /* Make sure cache action is effective for next memory access */
316 isb(); /* Make sure instruction stream sees it */
319 int icache_status(void)
321 return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_ICACHE)) != 0;
324 void icache_disable(void)
326 if (!icache_status())
329 isb(); /* flush pipeline */
330 clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
331 isb(); /* subsequent instructions fetch see cache disable effect */
334 void icache_enable(void)
339 void icache_disable(void)
344 int icache_status(void)
350 void enable_caches(void)
352 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
355 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)