blob: 1a14d18e57139551149a8a315bee833f241adc3b [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);
Catalin Marinas382266a2007-02-05 14:48:19 +010030
Russell King3d107432009-11-19 11:41:09 +000031static inline void cache_wait(void __iomem *reg, unsigned long mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010032{
Catalin Marinas382266a2007-02-05 14:48:19 +010033 /* wait for the operation to complete */
Russell King3d107432009-11-19 11:41:09 +000034 while (readl(reg) & mask)
Catalin Marinas382266a2007-02-05 14:48:19 +010035 ;
Catalin Marinas382266a2007-02-05 14:48:19 +010036}
37
38static inline void cache_sync(void)
39{
Russell King3d107432009-11-19 11:41:09 +000040 void __iomem *base = l2x0_base;
41 writel(0, base + L2X0_CACHE_SYNC);
42 cache_wait(base + L2X0_CACHE_SYNC, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +010043}
44
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010045static inline void l2x0_clean_line(unsigned long addr)
46{
47 void __iomem *base = l2x0_base;
48 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
49 writel(addr, base + L2X0_CLEAN_LINE_PA);
50}
51
52static inline void l2x0_inv_line(unsigned long addr)
53{
54 void __iomem *base = l2x0_base;
55 cache_wait(base + L2X0_INV_LINE_PA, 1);
56 writel(addr, base + L2X0_INV_LINE_PA);
57}
58
59static inline void l2x0_flush_line(unsigned long addr)
60{
61 void __iomem *base = l2x0_base;
62 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
63 writel(addr, base + L2X0_CLEAN_INV_LINE_PA);
64}
65
Catalin Marinas382266a2007-02-05 14:48:19 +010066static inline void l2x0_inv_all(void)
67{
Russell King0eb948d2009-11-19 11:12:15 +000068 unsigned long flags;
69
Catalin Marinas382266a2007-02-05 14:48:19 +010070 /* invalidate all ways */
Russell King0eb948d2009-11-19 11:12:15 +000071 spin_lock_irqsave(&l2x0_lock, flags);
Russell King3d107432009-11-19 11:41:09 +000072 writel(0xff, l2x0_base + L2X0_INV_WAY);
73 cache_wait(l2x0_base + L2X0_INV_WAY, 0xff);
Catalin Marinas382266a2007-02-05 14:48:19 +010074 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +000075 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +010076}
77
78static void l2x0_inv_range(unsigned long start, unsigned long end)
79{
Russell King3d107432009-11-19 11:41:09 +000080 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +000081 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +010082
Russell King0eb948d2009-11-19 11:12:15 +000083 spin_lock_irqsave(&l2x0_lock, flags);
Rui Sousa4f6627a2007-09-15 00:56:19 +010084 if (start & (CACHE_LINE_SIZE - 1)) {
85 start &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010086 l2x0_flush_line(start);
Rui Sousa4f6627a2007-09-15 00:56:19 +010087 start += CACHE_LINE_SIZE;
88 }
89
90 if (end & (CACHE_LINE_SIZE - 1)) {
91 end &= ~(CACHE_LINE_SIZE - 1);
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010092 l2x0_flush_line(end);
Rui Sousa4f6627a2007-09-15 00:56:19 +010093 }
94
Russell King0eb948d2009-11-19 11:12:15 +000095 while (start < end) {
96 unsigned long blk_end = start + min(end - start, 4096UL);
97
98 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +010099 l2x0_inv_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000100 start += CACHE_LINE_SIZE;
101 }
102
103 if (blk_end < end) {
104 spin_unlock_irqrestore(&l2x0_lock, flags);
105 spin_lock_irqsave(&l2x0_lock, flags);
106 }
107 }
Russell King3d107432009-11-19 11:41:09 +0000108 cache_wait(base + L2X0_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100109 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000110 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100111}
112
113static void l2x0_clean_range(unsigned long start, unsigned long end)
114{
Russell King3d107432009-11-19 11:41:09 +0000115 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000116 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100117
Russell King0eb948d2009-11-19 11:12:15 +0000118 spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100119 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000120 while (start < end) {
121 unsigned long blk_end = start + min(end - start, 4096UL);
122
123 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100124 l2x0_clean_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000125 start += CACHE_LINE_SIZE;
126 }
127
128 if (blk_end < end) {
129 spin_unlock_irqrestore(&l2x0_lock, flags);
130 spin_lock_irqsave(&l2x0_lock, flags);
131 }
132 }
Russell King3d107432009-11-19 11:41:09 +0000133 cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100134 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000135 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100136}
137
138static void l2x0_flush_range(unsigned long start, unsigned long end)
139{
Russell King3d107432009-11-19 11:41:09 +0000140 void __iomem *base = l2x0_base;
Russell King0eb948d2009-11-19 11:12:15 +0000141 unsigned long flags;
Catalin Marinas382266a2007-02-05 14:48:19 +0100142
Russell King0eb948d2009-11-19 11:12:15 +0000143 spin_lock_irqsave(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100144 start &= ~(CACHE_LINE_SIZE - 1);
Russell King0eb948d2009-11-19 11:12:15 +0000145 while (start < end) {
146 unsigned long blk_end = start + min(end - start, 4096UL);
147
148 while (start < blk_end) {
Santosh Shilimkar424d6b12010-02-04 19:35:06 +0100149 l2x0_flush_line(start);
Russell King0eb948d2009-11-19 11:12:15 +0000150 start += CACHE_LINE_SIZE;
151 }
152
153 if (blk_end < end) {
154 spin_unlock_irqrestore(&l2x0_lock, flags);
155 spin_lock_irqsave(&l2x0_lock, flags);
156 }
157 }
Russell King3d107432009-11-19 11:41:09 +0000158 cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
Catalin Marinas382266a2007-02-05 14:48:19 +0100159 cache_sync();
Russell King0eb948d2009-11-19 11:12:15 +0000160 spin_unlock_irqrestore(&l2x0_lock, flags);
Catalin Marinas382266a2007-02-05 14:48:19 +0100161}
162
163void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
164{
165 __u32 aux;
166
167 l2x0_base = base;
168
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100169 /*
170 * Check if l2x0 controller is already enabled.
171 * If you are booting from non-secure mode
172 * accessing the below registers will fault.
173 */
174 if (!(readl(l2x0_base + L2X0_CTRL) & 1)) {
Catalin Marinas382266a2007-02-05 14:48:19 +0100175
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100176 /* l2x0 controller is disabled */
Catalin Marinas382266a2007-02-05 14:48:19 +0100177
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100178 aux = readl(l2x0_base + L2X0_AUX_CTRL);
179 aux &= aux_mask;
180 aux |= aux_val;
181 writel(aux, l2x0_base + L2X0_AUX_CTRL);
Catalin Marinas382266a2007-02-05 14:48:19 +0100182
Srinidhi Kasagar48371cd2009-12-02 06:18:03 +0100183 l2x0_inv_all();
184
185 /* enable L2X0 */
186 writel(1, l2x0_base + L2X0_CTRL);
187 }
Catalin Marinas382266a2007-02-05 14:48:19 +0100188
189 outer_cache.inv_range = l2x0_inv_range;
190 outer_cache.clean_range = l2x0_clean_range;
191 outer_cache.flush_range = l2x0_flush_range;
192
193 printk(KERN_INFO "L2X0 cache controller enabled\n");
194}