cache-uniphier.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012-2014 Panasonic Corporation
  4. * Copyright (C) 2015-2016 Socionext Inc.
  5. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  6. */
  7. #include <common.h>
  8. #include <linux/io.h>
  9. #include <linux/kernel.h>
  10. #include <asm/armv7.h>
  11. #include <asm/processor.h>
  12. #include "cache-uniphier.h"
  13. /* control registers */
  14. #define UNIPHIER_SSCC 0x500c0000 /* Control Register */
  15. #define UNIPHIER_SSCC_BST (0x1 << 20) /* UCWG burst read */
  16. #define UNIPHIER_SSCC_ACT (0x1 << 19) /* Inst-Data separate */
  17. #define UNIPHIER_SSCC_WTG (0x1 << 18) /* WT gathering on */
  18. #define UNIPHIER_SSCC_PRD (0x1 << 17) /* enable pre-fetch */
  19. #define UNIPHIER_SSCC_ON (0x1 << 0) /* enable cache */
  20. #define UNIPHIER_SSCLPDAWCR 0x500c0030 /* Unified/Data Active Way Control */
  21. #define UNIPHIER_SSCLPIAWCR 0x500c0034 /* Instruction Active Way Control */
  22. /* revision registers */
  23. #define UNIPHIER_SSCID 0x503c0100 /* ID Register */
  24. /* operation registers */
  25. #define UNIPHIER_SSCOPE 0x506c0244 /* Cache Operation Primitive Entry */
  26. #define UNIPHIER_SSCOPE_CM_INV 0x0 /* invalidate */
  27. #define UNIPHIER_SSCOPE_CM_CLEAN 0x1 /* clean */
  28. #define UNIPHIER_SSCOPE_CM_FLUSH 0x2 /* flush */
  29. #define UNIPHIER_SSCOPE_CM_SYNC 0x8 /* sync (drain bufs) */
  30. #define UNIPHIER_SSCOPE_CM_FLUSH_PREFETCH 0x9 /* flush p-fetch buf */
  31. #define UNIPHIER_SSCOQM 0x506c0248
  32. #define UNIPHIER_SSCOQM_TID_MASK (0x3 << 21)
  33. #define UNIPHIER_SSCOQM_TID_LRU_DATA (0x0 << 21)
  34. #define UNIPHIER_SSCOQM_TID_LRU_INST (0x1 << 21)
  35. #define UNIPHIER_SSCOQM_TID_WAY (0x2 << 21)
  36. #define UNIPHIER_SSCOQM_S_MASK (0x3 << 17)
  37. #define UNIPHIER_SSCOQM_S_RANGE (0x0 << 17)
  38. #define UNIPHIER_SSCOQM_S_ALL (0x1 << 17)
  39. #define UNIPHIER_SSCOQM_S_WAY (0x2 << 17)
  40. #define UNIPHIER_SSCOQM_CE (0x1 << 15) /* notify completion */
  41. #define UNIPHIER_SSCOQM_CW (0x1 << 14)
  42. #define UNIPHIER_SSCOQM_CM_MASK (0x7)
  43. #define UNIPHIER_SSCOQM_CM_INV 0x0 /* invalidate */
  44. #define UNIPHIER_SSCOQM_CM_CLEAN 0x1 /* clean */
  45. #define UNIPHIER_SSCOQM_CM_FLUSH 0x2 /* flush */
  46. #define UNIPHIER_SSCOQM_CM_PREFETCH 0x3 /* prefetch to cache */
  47. #define UNIPHIER_SSCOQM_CM_PREFETCH_BUF 0x4 /* prefetch to pf-buf */
  48. #define UNIPHIER_SSCOQM_CM_TOUCH 0x5 /* touch */
  49. #define UNIPHIER_SSCOQM_CM_TOUCH_ZERO 0x6 /* touch to zero */
  50. #define UNIPHIER_SSCOQM_CM_TOUCH_DIRTY 0x7 /* touch with dirty */
  51. #define UNIPHIER_SSCOQAD 0x506c024c /* Cache Operation Queue Address */
  52. #define UNIPHIER_SSCOQSZ 0x506c0250 /* Cache Operation Queue Size */
  53. #define UNIPHIER_SSCOQMASK 0x506c0254 /* Cache Operation Queue Address Mask */
  54. #define UNIPHIER_SSCOQWN 0x506c0258 /* Cache Operation Queue Way Number */
  55. #define UNIPHIER_SSCOPPQSEF 0x506c025c /* Cache Operation Queue Set Complete */
  56. #define UNIPHIER_SSCOPPQSEF_FE (0x1 << 1)
  57. #define UNIPHIER_SSCOPPQSEF_OE (0x1 << 0)
  58. #define UNIPHIER_SSCOLPQS 0x506c0260 /* Cache Operation Queue Status */
  59. #define UNIPHIER_SSCOLPQS_EF (0x1 << 2)
  60. #define UNIPHIER_SSCOLPQS_EST (0x1 << 1)
  61. #define UNIPHIER_SSCOLPQS_QST (0x1 << 0)
  62. #define UNIPHIER_SSC_LINE_SIZE 128
  63. #define UNIPHIER_SSC_RANGE_OP_MAX_SIZE (0x00400000 - (UNIPHIER_SSC_LINE_SIZE))
  64. #define UNIPHIER_SSCOQAD_IS_NEEDED(op) \
  65. ((op & UNIPHIER_SSCOQM_S_MASK) == UNIPHIER_SSCOQM_S_RANGE)
  66. #define UNIPHIER_SSCOQWM_IS_NEEDED(op) \
  67. (((op & UNIPHIER_SSCOQM_S_MASK) == UNIPHIER_SSCOQM_S_WAY) || \
  68. ((op & UNIPHIER_SSCOQM_TID_MASK) == UNIPHIER_SSCOQM_TID_WAY))
  69. /* uniphier_cache_sync - perform a sync point for a particular cache level */
  70. static void uniphier_cache_sync(void)
  71. {
  72. /* drain internal buffers */
  73. writel(UNIPHIER_SSCOPE_CM_SYNC, UNIPHIER_SSCOPE);
  74. /* need a read back to confirm */
  75. readl(UNIPHIER_SSCOPE);
  76. }
  77. /**
  78. * uniphier_cache_maint_common - run a queue operation
  79. *
  80. * @start: start address of range operation (don't care for "all" operation)
  81. * @size: data size of range operation (don't care for "all" operation)
  82. * @ways: target ways (don't care for operations other than pre-fetch, touch
  83. * @operation: flags to specify the desired cache operation
  84. */
  85. static void uniphier_cache_maint_common(u32 start, u32 size, u32 ways,
  86. u32 operation)
  87. {
  88. /* clear the complete notification flag */
  89. writel(UNIPHIER_SSCOLPQS_EF, UNIPHIER_SSCOLPQS);
  90. do {
  91. /* set cache operation */
  92. writel(UNIPHIER_SSCOQM_CE | operation, UNIPHIER_SSCOQM);
  93. /* set address range if needed */
  94. if (likely(UNIPHIER_SSCOQAD_IS_NEEDED(operation))) {
  95. writel(start, UNIPHIER_SSCOQAD);
  96. writel(size, UNIPHIER_SSCOQSZ);
  97. }
  98. /* set target ways if needed */
  99. if (unlikely(UNIPHIER_SSCOQWM_IS_NEEDED(operation)))
  100. writel(ways, UNIPHIER_SSCOQWN);
  101. } while (unlikely(readl(UNIPHIER_SSCOPPQSEF) &
  102. (UNIPHIER_SSCOPPQSEF_FE | UNIPHIER_SSCOPPQSEF_OE)));
  103. /* wait until the operation is completed */
  104. while (likely(readl(UNIPHIER_SSCOLPQS) != UNIPHIER_SSCOLPQS_EF))
  105. cpu_relax();
  106. }
  107. static void uniphier_cache_maint_all(u32 operation)
  108. {
  109. uniphier_cache_maint_common(0, 0, 0, UNIPHIER_SSCOQM_S_ALL | operation);
  110. uniphier_cache_sync();
  111. }
  112. static void uniphier_cache_maint_range(u32 start, u32 end, u32 ways,
  113. u32 operation)
  114. {
  115. u32 size;
  116. /*
  117. * If the start address is not aligned,
  118. * perform a cache operation for the first cache-line
  119. */
  120. start = start & ~(UNIPHIER_SSC_LINE_SIZE - 1);
  121. size = end - start;
  122. if (unlikely(size >= (u32)(-UNIPHIER_SSC_LINE_SIZE))) {
  123. /* this means cache operation for all range */
  124. uniphier_cache_maint_all(operation);
  125. return;
  126. }
  127. /*
  128. * If the end address is not aligned,
  129. * perform a cache operation for the last cache-line
  130. */
  131. size = ALIGN(size, UNIPHIER_SSC_LINE_SIZE);
  132. while (size) {
  133. u32 chunk_size = min_t(u32, size, UNIPHIER_SSC_RANGE_OP_MAX_SIZE);
  134. uniphier_cache_maint_common(start, chunk_size, ways,
  135. UNIPHIER_SSCOQM_S_RANGE | operation);
  136. start += chunk_size;
  137. size -= chunk_size;
  138. }
  139. uniphier_cache_sync();
  140. }
  141. void uniphier_cache_prefetch_range(u32 start, u32 end, u32 ways)
  142. {
  143. uniphier_cache_maint_range(start, end, ways,
  144. UNIPHIER_SSCOQM_TID_WAY |
  145. UNIPHIER_SSCOQM_CM_PREFETCH);
  146. }
  147. void uniphier_cache_touch_range(u32 start, u32 end, u32 ways)
  148. {
  149. uniphier_cache_maint_range(start, end, ways,
  150. UNIPHIER_SSCOQM_TID_WAY |
  151. UNIPHIER_SSCOQM_CM_TOUCH);
  152. }
  153. void uniphier_cache_touch_zero_range(u32 start, u32 end, u32 ways)
  154. {
  155. uniphier_cache_maint_range(start, end, ways,
  156. UNIPHIER_SSCOQM_TID_WAY |
  157. UNIPHIER_SSCOQM_CM_TOUCH_ZERO);
  158. }
  159. void uniphier_cache_inv_way(u32 ways)
  160. {
  161. uniphier_cache_maint_common(0, 0, ways,
  162. UNIPHIER_SSCOQM_S_WAY |
  163. UNIPHIER_SSCOQM_CM_INV);
  164. }
  165. void uniphier_cache_set_active_ways(int cpu, u32 active_ways)
  166. {
  167. void __iomem *base = (void __iomem *)UNIPHIER_SSCC + 0xc00;
  168. switch (readl(UNIPHIER_SSCID)) { /* revision */
  169. case 0x12: /* LD4 */
  170. case 0x16: /* sld8 */
  171. base = (void __iomem *)UNIPHIER_SSCC + 0x840;
  172. break;
  173. default:
  174. base = (void __iomem *)UNIPHIER_SSCC + 0xc00;
  175. break;
  176. }
  177. writel(active_ways, base + 4 * cpu);
  178. }
  179. static void uniphier_cache_endisable(int enable)
  180. {
  181. u32 tmp;
  182. tmp = readl(UNIPHIER_SSCC);
  183. if (enable)
  184. tmp |= UNIPHIER_SSCC_ON;
  185. else
  186. tmp &= ~UNIPHIER_SSCC_ON;
  187. writel(tmp, UNIPHIER_SSCC);
  188. }
  189. void uniphier_cache_enable(void)
  190. {
  191. uniphier_cache_endisable(1);
  192. }
  193. void uniphier_cache_disable(void)
  194. {
  195. uniphier_cache_endisable(0);
  196. }
  197. #ifdef CONFIG_CACHE_UNIPHIER
  198. void v7_outer_cache_flush_all(void)
  199. {
  200. uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_FLUSH);
  201. }
  202. void v7_outer_cache_inval_all(void)
  203. {
  204. uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_INV);
  205. }
  206. void v7_outer_cache_flush_range(u32 start, u32 end)
  207. {
  208. uniphier_cache_maint_range(start, end, 0, UNIPHIER_SSCOQM_CM_FLUSH);
  209. }
  210. void v7_outer_cache_inval_range(u32 start, u32 end)
  211. {
  212. if (start & (UNIPHIER_SSC_LINE_SIZE - 1)) {
  213. start &= ~(UNIPHIER_SSC_LINE_SIZE - 1);
  214. uniphier_cache_maint_range(start, UNIPHIER_SSC_LINE_SIZE, 0,
  215. UNIPHIER_SSCOQM_CM_FLUSH);
  216. start += UNIPHIER_SSC_LINE_SIZE;
  217. }
  218. if (start >= end) {
  219. uniphier_cache_sync();
  220. return;
  221. }
  222. if (end & (UNIPHIER_SSC_LINE_SIZE - 1)) {
  223. end &= ~(UNIPHIER_SSC_LINE_SIZE - 1);
  224. uniphier_cache_maint_range(end, UNIPHIER_SSC_LINE_SIZE, 0,
  225. UNIPHIER_SSCOQM_CM_FLUSH);
  226. }
  227. if (start >= end) {
  228. uniphier_cache_sync();
  229. return;
  230. }
  231. uniphier_cache_maint_range(start, end, 0, UNIPHIER_SSCOQM_CM_INV);
  232. }
  233. void v7_outer_cache_enable(void)
  234. {
  235. uniphier_cache_set_active_ways(0, U32_MAX); /* activate all ways */
  236. uniphier_cache_enable();
  237. }
  238. void v7_outer_cache_disable(void)
  239. {
  240. uniphier_cache_disable();
  241. }
  242. #endif
  243. void enable_caches(void)
  244. {
  245. dcache_enable();
  246. }