blob: 9819869d2bc90954967a5a1ca0e636c883c09e2f [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 */
19#include <linux/init.h>
Catalin Marinas07620972007-07-20 11:42:40 +010020#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010022
23#include <asm/cacheflush.h>
Catalin Marinas382266a2007-02-05 14:48:19 +010024#include <asm/hardware/cache-l2x0.h>
25
26#define CACHE_LINE_SIZE 32
27
28static void __iomem *l2x0_base;
Catalin Marinas07620972007-07-20 11:42:40 +010029static DEFINE_SPINLOCK(l2x0_lock);
Jason McMullan64039be2010-05-05 18:59:37 +010030static uint32_t l2x0_way_mask; /* Bitmask of active ways */
Catalin Marinas382266a2007-02-05 14:48:19 +010031
Russell King3d107432009-11-19 11:41:09 +000032static inline void cache_wait(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010033{
Catalin Marinas382266a2007-02-05 14:48:19 +010034 /* wait for the operation to complete */
Russell King3d107432009-11-19 11:41:09 +000035 while (readl(reg) & mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010036 ;
Catalin Marinas382266a2007-02-05 14:48:19 +010037}
38
39static inline void cache_sync(void)
40{
Russell King3d107432009-11-19 11:41:09 +000041 void __iomem *base = l2x0_base;
42 writel(0, base + L2X0_CACHE_SYNC);
43 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010044}
45
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010046static inline void l2x0_clean_line(unsigned long addr)
47{
48 void __iomem *base = l2x0_base;
49 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
50 writel(addr, base + L2X0_CLEAN_LINE_PA);
51}
52
53static inline void l2x0_inv_line(unsigned long addr)
54{
55 void __iomem *base = l2x0_base;
56 cache_wait(base + L2X0_INV_LINE_PA, 1);
57 writel(addr, base + L2X0_INV_LINE_PA);
58}
59
Santosh Shilimkar9e655822010-02-04 19:42:42 +010060#ifdef CONFIG_PL310_ERRATA_588369
61static void debug_writel(unsigned long val)
62{
63 extern void omap_smc1(u32 fn, u32 arg);
64
65 /*
66 * Texas Instrument secure monitor api to modify the
67 * PL310 Debug Control Register.
68 */
69 omap_smc1(0x100, val);
70}
71
72static inline void l2x0_flush_line(unsigned long addr)
73{
74 void __iomem *base = l2x0_base;
75
76 /* Clean by PA followed by Invalidate by PA */
77 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
78 writel(addr, base + L2X0_CLEAN_LINE_PA);
79 cache_wait(base + L2X0_INV_LINE_PA, 1);
80 writel(addr, base + L2X0_INV_LINE_PA);
81}
82#else
83
84/* Optimised out for non-errata case */
85static inline void debug_writel(unsigned long val)
86{
87}
88
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010089static inline void l2x0_flush_line(unsigned long addr)
90{
91 void __iomem *base = l2x0_base;
92 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
93 writel(addr, base + L2X0_CLEAN_INV_LINE_PA);
94}
Santosh Shilimkar9e655822010-02-04 19:42:42 +010095#endif
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010096
Catalin Marinas23107c52010-03-24 16:48:53 +010097static void l2x0_cache_sync(void)
98{
99 unsigned long flags;
100
101 spin_lock_irqsave(&l2x0_lock, flags);
102 cache_sync();
103 spin_unlock_irqrestore(&l2x0_lock, flags);
104}
105
Catalin Marinas382266a2007-02-05 14:48:19 +0100106static inline void l2x0_inv_all(void)
107{
Russell King0eb948d2009-11-19 11:12:15 +0000108 unsigned long flags;
109
Catalin Marinas382266a2007-02-05 14:48:19 +0100110 /* invalidate all ways */
Russell King0eb948d2009-11-19 11:12:15 +0000111 spin_lock_irqsave(&l2x0_lock, flags);
Jason McMullan64039be2010-05-05 18:59:37 +0100112 writel(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
113 cache_wait(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
Catalin Marinas382266a2007-02-05 14:48:19 +0100114 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000115 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100116}
117
118static void l2x0_inv_range(unsigned long start, unsigned long end)
119{
Russell King3d107432009-11-19 11:41:09 +0000120 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000121 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100122
Russell King0eb948d2009-11-19 11:12:15 +0000123 spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100124 if (start & (CACHE_LINE_SIZE - 1)) {
125 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100126 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100127 l2x0_flush_line(start);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100128 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100129 start += CACHE_LINE_SIZE;
130 }
131
132 if (end & (CACHE_LINE_SIZE - 1)) {
133 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100134 debug_writel(0x03);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100135 l2x0_flush_line(end);
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100136 debug_writel(0x00);
Rui Sousa4f6627a2007-09-15 00:56:19 +0100137 }
138
Russell King0eb948d2009-11-19 11:12:15 +0000139 while (start < end) {
140 unsigned long blk_end = start + min(end - start, 4096UL);
141
142 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100143 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000144 start += CACHE_LINE_SIZE;
145 }
146
147 if (blk_end < end) {
148 spin_unlock_irqrestore(&l2x0_lock, flags);
149 spin_lock_irqsave(&l2x0_lock, flags);
150 }
151 }
Russell King3d107432009-11-19 11:41:09 +0000152 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100153 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000154 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100155}
156
157static void l2x0_clean_range(unsigned long start, unsigned long end)
158{
Russell King3d107432009-11-19 11:41:09 +0000159 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000160 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100161
Russell King0eb948d2009-11-19 11:12:15 +0000162 spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100163 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000164 while (start < end) {
165 unsigned long blk_end = start + min(end - start, 4096UL);
166
167 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100168 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000169 start += CACHE_LINE_SIZE;
170 }
171
172 if (blk_end < end) {
173 spin_unlock_irqrestore(&l2x0_lock, flags);
174 spin_lock_irqsave(&l2x0_lock, flags);
175 }
176 }
Russell King3d107432009-11-19 11:41:09 +0000177 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100178 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000179 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100180}
181
182static void l2x0_flush_range(unsigned long start, unsigned long end)
183{
Russell King3d107432009-11-19 11:41:09 +0000184 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000185 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100186
Russell King0eb948d2009-11-19 11:12:15 +0000187 spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100188 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000189 while (start < end) {
190 unsigned long blk_end = start + min(end - start, 4096UL);
191
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100192 debug_writel(0x03);
Russell King0eb948d2009-11-19 11:12:15 +0000193 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100194 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000195 start += CACHE_LINE_SIZE;
196 }
Santosh Shilimkar9e655822010-02-04 19:42:42 +0100197 debug_writel(0x00);
Russell King0eb948d2009-11-19 11:12:15 +0000198
199 if (blk_end < end) {
200 spin_unlock_irqrestore(&l2x0_lock, flags);
201 spin_lock_irqsave(&l2x0_lock, flags);
202 }
203 }
Russell King3d107432009-11-19 11:41:09 +0000204 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100205 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000206 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100207}
208
209void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
210{
211 __u32 aux;
Jason McMullan64039be2010-05-05 18:59:37 +0100212 __u32 cache_id;
213 int ways;
214 const char *type;
Catalin Marinas382266a2007-02-05 14:48:19 +0100215
216 l2x0_base = base;
217
Jason McMullan64039be2010-05-05 18:59:37 +0100218 cache_id = readl(l2x0_base + L2X0_CACHE_ID);
219 aux = readl(l2x0_base + L2X0_AUX_CTRL);
220
221 /* Determine the number of ways */
222 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
223 case L2X0_CACHE_ID_PART_L310:
224 if (aux & (1 << 16))
225 ways = 16;
226 else
227 ways = 8;
228 type = "L310";
229 break;
230 case L2X0_CACHE_ID_PART_L210:
231 ways = (aux >> 13) & 0xf;
232 type = "L210";
233 break;
234 default:
235 /* Assume unknown chips have 8 ways */
236 ways = 8;
237 type = "L2x0 series";
238 break;
239 }
240
241 l2x0_way_mask = (1 << ways) - 1;
242
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100243 /*
244 * Check if l2x0 controller is already enabled.
245 * If you are booting from non-secure mode
246 * accessing the below registers will fault.
247 */
248 if (!(readl(l2x0_base + L2X0_CTRL) & 1)) {
Catalin Marinas382266a2007-02-05 14:48:19 +0100249
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100250 /* l2x0 controller is disabled */
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100251 aux &= aux_mask;
252 aux |= aux_val;
253 writel(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100254
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100255 l2x0_inv_all();
256
257 /* enable L2X0 */
258 writel(1, l2x0_base + L2X0_CTRL);
259 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100260
261 outer_cache.inv_range = l2x0_inv_range;
262 outer_cache.clean_range = l2x0_clean_range;
263 outer_cache.flush_range = l2x0_flush_range;
Catalin Marinas23107c52010-03-24 16:48:53 +0100264 outer_cache.sync = l2x0_cache_sync;
Catalin Marinas382266a2007-02-05 14:48:19 +0100265
Jason McMullan64039be2010-05-05 18:59:37 +0100266 printk(KERN_INFO "%s cache controller enabled\n", type);
267 printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
268 ways, cache_id, aux);
Catalin Marinas382266a2007-02-05 14:48:19 +0100269}