blob: 01d6ac52278498dfdd61b7432803202c7244edec [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
Barry Songa9dd8f92011-09-30 14:43:12 +010050struct l2x0_regs l2x0_saved_regs;
51
52struct l2x0_of_data {
Russell King1add9082011-09-18 11:27:30 +010053 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Songa9dd8f92011-09-30 14:43:12 +010054 void (*save)(void);
55 void (*resume)(void);
56};
57
Catalin Marinas9a6655e2010-08-31 13:05:22 +010058static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010059{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010060 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010061 while (readl_relaxed(reg) & mask)
Barry Song7f5910a2011-09-09 10:30:34 +010062 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010063}
64
Catalin Marinas9a6655e2010-08-31 13:05:22 +010065#ifdef CONFIG_CACHE_PL310
66static inline void cache_wait(void __iomem *reg, unsigned long mask)
67{
68 /* cache operations by line are atomic on PL310 */
69}
70#else
71#define cache_wait cache_wait_way
72#endif
73
Catalin Marinas382266a2007-02-05 14:48:19 +010074static inline void cache_sync(void)
75{
Russell King3d107432009-11-19 11:41:09 +000076 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010077
Will Deaconc9c0cda2011-11-14 17:24:57 +010078#ifdef CONFIG_PL310_ERRATA_753970
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010079 /* write to an unmmapped register */
80 writel_relaxed(0, base + L2X0_DUMMY_REG);
81#else
Catalin Marinas6775a552010-07-28 22:01:25 +010082 writel_relaxed(0, base + L2X0_CACHE_SYNC);
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010083#endif
Russell King3d107432009-11-19 11:41:09 +000084 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010085}
86
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010087static inline void l2x0_clean_line(unsigned long addr)
88{
89 void __iomem *base = l2x0_base;
90 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010091 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010092}
93
94static inline void l2x0_inv_line(unsigned long addr)
95{
96 void __iomem *base = l2x0_base;
97 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010098 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010099}
100
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100101#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100102
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100103#define debug_writel(val) outer_cache.set_debug(val)
104
105static void l2x0_set_debug(unsigned long val)
106{
107 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
108}
109#else
110/* Optimised out for non-errata case */
111static inline void debug_writel(unsigned long val)
112{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100113}
114
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100115#define l2x0_set_debug NULL
116#endif
117
118#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100119static inline void l2x0_flush_line(unsigned long addr)
120{
121 void __iomem *base = l2x0_base;
122
123 /* Clean by PA followed by Invalidate by PA */
124 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100125 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100126 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100127 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100128}
129#else
130
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100131static inline void l2x0_flush_line(unsigned long addr)
132{
133 void __iomem *base = l2x0_base;
134 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100135 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100136}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100137#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139void l2x0_cache_sync(void)
Catalin Marinas23107c52010-03-24 16:48:53 +0100140{
Thomas Gleixner450ea482009-07-03 08:44:46 -0500141 unsigned long flags;
142
143 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100144 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500145 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100146}
147
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700148#ifdef CONFIG_PL310_ERRATA_727915
149static void l2x0_for_each_set_way(void __iomem *reg)
150{
151 int set;
152 int way;
153 unsigned long flags;
154
155 for (way = 0; way < l2x0_ways; way++) {
156 spin_lock_irqsave(&l2x0_lock, flags);
157 for (set = 0; set < l2x0_sets; set++)
158 writel_relaxed((way << 28) | (set << 5), reg);
159 cache_sync();
160 spin_unlock_irqrestore(&l2x0_lock, flags);
161 }
162}
163#endif
164
Will Deacon38a89142011-07-01 14:36:19 +0100165static void __l2x0_flush_all(void)
166{
167 debug_writel(0x03);
168 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
169 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
170 cache_sync();
171 debug_writel(0x00);
172}
173
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530174static void l2x0_flush_all(void)
175{
176 unsigned long flags;
177
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700178#ifdef CONFIG_PL310_ERRATA_727915
179 if (is_pl310_rev(REV_PL310_R2P0)) {
180 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
181 return;
182 }
183#endif
184
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530185 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500186 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100187 __l2x0_flush_all();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500188 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530189}
190
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530191static void l2x0_clean_all(void)
192{
193 unsigned long flags;
194
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700195#ifdef CONFIG_PL310_ERRATA_727915
196 if (is_pl310_rev(REV_PL310_R2P0)) {
197 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
198 return;
199 }
200#endif
201
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530202 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500203 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700204 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530205 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
206 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
207 cache_sync();
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700208 debug_writel(0x00);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500209 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530210}
211
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530212static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100213{
Russell King0eb948d2009-11-19 11:12:15 +0000214 unsigned long flags;
215
Catalin Marinas382266a2007-02-05 14:48:19 +0100216 /* invalidate all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500217 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530218 /* Invalidating when L2 is enabled is a nono */
219 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100220 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100221 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100222 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500223 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100224}
225
226static void l2x0_inv_range(unsigned long start, unsigned long end)
227{
Russell King3d107432009-11-19 11:41:09 +0000228 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000229 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100230
Thomas Gleixner450ea482009-07-03 08:44:46 -0500231 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100232 if (start & (CACHE_LINE_SIZE - 1)) {
233 start &= ~(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(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100236 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100237 start += CACHE_LINE_SIZE;
238 }
239
240 if (end & (CACHE_LINE_SIZE - 1)) {
241 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100242 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100243 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100244 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100245 }
246
Russell King0eb948d2009-11-19 11:12:15 +0000247 while (start < end) {
248 unsigned long blk_end = start + min(end - start, 4096UL);
249
250 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100251 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000252 start += CACHE_LINE_SIZE;
253 }
254
255 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500256 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
257 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000258 }
259 }
Russell King3d107432009-11-19 11:41:09 +0000260 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100261 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500262 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100263}
264
265static void l2x0_clean_range(unsigned long start, unsigned long end)
266{
Russell King3d107432009-11-19 11:41:09 +0000267 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000268 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100269
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530270 if ((end - start) >= l2x0_size) {
271 l2x0_clean_all();
272 return;
273 }
274
Thomas Gleixner450ea482009-07-03 08:44:46 -0500275 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100276 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000277 while (start < end) {
278 unsigned long blk_end = start + min(end - start, 4096UL);
279
280 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100281 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000282 start += CACHE_LINE_SIZE;
283 }
284
285 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500286 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
287 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000288 }
289 }
Russell King3d107432009-11-19 11:41:09 +0000290 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100291 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500292 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100293}
294
295static void l2x0_flush_range(unsigned long start, unsigned long end)
296{
Russell King3d107432009-11-19 11:41:09 +0000297 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000298 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100299
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530300 if ((end - start) >= l2x0_size) {
301 l2x0_flush_all();
302 return;
303 }
304
Thomas Gleixner450ea482009-07-03 08:44:46 -0500305 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100306 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000307 while (start < end) {
308 unsigned long blk_end = start + min(end - start, 4096UL);
309
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100310 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000311 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100312 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000313 start += CACHE_LINE_SIZE;
314 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100315 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000316
317 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500318 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
319 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000320 }
321 }
Russell King3d107432009-11-19 11:41:09 +0000322 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100323 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500324 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100325}
326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700327void l2x0_flush_range_atomic(unsigned long start, unsigned long end)
328{
329 unsigned long addr;
330
331 start &= ~(CACHE_LINE_SIZE - 1);
332 for (addr = start; addr < end; addr += CACHE_LINE_SIZE)
333 writel_relaxed(addr, l2x0_base + L2X0_CLEAN_INV_LINE_PA);
334
335 mb();
336}
337
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530338static void l2x0_disable(void)
339{
340 unsigned long flags;
341
Thomas Gleixner450ea482009-07-03 08:44:46 -0500342 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100343 __l2x0_flush_all();
344 writel_relaxed(0, l2x0_base + L2X0_CTRL);
345 dsb();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500346 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530347}
348
Russell King1add9082011-09-18 11:27:30 +0100349static void l2x0_unlock(u32 cache_id)
Linus Walleij20c335a2011-09-06 07:45:46 +0100350{
351 int lockregs;
352 int i;
353
354 if (cache_id == L2X0_CACHE_ID_PART_L310)
355 lockregs = 8;
356 else
357 /* L210 and unknown types */
358 lockregs = 1;
359
360 for (i = 0; i < lockregs; i++) {
361 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
362 i * L2X0_LOCKDOWN_STRIDE);
363 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
364 i * L2X0_LOCKDOWN_STRIDE);
365 }
366}
367
Russell King1add9082011-09-18 11:27:30 +0100368void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100369{
Russell King1add9082011-09-18 11:27:30 +0100370 u32 aux;
371 u32 cache_id;
372 u32 way_size = 0;
Linus Walleij20c335a2011-09-06 07:45:46 +0100373 int ways;
Jason McMullan64039be2010-05-05 18:59:37 +0100374 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100375
376 l2x0_base = base;
Linus Walleij20c335a2011-09-06 07:45:46 +0100377 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
378 l2x0_cache_id = cache_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379
Catalin Marinas6775a552010-07-28 22:01:25 +0100380 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100381
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100382 aux &= aux_mask;
383 aux |= aux_val;
384
Jason McMullan64039be2010-05-05 18:59:37 +0100385 /* Determine the number of ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100386 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100387 case L2X0_CACHE_ID_PART_L310:
388 if (aux & (1 << 16))
Linus Walleij20c335a2011-09-06 07:45:46 +0100389 ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100390 else
Linus Walleij20c335a2011-09-06 07:45:46 +0100391 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100392 type = "L310";
393 break;
394 case L2X0_CACHE_ID_PART_L210:
Linus Walleij20c335a2011-09-06 07:45:46 +0100395 ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100396 type = "L210";
397 break;
398 default:
399 /* Assume unknown chips have 8 ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100400 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100401 type = "L2x0 series";
402 break;
403 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Linus Walleij20c335a2011-09-06 07:45:46 +0100405 l2x0_way_mask = (1 << ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100406
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100407 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530408 * L2 cache Size = Way size * Number of ways
409 */
410 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Linus Walleij20c335a2011-09-06 07:45:46 +0100411 way_size = 1 << (way_size + 3);
412 l2x0_size = ways * way_size * SZ_1K;
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700413 l2x0_sets = way_size / CACHE_LINE_SIZE;
Linus Walleij20c335a2011-09-06 07:45:46 +0100414 l2x0_ways = ways;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530415
Linus Walleij20c335a2011-09-06 07:45:46 +0100416 /*
417 * Check if l2x0 controller is already enabled.
418 * If you are booting from non-secure mode
419 * accessing the below registers will fault.
420 */
421 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
422 /* Make sure that I&D is not locked down when starting */
423 l2x0_unlock(cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100424
Linus Walleij20c335a2011-09-06 07:45:46 +0100425 /* l2x0 controller is disabled */
426 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100427
Barry Songa9dd8f92011-09-30 14:43:12 +0100428 l2x0_saved_regs.aux_ctrl = aux;
429
Linus Walleij20c335a2011-09-06 07:45:46 +0100430 l2x0_inv_all();
431
432 /* enable L2X0 */
433 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100434 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100435
Linus Walleij20c335a2011-09-06 07:45:46 +0100436 outer_cache.inv_range = l2x0_inv_range;
437 outer_cache.clean_range = l2x0_clean_range;
438 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100439 outer_cache.sync = l2x0_cache_sync;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530441 outer_cache.flush_all = l2x0_flush_all;
442 outer_cache.inv_all = l2x0_inv_all;
443 outer_cache.disable = l2x0_disable;
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100444 outer_cache.set_debug = l2x0_set_debug;
Catalin Marinas382266a2007-02-05 14:48:19 +0100445
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700446 mb();
Jason McMullan64039be2010-05-05 18:59:37 +0100447 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530448 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 +0100449 ways, cache_id, aux, l2x0_size);
Catalin Marinas382266a2007-02-05 14:48:19 +0100450}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451
452void l2x0_suspend(void)
453{
454 /* Save aux control register value */
455 aux_ctrl_save = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Maheshkumar Sivasubramanianc71d8ff2011-09-26 13:17:58 -0600456 data_latency_ctrl = readl_relaxed(l2x0_base + L2X0_DATA_LATENCY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 /* Flush all cache */
458 l2x0_flush_all();
459 /* Disable the cache */
460 writel_relaxed(0, l2x0_base + L2X0_CTRL);
461
462 /* Memory barrier */
463 dmb();
464}
465
466void l2x0_resume(int collapsed)
467{
468 if (collapsed) {
469 /* Disable the cache */
470 writel_relaxed(0, l2x0_base + L2X0_CTRL);
471
472 /* Restore aux control register value */
473 writel_relaxed(aux_ctrl_save, l2x0_base + L2X0_AUX_CTRL);
Maheshkumar Sivasubramanianc71d8ff2011-09-26 13:17:58 -0600474 writel_relaxed(data_latency_ctrl, l2x0_base +
475 L2X0_DATA_LATENCY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476
477 /* Invalidate the cache */
478 l2x0_inv_all();
Anji jonnala4ddc453f2012-04-03 12:02:53 +0530479 /*
480 * TBD: make sure that l2xo_inv_all finished
481 * before actually enabling the cache. Logically this
482 * is not required as cache sync is atomic operation.
483 * but on 8x25, observed the random crashes and they go
484 * away if we add dmb or disable the L2.
485 * keeping this as temporary workaround until root
486 * cause is find out.
487 */
488 dmb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489 }
490
491 /* Enable the cache */
492 writel_relaxed(1, l2x0_base + L2X0_CTRL);
493
494 mb();
495}
Rob Herring78ae8b12011-08-03 18:12:05 +0100496
497#ifdef CONFIG_OF
498static void __init l2x0_of_setup(const struct device_node *np,
Russell King1add9082011-09-18 11:27:30 +0100499 u32 *aux_val, u32 *aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100500{
501 u32 data[2] = { 0, 0 };
502 u32 tag = 0;
503 u32 dirty = 0;
504 u32 val = 0, mask = 0;
505
506 of_property_read_u32(np, "arm,tag-latency", &tag);
507 if (tag) {
508 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
509 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
510 }
511
512 of_property_read_u32_array(np, "arm,data-latency",
513 data, ARRAY_SIZE(data));
514 if (data[0] && data[1]) {
515 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
516 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
517 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
518 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
519 }
520
521 of_property_read_u32(np, "arm,dirty-latency", &dirty);
522 if (dirty) {
523 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
524 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
525 }
526
527 *aux_val &= ~mask;
528 *aux_val |= val;
529 *aux_mask &= ~mask;
530}
531
532static void __init pl310_of_setup(const struct device_node *np,
Russell King1add9082011-09-18 11:27:30 +0100533 u32 *aux_val, u32 *aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100534{
535 u32 data[3] = { 0, 0, 0 };
536 u32 tag[3] = { 0, 0, 0 };
537 u32 filter[2] = { 0, 0 };
538
539 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
540 if (tag[0] && tag[1] && tag[2])
541 writel_relaxed(
542 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
543 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
544 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
545 l2x0_base + L2X0_TAG_LATENCY_CTRL);
546
547 of_property_read_u32_array(np, "arm,data-latency",
548 data, ARRAY_SIZE(data));
549 if (data[0] && data[1] && data[2])
550 writel_relaxed(
551 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
552 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
553 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
554 l2x0_base + L2X0_DATA_LATENCY_CTRL);
555
556 of_property_read_u32_array(np, "arm,filter-ranges",
557 filter, ARRAY_SIZE(filter));
Barry Songce72d042011-09-14 03:20:01 +0100558 if (filter[1]) {
Rob Herring78ae8b12011-08-03 18:12:05 +0100559 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
560 l2x0_base + L2X0_ADDR_FILTER_END);
561 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
562 l2x0_base + L2X0_ADDR_FILTER_START);
563 }
564}
565
Barry Songa9dd8f92011-09-30 14:43:12 +0100566static void __init pl310_save(void)
567{
568 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
569 L2X0_CACHE_ID_RTL_MASK;
570
571 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
572 L2X0_TAG_LATENCY_CTRL);
573 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
574 L2X0_DATA_LATENCY_CTRL);
575 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
576 L2X0_ADDR_FILTER_END);
577 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
578 L2X0_ADDR_FILTER_START);
579
580 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
581 /*
582 * From r2p0, there is Prefetch offset/control register
583 */
584 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
585 L2X0_PREFETCH_CTRL);
586 /*
587 * From r3p0, there is Power control register
588 */
589 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
590 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
591 L2X0_POWER_CTRL);
592 }
593}
594
595static void l2x0_resume(void)
596{
597 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
598 /* restore aux ctrl and enable l2 */
599 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
600
601 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
602 L2X0_AUX_CTRL);
603
604 l2x0_inv_all();
605
606 writel_relaxed(1, l2x0_base + L2X0_CTRL);
607 }
608}
609
610static void pl310_resume(void)
611{
612 u32 l2x0_revision;
613
614 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
615 /* restore pl310 setup */
616 writel_relaxed(l2x0_saved_regs.tag_latency,
617 l2x0_base + L2X0_TAG_LATENCY_CTRL);
618 writel_relaxed(l2x0_saved_regs.data_latency,
619 l2x0_base + L2X0_DATA_LATENCY_CTRL);
620 writel_relaxed(l2x0_saved_regs.filter_end,
621 l2x0_base + L2X0_ADDR_FILTER_END);
622 writel_relaxed(l2x0_saved_regs.filter_start,
623 l2x0_base + L2X0_ADDR_FILTER_START);
624
625 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
626 L2X0_CACHE_ID_RTL_MASK;
627
628 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
629 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
630 l2x0_base + L2X0_PREFETCH_CTRL);
631 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
632 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
633 l2x0_base + L2X0_POWER_CTRL);
634 }
635 }
636
637 l2x0_resume();
638}
639
640static const struct l2x0_of_data pl310_data = {
641 pl310_of_setup,
642 pl310_save,
643 pl310_resume,
644};
645
646static const struct l2x0_of_data l2x0_data = {
647 l2x0_of_setup,
648 NULL,
649 l2x0_resume,
650};
651
Rob Herring78ae8b12011-08-03 18:12:05 +0100652static const struct of_device_id l2x0_ids[] __initconst = {
Barry Songa9dd8f92011-09-30 14:43:12 +0100653 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
654 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
655 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring78ae8b12011-08-03 18:12:05 +0100656 {}
657};
658
Russell King1add9082011-09-18 11:27:30 +0100659int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100660{
661 struct device_node *np;
Barry Songa9dd8f92011-09-30 14:43:12 +0100662 struct l2x0_of_data *data;
663 struct resource res;
Rob Herring78ae8b12011-08-03 18:12:05 +0100664
665 np = of_find_matching_node(NULL, l2x0_ids);
666 if (!np)
667 return -ENODEV;
Barry Songa9dd8f92011-09-30 14:43:12 +0100668
669 if (of_address_to_resource(np, 0, &res))
670 return -ENODEV;
671
672 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring78ae8b12011-08-03 18:12:05 +0100673 if (!l2x0_base)
674 return -ENOMEM;
675
Barry Songa9dd8f92011-09-30 14:43:12 +0100676 l2x0_saved_regs.phy_base = res.start;
677
678 data = of_match_node(l2x0_ids, np)->data;
679
Rob Herring78ae8b12011-08-03 18:12:05 +0100680 /* L2 configuration can only be changed if the cache is disabled */
681 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Songa9dd8f92011-09-30 14:43:12 +0100682 if (data->setup)
683 data->setup(np, &aux_val, &aux_mask);
Rob Herring78ae8b12011-08-03 18:12:05 +0100684 }
Barry Songa9dd8f92011-09-30 14:43:12 +0100685
686 if (data->save)
687 data->save();
688
Rob Herring78ae8b12011-08-03 18:12:05 +0100689 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Songa9dd8f92011-09-30 14:43:12 +0100690
691 outer_cache.resume = data->resume;
Rob Herring78ae8b12011-08-03 18:12:05 +0100692 return 0;
693}
694#endif