blob: a8d02c048a1f4325f63992f603f888eee56c2d4c [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;
Will Deaconf154fe92012-04-20 17:21:08 +010035static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
Catalin Marinas382266a2007-02-05 14:48:19 +010036
Barry Song91c2ebb2011-09-30 14:43:12 +010037struct l2x0_regs l2x0_saved_regs;
38
39struct l2x0_of_data {
Russell King3e175ca2011-09-18 11:27:30 +010040 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Song91c2ebb2011-09-30 14:43:12 +010041 void (*save)(void);
42 void (*resume)(void);
43};
44
Catalin Marinas9a6655e2010-08-31 13:05:22 +010045static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010046{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010047 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010048 while (readl_relaxed(reg) & mask)
Barry Song1caf3092011-09-09 10:30:34 +010049 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010050}
51
Catalin Marinas9a6655e2010-08-31 13:05:22 +010052#ifdef CONFIG_CACHE_PL310
53static inline void cache_wait(void __iomem *reg, unsigned long mask)
54{
55 /* cache operations by line are atomic on PL310 */
56}
57#else
58#define cache_wait cache_wait_way
59#endif
60
Catalin Marinas382266a2007-02-05 14:48:19 +010061static inline void cache_sync(void)
62{
Russell King3d107432009-11-19 11:41:09 +000063 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010064
Will Deaconf154fe92012-04-20 17:21:08 +010065 writel_relaxed(0, base + sync_reg_offset);
Russell King3d107432009-11-19 11:41:09 +000066 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010067}
68
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010069static inline void l2x0_clean_line(unsigned long addr)
70{
71 void __iomem *base = l2x0_base;
72 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010073 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010074}
75
76static inline void l2x0_inv_line(unsigned long addr)
77{
78 void __iomem *base = l2x0_base;
79 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010080 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010081}
82
Santosh Shilimkar2839e062011-03-08 06:59:54 +010083#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Santosh Shilimkar9e655822010-02-04 19:42:42 +010084
Santosh Shilimkar2839e062011-03-08 06:59:54 +010085#define debug_writel(val) outer_cache.set_debug(val)
86
87static void l2x0_set_debug(unsigned long val)
88{
89 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
90}
91#else
92/* Optimised out for non-errata case */
93static inline void debug_writel(unsigned long val)
94{
Santosh Shilimkar9e655822010-02-04 19:42:42 +010095}
96
Santosh Shilimkar2839e062011-03-08 06:59:54 +010097#define l2x0_set_debug NULL
98#endif
99
100#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100101static inline void l2x0_flush_line(unsigned long addr)
102{
103 void __iomem *base = l2x0_base;
104
105 /* Clean by PA followed by Invalidate by PA */
106 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100107 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100108 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100109 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100110}
111#else
112
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100113static inline void l2x0_flush_line(unsigned long addr)
114{
115 void __iomem *base = l2x0_base;
116 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100117 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100118}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100119#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100120
Catalin Marinas23107c52010-03-24 16:48:53 +0100121static void l2x0_cache_sync(void)
122{
123 unsigned long flags;
124
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500125 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100126 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500127 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100128}
129
Will Deacon38a89142011-07-01 14:36:19 +0100130static void __l2x0_flush_all(void)
131{
132 debug_writel(0x03);
133 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
134 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
135 cache_sync();
136 debug_writel(0x00);
137}
138
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530139static void l2x0_flush_all(void)
140{
141 unsigned long flags;
142
143 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500144 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100145 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500146 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530147}
148
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530149static void l2x0_clean_all(void)
150{
151 unsigned long flags;
152
153 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500154 raw_spin_lock_irqsave(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530155 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
156 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
157 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500158 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530159}
160
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530161static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100162{
Russell King0eb948d2009-11-19 11:12:15 +0000163 unsigned long flags;
164
Catalin Marinas382266a2007-02-05 14:48:19 +0100165 /* invalidate all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500166 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530167 /* Invalidating when L2 is enabled is a nono */
168 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100169 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100170 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100171 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500172 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100173}
174
175static void l2x0_inv_range(unsigned long start, unsigned long end)
176{
Russell King3d107432009-11-19 11:41:09 +0000177 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000178 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100179
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500180 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100181 if (start & (CACHE_LINE_SIZE - 1)) {
182 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100183 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100184 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100185 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100186 start += CACHE_LINE_SIZE;
187 }
188
189 if (end & (CACHE_LINE_SIZE - 1)) {
190 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100191 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100192 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100193 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100194 }
195
Russell King0eb948d2009-11-19 11:12:15 +0000196 while (start < end) {
197 unsigned long blk_end = start + min(end - start, 4096UL);
198
199 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100200 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000201 start += CACHE_LINE_SIZE;
202 }
203
204 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500205 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
206 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000207 }
208 }
Russell King3d107432009-11-19 11:41:09 +0000209 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100210 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500211 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100212}
213
214static void l2x0_clean_range(unsigned long start, unsigned long end)
215{
Russell King3d107432009-11-19 11:41:09 +0000216 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000217 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100218
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530219 if ((end - start) >= l2x0_size) {
220 l2x0_clean_all();
221 return;
222 }
223
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500224 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100225 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000226 while (start < end) {
227 unsigned long blk_end = start + min(end - start, 4096UL);
228
229 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100230 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000231 start += CACHE_LINE_SIZE;
232 }
233
234 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500235 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
236 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000237 }
238 }
Russell King3d107432009-11-19 11:41:09 +0000239 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100240 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500241 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100242}
243
244static void l2x0_flush_range(unsigned long start, unsigned long end)
245{
Russell King3d107432009-11-19 11:41:09 +0000246 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000247 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100248
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530249 if ((end - start) >= l2x0_size) {
250 l2x0_flush_all();
251 return;
252 }
253
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500254 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100255 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000256 while (start < end) {
257 unsigned long blk_end = start + min(end - start, 4096UL);
258
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100259 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000260 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100261 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000262 start += CACHE_LINE_SIZE;
263 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100264 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000265
266 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500267 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
268 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000269 }
270 }
Russell King3d107432009-11-19 11:41:09 +0000271 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100272 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500273 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100274}
275
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530276static void l2x0_disable(void)
277{
278 unsigned long flags;
279
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500280 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100281 __l2x0_flush_all();
282 writel_relaxed(0, l2x0_base + L2X0_CTRL);
283 dsb();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500284 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530285}
286
Russell King3e175ca2011-09-18 11:27:30 +0100287static void l2x0_unlock(u32 cache_id)
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100288{
289 int lockregs;
290 int i;
291
292 if (cache_id == L2X0_CACHE_ID_PART_L310)
293 lockregs = 8;
294 else
295 /* L210 and unknown types */
296 lockregs = 1;
297
298 for (i = 0; i < lockregs; i++) {
299 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
300 i * L2X0_LOCKDOWN_STRIDE);
301 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
302 i * L2X0_LOCKDOWN_STRIDE);
303 }
304}
305
Russell King3e175ca2011-09-18 11:27:30 +0100306void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100307{
Russell King3e175ca2011-09-18 11:27:30 +0100308 u32 aux;
309 u32 cache_id;
310 u32 way_size = 0;
Jason McMullan64039be2010-05-05 18:59:37 +0100311 int ways;
312 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100313
314 l2x0_base = base;
315
Catalin Marinas6775a552010-07-28 22:01:25 +0100316 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
317 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100318
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100319 aux &= aux_mask;
320 aux |= aux_val;
321
Jason McMullan64039be2010-05-05 18:59:37 +0100322 /* Determine the number of ways */
323 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
324 case L2X0_CACHE_ID_PART_L310:
325 if (aux & (1 << 16))
326 ways = 16;
327 else
328 ways = 8;
329 type = "L310";
Will Deaconf154fe92012-04-20 17:21:08 +0100330#ifdef CONFIG_PL310_ERRATA_753970
331 /* Unmapped register. */
332 sync_reg_offset = L2X0_DUMMY_REG;
333#endif
Jason McMullan64039be2010-05-05 18:59:37 +0100334 break;
335 case L2X0_CACHE_ID_PART_L210:
336 ways = (aux >> 13) & 0xf;
337 type = "L210";
338 break;
339 default:
340 /* Assume unknown chips have 8 ways */
341 ways = 8;
342 type = "L2x0 series";
343 break;
344 }
345
346 l2x0_way_mask = (1 << ways) - 1;
347
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100348 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530349 * L2 cache Size = Way size * Number of ways
350 */
351 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
352 way_size = 1 << (way_size + 3);
353 l2x0_size = ways * way_size * SZ_1K;
354
355 /*
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100356 * Check if l2x0 controller is already enabled.
357 * If you are booting from non-secure mode
358 * accessing the below registers will fault.
359 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100360 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100361 /* Make sure that I&D is not locked down when starting */
362 l2x0_unlock(cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100363
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100364 /* l2x0 controller is disabled */
Catalin Marinas6775a552010-07-28 22:01:25 +0100365 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100366
Barry Song91c2ebb2011-09-30 14:43:12 +0100367 l2x0_saved_regs.aux_ctrl = aux;
368
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100369 l2x0_inv_all();
370
371 /* enable L2X0 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100372 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100373 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100374
375 outer_cache.inv_range = l2x0_inv_range;
376 outer_cache.clean_range = l2x0_clean_range;
377 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100378 outer_cache.sync = l2x0_cache_sync;
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530379 outer_cache.flush_all = l2x0_flush_all;
380 outer_cache.inv_all = l2x0_inv_all;
381 outer_cache.disable = l2x0_disable;
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100382 outer_cache.set_debug = l2x0_set_debug;
Catalin Marinas382266a2007-02-05 14:48:19 +0100383
Jason McMullan64039be2010-05-05 18:59:37 +0100384 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530385 printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n",
386 ways, cache_id, aux, l2x0_size);
Catalin Marinas382266a2007-02-05 14:48:19 +0100387}
Rob Herring8c369262011-08-03 18:12:05 +0100388
389#ifdef CONFIG_OF
390static void __init l2x0_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100391 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100392{
393 u32 data[2] = { 0, 0 };
394 u32 tag = 0;
395 u32 dirty = 0;
396 u32 val = 0, mask = 0;
397
398 of_property_read_u32(np, "arm,tag-latency", &tag);
399 if (tag) {
400 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
401 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
402 }
403
404 of_property_read_u32_array(np, "arm,data-latency",
405 data, ARRAY_SIZE(data));
406 if (data[0] && data[1]) {
407 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
408 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
409 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
410 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
411 }
412
413 of_property_read_u32(np, "arm,dirty-latency", &dirty);
414 if (dirty) {
415 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
416 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
417 }
418
419 *aux_val &= ~mask;
420 *aux_val |= val;
421 *aux_mask &= ~mask;
422}
423
424static void __init pl310_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100425 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100426{
427 u32 data[3] = { 0, 0, 0 };
428 u32 tag[3] = { 0, 0, 0 };
429 u32 filter[2] = { 0, 0 };
430
431 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
432 if (tag[0] && tag[1] && tag[2])
433 writel_relaxed(
434 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
435 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
436 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
437 l2x0_base + L2X0_TAG_LATENCY_CTRL);
438
439 of_property_read_u32_array(np, "arm,data-latency",
440 data, ARRAY_SIZE(data));
441 if (data[0] && data[1] && data[2])
442 writel_relaxed(
443 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
444 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
445 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
446 l2x0_base + L2X0_DATA_LATENCY_CTRL);
447
448 of_property_read_u32_array(np, "arm,filter-ranges",
449 filter, ARRAY_SIZE(filter));
Barry Song74d41f32011-09-14 03:20:01 +0100450 if (filter[1]) {
Rob Herring8c369262011-08-03 18:12:05 +0100451 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
452 l2x0_base + L2X0_ADDR_FILTER_END);
453 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
454 l2x0_base + L2X0_ADDR_FILTER_START);
455 }
456}
457
Barry Song91c2ebb2011-09-30 14:43:12 +0100458static void __init pl310_save(void)
459{
460 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
461 L2X0_CACHE_ID_RTL_MASK;
462
463 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
464 L2X0_TAG_LATENCY_CTRL);
465 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
466 L2X0_DATA_LATENCY_CTRL);
467 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
468 L2X0_ADDR_FILTER_END);
469 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
470 L2X0_ADDR_FILTER_START);
471
472 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
473 /*
474 * From r2p0, there is Prefetch offset/control register
475 */
476 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
477 L2X0_PREFETCH_CTRL);
478 /*
479 * From r3p0, there is Power control register
480 */
481 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
482 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
483 L2X0_POWER_CTRL);
484 }
485}
486
487static void l2x0_resume(void)
488{
489 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
490 /* restore aux ctrl and enable l2 */
491 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
492
493 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
494 L2X0_AUX_CTRL);
495
496 l2x0_inv_all();
497
498 writel_relaxed(1, l2x0_base + L2X0_CTRL);
499 }
500}
501
502static void pl310_resume(void)
503{
504 u32 l2x0_revision;
505
506 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
507 /* restore pl310 setup */
508 writel_relaxed(l2x0_saved_regs.tag_latency,
509 l2x0_base + L2X0_TAG_LATENCY_CTRL);
510 writel_relaxed(l2x0_saved_regs.data_latency,
511 l2x0_base + L2X0_DATA_LATENCY_CTRL);
512 writel_relaxed(l2x0_saved_regs.filter_end,
513 l2x0_base + L2X0_ADDR_FILTER_END);
514 writel_relaxed(l2x0_saved_regs.filter_start,
515 l2x0_base + L2X0_ADDR_FILTER_START);
516
517 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
518 L2X0_CACHE_ID_RTL_MASK;
519
520 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
521 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
522 l2x0_base + L2X0_PREFETCH_CTRL);
523 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
524 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
525 l2x0_base + L2X0_POWER_CTRL);
526 }
527 }
528
529 l2x0_resume();
530}
531
532static const struct l2x0_of_data pl310_data = {
533 pl310_of_setup,
534 pl310_save,
535 pl310_resume,
536};
537
538static const struct l2x0_of_data l2x0_data = {
539 l2x0_of_setup,
540 NULL,
541 l2x0_resume,
542};
543
Rob Herring8c369262011-08-03 18:12:05 +0100544static const struct of_device_id l2x0_ids[] __initconst = {
Barry Song91c2ebb2011-09-30 14:43:12 +0100545 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
546 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
547 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring8c369262011-08-03 18:12:05 +0100548 {}
549};
550
Russell King3e175ca2011-09-18 11:27:30 +0100551int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100552{
553 struct device_node *np;
Barry Song91c2ebb2011-09-30 14:43:12 +0100554 struct l2x0_of_data *data;
555 struct resource res;
Rob Herring8c369262011-08-03 18:12:05 +0100556
557 np = of_find_matching_node(NULL, l2x0_ids);
558 if (!np)
559 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +0100560
561 if (of_address_to_resource(np, 0, &res))
562 return -ENODEV;
563
564 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +0100565 if (!l2x0_base)
566 return -ENOMEM;
567
Barry Song91c2ebb2011-09-30 14:43:12 +0100568 l2x0_saved_regs.phy_base = res.start;
569
570 data = of_match_node(l2x0_ids, np)->data;
571
Rob Herring8c369262011-08-03 18:12:05 +0100572 /* L2 configuration can only be changed if the cache is disabled */
573 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Song91c2ebb2011-09-30 14:43:12 +0100574 if (data->setup)
575 data->setup(np, &aux_val, &aux_mask);
Rob Herring8c369262011-08-03 18:12:05 +0100576 }
Barry Song91c2ebb2011-09-30 14:43:12 +0100577
578 if (data->save)
579 data->save();
580
Rob Herring8c369262011-08-03 18:12:05 +0100581 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Song91c2ebb2011-09-30 14:43:12 +0100582
583 outer_cache.resume = data->resume;
Rob Herring8c369262011-08-03 18:12:05 +0100584 return 0;
585}
586#endif