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.
11 #include <asm/armv7m.h>
12 #include <asm/cache.h>
14 #include <linux/bitops.h>
16 /* Cache maintenance operation registers */
18 #define V7M_CACHE_REG_ICIALLU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x00))
19 #define INVAL_ICACHE_POU 0
20 #define V7M_CACHE_REG_ICIMVALU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x08))
21 #define V7M_CACHE_REG_DCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x0C))
22 #define V7M_CACHE_REG_DCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x10))
23 #define V7M_CACHE_REG_DCCMVAU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x14))
24 #define V7M_CACHE_REG_DCCMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x18))
25 #define V7M_CACHE_REG_DCCSW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x1C))
26 #define V7M_CACHE_REG_DCCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x20))
27 #define V7M_CACHE_REG_DCCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x24))
31 /* armv7m processor feature registers */
33 #define V7M_PROC_REG_CLIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x00))
34 #define V7M_PROC_REG_CTR ((u32 *)(V7M_PROC_FTR_BASE + 0x04))
35 #define V7M_PROC_REG_CCSIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x08))
36 #define MASK_NUM_WAYS GENMASK(12, 3)
37 #define MASK_NUM_SETS GENMASK(27, 13)
38 #define CLINE_SIZE_MASK GENMASK(2, 0)
39 #define NUM_WAYS_SHIFT 3
40 #define NUM_SETS_SHIFT 13
41 #define V7M_PROC_REG_CSSELR ((u32 *)(V7M_PROC_FTR_BASE + 0x0C))
42 #define SEL_I_OR_D BIT(0)
49 /* PoU : Point of Unification, Poc: Point of Coherency */
51 INVALIDATE_POU, /* i-cache invalidate by address */
52 INVALIDATE_POC, /* d-cache invalidate by address */
53 INVALIDATE_SET_WAY, /* d-cache invalidate by sets/ways */
54 FLUSH_POU, /* d-cache clean by address to the PoU */
55 FLUSH_POC, /* d-cache clean by address to the PoC */
56 FLUSH_SET_WAY, /* d-cache clean by sets/ways */
57 FLUSH_INVAL_POC, /* d-cache clean & invalidate by addr to PoC */
58 FLUSH_INVAL_SET_WAY, /* d-cache clean & invalidate by set/ways */
61 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
62 struct dcache_config {
67 static void get_cache_ways_sets(struct dcache_config *cache)
69 u32 cache_size_id = readl(V7M_PROC_REG_CCSIDR);
71 cache->ways = (cache_size_id & MASK_NUM_WAYS) >> NUM_WAYS_SHIFT;
72 cache->sets = (cache_size_id & MASK_NUM_SETS) >> NUM_SETS_SHIFT;
76 * Return the io register to perform required cache action like clean or clean
77 * & invalidate by sets/ways.
79 static u32 *get_action_reg_set_ways(enum cache_action action)
82 case INVALIDATE_SET_WAY:
83 return V7M_CACHE_REG_DCISW;
85 return V7M_CACHE_REG_DCCSW;
86 case FLUSH_INVAL_SET_WAY:
87 return V7M_CACHE_REG_DCCISW;
96 * Return the io register to perform required cache action like clean or clean
97 * & invalidate by adddress or range.
99 static u32 *get_action_reg_range(enum cache_action action)
103 return V7M_CACHE_REG_ICIMVALU;
105 return V7M_CACHE_REG_DCIMVAC;
107 return V7M_CACHE_REG_DCCMVAU;
109 return V7M_CACHE_REG_DCCMVAC;
110 case FLUSH_INVAL_POC:
111 return V7M_CACHE_REG_DCCIMVAC;
119 static u32 get_cline_size(enum cache_type type)
124 clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
125 else if (type == ICACHE)
126 setbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
127 /* Make sure cache selection is effective for next memory access */
130 size = readl(V7M_PROC_REG_CCSIDR) & CLINE_SIZE_MASK;
131 /* Size enocoded as 2 less than log(no_of_words_in_cache_line) base 2 */
132 size = 1 << (size + 2);
133 debug("cache line size is %d\n", size);
138 /* Perform the action like invalidate/clean on a range of cache addresses */
139 static int action_cache_range(enum cache_action action, u32 start_addr,
144 enum cache_type type;
146 action_reg = get_action_reg_range(action);
149 if (action == INVALIDATE_POU)
154 /* Cache line size is minium size for the cache action */
155 cline_size = get_cline_size(type);
156 /* Align start address to cache line boundary */
157 start_addr &= ~(cline_size - 1);
158 debug("total size for cache action = %llx\n", size);
160 writel(start_addr, action_reg);
162 start_addr += cline_size;
163 } while (size > cline_size);
165 /* Make sure cache action is effective for next memory access */
167 isb(); /* Make sure instruction stream sees it */
168 debug("cache action on range done\n");
173 /* Perform the action like invalidate/clean on all cached addresses */
174 static int action_dcache_all(enum cache_action action)
176 struct dcache_config cache;
180 action_reg = get_action_reg_set_ways(action);
184 clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
185 /* Make sure cache selection is effective for next memory access */
188 get_cache_ways_sets(&cache); /* Get number of ways & sets */
189 debug("cache: ways= %d, sets= %d\n", cache.ways + 1, cache.sets + 1);
190 for (i = cache.sets; i >= 0; i--) {
191 for (j = cache.ways; j >= 0; j--) {
192 writel((j << WAYS_SHIFT) | (i << SETS_SHIFT),
197 /* Make sure cache action is effective for next memory access */
199 isb(); /* Make sure instruction stream sees it */
204 void dcache_enable(void)
206 if (dcache_status()) /* return if cache already enabled */
209 if (action_dcache_all(INVALIDATE_SET_WAY)) {
210 printf("ERR: D-cache not enabled\n");
214 setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
216 /* Make sure cache action is effective for next memory access */
218 isb(); /* Make sure instruction stream sees it */
221 void dcache_disable(void)
223 if (!dcache_status())
226 /* if dcache is enabled-> dcache disable & then flush */
227 if (action_dcache_all(FLUSH_SET_WAY)) {
228 printf("ERR: D-cache not flushed\n");
232 clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
234 /* Make sure cache action is effective for next memory access */
236 isb(); /* Make sure instruction stream sees it */
239 int dcache_status(void)
241 return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_DCACHE)) != 0;
244 void invalidate_dcache_range(unsigned long start, unsigned long stop)
246 if (action_cache_range(INVALIDATE_POC, start, stop - start)) {
247 printf("ERR: D-cache not invalidated\n");
252 void flush_dcache_range(unsigned long start, unsigned long stop)
254 if (action_cache_range(FLUSH_POC, start, stop - start)) {
255 printf("ERR: D-cache not flushed\n");
259 void flush_dcache_all(void)
261 if (action_dcache_all(FLUSH_SET_WAY)) {
262 printf("ERR: D-cache not flushed\n");
267 void invalidate_dcache_all(void)
269 if (action_dcache_all(INVALIDATE_SET_WAY)) {
270 printf("ERR: D-cache not invalidated\n");
275 void dcache_enable(void)
280 void dcache_disable(void)
285 int dcache_status(void)
290 void flush_dcache_all(void)
294 void invalidate_dcache_all(void)
298 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
299 enum dcache_option option)
305 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
307 void invalidate_icache_all(void)
309 writel(INVAL_ICACHE_POU, V7M_CACHE_REG_ICIALLU);
311 /* Make sure cache action is effective for next memory access */
313 isb(); /* Make sure instruction stream sees it */
316 void icache_enable(void)
321 invalidate_icache_all();
322 setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
324 /* Make sure cache action is effective for next memory access */
326 isb(); /* Make sure instruction stream sees it */
329 int icache_status(void)
331 return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_ICACHE)) != 0;
334 void icache_disable(void)
336 if (!icache_status())
339 isb(); /* flush pipeline */
340 clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
341 isb(); /* subsequent instructions fetch see cache disable effect */
344 void invalidate_icache_all(void)
349 void icache_enable(void)
354 void icache_disable(void)
359 int icache_status(void)
365 void enable_caches(void)
367 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
370 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)