blob: eaa6847eea4f43fa332c83a02124680fb594c5a9 [file] [log] [blame]
Catalin Marinas382266a2007-02-05 14:48:19 +01001/*
2 * arch/arm/mm/cache-l2x0.c - L210/L220 cache controller support
3 *
4 * Copyright (C) 2007 ARM Limited
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Rob Herring8c369262011-08-03 18:12:05 +010019#include <linux/err.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010020#include <linux/init.h>
Catalin Marinas07620972007-07-20 11:42:40 +010021#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
Rob Herring8c369262011-08-03 18:12:05 +010023#include <linux/of.h>
24#include <linux/of_address.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010025
26#include <asm/cacheflush.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010027#include <asm/hardware/cache-l2x0.h>
28
29#define CACHE_LINE_SIZE 32
30
31static void __iomem *l2x0_base;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050032static DEFINE_RAW_SPINLOCK(l2x0_lock);
Russell King3e175ca2011-09-18 11:27:30 +010033static u32 l2x0_way_mask; /* Bitmask of active ways */
34static u32 l2x0_size;
Colin Cross74b6cdd2011-09-14 15:59:50 -070035static u32 l2x0_cache_id;
36static unsigned int l2x0_sets;
37static unsigned int l2x0_ways;
Will Deaconf154fe92012-04-20 17:21:08 +010038static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
Colin Cross74b6cdd2011-09-14 15:59:50 -070039
40static inline bool is_pl310_rev(int rev)
41{
42 return (l2x0_cache_id &
43 (L2X0_CACHE_ID_PART_MASK | L2X0_CACHE_ID_REV_MASK)) ==
44 (L2X0_CACHE_ID_PART_L310 | rev);
45}
Catalin Marinas382266a2007-02-05 14:48:19 +010046
Barry Song91c2ebb2011-09-30 14:43:12 +010047struct l2x0_regs l2x0_saved_regs;
48
49struct l2x0_of_data {
Russell King3e175ca2011-09-18 11:27:30 +010050 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Song91c2ebb2011-09-30 14:43:12 +010051 void (*save)(void);
52 void (*resume)(void);
53};
54
Catalin Marinas9a6655e2010-08-31 13:05:22 +010055static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010056{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010057 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010058 while (readl_relaxed(reg) & mask)
Barry Song1caf3092011-09-09 10:30:34 +010059 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010060}
61
Catalin Marinas9a6655e2010-08-31 13:05:22 +010062#ifdef CONFIG_CACHE_PL310
63static inline void cache_wait(void __iomem *reg, unsigned long mask)
64{
65 /* cache operations by line are atomic on PL310 */
66}
67#else
68#define cache_wait cache_wait_way
69#endif
70
Catalin Marinas382266a2007-02-05 14:48:19 +010071static inline void cache_sync(void)
72{
Russell King3d107432009-11-19 11:41:09 +000073 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010074
Will Deaconf154fe92012-04-20 17:21:08 +010075 writel_relaxed(0, base + sync_reg_offset);
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)
Will Deaconab4d5362012-04-20 17:22:11 +010094static inline void debug_writel(unsigned long val)
95{
96 if (outer_cache.set_debug)
97 outer_cache.set_debug(val);
98}
Santosh Shilimkar9e655822010-02-04 19:42:42 +010099
Will Deaconab4d5362012-04-20 17:22:11 +0100100static void pl310_set_debug(unsigned long val)
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100101{
102 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
103}
104#else
105/* Optimised out for non-errata case */
106static inline void debug_writel(unsigned long val)
107{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100108}
109
Will Deaconab4d5362012-04-20 17:22:11 +0100110#define pl310_set_debug NULL
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100111#endif
112
113#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100114static inline void l2x0_flush_line(unsigned long addr)
115{
116 void __iomem *base = l2x0_base;
117
118 /* Clean by PA followed by Invalidate by PA */
119 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100120 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100121 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100122 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100123}
124#else
125
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100126static inline void l2x0_flush_line(unsigned long addr)
127{
128 void __iomem *base = l2x0_base;
129 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100130 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100131}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100132#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100133
Catalin Marinas23107c52010-03-24 16:48:53 +0100134static void l2x0_cache_sync(void)
135{
136 unsigned long flags;
137
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500138 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100139 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500140 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100141}
142
Colin Cross74b6cdd2011-09-14 15:59:50 -0700143#ifdef CONFIG_PL310_ERRATA_727915
144static void l2x0_for_each_set_way(void __iomem *reg)
145{
146 int set;
147 int way;
148 unsigned long flags;
149
150 for (way = 0; way < l2x0_ways; way++) {
151 raw_spin_lock_irqsave(&l2x0_lock, flags);
152 for (set = 0; set < l2x0_sets; set++)
153 writel_relaxed((way << 28) | (set << 5), reg);
154 cache_sync();
155 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
156 }
157}
158#endif
159
Will Deacon38a89142011-07-01 14:36:19 +0100160static void __l2x0_flush_all(void)
161{
162 debug_writel(0x03);
163 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
164 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
165 cache_sync();
166 debug_writel(0x00);
167}
168
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530169static void l2x0_flush_all(void)
170{
171 unsigned long flags;
172
Colin Cross74b6cdd2011-09-14 15:59:50 -0700173#ifdef CONFIG_PL310_ERRATA_727915
174 if (is_pl310_rev(REV_PL310_R2P0)) {
175 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
176 return;
177 }
178#endif
179
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530180 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500181 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100182 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500183 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530184}
185
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530186static void l2x0_clean_all(void)
187{
188 unsigned long flags;
189
Colin Cross74b6cdd2011-09-14 15:59:50 -0700190#ifdef CONFIG_PL310_ERRATA_727915
191 if (is_pl310_rev(REV_PL310_R2P0)) {
192 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
193 return;
194 }
195#endif
196
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530197 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500198 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross74b6cdd2011-09-14 15:59:50 -0700199 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530200 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
201 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
202 cache_sync();
Colin Cross74b6cdd2011-09-14 15:59:50 -0700203 debug_writel(0x00);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500204 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530205}
206
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530207static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100208{
Russell King0eb948d2009-11-19 11:12:15 +0000209 unsigned long flags;
210
Catalin Marinas382266a2007-02-05 14:48:19 +0100211 /* invalidate all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500212 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530213 /* Invalidating when L2 is enabled is a nono */
214 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100215 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100216 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100217 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500218 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100219}
220
221static void l2x0_inv_range(unsigned long start, unsigned long end)
222{
Russell King3d107432009-11-19 11:41:09 +0000223 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000224 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100225
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500226 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100227 if (start & (CACHE_LINE_SIZE - 1)) {
228 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100229 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100230 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100231 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100232 start += CACHE_LINE_SIZE;
233 }
234
235 if (end & (CACHE_LINE_SIZE - 1)) {
236 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100237 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100238 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100239 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100240 }
241
Russell King0eb948d2009-11-19 11:12:15 +0000242 while (start < end) {
243 unsigned long blk_end = start + min(end - start, 4096UL);
244
245 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100246 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000247 start += CACHE_LINE_SIZE;
248 }
249
250 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500251 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
252 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000253 }
254 }
Russell King3d107432009-11-19 11:41:09 +0000255 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100256 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500257 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100258}
259
260static void l2x0_clean_range(unsigned long start, unsigned long end)
261{
Russell King3d107432009-11-19 11:41:09 +0000262 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000263 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100264
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530265 if ((end - start) >= l2x0_size) {
266 l2x0_clean_all();
267 return;
268 }
269
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500270 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100271 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000272 while (start < end) {
273 unsigned long blk_end = start + min(end - start, 4096UL);
274
275 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100276 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000277 start += CACHE_LINE_SIZE;
278 }
279
280 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500281 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
282 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000283 }
284 }
Russell King3d107432009-11-19 11:41:09 +0000285 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100286 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500287 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100288}
289
290static void l2x0_flush_range(unsigned long start, unsigned long end)
291{
Russell King3d107432009-11-19 11:41:09 +0000292 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000293 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100294
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530295 if ((end - start) >= l2x0_size) {
296 l2x0_flush_all();
297 return;
298 }
299
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500300 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100301 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000302 while (start < end) {
303 unsigned long blk_end = start + min(end - start, 4096UL);
304
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100305 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000306 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100307 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000308 start += CACHE_LINE_SIZE;
309 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100310 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000311
312 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500313 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
314 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000315 }
316 }
Russell King3d107432009-11-19 11:41:09 +0000317 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100318 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500319 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100320}
321
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530322static void l2x0_disable(void)
323{
324 unsigned long flags;
325
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500326 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100327 __l2x0_flush_all();
328 writel_relaxed(0, l2x0_base + L2X0_CTRL);
329 dsb();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500330 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530331}
332
Russell King3e175ca2011-09-18 11:27:30 +0100333static void l2x0_unlock(u32 cache_id)
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100334{
335 int lockregs;
336 int i;
337
338 if (cache_id == L2X0_CACHE_ID_PART_L310)
339 lockregs = 8;
340 else
341 /* L210 and unknown types */
342 lockregs = 1;
343
344 for (i = 0; i < lockregs; i++) {
345 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
346 i * L2X0_LOCKDOWN_STRIDE);
347 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
348 i * L2X0_LOCKDOWN_STRIDE);
349 }
350}
351
Russell King3e175ca2011-09-18 11:27:30 +0100352void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100353{
Russell King3e175ca2011-09-18 11:27:30 +0100354 u32 aux;
Russell King3e175ca2011-09-18 11:27:30 +0100355 u32 way_size = 0;
Jason McMullan64039be2010-05-05 18:59:37 +0100356 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100357
358 l2x0_base = base;
359
Colin Cross74b6cdd2011-09-14 15:59:50 -0700360 l2x0_cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
Catalin Marinas6775a552010-07-28 22:01:25 +0100361 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100362
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100363 aux &= aux_mask;
364 aux |= aux_val;
365
Jason McMullan64039be2010-05-05 18:59:37 +0100366 /* Determine the number of ways */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700367 switch (l2x0_cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100368 case L2X0_CACHE_ID_PART_L310:
369 if (aux & (1 << 16))
Colin Cross74b6cdd2011-09-14 15:59:50 -0700370 l2x0_ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100371 else
Colin Cross74b6cdd2011-09-14 15:59:50 -0700372 l2x0_ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100373 type = "L310";
Will Deaconf154fe92012-04-20 17:21:08 +0100374#ifdef CONFIG_PL310_ERRATA_753970
375 /* Unmapped register. */
376 sync_reg_offset = L2X0_DUMMY_REG;
377#endif
Will Deaconab4d5362012-04-20 17:22:11 +0100378 outer_cache.set_debug = pl310_set_debug;
Jason McMullan64039be2010-05-05 18:59:37 +0100379 break;
380 case L2X0_CACHE_ID_PART_L210:
Colin Cross74b6cdd2011-09-14 15:59:50 -0700381 l2x0_ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100382 type = "L210";
383 break;
384 default:
385 /* Assume unknown chips have 8 ways */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700386 l2x0_ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100387 type = "L2x0 series";
388 break;
389 }
390
Colin Cross74b6cdd2011-09-14 15:59:50 -0700391 l2x0_way_mask = (1 << l2x0_ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100392
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100393 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530394 * L2 cache Size = Way size * Number of ways
395 */
396 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Colin Cross74b6cdd2011-09-14 15:59:50 -0700397 way_size = SZ_1K << (way_size + 3);
398 l2x0_size = l2x0_ways * way_size;
399 l2x0_sets = way_size / CACHE_LINE_SIZE;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530400
401 /*
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100402 * Check if l2x0 controller is already enabled.
403 * If you are booting from non-secure mode
404 * accessing the below registers will fault.
405 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100406 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100407 /* Make sure that I&D is not locked down when starting */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700408 l2x0_unlock(l2x0_cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100409
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100410 /* l2x0 controller is disabled */
Catalin Marinas6775a552010-07-28 22:01:25 +0100411 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100412
Barry Song91c2ebb2011-09-30 14:43:12 +0100413 l2x0_saved_regs.aux_ctrl = aux;
414
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100415 l2x0_inv_all();
416
417 /* enable L2X0 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100418 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100419 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100420
421 outer_cache.inv_range = l2x0_inv_range;
422 outer_cache.clean_range = l2x0_clean_range;
423 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100424 outer_cache.sync = l2x0_cache_sync;
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530425 outer_cache.flush_all = l2x0_flush_all;
426 outer_cache.inv_all = l2x0_inv_all;
427 outer_cache.disable = l2x0_disable;
Catalin Marinas382266a2007-02-05 14:48:19 +0100428
Jason McMullan64039be2010-05-05 18:59:37 +0100429 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530430 printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n",
Colin Cross74b6cdd2011-09-14 15:59:50 -0700431 l2x0_ways, l2x0_cache_id, aux, l2x0_size);
Catalin Marinas382266a2007-02-05 14:48:19 +0100432}
Rob Herring8c369262011-08-03 18:12:05 +0100433
434#ifdef CONFIG_OF
435static void __init l2x0_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100436 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100437{
438 u32 data[2] = { 0, 0 };
439 u32 tag = 0;
440 u32 dirty = 0;
441 u32 val = 0, mask = 0;
442
443 of_property_read_u32(np, "arm,tag-latency", &tag);
444 if (tag) {
445 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
446 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
447 }
448
449 of_property_read_u32_array(np, "arm,data-latency",
450 data, ARRAY_SIZE(data));
451 if (data[0] && data[1]) {
452 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
453 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
454 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
455 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
456 }
457
458 of_property_read_u32(np, "arm,dirty-latency", &dirty);
459 if (dirty) {
460 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
461 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
462 }
463
464 *aux_val &= ~mask;
465 *aux_val |= val;
466 *aux_mask &= ~mask;
467}
468
469static void __init pl310_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100470 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100471{
472 u32 data[3] = { 0, 0, 0 };
473 u32 tag[3] = { 0, 0, 0 };
474 u32 filter[2] = { 0, 0 };
475
476 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
477 if (tag[0] && tag[1] && tag[2])
478 writel_relaxed(
479 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
480 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
481 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
482 l2x0_base + L2X0_TAG_LATENCY_CTRL);
483
484 of_property_read_u32_array(np, "arm,data-latency",
485 data, ARRAY_SIZE(data));
486 if (data[0] && data[1] && data[2])
487 writel_relaxed(
488 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
489 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
490 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
491 l2x0_base + L2X0_DATA_LATENCY_CTRL);
492
493 of_property_read_u32_array(np, "arm,filter-ranges",
494 filter, ARRAY_SIZE(filter));
Barry Song74d41f32011-09-14 03:20:01 +0100495 if (filter[1]) {
Rob Herring8c369262011-08-03 18:12:05 +0100496 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
497 l2x0_base + L2X0_ADDR_FILTER_END);
498 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
499 l2x0_base + L2X0_ADDR_FILTER_START);
500 }
501}
502
Barry Song91c2ebb2011-09-30 14:43:12 +0100503static void __init pl310_save(void)
504{
505 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
506 L2X0_CACHE_ID_RTL_MASK;
507
508 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
509 L2X0_TAG_LATENCY_CTRL);
510 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
511 L2X0_DATA_LATENCY_CTRL);
512 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
513 L2X0_ADDR_FILTER_END);
514 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
515 L2X0_ADDR_FILTER_START);
516
517 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
518 /*
519 * From r2p0, there is Prefetch offset/control register
520 */
521 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
522 L2X0_PREFETCH_CTRL);
523 /*
524 * From r3p0, there is Power control register
525 */
526 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
527 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
528 L2X0_POWER_CTRL);
529 }
530}
531
532static void l2x0_resume(void)
533{
534 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
535 /* restore aux ctrl and enable l2 */
536 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
537
538 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
539 L2X0_AUX_CTRL);
540
541 l2x0_inv_all();
542
543 writel_relaxed(1, l2x0_base + L2X0_CTRL);
544 }
545}
546
547static void pl310_resume(void)
548{
549 u32 l2x0_revision;
550
551 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
552 /* restore pl310 setup */
553 writel_relaxed(l2x0_saved_regs.tag_latency,
554 l2x0_base + L2X0_TAG_LATENCY_CTRL);
555 writel_relaxed(l2x0_saved_regs.data_latency,
556 l2x0_base + L2X0_DATA_LATENCY_CTRL);
557 writel_relaxed(l2x0_saved_regs.filter_end,
558 l2x0_base + L2X0_ADDR_FILTER_END);
559 writel_relaxed(l2x0_saved_regs.filter_start,
560 l2x0_base + L2X0_ADDR_FILTER_START);
561
562 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
563 L2X0_CACHE_ID_RTL_MASK;
564
565 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
566 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
567 l2x0_base + L2X0_PREFETCH_CTRL);
568 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
569 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
570 l2x0_base + L2X0_POWER_CTRL);
571 }
572 }
573
574 l2x0_resume();
575}
576
577static const struct l2x0_of_data pl310_data = {
578 pl310_of_setup,
579 pl310_save,
580 pl310_resume,
581};
582
583static const struct l2x0_of_data l2x0_data = {
584 l2x0_of_setup,
585 NULL,
586 l2x0_resume,
587};
588
Rob Herring8c369262011-08-03 18:12:05 +0100589static const struct of_device_id l2x0_ids[] __initconst = {
Barry Song91c2ebb2011-09-30 14:43:12 +0100590 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
591 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
592 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring8c369262011-08-03 18:12:05 +0100593 {}
594};
595
Russell King3e175ca2011-09-18 11:27:30 +0100596int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100597{
598 struct device_node *np;
Barry Song91c2ebb2011-09-30 14:43:12 +0100599 struct l2x0_of_data *data;
600 struct resource res;
Rob Herring8c369262011-08-03 18:12:05 +0100601
602 np = of_find_matching_node(NULL, l2x0_ids);
603 if (!np)
604 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +0100605
606 if (of_address_to_resource(np, 0, &res))
607 return -ENODEV;
608
609 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +0100610 if (!l2x0_base)
611 return -ENOMEM;
612
Barry Song91c2ebb2011-09-30 14:43:12 +0100613 l2x0_saved_regs.phy_base = res.start;
614
615 data = of_match_node(l2x0_ids, np)->data;
616
Rob Herring8c369262011-08-03 18:12:05 +0100617 /* L2 configuration can only be changed if the cache is disabled */
618 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Song91c2ebb2011-09-30 14:43:12 +0100619 if (data->setup)
620 data->setup(np, &aux_val, &aux_mask);
Rob Herring8c369262011-08-03 18:12:05 +0100621 }
Barry Song91c2ebb2011-09-30 14:43:12 +0100622
623 if (data->save)
624 data->save();
625
Rob Herring8c369262011-08-03 18:12:05 +0100626 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Song91c2ebb2011-09-30 14:43:12 +0100627
628 outer_cache.resume = data->resume;
Rob Herring8c369262011-08-03 18:12:05 +0100629 return 0;
630}
631#endif