cache-l2x0.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
  67. #define debug_writel(val) outer_cache.set_debug(val)
  68. static void l2x0_set_debug(unsigned long val)
  69. {
  70. writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
  71. }
  72. #else
  73. /* Optimised out for non-errata case */
  74. static inline void debug_writel(unsigned long val)
  75. {
  76. }
  77. #define l2x0_set_debug NULL
  78. #endif
  79. #ifdef CONFIG_PL310_ERRATA_588369
  80. static inline void l2x0_flush_line(unsigned long addr)
  81. {
  82. void __iomem *base = l2x0_base;
  83. /* Clean by PA followed by Invalidate by PA */
  84. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  85. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  86. cache_wait(base + L2X0_INV_LINE_PA, 1);
  87. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  88. }
  89. #else
  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. debug_writel(0x03);
  110. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
  111. cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
  112. cache_sync();
  113. debug_writel(0x00);
  114. spin_unlock_irqrestore(&l2x0_lock, flags);
  115. }
  116. static void l2x0_clean_all(void)
  117. {
  118. unsigned long flags;
  119. /* clean all ways */
  120. spin_lock_irqsave(&l2x0_lock, flags);
  121. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
  122. cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
  123. cache_sync();
  124. spin_unlock_irqrestore(&l2x0_lock, flags);
  125. }
  126. static void l2x0_inv_all(void)
  127. {
  128. unsigned long flags;
  129. /* invalidate all ways */
  130. spin_lock_irqsave(&l2x0_lock, flags);
  131. /* Invalidating when L2 is enabled is a nono */
  132. BUG_ON(readl(l2x0_base + L2X0_CTRL) & 1);
  133. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
  134. cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
  135. cache_sync();
  136. spin_unlock_irqrestore(&l2x0_lock, flags);
  137. }
  138. static void l2x0_inv_range(unsigned long start, unsigned long end)
  139. {
  140. void __iomem *base = l2x0_base;
  141. unsigned long flags;
  142. spin_lock_irqsave(&l2x0_lock, flags);
  143. if (start & (CACHE_LINE_SIZE - 1)) {
  144. start &= ~(CACHE_LINE_SIZE - 1);
  145. debug_writel(0x03);
  146. l2x0_flush_line(start);
  147. debug_writel(0x00);
  148. start += CACHE_LINE_SIZE;
  149. }
  150. if (end & (CACHE_LINE_SIZE - 1)) {
  151. end &= ~(CACHE_LINE_SIZE - 1);
  152. debug_writel(0x03);
  153. l2x0_flush_line(end);
  154. debug_writel(0x00);
  155. }
  156. while (start < end) {
  157. unsigned long blk_end = start + min(end - start, 4096UL);
  158. while (start < blk_end) {
  159. l2x0_inv_line(start);
  160. start += CACHE_LINE_SIZE;
  161. }
  162. if (blk_end < end) {
  163. spin_unlock_irqrestore(&l2x0_lock, flags);
  164. spin_lock_irqsave(&l2x0_lock, flags);
  165. }
  166. }
  167. cache_wait(base + L2X0_INV_LINE_PA, 1);
  168. cache_sync();
  169. spin_unlock_irqrestore(&l2x0_lock, flags);
  170. }
  171. static void l2x0_clean_range(unsigned long start, unsigned long end)
  172. {
  173. void __iomem *base = l2x0_base;
  174. unsigned long flags;
  175. if ((end - start) >= l2x0_size) {
  176. l2x0_clean_all();
  177. return;
  178. }
  179. spin_lock_irqsave(&l2x0_lock, flags);
  180. start &= ~(CACHE_LINE_SIZE - 1);
  181. while (start < end) {
  182. unsigned long blk_end = start + min(end - start, 4096UL);
  183. while (start < blk_end) {
  184. l2x0_clean_line(start);
  185. start += CACHE_LINE_SIZE;
  186. }
  187. if (blk_end < end) {
  188. spin_unlock_irqrestore(&l2x0_lock, flags);
  189. spin_lock_irqsave(&l2x0_lock, flags);
  190. }
  191. }
  192. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  193. cache_sync();
  194. spin_unlock_irqrestore(&l2x0_lock, flags);
  195. }
  196. static void l2x0_flush_range(unsigned long start, unsigned long end)
  197. {
  198. void __iomem *base = l2x0_base;
  199. unsigned long flags;
  200. if ((end - start) >= l2x0_size) {
  201. l2x0_flush_all();
  202. return;
  203. }
  204. spin_lock_irqsave(&l2x0_lock, flags);
  205. start &= ~(CACHE_LINE_SIZE - 1);
  206. while (start < end) {
  207. unsigned long blk_end = start + min(end - start, 4096UL);
  208. debug_writel(0x03);
  209. while (start < blk_end) {
  210. l2x0_flush_line(start);
  211. start += CACHE_LINE_SIZE;
  212. }
  213. debug_writel(0x00);
  214. if (blk_end < end) {
  215. spin_unlock_irqrestore(&l2x0_lock, flags);
  216. spin_lock_irqsave(&l2x0_lock, flags);
  217. }
  218. }
  219. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  220. cache_sync();
  221. spin_unlock_irqrestore(&l2x0_lock, flags);
  222. }
  223. static void l2x0_disable(void)
  224. {
  225. unsigned long flags;
  226. spin_lock_irqsave(&l2x0_lock, flags);
  227. writel(0, l2x0_base + L2X0_CTRL);
  228. spin_unlock_irqrestore(&l2x0_lock, flags);
  229. }
  230. void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
  231. {
  232. __u32 aux;
  233. __u32 cache_id;
  234. __u32 way_size = 0;
  235. int ways;
  236. const char *type;
  237. l2x0_base = base;
  238. cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
  239. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  240. aux &= aux_mask;
  241. aux |= aux_val;
  242. /* Determine the number of ways */
  243. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  244. case L2X0_CACHE_ID_PART_L310:
  245. if (aux & (1 << 16))
  246. ways = 16;
  247. else
  248. ways = 8;
  249. type = "L310";
  250. break;
  251. case L2X0_CACHE_ID_PART_L210:
  252. ways = (aux >> 13) & 0xf;
  253. type = "L210";
  254. break;
  255. default:
  256. /* Assume unknown chips have 8 ways */
  257. ways = 8;
  258. type = "L2x0 series";
  259. break;
  260. }
  261. l2x0_way_mask = (1 << ways) - 1;
  262. /*
  263. * L2 cache Size = Way size * Number of ways
  264. */
  265. way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
  266. way_size = 1 << (way_size + 3);
  267. l2x0_size = ways * way_size * SZ_1K;
  268. /*
  269. * Check if l2x0 controller is already enabled.
  270. * If you are booting from non-secure mode
  271. * accessing the below registers will fault.
  272. */
  273. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
  274. /* l2x0 controller is disabled */
  275. writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
  276. l2x0_inv_all();
  277. /* enable L2X0 */
  278. writel_relaxed(1, l2x0_base + L2X0_CTRL);
  279. }
  280. outer_cache.inv_range = l2x0_inv_range;
  281. outer_cache.clean_range = l2x0_clean_range;
  282. outer_cache.flush_range = l2x0_flush_range;
  283. outer_cache.sync = l2x0_cache_sync;
  284. outer_cache.flush_all = l2x0_flush_all;
  285. outer_cache.inv_all = l2x0_inv_all;
  286. outer_cache.disable = l2x0_disable;
  287. outer_cache.set_debug = l2x0_set_debug;
  288. printk(KERN_INFO "%s cache controller enabled\n", type);
  289. printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d B\n",
  290. ways, cache_id, aux, l2x0_size);
  291. }