blob: db55d18691ed1357f3971c39f7a7f6733bd67157 [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);
Gregory CLEMENT6248d062012-10-01 10:56:42 +010042 struct outer_cache_fns outer_cache;
Barry Song91c2ebb2011-09-30 14:43:12 +010043};
44
Gregory CLEMENT6248d062012-10-01 10:56:42 +010045static bool of_init = false;
46
Catalin Marinas9a6655e2010-08-31 13:05:22 +010047static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010048{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010049 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010050 while (readl_relaxed(reg) & mask)
Barry Song1caf3092011-09-09 10:30:34 +010051 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010052}
53
Catalin Marinas9a6655e2010-08-31 13:05:22 +010054#ifdef CONFIG_CACHE_PL310
55static inline void cache_wait(void __iomem *reg, unsigned long mask)
56{
57 /* cache operations by line are atomic on PL310 */
58}
59#else
60#define cache_wait cache_wait_way
61#endif
62
Catalin Marinas382266a2007-02-05 14:48:19 +010063static inline void cache_sync(void)
64{
Russell King3d107432009-11-19 11:41:09 +000065 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010066
Will Deaconf154fe92012-04-20 17:21:08 +010067 writel_relaxed(0, base + sync_reg_offset);
Russell King3d107432009-11-19 11:41:09 +000068 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010069}
70
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010071static inline void l2x0_clean_line(unsigned long addr)
72{
73 void __iomem *base = l2x0_base;
74 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010075 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010076}
77
78static inline void l2x0_inv_line(unsigned long addr)
79{
80 void __iomem *base = l2x0_base;
81 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010082 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010083}
84
Santosh Shilimkar2839e062011-03-08 06:59:54 +010085#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Will Deaconab4d5362012-04-20 17:22:11 +010086static inline void debug_writel(unsigned long val)
87{
88 if (outer_cache.set_debug)
89 outer_cache.set_debug(val);
90}
Santosh Shilimkar9e655822010-02-04 19:42:42 +010091
Will Deaconab4d5362012-04-20 17:22:11 +010092static void pl310_set_debug(unsigned long val)
Santosh Shilimkar2839e062011-03-08 06:59:54 +010093{
94 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
95}
96#else
97/* Optimised out for non-errata case */
98static inline void debug_writel(unsigned long val)
99{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100100}
101
Will Deaconab4d5362012-04-20 17:22:11 +0100102#define pl310_set_debug NULL
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100103#endif
104
105#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100106static inline void l2x0_flush_line(unsigned long addr)
107{
108 void __iomem *base = l2x0_base;
109
110 /* Clean by PA followed by Invalidate by PA */
111 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100112 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100113 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100114 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100115}
116#else
117
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100118static inline void l2x0_flush_line(unsigned long addr)
119{
120 void __iomem *base = l2x0_base;
121 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100122 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100123}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100124#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100125
Catalin Marinas23107c52010-03-24 16:48:53 +0100126static void l2x0_cache_sync(void)
127{
128 unsigned long flags;
129
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500130 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100131 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500132 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100133}
134
Will Deacon38a89142011-07-01 14:36:19 +0100135static void __l2x0_flush_all(void)
136{
137 debug_writel(0x03);
138 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
139 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
140 cache_sync();
141 debug_writel(0x00);
142}
143
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530144static void l2x0_flush_all(void)
145{
146 unsigned long flags;
147
148 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500149 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100150 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500151 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530152}
153
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530154static void l2x0_clean_all(void)
155{
156 unsigned long flags;
157
158 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500159 raw_spin_lock_irqsave(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530160 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
161 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
162 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500163 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530164}
165
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530166static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100167{
Russell King0eb948d2009-11-19 11:12:15 +0000168 unsigned long flags;
169
Catalin Marinas382266a2007-02-05 14:48:19 +0100170 /* invalidate all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500171 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530172 /* Invalidating when L2 is enabled is a nono */
173 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100174 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100175 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100176 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500177 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100178}
179
180static void l2x0_inv_range(unsigned long start, unsigned long end)
181{
Russell King3d107432009-11-19 11:41:09 +0000182 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000183 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100184
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500185 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100186 if (start & (CACHE_LINE_SIZE - 1)) {
187 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100188 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100189 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100190 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100191 start += CACHE_LINE_SIZE;
192 }
193
194 if (end & (CACHE_LINE_SIZE - 1)) {
195 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100196 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100197 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100198 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100199 }
200
Russell King0eb948d2009-11-19 11:12:15 +0000201 while (start < end) {
202 unsigned long blk_end = start + min(end - start, 4096UL);
203
204 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100205 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000206 start += CACHE_LINE_SIZE;
207 }
208
209 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500210 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
211 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000212 }
213 }
Russell King3d107432009-11-19 11:41:09 +0000214 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100215 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500216 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100217}
218
219static void l2x0_clean_range(unsigned long start, unsigned long end)
220{
Russell King3d107432009-11-19 11:41:09 +0000221 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000222 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100223
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530224 if ((end - start) >= l2x0_size) {
225 l2x0_clean_all();
226 return;
227 }
228
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500229 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100230 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000231 while (start < end) {
232 unsigned long blk_end = start + min(end - start, 4096UL);
233
234 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100235 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000236 start += CACHE_LINE_SIZE;
237 }
238
239 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500240 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
241 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000242 }
243 }
Russell King3d107432009-11-19 11:41:09 +0000244 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100245 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500246 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100247}
248
249static void l2x0_flush_range(unsigned long start, unsigned long end)
250{
Russell King3d107432009-11-19 11:41:09 +0000251 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000252 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100253
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530254 if ((end - start) >= l2x0_size) {
255 l2x0_flush_all();
256 return;
257 }
258
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500259 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100260 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000261 while (start < end) {
262 unsigned long blk_end = start + min(end - start, 4096UL);
263
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100264 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000265 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100266 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000267 start += CACHE_LINE_SIZE;
268 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100269 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000270
271 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500272 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
273 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000274 }
275 }
Russell King3d107432009-11-19 11:41:09 +0000276 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100277 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500278 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100279}
280
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530281static void l2x0_disable(void)
282{
283 unsigned long flags;
284
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500285 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100286 __l2x0_flush_all();
287 writel_relaxed(0, l2x0_base + L2X0_CTRL);
288 dsb();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500289 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530290}
291
Russell King3e175ca2011-09-18 11:27:30 +0100292static void l2x0_unlock(u32 cache_id)
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100293{
294 int lockregs;
295 int i;
296
297 if (cache_id == L2X0_CACHE_ID_PART_L310)
298 lockregs = 8;
299 else
300 /* L210 and unknown types */
301 lockregs = 1;
302
303 for (i = 0; i < lockregs; i++) {
304 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
305 i * L2X0_LOCKDOWN_STRIDE);
306 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
307 i * L2X0_LOCKDOWN_STRIDE);
308 }
309}
310
Russell King3e175ca2011-09-18 11:27:30 +0100311void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100312{
Russell King3e175ca2011-09-18 11:27:30 +0100313 u32 aux;
314 u32 cache_id;
315 u32 way_size = 0;
Jason McMullan64039be2010-05-05 18:59:37 +0100316 int ways;
317 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100318
319 l2x0_base = base;
320
Catalin Marinas6775a552010-07-28 22:01:25 +0100321 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
322 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100323
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100324 aux &= aux_mask;
325 aux |= aux_val;
326
Jason McMullan64039be2010-05-05 18:59:37 +0100327 /* Determine the number of ways */
328 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
329 case L2X0_CACHE_ID_PART_L310:
330 if (aux & (1 << 16))
331 ways = 16;
332 else
333 ways = 8;
334 type = "L310";
Will Deaconf154fe92012-04-20 17:21:08 +0100335#ifdef CONFIG_PL310_ERRATA_753970
336 /* Unmapped register. */
337 sync_reg_offset = L2X0_DUMMY_REG;
338#endif
Will Deaconab4d5362012-04-20 17:22:11 +0100339 outer_cache.set_debug = pl310_set_debug;
Jason McMullan64039be2010-05-05 18:59:37 +0100340 break;
341 case L2X0_CACHE_ID_PART_L210:
342 ways = (aux >> 13) & 0xf;
343 type = "L210";
344 break;
345 default:
346 /* Assume unknown chips have 8 ways */
347 ways = 8;
348 type = "L2x0 series";
349 break;
350 }
351
352 l2x0_way_mask = (1 << ways) - 1;
353
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100354 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530355 * L2 cache Size = Way size * Number of ways
356 */
357 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
358 way_size = 1 << (way_size + 3);
359 l2x0_size = ways * way_size * SZ_1K;
360
361 /*
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100362 * Check if l2x0 controller is already enabled.
363 * If you are booting from non-secure mode
364 * accessing the below registers will fault.
365 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100366 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100367 /* Make sure that I&D is not locked down when starting */
368 l2x0_unlock(cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100369
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100370 /* l2x0 controller is disabled */
Catalin Marinas6775a552010-07-28 22:01:25 +0100371 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100372
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100373 l2x0_inv_all();
374
375 /* enable L2X0 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100376 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100377 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100378
Yilu Mao9d4876f2012-09-03 09:14:56 +0100379 /* Re-read it in case some bits are reserved. */
380 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
381
382 /* Save the value for resuming. */
383 l2x0_saved_regs.aux_ctrl = aux;
384
Gregory CLEMENT6248d062012-10-01 10:56:42 +0100385 if (!of_init) {
386 outer_cache.inv_range = l2x0_inv_range;
387 outer_cache.clean_range = l2x0_clean_range;
388 outer_cache.flush_range = l2x0_flush_range;
389 outer_cache.sync = l2x0_cache_sync;
390 outer_cache.flush_all = l2x0_flush_all;
391 outer_cache.inv_all = l2x0_inv_all;
392 outer_cache.disable = l2x0_disable;
393 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100394
Jason McMullan64039be2010-05-05 18:59:37 +0100395 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530396 printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n",
397 ways, cache_id, aux, l2x0_size);
Catalin Marinas382266a2007-02-05 14:48:19 +0100398}
Rob Herring8c369262011-08-03 18:12:05 +0100399
400#ifdef CONFIG_OF
401static void __init l2x0_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100402 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100403{
404 u32 data[2] = { 0, 0 };
405 u32 tag = 0;
406 u32 dirty = 0;
407 u32 val = 0, mask = 0;
408
409 of_property_read_u32(np, "arm,tag-latency", &tag);
410 if (tag) {
411 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
412 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
413 }
414
415 of_property_read_u32_array(np, "arm,data-latency",
416 data, ARRAY_SIZE(data));
417 if (data[0] && data[1]) {
418 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
419 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
420 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
421 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
422 }
423
424 of_property_read_u32(np, "arm,dirty-latency", &dirty);
425 if (dirty) {
426 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
427 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
428 }
429
430 *aux_val &= ~mask;
431 *aux_val |= val;
432 *aux_mask &= ~mask;
433}
434
435static void __init pl310_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[3] = { 0, 0, 0 };
439 u32 tag[3] = { 0, 0, 0 };
440 u32 filter[2] = { 0, 0 };
441
442 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
443 if (tag[0] && tag[1] && tag[2])
444 writel_relaxed(
445 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
446 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
447 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
448 l2x0_base + L2X0_TAG_LATENCY_CTRL);
449
450 of_property_read_u32_array(np, "arm,data-latency",
451 data, ARRAY_SIZE(data));
452 if (data[0] && data[1] && data[2])
453 writel_relaxed(
454 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
455 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
456 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
457 l2x0_base + L2X0_DATA_LATENCY_CTRL);
458
459 of_property_read_u32_array(np, "arm,filter-ranges",
460 filter, ARRAY_SIZE(filter));
Barry Song74d41f32011-09-14 03:20:01 +0100461 if (filter[1]) {
Rob Herring8c369262011-08-03 18:12:05 +0100462 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
463 l2x0_base + L2X0_ADDR_FILTER_END);
464 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
465 l2x0_base + L2X0_ADDR_FILTER_START);
466 }
467}
468
Barry Song91c2ebb2011-09-30 14:43:12 +0100469static void __init pl310_save(void)
470{
471 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
472 L2X0_CACHE_ID_RTL_MASK;
473
474 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
475 L2X0_TAG_LATENCY_CTRL);
476 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
477 L2X0_DATA_LATENCY_CTRL);
478 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
479 L2X0_ADDR_FILTER_END);
480 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
481 L2X0_ADDR_FILTER_START);
482
483 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
484 /*
485 * From r2p0, there is Prefetch offset/control register
486 */
487 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
488 L2X0_PREFETCH_CTRL);
489 /*
490 * From r3p0, there is Power control register
491 */
492 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
493 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
494 L2X0_POWER_CTRL);
495 }
496}
497
498static void l2x0_resume(void)
499{
500 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
501 /* restore aux ctrl and enable l2 */
502 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
503
504 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
505 L2X0_AUX_CTRL);
506
507 l2x0_inv_all();
508
509 writel_relaxed(1, l2x0_base + L2X0_CTRL);
510 }
511}
512
513static void pl310_resume(void)
514{
515 u32 l2x0_revision;
516
517 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
518 /* restore pl310 setup */
519 writel_relaxed(l2x0_saved_regs.tag_latency,
520 l2x0_base + L2X0_TAG_LATENCY_CTRL);
521 writel_relaxed(l2x0_saved_regs.data_latency,
522 l2x0_base + L2X0_DATA_LATENCY_CTRL);
523 writel_relaxed(l2x0_saved_regs.filter_end,
524 l2x0_base + L2X0_ADDR_FILTER_END);
525 writel_relaxed(l2x0_saved_regs.filter_start,
526 l2x0_base + L2X0_ADDR_FILTER_START);
527
528 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
529 L2X0_CACHE_ID_RTL_MASK;
530
531 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
532 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
533 l2x0_base + L2X0_PREFETCH_CTRL);
534 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
535 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
536 l2x0_base + L2X0_POWER_CTRL);
537 }
538 }
539
540 l2x0_resume();
541}
542
543static const struct l2x0_of_data pl310_data = {
Gregory CLEMENT6248d062012-10-01 10:56:42 +0100544 .setup = pl310_of_setup,
545 .save = pl310_save,
546 .outer_cache = {
547 .resume = pl310_resume,
548 .inv_range = l2x0_inv_range,
549 .clean_range = l2x0_clean_range,
550 .flush_range = l2x0_flush_range,
551 .sync = l2x0_cache_sync,
552 .flush_all = l2x0_flush_all,
553 .inv_all = l2x0_inv_all,
554 .disable = l2x0_disable,
555 .set_debug = pl310_set_debug,
556 },
Barry Song91c2ebb2011-09-30 14:43:12 +0100557};
558
559static const struct l2x0_of_data l2x0_data = {
Gregory CLEMENT6248d062012-10-01 10:56:42 +0100560 .setup = l2x0_of_setup,
561 .save = NULL,
562 .outer_cache = {
563 .resume = l2x0_resume,
564 .inv_range = l2x0_inv_range,
565 .clean_range = l2x0_clean_range,
566 .flush_range = l2x0_flush_range,
567 .sync = l2x0_cache_sync,
568 .flush_all = l2x0_flush_all,
569 .inv_all = l2x0_inv_all,
570 .disable = l2x0_disable,
571 },
Barry Song91c2ebb2011-09-30 14:43:12 +0100572};
573
Rob Herring8c369262011-08-03 18:12:05 +0100574static const struct of_device_id l2x0_ids[] __initconst = {
Barry Song91c2ebb2011-09-30 14:43:12 +0100575 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
576 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
577 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring8c369262011-08-03 18:12:05 +0100578 {}
579};
580
Russell King3e175ca2011-09-18 11:27:30 +0100581int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100582{
583 struct device_node *np;
Uwe Kleine-Könige5b5d022012-05-21 21:54:06 +0200584 const struct l2x0_of_data *data;
Barry Song91c2ebb2011-09-30 14:43:12 +0100585 struct resource res;
Rob Herring8c369262011-08-03 18:12:05 +0100586
587 np = of_find_matching_node(NULL, l2x0_ids);
588 if (!np)
589 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +0100590
591 if (of_address_to_resource(np, 0, &res))
592 return -ENODEV;
593
594 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +0100595 if (!l2x0_base)
596 return -ENOMEM;
597
Barry Song91c2ebb2011-09-30 14:43:12 +0100598 l2x0_saved_regs.phy_base = res.start;
599
600 data = of_match_node(l2x0_ids, np)->data;
601
Rob Herring8c369262011-08-03 18:12:05 +0100602 /* L2 configuration can only be changed if the cache is disabled */
603 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Song91c2ebb2011-09-30 14:43:12 +0100604 if (data->setup)
605 data->setup(np, &aux_val, &aux_mask);
Rob Herring8c369262011-08-03 18:12:05 +0100606 }
Barry Song91c2ebb2011-09-30 14:43:12 +0100607
608 if (data->save)
609 data->save();
610
Gregory CLEMENT6248d062012-10-01 10:56:42 +0100611 of_init = true;
Rob Herring8c369262011-08-03 18:12:05 +0100612 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Song91c2ebb2011-09-30 14:43:12 +0100613
Gregory CLEMENT6248d062012-10-01 10:56:42 +0100614 memcpy(&outer_cache, &data->outer_cache, sizeof(outer_cache));
615
Rob Herring8c369262011-08-03 18:12:05 +0100616 return 0;
617}
618#endif