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.
10 #include <asm/armv7m.h>
11 #include <asm/cache.h>
14 /* Cache maintenance operation registers */
16 #define V7M_CACHE_REG_ICIALLU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x00))
17 #define INVAL_ICACHE_POU 0
18 #define V7M_CACHE_REG_ICIMVALU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x08))
19 #define V7M_CACHE_REG_DCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x0C))
20 #define V7M_CACHE_REG_DCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x10))
21 #define V7M_CACHE_REG_DCCMVAU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x14))
22 #define V7M_CACHE_REG_DCCMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x18))
23 #define V7M_CACHE_REG_DCCSW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x1C))
24 #define V7M_CACHE_REG_DCCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x20))
25 #define V7M_CACHE_REG_DCCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x24))
29 /* armv7m processor feature registers */
31 #define V7M_PROC_REG_CLIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x00))
32 #define V7M_PROC_REG_CTR ((u32 *)(V7M_PROC_FTR_BASE + 0x04))
33 #define V7M_PROC_REG_CCSIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x08))
34 #define MASK_NUM_WAYS GENMASK(12, 3)
35 #define MASK_NUM_SETS GENMASK(27, 13)
36 #define CLINE_SIZE_MASK GENMASK(2, 0)
37 #define NUM_WAYS_SHIFT 3
38 #define NUM_SETS_SHIFT 13
39 #define V7M_PROC_REG_CSSELR ((u32 *)(V7M_PROC_FTR_BASE + 0x0C))
40 #define SEL_I_OR_D BIT(0)
47 /* PoU : Point of Unification, Poc: Point of Coherency */
49 INVALIDATE_POU, /* i-cache invalidate by address */
50 INVALIDATE_POC, /* d-cache invalidate by address */
51 INVALIDATE_SET_WAY, /* d-cache invalidate by sets/ways */
52 FLUSH_POU, /* d-cache clean by address to the PoU */
53 FLUSH_POC, /* d-cache clean by address to the PoC */
54 FLUSH_SET_WAY, /* d-cache clean by sets/ways */
55 FLUSH_INVAL_POC, /* d-cache clean & invalidate by addr to PoC */
56 FLUSH_INVAL_SET_WAY, /* d-cache clean & invalidate by set/ways */
59 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
60 struct dcache_config {
65 static void get_cache_ways_sets(struct dcache_config *cache)
67 u32 cache_size_id = readl(V7M_PROC_REG_CCSIDR);
69 cache->ways = (cache_size_id & MASK_NUM_WAYS) >> NUM_WAYS_SHIFT;
70 cache->sets = (cache_size_id & MASK_NUM_SETS) >> NUM_SETS_SHIFT;
74 * Return the io register to perform required cache action like clean or clean
75 * & invalidate by sets/ways.
77 static u32 *get_action_reg_set_ways(enum cache_action action)
80 case INVALIDATE_SET_WAY:
81 return V7M_CACHE_REG_DCISW;
83 return V7M_CACHE_REG_DCCSW;
84 case FLUSH_INVAL_SET_WAY:
85 return V7M_CACHE_REG_DCCISW;
94 * Return the io register to perform required cache action like clean or clean
95 * & invalidate by adddress or range.
97 static u32 *get_action_reg_range(enum cache_action action)
101 return V7M_CACHE_REG_ICIMVALU;
103 return V7M_CACHE_REG_DCIMVAC;
105 return V7M_CACHE_REG_DCCMVAU;
107 return V7M_CACHE_REG_DCCMVAC;
108 case FLUSH_INVAL_POC:
109 return V7M_CACHE_REG_DCCIMVAC;
117 static u32 get_cline_size(enum cache_type type)
122 clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
123 else if (type == ICACHE)
124 setbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
125 /* Make sure cache selection is effective for next memory access */
128 size = readl(V7M_PROC_REG_CCSIDR) & CLINE_SIZE_MASK;
129 /* Size enocoded as 2 less than log(no_of_words_in_cache_line) base 2 */
130 size = 1 << (size + 2);
131 debug("cache line size is %d\n", size);
136 /* Perform the action like invalidate/clean on a range of cache addresses */
137 static int action_cache_range(enum cache_action action, u32 start_addr,
142 enum cache_type type;
144 action_reg = get_action_reg_range(action);
147 if (action == INVALIDATE_POU)
152 /* Cache line size is minium size for the cache action */
153 cline_size = get_cline_size(type);
154 /* Align start address to cache line boundary */
155 start_addr &= ~(cline_size - 1);
156 debug("total size for cache action = %llx\n", size);
158 writel(start_addr, action_reg);
160 start_addr += cline_size;
161 } while (size > cline_size);
163 /* Make sure cache action is effective for next memory access */
165 isb(); /* Make sure instruction stream sees it */
166 debug("cache action on range done\n");
171 /* Perform the action like invalidate/clean on all cached addresses */
172 static int action_dcache_all(enum cache_action action)
174 struct dcache_config cache;
178 action_reg = get_action_reg_set_ways(action);
182 clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
183 /* Make sure cache selection is effective for next memory access */
186 get_cache_ways_sets(&cache); /* Get number of ways & sets */
187 debug("cache: ways= %d, sets= %d\n", cache.ways + 1, cache.sets + 1);
188 for (i = cache.sets; i >= 0; i--) {
189 for (j = cache.ways; j >= 0; j--) {
190 writel((j << WAYS_SHIFT) | (i << SETS_SHIFT),
195 /* Make sure cache action is effective for next memory access */
197 isb(); /* Make sure instruction stream sees it */
202 void dcache_enable(void)
204 if (dcache_status()) /* return if cache already enabled */
207 if (action_dcache_all(INVALIDATE_SET_WAY)) {
208 printf("ERR: D-cache not enabled\n");
212 setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
214 /* Make sure cache action is effective for next memory access */
216 isb(); /* Make sure instruction stream sees it */
219 void dcache_disable(void)
221 if (!dcache_status())
224 /* if dcache is enabled-> dcache disable & then flush */
225 if (action_dcache_all(FLUSH_SET_WAY)) {
226 printf("ERR: D-cache not flushed\n");
230 clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
232 /* Make sure cache action is effective for next memory access */
234 isb(); /* Make sure instruction stream sees it */
237 int dcache_status(void)
239 return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_DCACHE)) != 0;
242 void invalidate_dcache_range(unsigned long start, unsigned long stop)
244 if (action_cache_range(INVALIDATE_POC, start, stop - start)) {
245 printf("ERR: D-cache not invalidated\n");
250 void flush_dcache_range(unsigned long start, unsigned long stop)
252 if (action_cache_range(FLUSH_POC, start, stop - start)) {
253 printf("ERR: D-cache not flushed\n");
257 void flush_dcache_all(void)
259 if (action_dcache_all(FLUSH_SET_WAY)) {
260 printf("ERR: D-cache not flushed\n");
265 void invalidate_dcache_all(void)
267 if (action_dcache_all(INVALIDATE_SET_WAY)) {
268 printf("ERR: D-cache not invalidated\n");
273 void dcache_enable(void)
278 void dcache_disable(void)
283 int dcache_status(void)
288 void flush_dcache_all(void)
292 void invalidate_dcache_all(void)
296 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
297 enum dcache_option option)
303 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
305 void invalidate_icache_all(void)
307 writel(INVAL_ICACHE_POU, V7M_CACHE_REG_ICIALLU);
309 /* Make sure cache action is effective for next memory access */
311 isb(); /* Make sure instruction stream sees it */
314 void icache_enable(void)
319 invalidate_icache_all();
320 setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
322 /* Make sure cache action is effective for next memory access */
324 isb(); /* Make sure instruction stream sees it */
327 int icache_status(void)
329 return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_ICACHE)) != 0;
332 void icache_disable(void)
334 if (!icache_status())
337 isb(); /* flush pipeline */
338 clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
339 isb(); /* subsequent instructions fetch see cache disable effect */
342 void invalidate_icache_all(void)
347 void icache_enable(void)
352 void icache_disable(void)
357 int icache_status(void)
363 void enable_caches(void)
365 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
368 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)