cache-l2x0.c 8.4 KB

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