xref: /openbmc/linux/arch/arm/mm/cache-xsc3l2.c (revision 0ba8b9b273c45dd23f60ff700e265a0069b33758)
1905a09d5SEric Miao /*
2905a09d5SEric Miao  * arch/arm/mm/cache-xsc3l2.c - XScale3 L2 cache controller support
3905a09d5SEric Miao  *
4905a09d5SEric Miao  * Copyright (C) 2007 ARM Limited
5905a09d5SEric Miao  *
6905a09d5SEric Miao  * This program is free software; you can redistribute it and/or modify
7905a09d5SEric Miao  * it under the terms of the GNU General Public License version 2 as
8905a09d5SEric Miao  * published by the Free Software Foundation.
9905a09d5SEric Miao  *
10905a09d5SEric Miao  * This program is distributed in the hope that it will be useful,
11905a09d5SEric Miao  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12905a09d5SEric Miao  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13905a09d5SEric Miao  * GNU General Public License for more details.
14905a09d5SEric Miao  *
15905a09d5SEric Miao  * You should have received a copy of the GNU General Public License
16905a09d5SEric Miao  * along with this program; if not, write to the Free Software
17905a09d5SEric Miao  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18905a09d5SEric Miao  */
19905a09d5SEric Miao #include <linux/init.h>
20905a09d5SEric Miao #include <linux/spinlock.h>
21*0ba8b9b2SRussell King #include <linux/io.h>
22905a09d5SEric Miao 
23905a09d5SEric Miao #include <asm/system.h>
24*0ba8b9b2SRussell King #include <asm/cputype.h>
25905a09d5SEric Miao #include <asm/cacheflush.h>
26905a09d5SEric Miao 
27905a09d5SEric Miao #define CR_L2	(1 << 26)
28905a09d5SEric Miao 
29905a09d5SEric Miao #define CACHE_LINE_SIZE		32
30905a09d5SEric Miao #define CACHE_LINE_SHIFT	5
31905a09d5SEric Miao #define CACHE_WAY_PER_SET	8
32905a09d5SEric Miao 
33905a09d5SEric Miao #define CACHE_WAY_SIZE(l2ctype)	(8192 << (((l2ctype) >> 8) & 0xf))
34905a09d5SEric Miao #define CACHE_SET_SIZE(l2ctype)	(CACHE_WAY_SIZE(l2ctype) >> CACHE_LINE_SHIFT)
35905a09d5SEric Miao 
36905a09d5SEric Miao static inline int xsc3_l2_present(void)
37905a09d5SEric Miao {
38905a09d5SEric Miao 	unsigned long l2ctype;
39905a09d5SEric Miao 
40905a09d5SEric Miao 	__asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
41905a09d5SEric Miao 
42905a09d5SEric Miao 	return !!(l2ctype & 0xf8);
43905a09d5SEric Miao }
44905a09d5SEric Miao 
45905a09d5SEric Miao static inline void xsc3_l2_clean_mva(unsigned long addr)
46905a09d5SEric Miao {
47905a09d5SEric Miao 	__asm__("mcr p15, 1, %0, c7, c11, 1" : : "r" (addr));
48905a09d5SEric Miao }
49905a09d5SEric Miao 
50905a09d5SEric Miao static inline void xsc3_l2_clean_pa(unsigned long addr)
51905a09d5SEric Miao {
52905a09d5SEric Miao 	xsc3_l2_clean_mva(__phys_to_virt(addr));
53905a09d5SEric Miao }
54905a09d5SEric Miao 
55905a09d5SEric Miao static inline void xsc3_l2_inv_mva(unsigned long addr)
56905a09d5SEric Miao {
57905a09d5SEric Miao 	__asm__("mcr p15, 1, %0, c7, c7, 1" : : "r" (addr));
58905a09d5SEric Miao }
59905a09d5SEric Miao 
60905a09d5SEric Miao static inline void xsc3_l2_inv_pa(unsigned long addr)
61905a09d5SEric Miao {
62905a09d5SEric Miao 	xsc3_l2_inv_mva(__phys_to_virt(addr));
63905a09d5SEric Miao }
64905a09d5SEric Miao 
65905a09d5SEric Miao static inline void xsc3_l2_inv_all(void)
66905a09d5SEric Miao {
67905a09d5SEric Miao 	unsigned long l2ctype, set_way;
68905a09d5SEric Miao 	int set, way;
69905a09d5SEric Miao 
70905a09d5SEric Miao 	__asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
71905a09d5SEric Miao 
72905a09d5SEric Miao 	for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
73905a09d5SEric Miao 		for (way = 0; way < CACHE_WAY_PER_SET; way++) {
74905a09d5SEric Miao 			set_way = (way << 29) | (set << 5);
75905a09d5SEric Miao 			__asm__("mcr p15, 1, %0, c7, c11, 2" : : "r"(set_way));
76905a09d5SEric Miao 		}
77905a09d5SEric Miao 	}
78905a09d5SEric Miao 
79905a09d5SEric Miao 	dsb();
80905a09d5SEric Miao }
81905a09d5SEric Miao 
82905a09d5SEric Miao static void xsc3_l2_inv_range(unsigned long start, unsigned long end)
83905a09d5SEric Miao {
84905a09d5SEric Miao 	if (start == 0 && end == -1ul) {
85905a09d5SEric Miao 		xsc3_l2_inv_all();
86905a09d5SEric Miao 		return;
87905a09d5SEric Miao 	}
88905a09d5SEric Miao 
89905a09d5SEric Miao 	/*
90905a09d5SEric Miao 	 * Clean and invalidate partial first cache line.
91905a09d5SEric Miao 	 */
92905a09d5SEric Miao 	if (start & (CACHE_LINE_SIZE - 1)) {
93905a09d5SEric Miao 		xsc3_l2_clean_pa(start & ~(CACHE_LINE_SIZE - 1));
94905a09d5SEric Miao 		xsc3_l2_inv_pa(start & ~(CACHE_LINE_SIZE - 1));
95905a09d5SEric Miao 		start = (start | (CACHE_LINE_SIZE - 1)) + 1;
96905a09d5SEric Miao 	}
97905a09d5SEric Miao 
98905a09d5SEric Miao 	/*
99905a09d5SEric Miao 	 * Clean and invalidate partial last cache line.
100905a09d5SEric Miao 	 */
101905a09d5SEric Miao 	if (end & (CACHE_LINE_SIZE - 1)) {
102905a09d5SEric Miao 		xsc3_l2_clean_pa(end & ~(CACHE_LINE_SIZE - 1));
103905a09d5SEric Miao 		xsc3_l2_inv_pa(end & ~(CACHE_LINE_SIZE - 1));
104905a09d5SEric Miao 		end &= ~(CACHE_LINE_SIZE - 1);
105905a09d5SEric Miao 	}
106905a09d5SEric Miao 
107905a09d5SEric Miao 	/*
108905a09d5SEric Miao 	 * Invalidate all full cache lines between 'start' and 'end'.
109905a09d5SEric Miao 	 */
110905a09d5SEric Miao 	while (start != end) {
111905a09d5SEric Miao 		xsc3_l2_inv_pa(start);
112905a09d5SEric Miao 		start += CACHE_LINE_SIZE;
113905a09d5SEric Miao 	}
114905a09d5SEric Miao 
115905a09d5SEric Miao 	dsb();
116905a09d5SEric Miao }
117905a09d5SEric Miao 
118905a09d5SEric Miao static void xsc3_l2_clean_range(unsigned long start, unsigned long end)
119905a09d5SEric Miao {
120905a09d5SEric Miao 	start &= ~(CACHE_LINE_SIZE - 1);
121905a09d5SEric Miao 	while (start < end) {
122905a09d5SEric Miao 		xsc3_l2_clean_pa(start);
123905a09d5SEric Miao 		start += CACHE_LINE_SIZE;
124905a09d5SEric Miao 	}
125905a09d5SEric Miao 
126905a09d5SEric Miao 	dsb();
127905a09d5SEric Miao }
128905a09d5SEric Miao 
129905a09d5SEric Miao /*
130905a09d5SEric Miao  * optimize L2 flush all operation by set/way format
131905a09d5SEric Miao  */
132905a09d5SEric Miao static inline void xsc3_l2_flush_all(void)
133905a09d5SEric Miao {
134905a09d5SEric Miao 	unsigned long l2ctype, set_way;
135905a09d5SEric Miao 	int set, way;
136905a09d5SEric Miao 
137905a09d5SEric Miao 	__asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
138905a09d5SEric Miao 
139905a09d5SEric Miao 	for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
140905a09d5SEric Miao 		for (way = 0; way < CACHE_WAY_PER_SET; way++) {
141905a09d5SEric Miao 			set_way = (way << 29) | (set << 5);
142905a09d5SEric Miao 			__asm__("mcr p15, 1, %0, c7, c15, 2" : : "r"(set_way));
143905a09d5SEric Miao 		}
144905a09d5SEric Miao 	}
145905a09d5SEric Miao 
146905a09d5SEric Miao 	dsb();
147905a09d5SEric Miao }
148905a09d5SEric Miao 
149905a09d5SEric Miao static void xsc3_l2_flush_range(unsigned long start, unsigned long end)
150905a09d5SEric Miao {
151905a09d5SEric Miao 	if (start == 0 && end == -1ul) {
152905a09d5SEric Miao 		xsc3_l2_flush_all();
153905a09d5SEric Miao 		return;
154905a09d5SEric Miao 	}
155905a09d5SEric Miao 
156905a09d5SEric Miao 	start &= ~(CACHE_LINE_SIZE - 1);
157905a09d5SEric Miao 	while (start < end) {
158905a09d5SEric Miao 		xsc3_l2_clean_pa(start);
159905a09d5SEric Miao 		xsc3_l2_inv_pa(start);
160905a09d5SEric Miao 		start += CACHE_LINE_SIZE;
161905a09d5SEric Miao 	}
162905a09d5SEric Miao 
163905a09d5SEric Miao 	dsb();
164905a09d5SEric Miao }
165905a09d5SEric Miao 
166905a09d5SEric Miao static int __init xsc3_l2_init(void)
167905a09d5SEric Miao {
168905a09d5SEric Miao 	if (!cpu_is_xsc3() || !xsc3_l2_present())
169905a09d5SEric Miao 		return 0;
170905a09d5SEric Miao 
171905a09d5SEric Miao 	if (!(get_cr() & CR_L2)) {
172905a09d5SEric Miao 		pr_info("XScale3 L2 cache enabled.\n");
173905a09d5SEric Miao 		adjust_cr(CR_L2, CR_L2);
174905a09d5SEric Miao 		xsc3_l2_inv_all();
175905a09d5SEric Miao 	}
176905a09d5SEric Miao 
177905a09d5SEric Miao 	outer_cache.inv_range = xsc3_l2_inv_range;
178905a09d5SEric Miao 	outer_cache.clean_range = xsc3_l2_clean_range;
179905a09d5SEric Miao 	outer_cache.flush_range = xsc3_l2_flush_range;
180905a09d5SEric Miao 
181905a09d5SEric Miao 	return 0;
182905a09d5SEric Miao }
183905a09d5SEric Miao core_initcall(xsc3_l2_init);
184