| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 1 | /* | 
|  | 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 Marinas | 0762097 | 2007-07-20 11:42:40 +0100 | [diff] [blame] | 20 | #include <linux/spinlock.h> | 
| Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 21 | #include <linux/io.h> | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 22 |  | 
|  | 23 | #include <asm/cacheflush.h> | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 24 | #include <asm/hardware/cache-l2x0.h> | 
|  | 25 |  | 
|  | 26 | #define CACHE_LINE_SIZE		32 | 
|  | 27 |  | 
|  | 28 | static void __iomem *l2x0_base; | 
| Catalin Marinas | 0762097 | 2007-07-20 11:42:40 +0100 | [diff] [blame] | 29 | static DEFINE_SPINLOCK(l2x0_lock); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 30 |  | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 31 | static inline void cache_wait(void __iomem *reg, unsigned long mask) | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 32 | { | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 33 | /* wait for the operation to complete */ | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 34 | while (readl(reg) & mask) | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 35 | ; | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
|  | 38 | static inline void cache_sync(void) | 
|  | 39 | { | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 40 | void __iomem *base = l2x0_base; | 
|  | 41 | writel(0, base + L2X0_CACHE_SYNC); | 
|  | 42 | cache_wait(base + L2X0_CACHE_SYNC, 1); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 45 | static 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 |  | 
|  | 52 | static 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 |  | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 59 | #ifdef CONFIG_PL310_ERRATA_588369 | 
|  | 60 | static void debug_writel(unsigned long val) | 
|  | 61 | { | 
|  | 62 | extern void omap_smc1(u32 fn, u32 arg); | 
|  | 63 |  | 
|  | 64 | /* | 
|  | 65 | * Texas Instrument secure monitor api to modify the | 
|  | 66 | * PL310 Debug Control Register. | 
|  | 67 | */ | 
|  | 68 | omap_smc1(0x100, val); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | static inline void l2x0_flush_line(unsigned long addr) | 
|  | 72 | { | 
|  | 73 | void __iomem *base = l2x0_base; | 
|  | 74 |  | 
|  | 75 | /* Clean by PA followed by Invalidate by PA */ | 
|  | 76 | cache_wait(base + L2X0_CLEAN_LINE_PA, 1); | 
|  | 77 | writel(addr, base + L2X0_CLEAN_LINE_PA); | 
|  | 78 | cache_wait(base + L2X0_INV_LINE_PA, 1); | 
|  | 79 | writel(addr, base + L2X0_INV_LINE_PA); | 
|  | 80 | } | 
|  | 81 | #else | 
|  | 82 |  | 
|  | 83 | /* Optimised out for non-errata case */ | 
|  | 84 | static inline void debug_writel(unsigned long val) | 
|  | 85 | { | 
|  | 86 | } | 
|  | 87 |  | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 88 | static inline void l2x0_flush_line(unsigned long addr) | 
|  | 89 | { | 
|  | 90 | void __iomem *base = l2x0_base; | 
|  | 91 | cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1); | 
|  | 92 | writel(addr, base + L2X0_CLEAN_INV_LINE_PA); | 
|  | 93 | } | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 94 | #endif | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 95 |  | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 96 | static inline void l2x0_inv_all(void) | 
|  | 97 | { | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 98 | unsigned long flags; | 
|  | 99 |  | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 100 | /* invalidate all ways */ | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 101 | spin_lock_irqsave(&l2x0_lock, flags); | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 102 | writel(0xff, l2x0_base + L2X0_INV_WAY); | 
|  | 103 | cache_wait(l2x0_base + L2X0_INV_WAY, 0xff); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 104 | cache_sync(); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 105 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
|  | 108 | static void l2x0_inv_range(unsigned long start, unsigned long end) | 
|  | 109 | { | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 110 | void __iomem *base = l2x0_base; | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 111 | unsigned long flags; | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 112 |  | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 113 | spin_lock_irqsave(&l2x0_lock, flags); | 
| Rui Sousa | 4f6627a | 2007-09-15 00:56:19 +0100 | [diff] [blame] | 114 | if (start & (CACHE_LINE_SIZE - 1)) { | 
|  | 115 | start &= ~(CACHE_LINE_SIZE - 1); | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 116 | debug_writel(0x03); | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 117 | l2x0_flush_line(start); | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 118 | debug_writel(0x00); | 
| Rui Sousa | 4f6627a | 2007-09-15 00:56:19 +0100 | [diff] [blame] | 119 | start += CACHE_LINE_SIZE; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | if (end & (CACHE_LINE_SIZE - 1)) { | 
|  | 123 | end &= ~(CACHE_LINE_SIZE - 1); | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 124 | debug_writel(0x03); | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 125 | l2x0_flush_line(end); | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 126 | debug_writel(0x00); | 
| Rui Sousa | 4f6627a | 2007-09-15 00:56:19 +0100 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 129 | while (start < end) { | 
|  | 130 | unsigned long blk_end = start + min(end - start, 4096UL); | 
|  | 131 |  | 
|  | 132 | while (start < blk_end) { | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 133 | l2x0_inv_line(start); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 134 | start += CACHE_LINE_SIZE; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | if (blk_end < end) { | 
|  | 138 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
|  | 139 | spin_lock_irqsave(&l2x0_lock, flags); | 
|  | 140 | } | 
|  | 141 | } | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 142 | cache_wait(base + L2X0_INV_LINE_PA, 1); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 143 | cache_sync(); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 144 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | static void l2x0_clean_range(unsigned long start, unsigned long end) | 
|  | 148 | { | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 149 | void __iomem *base = l2x0_base; | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 150 | unsigned long flags; | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 151 |  | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 152 | spin_lock_irqsave(&l2x0_lock, flags); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 153 | start &= ~(CACHE_LINE_SIZE - 1); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 154 | while (start < end) { | 
|  | 155 | unsigned long blk_end = start + min(end - start, 4096UL); | 
|  | 156 |  | 
|  | 157 | while (start < blk_end) { | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 158 | l2x0_clean_line(start); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 159 | start += CACHE_LINE_SIZE; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | if (blk_end < end) { | 
|  | 163 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
|  | 164 | spin_lock_irqsave(&l2x0_lock, flags); | 
|  | 165 | } | 
|  | 166 | } | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 167 | cache_wait(base + L2X0_CLEAN_LINE_PA, 1); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 168 | cache_sync(); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 169 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | static void l2x0_flush_range(unsigned long start, unsigned long end) | 
|  | 173 | { | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 174 | void __iomem *base = l2x0_base; | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 175 | unsigned long flags; | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 176 |  | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 177 | spin_lock_irqsave(&l2x0_lock, flags); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 178 | start &= ~(CACHE_LINE_SIZE - 1); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 179 | while (start < end) { | 
|  | 180 | unsigned long blk_end = start + min(end - start, 4096UL); | 
|  | 181 |  | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 182 | debug_writel(0x03); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 183 | while (start < blk_end) { | 
| Santosh Shilimkar | 424d6b1 | 2010-02-04 19:35:06 +0100 | [diff] [blame] | 184 | l2x0_flush_line(start); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 185 | start += CACHE_LINE_SIZE; | 
|  | 186 | } | 
| Santosh Shilimkar | 9e65582 | 2010-02-04 19:42:42 +0100 | [diff] [blame] | 187 | debug_writel(0x00); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 188 |  | 
|  | 189 | if (blk_end < end) { | 
|  | 190 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
|  | 191 | spin_lock_irqsave(&l2x0_lock, flags); | 
|  | 192 | } | 
|  | 193 | } | 
| Russell King | 3d10743 | 2009-11-19 11:41:09 +0000 | [diff] [blame] | 194 | cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 195 | cache_sync(); | 
| Russell King | 0eb948d | 2009-11-19 11:12:15 +0000 | [diff] [blame] | 196 | spin_unlock_irqrestore(&l2x0_lock, flags); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
|  | 199 | void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask) | 
|  | 200 | { | 
|  | 201 | __u32 aux; | 
|  | 202 |  | 
|  | 203 | l2x0_base = base; | 
|  | 204 |  | 
| Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 205 | /* | 
|  | 206 | * Check if l2x0 controller is already enabled. | 
|  | 207 | * If you are booting from non-secure mode | 
|  | 208 | * accessing the below registers will fault. | 
|  | 209 | */ | 
|  | 210 | if (!(readl(l2x0_base + L2X0_CTRL) & 1)) { | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 211 |  | 
| Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 212 | /* l2x0 controller is disabled */ | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 213 |  | 
| Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 214 | aux = readl(l2x0_base + L2X0_AUX_CTRL); | 
|  | 215 | aux &= aux_mask; | 
|  | 216 | aux |= aux_val; | 
|  | 217 | writel(aux, l2x0_base + L2X0_AUX_CTRL); | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 218 |  | 
| Srinidhi Kasagar | 48371cd | 2009-12-02 06:18:03 +0100 | [diff] [blame] | 219 | l2x0_inv_all(); | 
|  | 220 |  | 
|  | 221 | /* enable L2X0 */ | 
|  | 222 | writel(1, l2x0_base + L2X0_CTRL); | 
|  | 223 | } | 
| Catalin Marinas | 382266a | 2007-02-05 14:48:19 +0100 | [diff] [blame] | 224 |  | 
|  | 225 | outer_cache.inv_range = l2x0_inv_range; | 
|  | 226 | outer_cache.clean_range = l2x0_clean_range; | 
|  | 227 | outer_cache.flush_range = l2x0_flush_range; | 
|  | 228 |  | 
|  | 229 | printk(KERN_INFO "L2X0 cache controller enabled\n"); | 
|  | 230 | } |