cache-l2x0.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_inv_all(void)
  40. {
  41. unsigned long flags;
  42. /* invalidate all ways */
  43. spin_lock_irqsave(&l2x0_lock, flags);
  44. writel(0xff, l2x0_base + L2X0_INV_WAY);
  45. cache_wait(l2x0_base + L2X0_INV_WAY, 0xff);
  46. cache_sync();
  47. spin_unlock_irqrestore(&l2x0_lock, flags);
  48. }
  49. static void l2x0_inv_range(unsigned long start, unsigned long end)
  50. {
  51. void __iomem *base = l2x0_base;
  52. unsigned long flags;
  53. spin_lock_irqsave(&l2x0_lock, flags);
  54. if (start & (CACHE_LINE_SIZE - 1)) {
  55. start &= ~(CACHE_LINE_SIZE - 1);
  56. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  57. writel(start, base + L2X0_CLEAN_INV_LINE_PA);
  58. start += CACHE_LINE_SIZE;
  59. }
  60. if (end & (CACHE_LINE_SIZE - 1)) {
  61. end &= ~(CACHE_LINE_SIZE - 1);
  62. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  63. writel(end, base + L2X0_CLEAN_INV_LINE_PA);
  64. }
  65. while (start < end) {
  66. unsigned long blk_end = start + min(end - start, 4096UL);
  67. while (start < blk_end) {
  68. cache_wait(base + L2X0_INV_LINE_PA, 1);
  69. writel(start, base + L2X0_INV_LINE_PA);
  70. start += CACHE_LINE_SIZE;
  71. }
  72. if (blk_end < end) {
  73. spin_unlock_irqrestore(&l2x0_lock, flags);
  74. spin_lock_irqsave(&l2x0_lock, flags);
  75. }
  76. }
  77. cache_wait(base + L2X0_INV_LINE_PA, 1);
  78. cache_sync();
  79. spin_unlock_irqrestore(&l2x0_lock, flags);
  80. }
  81. static void l2x0_clean_range(unsigned long start, unsigned long end)
  82. {
  83. void __iomem *base = l2x0_base;
  84. unsigned long flags;
  85. spin_lock_irqsave(&l2x0_lock, flags);
  86. start &= ~(CACHE_LINE_SIZE - 1);
  87. while (start < end) {
  88. unsigned long blk_end = start + min(end - start, 4096UL);
  89. while (start < blk_end) {
  90. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  91. writel(start, base + L2X0_CLEAN_LINE_PA);
  92. start += CACHE_LINE_SIZE;
  93. }
  94. if (blk_end < end) {
  95. spin_unlock_irqrestore(&l2x0_lock, flags);
  96. spin_lock_irqsave(&l2x0_lock, flags);
  97. }
  98. }
  99. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  100. cache_sync();
  101. spin_unlock_irqrestore(&l2x0_lock, flags);
  102. }
  103. static void l2x0_flush_range(unsigned long start, unsigned long end)
  104. {
  105. void __iomem *base = l2x0_base;
  106. unsigned long flags;
  107. spin_lock_irqsave(&l2x0_lock, flags);
  108. start &= ~(CACHE_LINE_SIZE - 1);
  109. while (start < end) {
  110. unsigned long blk_end = start + min(end - start, 4096UL);
  111. while (start < blk_end) {
  112. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  113. writel(start, base + L2X0_CLEAN_INV_LINE_PA);
  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_CLEAN_INV_LINE_PA, 1);
  122. cache_sync();
  123. spin_unlock_irqrestore(&l2x0_lock, flags);
  124. }
  125. void __init l2x0_init(void __iomem *base, __u32 aux_val, __u32 aux_mask)
  126. {
  127. __u32 aux;
  128. l2x0_base = base;
  129. /*
  130. * Check if l2x0 controller is already enabled.
  131. * If you are booting from non-secure mode
  132. * accessing the below registers will fault.
  133. */
  134. if (!(readl(l2x0_base + L2X0_CTRL) & 1)) {
  135. /* l2x0 controller is disabled */
  136. aux = readl(l2x0_base + L2X0_AUX_CTRL);
  137. aux &= aux_mask;
  138. aux |= aux_val;
  139. writel(aux, l2x0_base + L2X0_AUX_CTRL);
  140. l2x0_inv_all();
  141. /* enable L2X0 */
  142. writel(1, l2x0_base + L2X0_CTRL);
  143. }
  144. outer_cache.inv_range = l2x0_inv_range;
  145. outer_cache.clean_range = l2x0_clean_range;
  146. outer_cache.flush_range = l2x0_flush_range;
  147. printk(KERN_INFO "L2X0 cache controller enabled\n");
  148. }