blob: cb9fc766cdf87ee0465867ed75c9ac4e24e9e092 [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 Herring8c369262011-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 Herring8c369262011-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 Gleixnerbd31b852009-07-03 08:44:46 -050033static DEFINE_RAW_SPINLOCK(l2x0_lock);
Russell King3e175ca2011-09-18 11:27:30 +010034static u32 l2x0_way_mask; /* Bitmask of active ways */
35static u32 l2x0_size;
Colin Cross74b6cdd2011-09-14 15:59:50 -070036static u32 l2x0_cache_id;
37static unsigned int l2x0_sets;
38static unsigned int l2x0_ways;
Will Deaconf154fe92012-04-20 17:21:08 +010039static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
Taniya Das38a8c6e2012-05-09 20:34:39 +053040static void pl310_save(void);
Colin Cross74b6cdd2011-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 Song91c2ebb2011-09-30 14:43:12 +010049struct l2x0_regs l2x0_saved_regs;
50
51struct l2x0_of_data {
Russell King3e175ca2011-09-18 11:27:30 +010052 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Song91c2ebb2011-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 Song1caf3092011-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 Deaconf154fe92012-04-20 17:21:08 +010077 writel_relaxed(0, base + sync_reg_offset);
Russell King3d107432009-11-19 11:41:09 +000078 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010079}
80
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010081static inline void l2x0_clean_line(unsigned long addr)
82{
83 void __iomem *base = l2x0_base;
84 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010085 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010086}
87
88static inline void l2x0_inv_line(unsigned long addr)
89{
90 void __iomem *base = l2x0_base;
91 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010092 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010093}
94
Santosh Shilimkar2839e062011-03-08 06:59:54 +010095#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Will Deaconab4d5362012-04-20 17:22:11 +010096static inline void debug_writel(unsigned long val)
97{
98 if (outer_cache.set_debug)
99 outer_cache.set_debug(val);
100}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100101
Will Deaconab4d5362012-04-20 17:22:11 +0100102static void pl310_set_debug(unsigned long val)
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100103{
104 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
105}
106#else
107/* Optimised out for non-errata case */
108static inline void debug_writel(unsigned long val)
109{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100110}
111
Will Deaconab4d5362012-04-20 17:22:11 +0100112#define pl310_set_debug NULL
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100113#endif
114
115#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100116static inline void l2x0_flush_line(unsigned long addr)
117{
118 void __iomem *base = l2x0_base;
119
120 /* Clean by PA followed by Invalidate by PA */
121 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100122 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100123 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100124 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100125}
126#else
127
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100128static inline void l2x0_flush_line(unsigned long addr)
129{
130 void __iomem *base = l2x0_base;
131 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100132 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100133}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100134#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100135
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136void l2x0_cache_sync(void)
Catalin Marinas23107c52010-03-24 16:48:53 +0100137{
138 unsigned long flags;
139
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500140 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100141 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500142 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100143}
144
Colin Cross74b6cdd2011-09-14 15:59:50 -0700145#ifdef CONFIG_PL310_ERRATA_727915
146static void l2x0_for_each_set_way(void __iomem *reg)
147{
148 int set;
149 int way;
150 unsigned long flags;
151
152 for (way = 0; way < l2x0_ways; way++) {
153 raw_spin_lock_irqsave(&l2x0_lock, flags);
154 for (set = 0; set < l2x0_sets; set++)
155 writel_relaxed((way << 28) | (set << 5), reg);
156 cache_sync();
157 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
158 }
159}
160#endif
161
Will Deacon38a89142011-07-01 14:36:19 +0100162static void __l2x0_flush_all(void)
163{
164 debug_writel(0x03);
165 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
166 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
167 cache_sync();
168 debug_writel(0x00);
169}
170
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530171static void l2x0_flush_all(void)
172{
173 unsigned long flags;
174
Colin Cross74b6cdd2011-09-14 15:59:50 -0700175#ifdef CONFIG_PL310_ERRATA_727915
176 if (is_pl310_rev(REV_PL310_R2P0)) {
177 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
178 return;
179 }
180#endif
181
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530182 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500183 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100184 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500185 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530186}
187
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530188static void l2x0_clean_all(void)
189{
190 unsigned long flags;
191
Colin Cross74b6cdd2011-09-14 15:59:50 -0700192#ifdef CONFIG_PL310_ERRATA_727915
193 if (is_pl310_rev(REV_PL310_R2P0)) {
194 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
195 return;
196 }
197#endif
198
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530199 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500200 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross74b6cdd2011-09-14 15:59:50 -0700201 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530202 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
203 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
204 cache_sync();
Colin Cross74b6cdd2011-09-14 15:59:50 -0700205 debug_writel(0x00);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500206 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530207}
208
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530209static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100210{
Russell King0eb948d2009-11-19 11:12:15 +0000211 unsigned long flags;
212
Catalin Marinas382266a2007-02-05 14:48:19 +0100213 /* invalidate all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500214 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530215 /* Invalidating when L2 is enabled is a nono */
216 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100217 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100218 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100219 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500220 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100221}
222
223static void l2x0_inv_range(unsigned long start, unsigned long end)
224{
Russell King3d107432009-11-19 11:41:09 +0000225 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000226 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100227
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500228 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100229 if (start & (CACHE_LINE_SIZE - 1)) {
230 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100231 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100232 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100233 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100234 start += CACHE_LINE_SIZE;
235 }
236
237 if (end & (CACHE_LINE_SIZE - 1)) {
238 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100239 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100240 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100241 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100242 }
243
Russell King0eb948d2009-11-19 11:12:15 +0000244 while (start < end) {
245 unsigned long blk_end = start + min(end - start, 4096UL);
246
247 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100248 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000249 start += CACHE_LINE_SIZE;
250 }
251
252 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500253 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
254 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000255 }
256 }
Russell King3d107432009-11-19 11:41:09 +0000257 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100258 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500259 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100260}
261
262static void l2x0_clean_range(unsigned long start, unsigned long end)
263{
Russell King3d107432009-11-19 11:41:09 +0000264 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000265 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100266
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530267 if ((end - start) >= l2x0_size) {
268 l2x0_clean_all();
269 return;
270 }
271
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500272 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100273 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000274 while (start < end) {
275 unsigned long blk_end = start + min(end - start, 4096UL);
276
277 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100278 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000279 start += CACHE_LINE_SIZE;
280 }
281
282 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500283 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
284 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000285 }
286 }
Russell King3d107432009-11-19 11:41:09 +0000287 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100288 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500289 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100290}
291
292static void l2x0_flush_range(unsigned long start, unsigned long end)
293{
Russell King3d107432009-11-19 11:41:09 +0000294 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000295 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100296
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530297 if ((end - start) >= l2x0_size) {
298 l2x0_flush_all();
299 return;
300 }
301
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500302 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100303 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000304 while (start < end) {
305 unsigned long blk_end = start + min(end - start, 4096UL);
306
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100307 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000308 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100309 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000310 start += CACHE_LINE_SIZE;
311 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100312 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000313
314 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500315 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
316 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000317 }
318 }
Russell King3d107432009-11-19 11:41:09 +0000319 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100320 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500321 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100322}
323
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530324static void l2x0_disable(void)
325{
326 unsigned long flags;
327
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500328 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100329 __l2x0_flush_all();
330 writel_relaxed(0, l2x0_base + L2X0_CTRL);
331 dsb();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500332 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530333}
334
Russell King3e175ca2011-09-18 11:27:30 +0100335static void l2x0_unlock(u32 cache_id)
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100336{
337 int lockregs;
338 int i;
339
340 if (cache_id == L2X0_CACHE_ID_PART_L310)
341 lockregs = 8;
342 else
343 /* L210 and unknown types */
344 lockregs = 1;
345
346 for (i = 0; i < lockregs; i++) {
347 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
348 i * L2X0_LOCKDOWN_STRIDE);
349 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
350 i * L2X0_LOCKDOWN_STRIDE);
351 }
352}
353
Russell King3e175ca2011-09-18 11:27:30 +0100354void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100355{
Russell King3e175ca2011-09-18 11:27:30 +0100356 u32 aux;
Russell King3e175ca2011-09-18 11:27:30 +0100357 u32 way_size = 0;
Jason McMullan64039be2010-05-05 18:59:37 +0100358 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100359
360 l2x0_base = base;
361
Colin Cross74b6cdd2011-09-14 15:59:50 -0700362 l2x0_cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
Catalin Marinas6775a552010-07-28 22:01:25 +0100363 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100364
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100365 aux &= aux_mask;
366 aux |= aux_val;
367
Jason McMullan64039be2010-05-05 18:59:37 +0100368 /* Determine the number of ways */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700369 switch (l2x0_cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100370 case L2X0_CACHE_ID_PART_L310:
371 if (aux & (1 << 16))
Colin Cross74b6cdd2011-09-14 15:59:50 -0700372 l2x0_ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100373 else
Colin Cross74b6cdd2011-09-14 15:59:50 -0700374 l2x0_ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100375 type = "L310";
Will Deaconf154fe92012-04-20 17:21:08 +0100376#ifdef CONFIG_PL310_ERRATA_753970
377 /* Unmapped register. */
378 sync_reg_offset = L2X0_DUMMY_REG;
379#endif
Will Deaconab4d5362012-04-20 17:22:11 +0100380 outer_cache.set_debug = pl310_set_debug;
Jason McMullan64039be2010-05-05 18:59:37 +0100381 break;
382 case L2X0_CACHE_ID_PART_L210:
Colin Cross74b6cdd2011-09-14 15:59:50 -0700383 l2x0_ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100384 type = "L210";
385 break;
386 default:
387 /* Assume unknown chips have 8 ways */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700388 l2x0_ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100389 type = "L2x0 series";
390 break;
391 }
392
Colin Cross74b6cdd2011-09-14 15:59:50 -0700393 l2x0_way_mask = (1 << l2x0_ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100394
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100395 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530396 * L2 cache Size = Way size * Number of ways
397 */
398 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Colin Cross74b6cdd2011-09-14 15:59:50 -0700399 way_size = SZ_1K << (way_size + 3);
400 l2x0_size = l2x0_ways * way_size;
401 l2x0_sets = way_size / CACHE_LINE_SIZE;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530402
403 /*
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100404 * Check if l2x0 controller is already enabled.
405 * If you are booting from non-secure mode
406 * accessing the below registers will fault.
407 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100408 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100409 /* Make sure that I&D is not locked down when starting */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700410 l2x0_unlock(l2x0_cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100411
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100412 /* l2x0 controller is disabled */
Catalin Marinas6775a552010-07-28 22:01:25 +0100413 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100414
Barry Song91c2ebb2011-09-30 14:43:12 +0100415 l2x0_saved_regs.aux_ctrl = aux;
416
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100417 l2x0_inv_all();
418
419 /* enable L2X0 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100420 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100421 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100422
Steve Mucklef132c6c2012-06-06 18:30:57 -0700423 outer_cache.inv_range = l2x0_inv_range;
424 outer_cache.clean_range = l2x0_clean_range;
425 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100426 outer_cache.sync = l2x0_cache_sync;
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530427 outer_cache.flush_all = l2x0_flush_all;
428 outer_cache.inv_all = l2x0_inv_all;
429 outer_cache.disable = l2x0_disable;
Catalin Marinas382266a2007-02-05 14:48:19 +0100430
Jason McMullan64039be2010-05-05 18:59:37 +0100431 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530432 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 -0700433 l2x0_ways, l2x0_cache_id, aux, l2x0_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434
Taniya Das38a8c6e2012-05-09 20:34:39 +0530435 /* Save the L2X0 contents, as they are not modified else where */
436 pl310_save();
Catalin Marinas382266a2007-02-05 14:48:19 +0100437}
Rob Herring8c369262011-08-03 18:12:05 +0100438
439#ifdef CONFIG_OF
440static void __init l2x0_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100441 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100442{
443 u32 data[2] = { 0, 0 };
444 u32 tag = 0;
445 u32 dirty = 0;
446 u32 val = 0, mask = 0;
447
448 of_property_read_u32(np, "arm,tag-latency", &tag);
449 if (tag) {
450 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
451 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
452 }
453
454 of_property_read_u32_array(np, "arm,data-latency",
455 data, ARRAY_SIZE(data));
456 if (data[0] && data[1]) {
457 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
458 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
459 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
460 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
461 }
462
463 of_property_read_u32(np, "arm,dirty-latency", &dirty);
464 if (dirty) {
465 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
466 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
467 }
468
469 *aux_val &= ~mask;
470 *aux_val |= val;
471 *aux_mask &= ~mask;
472}
473
474static void __init pl310_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100475 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100476{
477 u32 data[3] = { 0, 0, 0 };
478 u32 tag[3] = { 0, 0, 0 };
479 u32 filter[2] = { 0, 0 };
480
481 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
482 if (tag[0] && tag[1] && tag[2])
483 writel_relaxed(
484 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
485 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
486 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
487 l2x0_base + L2X0_TAG_LATENCY_CTRL);
488
489 of_property_read_u32_array(np, "arm,data-latency",
490 data, ARRAY_SIZE(data));
491 if (data[0] && data[1] && data[2])
492 writel_relaxed(
493 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
494 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
495 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
496 l2x0_base + L2X0_DATA_LATENCY_CTRL);
497
498 of_property_read_u32_array(np, "arm,filter-ranges",
499 filter, ARRAY_SIZE(filter));
Barry Song74d41f32011-09-14 03:20:01 +0100500 if (filter[1]) {
Rob Herring8c369262011-08-03 18:12:05 +0100501 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
502 l2x0_base + L2X0_ADDR_FILTER_END);
503 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
504 l2x0_base + L2X0_ADDR_FILTER_START);
505 }
506}
Taniya Das38a8c6e2012-05-09 20:34:39 +0530507#endif
Rob Herring8c369262011-08-03 18:12:05 +0100508
Stephen Boyd22ab9342012-04-25 11:42:14 -0700509static void pl310_save(void)
Barry Song91c2ebb2011-09-30 14:43:12 +0100510{
511 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
512 L2X0_CACHE_ID_RTL_MASK;
513
514 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
515 L2X0_TAG_LATENCY_CTRL);
516 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
517 L2X0_DATA_LATENCY_CTRL);
518 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
519 L2X0_ADDR_FILTER_END);
520 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
521 L2X0_ADDR_FILTER_START);
522
523 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
524 /*
525 * From r2p0, there is Prefetch offset/control register
526 */
527 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
528 L2X0_PREFETCH_CTRL);
529 /*
530 * From r3p0, there is Power control register
531 */
532 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
533 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
534 L2X0_POWER_CTRL);
535 }
536}
537
538static void l2x0_resume(void)
539{
540 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
541 /* restore aux ctrl and enable l2 */
542 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
543
544 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
545 L2X0_AUX_CTRL);
546
547 l2x0_inv_all();
548
549 writel_relaxed(1, l2x0_base + L2X0_CTRL);
550 }
551}
552
553static void pl310_resume(void)
554{
555 u32 l2x0_revision;
556
557 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
558 /* restore pl310 setup */
559 writel_relaxed(l2x0_saved_regs.tag_latency,
560 l2x0_base + L2X0_TAG_LATENCY_CTRL);
561 writel_relaxed(l2x0_saved_regs.data_latency,
562 l2x0_base + L2X0_DATA_LATENCY_CTRL);
563 writel_relaxed(l2x0_saved_regs.filter_end,
564 l2x0_base + L2X0_ADDR_FILTER_END);
565 writel_relaxed(l2x0_saved_regs.filter_start,
566 l2x0_base + L2X0_ADDR_FILTER_START);
567
568 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
569 L2X0_CACHE_ID_RTL_MASK;
570
571 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
572 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
573 l2x0_base + L2X0_PREFETCH_CTRL);
574 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
575 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
576 l2x0_base + L2X0_POWER_CTRL);
577 }
578 }
579
580 l2x0_resume();
581}
582
Taniya Das38a8c6e2012-05-09 20:34:39 +0530583#ifdef CONFIG_OF
Barry Song91c2ebb2011-09-30 14:43:12 +0100584static const struct l2x0_of_data pl310_data = {
585 pl310_of_setup,
586 pl310_save,
587 pl310_resume,
588};
589
590static const struct l2x0_of_data l2x0_data = {
591 l2x0_of_setup,
592 NULL,
593 l2x0_resume,
594};
595
Rob Herring8c369262011-08-03 18:12:05 +0100596static const struct of_device_id l2x0_ids[] __initconst = {
Barry Song91c2ebb2011-09-30 14:43:12 +0100597 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
598 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
599 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring8c369262011-08-03 18:12:05 +0100600 {}
601};
602
Russell King3e175ca2011-09-18 11:27:30 +0100603int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100604{
605 struct device_node *np;
Barry Song91c2ebb2011-09-30 14:43:12 +0100606 struct l2x0_of_data *data;
607 struct resource res;
Rob Herring8c369262011-08-03 18:12:05 +0100608
609 np = of_find_matching_node(NULL, l2x0_ids);
610 if (!np)
611 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +0100612
613 if (of_address_to_resource(np, 0, &res))
614 return -ENODEV;
615
616 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +0100617 if (!l2x0_base)
618 return -ENOMEM;
619
Barry Song91c2ebb2011-09-30 14:43:12 +0100620 l2x0_saved_regs.phy_base = res.start;
621
622 data = of_match_node(l2x0_ids, np)->data;
623
Rob Herring8c369262011-08-03 18:12:05 +0100624 /* L2 configuration can only be changed if the cache is disabled */
625 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Song91c2ebb2011-09-30 14:43:12 +0100626 if (data->setup)
627 data->setup(np, &aux_val, &aux_mask);
Rob Herring8c369262011-08-03 18:12:05 +0100628 }
Barry Song91c2ebb2011-09-30 14:43:12 +0100629
630 if (data->save)
631 data->save();
632
Rob Herring8c369262011-08-03 18:12:05 +0100633 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Song91c2ebb2011-09-30 14:43:12 +0100634
635 outer_cache.resume = data->resume;
Rob Herring8c369262011-08-03 18:12:05 +0100636 return 0;
637}
638#endif
Taniya Das38a8c6e2012-05-09 20:34:39 +0530639
640void l2cc_suspend(void)
641{
642 l2x0_disable();
643 dmb();
644}
645
646void l2cc_resume(void)
647{
648 pl310_resume();
649 dmb();
650}