blob: 7fcd0bf63d7e7daaf798e4538704be82e1f743d3 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 * Copyright (c) 2009, 2011, Code Aurora Forum. All rights reserved.
Catalin Marinas382266a2007-02-05 14:48:19 +01006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Rob Herring78ae8b12011-08-03 18:12:05 +010020#include <linux/err.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010021#include <linux/init.h>
Catalin Marinas07620972007-07-20 11:42:40 +010022#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Rob Herring78ae8b12011-08-03 18:12:05 +010024#include <linux/of.h>
25#include <linux/of_address.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010026
27#include <asm/cacheflush.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010028#include <asm/hardware/cache-l2x0.h>
29
30#define CACHE_LINE_SIZE 32
31
32static void __iomem *l2x0_base;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033static uint32_t aux_ctrl_save;
Maheshkumar Sivasubramanianc71d8ff2011-09-26 13:17:58 -060034static uint32_t data_latency_ctrl;
Thomas Gleixner450ea482009-07-03 08:44:46 -050035static DEFINE_RAW_SPINLOCK(l2x0_lock);
36
Jason McMullan64039be2010-05-05 18:59:37 +010037static uint32_t l2x0_way_mask; /* Bitmask of active ways */
Santosh Shilimkar5ba70372010-07-11 14:35:37 +053038static uint32_t l2x0_size;
Colin Cross5ea3a7c2011-09-14 15:59:50 -070039static u32 l2x0_cache_id;
40static unsigned int l2x0_sets;
41static unsigned int l2x0_ways;
42
43static inline bool is_pl310_rev(int rev)
44{
45 return (l2x0_cache_id &
46 (L2X0_CACHE_ID_PART_MASK | L2X0_CACHE_ID_REV_MASK)) ==
47 (L2X0_CACHE_ID_PART_L310 | rev);
48}
Catalin Marinas382266a2007-02-05 14:48:19 +010049
Catalin Marinas9a6655e2010-08-31 13:05:22 +010050static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010051{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010052 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010053 while (readl_relaxed(reg) & mask)
Barry Song7f5910a2011-09-09 10:30:34 +010054 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010055}
56
Catalin Marinas9a6655e2010-08-31 13:05:22 +010057#ifdef CONFIG_CACHE_PL310
58static inline void cache_wait(void __iomem *reg, unsigned long mask)
59{
60 /* cache operations by line are atomic on PL310 */
61}
62#else
63#define cache_wait cache_wait_way
64#endif
65
Catalin Marinas382266a2007-02-05 14:48:19 +010066static inline void cache_sync(void)
67{
Russell King3d107432009-11-19 11:41:09 +000068 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010069
70#ifdef CONFIG_ARM_ERRATA_753970
71 /* write to an unmmapped register */
72 writel_relaxed(0, base + L2X0_DUMMY_REG);
73#else
Catalin Marinas6775a552010-07-28 22:01:25 +010074 writel_relaxed(0, base + L2X0_CACHE_SYNC);
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010075#endif
Russell King3d107432009-11-19 11:41:09 +000076 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010077}
78
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010079static inline void l2x0_clean_line(unsigned long addr)
80{
81 void __iomem *base = l2x0_base;
82 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010083 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010084}
85
86static inline void l2x0_inv_line(unsigned long addr)
87{
88 void __iomem *base = l2x0_base;
89 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010090 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010091}
92
Santosh Shilimkar2839e062011-03-08 06:59:54 +010093#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Santosh Shilimkar9e655822010-02-04 19:42:42 +010094
Santosh Shilimkar2839e062011-03-08 06:59:54 +010095#define debug_writel(val) outer_cache.set_debug(val)
96
97static void l2x0_set_debug(unsigned long val)
98{
99 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
100}
101#else
102/* Optimised out for non-errata case */
103static inline void debug_writel(unsigned long val)
104{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100105}
106
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100107#define l2x0_set_debug NULL
108#endif
109
110#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100111static inline void l2x0_flush_line(unsigned long addr)
112{
113 void __iomem *base = l2x0_base;
114
115 /* Clean by PA followed by Invalidate by PA */
116 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100117 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100118 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100119 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100120}
121#else
122
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100123static inline void l2x0_flush_line(unsigned long addr)
124{
125 void __iomem *base = l2x0_base;
126 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100127 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100128}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100129#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100130
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131void l2x0_cache_sync(void)
Catalin Marinas23107c52010-03-24 16:48:53 +0100132{
Thomas Gleixner450ea482009-07-03 08:44:46 -0500133 unsigned long flags;
134
135 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100136 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500137 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100138}
139
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700140#ifdef CONFIG_PL310_ERRATA_727915
141static void l2x0_for_each_set_way(void __iomem *reg)
142{
143 int set;
144 int way;
145 unsigned long flags;
146
147 for (way = 0; way < l2x0_ways; way++) {
148 spin_lock_irqsave(&l2x0_lock, flags);
149 for (set = 0; set < l2x0_sets; set++)
150 writel_relaxed((way << 28) | (set << 5), reg);
151 cache_sync();
152 spin_unlock_irqrestore(&l2x0_lock, flags);
153 }
154}
155#endif
156
Will Deacon38a89142011-07-01 14:36:19 +0100157static void __l2x0_flush_all(void)
158{
159 debug_writel(0x03);
160 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
161 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
162 cache_sync();
163 debug_writel(0x00);
164}
165
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530166static void l2x0_flush_all(void)
167{
168 unsigned long flags;
169
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700170#ifdef CONFIG_PL310_ERRATA_727915
171 if (is_pl310_rev(REV_PL310_R2P0)) {
172 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
173 return;
174 }
175#endif
176
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530177 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500178 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100179 __l2x0_flush_all();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500180 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530181}
182
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530183static void l2x0_clean_all(void)
184{
185 unsigned long flags;
186
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700187#ifdef CONFIG_PL310_ERRATA_727915
188 if (is_pl310_rev(REV_PL310_R2P0)) {
189 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
190 return;
191 }
192#endif
193
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530194 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500195 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700196 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530197 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
198 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
199 cache_sync();
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700200 debug_writel(0x00);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500201 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530202}
203
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530204static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100205{
Russell King0eb948d2009-11-19 11:12:15 +0000206 unsigned long flags;
207
Catalin Marinas382266a2007-02-05 14:48:19 +0100208 /* invalidate all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500209 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530210 /* Invalidating when L2 is enabled is a nono */
211 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100212 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100213 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100214 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500215 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100216}
217
218static void l2x0_inv_range(unsigned long start, unsigned long end)
219{
Russell King3d107432009-11-19 11:41:09 +0000220 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000221 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100222
Thomas Gleixner450ea482009-07-03 08:44:46 -0500223 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100224 if (start & (CACHE_LINE_SIZE - 1)) {
225 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100226 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100227 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100228 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100229 start += CACHE_LINE_SIZE;
230 }
231
232 if (end & (CACHE_LINE_SIZE - 1)) {
233 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100234 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100235 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100236 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100237 }
238
Russell King0eb948d2009-11-19 11:12:15 +0000239 while (start < end) {
240 unsigned long blk_end = start + min(end - start, 4096UL);
241
242 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100243 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000244 start += CACHE_LINE_SIZE;
245 }
246
247 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500248 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
249 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000250 }
251 }
Russell King3d107432009-11-19 11:41:09 +0000252 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100253 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500254 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100255}
256
257static void l2x0_clean_range(unsigned long start, unsigned long end)
258{
Russell King3d107432009-11-19 11:41:09 +0000259 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000260 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100261
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530262 if ((end - start) >= l2x0_size) {
263 l2x0_clean_all();
264 return;
265 }
266
Thomas Gleixner450ea482009-07-03 08:44:46 -0500267 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100268 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000269 while (start < end) {
270 unsigned long blk_end = start + min(end - start, 4096UL);
271
272 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100273 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000274 start += CACHE_LINE_SIZE;
275 }
276
277 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500278 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
279 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000280 }
281 }
Russell King3d107432009-11-19 11:41:09 +0000282 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100283 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500284 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100285}
286
287static void l2x0_flush_range(unsigned long start, unsigned long end)
288{
Russell King3d107432009-11-19 11:41:09 +0000289 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000290 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100291
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530292 if ((end - start) >= l2x0_size) {
293 l2x0_flush_all();
294 return;
295 }
296
Thomas Gleixner450ea482009-07-03 08:44:46 -0500297 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100298 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000299 while (start < end) {
300 unsigned long blk_end = start + min(end - start, 4096UL);
301
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100302 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000303 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100304 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000305 start += CACHE_LINE_SIZE;
306 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100307 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000308
309 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500310 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
311 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000312 }
313 }
Russell King3d107432009-11-19 11:41:09 +0000314 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100315 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500316 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100317}
318
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319void l2x0_flush_range_atomic(unsigned long start, unsigned long end)
320{
321 unsigned long addr;
322
323 start &= ~(CACHE_LINE_SIZE - 1);
324 for (addr = start; addr < end; addr += CACHE_LINE_SIZE)
325 writel_relaxed(addr, l2x0_base + L2X0_CLEAN_INV_LINE_PA);
326
327 mb();
328}
329
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530330static void l2x0_disable(void)
331{
332 unsigned long flags;
333
Thomas Gleixner450ea482009-07-03 08:44:46 -0500334 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100335 __l2x0_flush_all();
336 writel_relaxed(0, l2x0_base + L2X0_CTRL);
337 dsb();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500338 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530339}
340
Linus Walleij20c335a2011-09-06 07:45:46 +0100341static void __init l2x0_unlock(__u32 cache_id)
342{
343 int lockregs;
344 int i;
345
346 if (cache_id == L2X0_CACHE_ID_PART_L310)
347 lockregs = 8;
348 else
349 /* L210 and unknown types */
350 lockregs = 1;
351
352 for (i = 0; i < lockregs; i++) {
353 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
354 i * L2X0_LOCKDOWN_STRIDE);
355 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
356 i * L2X0_LOCKDOWN_STRIDE);
357 }
358}
359
Catalin Marinas382266a2007-02-05 14:48:19 +0100360void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
361{
Linus Walleij20c335a2011-09-06 07:45:46 +0100362 __u32 aux;
363 __u32 cache_id;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530364 __u32 way_size = 0;
Linus Walleij20c335a2011-09-06 07:45:46 +0100365 int ways;
Jason McMullan64039be2010-05-05 18:59:37 +0100366 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100367
368 l2x0_base = base;
Linus Walleij20c335a2011-09-06 07:45:46 +0100369 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
370 l2x0_cache_id = cache_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371
Catalin Marinas6775a552010-07-28 22:01:25 +0100372 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100373
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100374 aux &= aux_mask;
375 aux |= aux_val;
376
Jason McMullan64039be2010-05-05 18:59:37 +0100377 /* Determine the number of ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100378 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100379 case L2X0_CACHE_ID_PART_L310:
380 if (aux & (1 << 16))
Linus Walleij20c335a2011-09-06 07:45:46 +0100381 ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100382 else
Linus Walleij20c335a2011-09-06 07:45:46 +0100383 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100384 type = "L310";
385 break;
386 case L2X0_CACHE_ID_PART_L210:
Linus Walleij20c335a2011-09-06 07:45:46 +0100387 ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100388 type = "L210";
389 break;
390 default:
391 /* Assume unknown chips have 8 ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100392 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100393 type = "L2x0 series";
394 break;
395 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Linus Walleij20c335a2011-09-06 07:45:46 +0100397 l2x0_way_mask = (1 << ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100398
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100399 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530400 * L2 cache Size = Way size * Number of ways
401 */
402 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Linus Walleij20c335a2011-09-06 07:45:46 +0100403 way_size = 1 << (way_size + 3);
404 l2x0_size = ways * way_size * SZ_1K;
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700405 l2x0_sets = way_size / CACHE_LINE_SIZE;
Linus Walleij20c335a2011-09-06 07:45:46 +0100406 l2x0_ways = ways;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530407
Linus Walleij20c335a2011-09-06 07:45:46 +0100408 /*
409 * Check if l2x0 controller is already enabled.
410 * If you are booting from non-secure mode
411 * accessing the below registers will fault.
412 */
413 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
414 /* Make sure that I&D is not locked down when starting */
415 l2x0_unlock(cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100416
Linus Walleij20c335a2011-09-06 07:45:46 +0100417 /* l2x0 controller is disabled */
418 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100419
Linus Walleij20c335a2011-09-06 07:45:46 +0100420 l2x0_inv_all();
421
422 /* enable L2X0 */
423 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100424 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100425
Linus Walleij20c335a2011-09-06 07:45:46 +0100426 outer_cache.inv_range = l2x0_inv_range;
427 outer_cache.clean_range = l2x0_clean_range;
428 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100429 outer_cache.sync = l2x0_cache_sync;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530431 outer_cache.flush_all = l2x0_flush_all;
432 outer_cache.inv_all = l2x0_inv_all;
433 outer_cache.disable = l2x0_disable;
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100434 outer_cache.set_debug = l2x0_set_debug;
Catalin Marinas382266a2007-02-05 14:48:19 +0100435
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436 mb();
Jason McMullan64039be2010-05-05 18:59:37 +0100437 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530438 printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n",
Linus Walleij20c335a2011-09-06 07:45:46 +0100439 ways, cache_id, aux, l2x0_size);
Catalin Marinas382266a2007-02-05 14:48:19 +0100440}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441
442void l2x0_suspend(void)
443{
444 /* Save aux control register value */
445 aux_ctrl_save = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Maheshkumar Sivasubramanianc71d8ff2011-09-26 13:17:58 -0600446 data_latency_ctrl = readl_relaxed(l2x0_base + L2X0_DATA_LATENCY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447 /* Flush all cache */
448 l2x0_flush_all();
449 /* Disable the cache */
450 writel_relaxed(0, l2x0_base + L2X0_CTRL);
451
452 /* Memory barrier */
453 dmb();
454}
455
456void l2x0_resume(int collapsed)
457{
458 if (collapsed) {
459 /* Disable the cache */
460 writel_relaxed(0, l2x0_base + L2X0_CTRL);
461
462 /* Restore aux control register value */
463 writel_relaxed(aux_ctrl_save, l2x0_base + L2X0_AUX_CTRL);
Maheshkumar Sivasubramanianc71d8ff2011-09-26 13:17:58 -0600464 writel_relaxed(data_latency_ctrl, l2x0_base +
465 L2X0_DATA_LATENCY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466
467 /* Invalidate the cache */
468 l2x0_inv_all();
Anji jonnala4ddc453f2012-04-03 12:02:53 +0530469 /*
470 * TBD: make sure that l2xo_inv_all finished
471 * before actually enabling the cache. Logically this
472 * is not required as cache sync is atomic operation.
473 * but on 8x25, observed the random crashes and they go
474 * away if we add dmb or disable the L2.
475 * keeping this as temporary workaround until root
476 * cause is find out.
477 */
478 dmb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479 }
480
481 /* Enable the cache */
482 writel_relaxed(1, l2x0_base + L2X0_CTRL);
483
484 mb();
485}
Rob Herring78ae8b12011-08-03 18:12:05 +0100486
487#ifdef CONFIG_OF
488static void __init l2x0_of_setup(const struct device_node *np,
489 __u32 *aux_val, __u32 *aux_mask)
490{
491 u32 data[2] = { 0, 0 };
492 u32 tag = 0;
493 u32 dirty = 0;
494 u32 val = 0, mask = 0;
495
496 of_property_read_u32(np, "arm,tag-latency", &tag);
497 if (tag) {
498 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
499 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
500 }
501
502 of_property_read_u32_array(np, "arm,data-latency",
503 data, ARRAY_SIZE(data));
504 if (data[0] && data[1]) {
505 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
506 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
507 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
508 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
509 }
510
511 of_property_read_u32(np, "arm,dirty-latency", &dirty);
512 if (dirty) {
513 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
514 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
515 }
516
517 *aux_val &= ~mask;
518 *aux_val |= val;
519 *aux_mask &= ~mask;
520}
521
522static void __init pl310_of_setup(const struct device_node *np,
523 __u32 *aux_val, __u32 *aux_mask)
524{
525 u32 data[3] = { 0, 0, 0 };
526 u32 tag[3] = { 0, 0, 0 };
527 u32 filter[2] = { 0, 0 };
528
529 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
530 if (tag[0] && tag[1] && tag[2])
531 writel_relaxed(
532 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
533 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
534 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
535 l2x0_base + L2X0_TAG_LATENCY_CTRL);
536
537 of_property_read_u32_array(np, "arm,data-latency",
538 data, ARRAY_SIZE(data));
539 if (data[0] && data[1] && data[2])
540 writel_relaxed(
541 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
542 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
543 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
544 l2x0_base + L2X0_DATA_LATENCY_CTRL);
545
546 of_property_read_u32_array(np, "arm,filter-ranges",
547 filter, ARRAY_SIZE(filter));
Barry Songce72d042011-09-14 03:20:01 +0100548 if (filter[1]) {
Rob Herring78ae8b12011-08-03 18:12:05 +0100549 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
550 l2x0_base + L2X0_ADDR_FILTER_END);
551 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
552 l2x0_base + L2X0_ADDR_FILTER_START);
553 }
554}
555
556static const struct of_device_id l2x0_ids[] __initconst = {
557 { .compatible = "arm,pl310-cache", .data = pl310_of_setup },
558 { .compatible = "arm,l220-cache", .data = l2x0_of_setup },
559 { .compatible = "arm,l210-cache", .data = l2x0_of_setup },
560 {}
561};
562
563int __init l2x0_of_init(__u32 aux_val, __u32 aux_mask)
564{
565 struct device_node *np;
566 void (*l2_setup)(const struct device_node *np,
567 __u32 *aux_val, __u32 *aux_mask);
568
569 np = of_find_matching_node(NULL, l2x0_ids);
570 if (!np)
571 return -ENODEV;
572 l2x0_base = of_iomap(np, 0);
573 if (!l2x0_base)
574 return -ENOMEM;
575
576 /* L2 configuration can only be changed if the cache is disabled */
577 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
578 l2_setup = of_match_node(l2x0_ids, np)->data;
579 if (l2_setup)
580 l2_setup(np, &aux_val, &aux_mask);
581 }
582 l2x0_init(l2x0_base, aux_val, aux_mask);
583 return 0;
584}
585#endif