cache-l2x0.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 inline void l2x0_inv_all(void)
  83. {
  84. unsigned long flags;
  85. /* invalidate all ways */
  86. spin_lock_irqsave(&l2x0_lock, flags);
  87. writel(0xff, l2x0_base + L2X0_INV_WAY);
  88. cache_wait(l2x0_base + L2X0_INV_WAY, 0xff);
  89. cache_sync();
  90. spin_unlock_irqrestore(&l2x0_lock, flags);
  91. }
  92. static void l2x0_inv_range(unsigned long start, unsigned long end)
  93. {
  94. void __iomem *base = l2x0_base;
  95. unsigned long flags;
  96. spin_lock_irqsave(&l2x0_lock, flags);
  97. if (start & (CACHE_LINE_SIZE - 1)) {
  98. start &= ~(CACHE_LINE_SIZE - 1);
  99. debug_writel(0x03);
  100. l2x0_flush_line(start);
  101. debug_writel(0x00);
  102. start += CACHE_LINE_SIZE;
  103. }
  104. if (end & (CACHE_LINE_SIZE - 1)) {
  105. end &= ~(CACHE_LINE_SIZE - 1);
  106. debug_writel(0x03);
  107. l2x0_flush_line(end);
  108. debug_writel(0x00);
  109. }
  110. while (start < end) {
  111. unsigned long blk_end = start + min(end - start, 4096UL);
  112. while (start < blk_end) {
  113. l2x0_inv_line(start);
  114. start += CACHE_LINE_SIZE;
  115. }
  116. if (blk_end < end) {
  117. spin_unlock_irqrestore(&l2x0_lock, flags);
  118. spin_lock_irqsave(&l2x0_lock, flags);
  119. }
  120. }
  121. cache_wait(base + L2X0_INV_LINE_PA, 1);
  122. cache_sync();
  123. spin_unlock_irqrestore(&l2x0_lock, flags);
  124. }
  125. static void l2x0_clean_range(unsigned long start, unsigned long end)
  126. {
  127. void __iomem *base = l2x0_base;
  128. unsigned long flags;
  129. spin_lock_irqsave(&l2x0_lock, flags);
  130. start &= ~(CACHE_LINE_SIZE - 1);
  131. while (start < end) {
  132. unsigned long blk_end = start + min(end - start, 4096UL);
  133. while (start < blk_end) {
  134. l2x0_clean_line(start);
  135. start += CACHE_LINE_SIZE;
  136. }
  137. if (blk_end < end) {
  138. spin_unlock_irqrestore(&l2x0_lock, flags);
  139. spin_lock_irqsave(&l2x0_lock, flags);
  140. }
  141. }
  142. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  143. cache_sync();
  144. spin_unlock_irqrestore(&l2x0_lock, flags);
  145. }
  146. static void l2x0_flush_range(unsigned long start, unsigned long end)
  147. {
  148. void __iomem *base = l2x0_base;
  149. unsigned long flags;
  150. spin_lock_irqsave(&l2x0_lock, flags);
  151. start &= ~(CACHE_LINE_SIZE - 1);
  152. while (start < end) {
  153. unsigned long blk_end = start + min(end - start, 4096UL);
  154. debug_writel(0x03);
  155. while (start < blk_end) {
  156. l2x0_flush_line(start);
  157. start += CACHE_LINE_SIZE;
  158. }
  159. debug_writel(0x00);
  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_CLEAN_INV_LINE_PA, 1);
  166. cache_sync();
  167. spin_unlock_irqrestore(&l2x0_lock, flags);
  168. }
  169. void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
  170. {
  171. __u32 aux;
  172. l2x0_base = base;
  173. /*
  174. * Check if l2x0 controller is already enabled.
  175. * If you are booting from non-secure mode
  176. * accessing the below registers will fault.
  177. */
  178. if (!(readl(l2x0_base + L2X0_CTRL) & 1)) {
  179. /* l2x0 controller is disabled */
  180. aux = readl(l2x0_base + L2X0_AUX_CTRL);
  181. aux &= aux_mask;
  182. aux |= aux_val;
  183. writel(aux, l2x0_base + L2X0_AUX_CTRL);
  184. l2x0_inv_all();
  185. /* enable L2X0 */
  186. writel(1, l2x0_base + L2X0_CTRL);
  187. }
  188. outer_cache.inv_range = l2x0_inv_range;
  189. outer_cache.clean_range = l2x0_clean_range;
  190. outer_cache.flush_range = l2x0_flush_range;
  191. printk(KERN_INFO "L2X0 cache controller enabled\n");
  192. }