cache-l2x0.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 inline void cache_wait(void __iomem *reg, unsigned long mask)
  29. {
  30. /* wait for the operation to complete */
  31. while (readl_relaxed(reg) & mask)
  32. ;
  33. }
  34. static inline void cache_sync(void)
  35. {
  36. void __iomem *base = l2x0_base;
  37. writel_relaxed(0, base + L2X0_CACHE_SYNC);
  38. cache_wait(base + L2X0_CACHE_SYNC, 1);
  39. }
  40. static inline void l2x0_clean_line(unsigned long addr)
  41. {
  42. void __iomem *base = l2x0_base;
  43. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  44. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  45. }
  46. static inline void l2x0_inv_line(unsigned long addr)
  47. {
  48. void __iomem *base = l2x0_base;
  49. cache_wait(base + L2X0_INV_LINE_PA, 1);
  50. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  51. }
  52. #ifdef CONFIG_PL310_ERRATA_588369
  53. static void debug_writel(unsigned long val)
  54. {
  55. extern void omap_smc1(u32 fn, u32 arg);
  56. /*
  57. * Texas Instrument secure monitor api to modify the
  58. * PL310 Debug Control Register.
  59. */
  60. omap_smc1(0x100, val);
  61. }
  62. static inline void l2x0_flush_line(unsigned long addr)
  63. {
  64. void __iomem *base = l2x0_base;
  65. /* Clean by PA followed by Invalidate by PA */
  66. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  67. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  68. cache_wait(base + L2X0_INV_LINE_PA, 1);
  69. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  70. }
  71. #else
  72. /* Optimised out for non-errata case */
  73. static inline void debug_writel(unsigned long val)
  74. {
  75. }
  76. static inline void l2x0_flush_line(unsigned long addr)
  77. {
  78. void __iomem *base = l2x0_base;
  79. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  80. writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
  81. }
  82. #endif
  83. static void l2x0_cache_sync(void)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(&l2x0_lock, flags);
  87. cache_sync();
  88. spin_unlock_irqrestore(&l2x0_lock, flags);
  89. }
  90. static inline void l2x0_inv_all(void)
  91. {
  92. unsigned long flags;
  93. /* invalidate all ways */
  94. spin_lock_irqsave(&l2x0_lock, flags);
  95. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
  96. cache_wait(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
  97. cache_sync();
  98. spin_unlock_irqrestore(&l2x0_lock, flags);
  99. }
  100. static void l2x0_inv_range(unsigned long start, unsigned long end)
  101. {
  102. void __iomem *base = l2x0_base;
  103. unsigned long flags;
  104. spin_lock_irqsave(&l2x0_lock, flags);
  105. if (start & (CACHE_LINE_SIZE - 1)) {
  106. start &= ~(CACHE_LINE_SIZE - 1);
  107. debug_writel(0x03);
  108. l2x0_flush_line(start);
  109. debug_writel(0x00);
  110. start += CACHE_LINE_SIZE;
  111. }
  112. if (end & (CACHE_LINE_SIZE - 1)) {
  113. end &= ~(CACHE_LINE_SIZE - 1);
  114. debug_writel(0x03);
  115. l2x0_flush_line(end);
  116. debug_writel(0x00);
  117. }
  118. while (start < end) {
  119. unsigned long blk_end = start + min(end - start, 4096UL);
  120. while (start < blk_end) {
  121. l2x0_inv_line(start);
  122. start += CACHE_LINE_SIZE;
  123. }
  124. if (blk_end < end) {
  125. spin_unlock_irqrestore(&l2x0_lock, flags);
  126. spin_lock_irqsave(&l2x0_lock, flags);
  127. }
  128. }
  129. cache_wait(base + L2X0_INV_LINE_PA, 1);
  130. cache_sync();
  131. spin_unlock_irqrestore(&l2x0_lock, flags);
  132. }
  133. static void l2x0_clean_range(unsigned long start, unsigned long end)
  134. {
  135. void __iomem *base = l2x0_base;
  136. unsigned long flags;
  137. spin_lock_irqsave(&l2x0_lock, flags);
  138. start &= ~(CACHE_LINE_SIZE - 1);
  139. while (start < end) {
  140. unsigned long blk_end = start + min(end - start, 4096UL);
  141. while (start < blk_end) {
  142. l2x0_clean_line(start);
  143. start += CACHE_LINE_SIZE;
  144. }
  145. if (blk_end < end) {
  146. spin_unlock_irqrestore(&l2x0_lock, flags);
  147. spin_lock_irqsave(&l2x0_lock, flags);
  148. }
  149. }
  150. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  151. cache_sync();
  152. spin_unlock_irqrestore(&l2x0_lock, flags);
  153. }
  154. static void l2x0_flush_range(unsigned long start, unsigned long end)
  155. {
  156. void __iomem *base = l2x0_base;
  157. unsigned long flags;
  158. spin_lock_irqsave(&l2x0_lock, flags);
  159. start &= ~(CACHE_LINE_SIZE - 1);
  160. while (start < end) {
  161. unsigned long blk_end = start + min(end - start, 4096UL);
  162. debug_writel(0x03);
  163. while (start < blk_end) {
  164. l2x0_flush_line(start);
  165. start += CACHE_LINE_SIZE;
  166. }
  167. debug_writel(0x00);
  168. if (blk_end < end) {
  169. spin_unlock_irqrestore(&l2x0_lock, flags);
  170. spin_lock_irqsave(&l2x0_lock, flags);
  171. }
  172. }
  173. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  174. cache_sync();
  175. spin_unlock_irqrestore(&l2x0_lock, flags);
  176. }
  177. void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
  178. {
  179. __u32 aux;
  180. __u32 cache_id;
  181. int ways;
  182. const char *type;
  183. l2x0_base = base;
  184. cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
  185. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  186. aux &= aux_mask;
  187. aux |= aux_val;
  188. /* Determine the number of ways */
  189. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  190. case L2X0_CACHE_ID_PART_L310:
  191. if (aux & (1 << 16))
  192. ways = 16;
  193. else
  194. ways = 8;
  195. type = "L310";
  196. break;
  197. case L2X0_CACHE_ID_PART_L210:
  198. ways = (aux >> 13) & 0xf;
  199. type = "L210";
  200. break;
  201. default:
  202. /* Assume unknown chips have 8 ways */
  203. ways = 8;
  204. type = "L2x0 series";
  205. break;
  206. }
  207. l2x0_way_mask = (1 << ways) - 1;
  208. /*
  209. * Check if l2x0 controller is already enabled.
  210. * If you are booting from non-secure mode
  211. * accessing the below registers will fault.
  212. */
  213. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & 1)) {
  214. /* l2x0 controller is disabled */
  215. writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
  216. l2x0_inv_all();
  217. /* enable L2X0 */
  218. writel_relaxed(1, l2x0_base + L2X0_CTRL);
  219. }
  220. outer_cache.inv_range = l2x0_inv_range;
  221. outer_cache.clean_range = l2x0_clean_range;
  222. outer_cache.flush_range = l2x0_flush_range;
  223. outer_cache.sync = l2x0_cache_sync;
  224. printk(KERN_INFO "%s cache controller enabled\n", type);
  225. printk(KERN_INFO "l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
  226. ways, cache_id, aux);
  227. }