cache.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Copyright (C) 2011 Texas Instruments Incorporated
  3. * Author: Mark Salter <msalter@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/io.h>
  12. #include <asm/cache.h>
  13. #include <asm/soc.h>
  14. /*
  15. * Internal Memory Control Registers for caches
  16. */
  17. #define IMCR_CCFG 0x0000
  18. #define IMCR_L1PCFG 0x0020
  19. #define IMCR_L1PCC 0x0024
  20. #define IMCR_L1DCFG 0x0040
  21. #define IMCR_L1DCC 0x0044
  22. #define IMCR_L2ALLOC0 0x2000
  23. #define IMCR_L2ALLOC1 0x2004
  24. #define IMCR_L2ALLOC2 0x2008
  25. #define IMCR_L2ALLOC3 0x200c
  26. #define IMCR_L2WBAR 0x4000
  27. #define IMCR_L2WWC 0x4004
  28. #define IMCR_L2WIBAR 0x4010
  29. #define IMCR_L2WIWC 0x4014
  30. #define IMCR_L2IBAR 0x4018
  31. #define IMCR_L2IWC 0x401c
  32. #define IMCR_L1PIBAR 0x4020
  33. #define IMCR_L1PIWC 0x4024
  34. #define IMCR_L1DWIBAR 0x4030
  35. #define IMCR_L1DWIWC 0x4034
  36. #define IMCR_L1DWBAR 0x4040
  37. #define IMCR_L1DWWC 0x4044
  38. #define IMCR_L1DIBAR 0x4048
  39. #define IMCR_L1DIWC 0x404c
  40. #define IMCR_L2WB 0x5000
  41. #define IMCR_L2WBINV 0x5004
  42. #define IMCR_L2INV 0x5008
  43. #define IMCR_L1PINV 0x5028
  44. #define IMCR_L1DWB 0x5040
  45. #define IMCR_L1DWBINV 0x5044
  46. #define IMCR_L1DINV 0x5048
  47. #define IMCR_MAR_BASE 0x8000
  48. #define IMCR_MAR96_111 0x8180
  49. #define IMCR_MAR128_191 0x8200
  50. #define IMCR_MAR224_239 0x8380
  51. #define IMCR_L2MPFAR 0xa000
  52. #define IMCR_L2MPFSR 0xa004
  53. #define IMCR_L2MPFCR 0xa008
  54. #define IMCR_L2MPLK0 0xa100
  55. #define IMCR_L2MPLK1 0xa104
  56. #define IMCR_L2MPLK2 0xa108
  57. #define IMCR_L2MPLK3 0xa10c
  58. #define IMCR_L2MPLKCMD 0xa110
  59. #define IMCR_L2MPLKSTAT 0xa114
  60. #define IMCR_L2MPPA_BASE 0xa200
  61. #define IMCR_L1PMPFAR 0xa400
  62. #define IMCR_L1PMPFSR 0xa404
  63. #define IMCR_L1PMPFCR 0xa408
  64. #define IMCR_L1PMPLK0 0xa500
  65. #define IMCR_L1PMPLK1 0xa504
  66. #define IMCR_L1PMPLK2 0xa508
  67. #define IMCR_L1PMPLK3 0xa50c
  68. #define IMCR_L1PMPLKCMD 0xa510
  69. #define IMCR_L1PMPLKSTAT 0xa514
  70. #define IMCR_L1PMPPA_BASE 0xa600
  71. #define IMCR_L1DMPFAR 0xac00
  72. #define IMCR_L1DMPFSR 0xac04
  73. #define IMCR_L1DMPFCR 0xac08
  74. #define IMCR_L1DMPLK0 0xad00
  75. #define IMCR_L1DMPLK1 0xad04
  76. #define IMCR_L1DMPLK2 0xad08
  77. #define IMCR_L1DMPLK3 0xad0c
  78. #define IMCR_L1DMPLKCMD 0xad10
  79. #define IMCR_L1DMPLKSTAT 0xad14
  80. #define IMCR_L1DMPPA_BASE 0xae00
  81. #define IMCR_L2PDWAKE0 0xc040
  82. #define IMCR_L2PDWAKE1 0xc044
  83. #define IMCR_L2PDSLEEP0 0xc050
  84. #define IMCR_L2PDSLEEP1 0xc054
  85. #define IMCR_L2PDSTAT0 0xc060
  86. #define IMCR_L2PDSTAT1 0xc064
  87. /*
  88. * CCFG register values and bits
  89. */
  90. #define L2MODE_0K_CACHE 0x0
  91. #define L2MODE_32K_CACHE 0x1
  92. #define L2MODE_64K_CACHE 0x2
  93. #define L2MODE_128K_CACHE 0x3
  94. #define L2MODE_256K_CACHE 0x7
  95. #define L2PRIO_URGENT 0x0
  96. #define L2PRIO_HIGH 0x1
  97. #define L2PRIO_MEDIUM 0x2
  98. #define L2PRIO_LOW 0x3
  99. #define CCFG_ID 0x100 /* Invalidate L1P bit */
  100. #define CCFG_IP 0x200 /* Invalidate L1D bit */
  101. static void __iomem *cache_base;
  102. /*
  103. * L1 & L2 caches generic functions
  104. */
  105. #define imcr_get(reg) soc_readl(cache_base + (reg))
  106. #define imcr_set(reg, value) \
  107. do { \
  108. soc_writel((value), cache_base + (reg)); \
  109. soc_readl(cache_base + (reg)); \
  110. } while (0)
  111. static void cache_block_operation_wait(unsigned int wc_reg)
  112. {
  113. /* Wait for completion */
  114. while (imcr_get(wc_reg))
  115. cpu_relax();
  116. }
  117. static DEFINE_SPINLOCK(cache_lock);
  118. /*
  119. * Generic function to perform a block cache operation as
  120. * invalidate or writeback/invalidate
  121. */
  122. static void cache_block_operation(unsigned int *start,
  123. unsigned int *end,
  124. unsigned int bar_reg,
  125. unsigned int wc_reg)
  126. {
  127. unsigned long flags;
  128. unsigned int wcnt =
  129. (L2_CACHE_ALIGN_CNT((unsigned int) end)
  130. - L2_CACHE_ALIGN_LOW((unsigned int) start)) >> 2;
  131. unsigned int wc = 0;
  132. for (; wcnt; wcnt -= wc, start += wc) {
  133. loop:
  134. spin_lock_irqsave(&cache_lock, flags);
  135. /*
  136. * If another cache operation is occuring
  137. */
  138. if (unlikely(imcr_get(wc_reg))) {
  139. spin_unlock_irqrestore(&cache_lock, flags);
  140. /* Wait for previous operation completion */
  141. cache_block_operation_wait(wc_reg);
  142. /* Try again */
  143. goto loop;
  144. }
  145. imcr_set(bar_reg, L2_CACHE_ALIGN_LOW((unsigned int) start));
  146. if (wcnt > 0xffff)
  147. wc = 0xffff;
  148. else
  149. wc = wcnt;
  150. /* Set word count value in the WC register */
  151. imcr_set(wc_reg, wc & 0xffff);
  152. spin_unlock_irqrestore(&cache_lock, flags);
  153. /* Wait for completion */
  154. cache_block_operation_wait(wc_reg);
  155. }
  156. }
  157. static void cache_block_operation_nowait(unsigned int *start,
  158. unsigned int *end,
  159. unsigned int bar_reg,
  160. unsigned int wc_reg)
  161. {
  162. unsigned long flags;
  163. unsigned int wcnt =
  164. (L2_CACHE_ALIGN_CNT((unsigned int) end)
  165. - L2_CACHE_ALIGN_LOW((unsigned int) start)) >> 2;
  166. unsigned int wc = 0;
  167. for (; wcnt; wcnt -= wc, start += wc) {
  168. spin_lock_irqsave(&cache_lock, flags);
  169. imcr_set(bar_reg, L2_CACHE_ALIGN_LOW((unsigned int) start));
  170. if (wcnt > 0xffff)
  171. wc = 0xffff;
  172. else
  173. wc = wcnt;
  174. /* Set word count value in the WC register */
  175. imcr_set(wc_reg, wc & 0xffff);
  176. spin_unlock_irqrestore(&cache_lock, flags);
  177. /* Don't wait for completion on last cache operation */
  178. if (wcnt > 0xffff)
  179. cache_block_operation_wait(wc_reg);
  180. }
  181. }
  182. /*
  183. * L1 caches management
  184. */
  185. /*
  186. * Disable L1 caches
  187. */
  188. void L1_cache_off(void)
  189. {
  190. unsigned int dummy;
  191. imcr_set(IMCR_L1PCFG, 0);
  192. dummy = imcr_get(IMCR_L1PCFG);
  193. imcr_set(IMCR_L1DCFG, 0);
  194. dummy = imcr_get(IMCR_L1DCFG);
  195. }
  196. /*
  197. * Enable L1 caches
  198. */
  199. void L1_cache_on(void)
  200. {
  201. unsigned int dummy;
  202. imcr_set(IMCR_L1PCFG, 7);
  203. dummy = imcr_get(IMCR_L1PCFG);
  204. imcr_set(IMCR_L1DCFG, 7);
  205. dummy = imcr_get(IMCR_L1DCFG);
  206. }
  207. /*
  208. * L1P global-invalidate all
  209. */
  210. void L1P_cache_global_invalidate(void)
  211. {
  212. unsigned int set = 1;
  213. imcr_set(IMCR_L1PINV, set);
  214. while (imcr_get(IMCR_L1PINV) & 1)
  215. cpu_relax();
  216. }
  217. /*
  218. * L1D global-invalidate all
  219. *
  220. * Warning: this operation causes all updated data in L1D to
  221. * be discarded rather than written back to the lower levels of
  222. * memory
  223. */
  224. void L1D_cache_global_invalidate(void)
  225. {
  226. unsigned int set = 1;
  227. imcr_set(IMCR_L1DINV, set);
  228. while (imcr_get(IMCR_L1DINV) & 1)
  229. cpu_relax();
  230. }
  231. void L1D_cache_global_writeback(void)
  232. {
  233. unsigned int set = 1;
  234. imcr_set(IMCR_L1DWB, set);
  235. while (imcr_get(IMCR_L1DWB) & 1)
  236. cpu_relax();
  237. }
  238. void L1D_cache_global_writeback_invalidate(void)
  239. {
  240. unsigned int set = 1;
  241. imcr_set(IMCR_L1DWBINV, set);
  242. while (imcr_get(IMCR_L1DWBINV) & 1)
  243. cpu_relax();
  244. }
  245. /*
  246. * L2 caches management
  247. */
  248. /*
  249. * Set L2 operation mode
  250. */
  251. void L2_cache_set_mode(unsigned int mode)
  252. {
  253. unsigned int ccfg = imcr_get(IMCR_CCFG);
  254. /* Clear and set the L2MODE bits in CCFG */
  255. ccfg &= ~7;
  256. ccfg |= (mode & 7);
  257. imcr_set(IMCR_CCFG, ccfg);
  258. ccfg = imcr_get(IMCR_CCFG);
  259. }
  260. /*
  261. * L2 global-writeback and global-invalidate all
  262. */
  263. void L2_cache_global_writeback_invalidate(void)
  264. {
  265. imcr_set(IMCR_L2WBINV, 1);
  266. while (imcr_get(IMCR_L2WBINV))
  267. cpu_relax();
  268. }
  269. /*
  270. * L2 global-writeback all
  271. */
  272. void L2_cache_global_writeback(void)
  273. {
  274. imcr_set(IMCR_L2WB, 1);
  275. while (imcr_get(IMCR_L2WB))
  276. cpu_relax();
  277. }
  278. /*
  279. * Cacheability controls
  280. */
  281. void enable_caching(unsigned long start, unsigned long end)
  282. {
  283. unsigned int mar = IMCR_MAR_BASE + ((start >> 24) << 2);
  284. unsigned int mar_e = IMCR_MAR_BASE + ((end >> 24) << 2);
  285. for (; mar <= mar_e; mar += 4)
  286. imcr_set(mar, imcr_get(mar) | 1);
  287. }
  288. void disable_caching(unsigned long start, unsigned long end)
  289. {
  290. unsigned int mar = IMCR_MAR_BASE + ((start >> 24) << 2);
  291. unsigned int mar_e = IMCR_MAR_BASE + ((end >> 24) << 2);
  292. for (; mar <= mar_e; mar += 4)
  293. imcr_set(mar, imcr_get(mar) & ~1);
  294. }
  295. /*
  296. * L1 block operations
  297. */
  298. void L1P_cache_block_invalidate(unsigned int start, unsigned int end)
  299. {
  300. cache_block_operation((unsigned int *) start,
  301. (unsigned int *) end,
  302. IMCR_L1PIBAR, IMCR_L1PIWC);
  303. }
  304. EXPORT_SYMBOL(L1P_cache_block_invalidate);
  305. void L1D_cache_block_invalidate(unsigned int start, unsigned int end)
  306. {
  307. cache_block_operation((unsigned int *) start,
  308. (unsigned int *) end,
  309. IMCR_L1DIBAR, IMCR_L1DIWC);
  310. }
  311. void L1D_cache_block_writeback_invalidate(unsigned int start, unsigned int end)
  312. {
  313. cache_block_operation((unsigned int *) start,
  314. (unsigned int *) end,
  315. IMCR_L1DWIBAR, IMCR_L1DWIWC);
  316. }
  317. void L1D_cache_block_writeback(unsigned int start, unsigned int end)
  318. {
  319. cache_block_operation((unsigned int *) start,
  320. (unsigned int *) end,
  321. IMCR_L1DWBAR, IMCR_L1DWWC);
  322. }
  323. EXPORT_SYMBOL(L1D_cache_block_writeback);
  324. /*
  325. * L2 block operations
  326. */
  327. void L2_cache_block_invalidate(unsigned int start, unsigned int end)
  328. {
  329. cache_block_operation((unsigned int *) start,
  330. (unsigned int *) end,
  331. IMCR_L2IBAR, IMCR_L2IWC);
  332. }
  333. void L2_cache_block_writeback(unsigned int start, unsigned int end)
  334. {
  335. cache_block_operation((unsigned int *) start,
  336. (unsigned int *) end,
  337. IMCR_L2WBAR, IMCR_L2WWC);
  338. }
  339. void L2_cache_block_writeback_invalidate(unsigned int start, unsigned int end)
  340. {
  341. cache_block_operation((unsigned int *) start,
  342. (unsigned int *) end,
  343. IMCR_L2WIBAR, IMCR_L2WIWC);
  344. }
  345. void L2_cache_block_invalidate_nowait(unsigned int start, unsigned int end)
  346. {
  347. cache_block_operation_nowait((unsigned int *) start,
  348. (unsigned int *) end,
  349. IMCR_L2IBAR, IMCR_L2IWC);
  350. }
  351. void L2_cache_block_writeback_nowait(unsigned int start, unsigned int end)
  352. {
  353. cache_block_operation_nowait((unsigned int *) start,
  354. (unsigned int *) end,
  355. IMCR_L2WBAR, IMCR_L2WWC);
  356. }
  357. void L2_cache_block_writeback_invalidate_nowait(unsigned int start,
  358. unsigned int end)
  359. {
  360. cache_block_operation_nowait((unsigned int *) start,
  361. (unsigned int *) end,
  362. IMCR_L2WIBAR, IMCR_L2WIWC);
  363. }
  364. /*
  365. * L1 and L2 caches configuration
  366. */
  367. void __init c6x_cache_init(void)
  368. {
  369. struct device_node *node;
  370. node = of_find_compatible_node(NULL, NULL, "ti,c64x+cache");
  371. if (!node)
  372. return;
  373. cache_base = of_iomap(node, 0);
  374. of_node_put(node);
  375. if (!cache_base)
  376. return;
  377. /* Set L2 caches on the the whole L2 SRAM memory */
  378. L2_cache_set_mode(L2MODE_SIZE);
  379. /* Enable L1 */
  380. L1_cache_on();
  381. }