blob: 2d940303e6dba79f28b42633c170b48c504ebe6b [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;
Thomas Gleixner450ea482009-07-03 08:44:46 -050033static DEFINE_RAW_SPINLOCK(l2x0_lock);
34
Jason McMullan64039be2010-05-05 18:59:37 +010035static uint32_t l2x0_way_mask; /* Bitmask of active ways */
Santosh Shilimkar5ba70372010-07-11 14:35:37 +053036static uint32_t l2x0_size;
Colin Cross5ea3a7c2011-09-14 15:59:50 -070037static u32 l2x0_cache_id;
38static unsigned int l2x0_sets;
39static unsigned int l2x0_ways;
40
41static inline bool is_pl310_rev(int rev)
42{
43 return (l2x0_cache_id &
44 (L2X0_CACHE_ID_PART_MASK | L2X0_CACHE_ID_REV_MASK)) ==
45 (L2X0_CACHE_ID_PART_L310 | rev);
46}
Catalin Marinas382266a2007-02-05 14:48:19 +010047
Barry Songa9dd8f92011-09-30 14:43:12 +010048struct l2x0_regs l2x0_saved_regs;
49
50struct l2x0_of_data {
Russell King1add9082011-09-18 11:27:30 +010051 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Songa9dd8f92011-09-30 14:43:12 +010052 void (*save)(void);
53 void (*resume)(void);
54};
55
Catalin Marinas9a6655e2010-08-31 13:05:22 +010056static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010057{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010058 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010059 while (readl_relaxed(reg) & mask)
Barry Song7f5910a2011-09-09 10:30:34 +010060 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010061}
62
Catalin Marinas9a6655e2010-08-31 13:05:22 +010063#ifdef CONFIG_CACHE_PL310
64static inline void cache_wait(void __iomem *reg, unsigned long mask)
65{
66 /* cache operations by line are atomic on PL310 */
67}
68#else
69#define cache_wait cache_wait_way
70#endif
71
Catalin Marinas382266a2007-02-05 14:48:19 +010072static inline void cache_sync(void)
73{
Russell King3d107432009-11-19 11:41:09 +000074 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010075
Will Deaconc9c0cda2011-11-14 17:24:57 +010076#ifdef CONFIG_PL310_ERRATA_753970
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010077 /* write to an unmmapped register */
78 writel_relaxed(0, base + L2X0_DUMMY_REG);
79#else
Catalin Marinas6775a552010-07-28 22:01:25 +010080 writel_relaxed(0, base + L2X0_CACHE_SYNC);
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010081#endif
Russell King3d107432009-11-19 11:41:09 +000082 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010083}
84
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010085static inline void l2x0_clean_line(unsigned long addr)
86{
87 void __iomem *base = l2x0_base;
88 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010089 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010090}
91
92static inline void l2x0_inv_line(unsigned long addr)
93{
94 void __iomem *base = l2x0_base;
95 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010096 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010097}
98
Santosh Shilimkar2839e062011-03-08 06:59:54 +010099#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100100
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100101#define debug_writel(val) outer_cache.set_debug(val)
102
103static void l2x0_set_debug(unsigned long val)
104{
105 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
106}
107#else
108/* Optimised out for non-errata case */
109static inline void debug_writel(unsigned long val)
110{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100111}
112
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100113#define l2x0_set_debug NULL
114#endif
115
116#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100117static inline void l2x0_flush_line(unsigned long addr)
118{
119 void __iomem *base = l2x0_base;
120
121 /* Clean by PA followed by Invalidate by PA */
122 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100123 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100124 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100125 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100126}
127#else
128
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100129static inline void l2x0_flush_line(unsigned long addr)
130{
131 void __iomem *base = l2x0_base;
132 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100133 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100134}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100135#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137void l2x0_cache_sync(void)
Catalin Marinas23107c52010-03-24 16:48:53 +0100138{
Thomas Gleixner450ea482009-07-03 08:44:46 -0500139 unsigned long flags;
140
141 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100142 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500143 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100144}
145
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700146#ifdef CONFIG_PL310_ERRATA_727915
147static void l2x0_for_each_set_way(void __iomem *reg)
148{
149 int set;
150 int way;
151 unsigned long flags;
152
153 for (way = 0; way < l2x0_ways; way++) {
154 spin_lock_irqsave(&l2x0_lock, flags);
155 for (set = 0; set < l2x0_sets; set++)
156 writel_relaxed((way << 28) | (set << 5), reg);
157 cache_sync();
158 spin_unlock_irqrestore(&l2x0_lock, flags);
159 }
160}
161#endif
162
Will Deacon38a89142011-07-01 14:36:19 +0100163static void __l2x0_flush_all(void)
164{
165 debug_writel(0x03);
166 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
167 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
168 cache_sync();
169 debug_writel(0x00);
170}
171
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530172static void l2x0_flush_all(void)
173{
174 unsigned long flags;
175
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700176#ifdef CONFIG_PL310_ERRATA_727915
177 if (is_pl310_rev(REV_PL310_R2P0)) {
178 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
179 return;
180 }
181#endif
182
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530183 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500184 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100185 __l2x0_flush_all();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500186 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530187}
188
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530189static void l2x0_clean_all(void)
190{
191 unsigned long flags;
192
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700193#ifdef CONFIG_PL310_ERRATA_727915
194 if (is_pl310_rev(REV_PL310_R2P0)) {
195 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
196 return;
197 }
198#endif
199
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530200 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500201 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700202 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530203 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
204 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
205 cache_sync();
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700206 debug_writel(0x00);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500207 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530208}
209
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530210static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100211{
Russell King0eb948d2009-11-19 11:12:15 +0000212 unsigned long flags;
213
Catalin Marinas382266a2007-02-05 14:48:19 +0100214 /* invalidate all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500215 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530216 /* Invalidating when L2 is enabled is a nono */
217 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100218 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100219 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100220 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500221 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100222}
223
224static void l2x0_inv_range(unsigned long start, unsigned long end)
225{
Russell King3d107432009-11-19 11:41:09 +0000226 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000227 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100228
Thomas Gleixner450ea482009-07-03 08:44:46 -0500229 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100230 if (start & (CACHE_LINE_SIZE - 1)) {
231 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100232 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100233 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100234 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100235 start += CACHE_LINE_SIZE;
236 }
237
238 if (end & (CACHE_LINE_SIZE - 1)) {
239 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100240 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100241 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100242 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100243 }
244
Russell King0eb948d2009-11-19 11:12:15 +0000245 while (start < end) {
246 unsigned long blk_end = start + min(end - start, 4096UL);
247
248 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100249 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000250 start += CACHE_LINE_SIZE;
251 }
252
253 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500254 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
255 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000256 }
257 }
Russell King3d107432009-11-19 11:41:09 +0000258 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100259 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500260 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100261}
262
263static void l2x0_clean_range(unsigned long start, unsigned long end)
264{
Russell King3d107432009-11-19 11:41:09 +0000265 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000266 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100267
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530268 if ((end - start) >= l2x0_size) {
269 l2x0_clean_all();
270 return;
271 }
272
Thomas Gleixner450ea482009-07-03 08:44:46 -0500273 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100274 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000275 while (start < end) {
276 unsigned long blk_end = start + min(end - start, 4096UL);
277
278 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100279 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000280 start += CACHE_LINE_SIZE;
281 }
282
283 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500284 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
285 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000286 }
287 }
Russell King3d107432009-11-19 11:41:09 +0000288 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100289 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500290 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100291}
292
293static void l2x0_flush_range(unsigned long start, unsigned long end)
294{
Russell King3d107432009-11-19 11:41:09 +0000295 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000296 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100297
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530298 if ((end - start) >= l2x0_size) {
299 l2x0_flush_all();
300 return;
301 }
302
Thomas Gleixner450ea482009-07-03 08:44:46 -0500303 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100304 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000305 while (start < end) {
306 unsigned long blk_end = start + min(end - start, 4096UL);
307
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100308 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000309 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100310 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000311 start += CACHE_LINE_SIZE;
312 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100313 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000314
315 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500316 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
317 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000318 }
319 }
Russell King3d107432009-11-19 11:41:09 +0000320 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100321 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500322 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100323}
324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700325void l2x0_flush_range_atomic(unsigned long start, unsigned long end)
326{
327 unsigned long addr;
328
329 start &= ~(CACHE_LINE_SIZE - 1);
330 for (addr = start; addr < end; addr += CACHE_LINE_SIZE)
331 writel_relaxed(addr, l2x0_base + L2X0_CLEAN_INV_LINE_PA);
332
333 mb();
334}
335
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530336static void l2x0_disable(void)
337{
338 unsigned long flags;
339
Thomas Gleixner450ea482009-07-03 08:44:46 -0500340 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100341 __l2x0_flush_all();
342 writel_relaxed(0, l2x0_base + L2X0_CTRL);
343 dsb();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500344 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530345}
346
Russell King1add9082011-09-18 11:27:30 +0100347static void l2x0_unlock(u32 cache_id)
Linus Walleij20c335a2011-09-06 07:45:46 +0100348{
349 int lockregs;
350 int i;
351
352 if (cache_id == L2X0_CACHE_ID_PART_L310)
353 lockregs = 8;
354 else
355 /* L210 and unknown types */
356 lockregs = 1;
357
358 for (i = 0; i < lockregs; i++) {
359 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
360 i * L2X0_LOCKDOWN_STRIDE);
361 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
362 i * L2X0_LOCKDOWN_STRIDE);
363 }
364}
365
Russell King1add9082011-09-18 11:27:30 +0100366void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100367{
Russell King1add9082011-09-18 11:27:30 +0100368 u32 aux;
369 u32 cache_id;
370 u32 way_size = 0;
Linus Walleij20c335a2011-09-06 07:45:46 +0100371 int ways;
Jason McMullan64039be2010-05-05 18:59:37 +0100372 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100373
374 l2x0_base = base;
Linus Walleij20c335a2011-09-06 07:45:46 +0100375 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
376 l2x0_cache_id = cache_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377
Catalin Marinas6775a552010-07-28 22:01:25 +0100378 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100379
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100380 aux &= aux_mask;
381 aux |= aux_val;
382
Jason McMullan64039be2010-05-05 18:59:37 +0100383 /* Determine the number of ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100384 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100385 case L2X0_CACHE_ID_PART_L310:
386 if (aux & (1 << 16))
Linus Walleij20c335a2011-09-06 07:45:46 +0100387 ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100388 else
Linus Walleij20c335a2011-09-06 07:45:46 +0100389 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100390 type = "L310";
391 break;
392 case L2X0_CACHE_ID_PART_L210:
Linus Walleij20c335a2011-09-06 07:45:46 +0100393 ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100394 type = "L210";
395 break;
396 default:
397 /* Assume unknown chips have 8 ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100398 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100399 type = "L2x0 series";
400 break;
401 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Linus Walleij20c335a2011-09-06 07:45:46 +0100403 l2x0_way_mask = (1 << ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100404
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100405 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530406 * L2 cache Size = Way size * Number of ways
407 */
408 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Linus Walleij20c335a2011-09-06 07:45:46 +0100409 way_size = 1 << (way_size + 3);
410 l2x0_size = ways * way_size * SZ_1K;
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700411 l2x0_sets = way_size / CACHE_LINE_SIZE;
Linus Walleij20c335a2011-09-06 07:45:46 +0100412 l2x0_ways = ways;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530413
Linus Walleij20c335a2011-09-06 07:45:46 +0100414 /*
415 * Check if l2x0 controller is already enabled.
416 * If you are booting from non-secure mode
417 * accessing the below registers will fault.
418 */
419 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
420 /* Make sure that I&D is not locked down when starting */
421 l2x0_unlock(cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100422
Linus Walleij20c335a2011-09-06 07:45:46 +0100423 /* l2x0 controller is disabled */
424 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100425
Barry Songa9dd8f92011-09-30 14:43:12 +0100426 l2x0_saved_regs.aux_ctrl = aux;
427
Linus Walleij20c335a2011-09-06 07:45:46 +0100428 l2x0_inv_all();
429
430 /* enable L2X0 */
431 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100432 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100433
Linus Walleij20c335a2011-09-06 07:45:46 +0100434 outer_cache.inv_range = l2x0_inv_range;
435 outer_cache.clean_range = l2x0_clean_range;
436 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100437 outer_cache.sync = l2x0_cache_sync;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530439 outer_cache.flush_all = l2x0_flush_all;
440 outer_cache.inv_all = l2x0_inv_all;
441 outer_cache.disable = l2x0_disable;
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100442 outer_cache.set_debug = l2x0_set_debug;
Catalin Marinas382266a2007-02-05 14:48:19 +0100443
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 mb();
Jason McMullan64039be2010-05-05 18:59:37 +0100445 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530446 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 +0100447 ways, cache_id, aux, l2x0_size);
Catalin Marinas382266a2007-02-05 14:48:19 +0100448}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
Rob Herring78ae8b12011-08-03 18:12:05 +0100450#ifdef CONFIG_OF
451static void __init l2x0_of_setup(const struct device_node *np,
Russell King1add9082011-09-18 11:27:30 +0100452 u32 *aux_val, u32 *aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100453{
454 u32 data[2] = { 0, 0 };
455 u32 tag = 0;
456 u32 dirty = 0;
457 u32 val = 0, mask = 0;
458
459 of_property_read_u32(np, "arm,tag-latency", &tag);
460 if (tag) {
461 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
462 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
463 }
464
465 of_property_read_u32_array(np, "arm,data-latency",
466 data, ARRAY_SIZE(data));
467 if (data[0] && data[1]) {
468 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
469 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
470 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
471 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
472 }
473
474 of_property_read_u32(np, "arm,dirty-latency", &dirty);
475 if (dirty) {
476 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
477 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
478 }
479
480 *aux_val &= ~mask;
481 *aux_val |= val;
482 *aux_mask &= ~mask;
483}
484
485static void __init pl310_of_setup(const struct device_node *np,
Russell King1add9082011-09-18 11:27:30 +0100486 u32 *aux_val, u32 *aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100487{
488 u32 data[3] = { 0, 0, 0 };
489 u32 tag[3] = { 0, 0, 0 };
490 u32 filter[2] = { 0, 0 };
491
492 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
493 if (tag[0] && tag[1] && tag[2])
494 writel_relaxed(
495 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
496 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
497 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
498 l2x0_base + L2X0_TAG_LATENCY_CTRL);
499
500 of_property_read_u32_array(np, "arm,data-latency",
501 data, ARRAY_SIZE(data));
502 if (data[0] && data[1] && data[2])
503 writel_relaxed(
504 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
505 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
506 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
507 l2x0_base + L2X0_DATA_LATENCY_CTRL);
508
509 of_property_read_u32_array(np, "arm,filter-ranges",
510 filter, ARRAY_SIZE(filter));
Barry Songce72d042011-09-14 03:20:01 +0100511 if (filter[1]) {
Rob Herring78ae8b12011-08-03 18:12:05 +0100512 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
513 l2x0_base + L2X0_ADDR_FILTER_END);
514 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
515 l2x0_base + L2X0_ADDR_FILTER_START);
516 }
517}
Taniya Dasf51e94e2012-04-12 12:02:38 +0530518#endif
Rob Herring78ae8b12011-08-03 18:12:05 +0100519
Stephen Boyd22ab9342012-04-25 11:42:14 -0700520static void pl310_save(void)
Barry Songa9dd8f92011-09-30 14:43:12 +0100521{
522 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
523 L2X0_CACHE_ID_RTL_MASK;
524
525 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
526 L2X0_TAG_LATENCY_CTRL);
527 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
528 L2X0_DATA_LATENCY_CTRL);
529 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
530 L2X0_ADDR_FILTER_END);
531 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
532 L2X0_ADDR_FILTER_START);
533
534 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
535 /*
536 * From r2p0, there is Prefetch offset/control register
537 */
538 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
539 L2X0_PREFETCH_CTRL);
540 /*
541 * From r3p0, there is Power control register
542 */
543 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
544 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
545 L2X0_POWER_CTRL);
546 }
547}
548
549static void l2x0_resume(void)
550{
551 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
552 /* restore aux ctrl and enable l2 */
553 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
554
555 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
556 L2X0_AUX_CTRL);
557
558 l2x0_inv_all();
559
560 writel_relaxed(1, l2x0_base + L2X0_CTRL);
561 }
562}
563
564static void pl310_resume(void)
565{
566 u32 l2x0_revision;
567
568 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
569 /* restore pl310 setup */
570 writel_relaxed(l2x0_saved_regs.tag_latency,
571 l2x0_base + L2X0_TAG_LATENCY_CTRL);
572 writel_relaxed(l2x0_saved_regs.data_latency,
573 l2x0_base + L2X0_DATA_LATENCY_CTRL);
574 writel_relaxed(l2x0_saved_regs.filter_end,
575 l2x0_base + L2X0_ADDR_FILTER_END);
576 writel_relaxed(l2x0_saved_regs.filter_start,
577 l2x0_base + L2X0_ADDR_FILTER_START);
578
579 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
580 L2X0_CACHE_ID_RTL_MASK;
581
582 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
583 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
584 l2x0_base + L2X0_PREFETCH_CTRL);
585 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
586 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
587 l2x0_base + L2X0_POWER_CTRL);
588 }
589 }
590
591 l2x0_resume();
592}
593
Taniya Dasf51e94e2012-04-12 12:02:38 +0530594#ifdef CONFIG_OF
Barry Songa9dd8f92011-09-30 14:43:12 +0100595static const struct l2x0_of_data pl310_data = {
596 pl310_of_setup,
597 pl310_save,
598 pl310_resume,
599};
600
601static const struct l2x0_of_data l2x0_data = {
602 l2x0_of_setup,
603 NULL,
604 l2x0_resume,
605};
606
Rob Herring78ae8b12011-08-03 18:12:05 +0100607static const struct of_device_id l2x0_ids[] __initconst = {
Barry Songa9dd8f92011-09-30 14:43:12 +0100608 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
609 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
610 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring78ae8b12011-08-03 18:12:05 +0100611 {}
612};
613
Russell King1add9082011-09-18 11:27:30 +0100614int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100615{
616 struct device_node *np;
Barry Songa9dd8f92011-09-30 14:43:12 +0100617 struct l2x0_of_data *data;
618 struct resource res;
Rob Herring78ae8b12011-08-03 18:12:05 +0100619
620 np = of_find_matching_node(NULL, l2x0_ids);
621 if (!np)
622 return -ENODEV;
Barry Songa9dd8f92011-09-30 14:43:12 +0100623
624 if (of_address_to_resource(np, 0, &res))
625 return -ENODEV;
626
627 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring78ae8b12011-08-03 18:12:05 +0100628 if (!l2x0_base)
629 return -ENOMEM;
630
Barry Songa9dd8f92011-09-30 14:43:12 +0100631 l2x0_saved_regs.phy_base = res.start;
632
633 data = of_match_node(l2x0_ids, np)->data;
634
Rob Herring78ae8b12011-08-03 18:12:05 +0100635 /* L2 configuration can only be changed if the cache is disabled */
636 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Songa9dd8f92011-09-30 14:43:12 +0100637 if (data->setup)
638 data->setup(np, &aux_val, &aux_mask);
Rob Herring78ae8b12011-08-03 18:12:05 +0100639 }
Barry Songa9dd8f92011-09-30 14:43:12 +0100640
641 if (data->save)
642 data->save();
643
Rob Herring78ae8b12011-08-03 18:12:05 +0100644 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Songa9dd8f92011-09-30 14:43:12 +0100645
646 outer_cache.resume = data->resume;
Rob Herring78ae8b12011-08-03 18:12:05 +0100647 return 0;
648}
649#endif
Taniya Dasf51e94e2012-04-12 12:02:38 +0530650
651void l2cc_suspend(void)
652{
653 pl310_save();
654 l2x0_disable();
655 dmb();
656}
657
658void l2cc_resume(int collapsed)
659{
660 if (collapsed)
661 pl310_resume();
662 dmb();
663}