blob: 84933f48edea8e63528ce899d910cd0f9e37ce07 [file] [log] [blame]
Catalin Marinas382266a2007-02-05 14:48:19 +01001/*
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 */
Rob Herring8c369262011-08-03 18:12:05 +010019#include <linux/err.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010020#include <linux/init.h>
Catalin Marinas07620972007-07-20 11:42:40 +010021#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
Rob Herring8c369262011-08-03 18:12:05 +010023#include <linux/of.h>
24#include <linux/of_address.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010025
26#include <asm/cacheflush.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010027#include <asm/hardware/cache-l2x0.h>
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +010028#include "cache-tauros3.h"
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +010029#include "cache-aurora-l2.h"
Catalin Marinas382266a2007-02-05 14:48:19 +010030
Russell Kingc02642b2014-03-15 16:47:54 +000031struct l2c_init_data {
Russell King051334b2014-03-15 23:04:10 +000032 const char *type;
Russell King0493aef2014-03-15 23:26:24 +000033 unsigned way_size_0;
Russell King3b8bad52014-03-15 16:47:57 +000034 unsigned num_lock;
Russell Kingc02642b2014-03-15 16:47:54 +000035 void (*of_parse)(const struct device_node *, u32 *, u32 *);
Russell King3b8bad52014-03-15 16:47:57 +000036 void (*enable)(void __iomem *, u32, unsigned);
Russell King75461f52014-03-15 16:48:07 +000037 void (*fixup)(void __iomem *, u32, struct outer_cache_fns *);
Russell King9846dfc2014-03-15 16:47:55 +000038 void (*save)(void __iomem *);
Russell Kingc02642b2014-03-15 16:47:54 +000039 struct outer_cache_fns outer_cache;
40};
41
Catalin Marinas382266a2007-02-05 14:48:19 +010042#define CACHE_LINE_SIZE 32
43
44static void __iomem *l2x0_base;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050045static DEFINE_RAW_SPINLOCK(l2x0_lock);
Russell King3e175ca2011-09-18 11:27:30 +010046static u32 l2x0_way_mask; /* Bitmask of active ways */
47static u32 l2x0_size;
Will Deaconf154fe92012-04-20 17:21:08 +010048static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
Catalin Marinas382266a2007-02-05 14:48:19 +010049
Barry Song91c2ebb2011-09-30 14:43:12 +010050struct l2x0_regs l2x0_saved_regs;
51
Russell King37abcdb2014-03-15 16:47:50 +000052/*
53 * Common code for all cache controllers.
54 */
Russell King83841fe2014-03-15 16:48:14 +000055static inline void l2c_wait_mask(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010056{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010057 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010058 while (readl_relaxed(reg) & mask)
Barry Song1caf3092011-09-09 10:30:34 +010059 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010060}
61
Russell King2b2a87a2014-03-16 17:19:21 +000062/*
Russell King8abd2592014-03-16 17:38:08 +000063 * By default, we write directly to secure registers. Platforms must
64 * override this if they are running non-secure.
65 */
66static void l2c_write_sec(unsigned long val, void __iomem *base, unsigned reg)
67{
68 if (val == readl_relaxed(base + reg))
69 return;
70 if (outer_cache.write_sec)
71 outer_cache.write_sec(val, reg);
72 else
73 writel_relaxed(val, base + reg);
74}
75
76/*
Russell King2b2a87a2014-03-16 17:19:21 +000077 * This should only be called when we have a requirement that the
78 * register be written due to a work-around, as platforms running
79 * in non-secure mode may not be able to access this register.
80 */
81static inline void l2c_set_debug(void __iomem *base, unsigned long val)
82{
Russell King8abd2592014-03-16 17:38:08 +000083 if (outer_cache.set_debug)
84 outer_cache.set_debug(val);
85 else
86 l2c_write_sec(val, base, L2X0_DEBUG_CTRL);
Russell King2b2a87a2014-03-16 17:19:21 +000087}
88
Russell Kingdf5dd4c2014-03-15 16:47:56 +000089static void __l2c_op_way(void __iomem *reg)
90{
91 writel_relaxed(l2x0_way_mask, reg);
Russell King83841fe2014-03-15 16:48:14 +000092 l2c_wait_mask(reg, l2x0_way_mask);
Russell Kingdf5dd4c2014-03-15 16:47:56 +000093}
94
Russell King37abcdb2014-03-15 16:47:50 +000095static inline void l2c_unlock(void __iomem *base, unsigned num)
96{
97 unsigned i;
98
99 for (i = 0; i < num; i++) {
100 writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_D_BASE +
101 i * L2X0_LOCKDOWN_STRIDE);
102 writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_I_BASE +
103 i * L2X0_LOCKDOWN_STRIDE);
104 }
105}
106
Russell King3b8bad52014-03-15 16:47:57 +0000107/*
108 * Enable the L2 cache controller. This function must only be
109 * called when the cache controller is known to be disabled.
110 */
111static void l2c_enable(void __iomem *base, u32 aux, unsigned num_lock)
112{
113 unsigned long flags;
114
Russell King8abd2592014-03-16 17:38:08 +0000115 l2c_write_sec(aux, base, L2X0_AUX_CTRL);
Russell King3b8bad52014-03-15 16:47:57 +0000116
Russell King17f3f992014-03-17 17:15:02 +0000117 l2c_unlock(base, num_lock);
118
Russell King3b8bad52014-03-15 16:47:57 +0000119 local_irq_save(flags);
120 __l2c_op_way(base + L2X0_INV_WAY);
121 writel_relaxed(0, base + sync_reg_offset);
122 l2c_wait_mask(base + sync_reg_offset, 1);
123 local_irq_restore(flags);
124
Russell King8abd2592014-03-16 17:38:08 +0000125 l2c_write_sec(L2X0_CTRL_EN, base, L2X0_CTRL);
Russell King3b8bad52014-03-15 16:47:57 +0000126}
127
128static void l2c_disable(void)
129{
130 void __iomem *base = l2x0_base;
131
132 outer_cache.flush_all();
Russell King8abd2592014-03-16 17:38:08 +0000133 l2c_write_sec(0, base, L2X0_CTRL);
Russell King3b8bad52014-03-15 16:47:57 +0000134 dsb(st);
135}
136
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100137#ifdef CONFIG_CACHE_PL310
138static inline void cache_wait(void __iomem *reg, unsigned long mask)
139{
140 /* cache operations by line are atomic on PL310 */
141}
142#else
Russell King83841fe2014-03-15 16:48:14 +0000143#define cache_wait l2c_wait_mask
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100144#endif
145
Catalin Marinas382266a2007-02-05 14:48:19 +0100146static inline void cache_sync(void)
147{
Russell King3d107432009-11-19 11:41:09 +0000148 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +0100149
Will Deaconf154fe92012-04-20 17:21:08 +0100150 writel_relaxed(0, base + sync_reg_offset);
Russell King3d107432009-11-19 11:41:09 +0000151 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100152}
153
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100154#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Will Deaconab4d5362012-04-20 17:22:11 +0100155static inline void debug_writel(unsigned long val)
156{
Russell King8abd2592014-03-16 17:38:08 +0000157 if (outer_cache.set_debug || outer_cache.write_sec)
Russell King2b2a87a2014-03-16 17:19:21 +0000158 l2c_set_debug(l2x0_base, val);
Will Deaconab4d5362012-04-20 17:22:11 +0100159}
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100160#else
161/* Optimised out for non-errata case */
162static inline void debug_writel(unsigned long val)
163{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100164}
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100165#endif
166
Catalin Marinas23107c52010-03-24 16:48:53 +0100167static void l2x0_cache_sync(void)
168{
169 unsigned long flags;
170
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500171 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100172 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500173 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100174}
175
Will Deacon38a89142011-07-01 14:36:19 +0100176static void __l2x0_flush_all(void)
177{
178 debug_writel(0x03);
Russell Kingdf5dd4c2014-03-15 16:47:56 +0000179 __l2c_op_way(l2x0_base + L2X0_CLEAN_INV_WAY);
Will Deacon38a89142011-07-01 14:36:19 +0100180 cache_sync();
181 debug_writel(0x00);
182}
183
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530184static void l2x0_flush_all(void)
185{
186 unsigned long flags;
187
188 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500189 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100190 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500191 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530192}
193
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530194static void l2x0_disable(void)
195{
196 unsigned long flags;
197
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500198 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100199 __l2x0_flush_all();
Russell King8abd2592014-03-16 17:38:08 +0000200 l2c_write_sec(0, l2x0_base, L2X0_CTRL);
Will Deacon9781aa82013-06-12 09:59:59 +0100201 dsb(st);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500202 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530203}
204
Russell King75461f52014-03-15 16:48:07 +0000205/*
Russell King6a28cf52014-03-15 18:55:53 +0000206 * L2C-210 specific code.
207 *
208 * The L2C-2x0 PA, set/way and sync operations are atomic, but we must
209 * ensure that no background operation is running. The way operations
210 * are all background tasks.
211 *
212 * While a background operation is in progress, any new operation is
213 * ignored (unspecified whether this causes an error.) Thankfully, not
214 * used on SMP.
215 *
216 * Never has a different sync register other than L2X0_CACHE_SYNC, but
217 * we use sync_reg_offset here so we can share some of this with L2C-310.
218 */
219static void __l2c210_cache_sync(void __iomem *base)
220{
221 writel_relaxed(0, base + sync_reg_offset);
222}
223
224static void __l2c210_op_pa_range(void __iomem *reg, unsigned long start,
225 unsigned long end)
226{
227 while (start < end) {
228 writel_relaxed(start, reg);
229 start += CACHE_LINE_SIZE;
230 }
231}
232
233static void l2c210_inv_range(unsigned long start, unsigned long end)
234{
235 void __iomem *base = l2x0_base;
236
237 if (start & (CACHE_LINE_SIZE - 1)) {
238 start &= ~(CACHE_LINE_SIZE - 1);
239 writel_relaxed(start, base + L2X0_CLEAN_INV_LINE_PA);
240 start += CACHE_LINE_SIZE;
241 }
242
243 if (end & (CACHE_LINE_SIZE - 1)) {
244 end &= ~(CACHE_LINE_SIZE - 1);
245 writel_relaxed(end, base + L2X0_CLEAN_INV_LINE_PA);
246 }
247
248 __l2c210_op_pa_range(base + L2X0_INV_LINE_PA, start, end);
249 __l2c210_cache_sync(base);
250}
251
252static void l2c210_clean_range(unsigned long start, unsigned long end)
253{
254 void __iomem *base = l2x0_base;
255
256 start &= ~(CACHE_LINE_SIZE - 1);
257 __l2c210_op_pa_range(base + L2X0_CLEAN_LINE_PA, start, end);
258 __l2c210_cache_sync(base);
259}
260
261static void l2c210_flush_range(unsigned long start, unsigned long end)
262{
263 void __iomem *base = l2x0_base;
264
265 start &= ~(CACHE_LINE_SIZE - 1);
266 __l2c210_op_pa_range(base + L2X0_CLEAN_INV_LINE_PA, start, end);
267 __l2c210_cache_sync(base);
268}
269
270static void l2c210_flush_all(void)
271{
272 void __iomem *base = l2x0_base;
273
274 BUG_ON(!irqs_disabled());
275
276 __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
277 __l2c210_cache_sync(base);
278}
279
280static void l2c210_sync(void)
281{
282 __l2c210_cache_sync(l2x0_base);
283}
284
285static void l2c210_resume(void)
286{
287 void __iomem *base = l2x0_base;
288
289 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN))
290 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 1);
291}
292
293static const struct l2c_init_data l2c210_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +0000294 .type = "L2C-210",
Russell King0493aef2014-03-15 23:26:24 +0000295 .way_size_0 = SZ_8K,
Russell King6a28cf52014-03-15 18:55:53 +0000296 .num_lock = 1,
297 .enable = l2c_enable,
298 .outer_cache = {
299 .inv_range = l2c210_inv_range,
300 .clean_range = l2c210_clean_range,
301 .flush_range = l2c210_flush_range,
302 .flush_all = l2c210_flush_all,
303 .disable = l2c_disable,
304 .sync = l2c210_sync,
305 .resume = l2c210_resume,
306 },
307};
308
309/*
Russell King733c6bb2014-03-15 21:29:28 +0000310 * L2C-220 specific code.
311 *
312 * All operations are background operations: they have to be waited for.
313 * Conflicting requests generate a slave error (which will cause an
314 * imprecise abort.) Never uses sync_reg_offset, so we hard-code the
315 * sync register here.
316 *
317 * However, we can re-use the l2c210_resume call.
318 */
319static inline void __l2c220_cache_sync(void __iomem *base)
320{
321 writel_relaxed(0, base + L2X0_CACHE_SYNC);
322 l2c_wait_mask(base + L2X0_CACHE_SYNC, 1);
323}
324
325static void l2c220_op_way(void __iomem *base, unsigned reg)
326{
327 unsigned long flags;
328
329 raw_spin_lock_irqsave(&l2x0_lock, flags);
330 __l2c_op_way(base + reg);
331 __l2c220_cache_sync(base);
332 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
333}
334
335static unsigned long l2c220_op_pa_range(void __iomem *reg, unsigned long start,
336 unsigned long end, unsigned long flags)
337{
338 raw_spinlock_t *lock = &l2x0_lock;
339
340 while (start < end) {
341 unsigned long blk_end = start + min(end - start, 4096UL);
342
343 while (start < blk_end) {
344 l2c_wait_mask(reg, 1);
345 writel_relaxed(start, reg);
346 start += CACHE_LINE_SIZE;
347 }
348
349 if (blk_end < end) {
350 raw_spin_unlock_irqrestore(lock, flags);
351 raw_spin_lock_irqsave(lock, flags);
352 }
353 }
354
355 return flags;
356}
357
358static void l2c220_inv_range(unsigned long start, unsigned long end)
359{
360 void __iomem *base = l2x0_base;
361 unsigned long flags;
362
363 raw_spin_lock_irqsave(&l2x0_lock, flags);
364 if ((start | end) & (CACHE_LINE_SIZE - 1)) {
365 if (start & (CACHE_LINE_SIZE - 1)) {
366 start &= ~(CACHE_LINE_SIZE - 1);
367 writel_relaxed(start, base + L2X0_CLEAN_INV_LINE_PA);
368 start += CACHE_LINE_SIZE;
369 }
370
371 if (end & (CACHE_LINE_SIZE - 1)) {
372 end &= ~(CACHE_LINE_SIZE - 1);
373 l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
374 writel_relaxed(end, base + L2X0_CLEAN_INV_LINE_PA);
375 }
376 }
377
378 flags = l2c220_op_pa_range(base + L2X0_INV_LINE_PA,
379 start, end, flags);
380 l2c_wait_mask(base + L2X0_INV_LINE_PA, 1);
381 __l2c220_cache_sync(base);
382 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
383}
384
385static void l2c220_clean_range(unsigned long start, unsigned long end)
386{
387 void __iomem *base = l2x0_base;
388 unsigned long flags;
389
390 start &= ~(CACHE_LINE_SIZE - 1);
391 if ((end - start) >= l2x0_size) {
392 l2c220_op_way(base, L2X0_CLEAN_WAY);
393 return;
394 }
395
396 raw_spin_lock_irqsave(&l2x0_lock, flags);
397 flags = l2c220_op_pa_range(base + L2X0_CLEAN_LINE_PA,
398 start, end, flags);
399 l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
400 __l2c220_cache_sync(base);
401 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
402}
403
404static void l2c220_flush_range(unsigned long start, unsigned long end)
405{
406 void __iomem *base = l2x0_base;
407 unsigned long flags;
408
409 start &= ~(CACHE_LINE_SIZE - 1);
410 if ((end - start) >= l2x0_size) {
411 l2c220_op_way(base, L2X0_CLEAN_INV_WAY);
412 return;
413 }
414
415 raw_spin_lock_irqsave(&l2x0_lock, flags);
416 flags = l2c220_op_pa_range(base + L2X0_CLEAN_INV_LINE_PA,
417 start, end, flags);
418 l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
419 __l2c220_cache_sync(base);
420 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
421}
422
423static void l2c220_flush_all(void)
424{
425 l2c220_op_way(l2x0_base, L2X0_CLEAN_INV_WAY);
426}
427
428static void l2c220_sync(void)
429{
430 unsigned long flags;
431
432 raw_spin_lock_irqsave(&l2x0_lock, flags);
433 __l2c220_cache_sync(l2x0_base);
434 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
435}
436
437static const struct l2c_init_data l2c220_data = {
Russell King051334b2014-03-15 23:04:10 +0000438 .type = "L2C-220",
Russell King0493aef2014-03-15 23:26:24 +0000439 .way_size_0 = SZ_8K,
Russell King733c6bb2014-03-15 21:29:28 +0000440 .num_lock = 1,
441 .enable = l2c_enable,
442 .outer_cache = {
443 .inv_range = l2c220_inv_range,
444 .clean_range = l2c220_clean_range,
445 .flush_range = l2c220_flush_range,
446 .flush_all = l2c220_flush_all,
447 .disable = l2c_disable,
448 .sync = l2c220_sync,
449 .resume = l2c210_resume,
450 },
451};
452
453/*
Russell King75461f52014-03-15 16:48:07 +0000454 * L2C-310 specific code.
455 *
Russell Kingf7773322014-03-15 20:51:47 +0000456 * Very similar to L2C-210, the PA, set/way and sync operations are atomic,
457 * and the way operations are all background tasks. However, issuing an
458 * operation while a background operation is in progress results in a
459 * SLVERR response. We can reuse:
460 *
461 * __l2c210_cache_sync (using sync_reg_offset)
462 * l2c210_sync
463 * l2c210_inv_range (if 588369 is not applicable)
464 * l2c210_clean_range
465 * l2c210_flush_range (if 588369 is not applicable)
466 * l2c210_flush_all (if 727915 is not applicable)
467 *
Russell King75461f52014-03-15 16:48:07 +0000468 * Errata:
469 * 588369: PL310 R0P0->R1P0, fixed R2P0.
470 * Affects: all clean+invalidate operations
471 * clean and invalidate skips the invalidate step, so we need to issue
472 * separate operations. We also require the above debug workaround
473 * enclosing this code fragment on affected parts. On unaffected parts,
474 * we must not use this workaround without the debug register writes
475 * to avoid exposing a problem similar to 727915.
476 *
477 * 727915: PL310 R2P0->R3P0, fixed R3P1.
478 * Affects: clean+invalidate by way
479 * clean and invalidate by way runs in the background, and a store can
480 * hit the line between the clean operation and invalidate operation,
481 * resulting in the store being lost.
482 *
Russell Kinga8875a02014-03-16 20:02:06 +0000483 * 752271: PL310 R3P0->R3P1-50REL0, fixed R3P2.
484 * Affects: 8x64-bit (double fill) line fetches
485 * double fill line fetches can fail to cause dirty data to be evicted
486 * from the cache before the new data overwrites the second line.
487 *
Russell King75461f52014-03-15 16:48:07 +0000488 * 753970: PL310 R3P0, fixed R3P1.
489 * Affects: sync
490 * prevents merging writes after the sync operation, until another L2C
491 * operation is performed (or a number of other conditions.)
492 *
493 * 769419: PL310 R0P0->R3P1, fixed R3P2.
494 * Affects: store buffer
495 * store buffer is not automatically drained.
496 */
Russell Kingbda0b742014-03-15 16:48:16 +0000497static void l2c310_set_debug(unsigned long val)
498{
499 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
500}
501
Russell Kingebd4219f2014-03-15 19:08:11 +0000502static void l2c310_inv_range_erratum(unsigned long start, unsigned long end)
503{
504 void __iomem *base = l2x0_base;
505
506 if ((start | end) & (CACHE_LINE_SIZE - 1)) {
507 unsigned long flags;
508
509 /* Erratum 588369 for both clean+invalidate operations */
510 raw_spin_lock_irqsave(&l2x0_lock, flags);
511 l2c_set_debug(base, 0x03);
512
513 if (start & (CACHE_LINE_SIZE - 1)) {
514 start &= ~(CACHE_LINE_SIZE - 1);
515 writel_relaxed(start, base + L2X0_CLEAN_LINE_PA);
516 writel_relaxed(start, base + L2X0_INV_LINE_PA);
517 start += CACHE_LINE_SIZE;
518 }
519
520 if (end & (CACHE_LINE_SIZE - 1)) {
521 end &= ~(CACHE_LINE_SIZE - 1);
522 writel_relaxed(end, base + L2X0_CLEAN_LINE_PA);
523 writel_relaxed(end, base + L2X0_INV_LINE_PA);
524 }
525
526 l2c_set_debug(base, 0x00);
527 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
528 }
529
530 __l2c210_op_pa_range(base + L2X0_INV_LINE_PA, start, end);
531 __l2c210_cache_sync(base);
532}
533
534static void l2c310_flush_range_erratum(unsigned long start, unsigned long end)
535{
536 raw_spinlock_t *lock = &l2x0_lock;
537 unsigned long flags;
538 void __iomem *base = l2x0_base;
539
540 raw_spin_lock_irqsave(lock, flags);
541 while (start < end) {
542 unsigned long blk_end = start + min(end - start, 4096UL);
543
544 l2c_set_debug(base, 0x03);
545 while (start < blk_end) {
546 writel_relaxed(start, base + L2X0_CLEAN_LINE_PA);
547 writel_relaxed(start, base + L2X0_INV_LINE_PA);
548 start += CACHE_LINE_SIZE;
549 }
550 l2c_set_debug(base, 0x00);
551
552 if (blk_end < end) {
553 raw_spin_unlock_irqrestore(lock, flags);
554 raw_spin_lock_irqsave(lock, flags);
555 }
556 }
557 raw_spin_unlock_irqrestore(lock, flags);
558 __l2c210_cache_sync(base);
559}
560
Russell King99ca17722014-03-15 16:48:18 +0000561static void l2c310_flush_all_erratum(void)
562{
563 void __iomem *base = l2x0_base;
564 unsigned long flags;
565
566 raw_spin_lock_irqsave(&l2x0_lock, flags);
567 l2c_set_debug(base, 0x03);
568 __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
569 l2c_set_debug(base, 0x00);
570 __l2c210_cache_sync(base);
571 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
572}
573
Russell King09a5d182014-03-15 16:48:13 +0000574static void __init l2c310_save(void __iomem *base)
Russell Kingb98556f22014-03-15 16:48:11 +0000575{
Russell King09a5d182014-03-15 16:48:13 +0000576 unsigned revision;
Russell Kingb98556f22014-03-15 16:48:11 +0000577
578 l2x0_saved_regs.tag_latency = readl_relaxed(base +
579 L2X0_TAG_LATENCY_CTRL);
580 l2x0_saved_regs.data_latency = readl_relaxed(base +
581 L2X0_DATA_LATENCY_CTRL);
582 l2x0_saved_regs.filter_end = readl_relaxed(base +
583 L2X0_ADDR_FILTER_END);
584 l2x0_saved_regs.filter_start = readl_relaxed(base +
585 L2X0_ADDR_FILTER_START);
586
Russell King09a5d182014-03-15 16:48:13 +0000587 revision = readl_relaxed(base + L2X0_CACHE_ID) &
Russell Kingb98556f22014-03-15 16:48:11 +0000588 L2X0_CACHE_ID_RTL_MASK;
589
Russell King09a5d182014-03-15 16:48:13 +0000590 /* From r2p0, there is Prefetch offset/control register */
591 if (revision >= L310_CACHE_ID_RTL_R2P0)
592 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(base +
593 L2X0_PREFETCH_CTRL);
Russell Kingb98556f22014-03-15 16:48:11 +0000594
Russell King09a5d182014-03-15 16:48:13 +0000595 /* From r3p0, there is Power control register */
596 if (revision >= L310_CACHE_ID_RTL_R3P0)
597 l2x0_saved_regs.pwr_ctrl = readl_relaxed(base +
598 L2X0_POWER_CTRL);
599}
600
601static void l2c310_resume(void)
602{
603 void __iomem *base = l2x0_base;
604
605 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
606 unsigned revision;
607
608 /* restore pl310 setup */
609 writel_relaxed(l2x0_saved_regs.tag_latency,
610 base + L2X0_TAG_LATENCY_CTRL);
611 writel_relaxed(l2x0_saved_regs.data_latency,
612 base + L2X0_DATA_LATENCY_CTRL);
613 writel_relaxed(l2x0_saved_regs.filter_end,
614 base + L2X0_ADDR_FILTER_END);
615 writel_relaxed(l2x0_saved_regs.filter_start,
616 base + L2X0_ADDR_FILTER_START);
617
618 revision = readl_relaxed(base + L2X0_CACHE_ID) &
619 L2X0_CACHE_ID_RTL_MASK;
620
621 if (revision >= L310_CACHE_ID_RTL_R2P0)
Russell King8abd2592014-03-16 17:38:08 +0000622 l2c_write_sec(l2x0_saved_regs.prefetch_ctrl, base,
623 L2X0_PREFETCH_CTRL);
Russell King09a5d182014-03-15 16:48:13 +0000624 if (revision >= L310_CACHE_ID_RTL_R3P0)
Russell King8abd2592014-03-16 17:38:08 +0000625 l2c_write_sec(l2x0_saved_regs.pwr_ctrl, base,
626 L2X0_POWER_CTRL);
Russell King09a5d182014-03-15 16:48:13 +0000627
628 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
629 }
Russell Kingb98556f22014-03-15 16:48:11 +0000630}
631
Russell King75461f52014-03-15 16:48:07 +0000632static void __init l2c310_fixup(void __iomem *base, u32 cache_id,
633 struct outer_cache_fns *fns)
634{
635 unsigned revision = cache_id & L2X0_CACHE_ID_RTL_MASK;
Russell Kinga8875a02014-03-16 20:02:06 +0000636 const char *errata[8];
Russell King75461f52014-03-15 16:48:07 +0000637 unsigned n = 0;
638
Russell Kingebd4219f2014-03-15 19:08:11 +0000639 /* For compatibility */
Russell King75461f52014-03-15 16:48:07 +0000640 if (revision <= L310_CACHE_ID_RTL_R3P0)
Russell Kingbda0b742014-03-15 16:48:16 +0000641 fns->set_debug = l2c310_set_debug;
Russell King75461f52014-03-15 16:48:07 +0000642
Russell Kingebd4219f2014-03-15 19:08:11 +0000643 if (IS_ENABLED(CONFIG_PL310_ERRATA_588369) &&
644 revision < L310_CACHE_ID_RTL_R2P0 &&
645 /* For bcm compatibility */
Russell Kingf7773322014-03-15 20:51:47 +0000646 fns->inv_range == l2c210_inv_range) {
Russell Kingebd4219f2014-03-15 19:08:11 +0000647 fns->inv_range = l2c310_inv_range_erratum;
648 fns->flush_range = l2c310_flush_range_erratum;
649 errata[n++] = "588369";
650 }
651
Russell King99ca17722014-03-15 16:48:18 +0000652 if (IS_ENABLED(CONFIG_PL310_ERRATA_727915) &&
653 revision >= L310_CACHE_ID_RTL_R2P0 &&
654 revision < L310_CACHE_ID_RTL_R3P1) {
655 fns->flush_all = l2c310_flush_all_erratum;
656 errata[n++] = "727915";
657 }
658
Russell Kinga8875a02014-03-16 20:02:06 +0000659 if (revision >= L310_CACHE_ID_RTL_R3P0 &&
660 revision < L310_CACHE_ID_RTL_R3P2) {
661 u32 val = readl_relaxed(base + L2X0_PREFETCH_CTRL);
662 /* I don't think bit23 is required here... but iMX6 does so */
663 if (val & (BIT(30) | BIT(23))) {
664 val &= ~(BIT(30) | BIT(23));
665 l2c_write_sec(val, base, L2X0_PREFETCH_CTRL);
666 errata[n++] = "752271";
667 }
668 }
669
Russell King75461f52014-03-15 16:48:07 +0000670 if (IS_ENABLED(CONFIG_PL310_ERRATA_753970) &&
671 revision == L310_CACHE_ID_RTL_R3P0) {
672 sync_reg_offset = L2X0_DUMMY_REG;
673 errata[n++] = "753970";
674 }
675
676 if (IS_ENABLED(CONFIG_PL310_ERRATA_769419))
677 errata[n++] = "769419";
678
679 if (n) {
680 unsigned i;
681
682 pr_info("L2C-310 errat%s", n > 1 ? "a" : "um");
683 for (i = 0; i < n; i++)
684 pr_cont(" %s", errata[i]);
685 pr_cont(" enabled\n");
686 }
687}
688
689static const struct l2c_init_data l2c310_init_fns __initconst = {
Russell King051334b2014-03-15 23:04:10 +0000690 .type = "L2C-310",
Russell King0493aef2014-03-15 23:26:24 +0000691 .way_size_0 = SZ_8K,
Russell King75461f52014-03-15 16:48:07 +0000692 .num_lock = 8,
693 .enable = l2c_enable,
694 .fixup = l2c310_fixup,
Russell King09a5d182014-03-15 16:48:13 +0000695 .save = l2c310_save,
Russell King75461f52014-03-15 16:48:07 +0000696 .outer_cache = {
Russell Kingf7773322014-03-15 20:51:47 +0000697 .inv_range = l2c210_inv_range,
698 .clean_range = l2c210_clean_range,
699 .flush_range = l2c210_flush_range,
700 .flush_all = l2c210_flush_all,
701 .disable = l2c_disable,
702 .sync = l2c210_sync,
703 .set_debug = l2c310_set_debug,
Russell King09a5d182014-03-15 16:48:13 +0000704 .resume = l2c310_resume,
Russell King75461f52014-03-15 16:48:07 +0000705 },
706};
707
Russell King96054b02014-03-15 16:47:52 +0000708static void __init __l2c_init(const struct l2c_init_data *data,
709 u32 aux_val, u32 aux_mask, u32 cache_id)
Catalin Marinas382266a2007-02-05 14:48:19 +0100710{
Russell King75461f52014-03-15 16:48:07 +0000711 struct outer_cache_fns fns;
Russell King0493aef2014-03-15 23:26:24 +0000712 unsigned way_size_bits, ways;
Russell King3e175ca2011-09-18 11:27:30 +0100713 u32 aux;
Catalin Marinas382266a2007-02-05 14:48:19 +0100714
Russell Kingc40e7eb2014-03-15 16:48:04 +0000715 /*
716 * It is strange to save the register state before initialisation,
717 * but hey, this is what the DT implementations decided to do.
718 */
719 if (data->save)
720 data->save(l2x0_base);
721
Catalin Marinas6775a552010-07-28 22:01:25 +0100722 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100723
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100724 aux &= aux_mask;
725 aux |= aux_val;
726
Jason McMullan64039be2010-05-05 18:59:37 +0100727 /* Determine the number of ways */
Rob Herring6e7acee2013-03-25 17:02:48 +0100728 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100729 case L2X0_CACHE_ID_PART_L310:
730 if (aux & (1 << 16))
731 ways = 16;
732 else
733 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100734 break;
Russell King75461f52014-03-15 16:48:07 +0000735
Jason McMullan64039be2010-05-05 18:59:37 +0100736 case L2X0_CACHE_ID_PART_L210:
Russell King5f47c382014-03-15 23:07:07 +0000737 case L2X0_CACHE_ID_PART_L220:
Jason McMullan64039be2010-05-05 18:59:37 +0100738 ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100739 break;
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100740
741 case AURORA_CACHE_ID:
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100742 ways = (aux >> 13) & 0xf;
743 ways = 2 << ((ways + 1) >> 2);
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100744 break;
Russell King75461f52014-03-15 16:48:07 +0000745
Jason McMullan64039be2010-05-05 18:59:37 +0100746 default:
747 /* Assume unknown chips have 8 ways */
748 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100749 break;
750 }
751
752 l2x0_way_mask = (1 << ways) - 1;
753
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100754 /*
Russell King0493aef2014-03-15 23:26:24 +0000755 * way_size_0 is the size that a way_size value of zero would be
756 * given the calculation: way_size = way_size_0 << way_size_bits.
757 * So, if way_size_bits=0 is reserved, but way_size_bits=1 is 16k,
758 * then way_size_0 would be 8k.
759 *
760 * L2 cache size = number of ways * way size.
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530761 */
Russell King0493aef2014-03-15 23:26:24 +0000762 way_size_bits = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
763 l2x0_size = ways * (data->way_size_0 << way_size_bits);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530764
Russell King75461f52014-03-15 16:48:07 +0000765 fns = data->outer_cache;
Russell King8abd2592014-03-16 17:38:08 +0000766 fns.write_sec = outer_cache.write_sec;
Russell King75461f52014-03-15 16:48:07 +0000767 if (data->fixup)
768 data->fixup(l2x0_base, cache_id, &fns);
Russell King8abd2592014-03-16 17:38:08 +0000769 if (fns.write_sec)
770 fns.set_debug = NULL;
Russell King75461f52014-03-15 16:48:07 +0000771
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530772 /*
Russell King3b8bad52014-03-15 16:47:57 +0000773 * Check if l2x0 controller is already enabled. If we are booting
774 * in non-secure mode accessing the below registers will fault.
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100775 */
Russell King3b8bad52014-03-15 16:47:57 +0000776 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
777 data->enable(l2x0_base, aux, data->num_lock);
Catalin Marinas382266a2007-02-05 14:48:19 +0100778
Yilu Mao9d4876f2012-09-03 09:14:56 +0100779 /* Re-read it in case some bits are reserved. */
780 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
781
782 /* Save the value for resuming. */
783 l2x0_saved_regs.aux_ctrl = aux;
784
Russell King75461f52014-03-15 16:48:07 +0000785 outer_cache = fns;
Catalin Marinas382266a2007-02-05 14:48:19 +0100786
Russell Kingcdef8682014-03-15 16:48:08 +0000787 pr_info("%s cache controller enabled, %d ways, %d kB\n",
Russell King051334b2014-03-15 23:04:10 +0000788 data->type, ways, l2x0_size >> 10);
Russell Kingcdef8682014-03-15 16:48:08 +0000789 pr_info("%s: CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
Russell King051334b2014-03-15 23:04:10 +0000790 data->type, cache_id, aux);
Catalin Marinas382266a2007-02-05 14:48:19 +0100791}
Rob Herring8c369262011-08-03 18:12:05 +0100792
Russell King96054b02014-03-15 16:47:52 +0000793void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
794{
Russell King75461f52014-03-15 16:48:07 +0000795 const struct l2c_init_data *data;
Russell King96054b02014-03-15 16:47:52 +0000796 u32 cache_id;
797
798 l2x0_base = base;
799
800 cache_id = readl_relaxed(base + L2X0_CACHE_ID);
801
Russell King75461f52014-03-15 16:48:07 +0000802 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
803 default:
Russell King6a28cf52014-03-15 18:55:53 +0000804 case L2X0_CACHE_ID_PART_L210:
805 data = &l2c210_data;
806 break;
807
Russell King733c6bb2014-03-15 21:29:28 +0000808 case L2X0_CACHE_ID_PART_L220:
809 data = &l2c220_data;
810 break;
811
Russell King75461f52014-03-15 16:48:07 +0000812 case L2X0_CACHE_ID_PART_L310:
813 data = &l2c310_init_fns;
814 break;
815 }
816
817 __l2c_init(data, aux_val, aux_mask, cache_id);
Russell King96054b02014-03-15 16:47:52 +0000818}
819
Rob Herring8c369262011-08-03 18:12:05 +0100820#ifdef CONFIG_OF
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100821static int l2_wt_override;
822
Russell King96054b02014-03-15 16:47:52 +0000823/* Aurora don't have the cache ID register available, so we have to
824 * pass it though the device tree */
825static u32 cache_id_part_number_from_dt;
826
Russell Kingda3627f2014-03-15 16:48:06 +0000827static void __init l2x0_of_parse(const struct device_node *np,
828 u32 *aux_val, u32 *aux_mask)
829{
830 u32 data[2] = { 0, 0 };
831 u32 tag = 0;
832 u32 dirty = 0;
833 u32 val = 0, mask = 0;
834
835 of_property_read_u32(np, "arm,tag-latency", &tag);
836 if (tag) {
837 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
838 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
839 }
840
841 of_property_read_u32_array(np, "arm,data-latency",
842 data, ARRAY_SIZE(data));
843 if (data[0] && data[1]) {
844 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
845 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
846 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
847 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
848 }
849
850 of_property_read_u32(np, "arm,dirty-latency", &dirty);
851 if (dirty) {
852 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
853 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
854 }
855
856 *aux_val &= ~mask;
857 *aux_val |= val;
858 *aux_mask &= ~mask;
859}
860
Russell King6a28cf52014-03-15 18:55:53 +0000861static const struct l2c_init_data of_l2c210_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +0000862 .type = "L2C-210",
Russell King0493aef2014-03-15 23:26:24 +0000863 .way_size_0 = SZ_8K,
Russell King6a28cf52014-03-15 18:55:53 +0000864 .num_lock = 1,
865 .of_parse = l2x0_of_parse,
866 .enable = l2c_enable,
867 .outer_cache = {
868 .inv_range = l2c210_inv_range,
869 .clean_range = l2c210_clean_range,
870 .flush_range = l2c210_flush_range,
871 .flush_all = l2c210_flush_all,
872 .disable = l2c_disable,
873 .sync = l2c210_sync,
874 .resume = l2c210_resume,
875 },
876};
877
Russell King733c6bb2014-03-15 21:29:28 +0000878static const struct l2c_init_data of_l2c220_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +0000879 .type = "L2C-220",
Russell King0493aef2014-03-15 23:26:24 +0000880 .way_size_0 = SZ_8K,
Russell King733c6bb2014-03-15 21:29:28 +0000881 .num_lock = 1,
Russell Kingda3627f2014-03-15 16:48:06 +0000882 .of_parse = l2x0_of_parse,
Russell King733c6bb2014-03-15 21:29:28 +0000883 .enable = l2c_enable,
Russell Kingda3627f2014-03-15 16:48:06 +0000884 .outer_cache = {
Russell King733c6bb2014-03-15 21:29:28 +0000885 .inv_range = l2c220_inv_range,
886 .clean_range = l2c220_clean_range,
887 .flush_range = l2c220_flush_range,
888 .flush_all = l2c220_flush_all,
889 .disable = l2c_disable,
890 .sync = l2c220_sync,
891 .resume = l2c210_resume,
Russell Kingda3627f2014-03-15 16:48:06 +0000892 },
893};
894
Russell Kingf7773322014-03-15 20:51:47 +0000895static void __init l2c310_of_parse(const struct device_node *np,
896 u32 *aux_val, u32 *aux_mask)
Russell Kingda3627f2014-03-15 16:48:06 +0000897{
898 u32 data[3] = { 0, 0, 0 };
899 u32 tag[3] = { 0, 0, 0 };
900 u32 filter[2] = { 0, 0 };
901
902 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
903 if (tag[0] && tag[1] && tag[2])
904 writel_relaxed(
905 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
906 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
907 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
908 l2x0_base + L2X0_TAG_LATENCY_CTRL);
909
910 of_property_read_u32_array(np, "arm,data-latency",
911 data, ARRAY_SIZE(data));
912 if (data[0] && data[1] && data[2])
913 writel_relaxed(
914 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
915 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
916 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
917 l2x0_base + L2X0_DATA_LATENCY_CTRL);
918
919 of_property_read_u32_array(np, "arm,filter-ranges",
920 filter, ARRAY_SIZE(filter));
921 if (filter[1]) {
922 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
923 l2x0_base + L2X0_ADDR_FILTER_END);
924 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
925 l2x0_base + L2X0_ADDR_FILTER_START);
926 }
927}
928
Russell Kingf7773322014-03-15 20:51:47 +0000929static const struct l2c_init_data of_l2c310_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +0000930 .type = "L2C-310",
Russell King0493aef2014-03-15 23:26:24 +0000931 .way_size_0 = SZ_8K,
Russell King3b8bad52014-03-15 16:47:57 +0000932 .num_lock = 8,
Russell Kingf7773322014-03-15 20:51:47 +0000933 .of_parse = l2c310_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +0000934 .enable = l2c_enable,
Russell King75461f52014-03-15 16:48:07 +0000935 .fixup = l2c310_fixup,
Russell King09a5d182014-03-15 16:48:13 +0000936 .save = l2c310_save,
Russell Kingda3627f2014-03-15 16:48:06 +0000937 .outer_cache = {
Russell Kingf7773322014-03-15 20:51:47 +0000938 .inv_range = l2c210_inv_range,
939 .clean_range = l2c210_clean_range,
940 .flush_range = l2c210_flush_range,
941 .flush_all = l2c210_flush_all,
942 .disable = l2c_disable,
943 .sync = l2c210_sync,
944 .set_debug = l2c310_set_debug,
Russell King09a5d182014-03-15 16:48:13 +0000945 .resume = l2c310_resume,
Russell Kingda3627f2014-03-15 16:48:06 +0000946 },
947};
948
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100949/*
950 * Note that the end addresses passed to Linux primitives are
951 * noninclusive, while the hardware cache range operations use
952 * inclusive start and end addresses.
953 */
954static unsigned long calc_range_end(unsigned long start, unsigned long end)
955{
956 /*
957 * Limit the number of cache lines processed at once,
958 * since cache range operations stall the CPU pipeline
959 * until completion.
960 */
961 if (end > start + MAX_RANGE_SIZE)
962 end = start + MAX_RANGE_SIZE;
963
964 /*
965 * Cache range operations can't straddle a page boundary.
966 */
967 if (end > PAGE_ALIGN(start+1))
968 end = PAGE_ALIGN(start+1);
969
970 return end;
971}
972
973/*
974 * Make sure 'start' and 'end' reference the same page, as L2 is PIPT
975 * and range operations only do a TLB lookup on the start address.
976 */
977static void aurora_pa_range(unsigned long start, unsigned long end,
978 unsigned long offset)
979{
980 unsigned long flags;
981
982 raw_spin_lock_irqsave(&l2x0_lock, flags);
Gregory CLEMENT8a3a1802013-01-07 11:28:42 +0100983 writel_relaxed(start, l2x0_base + AURORA_RANGE_BASE_ADDR_REG);
984 writel_relaxed(end, l2x0_base + offset);
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100985 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
986
987 cache_sync();
988}
989
990static void aurora_inv_range(unsigned long start, unsigned long end)
991{
992 /*
993 * round start and end adresses up to cache line size
994 */
995 start &= ~(CACHE_LINE_SIZE - 1);
996 end = ALIGN(end, CACHE_LINE_SIZE);
997
998 /*
999 * Invalidate all full cache lines between 'start' and 'end'.
1000 */
1001 while (start < end) {
1002 unsigned long range_end = calc_range_end(start, end);
1003 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1004 AURORA_INVAL_RANGE_REG);
1005 start = range_end;
1006 }
1007}
1008
1009static void aurora_clean_range(unsigned long start, unsigned long end)
1010{
1011 /*
1012 * If L2 is forced to WT, the L2 will always be clean and we
1013 * don't need to do anything here.
1014 */
1015 if (!l2_wt_override) {
1016 start &= ~(CACHE_LINE_SIZE - 1);
1017 end = ALIGN(end, CACHE_LINE_SIZE);
1018 while (start != end) {
1019 unsigned long range_end = calc_range_end(start, end);
1020 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1021 AURORA_CLEAN_RANGE_REG);
1022 start = range_end;
1023 }
1024 }
1025}
1026
1027static void aurora_flush_range(unsigned long start, unsigned long end)
1028{
Gregory CLEMENT8b827c62013-01-07 11:27:14 +01001029 start &= ~(CACHE_LINE_SIZE - 1);
1030 end = ALIGN(end, CACHE_LINE_SIZE);
1031 while (start != end) {
1032 unsigned long range_end = calc_range_end(start, end);
1033 /*
1034 * If L2 is forced to WT, the L2 will always be clean and we
1035 * just need to invalidate.
1036 */
1037 if (l2_wt_override)
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +01001038 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
Gregory CLEMENT8b827c62013-01-07 11:27:14 +01001039 AURORA_INVAL_RANGE_REG);
1040 else
1041 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1042 AURORA_FLUSH_RANGE_REG);
1043 start = range_end;
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +01001044 }
1045}
1046
Russell Kingda3627f2014-03-15 16:48:06 +00001047static void aurora_save(void __iomem *base)
1048{
1049 l2x0_saved_regs.ctrl = readl_relaxed(base + L2X0_CTRL);
1050 l2x0_saved_regs.aux_ctrl = readl_relaxed(base + L2X0_AUX_CTRL);
1051}
1052
1053static void aurora_resume(void)
1054{
Russell King09a5d182014-03-15 16:48:13 +00001055 void __iomem *base = l2x0_base;
1056
1057 if (!(readl(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
1058 writel_relaxed(l2x0_saved_regs.aux_ctrl, base + L2X0_AUX_CTRL);
1059 writel_relaxed(l2x0_saved_regs.ctrl, base + L2X0_CTRL);
Russell Kingda3627f2014-03-15 16:48:06 +00001060 }
1061}
1062
Russell King40266d62014-03-15 16:47:59 +00001063/*
1064 * For Aurora cache in no outer mode, enable via the CP15 coprocessor
1065 * broadcasting of cache commands to L2.
1066 */
1067static void __init aurora_enable_no_outer(void __iomem *base, u32 aux,
1068 unsigned num_lock)
Russell Kingda3627f2014-03-15 16:48:06 +00001069{
Russell King40266d62014-03-15 16:47:59 +00001070 u32 u;
1071
1072 asm volatile("mrc p15, 1, %0, c15, c2, 0" : "=r" (u));
Russell Kingda3627f2014-03-15 16:48:06 +00001073 u |= AURORA_CTRL_FW; /* Set the FW bit */
Russell King40266d62014-03-15 16:47:59 +00001074 asm volatile("mcr p15, 1, %0, c15, c2, 0" : : "r" (u));
1075
Russell Kingda3627f2014-03-15 16:48:06 +00001076 isb();
Russell King40266d62014-03-15 16:47:59 +00001077
1078 l2c_enable(base, aux, num_lock);
Russell Kingda3627f2014-03-15 16:48:06 +00001079}
1080
Russell King75461f52014-03-15 16:48:07 +00001081static void __init aurora_fixup(void __iomem *base, u32 cache_id,
1082 struct outer_cache_fns *fns)
1083{
1084 sync_reg_offset = AURORA_SYNC_REG;
1085}
1086
Russell Kingda3627f2014-03-15 16:48:06 +00001087static void __init aurora_of_parse(const struct device_node *np,
1088 u32 *aux_val, u32 *aux_mask)
1089{
1090 u32 val = AURORA_ACR_REPLACEMENT_TYPE_SEMIPLRU;
1091 u32 mask = AURORA_ACR_REPLACEMENT_MASK;
1092
1093 of_property_read_u32(np, "cache-id-part",
1094 &cache_id_part_number_from_dt);
1095
1096 /* Determine and save the write policy */
1097 l2_wt_override = of_property_read_bool(np, "wt-override");
1098
1099 if (l2_wt_override) {
1100 val |= AURORA_ACR_FORCE_WRITE_THRO_POLICY;
1101 mask |= AURORA_ACR_FORCE_WRITE_POLICY_MASK;
1102 }
1103
1104 *aux_val &= ~mask;
1105 *aux_val |= val;
1106 *aux_mask &= ~mask;
1107}
1108
1109static const struct l2c_init_data of_aurora_with_outer_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +00001110 .type = "Aurora",
Russell King0493aef2014-03-15 23:26:24 +00001111 .way_size_0 = SZ_4K,
Russell King3b8bad52014-03-15 16:47:57 +00001112 .num_lock = 4,
Russell Kingda3627f2014-03-15 16:48:06 +00001113 .of_parse = aurora_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +00001114 .enable = l2c_enable,
Russell King75461f52014-03-15 16:48:07 +00001115 .fixup = aurora_fixup,
Russell Kingda3627f2014-03-15 16:48:06 +00001116 .save = aurora_save,
1117 .outer_cache = {
1118 .inv_range = aurora_inv_range,
1119 .clean_range = aurora_clean_range,
1120 .flush_range = aurora_flush_range,
1121 .flush_all = l2x0_flush_all,
1122 .disable = l2x0_disable,
1123 .sync = l2x0_cache_sync,
1124 .resume = aurora_resume,
1125 },
1126};
1127
1128static const struct l2c_init_data of_aurora_no_outer_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +00001129 .type = "Aurora",
Russell King0493aef2014-03-15 23:26:24 +00001130 .way_size_0 = SZ_4K,
Russell King3b8bad52014-03-15 16:47:57 +00001131 .num_lock = 4,
Russell Kingda3627f2014-03-15 16:48:06 +00001132 .of_parse = aurora_of_parse,
Russell King40266d62014-03-15 16:47:59 +00001133 .enable = aurora_enable_no_outer,
Russell King75461f52014-03-15 16:48:07 +00001134 .fixup = aurora_fixup,
Russell Kingda3627f2014-03-15 16:48:06 +00001135 .save = aurora_save,
1136 .outer_cache = {
1137 .resume = aurora_resume,
1138 },
1139};
1140
Christian Daudt3b656fe2013-05-09 22:21:01 +01001141/*
1142 * For certain Broadcom SoCs, depending on the address range, different offsets
1143 * need to be added to the address before passing it to L2 for
1144 * invalidation/clean/flush
1145 *
1146 * Section Address Range Offset EMI
1147 * 1 0x00000000 - 0x3FFFFFFF 0x80000000 VC
1148 * 2 0x40000000 - 0xBFFFFFFF 0x40000000 SYS
1149 * 3 0xC0000000 - 0xFFFFFFFF 0x80000000 VC
1150 *
1151 * When the start and end addresses have crossed two different sections, we
1152 * need to break the L2 operation into two, each within its own section.
1153 * For example, if we need to invalidate addresses starts at 0xBFFF0000 and
1154 * ends at 0xC0001000, we need do invalidate 1) 0xBFFF0000 - 0xBFFFFFFF and 2)
1155 * 0xC0000000 - 0xC0001000
1156 *
1157 * Note 1:
1158 * By breaking a single L2 operation into two, we may potentially suffer some
1159 * performance hit, but keep in mind the cross section case is very rare
1160 *
1161 * Note 2:
1162 * We do not need to handle the case when the start address is in
1163 * Section 1 and the end address is in Section 3, since it is not a valid use
1164 * case
1165 *
1166 * Note 3:
1167 * Section 1 in practical terms can no longer be used on rev A2. Because of
1168 * that the code does not need to handle section 1 at all.
1169 *
1170 */
1171#define BCM_SYS_EMI_START_ADDR 0x40000000UL
1172#define BCM_VC_EMI_SEC3_START_ADDR 0xC0000000UL
1173
1174#define BCM_SYS_EMI_OFFSET 0x40000000UL
1175#define BCM_VC_EMI_OFFSET 0x80000000UL
1176
1177static inline int bcm_addr_is_sys_emi(unsigned long addr)
1178{
1179 return (addr >= BCM_SYS_EMI_START_ADDR) &&
1180 (addr < BCM_VC_EMI_SEC3_START_ADDR);
1181}
1182
1183static inline unsigned long bcm_l2_phys_addr(unsigned long addr)
1184{
1185 if (bcm_addr_is_sys_emi(addr))
1186 return addr + BCM_SYS_EMI_OFFSET;
1187 else
1188 return addr + BCM_VC_EMI_OFFSET;
1189}
1190
1191static void bcm_inv_range(unsigned long start, unsigned long end)
1192{
1193 unsigned long new_start, new_end;
1194
1195 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1196
1197 if (unlikely(end <= start))
1198 return;
1199
1200 new_start = bcm_l2_phys_addr(start);
1201 new_end = bcm_l2_phys_addr(end);
1202
1203 /* normal case, no cross section between start and end */
1204 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
Russell King90811142014-03-19 19:14:13 +00001205 l2c210_inv_range(new_start, new_end);
Christian Daudt3b656fe2013-05-09 22:21:01 +01001206 return;
1207 }
1208
1209 /* They cross sections, so it can only be a cross from section
1210 * 2 to section 3
1211 */
Russell King90811142014-03-19 19:14:13 +00001212 l2c210_inv_range(new_start,
Christian Daudt3b656fe2013-05-09 22:21:01 +01001213 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
Russell King90811142014-03-19 19:14:13 +00001214 l2c210_inv_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
Christian Daudt3b656fe2013-05-09 22:21:01 +01001215 new_end);
1216}
1217
1218static void bcm_clean_range(unsigned long start, unsigned long end)
1219{
1220 unsigned long new_start, new_end;
1221
1222 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1223
1224 if (unlikely(end <= start))
1225 return;
1226
Christian Daudt3b656fe2013-05-09 22:21:01 +01001227 new_start = bcm_l2_phys_addr(start);
1228 new_end = bcm_l2_phys_addr(end);
1229
1230 /* normal case, no cross section between start and end */
1231 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
Russell King90811142014-03-19 19:14:13 +00001232 l2c210_clean_range(new_start, new_end);
Christian Daudt3b656fe2013-05-09 22:21:01 +01001233 return;
1234 }
1235
1236 /* They cross sections, so it can only be a cross from section
1237 * 2 to section 3
1238 */
Russell King90811142014-03-19 19:14:13 +00001239 l2c210_clean_range(new_start,
Christian Daudt3b656fe2013-05-09 22:21:01 +01001240 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
Russell King90811142014-03-19 19:14:13 +00001241 l2c210_clean_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
Christian Daudt3b656fe2013-05-09 22:21:01 +01001242 new_end);
1243}
1244
1245static void bcm_flush_range(unsigned long start, unsigned long end)
1246{
1247 unsigned long new_start, new_end;
1248
1249 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1250
1251 if (unlikely(end <= start))
1252 return;
1253
1254 if ((end - start) >= l2x0_size) {
Russell King90811142014-03-19 19:14:13 +00001255 outer_cache.flush_all();
Christian Daudt3b656fe2013-05-09 22:21:01 +01001256 return;
1257 }
1258
1259 new_start = bcm_l2_phys_addr(start);
1260 new_end = bcm_l2_phys_addr(end);
1261
1262 /* normal case, no cross section between start and end */
1263 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
Russell King90811142014-03-19 19:14:13 +00001264 l2c210_flush_range(new_start, new_end);
Christian Daudt3b656fe2013-05-09 22:21:01 +01001265 return;
1266 }
1267
1268 /* They cross sections, so it can only be a cross from section
1269 * 2 to section 3
1270 */
Russell King90811142014-03-19 19:14:13 +00001271 l2c210_flush_range(new_start,
Christian Daudt3b656fe2013-05-09 22:21:01 +01001272 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
Russell King90811142014-03-19 19:14:13 +00001273 l2c210_flush_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
Christian Daudt3b656fe2013-05-09 22:21:01 +01001274 new_end);
1275}
1276
Russell King90811142014-03-19 19:14:13 +00001277/* Broadcom L2C-310 start from ARMs R3P2 or later, and require no fixups */
Russell Kingda3627f2014-03-15 16:48:06 +00001278static const struct l2c_init_data of_bcm_l2x0_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +00001279 .type = "BCM-L2C-310",
Russell King0493aef2014-03-15 23:26:24 +00001280 .way_size_0 = SZ_8K,
Russell King3b8bad52014-03-15 16:47:57 +00001281 .num_lock = 8,
Russell Kingf7773322014-03-15 20:51:47 +00001282 .of_parse = l2c310_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +00001283 .enable = l2c_enable,
Russell King09a5d182014-03-15 16:48:13 +00001284 .save = l2c310_save,
Russell Kingda3627f2014-03-15 16:48:06 +00001285 .outer_cache = {
1286 .inv_range = bcm_inv_range,
1287 .clean_range = bcm_clean_range,
1288 .flush_range = bcm_flush_range,
Russell Kingf7773322014-03-15 20:51:47 +00001289 .flush_all = l2c210_flush_all,
1290 .disable = l2c_disable,
1291 .sync = l2c210_sync,
Russell King09a5d182014-03-15 16:48:13 +00001292 .resume = l2c310_resume,
Russell Kingda3627f2014-03-15 16:48:06 +00001293 },
1294};
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +01001295
Russell King9846dfc2014-03-15 16:47:55 +00001296static void __init tauros3_save(void __iomem *base)
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001297{
1298 l2x0_saved_regs.aux2_ctrl =
Russell King9846dfc2014-03-15 16:47:55 +00001299 readl_relaxed(base + TAUROS3_AUX2_CTRL);
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001300 l2x0_saved_regs.prefetch_ctrl =
Russell King9846dfc2014-03-15 16:47:55 +00001301 readl_relaxed(base + L2X0_PREFETCH_CTRL);
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001302}
1303
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001304static void tauros3_resume(void)
1305{
Russell King09a5d182014-03-15 16:48:13 +00001306 void __iomem *base = l2x0_base;
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001307
Russell King09a5d182014-03-15 16:48:13 +00001308 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
1309 writel_relaxed(l2x0_saved_regs.aux2_ctrl,
1310 base + TAUROS3_AUX2_CTRL);
1311 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
1312 base + L2X0_PREFETCH_CTRL);
1313
1314 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
1315 }
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001316}
1317
Russell Kingc02642b2014-03-15 16:47:54 +00001318static const struct l2c_init_data of_tauros3_data __initconst = {
Russell King051334b2014-03-15 23:04:10 +00001319 .type = "Tauros3",
Russell King0493aef2014-03-15 23:26:24 +00001320 .way_size_0 = SZ_8K,
Russell King3b8bad52014-03-15 16:47:57 +00001321 .num_lock = 8,
1322 .enable = l2c_enable,
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001323 .save = tauros3_save,
1324 /* Tauros3 broadcasts L1 cache operations to L2 */
1325 .outer_cache = {
1326 .resume = tauros3_resume,
1327 },
1328};
1329
Russell Kinga65bb922014-03-15 16:48:01 +00001330#define L2C_ID(name, fns) { .compatible = name, .data = (void *)&fns }
Rob Herring8c369262011-08-03 18:12:05 +01001331static const struct of_device_id l2x0_ids[] __initconst = {
Russell King6a28cf52014-03-15 18:55:53 +00001332 L2C_ID("arm,l210-cache", of_l2c210_data),
Russell King733c6bb2014-03-15 21:29:28 +00001333 L2C_ID("arm,l220-cache", of_l2c220_data),
Russell Kingf7773322014-03-15 20:51:47 +00001334 L2C_ID("arm,pl310-cache", of_l2c310_data),
Russell Kingc02642b2014-03-15 16:47:54 +00001335 L2C_ID("brcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
1336 L2C_ID("marvell,aurora-outer-cache", of_aurora_with_outer_data),
1337 L2C_ID("marvell,aurora-system-cache", of_aurora_no_outer_data),
1338 L2C_ID("marvell,tauros3-cache", of_tauros3_data),
Russell Kinga65bb922014-03-15 16:48:01 +00001339 /* Deprecated IDs */
Russell Kingc02642b2014-03-15 16:47:54 +00001340 L2C_ID("bcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
Rob Herring8c369262011-08-03 18:12:05 +01001341 {}
1342};
1343
Russell King3e175ca2011-09-18 11:27:30 +01001344int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +01001345{
Russell Kingc02642b2014-03-15 16:47:54 +00001346 const struct l2c_init_data *data;
Rob Herring8c369262011-08-03 18:12:05 +01001347 struct device_node *np;
Barry Song91c2ebb2011-09-30 14:43:12 +01001348 struct resource res;
Russell King96054b02014-03-15 16:47:52 +00001349 u32 cache_id;
Rob Herring8c369262011-08-03 18:12:05 +01001350
1351 np = of_find_matching_node(NULL, l2x0_ids);
1352 if (!np)
1353 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +01001354
1355 if (of_address_to_resource(np, 0, &res))
1356 return -ENODEV;
1357
1358 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +01001359 if (!l2x0_base)
1360 return -ENOMEM;
1361
Barry Song91c2ebb2011-09-30 14:43:12 +01001362 l2x0_saved_regs.phy_base = res.start;
1363
1364 data = of_match_node(l2x0_ids, np)->data;
1365
Rob Herring8c369262011-08-03 18:12:05 +01001366 /* L2 configuration can only be changed if the cache is disabled */
Russell King40266d62014-03-15 16:47:59 +00001367 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
Russell Kingc02642b2014-03-15 16:47:54 +00001368 if (data->of_parse)
1369 data->of_parse(np, &aux_val, &aux_mask);
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +01001370
Russell King96054b02014-03-15 16:47:52 +00001371 if (cache_id_part_number_from_dt)
1372 cache_id = cache_id_part_number_from_dt;
1373 else
1374 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
1375
1376 __l2c_init(data, aux_val, aux_mask, cache_id);
Gregory CLEMENT6248d062012-10-01 10:56:42 +01001377
Rob Herring8c369262011-08-03 18:12:05 +01001378 return 0;
1379}
1380#endif