cache-uniphier.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2012-2014 Panasonic Corporation
  3. * Copyright (C) 2015-2016 Socionext Inc.
  4. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include <asm/armv7.h>
  12. #include <asm/processor.h>
  13. #include "cache-uniphier.h"
  14. #include "ssc-regs.h"
  15. #define UNIPHIER_SSCOQAD_IS_NEEDED(op) \
  16. ((op & UNIPHIER_SSCOQM_S_MASK) == UNIPHIER_SSCOQM_S_RANGE)
  17. #define UNIPHIER_SSCOQWM_IS_NEEDED(op) \
  18. ((op & UNIPHIER_SSCOQM_TID_MASK) == UNIPHIER_SSCOQM_TID_WAY)
  19. /* uniphier_cache_sync - perform a sync point for a particular cache level */
  20. static void uniphier_cache_sync(void)
  21. {
  22. /* drain internal buffers */
  23. writel(UNIPHIER_SSCOPE_CM_SYNC, UNIPHIER_SSCOPE);
  24. /* need a read back to confirm */
  25. readl(UNIPHIER_SSCOPE);
  26. }
  27. /**
  28. * uniphier_cache_maint_common - run a queue operation
  29. *
  30. * @start: start address of range operation (don't care for "all" operation)
  31. * @size: data size of range operation (don't care for "all" operation)
  32. * @ways: target ways (don't care for operations other than pre-fetch, touch
  33. * @operation: flags to specify the desired cache operation
  34. */
  35. static void uniphier_cache_maint_common(u32 start, u32 size, u32 ways,
  36. u32 operation)
  37. {
  38. /* clear the complete notification flag */
  39. writel(UNIPHIER_SSCOLPQS_EF, UNIPHIER_SSCOLPQS);
  40. do {
  41. /* set cache operation */
  42. writel(UNIPHIER_SSCOQM_CE | operation, UNIPHIER_SSCOQM);
  43. /* set address range if needed */
  44. if (likely(UNIPHIER_SSCOQAD_IS_NEEDED(operation))) {
  45. writel(start, UNIPHIER_SSCOQAD);
  46. writel(size, UNIPHIER_SSCOQSZ);
  47. }
  48. /* set target ways if needed */
  49. if (unlikely(UNIPHIER_SSCOQWM_IS_NEEDED(operation)))
  50. writel(ways, UNIPHIER_SSCOQWN);
  51. } while (unlikely(readl(UNIPHIER_SSCOPPQSEF) &
  52. (UNIPHIER_SSCOPPQSEF_FE | UNIPHIER_SSCOPPQSEF_OE)));
  53. /* wait until the operation is completed */
  54. while (likely(readl(UNIPHIER_SSCOLPQS) != UNIPHIER_SSCOLPQS_EF))
  55. cpu_relax();
  56. }
  57. static void uniphier_cache_maint_all(u32 operation)
  58. {
  59. uniphier_cache_maint_common(0, 0, 0, UNIPHIER_SSCOQM_S_ALL | operation);
  60. uniphier_cache_sync();
  61. }
  62. static void uniphier_cache_maint_range(u32 start, u32 end, u32 ways,
  63. u32 operation)
  64. {
  65. u32 size;
  66. /*
  67. * If the start address is not aligned,
  68. * perform a cache operation for the first cache-line
  69. */
  70. start = start & ~(UNIPHIER_SSC_LINE_SIZE - 1);
  71. size = end - start;
  72. if (unlikely(size >= (u32)(-UNIPHIER_SSC_LINE_SIZE))) {
  73. /* this means cache operation for all range */
  74. uniphier_cache_maint_all(operation);
  75. return;
  76. }
  77. /*
  78. * If the end address is not aligned,
  79. * perform a cache operation for the last cache-line
  80. */
  81. size = ALIGN(size, UNIPHIER_SSC_LINE_SIZE);
  82. while (size) {
  83. u32 chunk_size = min_t(u32, size, UNIPHIER_SSC_RANGE_OP_MAX_SIZE);
  84. uniphier_cache_maint_common(start, chunk_size, ways,
  85. UNIPHIER_SSCOQM_S_RANGE | operation);
  86. start += chunk_size;
  87. size -= chunk_size;
  88. }
  89. uniphier_cache_sync();
  90. }
  91. void uniphier_cache_prefetch_range(u32 start, u32 end, u32 ways)
  92. {
  93. uniphier_cache_maint_range(start, end, ways,
  94. UNIPHIER_SSCOQM_TID_WAY |
  95. UNIPHIER_SSCOQM_CM_PREFETCH);
  96. }
  97. void uniphier_cache_touch_range(u32 start, u32 end, u32 ways)
  98. {
  99. uniphier_cache_maint_range(start, end, ways,
  100. UNIPHIER_SSCOQM_TID_WAY |
  101. UNIPHIER_SSCOQM_CM_TOUCH);
  102. }
  103. void uniphier_cache_touch_zero_range(u32 start, u32 end, u32 ways)
  104. {
  105. uniphier_cache_maint_range(start, end, ways,
  106. UNIPHIER_SSCOQM_TID_WAY |
  107. UNIPHIER_SSCOQM_CM_TOUCH_ZERO);
  108. }
  109. #ifdef CONFIG_UNIPHIER_L2CACHE_ON
  110. void v7_outer_cache_flush_all(void)
  111. {
  112. uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_FLUSH);
  113. }
  114. void v7_outer_cache_inval_all(void)
  115. {
  116. uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_INV);
  117. }
  118. void v7_outer_cache_flush_range(u32 start, u32 end)
  119. {
  120. uniphier_cache_maint_range(start, end, 0, UNIPHIER_SSCOQM_CM_FLUSH);
  121. }
  122. void v7_outer_cache_inval_range(u32 start, u32 end)
  123. {
  124. if (start & (UNIPHIER_SSC_LINE_SIZE - 1)) {
  125. start &= ~(UNIPHIER_SSC_LINE_SIZE - 1);
  126. uniphier_cache_maint_range(start, UNIPHIER_SSC_LINE_SIZE, 0,
  127. UNIPHIER_SSCOQM_CM_FLUSH);
  128. start += UNIPHIER_SSC_LINE_SIZE;
  129. }
  130. if (start >= end) {
  131. uniphier_cache_sync();
  132. return;
  133. }
  134. if (end & (UNIPHIER_SSC_LINE_SIZE - 1)) {
  135. end &= ~(UNIPHIER_SSC_LINE_SIZE - 1);
  136. uniphier_cache_maint_range(end, UNIPHIER_SSC_LINE_SIZE, 0,
  137. UNIPHIER_SSCOQM_CM_FLUSH);
  138. }
  139. if (start >= end) {
  140. uniphier_cache_sync();
  141. return;
  142. }
  143. uniphier_cache_maint_range(start, end, 0, UNIPHIER_SSCOQM_CM_INV);
  144. }
  145. void v7_outer_cache_enable(void)
  146. {
  147. u32 tmp;
  148. writel(U32_MAX, UNIPHIER_SSCLPDAWCR); /* activate all ways */
  149. tmp = readl(UNIPHIER_SSCC);
  150. tmp |= UNIPHIER_SSCC_ON;
  151. writel(tmp, UNIPHIER_SSCC);
  152. }
  153. #endif
  154. void v7_outer_cache_disable(void)
  155. {
  156. u32 tmp;
  157. tmp = readl(UNIPHIER_SSCC);
  158. tmp &= ~UNIPHIER_SSCC_ON;
  159. writel(tmp, UNIPHIER_SSCC);
  160. }
  161. void enable_caches(void)
  162. {
  163. dcache_enable();
  164. }