blob: 892b007ba344560dd0eee53b22d2d330e47f3292 [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
Duy Truong790f06d2013-02-13 16:38:12 -08005 * Copyright (c) 2009, 2011-2012, The Linux Foundation. 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
Ashwin Chaugule8b459cc2012-11-26 15:20:54 -050032void __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);
Girish Mahadevan74934a32012-10-11 10:44:47 -060041static void pl310_resume(void);
42static void l2x0_resume(void);
Colin Cross74b6cdd2011-09-14 15:59:50 -070043
44static inline bool is_pl310_rev(int rev)
45{
46 return (l2x0_cache_id &
47 (L2X0_CACHE_ID_PART_MASK | L2X0_CACHE_ID_REV_MASK)) ==
48 (L2X0_CACHE_ID_PART_L310 | rev);
49}
Catalin Marinas382266a2007-02-05 14:48:19 +010050
Barry Song91c2ebb2011-09-30 14:43:12 +010051struct l2x0_regs l2x0_saved_regs;
52
53struct l2x0_of_data {
Russell King3e175ca2011-09-18 11:27:30 +010054 void (*setup)(const struct device_node *, u32 *, u32 *);
Barry Song91c2ebb2011-09-30 14:43:12 +010055 void (*save)(void);
56 void (*resume)(void);
57};
58
Catalin Marinas9a6655e2010-08-31 13:05:22 +010059static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010060{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010061 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010062 while (readl_relaxed(reg) & mask)
Barry Song1caf3092011-09-09 10:30:34 +010063 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010064}
65
Catalin Marinas9a6655e2010-08-31 13:05:22 +010066#ifdef CONFIG_CACHE_PL310
67static inline void cache_wait(void __iomem *reg, unsigned long mask)
68{
69 /* cache operations by line are atomic on PL310 */
70}
71#else
72#define cache_wait cache_wait_way
73#endif
74
Catalin Marinas382266a2007-02-05 14:48:19 +010075static inline void cache_sync(void)
76{
Russell King3d107432009-11-19 11:41:09 +000077 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +010078
Will Deaconf154fe92012-04-20 17:21:08 +010079 writel_relaxed(0, base + sync_reg_offset);
Russell King3d107432009-11-19 11:41:09 +000080 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010081}
82
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010083static inline void l2x0_clean_line(unsigned long addr)
84{
85 void __iomem *base = l2x0_base;
86 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010087 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010088}
89
90static inline void l2x0_inv_line(unsigned long addr)
91{
92 void __iomem *base = l2x0_base;
93 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +010094 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010095}
96
Santosh Shilimkar2839e062011-03-08 06:59:54 +010097#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Will Deaconab4d5362012-04-20 17:22:11 +010098static inline void debug_writel(unsigned long val)
99{
100 if (outer_cache.set_debug)
101 outer_cache.set_debug(val);
102}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100103
Will Deaconab4d5362012-04-20 17:22:11 +0100104static void pl310_set_debug(unsigned long val)
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100105{
106 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
107}
108#else
109/* Optimised out for non-errata case */
110static inline void debug_writel(unsigned long val)
111{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100112}
113
Will Deaconab4d5362012-04-20 17:22:11 +0100114#define pl310_set_debug NULL
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100115#endif
116
117#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100118static inline void l2x0_flush_line(unsigned long addr)
119{
120 void __iomem *base = l2x0_base;
121
122 /* Clean by PA followed by Invalidate by PA */
123 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100124 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100125 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100126 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100127}
128#else
129
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100130static inline void l2x0_flush_line(unsigned long addr)
131{
132 void __iomem *base = l2x0_base;
133 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100134 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100135}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100136#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138void l2x0_cache_sync(void)
Catalin Marinas23107c52010-03-24 16:48:53 +0100139{
140 unsigned long flags;
141
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500142 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100143 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500144 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100145}
146
Colin Cross74b6cdd2011-09-14 15:59:50 -0700147#ifdef CONFIG_PL310_ERRATA_727915
148static void l2x0_for_each_set_way(void __iomem *reg)
149{
150 int set;
151 int way;
152 unsigned long flags;
153
154 for (way = 0; way < l2x0_ways; way++) {
155 raw_spin_lock_irqsave(&l2x0_lock, flags);
156 for (set = 0; set < l2x0_sets; set++)
157 writel_relaxed((way << 28) | (set << 5), reg);
158 cache_sync();
159 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
160 }
161}
162#endif
163
Will Deacon38a89142011-07-01 14:36:19 +0100164static void __l2x0_flush_all(void)
165{
166 debug_writel(0x03);
167 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
168 cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
169 cache_sync();
170 debug_writel(0x00);
171}
172
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530173static void l2x0_flush_all(void)
174{
175 unsigned long flags;
176
Colin Cross74b6cdd2011-09-14 15:59:50 -0700177#ifdef CONFIG_PL310_ERRATA_727915
178 if (is_pl310_rev(REV_PL310_R2P0)) {
179 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_INV_LINE_IDX);
180 return;
181 }
182#endif
183
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530184 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500185 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100186 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500187 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530188}
189
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530190static void l2x0_clean_all(void)
191{
192 unsigned long flags;
193
Colin Cross74b6cdd2011-09-14 15:59:50 -0700194#ifdef CONFIG_PL310_ERRATA_727915
195 if (is_pl310_rev(REV_PL310_R2P0)) {
196 l2x0_for_each_set_way(l2x0_base + L2X0_CLEAN_LINE_IDX);
197 return;
198 }
199#endif
200
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530201 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500202 raw_spin_lock_irqsave(&l2x0_lock, flags);
Colin Cross74b6cdd2011-09-14 15:59:50 -0700203 debug_writel(0x03);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530204 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
205 cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
206 cache_sync();
Colin Cross74b6cdd2011-09-14 15:59:50 -0700207 debug_writel(0x00);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500208 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530209}
210
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530211static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100212{
Russell King0eb948d2009-11-19 11:12:15 +0000213 unsigned long flags;
214
Catalin Marinas382266a2007-02-05 14:48:19 +0100215 /* invalidate all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500216 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530217 /* Invalidating when L2 is enabled is a nono */
218 BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100219 writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100220 cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100221 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500222 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100223}
224
225static void l2x0_inv_range(unsigned long start, unsigned long end)
226{
Russell King3d107432009-11-19 11:41:09 +0000227 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000228 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100229
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500230 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100231 if (start & (CACHE_LINE_SIZE - 1)) {
232 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100233 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100234 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100235 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100236 start += CACHE_LINE_SIZE;
237 }
238
239 if (end & (CACHE_LINE_SIZE - 1)) {
240 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100241 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100242 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100243 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100244 }
245
Russell King0eb948d2009-11-19 11:12:15 +0000246 while (start < end) {
247 unsigned long blk_end = start + min(end - start, 4096UL);
248
249 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100250 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000251 start += CACHE_LINE_SIZE;
252 }
253
254 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500255 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
256 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000257 }
258 }
Russell King3d107432009-11-19 11:41:09 +0000259 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100260 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500261 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100262}
263
264static void l2x0_clean_range(unsigned long start, unsigned long end)
265{
Russell King3d107432009-11-19 11:41:09 +0000266 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000267 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100268
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530269 if ((end - start) >= l2x0_size) {
270 l2x0_clean_all();
271 return;
272 }
273
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500274 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100275 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000276 while (start < end) {
277 unsigned long blk_end = start + min(end - start, 4096UL);
278
279 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100280 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000281 start += CACHE_LINE_SIZE;
282 }
283
284 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500285 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
286 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000287 }
288 }
Russell King3d107432009-11-19 11:41:09 +0000289 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100290 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500291 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100292}
293
294static void l2x0_flush_range(unsigned long start, unsigned long end)
295{
Russell King3d107432009-11-19 11:41:09 +0000296 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000297 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100298
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530299 if ((end - start) >= l2x0_size) {
300 l2x0_flush_all();
301 return;
302 }
303
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500304 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100305 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000306 while (start < end) {
307 unsigned long blk_end = start + min(end - start, 4096UL);
308
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100309 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000310 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100311 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000312 start += CACHE_LINE_SIZE;
313 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100314 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000315
316 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500317 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
318 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000319 }
320 }
Russell King3d107432009-11-19 11:41:09 +0000321 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100322 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500323 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100324}
325
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530326static void l2x0_disable(void)
327{
328 unsigned long flags;
329
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500330 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100331 __l2x0_flush_all();
332 writel_relaxed(0, l2x0_base + L2X0_CTRL);
333 dsb();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500334 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530335}
336
Russell King3e175ca2011-09-18 11:27:30 +0100337static void l2x0_unlock(u32 cache_id)
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100338{
339 int lockregs;
340 int i;
341
342 if (cache_id == L2X0_CACHE_ID_PART_L310)
343 lockregs = 8;
344 else
345 /* L210 and unknown types */
346 lockregs = 1;
347
348 for (i = 0; i < lockregs; i++) {
349 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
350 i * L2X0_LOCKDOWN_STRIDE);
351 writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
352 i * L2X0_LOCKDOWN_STRIDE);
353 }
354}
355
Russell King3e175ca2011-09-18 11:27:30 +0100356void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
Catalin Marinas382266a2007-02-05 14:48:19 +0100357{
Russell King3e175ca2011-09-18 11:27:30 +0100358 u32 aux;
Russell King3e175ca2011-09-18 11:27:30 +0100359 u32 way_size = 0;
Jason McMullan64039be2010-05-05 18:59:37 +0100360 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100361
362 l2x0_base = base;
363
Colin Cross74b6cdd2011-09-14 15:59:50 -0700364 l2x0_cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
Catalin Marinas6775a552010-07-28 22:01:25 +0100365 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100366
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100367 aux &= aux_mask;
368 aux |= aux_val;
369
Jason McMullan64039be2010-05-05 18:59:37 +0100370 /* Determine the number of ways */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700371 switch (l2x0_cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100372 case L2X0_CACHE_ID_PART_L310:
373 if (aux & (1 << 16))
Colin Cross74b6cdd2011-09-14 15:59:50 -0700374 l2x0_ways = 16;
Jason McMullan64039be2010-05-05 18:59:37 +0100375 else
Colin Cross74b6cdd2011-09-14 15:59:50 -0700376 l2x0_ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100377 type = "L310";
Will Deaconf154fe92012-04-20 17:21:08 +0100378#ifdef CONFIG_PL310_ERRATA_753970
379 /* Unmapped register. */
380 sync_reg_offset = L2X0_DUMMY_REG;
381#endif
Will Deaconab4d5362012-04-20 17:22:11 +0100382 outer_cache.set_debug = pl310_set_debug;
Girish Mahadevan74934a32012-10-11 10:44:47 -0600383 outer_cache.resume = pl310_resume;
Jason McMullan64039be2010-05-05 18:59:37 +0100384 break;
385 case L2X0_CACHE_ID_PART_L210:
Colin Cross74b6cdd2011-09-14 15:59:50 -0700386 l2x0_ways = (aux >> 13) & 0xf;
Jason McMullan64039be2010-05-05 18:59:37 +0100387 type = "L210";
Girish Mahadevan74934a32012-10-11 10:44:47 -0600388 outer_cache.resume = l2x0_resume;
Jason McMullan64039be2010-05-05 18:59:37 +0100389 break;
390 default:
391 /* Assume unknown chips have 8 ways */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700392 l2x0_ways = 8;
Jason McMullan64039be2010-05-05 18:59:37 +0100393 type = "L2x0 series";
Girish Mahadevan74934a32012-10-11 10:44:47 -0600394 outer_cache.resume = l2x0_resume;
Jason McMullan64039be2010-05-05 18:59:37 +0100395 break;
396 }
397
Colin Cross74b6cdd2011-09-14 15:59:50 -0700398 l2x0_way_mask = (1 << l2x0_ways) - 1;
Jason McMullan64039be2010-05-05 18:59:37 +0100399
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100400 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530401 * L2 cache Size = Way size * Number of ways
402 */
403 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Colin Cross74b6cdd2011-09-14 15:59:50 -0700404 way_size = SZ_1K << (way_size + 3);
405 l2x0_size = l2x0_ways * way_size;
406 l2x0_sets = way_size / CACHE_LINE_SIZE;
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530407
408 /*
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100409 * Check if l2x0 controller is already enabled.
410 * If you are booting from non-secure mode
411 * accessing the below registers will fault.
412 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100413 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Linus Walleijbac7e6e2011-09-06 07:45:46 +0100414 /* Make sure that I&D is not locked down when starting */
Colin Cross74b6cdd2011-09-14 15:59:50 -0700415 l2x0_unlock(l2x0_cache_id);
Catalin Marinas382266a2007-02-05 14:48:19 +0100416
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100417 /* l2x0 controller is disabled */
Catalin Marinas6775a552010-07-28 22:01:25 +0100418 writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100419
Barry Song91c2ebb2011-09-30 14:43:12 +0100420 l2x0_saved_regs.aux_ctrl = aux;
421
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100422 l2x0_inv_all();
423
424 /* enable L2X0 */
Catalin Marinas6775a552010-07-28 22:01:25 +0100425 writel_relaxed(1, l2x0_base + L2X0_CTRL);
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100426 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100427
Stephen Boyd42517402013-01-14 16:41:42 -0800428 outer_cache.inv_range = l2x0_inv_range;
429 outer_cache.clean_range = l2x0_clean_range;
430 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100431 outer_cache.sync = l2x0_cache_sync;
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530432 outer_cache.flush_all = l2x0_flush_all;
433 outer_cache.inv_all = l2x0_inv_all;
434 outer_cache.disable = l2x0_disable;
Catalin Marinas382266a2007-02-05 14:48:19 +0100435
Jason McMullan64039be2010-05-05 18:59:37 +0100436 printk(KERN_INFO "%s cache controller enabled\n", type);
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530437 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 -0700438 l2x0_ways, l2x0_cache_id, aux, l2x0_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439
Taniya Das38a8c6e2012-05-09 20:34:39 +0530440 /* Save the L2X0 contents, as they are not modified else where */
441 pl310_save();
Catalin Marinas382266a2007-02-05 14:48:19 +0100442}
Rob Herring8c369262011-08-03 18:12:05 +0100443
444#ifdef CONFIG_OF
445static void __init l2x0_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100446 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100447{
448 u32 data[2] = { 0, 0 };
449 u32 tag = 0;
450 u32 dirty = 0;
451 u32 val = 0, mask = 0;
452
453 of_property_read_u32(np, "arm,tag-latency", &tag);
454 if (tag) {
455 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
456 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
457 }
458
459 of_property_read_u32_array(np, "arm,data-latency",
460 data, ARRAY_SIZE(data));
461 if (data[0] && data[1]) {
462 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
463 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
464 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
465 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
466 }
467
468 of_property_read_u32(np, "arm,dirty-latency", &dirty);
469 if (dirty) {
470 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
471 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
472 }
473
474 *aux_val &= ~mask;
475 *aux_val |= val;
476 *aux_mask &= ~mask;
477}
478
479static void __init pl310_of_setup(const struct device_node *np,
Russell King3e175ca2011-09-18 11:27:30 +0100480 u32 *aux_val, u32 *aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100481{
482 u32 data[3] = { 0, 0, 0 };
483 u32 tag[3] = { 0, 0, 0 };
484 u32 filter[2] = { 0, 0 };
485
486 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
487 if (tag[0] && tag[1] && tag[2])
488 writel_relaxed(
489 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
490 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
491 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
492 l2x0_base + L2X0_TAG_LATENCY_CTRL);
493
494 of_property_read_u32_array(np, "arm,data-latency",
495 data, ARRAY_SIZE(data));
496 if (data[0] && data[1] && data[2])
497 writel_relaxed(
498 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
499 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
500 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
501 l2x0_base + L2X0_DATA_LATENCY_CTRL);
502
503 of_property_read_u32_array(np, "arm,filter-ranges",
504 filter, ARRAY_SIZE(filter));
Barry Song74d41f32011-09-14 03:20:01 +0100505 if (filter[1]) {
Rob Herring8c369262011-08-03 18:12:05 +0100506 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
507 l2x0_base + L2X0_ADDR_FILTER_END);
508 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
509 l2x0_base + L2X0_ADDR_FILTER_START);
510 }
511}
Taniya Das38a8c6e2012-05-09 20:34:39 +0530512#endif
Rob Herring8c369262011-08-03 18:12:05 +0100513
Stephen Boyd22ab9342012-04-25 11:42:14 -0700514static void pl310_save(void)
Barry Song91c2ebb2011-09-30 14:43:12 +0100515{
516 u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
517 L2X0_CACHE_ID_RTL_MASK;
518
519 l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
520 L2X0_TAG_LATENCY_CTRL);
521 l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
522 L2X0_DATA_LATENCY_CTRL);
523 l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
524 L2X0_ADDR_FILTER_END);
525 l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
526 L2X0_ADDR_FILTER_START);
527
528 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
529 /*
530 * From r2p0, there is Prefetch offset/control register
531 */
532 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
533 L2X0_PREFETCH_CTRL);
534 /*
535 * From r3p0, there is Power control register
536 */
537 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
538 l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
539 L2X0_POWER_CTRL);
540 }
541}
542
543static void l2x0_resume(void)
544{
545 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
546 /* restore aux ctrl and enable l2 */
547 l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
548
549 writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
550 L2X0_AUX_CTRL);
551
552 l2x0_inv_all();
553
554 writel_relaxed(1, l2x0_base + L2X0_CTRL);
555 }
556}
557
558static void pl310_resume(void)
559{
560 u32 l2x0_revision;
561
562 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
563 /* restore pl310 setup */
564 writel_relaxed(l2x0_saved_regs.tag_latency,
565 l2x0_base + L2X0_TAG_LATENCY_CTRL);
566 writel_relaxed(l2x0_saved_regs.data_latency,
567 l2x0_base + L2X0_DATA_LATENCY_CTRL);
568 writel_relaxed(l2x0_saved_regs.filter_end,
569 l2x0_base + L2X0_ADDR_FILTER_END);
570 writel_relaxed(l2x0_saved_regs.filter_start,
571 l2x0_base + L2X0_ADDR_FILTER_START);
572
573 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
574 L2X0_CACHE_ID_RTL_MASK;
575
576 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
577 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
578 l2x0_base + L2X0_PREFETCH_CTRL);
579 if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
580 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
581 l2x0_base + L2X0_POWER_CTRL);
582 }
583 }
584
585 l2x0_resume();
586}
587
Taniya Das38a8c6e2012-05-09 20:34:39 +0530588#ifdef CONFIG_OF
Barry Song91c2ebb2011-09-30 14:43:12 +0100589static const struct l2x0_of_data pl310_data = {
590 pl310_of_setup,
591 pl310_save,
592 pl310_resume,
593};
594
595static const struct l2x0_of_data l2x0_data = {
596 l2x0_of_setup,
597 NULL,
598 l2x0_resume,
599};
600
Rob Herring8c369262011-08-03 18:12:05 +0100601static const struct of_device_id l2x0_ids[] __initconst = {
Barry Song91c2ebb2011-09-30 14:43:12 +0100602 { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
603 { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
604 { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
Rob Herring8c369262011-08-03 18:12:05 +0100605 {}
606};
607
Russell King3e175ca2011-09-18 11:27:30 +0100608int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +0100609{
610 struct device_node *np;
Barry Song91c2ebb2011-09-30 14:43:12 +0100611 struct l2x0_of_data *data;
612 struct resource res;
Rob Herring8c369262011-08-03 18:12:05 +0100613
614 np = of_find_matching_node(NULL, l2x0_ids);
615 if (!np)
616 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +0100617
618 if (of_address_to_resource(np, 0, &res))
619 return -ENODEV;
620
621 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +0100622 if (!l2x0_base)
623 return -ENOMEM;
624
Barry Song91c2ebb2011-09-30 14:43:12 +0100625 l2x0_saved_regs.phy_base = res.start;
626
627 data = of_match_node(l2x0_ids, np)->data;
628
Rob Herring8c369262011-08-03 18:12:05 +0100629 /* L2 configuration can only be changed if the cache is disabled */
630 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
Barry Song91c2ebb2011-09-30 14:43:12 +0100631 if (data->setup)
632 data->setup(np, &aux_val, &aux_mask);
Rob Herring8c369262011-08-03 18:12:05 +0100633 }
Barry Song91c2ebb2011-09-30 14:43:12 +0100634
635 if (data->save)
636 data->save();
637
Rob Herring8c369262011-08-03 18:12:05 +0100638 l2x0_init(l2x0_base, aux_val, aux_mask);
Barry Song91c2ebb2011-09-30 14:43:12 +0100639
640 outer_cache.resume = data->resume;
Rob Herring8c369262011-08-03 18:12:05 +0100641 return 0;
642}
643#endif
Taniya Das38a8c6e2012-05-09 20:34:39 +0530644
645void l2cc_suspend(void)
646{
647 l2x0_disable();
648 dmb();
649}
650
651void l2cc_resume(void)
652{
653 pl310_resume();
654 dmb();
655}