cache.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
  4. * Author(s): Vikas Manocha, <vikas.manocha@st.com> for STMicroelectronics.
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <asm/armv7m.h>
  9. #include <asm/io.h>
  10. /* Cache maintenance operation registers */
  11. #define V7M_CACHE_REG_ICIALLU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x00))
  12. #define INVAL_ICACHE_POU 0
  13. #define V7M_CACHE_REG_ICIMVALU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x08))
  14. #define V7M_CACHE_REG_DCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x0C))
  15. #define V7M_CACHE_REG_DCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x10))
  16. #define V7M_CACHE_REG_DCCMVAU ((u32 *)(V7M_CACHE_MAINT_BASE + 0x14))
  17. #define V7M_CACHE_REG_DCCMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x18))
  18. #define V7M_CACHE_REG_DCCSW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x1C))
  19. #define V7M_CACHE_REG_DCCIMVAC ((u32 *)(V7M_CACHE_MAINT_BASE + 0x20))
  20. #define V7M_CACHE_REG_DCCISW ((u32 *)(V7M_CACHE_MAINT_BASE + 0x24))
  21. #define WAYS_SHIFT 30
  22. #define SETS_SHIFT 5
  23. /* armv7m processor feature registers */
  24. #define V7M_PROC_REG_CLIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x00))
  25. #define V7M_PROC_REG_CTR ((u32 *)(V7M_PROC_FTR_BASE + 0x04))
  26. #define V7M_PROC_REG_CCSIDR ((u32 *)(V7M_PROC_FTR_BASE + 0x08))
  27. #define MASK_NUM_WAYS GENMASK(12, 3)
  28. #define MASK_NUM_SETS GENMASK(27, 13)
  29. #define CLINE_SIZE_MASK GENMASK(2, 0)
  30. #define NUM_WAYS_SHIFT 3
  31. #define NUM_SETS_SHIFT 13
  32. #define V7M_PROC_REG_CSSELR ((u32 *)(V7M_PROC_FTR_BASE + 0x0C))
  33. #define SEL_I_OR_D BIT(0)
  34. enum cache_type {
  35. DCACHE,
  36. ICACHE,
  37. };
  38. /* PoU : Point of Unification, Poc: Point of Coherency */
  39. enum cache_action {
  40. INVALIDATE_POU, /* i-cache invalidate by address */
  41. INVALIDATE_POC, /* d-cache invalidate by address */
  42. INVALIDATE_SET_WAY, /* d-cache invalidate by sets/ways */
  43. FLUSH_POU, /* d-cache clean by address to the PoU */
  44. FLUSH_POC, /* d-cache clean by address to the PoC */
  45. FLUSH_SET_WAY, /* d-cache clean by sets/ways */
  46. FLUSH_INVAL_POC, /* d-cache clean & invalidate by addr to PoC */
  47. FLUSH_INVAL_SET_WAY, /* d-cache clean & invalidate by set/ways */
  48. };
  49. #ifndef CONFIG_SYS_DCACHE_OFF
  50. struct dcache_config {
  51. u32 ways;
  52. u32 sets;
  53. };
  54. static void get_cache_ways_sets(struct dcache_config *cache)
  55. {
  56. u32 cache_size_id = readl(V7M_PROC_REG_CCSIDR);
  57. cache->ways = (cache_size_id & MASK_NUM_WAYS) >> NUM_WAYS_SHIFT;
  58. cache->sets = (cache_size_id & MASK_NUM_SETS) >> NUM_SETS_SHIFT;
  59. }
  60. /*
  61. * Return the io register to perform required cache action like clean or clean
  62. * & invalidate by sets/ways.
  63. */
  64. static u32 *get_action_reg_set_ways(enum cache_action action)
  65. {
  66. switch (action) {
  67. case INVALIDATE_SET_WAY:
  68. return V7M_CACHE_REG_DCISW;
  69. case FLUSH_SET_WAY:
  70. return V7M_CACHE_REG_DCCSW;
  71. case FLUSH_INVAL_SET_WAY:
  72. return V7M_CACHE_REG_DCCISW;
  73. default:
  74. break;
  75. };
  76. return NULL;
  77. }
  78. /*
  79. * Return the io register to perform required cache action like clean or clean
  80. * & invalidate by adddress or range.
  81. */
  82. static u32 *get_action_reg_range(enum cache_action action)
  83. {
  84. switch (action) {
  85. case INVALIDATE_POU:
  86. return V7M_CACHE_REG_ICIMVALU;
  87. case INVALIDATE_POC:
  88. return V7M_CACHE_REG_DCIMVAC;
  89. case FLUSH_POU:
  90. return V7M_CACHE_REG_DCCMVAU;
  91. case FLUSH_POC:
  92. return V7M_CACHE_REG_DCCMVAC;
  93. case FLUSH_INVAL_POC:
  94. return V7M_CACHE_REG_DCCIMVAC;
  95. default:
  96. break;
  97. }
  98. return NULL;
  99. }
  100. static u32 get_cline_size(enum cache_type type)
  101. {
  102. u32 size;
  103. if (type == DCACHE)
  104. clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
  105. else if (type == ICACHE)
  106. setbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
  107. /* Make sure cache selection is effective for next memory access */
  108. dsb();
  109. size = readl(V7M_PROC_REG_CCSIDR) & CLINE_SIZE_MASK;
  110. /* Size enocoded as 2 less than log(no_of_words_in_cache_line) base 2 */
  111. size = 1 << (size + 2);
  112. debug("cache line size is %d\n", size);
  113. return size;
  114. }
  115. /* Perform the action like invalidate/clean on a range of cache addresses */
  116. static int action_cache_range(enum cache_action action, u32 start_addr,
  117. int64_t size)
  118. {
  119. u32 cline_size;
  120. u32 *action_reg;
  121. enum cache_type type;
  122. action_reg = get_action_reg_range(action);
  123. if (!action_reg)
  124. return -EINVAL;
  125. if (action == INVALIDATE_POU)
  126. type = ICACHE;
  127. else
  128. type = DCACHE;
  129. /* Cache line size is minium size for the cache action */
  130. cline_size = get_cline_size(type);
  131. /* Align start address to cache line boundary */
  132. start_addr &= ~(cline_size - 1);
  133. debug("total size for cache action = %llx\n", size);
  134. do {
  135. writel(start_addr, action_reg);
  136. size -= cline_size;
  137. start_addr += cline_size;
  138. } while (size > cline_size);
  139. /* Make sure cache action is effective for next memory access */
  140. dsb();
  141. isb(); /* Make sure instruction stream sees it */
  142. debug("cache action on range done\n");
  143. return 0;
  144. }
  145. /* Perform the action like invalidate/clean on all cached addresses */
  146. static int action_dcache_all(enum cache_action action)
  147. {
  148. struct dcache_config cache;
  149. u32 *action_reg;
  150. int i, j;
  151. action_reg = get_action_reg_set_ways(action);
  152. if (!action_reg)
  153. return -EINVAL;
  154. clrbits_le32(V7M_PROC_REG_CSSELR, BIT(SEL_I_OR_D));
  155. /* Make sure cache selection is effective for next memory access */
  156. dsb();
  157. get_cache_ways_sets(&cache); /* Get number of ways & sets */
  158. debug("cache: ways= %d, sets= %d\n", cache.ways + 1, cache.sets + 1);
  159. for (i = cache.sets; i >= 0; i--) {
  160. for (j = cache.ways; j >= 0; j--) {
  161. writel((j << WAYS_SHIFT) | (i << SETS_SHIFT),
  162. action_reg);
  163. }
  164. }
  165. /* Make sure cache action is effective for next memory access */
  166. dsb();
  167. isb(); /* Make sure instruction stream sees it */
  168. return 0;
  169. }
  170. void dcache_enable(void)
  171. {
  172. if (dcache_status()) /* return if cache already enabled */
  173. return;
  174. if (action_dcache_all(INVALIDATE_SET_WAY)) {
  175. printf("ERR: D-cache not enabled\n");
  176. return;
  177. }
  178. setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
  179. /* Make sure cache action is effective for next memory access */
  180. dsb();
  181. isb(); /* Make sure instruction stream sees it */
  182. }
  183. void dcache_disable(void)
  184. {
  185. if (!dcache_status())
  186. return;
  187. /* if dcache is enabled-> dcache disable & then flush */
  188. if (action_dcache_all(FLUSH_SET_WAY)) {
  189. printf("ERR: D-cache not flushed\n");
  190. return;
  191. }
  192. clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_DCACHE));
  193. /* Make sure cache action is effective for next memory access */
  194. dsb();
  195. isb(); /* Make sure instruction stream sees it */
  196. }
  197. int dcache_status(void)
  198. {
  199. return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_DCACHE)) != 0;
  200. }
  201. void invalidate_dcache_range(unsigned long start, unsigned long stop)
  202. {
  203. if (action_cache_range(INVALIDATE_POC, start, stop - start)) {
  204. printf("ERR: D-cache not invalidated\n");
  205. return;
  206. }
  207. }
  208. void flush_dcache_range(unsigned long start, unsigned long stop)
  209. {
  210. if (action_cache_range(FLUSH_POC, start, stop - start)) {
  211. printf("ERR: D-cache not flushed\n");
  212. return;
  213. }
  214. }
  215. void flush_dcache_all(void)
  216. {
  217. if (action_dcache_all(FLUSH_SET_WAY)) {
  218. printf("ERR: D-cache not flushed\n");
  219. return;
  220. }
  221. }
  222. void invalidate_dcache_all(void)
  223. {
  224. if (action_dcache_all(INVALIDATE_SET_WAY)) {
  225. printf("ERR: D-cache not invalidated\n");
  226. return;
  227. }
  228. }
  229. #else
  230. void dcache_enable(void)
  231. {
  232. return;
  233. }
  234. void dcache_disable(void)
  235. {
  236. return;
  237. }
  238. int dcache_status(void)
  239. {
  240. return 0;
  241. }
  242. void flush_dcache_all(void)
  243. {
  244. }
  245. void invalidate_dcache_all(void)
  246. {
  247. }
  248. #endif
  249. #ifndef CONFIG_SYS_ICACHE_OFF
  250. void invalidate_icache_all(void)
  251. {
  252. writel(INVAL_ICACHE_POU, V7M_CACHE_REG_ICIALLU);
  253. /* Make sure cache action is effective for next memory access */
  254. dsb();
  255. isb(); /* Make sure instruction stream sees it */
  256. }
  257. void icache_enable(void)
  258. {
  259. if (icache_status())
  260. return;
  261. invalidate_icache_all();
  262. setbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
  263. /* Make sure cache action is effective for next memory access */
  264. dsb();
  265. isb(); /* Make sure instruction stream sees it */
  266. }
  267. int icache_status(void)
  268. {
  269. return (readl(&V7M_SCB->ccr) & BIT(V7M_CCR_ICACHE)) != 0;
  270. }
  271. void icache_disable(void)
  272. {
  273. if (!icache_status())
  274. return;
  275. isb(); /* flush pipeline */
  276. clrbits_le32(&V7M_SCB->ccr, BIT(V7M_CCR_ICACHE));
  277. isb(); /* subsequent instructions fetch see cache disable effect */
  278. }
  279. #else
  280. void icache_enable(void)
  281. {
  282. return;
  283. }
  284. void icache_disable(void)
  285. {
  286. return;
  287. }
  288. int icache_status(void)
  289. {
  290. return 0;
  291. }
  292. #endif
  293. void enable_caches(void)
  294. {
  295. #ifndef CONFIG_SYS_ICACHE_OFF
  296. icache_enable();
  297. #endif
  298. #ifndef CONFIG_SYS_DCACHE_OFF
  299. dcache_enable();
  300. #endif
  301. }