blob: 9586be73ca4f947ec16efe718bdf1f79ac7c98ec [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>
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +010028#include "cache-tauros3.h"
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +010029#include "cache-aurora-l2.h"
Catalin Marinas382266a2007-02-05 14:48:19 +010030
Russell Kingc02642b2014-03-15 16:47:54 +000031struct l2c_init_data {
Russell King3b8bad52014-03-15 16:47:57 +000032 unsigned num_lock;
Russell Kingc02642b2014-03-15 16:47:54 +000033 void (*of_parse)(const struct device_node *, u32 *, u32 *);
Russell King3b8bad52014-03-15 16:47:57 +000034 void (*enable)(void __iomem *, u32, unsigned);
Russell King75461f52014-03-15 16:48:07 +000035 void (*fixup)(void __iomem *, u32, struct outer_cache_fns *);
Russell King9846dfc2014-03-15 16:47:55 +000036 void (*save)(void __iomem *);
Russell Kingc02642b2014-03-15 16:47:54 +000037 struct outer_cache_fns outer_cache;
38};
39
Catalin Marinas382266a2007-02-05 14:48:19 +010040#define CACHE_LINE_SIZE 32
41
42static void __iomem *l2x0_base;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050043static DEFINE_RAW_SPINLOCK(l2x0_lock);
Russell King3e175ca2011-09-18 11:27:30 +010044static u32 l2x0_way_mask; /* Bitmask of active ways */
45static u32 l2x0_size;
Will Deaconf154fe92012-04-20 17:21:08 +010046static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
Catalin Marinas382266a2007-02-05 14:48:19 +010047
Barry Song91c2ebb2011-09-30 14:43:12 +010048struct l2x0_regs l2x0_saved_regs;
49
Russell King37abcdb2014-03-15 16:47:50 +000050/*
51 * Common code for all cache controllers.
52 */
Russell King83841fe2014-03-15 16:48:14 +000053static inline void l2c_wait_mask(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010054{
Catalin Marinas9a6655e2010-08-31 13:05:22 +010055 /* wait for cache operation by line or way to complete */
Catalin Marinas6775a552010-07-28 22:01:25 +010056 while (readl_relaxed(reg) & mask)
Barry Song1caf3092011-09-09 10:30:34 +010057 cpu_relax();
Catalin Marinas382266a2007-02-05 14:48:19 +010058}
59
Russell King2b2a87a2014-03-16 17:19:21 +000060/*
61 * This should only be called when we have a requirement that the
62 * register be written due to a work-around, as platforms running
63 * in non-secure mode may not be able to access this register.
64 */
65static inline void l2c_set_debug(void __iomem *base, unsigned long val)
66{
67 outer_cache.set_debug(val);
68}
69
Russell Kingdf5dd4c2014-03-15 16:47:56 +000070static void __l2c_op_way(void __iomem *reg)
71{
72 writel_relaxed(l2x0_way_mask, reg);
Russell King83841fe2014-03-15 16:48:14 +000073 l2c_wait_mask(reg, l2x0_way_mask);
Russell Kingdf5dd4c2014-03-15 16:47:56 +000074}
75
Russell King37abcdb2014-03-15 16:47:50 +000076static inline void l2c_unlock(void __iomem *base, unsigned num)
77{
78 unsigned i;
79
80 for (i = 0; i < num; i++) {
81 writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_D_BASE +
82 i * L2X0_LOCKDOWN_STRIDE);
83 writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_I_BASE +
84 i * L2X0_LOCKDOWN_STRIDE);
85 }
86}
87
Russell King3b8bad52014-03-15 16:47:57 +000088/*
89 * Enable the L2 cache controller. This function must only be
90 * called when the cache controller is known to be disabled.
91 */
92static void l2c_enable(void __iomem *base, u32 aux, unsigned num_lock)
93{
94 unsigned long flags;
95
Russell King9a07f272014-03-17 20:10:31 +000096 /* Only write the aux register if it needs changing */
97 if (readl_relaxed(base + L2X0_AUX_CTRL) != aux)
98 writel_relaxed(aux, base + L2X0_AUX_CTRL);
Russell King3b8bad52014-03-15 16:47:57 +000099
Russell King17f3f992014-03-17 17:15:02 +0000100 l2c_unlock(base, num_lock);
101
Russell King3b8bad52014-03-15 16:47:57 +0000102 local_irq_save(flags);
103 __l2c_op_way(base + L2X0_INV_WAY);
104 writel_relaxed(0, base + sync_reg_offset);
105 l2c_wait_mask(base + sync_reg_offset, 1);
106 local_irq_restore(flags);
107
108 writel_relaxed(L2X0_CTRL_EN, base + L2X0_CTRL);
109}
110
111static void l2c_disable(void)
112{
113 void __iomem *base = l2x0_base;
114
115 outer_cache.flush_all();
116 writel_relaxed(0, base + L2X0_CTRL);
117 dsb(st);
118}
119
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100120#ifdef CONFIG_CACHE_PL310
121static inline void cache_wait(void __iomem *reg, unsigned long mask)
122{
123 /* cache operations by line are atomic on PL310 */
124}
125#else
Russell King83841fe2014-03-15 16:48:14 +0000126#define cache_wait l2c_wait_mask
Catalin Marinas9a6655e2010-08-31 13:05:22 +0100127#endif
128
Catalin Marinas382266a2007-02-05 14:48:19 +0100129static inline void cache_sync(void)
130{
Russell King3d107432009-11-19 11:41:09 +0000131 void __iomem *base = l2x0_base;
Srinidhi Kasagar885028e2011-02-17 07:03:51 +0100132
Will Deaconf154fe92012-04-20 17:21:08 +0100133 writel_relaxed(0, base + sync_reg_offset);
Russell King3d107432009-11-19 11:41:09 +0000134 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100135}
136
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100137static inline void l2x0_clean_line(unsigned long addr)
138{
139 void __iomem *base = l2x0_base;
140 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100141 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100142}
143
144static inline void l2x0_inv_line(unsigned long addr)
145{
146 void __iomem *base = l2x0_base;
147 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100148 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100149}
150
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100151#if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
Will Deaconab4d5362012-04-20 17:22:11 +0100152static inline void debug_writel(unsigned long val)
153{
154 if (outer_cache.set_debug)
Russell King2b2a87a2014-03-16 17:19:21 +0000155 l2c_set_debug(l2x0_base, val);
Will Deaconab4d5362012-04-20 17:22:11 +0100156}
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100157#else
158/* Optimised out for non-errata case */
159static inline void debug_writel(unsigned long val)
160{
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100161}
Santosh Shilimkar2839e062011-03-08 06:59:54 +0100162#endif
163
164#ifdef CONFIG_PL310_ERRATA_588369
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100165static inline void l2x0_flush_line(unsigned long addr)
166{
167 void __iomem *base = l2x0_base;
168
169 /* Clean by PA followed by Invalidate by PA */
170 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100171 writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100172 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100173 writel_relaxed(addr, base + L2X0_INV_LINE_PA);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100174}
175#else
176
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100177static inline void l2x0_flush_line(unsigned long addr)
178{
179 void __iomem *base = l2x0_base;
180 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas6775a552010-07-28 22:01:25 +0100181 writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100182}
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100183#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100184
Catalin Marinas23107c52010-03-24 16:48:53 +0100185static void l2x0_cache_sync(void)
186{
187 unsigned long flags;
188
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500189 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100190 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500191 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas23107c52010-03-24 16:48:53 +0100192}
193
Will Deacon38a89142011-07-01 14:36:19 +0100194static void __l2x0_flush_all(void)
195{
196 debug_writel(0x03);
Russell Kingdf5dd4c2014-03-15 16:47:56 +0000197 __l2c_op_way(l2x0_base + L2X0_CLEAN_INV_WAY);
Will Deacon38a89142011-07-01 14:36:19 +0100198 cache_sync();
199 debug_writel(0x00);
200}
201
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530202static void l2x0_flush_all(void)
203{
204 unsigned long flags;
205
206 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500207 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100208 __l2x0_flush_all();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500209 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530210}
211
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530212static void l2x0_clean_all(void)
213{
214 unsigned long flags;
215
216 /* clean all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500217 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell Kingdf5dd4c2014-03-15 16:47:56 +0000218 __l2c_op_way(l2x0_base + L2X0_CLEAN_WAY);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530219 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500220 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530221}
222
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530223static void l2x0_inv_all(void)
Catalin Marinas382266a2007-02-05 14:48:19 +0100224{
Russell King0eb948d2009-11-19 11:12:15 +0000225 unsigned long flags;
226
Catalin Marinas382266a2007-02-05 14:48:19 +0100227 /* invalidate all ways */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500228 raw_spin_lock_irqsave(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530229 /* Invalidating when L2 is enabled is a nono */
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100230 BUG_ON(readl(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN);
Russell Kingdf5dd4c2014-03-15 16:47:56 +0000231 __l2c_op_way(l2x0_base + L2X0_INV_WAY);
Catalin Marinas382266a2007-02-05 14:48:19 +0100232 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500233 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100234}
235
236static void l2x0_inv_range(unsigned long start, unsigned long end)
237{
Russell King3d107432009-11-19 11:41:09 +0000238 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000239 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100240
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500241 raw_spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100242 if (start & (CACHE_LINE_SIZE - 1)) {
243 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100244 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100245 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100246 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100247 start += CACHE_LINE_SIZE;
248 }
249
250 if (end & (CACHE_LINE_SIZE - 1)) {
251 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100252 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100253 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100254 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100255 }
256
Russell King0eb948d2009-11-19 11:12:15 +0000257 while (start < end) {
258 unsigned long blk_end = start + min(end - start, 4096UL);
259
260 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100261 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000262 start += CACHE_LINE_SIZE;
263 }
264
265 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500266 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
267 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000268 }
269 }
Russell King3d107432009-11-19 11:41:09 +0000270 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100271 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500272 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100273}
274
275static void l2x0_clean_range(unsigned long start, unsigned long end)
276{
Russell King3d107432009-11-19 11:41:09 +0000277 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000278 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100279
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530280 if ((end - start) >= l2x0_size) {
281 l2x0_clean_all();
282 return;
283 }
284
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500285 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100286 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000287 while (start < end) {
288 unsigned long blk_end = start + min(end - start, 4096UL);
289
290 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100291 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000292 start += CACHE_LINE_SIZE;
293 }
294
295 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500296 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
297 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000298 }
299 }
Russell King3d107432009-11-19 11:41:09 +0000300 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100301 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500302 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100303}
304
305static void l2x0_flush_range(unsigned long start, unsigned long end)
306{
Russell King3d107432009-11-19 11:41:09 +0000307 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000308 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100309
Santosh Shilimkar444457c2010-07-11 14:58:41 +0530310 if ((end - start) >= l2x0_size) {
311 l2x0_flush_all();
312 return;
313 }
314
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500315 raw_spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100316 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000317 while (start < end) {
318 unsigned long blk_end = start + min(end - start, 4096UL);
319
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100320 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000321 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100322 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000323 start += CACHE_LINE_SIZE;
324 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100325 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000326
327 if (blk_end < end) {
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500328 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
329 raw_spin_lock_irqsave(&l2x0_lock, flags);
Russell King0eb948d2009-11-19 11:12:15 +0000330 }
331 }
Russell King3d107432009-11-19 11:41:09 +0000332 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100333 cache_sync();
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500334 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100335}
336
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530337static void l2x0_disable(void)
338{
339 unsigned long flags;
340
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500341 raw_spin_lock_irqsave(&l2x0_lock, flags);
Will Deacon38a89142011-07-01 14:36:19 +0100342 __l2x0_flush_all();
343 writel_relaxed(0, l2x0_base + L2X0_CTRL);
Will Deacon9781aa82013-06-12 09:59:59 +0100344 dsb(st);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500345 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
Thomas Gleixner2fd86582010-07-31 21:05:24 +0530346}
347
Russell King3b8bad52014-03-15 16:47:57 +0000348static void l2x0_enable(void __iomem *base, u32 aux, unsigned num_lock)
349{
Russell Kingfaf9b2e2014-03-15 22:49:59 +0000350 unsigned id;
351
352 id = readl_relaxed(base + L2X0_CACHE_ID) & L2X0_CACHE_ID_PART_MASK;
353 if (id == L2X0_CACHE_ID_PART_L310)
354 num_lock = 8;
355 else
356 num_lock = 1;
357
Russell King3b8bad52014-03-15 16:47:57 +0000358 /* l2x0 controller is disabled */
359 writel_relaxed(aux, base + L2X0_AUX_CTRL);
360
Russell King17f3f992014-03-17 17:15:02 +0000361 /* Make sure that I&D is not locked down when starting */
Russell Kingfaf9b2e2014-03-15 22:49:59 +0000362 l2c_unlock(base, num_lock);
Russell King17f3f992014-03-17 17:15:02 +0000363
Russell King3b8bad52014-03-15 16:47:57 +0000364 l2x0_inv_all();
365
366 /* enable L2X0 */
367 writel_relaxed(L2X0_CTRL_EN, base + L2X0_CTRL);
368}
369
Russell Kingb98556f22014-03-15 16:48:11 +0000370static void l2x0_resume(void)
371{
Russell King09a5d182014-03-15 16:48:13 +0000372 void __iomem *base = l2x0_base;
Russell Kingb98556f22014-03-15 16:48:11 +0000373
Russell King09a5d182014-03-15 16:48:13 +0000374 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN))
375 l2x0_enable(base, l2x0_saved_regs.aux_ctrl, 0);
Russell Kingb98556f22014-03-15 16:48:11 +0000376}
377
Russell King96054b02014-03-15 16:47:52 +0000378static const struct l2c_init_data l2x0_init_fns __initconst = {
Russell King3b8bad52014-03-15 16:47:57 +0000379 .enable = l2x0_enable,
Russell King96054b02014-03-15 16:47:52 +0000380 .outer_cache = {
381 .inv_range = l2x0_inv_range,
382 .clean_range = l2x0_clean_range,
383 .flush_range = l2x0_flush_range,
384 .flush_all = l2x0_flush_all,
385 .disable = l2x0_disable,
386 .sync = l2x0_cache_sync,
Russell Kingb98556f22014-03-15 16:48:11 +0000387 .resume = l2x0_resume,
Russell King96054b02014-03-15 16:47:52 +0000388 },
389};
390
Russell King75461f52014-03-15 16:48:07 +0000391/*
392 * L2C-310 specific code.
393 *
394 * Errata:
395 * 588369: PL310 R0P0->R1P0, fixed R2P0.
396 * Affects: all clean+invalidate operations
397 * clean and invalidate skips the invalidate step, so we need to issue
398 * separate operations. We also require the above debug workaround
399 * enclosing this code fragment on affected parts. On unaffected parts,
400 * we must not use this workaround without the debug register writes
401 * to avoid exposing a problem similar to 727915.
402 *
403 * 727915: PL310 R2P0->R3P0, fixed R3P1.
404 * Affects: clean+invalidate by way
405 * clean and invalidate by way runs in the background, and a store can
406 * hit the line between the clean operation and invalidate operation,
407 * resulting in the store being lost.
408 *
409 * 753970: PL310 R3P0, fixed R3P1.
410 * Affects: sync
411 * prevents merging writes after the sync operation, until another L2C
412 * operation is performed (or a number of other conditions.)
413 *
414 * 769419: PL310 R0P0->R3P1, fixed R3P2.
415 * Affects: store buffer
416 * store buffer is not automatically drained.
417 */
Russell Kingbda0b742014-03-15 16:48:16 +0000418static void l2c310_set_debug(unsigned long val)
419{
420 writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
421}
422
Russell King09a5d182014-03-15 16:48:13 +0000423static void __init l2c310_save(void __iomem *base)
Russell Kingb98556f22014-03-15 16:48:11 +0000424{
Russell King09a5d182014-03-15 16:48:13 +0000425 unsigned revision;
Russell Kingb98556f22014-03-15 16:48:11 +0000426
427 l2x0_saved_regs.tag_latency = readl_relaxed(base +
428 L2X0_TAG_LATENCY_CTRL);
429 l2x0_saved_regs.data_latency = readl_relaxed(base +
430 L2X0_DATA_LATENCY_CTRL);
431 l2x0_saved_regs.filter_end = readl_relaxed(base +
432 L2X0_ADDR_FILTER_END);
433 l2x0_saved_regs.filter_start = readl_relaxed(base +
434 L2X0_ADDR_FILTER_START);
435
Russell King09a5d182014-03-15 16:48:13 +0000436 revision = readl_relaxed(base + L2X0_CACHE_ID) &
Russell Kingb98556f22014-03-15 16:48:11 +0000437 L2X0_CACHE_ID_RTL_MASK;
438
Russell King09a5d182014-03-15 16:48:13 +0000439 /* From r2p0, there is Prefetch offset/control register */
440 if (revision >= L310_CACHE_ID_RTL_R2P0)
441 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(base +
442 L2X0_PREFETCH_CTRL);
Russell Kingb98556f22014-03-15 16:48:11 +0000443
Russell King09a5d182014-03-15 16:48:13 +0000444 /* From r3p0, there is Power control register */
445 if (revision >= L310_CACHE_ID_RTL_R3P0)
446 l2x0_saved_regs.pwr_ctrl = readl_relaxed(base +
447 L2X0_POWER_CTRL);
448}
449
450static void l2c310_resume(void)
451{
452 void __iomem *base = l2x0_base;
453
454 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
455 unsigned revision;
456
457 /* restore pl310 setup */
458 writel_relaxed(l2x0_saved_regs.tag_latency,
459 base + L2X0_TAG_LATENCY_CTRL);
460 writel_relaxed(l2x0_saved_regs.data_latency,
461 base + L2X0_DATA_LATENCY_CTRL);
462 writel_relaxed(l2x0_saved_regs.filter_end,
463 base + L2X0_ADDR_FILTER_END);
464 writel_relaxed(l2x0_saved_regs.filter_start,
465 base + L2X0_ADDR_FILTER_START);
466
467 revision = readl_relaxed(base + L2X0_CACHE_ID) &
468 L2X0_CACHE_ID_RTL_MASK;
469
470 if (revision >= L310_CACHE_ID_RTL_R2P0)
471 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
472 base + L2X0_PREFETCH_CTRL);
473 if (revision >= L310_CACHE_ID_RTL_R3P0)
474 writel_relaxed(l2x0_saved_regs.pwr_ctrl,
475 base + L2X0_POWER_CTRL);
476
477 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
478 }
Russell Kingb98556f22014-03-15 16:48:11 +0000479}
480
Russell King75461f52014-03-15 16:48:07 +0000481static void __init l2c310_fixup(void __iomem *base, u32 cache_id,
482 struct outer_cache_fns *fns)
483{
484 unsigned revision = cache_id & L2X0_CACHE_ID_RTL_MASK;
485 const char *errata[4];
486 unsigned n = 0;
487
488 if (revision <= L310_CACHE_ID_RTL_R3P0)
Russell Kingbda0b742014-03-15 16:48:16 +0000489 fns->set_debug = l2c310_set_debug;
Russell King75461f52014-03-15 16:48:07 +0000490
491 if (IS_ENABLED(CONFIG_PL310_ERRATA_753970) &&
492 revision == L310_CACHE_ID_RTL_R3P0) {
493 sync_reg_offset = L2X0_DUMMY_REG;
494 errata[n++] = "753970";
495 }
496
497 if (IS_ENABLED(CONFIG_PL310_ERRATA_769419))
498 errata[n++] = "769419";
499
500 if (n) {
501 unsigned i;
502
503 pr_info("L2C-310 errat%s", n > 1 ? "a" : "um");
504 for (i = 0; i < n; i++)
505 pr_cont(" %s", errata[i]);
506 pr_cont(" enabled\n");
507 }
508}
509
510static const struct l2c_init_data l2c310_init_fns __initconst = {
511 .num_lock = 8,
512 .enable = l2c_enable,
513 .fixup = l2c310_fixup,
Russell King09a5d182014-03-15 16:48:13 +0000514 .save = l2c310_save,
Russell King75461f52014-03-15 16:48:07 +0000515 .outer_cache = {
516 .inv_range = l2x0_inv_range,
517 .clean_range = l2x0_clean_range,
518 .flush_range = l2x0_flush_range,
519 .flush_all = l2x0_flush_all,
520 .disable = l2x0_disable,
521 .sync = l2x0_cache_sync,
Russell King09a5d182014-03-15 16:48:13 +0000522 .resume = l2c310_resume,
Russell King75461f52014-03-15 16:48:07 +0000523 },
524};
525
Russell King96054b02014-03-15 16:47:52 +0000526static void __init __l2c_init(const struct l2c_init_data *data,
527 u32 aux_val, u32 aux_mask, u32 cache_id)
Catalin Marinas382266a2007-02-05 14:48:19 +0100528{
Russell King75461f52014-03-15 16:48:07 +0000529 struct outer_cache_fns fns;
Russell King3e175ca2011-09-18 11:27:30 +0100530 u32 aux;
Russell King3e175ca2011-09-18 11:27:30 +0100531 u32 way_size = 0;
Jason McMullan64039be2010-05-05 18:59:37 +0100532 int ways;
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100533 int way_size_shift = L2X0_WAY_SIZE_SHIFT;
Jason McMullan64039be2010-05-05 18:59:37 +0100534 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100535
Russell Kingc40e7eb2014-03-15 16:48:04 +0000536 /*
537 * It is strange to save the register state before initialisation,
538 * but hey, this is what the DT implementations decided to do.
539 */
540 if (data->save)
541 data->save(l2x0_base);
542
Catalin Marinas6775a552010-07-28 22:01:25 +0100543 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
Jason McMullan64039be2010-05-05 18:59:37 +0100544
Sascha Hauer4082cfa2010-07-08 08:36:21 +0100545 aux &= aux_mask;
546 aux |= aux_val;
547
Jason McMullan64039be2010-05-05 18:59:37 +0100548 /* Determine the number of ways */
Rob Herring6e7acee2013-03-25 17:02:48 +0100549 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
Jason McMullan64039be2010-05-05 18:59:37 +0100550 case L2X0_CACHE_ID_PART_L310:
551 if (aux & (1 << 16))
552 ways = 16;
553 else
554 ways = 8;
555 type = "L310";
556 break;
Russell King75461f52014-03-15 16:48:07 +0000557
Jason McMullan64039be2010-05-05 18:59:37 +0100558 case L2X0_CACHE_ID_PART_L210:
559 ways = (aux >> 13) & 0xf;
560 type = "L210";
561 break;
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100562
563 case AURORA_CACHE_ID:
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100564 ways = (aux >> 13) & 0xf;
565 ways = 2 << ((ways + 1) >> 2);
566 way_size_shift = AURORA_WAY_SIZE_SHIFT;
567 type = "Aurora";
568 break;
Russell King75461f52014-03-15 16:48:07 +0000569
Jason McMullan64039be2010-05-05 18:59:37 +0100570 default:
571 /* Assume unknown chips have 8 ways */
572 ways = 8;
573 type = "L2x0 series";
574 break;
575 }
576
577 l2x0_way_mask = (1 << ways) - 1;
578
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100579 /*
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530580 * L2 cache Size = Way size * Number of ways
581 */
582 way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100583 way_size = 1 << (way_size + way_size_shift);
584
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530585 l2x0_size = ways * way_size * SZ_1K;
586
Russell King75461f52014-03-15 16:48:07 +0000587 fns = data->outer_cache;
588 if (data->fixup)
589 data->fixup(l2x0_base, cache_id, &fns);
590
Santosh Shilimkar5ba70372010-07-11 14:35:37 +0530591 /*
Russell King3b8bad52014-03-15 16:47:57 +0000592 * Check if l2x0 controller is already enabled. If we are booting
593 * in non-secure mode accessing the below registers will fault.
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100594 */
Russell King3b8bad52014-03-15 16:47:57 +0000595 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
596 data->enable(l2x0_base, aux, data->num_lock);
Catalin Marinas382266a2007-02-05 14:48:19 +0100597
Yilu Mao9d4876f2012-09-03 09:14:56 +0100598 /* Re-read it in case some bits are reserved. */
599 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
600
601 /* Save the value for resuming. */
602 l2x0_saved_regs.aux_ctrl = aux;
603
Russell King75461f52014-03-15 16:48:07 +0000604 outer_cache = fns;
Catalin Marinas382266a2007-02-05 14:48:19 +0100605
Russell Kingcdef8682014-03-15 16:48:08 +0000606 pr_info("%s cache controller enabled, %d ways, %d kB\n",
607 type, ways, l2x0_size >> 10);
608 pr_info("%s: CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
609 type, cache_id, aux);
Catalin Marinas382266a2007-02-05 14:48:19 +0100610}
Rob Herring8c369262011-08-03 18:12:05 +0100611
Russell King96054b02014-03-15 16:47:52 +0000612void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
613{
Russell King75461f52014-03-15 16:48:07 +0000614 const struct l2c_init_data *data;
Russell King96054b02014-03-15 16:47:52 +0000615 u32 cache_id;
616
617 l2x0_base = base;
618
619 cache_id = readl_relaxed(base + L2X0_CACHE_ID);
620
Russell King75461f52014-03-15 16:48:07 +0000621 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
622 default:
623 data = &l2x0_init_fns;
624 break;
625
626 case L2X0_CACHE_ID_PART_L310:
627 data = &l2c310_init_fns;
628 break;
629 }
630
631 __l2c_init(data, aux_val, aux_mask, cache_id);
Russell King96054b02014-03-15 16:47:52 +0000632}
633
Rob Herring8c369262011-08-03 18:12:05 +0100634#ifdef CONFIG_OF
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100635static int l2_wt_override;
636
Russell King96054b02014-03-15 16:47:52 +0000637/* Aurora don't have the cache ID register available, so we have to
638 * pass it though the device tree */
639static u32 cache_id_part_number_from_dt;
640
Russell Kingda3627f2014-03-15 16:48:06 +0000641static void __init l2x0_of_parse(const struct device_node *np,
642 u32 *aux_val, u32 *aux_mask)
643{
644 u32 data[2] = { 0, 0 };
645 u32 tag = 0;
646 u32 dirty = 0;
647 u32 val = 0, mask = 0;
648
649 of_property_read_u32(np, "arm,tag-latency", &tag);
650 if (tag) {
651 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
652 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
653 }
654
655 of_property_read_u32_array(np, "arm,data-latency",
656 data, ARRAY_SIZE(data));
657 if (data[0] && data[1]) {
658 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
659 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
660 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
661 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
662 }
663
664 of_property_read_u32(np, "arm,dirty-latency", &dirty);
665 if (dirty) {
666 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
667 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
668 }
669
670 *aux_val &= ~mask;
671 *aux_val |= val;
672 *aux_mask &= ~mask;
673}
674
Russell Kingda3627f2014-03-15 16:48:06 +0000675static const struct l2c_init_data of_l2x0_data __initconst = {
676 .of_parse = l2x0_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +0000677 .enable = l2x0_enable,
Russell Kingda3627f2014-03-15 16:48:06 +0000678 .outer_cache = {
679 .inv_range = l2x0_inv_range,
680 .clean_range = l2x0_clean_range,
681 .flush_range = l2x0_flush_range,
682 .flush_all = l2x0_flush_all,
683 .disable = l2x0_disable,
684 .sync = l2x0_cache_sync,
685 .resume = l2x0_resume,
686 },
687};
688
689static void __init pl310_of_parse(const struct device_node *np,
690 u32 *aux_val, u32 *aux_mask)
691{
692 u32 data[3] = { 0, 0, 0 };
693 u32 tag[3] = { 0, 0, 0 };
694 u32 filter[2] = { 0, 0 };
695
696 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
697 if (tag[0] && tag[1] && tag[2])
698 writel_relaxed(
699 ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
700 ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
701 ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
702 l2x0_base + L2X0_TAG_LATENCY_CTRL);
703
704 of_property_read_u32_array(np, "arm,data-latency",
705 data, ARRAY_SIZE(data));
706 if (data[0] && data[1] && data[2])
707 writel_relaxed(
708 ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
709 ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
710 ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
711 l2x0_base + L2X0_DATA_LATENCY_CTRL);
712
713 of_property_read_u32_array(np, "arm,filter-ranges",
714 filter, ARRAY_SIZE(filter));
715 if (filter[1]) {
716 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
717 l2x0_base + L2X0_ADDR_FILTER_END);
718 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
719 l2x0_base + L2X0_ADDR_FILTER_START);
720 }
721}
722
Russell Kingda3627f2014-03-15 16:48:06 +0000723static const struct l2c_init_data of_pl310_data __initconst = {
Russell King3b8bad52014-03-15 16:47:57 +0000724 .num_lock = 8,
Russell Kingda3627f2014-03-15 16:48:06 +0000725 .of_parse = pl310_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +0000726 .enable = l2c_enable,
Russell King75461f52014-03-15 16:48:07 +0000727 .fixup = l2c310_fixup,
Russell King09a5d182014-03-15 16:48:13 +0000728 .save = l2c310_save,
Russell Kingda3627f2014-03-15 16:48:06 +0000729 .outer_cache = {
730 .inv_range = l2x0_inv_range,
731 .clean_range = l2x0_clean_range,
732 .flush_range = l2x0_flush_range,
733 .flush_all = l2x0_flush_all,
734 .disable = l2x0_disable,
735 .sync = l2x0_cache_sync,
Russell King09a5d182014-03-15 16:48:13 +0000736 .resume = l2c310_resume,
Russell Kingda3627f2014-03-15 16:48:06 +0000737 },
738};
739
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100740/*
741 * Note that the end addresses passed to Linux primitives are
742 * noninclusive, while the hardware cache range operations use
743 * inclusive start and end addresses.
744 */
745static unsigned long calc_range_end(unsigned long start, unsigned long end)
746{
747 /*
748 * Limit the number of cache lines processed at once,
749 * since cache range operations stall the CPU pipeline
750 * until completion.
751 */
752 if (end > start + MAX_RANGE_SIZE)
753 end = start + MAX_RANGE_SIZE;
754
755 /*
756 * Cache range operations can't straddle a page boundary.
757 */
758 if (end > PAGE_ALIGN(start+1))
759 end = PAGE_ALIGN(start+1);
760
761 return end;
762}
763
764/*
765 * Make sure 'start' and 'end' reference the same page, as L2 is PIPT
766 * and range operations only do a TLB lookup on the start address.
767 */
768static void aurora_pa_range(unsigned long start, unsigned long end,
769 unsigned long offset)
770{
771 unsigned long flags;
772
773 raw_spin_lock_irqsave(&l2x0_lock, flags);
Gregory CLEMENT8a3a1802013-01-07 11:28:42 +0100774 writel_relaxed(start, l2x0_base + AURORA_RANGE_BASE_ADDR_REG);
775 writel_relaxed(end, l2x0_base + offset);
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100776 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
777
778 cache_sync();
779}
780
781static void aurora_inv_range(unsigned long start, unsigned long end)
782{
783 /*
784 * round start and end adresses up to cache line size
785 */
786 start &= ~(CACHE_LINE_SIZE - 1);
787 end = ALIGN(end, CACHE_LINE_SIZE);
788
789 /*
790 * Invalidate all full cache lines between 'start' and 'end'.
791 */
792 while (start < end) {
793 unsigned long range_end = calc_range_end(start, end);
794 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
795 AURORA_INVAL_RANGE_REG);
796 start = range_end;
797 }
798}
799
800static void aurora_clean_range(unsigned long start, unsigned long end)
801{
802 /*
803 * If L2 is forced to WT, the L2 will always be clean and we
804 * don't need to do anything here.
805 */
806 if (!l2_wt_override) {
807 start &= ~(CACHE_LINE_SIZE - 1);
808 end = ALIGN(end, CACHE_LINE_SIZE);
809 while (start != end) {
810 unsigned long range_end = calc_range_end(start, end);
811 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
812 AURORA_CLEAN_RANGE_REG);
813 start = range_end;
814 }
815 }
816}
817
818static void aurora_flush_range(unsigned long start, unsigned long end)
819{
Gregory CLEMENT8b827c62013-01-07 11:27:14 +0100820 start &= ~(CACHE_LINE_SIZE - 1);
821 end = ALIGN(end, CACHE_LINE_SIZE);
822 while (start != end) {
823 unsigned long range_end = calc_range_end(start, end);
824 /*
825 * If L2 is forced to WT, the L2 will always be clean and we
826 * just need to invalidate.
827 */
828 if (l2_wt_override)
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100829 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
Gregory CLEMENT8b827c62013-01-07 11:27:14 +0100830 AURORA_INVAL_RANGE_REG);
831 else
832 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
833 AURORA_FLUSH_RANGE_REG);
834 start = range_end;
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +0100835 }
836}
837
Russell Kingda3627f2014-03-15 16:48:06 +0000838static void aurora_save(void __iomem *base)
839{
840 l2x0_saved_regs.ctrl = readl_relaxed(base + L2X0_CTRL);
841 l2x0_saved_regs.aux_ctrl = readl_relaxed(base + L2X0_AUX_CTRL);
842}
843
844static void aurora_resume(void)
845{
Russell King09a5d182014-03-15 16:48:13 +0000846 void __iomem *base = l2x0_base;
847
848 if (!(readl(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
849 writel_relaxed(l2x0_saved_regs.aux_ctrl, base + L2X0_AUX_CTRL);
850 writel_relaxed(l2x0_saved_regs.ctrl, base + L2X0_CTRL);
Russell Kingda3627f2014-03-15 16:48:06 +0000851 }
852}
853
Russell King40266d62014-03-15 16:47:59 +0000854/*
855 * For Aurora cache in no outer mode, enable via the CP15 coprocessor
856 * broadcasting of cache commands to L2.
857 */
858static void __init aurora_enable_no_outer(void __iomem *base, u32 aux,
859 unsigned num_lock)
Russell Kingda3627f2014-03-15 16:48:06 +0000860{
Russell King40266d62014-03-15 16:47:59 +0000861 u32 u;
862
863 asm volatile("mrc p15, 1, %0, c15, c2, 0" : "=r" (u));
Russell Kingda3627f2014-03-15 16:48:06 +0000864 u |= AURORA_CTRL_FW; /* Set the FW bit */
Russell King40266d62014-03-15 16:47:59 +0000865 asm volatile("mcr p15, 1, %0, c15, c2, 0" : : "r" (u));
866
Russell Kingda3627f2014-03-15 16:48:06 +0000867 isb();
Russell King40266d62014-03-15 16:47:59 +0000868
869 l2c_enable(base, aux, num_lock);
Russell Kingda3627f2014-03-15 16:48:06 +0000870}
871
Russell King75461f52014-03-15 16:48:07 +0000872static void __init aurora_fixup(void __iomem *base, u32 cache_id,
873 struct outer_cache_fns *fns)
874{
875 sync_reg_offset = AURORA_SYNC_REG;
876}
877
Russell Kingda3627f2014-03-15 16:48:06 +0000878static void __init aurora_of_parse(const struct device_node *np,
879 u32 *aux_val, u32 *aux_mask)
880{
881 u32 val = AURORA_ACR_REPLACEMENT_TYPE_SEMIPLRU;
882 u32 mask = AURORA_ACR_REPLACEMENT_MASK;
883
884 of_property_read_u32(np, "cache-id-part",
885 &cache_id_part_number_from_dt);
886
887 /* Determine and save the write policy */
888 l2_wt_override = of_property_read_bool(np, "wt-override");
889
890 if (l2_wt_override) {
891 val |= AURORA_ACR_FORCE_WRITE_THRO_POLICY;
892 mask |= AURORA_ACR_FORCE_WRITE_POLICY_MASK;
893 }
894
895 *aux_val &= ~mask;
896 *aux_val |= val;
897 *aux_mask &= ~mask;
898}
899
900static const struct l2c_init_data of_aurora_with_outer_data __initconst = {
Russell King3b8bad52014-03-15 16:47:57 +0000901 .num_lock = 4,
Russell Kingda3627f2014-03-15 16:48:06 +0000902 .of_parse = aurora_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +0000903 .enable = l2c_enable,
Russell King75461f52014-03-15 16:48:07 +0000904 .fixup = aurora_fixup,
Russell Kingda3627f2014-03-15 16:48:06 +0000905 .save = aurora_save,
906 .outer_cache = {
907 .inv_range = aurora_inv_range,
908 .clean_range = aurora_clean_range,
909 .flush_range = aurora_flush_range,
910 .flush_all = l2x0_flush_all,
911 .disable = l2x0_disable,
912 .sync = l2x0_cache_sync,
913 .resume = aurora_resume,
914 },
915};
916
917static const struct l2c_init_data of_aurora_no_outer_data __initconst = {
Russell King3b8bad52014-03-15 16:47:57 +0000918 .num_lock = 4,
Russell Kingda3627f2014-03-15 16:48:06 +0000919 .of_parse = aurora_of_parse,
Russell King40266d62014-03-15 16:47:59 +0000920 .enable = aurora_enable_no_outer,
Russell King75461f52014-03-15 16:48:07 +0000921 .fixup = aurora_fixup,
Russell Kingda3627f2014-03-15 16:48:06 +0000922 .save = aurora_save,
923 .outer_cache = {
924 .resume = aurora_resume,
925 },
926};
927
Christian Daudt3b656fe2013-05-09 22:21:01 +0100928/*
929 * For certain Broadcom SoCs, depending on the address range, different offsets
930 * need to be added to the address before passing it to L2 for
931 * invalidation/clean/flush
932 *
933 * Section Address Range Offset EMI
934 * 1 0x00000000 - 0x3FFFFFFF 0x80000000 VC
935 * 2 0x40000000 - 0xBFFFFFFF 0x40000000 SYS
936 * 3 0xC0000000 - 0xFFFFFFFF 0x80000000 VC
937 *
938 * When the start and end addresses have crossed two different sections, we
939 * need to break the L2 operation into two, each within its own section.
940 * For example, if we need to invalidate addresses starts at 0xBFFF0000 and
941 * ends at 0xC0001000, we need do invalidate 1) 0xBFFF0000 - 0xBFFFFFFF and 2)
942 * 0xC0000000 - 0xC0001000
943 *
944 * Note 1:
945 * By breaking a single L2 operation into two, we may potentially suffer some
946 * performance hit, but keep in mind the cross section case is very rare
947 *
948 * Note 2:
949 * We do not need to handle the case when the start address is in
950 * Section 1 and the end address is in Section 3, since it is not a valid use
951 * case
952 *
953 * Note 3:
954 * Section 1 in practical terms can no longer be used on rev A2. Because of
955 * that the code does not need to handle section 1 at all.
956 *
957 */
958#define BCM_SYS_EMI_START_ADDR 0x40000000UL
959#define BCM_VC_EMI_SEC3_START_ADDR 0xC0000000UL
960
961#define BCM_SYS_EMI_OFFSET 0x40000000UL
962#define BCM_VC_EMI_OFFSET 0x80000000UL
963
964static inline int bcm_addr_is_sys_emi(unsigned long addr)
965{
966 return (addr >= BCM_SYS_EMI_START_ADDR) &&
967 (addr < BCM_VC_EMI_SEC3_START_ADDR);
968}
969
970static inline unsigned long bcm_l2_phys_addr(unsigned long addr)
971{
972 if (bcm_addr_is_sys_emi(addr))
973 return addr + BCM_SYS_EMI_OFFSET;
974 else
975 return addr + BCM_VC_EMI_OFFSET;
976}
977
978static void bcm_inv_range(unsigned long start, unsigned long end)
979{
980 unsigned long new_start, new_end;
981
982 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
983
984 if (unlikely(end <= start))
985 return;
986
987 new_start = bcm_l2_phys_addr(start);
988 new_end = bcm_l2_phys_addr(end);
989
990 /* normal case, no cross section between start and end */
991 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
992 l2x0_inv_range(new_start, new_end);
993 return;
994 }
995
996 /* They cross sections, so it can only be a cross from section
997 * 2 to section 3
998 */
999 l2x0_inv_range(new_start,
1000 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
1001 l2x0_inv_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
1002 new_end);
1003}
1004
1005static void bcm_clean_range(unsigned long start, unsigned long end)
1006{
1007 unsigned long new_start, new_end;
1008
1009 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1010
1011 if (unlikely(end <= start))
1012 return;
1013
1014 if ((end - start) >= l2x0_size) {
1015 l2x0_clean_all();
1016 return;
1017 }
1018
1019 new_start = bcm_l2_phys_addr(start);
1020 new_end = bcm_l2_phys_addr(end);
1021
1022 /* normal case, no cross section between start and end */
1023 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
1024 l2x0_clean_range(new_start, new_end);
1025 return;
1026 }
1027
1028 /* They cross sections, so it can only be a cross from section
1029 * 2 to section 3
1030 */
1031 l2x0_clean_range(new_start,
1032 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
1033 l2x0_clean_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
1034 new_end);
1035}
1036
1037static void bcm_flush_range(unsigned long start, unsigned long end)
1038{
1039 unsigned long new_start, new_end;
1040
1041 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1042
1043 if (unlikely(end <= start))
1044 return;
1045
1046 if ((end - start) >= l2x0_size) {
1047 l2x0_flush_all();
1048 return;
1049 }
1050
1051 new_start = bcm_l2_phys_addr(start);
1052 new_end = bcm_l2_phys_addr(end);
1053
1054 /* normal case, no cross section between start and end */
1055 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
1056 l2x0_flush_range(new_start, new_end);
1057 return;
1058 }
1059
1060 /* They cross sections, so it can only be a cross from section
1061 * 2 to section 3
1062 */
1063 l2x0_flush_range(new_start,
1064 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
1065 l2x0_flush_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
1066 new_end);
1067}
1068
Russell Kingda3627f2014-03-15 16:48:06 +00001069static const struct l2c_init_data of_bcm_l2x0_data __initconst = {
Russell King3b8bad52014-03-15 16:47:57 +00001070 .num_lock = 8,
Russell Kingda3627f2014-03-15 16:48:06 +00001071 .of_parse = pl310_of_parse,
Russell King3b8bad52014-03-15 16:47:57 +00001072 .enable = l2c_enable,
Russell King75461f52014-03-15 16:48:07 +00001073 .fixup = l2c310_fixup,
Russell King09a5d182014-03-15 16:48:13 +00001074 .save = l2c310_save,
Russell Kingda3627f2014-03-15 16:48:06 +00001075 .outer_cache = {
1076 .inv_range = bcm_inv_range,
1077 .clean_range = bcm_clean_range,
1078 .flush_range = bcm_flush_range,
1079 .flush_all = l2x0_flush_all,
1080 .disable = l2x0_disable,
1081 .sync = l2x0_cache_sync,
Russell King09a5d182014-03-15 16:48:13 +00001082 .resume = l2c310_resume,
Russell Kingda3627f2014-03-15 16:48:06 +00001083 },
1084};
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +01001085
Russell King9846dfc2014-03-15 16:47:55 +00001086static void __init tauros3_save(void __iomem *base)
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001087{
1088 l2x0_saved_regs.aux2_ctrl =
Russell King9846dfc2014-03-15 16:47:55 +00001089 readl_relaxed(base + TAUROS3_AUX2_CTRL);
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001090 l2x0_saved_regs.prefetch_ctrl =
Russell King9846dfc2014-03-15 16:47:55 +00001091 readl_relaxed(base + L2X0_PREFETCH_CTRL);
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001092}
1093
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001094static void tauros3_resume(void)
1095{
Russell King09a5d182014-03-15 16:48:13 +00001096 void __iomem *base = l2x0_base;
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001097
Russell King09a5d182014-03-15 16:48:13 +00001098 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
1099 writel_relaxed(l2x0_saved_regs.aux2_ctrl,
1100 base + TAUROS3_AUX2_CTRL);
1101 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
1102 base + L2X0_PREFETCH_CTRL);
1103
1104 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
1105 }
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001106}
1107
Russell Kingc02642b2014-03-15 16:47:54 +00001108static const struct l2c_init_data of_tauros3_data __initconst = {
Russell King3b8bad52014-03-15 16:47:57 +00001109 .num_lock = 8,
1110 .enable = l2c_enable,
Sebastian Hesselbarthe68f31f2013-12-13 16:42:19 +01001111 .save = tauros3_save,
1112 /* Tauros3 broadcasts L1 cache operations to L2 */
1113 .outer_cache = {
1114 .resume = tauros3_resume,
1115 },
1116};
1117
Russell Kinga65bb922014-03-15 16:48:01 +00001118#define L2C_ID(name, fns) { .compatible = name, .data = (void *)&fns }
Rob Herring8c369262011-08-03 18:12:05 +01001119static const struct of_device_id l2x0_ids[] __initconst = {
Russell Kingc02642b2014-03-15 16:47:54 +00001120 L2C_ID("arm,l210-cache", of_l2x0_data),
1121 L2C_ID("arm,l220-cache", of_l2x0_data),
1122 L2C_ID("arm,pl310-cache", of_pl310_data),
1123 L2C_ID("brcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
1124 L2C_ID("marvell,aurora-outer-cache", of_aurora_with_outer_data),
1125 L2C_ID("marvell,aurora-system-cache", of_aurora_no_outer_data),
1126 L2C_ID("marvell,tauros3-cache", of_tauros3_data),
Russell Kinga65bb922014-03-15 16:48:01 +00001127 /* Deprecated IDs */
Russell Kingc02642b2014-03-15 16:47:54 +00001128 L2C_ID("bcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
Rob Herring8c369262011-08-03 18:12:05 +01001129 {}
1130};
1131
Russell King3e175ca2011-09-18 11:27:30 +01001132int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
Rob Herring8c369262011-08-03 18:12:05 +01001133{
Russell Kingc02642b2014-03-15 16:47:54 +00001134 const struct l2c_init_data *data;
Rob Herring8c369262011-08-03 18:12:05 +01001135 struct device_node *np;
Barry Song91c2ebb2011-09-30 14:43:12 +01001136 struct resource res;
Russell King96054b02014-03-15 16:47:52 +00001137 u32 cache_id;
Rob Herring8c369262011-08-03 18:12:05 +01001138
1139 np = of_find_matching_node(NULL, l2x0_ids);
1140 if (!np)
1141 return -ENODEV;
Barry Song91c2ebb2011-09-30 14:43:12 +01001142
1143 if (of_address_to_resource(np, 0, &res))
1144 return -ENODEV;
1145
1146 l2x0_base = ioremap(res.start, resource_size(&res));
Rob Herring8c369262011-08-03 18:12:05 +01001147 if (!l2x0_base)
1148 return -ENOMEM;
1149
Barry Song91c2ebb2011-09-30 14:43:12 +01001150 l2x0_saved_regs.phy_base = res.start;
1151
1152 data = of_match_node(l2x0_ids, np)->data;
1153
Rob Herring8c369262011-08-03 18:12:05 +01001154 /* L2 configuration can only be changed if the cache is disabled */
Russell King40266d62014-03-15 16:47:59 +00001155 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
Russell Kingc02642b2014-03-15 16:47:54 +00001156 if (data->of_parse)
1157 data->of_parse(np, &aux_val, &aux_mask);
Gregory CLEMENTb8db6b82012-11-06 01:58:07 +01001158
Russell King96054b02014-03-15 16:47:52 +00001159 if (cache_id_part_number_from_dt)
1160 cache_id = cache_id_part_number_from_dt;
1161 else
1162 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
1163
1164 __l2c_init(data, aux_val, aux_mask, cache_id);
Gregory CLEMENT6248d062012-10-01 10:56:42 +01001165
Rob Herring8c369262011-08-03 18:12:05 +01001166 return 0;
1167}
1168#endif