cache-l2x0.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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>
  20. #include <linux/spinlock.h>
  21. #include <linux/io.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/hardware/cache-l2x0.h>
  24. #define CACHE_LINE_SIZE 32
  25. static void __iomem *l2x0_base;
  26. static DEFINE_SPINLOCK(l2x0_lock);
  27. static uint32_t l2x0_way_mask; /* Bitmask of active ways */
  28. static uint32_t l2x0_size;
  29. static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
  30. {
  31. /* wait for cache operation by line or way to complete */
  32. while (readl_relaxed(reg) & mask)
  33. ;
  34. }
  35. #ifdef CONFIG_CACHE_PL310
  36. static inline void cache_wait(void __iomem *reg, unsigned long mask)
  37. {
  38. /* cache operations by line are atomic on PL310 */
  39. }
  40. #else
  41. #define cache_wait cache_wait_way
  42. #endif
  43. static inline void cache_sync(void)
  44. {
  45. void __iomem *base = l2x0_base;
  46. #ifdef CONFIG_ARM_ERRATA_753970
  47. /* write to an unmmapped register */
  48. writel_relaxed(0, base + L2X0_DUMMY_REG);
  49. #else
  50. writel_relaxed(0, base + L2X0_CACHE_SYNC);
  51. #endif
  52. cache_wait(base + L2X0_CACHE_SYNC, 1);
  53. }
  54. static inline void l2x0_clean_line(unsigned long addr)
  55. {
  56. void __iomem *base = l2x0_base;
  57. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  58. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  59. }
  60. static inline void l2x0_inv_line(unsigned long addr)
  61. {
  62. void __iomem *base = l2x0_base;
  63. cache_wait(base + L2X0_INV_LINE_PA, 1);
  64. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  65. }
  66. #ifdef CONFIG_PL310_ERRATA_588369
  67. static void debug_writel(unsigned long val)
  68. {
  69. extern void omap_smc1(u32 fn, u32 arg);
  70. /*
  71. * Texas Instrument secure monitor api to modify the
  72. * PL310 Debug Control Register.
  73. */
  74. omap_smc1(0x100, val);
  75. }
  76. static inline void l2x0_flush_line(unsigned long addr)
  77. {
  78. void __iomem *base = l2x0_base;
  79. /* Clean by PA followed by Invalidate by PA */
  80. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  81. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  82. cache_wait(base + L2X0_INV_LINE_PA, 1);
  83. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  84. }
  85. #else
  86. /* Optimised out for non-errata case */
  87. static inline void debug_writel(unsigned long val)
  88. {
  89. }
  90. static inline void l2x0_flush_line(unsigned long addr)
  91. {
  92. void __iomem *base = l2x0_base;
  93. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  94. writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
  95. }
  96. #endif
  97. static void l2x0_cache_sync(void)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&l2x0_lock, flags);
  101. cache_sync();
  102. spin_unlock_irqrestore(&l2x0_lock, flags);
  103. }
  104. static void l2x0_flush_all(void)
  105. {
  106. unsigned long flags;
  107. /* clean all ways */
  108. spin_lock_irqsave(&l2x0_lock, flags);
  109. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
  110. cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
  111. cache_sync();
  112. spin_unlock_irqrestore(&l2x0_lock, flags);
  113. }
  114. static void l2x0_clean_all(void)
  115. {
  116. unsigned long flags;
  117. /* clean all ways */
  118. spin_lock_irqsave(&l2x0_lock, flags);
  119. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
  120. cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
  121. cache_sync();
  122. spin_unlock_irqrestore(&l2x0_lock, flags);
  123. }
  124. static void l2x0_inv_all(void)
  125. {
  126. unsigned long flags;
  127. /* invalidate all ways */
  128. spin_lock_irqsave(&l2x0_lock, flags);
  129. /* Invalidating when L2 is enabled is a nono */
  130. BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
  131. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
  132. cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
  133. cache_sync();
  134. spin_unlock_irqrestore(&l2x0_lock, flags);
  135. }
  136. static void l2x0_inv_range(unsigned long start, unsigned long end)
  137. {
  138. void __iomem *base = l2x0_base;
  139. unsigned long flags;
  140. spin_lock_irqsave(&l2x0_lock, flags);
  141. if (start & (CACHE_LINE_SIZE - 1)) {
  142. start &= ~(CACHE_LINE_SIZE - 1);
  143. debug_writel(0x03);
  144. l2x0_flush_line(start);
  145. debug_writel(0x00);
  146. start += CACHE_LINE_SIZE;
  147. }
  148. if (end & (CACHE_LINE_SIZE - 1)) {
  149. end &= ~(CACHE_LINE_SIZE - 1);
  150. debug_writel(0x03);
  151. l2x0_flush_line(end);
  152. debug_writel(0x00);
  153. }
  154. while (start < end) {
  155. unsigned long blk_end = start + min(end - start, 4096UL);
  156. while (start < blk_end) {
  157. l2x0_inv_line(start);
  158. start += CACHE_LINE_SIZE;
  159. }
  160. if (blk_end < end) {
  161. spin_unlock_irqrestore(&l2x0_lock, flags);
  162. spin_lock_irqsave(&l2x0_lock, flags);
  163. }
  164. }
  165. cache_wait(base + L2X0_INV_LINE_PA, 1);
  166. cache_sync();
  167. spin_unlock_irqrestore(&l2x0_lock, flags);
  168. }
  169. static void l2x0_clean_range(unsigned long start, unsigned long end)
  170. {
  171. void __iomem *base = l2x0_base;
  172. unsigned long flags;
  173. if ((end - start) >= l2x0_size) {
  174. l2x0_clean_all();
  175. return;
  176. }
  177. spin_lock_irqsave(&l2x0_lock, flags);
  178. start &= ~(CACHE_LINE_SIZE - 1);
  179. while (start < end) {
  180. unsigned long blk_end = start + min(end - start, 4096UL);
  181. while (start < blk_end) {
  182. l2x0_clean_line(start);
  183. start += CACHE_LINE_SIZE;
  184. }
  185. if (blk_end < end) {
  186. spin_unlock_irqrestore(&l2x0_lock, flags);
  187. spin_lock_irqsave(&l2x0_lock, flags);
  188. }
  189. }
  190. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  191. cache_sync();
  192. spin_unlock_irqrestore(&l2x0_lock, flags);
  193. }
  194. static void l2x0_flush_range(unsigned long start, unsigned long end)
  195. {
  196. void __iomem *base = l2x0_base;
  197. unsigned long flags;
  198. if ((end - start) >= l2x0_size) {
  199. l2x0_flush_all();
  200. return;
  201. }
  202. spin_lock_irqsave(&l2x0_lock, flags);
  203. start &= ~(CACHE_LINE_SIZE - 1);
  204. while (start < end) {
  205. unsigned long blk_end = start + min(end - start, 4096UL);
  206. debug_writel(0x03);
  207. while (start < blk_end) {
  208. l2x0_flush_line(start);
  209. start += CACHE_LINE_SIZE;
  210. }
  211. debug_writel(0x00);
  212. if (blk_end < end) {
  213. spin_unlock_irqrestore(&l2x0_lock, flags);
  214. spin_lock_irqsave(&l2x0_lock, flags);
  215. }
  216. }
  217. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  218. cache_sync();
  219. spin_unlock_irqrestore(&l2x0_lock, flags);
  220. }
  221. static void l2x0_disable(void)
  222. {
  223. unsigned long flags;
  224. spin_lock_irqsave(&l2x0_lock, flags);
  225. writel(0, l2x0_base + L2X0_CTRL);
  226. spin_unlock_irqrestore(&l2x0_lock, flags);
  227. }
  228. void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
  229. {
  230. __u32 aux;
  231. __u32 cache_id;
  232. __u32 way_size = 0;
  233. int ways;
  234. const char *type;
  235. l2x0_base = base;
  236. cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
  237. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  238. aux &= aux_mask;
  239. aux |= aux_val;
  240. /* Determine the number of ways */
  241. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  242. case L2X0_CACHE_ID_PART_L310:
  243. if (aux & (1 << 16))
  244. ways = 16;
  245. else
  246. ways = 8;
  247. type = "L310";
  248. break;
  249. case L2X0_CACHE_ID_PART_L210:
  250. ways = (aux >> 13) & 0xf;
  251. type = "L210";
  252. break;
  253. default:
  254. /* Assume unknown chips have 8 ways */
  255. ways = 8;
  256. type = "L2x0 series";
  257. break;
  258. }
  259. l2x0_way_mask = (1 << ways) - 1;
  260. /*
  261. * L2 cache Size = Way size * Number of ways
  262. */
  263. way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
  264. way_size = 1 << (way_size + 3);
  265. l2x0_size = ways * way_size * SZ_1K;
  266. /*
  267. * Check if l2x0 controller is already enabled.
  268. * If you are booting from non-secure mode
  269. * accessing the below registers will fault.
  270. */
  271. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
  272. /* l2x0 controller is disabled */
  273. writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
  274. l2x0_inv_all();
  275. /* enable L2X0 */
  276. writel_relaxed(1, l2x0_base + L2X0_CTRL);
  277. }
  278. outer_cache.inv_range = l2x0_inv_range;
  279. outer_cache.clean_range = l2x0_clean_range;
  280. outer_cache.flush_range = l2x0_flush_range;
  281. outer_cache.sync = l2x0_cache_sync;
  282. outer_cache.flush_all = l2x0_flush_all;
  283. outer_cache.inv_all = l2x0_inv_all;
  284. outer_cache.disable = l2x0_disable;
  285. printk(KERN_INFO "%s cache controller enabled\n", type);
  286. printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n",
  287. ways, cache_id, aux, l2x0_size);
  288. }