cache-l2x0.c 5.7 KB

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