cache-l2x0.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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/err.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/hardware/cache-l2x0.h>
  27. #include "cache-tauros3.h"
  28. #include "cache-aurora-l2.h"
  29. #define CACHE_LINE_SIZE 32
  30. static void __iomem *l2x0_base;
  31. static DEFINE_RAW_SPINLOCK(l2x0_lock);
  32. static u32 l2x0_way_mask; /* Bitmask of active ways */
  33. static u32 l2x0_size;
  34. static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
  35. /* Aurora don't have the cache ID register available, so we have to
  36. * pass it though the device tree */
  37. static u32 cache_id_part_number_from_dt;
  38. struct l2x0_regs l2x0_saved_regs;
  39. struct l2x0_of_data {
  40. void (*setup)(const struct device_node *, u32 *, u32 *);
  41. void (*save)(void);
  42. struct outer_cache_fns outer_cache;
  43. };
  44. static bool of_init = false;
  45. static inline void cache_wait_way(void __iomem *reg, unsigned long mask)
  46. {
  47. /* wait for cache operation by line or way to complete */
  48. while (readl_relaxed(reg) & mask)
  49. cpu_relax();
  50. }
  51. #ifdef CONFIG_CACHE_PL310
  52. static inline void cache_wait(void __iomem *reg, unsigned long mask)
  53. {
  54. /* cache operations by line are atomic on PL310 */
  55. }
  56. #else
  57. #define cache_wait cache_wait_way
  58. #endif
  59. static inline void cache_sync(void)
  60. {
  61. void __iomem *base = l2x0_base;
  62. writel_relaxed(0, base + sync_reg_offset);
  63. cache_wait(base + L2X0_CACHE_SYNC, 1);
  64. }
  65. static inline void l2x0_clean_line(unsigned long addr)
  66. {
  67. void __iomem *base = l2x0_base;
  68. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  69. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  70. }
  71. static inline void l2x0_inv_line(unsigned long addr)
  72. {
  73. void __iomem *base = l2x0_base;
  74. cache_wait(base + L2X0_INV_LINE_PA, 1);
  75. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  76. }
  77. #if defined(CONFIG_PL310_ERRATA_588369) || defined(CONFIG_PL310_ERRATA_727915)
  78. static inline void debug_writel(unsigned long val)
  79. {
  80. if (outer_cache.set_debug)
  81. outer_cache.set_debug(val);
  82. }
  83. static void pl310_set_debug(unsigned long val)
  84. {
  85. writel_relaxed(val, l2x0_base + L2X0_DEBUG_CTRL);
  86. }
  87. #else
  88. /* Optimised out for non-errata case */
  89. static inline void debug_writel(unsigned long val)
  90. {
  91. }
  92. #define pl310_set_debug NULL
  93. #endif
  94. #ifdef CONFIG_PL310_ERRATA_588369
  95. static inline void l2x0_flush_line(unsigned long addr)
  96. {
  97. void __iomem *base = l2x0_base;
  98. /* Clean by PA followed by Invalidate by PA */
  99. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  100. writel_relaxed(addr, base + L2X0_CLEAN_LINE_PA);
  101. cache_wait(base + L2X0_INV_LINE_PA, 1);
  102. writel_relaxed(addr, base + L2X0_INV_LINE_PA);
  103. }
  104. #else
  105. static inline void l2x0_flush_line(unsigned long addr)
  106. {
  107. void __iomem *base = l2x0_base;
  108. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  109. writel_relaxed(addr, base + L2X0_CLEAN_INV_LINE_PA);
  110. }
  111. #endif
  112. static void l2x0_cache_sync(void)
  113. {
  114. unsigned long flags;
  115. raw_spin_lock_irqsave(&l2x0_lock, flags);
  116. cache_sync();
  117. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  118. }
  119. static void __l2x0_flush_all(void)
  120. {
  121. debug_writel(0x03);
  122. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_INV_WAY);
  123. cache_wait_way(l2x0_base + L2X0_CLEAN_INV_WAY, l2x0_way_mask);
  124. cache_sync();
  125. debug_writel(0x00);
  126. }
  127. static void l2x0_flush_all(void)
  128. {
  129. unsigned long flags;
  130. /* clean all ways */
  131. raw_spin_lock_irqsave(&l2x0_lock, flags);
  132. __l2x0_flush_all();
  133. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  134. }
  135. static void l2x0_clean_all(void)
  136. {
  137. unsigned long flags;
  138. /* clean all ways */
  139. raw_spin_lock_irqsave(&l2x0_lock, flags);
  140. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_CLEAN_WAY);
  141. cache_wait_way(l2x0_base + L2X0_CLEAN_WAY, l2x0_way_mask);
  142. cache_sync();
  143. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  144. }
  145. static void l2x0_inv_all(void)
  146. {
  147. unsigned long flags;
  148. /* invalidate all ways */
  149. raw_spin_lock_irqsave(&l2x0_lock, flags);
  150. /* Invalidating when L2 is enabled is a nono */
  151. BUG_ON(readl(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN);
  152. writel_relaxed(l2x0_way_mask, l2x0_base + L2X0_INV_WAY);
  153. cache_wait_way(l2x0_base + L2X0_INV_WAY, l2x0_way_mask);
  154. cache_sync();
  155. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  156. }
  157. static void l2x0_inv_range(unsigned long start, unsigned long end)
  158. {
  159. void __iomem *base = l2x0_base;
  160. unsigned long flags;
  161. raw_spin_lock_irqsave(&l2x0_lock, flags);
  162. if (start & (CACHE_LINE_SIZE - 1)) {
  163. start &= ~(CACHE_LINE_SIZE - 1);
  164. debug_writel(0x03);
  165. l2x0_flush_line(start);
  166. debug_writel(0x00);
  167. start += CACHE_LINE_SIZE;
  168. }
  169. if (end & (CACHE_LINE_SIZE - 1)) {
  170. end &= ~(CACHE_LINE_SIZE - 1);
  171. debug_writel(0x03);
  172. l2x0_flush_line(end);
  173. debug_writel(0x00);
  174. }
  175. while (start < end) {
  176. unsigned long blk_end = start + min(end - start, 4096UL);
  177. while (start < blk_end) {
  178. l2x0_inv_line(start);
  179. start += CACHE_LINE_SIZE;
  180. }
  181. if (blk_end < end) {
  182. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  183. raw_spin_lock_irqsave(&l2x0_lock, flags);
  184. }
  185. }
  186. cache_wait(base + L2X0_INV_LINE_PA, 1);
  187. cache_sync();
  188. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  189. }
  190. static void l2x0_clean_range(unsigned long start, unsigned long end)
  191. {
  192. void __iomem *base = l2x0_base;
  193. unsigned long flags;
  194. if ((end - start) >= l2x0_size) {
  195. l2x0_clean_all();
  196. return;
  197. }
  198. raw_spin_lock_irqsave(&l2x0_lock, flags);
  199. start &= ~(CACHE_LINE_SIZE - 1);
  200. while (start < end) {
  201. unsigned long blk_end = start + min(end - start, 4096UL);
  202. while (start < blk_end) {
  203. l2x0_clean_line(start);
  204. start += CACHE_LINE_SIZE;
  205. }
  206. if (blk_end < end) {
  207. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  208. raw_spin_lock_irqsave(&l2x0_lock, flags);
  209. }
  210. }
  211. cache_wait(base + L2X0_CLEAN_LINE_PA, 1);
  212. cache_sync();
  213. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  214. }
  215. static void l2x0_flush_range(unsigned long start, unsigned long end)
  216. {
  217. void __iomem *base = l2x0_base;
  218. unsigned long flags;
  219. if ((end - start) >= l2x0_size) {
  220. l2x0_flush_all();
  221. return;
  222. }
  223. raw_spin_lock_irqsave(&l2x0_lock, flags);
  224. start &= ~(CACHE_LINE_SIZE - 1);
  225. while (start < end) {
  226. unsigned long blk_end = start + min(end - start, 4096UL);
  227. debug_writel(0x03);
  228. while (start < blk_end) {
  229. l2x0_flush_line(start);
  230. start += CACHE_LINE_SIZE;
  231. }
  232. debug_writel(0x00);
  233. if (blk_end < end) {
  234. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  235. raw_spin_lock_irqsave(&l2x0_lock, flags);
  236. }
  237. }
  238. cache_wait(base + L2X0_CLEAN_INV_LINE_PA, 1);
  239. cache_sync();
  240. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  241. }
  242. static void l2x0_disable(void)
  243. {
  244. unsigned long flags;
  245. raw_spin_lock_irqsave(&l2x0_lock, flags);
  246. __l2x0_flush_all();
  247. writel_relaxed(0, l2x0_base + L2X0_CTRL);
  248. dsb(st);
  249. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  250. }
  251. static void l2x0_unlock(u32 cache_id)
  252. {
  253. int lockregs;
  254. int i;
  255. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  256. case L2X0_CACHE_ID_PART_L310:
  257. lockregs = 8;
  258. break;
  259. case AURORA_CACHE_ID:
  260. lockregs = 4;
  261. break;
  262. default:
  263. /* L210 and unknown types */
  264. lockregs = 1;
  265. break;
  266. }
  267. for (i = 0; i < lockregs; i++) {
  268. writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_D_BASE +
  269. i * L2X0_LOCKDOWN_STRIDE);
  270. writel_relaxed(0x0, l2x0_base + L2X0_LOCKDOWN_WAY_I_BASE +
  271. i * L2X0_LOCKDOWN_STRIDE);
  272. }
  273. }
  274. void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
  275. {
  276. u32 aux;
  277. u32 cache_id;
  278. u32 way_size = 0;
  279. int ways;
  280. int way_size_shift = L2X0_WAY_SIZE_SHIFT;
  281. const char *type;
  282. l2x0_base = base;
  283. if (cache_id_part_number_from_dt)
  284. cache_id = cache_id_part_number_from_dt;
  285. else
  286. cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
  287. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  288. aux &= aux_mask;
  289. aux |= aux_val;
  290. /* Determine the number of ways */
  291. switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
  292. case L2X0_CACHE_ID_PART_L310:
  293. if (aux & (1 << 16))
  294. ways = 16;
  295. else
  296. ways = 8;
  297. type = "L310";
  298. #ifdef CONFIG_PL310_ERRATA_753970
  299. /* Unmapped register. */
  300. sync_reg_offset = L2X0_DUMMY_REG;
  301. #endif
  302. if ((cache_id & L2X0_CACHE_ID_RTL_MASK) <= L2X0_CACHE_ID_RTL_R3P0)
  303. outer_cache.set_debug = pl310_set_debug;
  304. break;
  305. case L2X0_CACHE_ID_PART_L210:
  306. ways = (aux >> 13) & 0xf;
  307. type = "L210";
  308. break;
  309. case AURORA_CACHE_ID:
  310. sync_reg_offset = AURORA_SYNC_REG;
  311. ways = (aux >> 13) & 0xf;
  312. ways = 2 << ((ways + 1) >> 2);
  313. way_size_shift = AURORA_WAY_SIZE_SHIFT;
  314. type = "Aurora";
  315. break;
  316. default:
  317. /* Assume unknown chips have 8 ways */
  318. ways = 8;
  319. type = "L2x0 series";
  320. break;
  321. }
  322. l2x0_way_mask = (1 << ways) - 1;
  323. /*
  324. * L2 cache Size = Way size * Number of ways
  325. */
  326. way_size = (aux & L2X0_AUX_CTRL_WAY_SIZE_MASK) >> 17;
  327. way_size = 1 << (way_size + way_size_shift);
  328. l2x0_size = ways * way_size * SZ_1K;
  329. /*
  330. * Check if l2x0 controller is already enabled.
  331. * If you are booting from non-secure mode
  332. * accessing the below registers will fault.
  333. */
  334. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  335. /* Make sure that I&D is not locked down when starting */
  336. l2x0_unlock(cache_id);
  337. /* l2x0 controller is disabled */
  338. writel_relaxed(aux, l2x0_base + L2X0_AUX_CTRL);
  339. l2x0_inv_all();
  340. /* enable L2X0 */
  341. writel_relaxed(L2X0_CTRL_EN, l2x0_base + L2X0_CTRL);
  342. }
  343. /* Re-read it in case some bits are reserved. */
  344. aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  345. /* Save the value for resuming. */
  346. l2x0_saved_regs.aux_ctrl = aux;
  347. if (!of_init) {
  348. outer_cache.inv_range = l2x0_inv_range;
  349. outer_cache.clean_range = l2x0_clean_range;
  350. outer_cache.flush_range = l2x0_flush_range;
  351. outer_cache.sync = l2x0_cache_sync;
  352. outer_cache.flush_all = l2x0_flush_all;
  353. outer_cache.inv_all = l2x0_inv_all;
  354. outer_cache.disable = l2x0_disable;
  355. }
  356. pr_info("%s cache controller enabled\n", type);
  357. pr_info("l2x0: %d ways, CACHE_ID 0x%08x, AUX_CTRL 0x%08x, Cache size: %d kB\n",
  358. ways, cache_id, aux, l2x0_size >> 10);
  359. }
  360. #ifdef CONFIG_OF
  361. static int l2_wt_override;
  362. /*
  363. * Note that the end addresses passed to Linux primitives are
  364. * noninclusive, while the hardware cache range operations use
  365. * inclusive start and end addresses.
  366. */
  367. static unsigned long calc_range_end(unsigned long start, unsigned long end)
  368. {
  369. /*
  370. * Limit the number of cache lines processed at once,
  371. * since cache range operations stall the CPU pipeline
  372. * until completion.
  373. */
  374. if (end > start + MAX_RANGE_SIZE)
  375. end = start + MAX_RANGE_SIZE;
  376. /*
  377. * Cache range operations can't straddle a page boundary.
  378. */
  379. if (end > PAGE_ALIGN(start+1))
  380. end = PAGE_ALIGN(start+1);
  381. return end;
  382. }
  383. /*
  384. * Make sure 'start' and 'end' reference the same page, as L2 is PIPT
  385. * and range operations only do a TLB lookup on the start address.
  386. */
  387. static void aurora_pa_range(unsigned long start, unsigned long end,
  388. unsigned long offset)
  389. {
  390. unsigned long flags;
  391. raw_spin_lock_irqsave(&l2x0_lock, flags);
  392. writel_relaxed(start, l2x0_base + AURORA_RANGE_BASE_ADDR_REG);
  393. writel_relaxed(end, l2x0_base + offset);
  394. raw_spin_unlock_irqrestore(&l2x0_lock, flags);
  395. cache_sync();
  396. }
  397. static void aurora_inv_range(unsigned long start, unsigned long end)
  398. {
  399. /*
  400. * round start and end adresses up to cache line size
  401. */
  402. start &= ~(CACHE_LINE_SIZE - 1);
  403. end = ALIGN(end, CACHE_LINE_SIZE);
  404. /*
  405. * Invalidate all full cache lines between 'start' and 'end'.
  406. */
  407. while (start < end) {
  408. unsigned long range_end = calc_range_end(start, end);
  409. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  410. AURORA_INVAL_RANGE_REG);
  411. start = range_end;
  412. }
  413. }
  414. static void aurora_clean_range(unsigned long start, unsigned long end)
  415. {
  416. /*
  417. * If L2 is forced to WT, the L2 will always be clean and we
  418. * don't need to do anything here.
  419. */
  420. if (!l2_wt_override) {
  421. start &= ~(CACHE_LINE_SIZE - 1);
  422. end = ALIGN(end, CACHE_LINE_SIZE);
  423. while (start != end) {
  424. unsigned long range_end = calc_range_end(start, end);
  425. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  426. AURORA_CLEAN_RANGE_REG);
  427. start = range_end;
  428. }
  429. }
  430. }
  431. static void aurora_flush_range(unsigned long start, unsigned long end)
  432. {
  433. start &= ~(CACHE_LINE_SIZE - 1);
  434. end = ALIGN(end, CACHE_LINE_SIZE);
  435. while (start != end) {
  436. unsigned long range_end = calc_range_end(start, end);
  437. /*
  438. * If L2 is forced to WT, the L2 will always be clean and we
  439. * just need to invalidate.
  440. */
  441. if (l2_wt_override)
  442. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  443. AURORA_INVAL_RANGE_REG);
  444. else
  445. aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
  446. AURORA_FLUSH_RANGE_REG);
  447. start = range_end;
  448. }
  449. }
  450. /*
  451. * For certain Broadcom SoCs, depending on the address range, different offsets
  452. * need to be added to the address before passing it to L2 for
  453. * invalidation/clean/flush
  454. *
  455. * Section Address Range Offset EMI
  456. * 1 0x00000000 - 0x3FFFFFFF 0x80000000 VC
  457. * 2 0x40000000 - 0xBFFFFFFF 0x40000000 SYS
  458. * 3 0xC0000000 - 0xFFFFFFFF 0x80000000 VC
  459. *
  460. * When the start and end addresses have crossed two different sections, we
  461. * need to break the L2 operation into two, each within its own section.
  462. * For example, if we need to invalidate addresses starts at 0xBFFF0000 and
  463. * ends at 0xC0001000, we need do invalidate 1) 0xBFFF0000 - 0xBFFFFFFF and 2)
  464. * 0xC0000000 - 0xC0001000
  465. *
  466. * Note 1:
  467. * By breaking a single L2 operation into two, we may potentially suffer some
  468. * performance hit, but keep in mind the cross section case is very rare
  469. *
  470. * Note 2:
  471. * We do not need to handle the case when the start address is in
  472. * Section 1 and the end address is in Section 3, since it is not a valid use
  473. * case
  474. *
  475. * Note 3:
  476. * Section 1 in practical terms can no longer be used on rev A2. Because of
  477. * that the code does not need to handle section 1 at all.
  478. *
  479. */
  480. #define BCM_SYS_EMI_START_ADDR 0x40000000UL
  481. #define BCM_VC_EMI_SEC3_START_ADDR 0xC0000000UL
  482. #define BCM_SYS_EMI_OFFSET 0x40000000UL
  483. #define BCM_VC_EMI_OFFSET 0x80000000UL
  484. static inline int bcm_addr_is_sys_emi(unsigned long addr)
  485. {
  486. return (addr >= BCM_SYS_EMI_START_ADDR) &&
  487. (addr < BCM_VC_EMI_SEC3_START_ADDR);
  488. }
  489. static inline unsigned long bcm_l2_phys_addr(unsigned long addr)
  490. {
  491. if (bcm_addr_is_sys_emi(addr))
  492. return addr + BCM_SYS_EMI_OFFSET;
  493. else
  494. return addr + BCM_VC_EMI_OFFSET;
  495. }
  496. static void bcm_inv_range(unsigned long start, unsigned long end)
  497. {
  498. unsigned long new_start, new_end;
  499. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  500. if (unlikely(end <= start))
  501. return;
  502. new_start = bcm_l2_phys_addr(start);
  503. new_end = bcm_l2_phys_addr(end);
  504. /* normal case, no cross section between start and end */
  505. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  506. l2x0_inv_range(new_start, new_end);
  507. return;
  508. }
  509. /* They cross sections, so it can only be a cross from section
  510. * 2 to section 3
  511. */
  512. l2x0_inv_range(new_start,
  513. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  514. l2x0_inv_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  515. new_end);
  516. }
  517. static void bcm_clean_range(unsigned long start, unsigned long end)
  518. {
  519. unsigned long new_start, new_end;
  520. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  521. if (unlikely(end <= start))
  522. return;
  523. if ((end - start) >= l2x0_size) {
  524. l2x0_clean_all();
  525. return;
  526. }
  527. new_start = bcm_l2_phys_addr(start);
  528. new_end = bcm_l2_phys_addr(end);
  529. /* normal case, no cross section between start and end */
  530. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  531. l2x0_clean_range(new_start, new_end);
  532. return;
  533. }
  534. /* They cross sections, so it can only be a cross from section
  535. * 2 to section 3
  536. */
  537. l2x0_clean_range(new_start,
  538. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  539. l2x0_clean_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  540. new_end);
  541. }
  542. static void bcm_flush_range(unsigned long start, unsigned long end)
  543. {
  544. unsigned long new_start, new_end;
  545. BUG_ON(start < BCM_SYS_EMI_START_ADDR);
  546. if (unlikely(end <= start))
  547. return;
  548. if ((end - start) >= l2x0_size) {
  549. l2x0_flush_all();
  550. return;
  551. }
  552. new_start = bcm_l2_phys_addr(start);
  553. new_end = bcm_l2_phys_addr(end);
  554. /* normal case, no cross section between start and end */
  555. if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
  556. l2x0_flush_range(new_start, new_end);
  557. return;
  558. }
  559. /* They cross sections, so it can only be a cross from section
  560. * 2 to section 3
  561. */
  562. l2x0_flush_range(new_start,
  563. bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
  564. l2x0_flush_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
  565. new_end);
  566. }
  567. static void __init l2x0_of_setup(const struct device_node *np,
  568. u32 *aux_val, u32 *aux_mask)
  569. {
  570. u32 data[2] = { 0, 0 };
  571. u32 tag = 0;
  572. u32 dirty = 0;
  573. u32 val = 0, mask = 0;
  574. of_property_read_u32(np, "arm,tag-latency", &tag);
  575. if (tag) {
  576. mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
  577. val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
  578. }
  579. of_property_read_u32_array(np, "arm,data-latency",
  580. data, ARRAY_SIZE(data));
  581. if (data[0] && data[1]) {
  582. mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
  583. L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
  584. val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
  585. ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
  586. }
  587. of_property_read_u32(np, "arm,dirty-latency", &dirty);
  588. if (dirty) {
  589. mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
  590. val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
  591. }
  592. *aux_val &= ~mask;
  593. *aux_val |= val;
  594. *aux_mask &= ~mask;
  595. }
  596. static void __init pl310_of_setup(const struct device_node *np,
  597. u32 *aux_val, u32 *aux_mask)
  598. {
  599. u32 data[3] = { 0, 0, 0 };
  600. u32 tag[3] = { 0, 0, 0 };
  601. u32 filter[2] = { 0, 0 };
  602. of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
  603. if (tag[0] && tag[1] && tag[2])
  604. writel_relaxed(
  605. ((tag[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
  606. ((tag[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
  607. ((tag[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
  608. l2x0_base + L2X0_TAG_LATENCY_CTRL);
  609. of_property_read_u32_array(np, "arm,data-latency",
  610. data, ARRAY_SIZE(data));
  611. if (data[0] && data[1] && data[2])
  612. writel_relaxed(
  613. ((data[0] - 1) << L2X0_LATENCY_CTRL_RD_SHIFT) |
  614. ((data[1] - 1) << L2X0_LATENCY_CTRL_WR_SHIFT) |
  615. ((data[2] - 1) << L2X0_LATENCY_CTRL_SETUP_SHIFT),
  616. l2x0_base + L2X0_DATA_LATENCY_CTRL);
  617. of_property_read_u32_array(np, "arm,filter-ranges",
  618. filter, ARRAY_SIZE(filter));
  619. if (filter[1]) {
  620. writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
  621. l2x0_base + L2X0_ADDR_FILTER_END);
  622. writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L2X0_ADDR_FILTER_EN,
  623. l2x0_base + L2X0_ADDR_FILTER_START);
  624. }
  625. }
  626. static void __init pl310_save(void)
  627. {
  628. u32 l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
  629. L2X0_CACHE_ID_RTL_MASK;
  630. l2x0_saved_regs.tag_latency = readl_relaxed(l2x0_base +
  631. L2X0_TAG_LATENCY_CTRL);
  632. l2x0_saved_regs.data_latency = readl_relaxed(l2x0_base +
  633. L2X0_DATA_LATENCY_CTRL);
  634. l2x0_saved_regs.filter_end = readl_relaxed(l2x0_base +
  635. L2X0_ADDR_FILTER_END);
  636. l2x0_saved_regs.filter_start = readl_relaxed(l2x0_base +
  637. L2X0_ADDR_FILTER_START);
  638. if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
  639. /*
  640. * From r2p0, there is Prefetch offset/control register
  641. */
  642. l2x0_saved_regs.prefetch_ctrl = readl_relaxed(l2x0_base +
  643. L2X0_PREFETCH_CTRL);
  644. /*
  645. * From r3p0, there is Power control register
  646. */
  647. if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
  648. l2x0_saved_regs.pwr_ctrl = readl_relaxed(l2x0_base +
  649. L2X0_POWER_CTRL);
  650. }
  651. }
  652. static void aurora_save(void)
  653. {
  654. l2x0_saved_regs.ctrl = readl_relaxed(l2x0_base + L2X0_CTRL);
  655. l2x0_saved_regs.aux_ctrl = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
  656. }
  657. static void __init tauros3_save(void)
  658. {
  659. l2x0_saved_regs.aux2_ctrl =
  660. readl_relaxed(l2x0_base + TAUROS3_AUX2_CTRL);
  661. l2x0_saved_regs.prefetch_ctrl =
  662. readl_relaxed(l2x0_base + L2X0_PREFETCH_CTRL);
  663. }
  664. static void l2x0_resume(void)
  665. {
  666. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  667. /* restore aux ctrl and enable l2 */
  668. l2x0_unlock(readl_relaxed(l2x0_base + L2X0_CACHE_ID));
  669. writel_relaxed(l2x0_saved_regs.aux_ctrl, l2x0_base +
  670. L2X0_AUX_CTRL);
  671. l2x0_inv_all();
  672. writel_relaxed(L2X0_CTRL_EN, l2x0_base + L2X0_CTRL);
  673. }
  674. }
  675. static void pl310_resume(void)
  676. {
  677. u32 l2x0_revision;
  678. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  679. /* restore pl310 setup */
  680. writel_relaxed(l2x0_saved_regs.tag_latency,
  681. l2x0_base + L2X0_TAG_LATENCY_CTRL);
  682. writel_relaxed(l2x0_saved_regs.data_latency,
  683. l2x0_base + L2X0_DATA_LATENCY_CTRL);
  684. writel_relaxed(l2x0_saved_regs.filter_end,
  685. l2x0_base + L2X0_ADDR_FILTER_END);
  686. writel_relaxed(l2x0_saved_regs.filter_start,
  687. l2x0_base + L2X0_ADDR_FILTER_START);
  688. l2x0_revision = readl_relaxed(l2x0_base + L2X0_CACHE_ID) &
  689. L2X0_CACHE_ID_RTL_MASK;
  690. if (l2x0_revision >= L2X0_CACHE_ID_RTL_R2P0) {
  691. writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
  692. l2x0_base + L2X0_PREFETCH_CTRL);
  693. if (l2x0_revision >= L2X0_CACHE_ID_RTL_R3P0)
  694. writel_relaxed(l2x0_saved_regs.pwr_ctrl,
  695. l2x0_base + L2X0_POWER_CTRL);
  696. }
  697. }
  698. l2x0_resume();
  699. }
  700. static void aurora_resume(void)
  701. {
  702. if (!(readl(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  703. writel_relaxed(l2x0_saved_regs.aux_ctrl,
  704. l2x0_base + L2X0_AUX_CTRL);
  705. writel_relaxed(l2x0_saved_regs.ctrl, l2x0_base + L2X0_CTRL);
  706. }
  707. }
  708. static void tauros3_resume(void)
  709. {
  710. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  711. writel_relaxed(l2x0_saved_regs.aux2_ctrl,
  712. l2x0_base + TAUROS3_AUX2_CTRL);
  713. writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
  714. l2x0_base + L2X0_PREFETCH_CTRL);
  715. }
  716. l2x0_resume();
  717. }
  718. static void __init aurora_broadcast_l2_commands(void)
  719. {
  720. __u32 u;
  721. /* Enable Broadcasting of cache commands to L2*/
  722. __asm__ __volatile__("mrc p15, 1, %0, c15, c2, 0" : "=r"(u));
  723. u |= AURORA_CTRL_FW; /* Set the FW bit */
  724. __asm__ __volatile__("mcr p15, 1, %0, c15, c2, 0\n" : : "r"(u));
  725. isb();
  726. }
  727. static void __init aurora_of_setup(const struct device_node *np,
  728. u32 *aux_val, u32 *aux_mask)
  729. {
  730. u32 val = AURORA_ACR_REPLACEMENT_TYPE_SEMIPLRU;
  731. u32 mask = AURORA_ACR_REPLACEMENT_MASK;
  732. of_property_read_u32(np, "cache-id-part",
  733. &cache_id_part_number_from_dt);
  734. /* Determine and save the write policy */
  735. l2_wt_override = of_property_read_bool(np, "wt-override");
  736. if (l2_wt_override) {
  737. val |= AURORA_ACR_FORCE_WRITE_THRO_POLICY;
  738. mask |= AURORA_ACR_FORCE_WRITE_POLICY_MASK;
  739. }
  740. *aux_val &= ~mask;
  741. *aux_val |= val;
  742. *aux_mask &= ~mask;
  743. }
  744. static const struct l2x0_of_data pl310_data = {
  745. .setup = pl310_of_setup,
  746. .save = pl310_save,
  747. .outer_cache = {
  748. .resume = pl310_resume,
  749. .inv_range = l2x0_inv_range,
  750. .clean_range = l2x0_clean_range,
  751. .flush_range = l2x0_flush_range,
  752. .sync = l2x0_cache_sync,
  753. .flush_all = l2x0_flush_all,
  754. .inv_all = l2x0_inv_all,
  755. .disable = l2x0_disable,
  756. },
  757. };
  758. static const struct l2x0_of_data l2x0_data = {
  759. .setup = l2x0_of_setup,
  760. .save = NULL,
  761. .outer_cache = {
  762. .resume = l2x0_resume,
  763. .inv_range = l2x0_inv_range,
  764. .clean_range = l2x0_clean_range,
  765. .flush_range = l2x0_flush_range,
  766. .sync = l2x0_cache_sync,
  767. .flush_all = l2x0_flush_all,
  768. .inv_all = l2x0_inv_all,
  769. .disable = l2x0_disable,
  770. },
  771. };
  772. static const struct l2x0_of_data aurora_with_outer_data = {
  773. .setup = aurora_of_setup,
  774. .save = aurora_save,
  775. .outer_cache = {
  776. .resume = aurora_resume,
  777. .inv_range = aurora_inv_range,
  778. .clean_range = aurora_clean_range,
  779. .flush_range = aurora_flush_range,
  780. .sync = l2x0_cache_sync,
  781. .flush_all = l2x0_flush_all,
  782. .inv_all = l2x0_inv_all,
  783. .disable = l2x0_disable,
  784. },
  785. };
  786. static const struct l2x0_of_data aurora_no_outer_data = {
  787. .setup = aurora_of_setup,
  788. .save = aurora_save,
  789. .outer_cache = {
  790. .resume = aurora_resume,
  791. },
  792. };
  793. static const struct l2x0_of_data tauros3_data = {
  794. .setup = NULL,
  795. .save = tauros3_save,
  796. /* Tauros3 broadcasts L1 cache operations to L2 */
  797. .outer_cache = {
  798. .resume = tauros3_resume,
  799. },
  800. };
  801. static const struct l2x0_of_data bcm_l2x0_data = {
  802. .setup = pl310_of_setup,
  803. .save = pl310_save,
  804. .outer_cache = {
  805. .resume = pl310_resume,
  806. .inv_range = bcm_inv_range,
  807. .clean_range = bcm_clean_range,
  808. .flush_range = bcm_flush_range,
  809. .sync = l2x0_cache_sync,
  810. .flush_all = l2x0_flush_all,
  811. .inv_all = l2x0_inv_all,
  812. .disable = l2x0_disable,
  813. },
  814. };
  815. static const struct of_device_id l2x0_ids[] __initconst = {
  816. { .compatible = "arm,l210-cache", .data = (void *)&l2x0_data },
  817. { .compatible = "arm,l220-cache", .data = (void *)&l2x0_data },
  818. { .compatible = "arm,pl310-cache", .data = (void *)&pl310_data },
  819. { .compatible = "bcm,bcm11351-a2-pl310-cache", /* deprecated name */
  820. .data = (void *)&bcm_l2x0_data},
  821. { .compatible = "brcm,bcm11351-a2-pl310-cache",
  822. .data = (void *)&bcm_l2x0_data},
  823. { .compatible = "marvell,aurora-outer-cache",
  824. .data = (void *)&aurora_with_outer_data},
  825. { .compatible = "marvell,aurora-system-cache",
  826. .data = (void *)&aurora_no_outer_data},
  827. { .compatible = "marvell,tauros3-cache",
  828. .data = (void *)&tauros3_data },
  829. {}
  830. };
  831. int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
  832. {
  833. struct device_node *np;
  834. const struct l2x0_of_data *data;
  835. struct resource res;
  836. np = of_find_matching_node(NULL, l2x0_ids);
  837. if (!np)
  838. return -ENODEV;
  839. if (of_address_to_resource(np, 0, &res))
  840. return -ENODEV;
  841. l2x0_base = ioremap(res.start, resource_size(&res));
  842. if (!l2x0_base)
  843. return -ENOMEM;
  844. l2x0_saved_regs.phy_base = res.start;
  845. data = of_match_node(l2x0_ids, np)->data;
  846. /* L2 configuration can only be changed if the cache is disabled */
  847. if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN)) {
  848. if (data->setup)
  849. data->setup(np, &aux_val, &aux_mask);
  850. /* For aurora cache in no outer mode select the
  851. * correct mode using the coprocessor*/
  852. if (data == &aurora_no_outer_data)
  853. aurora_broadcast_l2_commands();
  854. }
  855. if (data->save)
  856. data->save();
  857. of_init = true;
  858. memcpy(&outer_cache, &data->outer_cache, sizeof(outer_cache));
  859. l2x0_init(l2x0_base, aux_val, aux_mask);
  860. return 0;
  861. }
  862. #endif