Merge branch 'linux-tegra-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / arch / arm / mm / cache-l2x0.c
1 /*
2  * arch/arm/mm/cache-l2x0.c - L210/L220 cache controller support
3  *
4  * Copyright (C) 2007 ARM Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 #include <linux/init.h>
20 #include <linux/spinlock.h>
21 #include <linux/io.h>
22
23 #include <asm/cacheflush.h>
24 #include <asm/hardware/cache-l2x0.h>
25
26 #define CACHE_LINE_SIZE         32
27
28 static void __iomem *l2x0_base;
29 static uint32_t l2x0_way_mask;  /* Bitmask of active ways */
30 bool l2x0_disabled;
31
32 static inline void cache_wait_always(void __iomem *reg, unsigned long mask)
33 {
34         /* wait for the operation to complete */
35         while (readl_relaxed(reg) & mask)
36                 ;
37 }
38
39 #ifdef CONFIG_CACHE_PL310
40
41 static inline void cache_wait(void __iomem *reg, unsigned long mask)
42 {
43         /* cache operations are atomic */
44 }
45
46 #define _l2x0_lock(lock, flags)         ((void)(flags))
47 #define _l2x0_unlock(lock, flags)       ((void)(flags))
48
49 #define block_end(start, end)           (end)
50
51 #define L2CC_TYPE                       "PL310/L2C-310"
52
53 #else   /* !CONFIG_CACHE_PL310 */
54
55 #define cache_wait                      cache_wait_always
56
57 static DEFINE_SPINLOCK(l2x0_lock);
58 #define _l2x0_lock(lock, flags)         spin_lock_irqsave(lock, flags)
59 #define _l2x0_unlock(lock, flags)       spin_unlock_irqrestore(lock, flags)
60
61 #define block_end(start, end)           ((start) + min((end) - (start), 4096UL))
62
63 #define L2CC_TYPE                       "L2x0"
64
65 #endif  /* CONFIG_CACHE_PL310 */
66
67 static inline void cache_sync(void)
68 {
69         void __iomem *base = l2x0_base;
70         writel_relaxed(0, base + L2X0_CACHE_SYNC);
71         cache_wait(base + L2X0_CACHE_SYNC, 1);
72 }
73
74 static inline void l2x0_clean_line(unsigned long addr)
75 {
76         void __iomem *base = l2x0_base;
77         cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
78         writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
79 }
80
81 static inline void l2x0_inv_line(unsigned long addr)
82 {
83         void __iomem *base = l2x0_base;
84         cache_wait(base + L2X0_INV_LINE_PA, 1);
85         writel_relaxed(addr, base + L2X0_INV_LINE_PA);
86 }
87
88 #ifdef CONFIG_PL310_ERRATA_588369
89 static void debug_writel(unsigned long val)
90 {
91         extern void omap_smc1(u32 fn, u32 arg);
92
93         /*
94          * Texas Instrument secure monitor api to modify the
95          * PL310 Debug Control Register.
96          */
97         omap_smc1(0x100, val);
98 }
99
100 static inline void l2x0_flush_line(unsigned long addr)
101 {
102         void __iomem *base = l2x0_base;
103
104         /* Clean by PA followed by Invalidate by PA */
105         cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
106         writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
107         cache_wait(base + L2X0_INV_LINE_PA, 1);
108         writel_relaxed(addr, base + L2X0_INV_LINE_PA);
109 }
110 #else
111
112 /* Optimised out for non-errata case */
113 static inline void debug_writel(unsigned long val)
114 {
115 }
116
117 static inline void l2x0_flush_line(unsigned long addr)
118 {
119         void __iomem *base = l2x0_base;
120         cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
121         writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
122 }
123 #endif
124
125 static void l2x0_cache_sync(void)
126 {
127         unsigned long flags;
128
129         _l2x0_lock(&l2x0_lock, flags);
130         cache_sync();
131         _l2x0_unlock(&l2x0_lock, flags);
132 }
133
134 static inline void l2x0_inv_all(void)
135 {
136         unsigned long flags;
137
138         /* invalidate all ways */
139         _l2x0_lock(&l2x0_lock, flags);
140         writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
141         cache_wait_always(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
142         cache_sync();
143         _l2x0_unlock(&l2x0_lock, flags);
144 }
145
146 static inline void l2x0_flush_all(void)
147 {
148         unsigned long flags;
149
150         /* flush all ways */
151         _l2x0_lock(&l2x0_lock, flags);
152         writel(0xff, l2x0_base + L2X0_CLEAN_INV_WAY);
153         cache_wait_always(l2x0_base + L2X0_CLEAN_INV_WAY, 0xff);
154         cache_sync();
155         _l2x0_unlock(&l2x0_lock, flags);
156 }
157
158 static void l2x0_inv_range(unsigned long start, unsigned long end)
159 {
160         void __iomem *base = l2x0_base;
161         unsigned long flags;
162
163         _l2x0_lock(&l2x0_lock, flags);
164         if (start & (CACHE_LINE_SIZE - 1)) {
165                 start &= ~(CACHE_LINE_SIZE - 1);
166                 debug_writel(0x03);
167                 l2x0_flush_line(start);
168                 debug_writel(0x00);
169                 start += CACHE_LINE_SIZE;
170         }
171
172         if (end & (CACHE_LINE_SIZE - 1)) {
173                 end &= ~(CACHE_LINE_SIZE - 1);
174                 debug_writel(0x03);
175                 l2x0_flush_line(end);
176                 debug_writel(0x00);
177         }
178
179         while (start < end) {
180                 unsigned long blk_end = block_end(start, end);
181
182                 while (start < blk_end) {
183                         l2x0_inv_line(start);
184                         start += CACHE_LINE_SIZE;
185                 }
186
187                 if (blk_end < end) {
188                         _l2x0_unlock(&l2x0_lock, flags);
189                         _l2x0_lock(&l2x0_lock, flags);
190                 }
191         }
192         cache_wait(base + L2X0_INV_LINE_PA, 1);
193         cache_sync();
194         _l2x0_unlock(&l2x0_lock, flags);
195 }
196
197 static void l2x0_clean_range(unsigned long start, unsigned long end)
198 {
199         void __iomem *base = l2x0_base;
200         unsigned long flags;
201
202         _l2x0_lock(&l2x0_lock, flags);
203         start &= ~(CACHE_LINE_SIZE - 1);
204         while (start < end) {
205                 unsigned long blk_end = block_end(start, end);
206
207                 while (start < blk_end) {
208                         l2x0_clean_line(start);
209                         start += CACHE_LINE_SIZE;
210                 }
211
212                 if (blk_end < end) {
213                         _l2x0_unlock(&l2x0_lock, flags);
214                         _l2x0_lock(&l2x0_lock, flags);
215                 }
216         }
217         cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
218         cache_sync();
219         _l2x0_unlock(&l2x0_lock, flags);
220 }
221
222 static void l2x0_flush_range(unsigned long start, unsigned long end)
223 {
224         void __iomem *base = l2x0_base;
225         unsigned long flags;
226
227         _l2x0_lock(&l2x0_lock, flags);
228         start &= ~(CACHE_LINE_SIZE - 1);
229         while (start < end) {
230                 unsigned long blk_end = block_end(start, end);
231
232                 debug_writel(0x03);
233                 while (start < blk_end) {
234                         l2x0_flush_line(start);
235                         start += CACHE_LINE_SIZE;
236                 }
237                 debug_writel(0x00);
238
239                 if (blk_end < end) {
240                         _l2x0_unlock(&l2x0_lock, flags);
241                         _l2x0_lock(&l2x0_lock, flags);
242                 }
243         }
244         cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
245         cache_sync();
246         _l2x0_unlock(&l2x0_lock, flags);
247 }
248
249 void l2x0_shutdown(void)
250 {
251         unsigned long flags;
252
253         if (l2x0_disabled)
254                 return;
255
256         BUG_ON(num_online_cpus() > 1);
257
258         local_irq_save(flags);
259
260         if (readl(l2x0_base + L2X0_CTRL) & 1) {
261                 int m;
262                 /* lockdown all ways, all masters to prevent new line
263                  * allocation during maintenance */
264                 for (m=0; m<8; m++) {
265                         writel(l2x0_way_mask,
266                                l2x0_base + L2X0_LOCKDOWN_WAY_D + (m*8));
267                         writel(l2x0_way_mask,
268                                l2x0_base + L2X0_LOCKDOWN_WAY_I + (m*8));
269                 }
270                 l2x0_flush_all();
271                 writel(0, l2x0_base + L2X0_CTRL);
272                 /* unlock cache ways */
273                 for (m=0; m<8; m++) {
274                         writel(0, l2x0_base + L2X0_LOCKDOWN_WAY_D + (m*8));
275                         writel(0, l2x0_base + L2X0_LOCKDOWN_WAY_I + (m*8));
276                 }
277         }
278
279         local_irq_restore(flags);
280 }
281
282 static void l2x0_enable(__u32 aux_val, __u32 aux_mask)
283 {
284         __u32 aux;
285         __u32 cache_id;
286         int ways;
287         const char *type;
288
289         if (l2x0_disabled)
290                 return;
291
292         cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
293         aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
294
295         aux &= aux_mask;
296         aux |= aux_val;
297
298         /* Determine the number of ways */
299         switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
300         case L2X0_CACHE_ID_PART_L310:
301                 if (aux & (1 << 16))
302                         ways = 16;
303                 else
304                         ways = 8;
305                 type = "L310";
306                 break;
307         case L2X0_CACHE_ID_PART_L210:
308                 ways = (aux >> 13) & 0xf;
309                 type = "L210";
310                 break;
311         default:
312                 /* Assume unknown chips have 8 ways */
313                 ways = 8;
314                 type = "L2x0 series";
315                 break;
316         }
317
318         l2x0_way_mask = (1 << ways) - 1;
319
320         /*
321          * Check if l2x0 controller is already enabled.
322          * If you are booting from non-secure mode
323          * accessing the below registers will fault.
324          */
325         if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
326
327                 /* l2x0 controller is disabled */
328                 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
329
330                 l2x0_inv_all();
331
332                 /* enable L2X0 */
333                 writel_relaxed(1, l2x0_base + L2X0_CTRL);
334         }
335
336         /*printk(KERN_INFO "%s cache controller enabled\n", type);
337         printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
338                          ways, cache_id, aux);*/
339 }
340
341 void l2x0_restart(void)
342 {
343         l2x0_enable(0, ~0ul);
344 }
345
346 void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
347 {
348         if (l2x0_disabled) {
349                 pr_info(L2CC_TYPE " cache controller disabled\n");
350                 return;
351         }
352
353         l2x0_base = base;
354
355         l2x0_enable(aux_val, aux_mask);
356
357         outer_cache.inv_range = l2x0_inv_range;
358         outer_cache.clean_range = l2x0_clean_range;
359         outer_cache.flush_range = l2x0_flush_range;
360         outer_cache.sync = l2x0_cache_sync;
361 }
362
363 static int __init l2x0_disable(char *unused)
364 {
365         l2x0_disabled = 1;
366         return 0;
367 }
368 early_param("nol2x0", l2x0_disable);