blob: e81f881e6655570f8ea8089a5a2935dc2c1936c3 [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
Taniya Das38a8c6e2012-05-09 20:34:39 +05305 * Copyright (c) 2009, 2011-2012, 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;
Taniya Das38a8c6e2012-05-09 20:34:39 +053040static void pl310_save(void);
Colin Cross5ea3a7c2011-09-14 15:59:50 -070041
42static inline bool is_pl310_rev(int rev)
43{
44 return (l2x0_cache_id &
45 (L2X0_CACHE_ID_PART_MASK | L2X0_CACHE_ID_REV_MASK)) ==
46 (L2X0_CACHE_ID_PART_L310 | rev);
47}
Catalin Marinas382266a2007-02-05 14:48:19 +010048
Barry Songa9dd8f92011-09-30 14:43:12 +010049struct l2x0_regs l2x0_saved_regs;
50
51struct l2x0_of_data {
Russell King1add9082011-09-18 11:27:30 +010052 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Songa9dd8f92011-09-30 14:43:12 +010053 void (*save)(void);
54 void (*resume)(void);
55};
56
Catalin Marinas9a6655e2010-08-31 13:05:22 +010057static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010058{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010059 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010060 while (readl_relaxed(reg) & mask)
Barry Song7f5910a2011-09-09 10:30:34 +010061 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010062}
63
Catalin Marinas9a6655e2010-08-31 13:05:22 +010064#ifdef CONFIG_CACHE_PL310
65static inline void cache_wait(void __iomem *reg, unsigned long mask)
66{
67 /* cache operations by line are atomic on PL310 */
68}
69#else
70#define cache_wait cache_wait_way
71#endif
72
Catalin Marinas382266a2007-02-05 14:48:19 +010073static inline void cache_sync(void)
74{
Russell King3d107432009-11-19 11:41:09 +000075 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010076
Will Deaconc9c0cda2011-11-14 17:24:57 +010077#ifdef CONFIG_PL310_ERRATA_753970
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010078 /* write to an unmmapped register */
79 writel_relaxed(0, base + L2X0_DUMMY_REG);
80#else
Catalin Marinas6775a552010-07-28 22:01:25 +010081 writel_relaxed(0, base + L2X0_CACHE_SYNC);
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010082#endif
Russell King3d107432009-11-19 11:41:09 +000083 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010084}
85
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010086static inline void l2x0_clean_line(unsigned long addr)
87{
88 void __iomem *base = l2x0_base;
89 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010090 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010091}
92
93static inline void l2x0_inv_line(unsigned long addr)
94{
95 void __iomem *base = l2x0_base;
96 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010097 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010098}
99
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100100#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100101
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100102#define debug_writel(val) outer_cache.set_debug(val)
103
104static void l2x0_set_debug(unsigned long val)
105{
106 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
107}
108#else
109/* Optimised out for non-errata case */
110static inline void debug_writel(unsigned long val)
111{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100112}
113
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100114#define l2x0_set_debug NULL
115#endif
116
117#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100118static inline void l2x0_flush_line(unsigned long addr)
119{
120 void __iomem *base = l2x0_base;
121
122 /* Clean by PA followed by Invalidate by PA */
123 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100124 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100125 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100126 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100127}
128#else
129
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100130static inline void l2x0_flush_line(unsigned long addr)
131{
132 void __iomem *base = l2x0_base;
133 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100134 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100135}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100136#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138void l2x0_cache_sync(void)
Catalin Marinas23107c52010-03-24 16:48:53 +0100139{
Thomas Gleixner450ea482009-07-03 08:44:46 -0500140 unsigned long flags;
141
142 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100143 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500144 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100145}
146
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700147#ifdef CONFIG_PL310_ERRATA_727915
148static void l2x0_for_each_set_way(void __iomem *reg)
149{
150 int set;
151 int way;
152 unsigned long flags;
153
154 for (way = 0; way < l2x0_ways; way++) {
155 spin_lock_irqsave(&l2x0_lock, flags);
156 for (set = 0; set < l2x0_sets; set++)
157 writel_relaxed((way << 28) | (set << 5), reg);
158 cache_sync();
159 spin_unlock_irqrestore(&l2x0_lock, flags);
160 }
161}
162#endif
163
Will Deacon38a89142011-07-01 14:36:19 +0100164static void __l2x0_flush_all(void)
165{
166 debug_writel(0x03);
167 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
168 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
169 cache_sync();
170 debug_writel(0x00);
171}
172
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530173static void l2x0_flush_all(void)
174{
175 unsigned long flags;
176
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700177#ifdef CONFIG_PL310_ERRATA_727915
178 if (is_pl310_rev(REV_PL310_R2P0)) {
179 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
180 return;
181 }
182#endif
183
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530184 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500185 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100186 __l2x0_flush_all();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500187 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530188}
189
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530190static void l2x0_clean_all(void)
191{
192 unsigned long flags;
193
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700194#ifdef CONFIG_PL310_ERRATA_727915
195 if (is_pl310_rev(REV_PL310_R2P0)) {
196 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
197 return;
198 }
199#endif
200
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530201 /* clean all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500202 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700203 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530204 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
205 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
206 cache_sync();
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700207 debug_writel(0x00);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500208 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530209}
210
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530211static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100212{
Russell King0eb948d2009-11-19 11:12:15 +0000213 unsigned long flags;
214
Catalin Marinas382266a2007-02-05 14:48:19 +0100215 /* invalidate all ways */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500216 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530217 /* Invalidating when L2 is enabled is a nono */
218 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100219 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100220 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100221 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500222 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100223}
224
225static void l2x0_inv_range(unsigned long start, unsigned long end)
226{
Russell King3d107432009-11-19 11:41:09 +0000227 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000228 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100229
Thomas Gleixner450ea482009-07-03 08:44:46 -0500230 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100231 if (start & (CACHE_LINE_SIZE - 1)) {
232 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100233 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100234 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100235 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100236 start += CACHE_LINE_SIZE;
237 }
238
239 if (end & (CACHE_LINE_SIZE - 1)) {
240 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100241 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100242 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100243 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100244 }
245
Russell King0eb948d2009-11-19 11:12:15 +0000246 while (start < end) {
247 unsigned long blk_end = start + min(end - start, 4096UL);
248
249 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100250 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000251 start += CACHE_LINE_SIZE;
252 }
253
254 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500255 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
256 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000257 }
258 }
Russell King3d107432009-11-19 11:41:09 +0000259 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100260 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500261 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100262}
263
264static void l2x0_clean_range(unsigned long start, unsigned long end)
265{
Russell King3d107432009-11-19 11:41:09 +0000266 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000267 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100268
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530269 if ((end - start) >= l2x0_size) {
270 l2x0_clean_all();
271 return;
272 }
273
Thomas Gleixner450ea482009-07-03 08:44:46 -0500274 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100275 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000276 while (start < end) {
277 unsigned long blk_end = start + min(end - start, 4096UL);
278
279 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100280 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000281 start += CACHE_LINE_SIZE;
282 }
283
284 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500285 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
286 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000287 }
288 }
Russell King3d107432009-11-19 11:41:09 +0000289 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100290 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500291 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100292}
293
294static void l2x0_flush_range(unsigned long start, unsigned long end)
295{
Russell King3d107432009-11-19 11:41:09 +0000296 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000297 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100298
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530299 if ((end - start) >= l2x0_size) {
300 l2x0_flush_all();
301 return;
302 }
303
Thomas Gleixner450ea482009-07-03 08:44:46 -0500304 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100305 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000306 while (start < end) {
307 unsigned long blk_end = start + min(end - start, 4096UL);
308
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100309 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000310 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100311 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000312 start += CACHE_LINE_SIZE;
313 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100314 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000315
316 if (blk_end < end) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500317 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
318 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000319 }
320 }
Russell King3d107432009-11-19 11:41:09 +0000321 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100322 cache_sync();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500323 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100324}
325
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326void l2x0_flush_range_atomic(unsigned long start, unsigned long end)
327{
328 unsigned long addr;
329
330 start &= ~(CACHE_LINE_SIZE - 1);
331 for (addr = start; addr < end; addr += CACHE_LINE_SIZE)
332 writel_relaxed(addr, l2x0_base + L2X0_CLEAN_INV_LINE_PA);
333
334 mb();
335}
336
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530337static void l2x0_disable(void)
338{
339 unsigned long flags;
340
Thomas Gleixner450ea482009-07-03 08:44:46 -0500341 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100342 __l2x0_flush_all();
343 writel_relaxed(0, l2x0_base + L2X0_CTRL);
344 dsb();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500345 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530346}
347
Russell King1add9082011-09-18 11:27:30 +0100348static void l2x0_unlock(u32 cache_id)
Linus Walleij20c335a2011-09-06 07:45:46 +0100349{
350 int lockregs;
351 int i;
352
353 if (cache_id == L2X0_CACHE_ID_PART_L310)
354 lockregs = 8;
355 else
356 /* L210 and unknown types */
357 lockregs = 1;
358
359 for (i = 0; i < lockregs; i++) {
360 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
361 i * L2X0_LOCKDOWN_STRIDE);
362 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
363 i * L2X0_LOCKDOWN_STRIDE);
364 }
365}
366
Russell King1add9082011-09-18 11:27:30 +0100367void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100368{
Russell King1add9082011-09-18 11:27:30 +0100369 u32 aux;
370 u32 cache_id;
371 u32 way_size = 0;
Linus Walleij20c335a2011-09-06 07:45:46 +0100372 int ways;
Jason McMullan64039be2010-05-05 18:59:37 +0100373 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100374
375 l2x0_base = base;
Linus Walleij20c335a2011-09-06 07:45:46 +0100376 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
377 l2x0_cache_id = cache_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378
Catalin Marinas6775a552010-07-28 22:01:25 +0100379 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100380
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100381 aux &= aux_mask;
382 aux |= aux_val;
383
Jason McMullan64039be2010-05-05 18:59:37 +0100384 /* Determine the number of ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100385 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100386 case L2X0_CACHE_ID_PART_L310:
387 if (aux & (1 << 16))
Linus Walleij20c335a2011-09-06 07:45:46 +0100388 ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100389 else
Linus Walleij20c335a2011-09-06 07:45:46 +0100390 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100391 type = "L310";
392 break;
393 case L2X0_CACHE_ID_PART_L210:
Linus Walleij20c335a2011-09-06 07:45:46 +0100394 ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100395 type = "L210";
396 break;
397 default:
398 /* Assume unknown chips have 8 ways */
Linus Walleij20c335a2011-09-06 07:45:46 +0100399 ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100400 type = "L2x0 series";
401 break;
402 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Linus Walleij20c335a2011-09-06 07:45:46 +0100404 l2x0_way_mask = (1 << ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100405
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100406 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530407 * L2 cache Size = Way size * Number of ways
408 */
409 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Linus Walleij20c335a2011-09-06 07:45:46 +0100410 way_size = 1 << (way_size + 3);
411 l2x0_size = ways * way_size * SZ_1K;
Colin Cross5ea3a7c2011-09-14 15:59:50 -0700412 l2x0_sets = way_size / CACHE_LINE_SIZE;
Linus Walleij20c335a2011-09-06 07:45:46 +0100413 l2x0_ways = ways;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530414
Linus Walleij20c335a2011-09-06 07:45:46 +0100415 /*
416 * Check if l2x0 controller is already enabled.
417 * If you are booting from non-secure mode
418 * accessing the below registers will fault.
419 */
420 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
421 /* Make sure that I&D is not locked down when starting */
422 l2x0_unlock(cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100423
Linus Walleij20c335a2011-09-06 07:45:46 +0100424 /* l2x0 controller is disabled */
425 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100426
Barry Songa9dd8f92011-09-30 14:43:12 +0100427 l2x0_saved_regs.aux_ctrl = aux;
428
Linus Walleij20c335a2011-09-06 07:45:46 +0100429 l2x0_inv_all();
430
431 /* enable L2X0 */
432 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100433 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100434
Linus Walleij20c335a2011-09-06 07:45:46 +0100435 outer_cache.inv_range = l2x0_inv_range;
436 outer_cache.clean_range = l2x0_clean_range;
437 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100438 outer_cache.sync = l2x0_cache_sync;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530440 outer_cache.flush_all = l2x0_flush_all;
441 outer_cache.inv_all = l2x0_inv_all;
442 outer_cache.disable = l2x0_disable;
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100443 outer_cache.set_debug = l2x0_set_debug;
Catalin Marinas382266a2007-02-05 14:48:19 +0100444
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445 mb();
Jason McMullan64039be2010-05-05 18:59:37 +0100446 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530447 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 +0100448 ways, cache_id, aux, l2x0_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700449
Taniya Das38a8c6e2012-05-09 20:34:39 +0530450 /* Save the L2X0 contents, as they are not modified else where */
451 pl310_save();
Sridhar Parasurama0222902012-04-27 11:18:02 -0700452}
453
Rob Herring78ae8b12011-08-03 18:12:05 +0100454#ifdef CONFIG_OF
455static void __init l2x0_of_setup(const struct device_node *np,
Russell King1add9082011-09-18 11:27:30 +0100456 u32 *aux_val, u32 *aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100457{
458 u32 data[2] = { 0, 0 };
459 u32 tag = 0;
460 u32 dirty = 0;
461 u32 val = 0, mask = 0;
462
463 of_property_read_u32(np, "arm,tag-latency", &tag);
464 if (tag) {
465 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
466 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
467 }
468
469 of_property_read_u32_array(np, "arm,data-latency",
470 data, ARRAY_SIZE(data));
471 if (data[0] && data[1]) {
472 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
473 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
474 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
475 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
476 }
477
478 of_property_read_u32(np, "arm,dirty-latency", &dirty);
479 if (dirty) {
480 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
481 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
482 }
483
484 *aux_val &= ~mask;
485 *aux_val |= val;
486 *aux_mask &= ~mask;
487}
488
489static void __init pl310_of_setup(const struct device_node *np,
Russell King1add9082011-09-18 11:27:30 +0100490 u32 *aux_val, u32 *aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100491{
492 u32 data[3] = { 0, 0, 0 };
493 u32 tag[3] = { 0, 0, 0 };
494 u32 filter[2] = { 0, 0 };
495
496 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
497 if (tag[0] && tag[1] && tag[2])
498 writel_relaxed(
499 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
500 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
501 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
502 l2x0_base + L2X0_TAG_LATENCY_CTRL);
503
504 of_property_read_u32_array(np, "arm,data-latency",
505 data, ARRAY_SIZE(data));
506 if (data[0] && data[1] && data[2])
507 writel_relaxed(
508 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
509 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
510 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
511 l2x0_base + L2X0_DATA_LATENCY_CTRL);
512
513 of_property_read_u32_array(np, "arm,filter-ranges",
514 filter, ARRAY_SIZE(filter));
Barry Songce72d042011-09-14 03:20:01 +0100515 if (filter[1]) {
Rob Herring78ae8b12011-08-03 18:12:05 +0100516 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
517 l2x0_base + L2X0_ADDR_FILTER_END);
518 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
519 l2x0_base + L2X0_ADDR_FILTER_START);
520 }
521}
Taniya Das38a8c6e2012-05-09 20:34:39 +0530522#endif
Rob Herring78ae8b12011-08-03 18:12:05 +0100523
Stephen Boyd22ab9342012-04-25 11:42:14 -0700524static void pl310_save(void)
Barry Songa9dd8f92011-09-30 14:43:12 +0100525{
526 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
527 L2X0_CACHE_ID_RTL_MASK;
528
529 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
530 L2X0_TAG_LATENCY_CTRL);
531 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
532 L2X0_DATA_LATENCY_CTRL);
533 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
534 L2X0_ADDR_FILTER_END);
535 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
536 L2X0_ADDR_FILTER_START);
537
538 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
539 /*
540 * From r2p0, there is Prefetch offset/control register
541 */
542 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
543 L2X0_PREFETCH_CTRL);
544 /*
545 * From r3p0, there is Power control register
546 */
547 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
548 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
549 L2X0_POWER_CTRL);
550 }
551}
552
553static void l2x0_resume(void)
554{
555 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
556 /* restore aux ctrl and enable l2 */
557 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
558
559 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
560 L2X0_AUX_CTRL);
561
562 l2x0_inv_all();
563
564 writel_relaxed(1, l2x0_base + L2X0_CTRL);
565 }
566}
567
568static void pl310_resume(void)
569{
570 u32 l2x0_revision;
571
572 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
573 /* restore pl310 setup */
574 writel_relaxed(l2x0_saved_regs.tag_latency,
575 l2x0_base + L2X0_TAG_LATENCY_CTRL);
576 writel_relaxed(l2x0_saved_regs.data_latency,
577 l2x0_base + L2X0_DATA_LATENCY_CTRL);
578 writel_relaxed(l2x0_saved_regs.filter_end,
579 l2x0_base + L2X0_ADDR_FILTER_END);
580 writel_relaxed(l2x0_saved_regs.filter_start,
581 l2x0_base + L2X0_ADDR_FILTER_START);
582
583 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
584 L2X0_CACHE_ID_RTL_MASK;
585
586 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
587 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
588 l2x0_base + L2X0_PREFETCH_CTRL);
589 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
590 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
591 l2x0_base + L2X0_POWER_CTRL);
592 }
593 }
594
595 l2x0_resume();
596}
597
Taniya Das38a8c6e2012-05-09 20:34:39 +0530598#ifdef CONFIG_OF
Barry Songa9dd8f92011-09-30 14:43:12 +0100599static const struct l2x0_of_data pl310_data = {
600 pl310_of_setup,
601 pl310_save,
602 pl310_resume,
603};
604
605static const struct l2x0_of_data l2x0_data = {
606 l2x0_of_setup,
607 NULL,
608 l2x0_resume,
609};
610
Rob Herring78ae8b12011-08-03 18:12:05 +0100611static const struct of_device_id l2x0_ids[] __initconst = {
Barry Songa9dd8f92011-09-30 14:43:12 +0100612 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
613 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
614 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring78ae8b12011-08-03 18:12:05 +0100615 {}
616};
617
Russell King1add9082011-09-18 11:27:30 +0100618int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring78ae8b12011-08-03 18:12:05 +0100619{
620 struct device_node *np;
Barry Songa9dd8f92011-09-30 14:43:12 +0100621 struct l2x0_of_data *data;
622 struct resource res;
Rob Herring78ae8b12011-08-03 18:12:05 +0100623
624 np = of_find_matching_node(NULL, l2x0_ids);
625 if (!np)
626 return -ENODEV;
Barry Songa9dd8f92011-09-30 14:43:12 +0100627
628 if (of_address_to_resource(np, 0, &res))
629 return -ENODEV;
630
631 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring78ae8b12011-08-03 18:12:05 +0100632 if (!l2x0_base)
633 return -ENOMEM;
634
Barry Songa9dd8f92011-09-30 14:43:12 +0100635 l2x0_saved_regs.phy_base = res.start;
636
637 data = of_match_node(l2x0_ids, np)->data;
638
Rob Herring78ae8b12011-08-03 18:12:05 +0100639 /* L2 configuration can only be changed if the cache is disabled */
640 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Songa9dd8f92011-09-30 14:43:12 +0100641 if (data->setup)
642 data->setup(np, &aux_val, &aux_mask);
Rob Herring78ae8b12011-08-03 18:12:05 +0100643 }
Barry Songa9dd8f92011-09-30 14:43:12 +0100644
645 if (data->save)
646 data->save();
647
Rob Herring78ae8b12011-08-03 18:12:05 +0100648 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Songa9dd8f92011-09-30 14:43:12 +0100649
650 outer_cache.resume = data->resume;
Rob Herring78ae8b12011-08-03 18:12:05 +0100651 return 0;
652}
653#endif
Taniya Das38a8c6e2012-05-09 20:34:39 +0530654
655void l2cc_suspend(void)
656{
657 l2x0_disable();
658 dmb();
659}
660
661void l2cc_resume(void)
662{
663 pl310_resume();
664 dmb();
665}