blob: eba7fff316943dd2fd5467857c6ca85060b1502d [file] [log] [blame]
Paul Burton30374f92015-01-29 01:27:57 +00001/*
2 * (C) Copyright 2003
3 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/cacheops.h>
Paul Burton939a2552017-05-12 13:26:11 +020010#ifdef CONFIG_MIPS_L2_CACHE
Paul Burton4baa0ab2016-09-21 11:18:54 +010011#include <asm/cm.h>
Paul Burton939a2552017-05-12 13:26:11 +020012#endif
Paul Burton219c2db2017-11-21 11:18:37 -080013#include <asm/io.h>
Paul Burton30374f92015-01-29 01:27:57 +000014#include <asm/mipsregs.h>
15
Paul Burton8cb48172016-09-21 11:18:48 +010016DECLARE_GLOBAL_DATA_PTR;
Paul Burton37228622016-05-27 14:28:05 +010017
Paul Burton4baa0ab2016-09-21 11:18:54 +010018static void probe_l2(void)
19{
20#ifdef CONFIG_MIPS_L2_CACHE
21 unsigned long conf2, sl;
22 bool l2c = false;
23
24 if (!(read_c0_config1() & MIPS_CONF_M))
25 return;
26
27 conf2 = read_c0_config2();
28
29 if (__mips_isa_rev >= 6) {
30 l2c = conf2 & MIPS_CONF_M;
31 if (l2c)
32 l2c = read_c0_config3() & MIPS_CONF_M;
33 if (l2c)
34 l2c = read_c0_config4() & MIPS_CONF_M;
35 if (l2c)
36 l2c = read_c0_config5() & MIPS_CONF5_L2C;
37 }
38
39 if (l2c && config_enabled(CONFIG_MIPS_CM)) {
40 gd->arch.l2_line_size = mips_cm_l2_line_size();
41 } else if (l2c) {
42 /* We don't know how to retrieve L2 config on this system */
43 BUG();
44 } else {
45 sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF;
46 gd->arch.l2_line_size = sl ? (2 << sl) : 0;
47 }
48#endif
49}
50
Paul Burton8cb48172016-09-21 11:18:48 +010051void mips_cache_probe(void)
52{
53#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
54 unsigned long conf1, il, dl;
Paul Burton37228622016-05-27 14:28:05 +010055
Paul Burton30374f92015-01-29 01:27:57 +000056 conf1 = read_c0_config1();
Paul Burton8cb48172016-09-21 11:18:48 +010057
Daniel Schwierzecka3ab2ae2016-01-12 21:48:26 +010058 il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
Paul Burton8cb48172016-09-21 11:18:48 +010059 dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
60
61 gd->arch.l1i_line_size = il ? (2 << il) : 0;
62 gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
63#endif
Paul Burton4baa0ab2016-09-21 11:18:54 +010064 probe_l2();
Paul Burton8cb48172016-09-21 11:18:48 +010065}
66
67static inline unsigned long icache_line_size(void)
68{
69#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
70 return gd->arch.l1i_line_size;
71#else
72 return CONFIG_SYS_ICACHE_LINE_SIZE;
73#endif
Paul Burton30374f92015-01-29 01:27:57 +000074}
75
76static inline unsigned long dcache_line_size(void)
77{
Paul Burton8cb48172016-09-21 11:18:48 +010078#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
79 return gd->arch.l1d_line_size;
80#else
81 return CONFIG_SYS_DCACHE_LINE_SIZE;
82#endif
Paul Burton30374f92015-01-29 01:27:57 +000083}
84
Paul Burton4baa0ab2016-09-21 11:18:54 +010085static inline unsigned long scache_line_size(void)
86{
87#ifdef CONFIG_MIPS_L2_CACHE
88 return gd->arch.l2_line_size;
89#else
90 return 0;
91#endif
92}
93
Paul Burtonfb64cda2016-05-27 14:28:06 +010094#define cache_loop(start, end, lsize, ops...) do { \
95 const void *addr = (const void *)(start & ~(lsize - 1)); \
96 const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
97 const unsigned int cache_ops[] = { ops }; \
98 unsigned int i; \
99 \
100 for (; addr <= aend; addr += lsize) { \
101 for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
102 mips_cache(cache_ops[i], addr); \
103 } \
104} while (0)
105
Paul Burton30374f92015-01-29 01:27:57 +0000106void flush_cache(ulong start_addr, ulong size)
107{
108 unsigned long ilsize = icache_line_size();
109 unsigned long dlsize = dcache_line_size();
Paul Burton4baa0ab2016-09-21 11:18:54 +0100110 unsigned long slsize = scache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +0000111
112 /* aend will be miscalculated when size is zero, so we return here */
113 if (size == 0)
114 return;
115
Paul Burton4baa0ab2016-09-21 11:18:54 +0100116 if ((ilsize == dlsize) && !slsize) {
Paul Burton30374f92015-01-29 01:27:57 +0000117 /* flush I-cache & D-cache simultaneously */
Paul Burtonfb64cda2016-05-27 14:28:06 +0100118 cache_loop(start_addr, start_addr + size, ilsize,
119 HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
Paul Burton219c2db2017-11-21 11:18:37 -0800120 goto ops_done;
Paul Burton30374f92015-01-29 01:27:57 +0000121 }
122
123 /* flush D-cache */
Paul Burtonfb64cda2016-05-27 14:28:06 +0100124 cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
Paul Burton30374f92015-01-29 01:27:57 +0000125
Paul Burton4baa0ab2016-09-21 11:18:54 +0100126 /* flush L2 cache */
127 if (slsize)
128 cache_loop(start_addr, start_addr + size, slsize,
129 HIT_WRITEBACK_INV_SD);
130
Paul Burton30374f92015-01-29 01:27:57 +0000131 /* flush I-cache */
Paul Burtonfb64cda2016-05-27 14:28:06 +0100132 cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
Paul Burton219c2db2017-11-21 11:18:37 -0800133
134ops_done:
135 /* ensure cache ops complete before any further memory accesses */
136 sync();
Paul Burton30374f92015-01-29 01:27:57 +0000137}
138
139void flush_dcache_range(ulong start_addr, ulong stop)
140{
141 unsigned long lsize = dcache_line_size();
Paul Burton4baa0ab2016-09-21 11:18:54 +0100142 unsigned long slsize = scache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +0000143
Marek Vasutfbb0de02016-01-27 03:13:59 +0100144 /* aend will be miscalculated when size is zero, so we return here */
145 if (start_addr == stop)
146 return;
147
Paul Burtonfb64cda2016-05-27 14:28:06 +0100148 cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
Paul Burton4baa0ab2016-09-21 11:18:54 +0100149
150 /* flush L2 cache */
151 if (slsize)
152 cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
Paul Burton219c2db2017-11-21 11:18:37 -0800153
154 /* ensure cache ops complete before any further memory accesses */
155 sync();
Paul Burton30374f92015-01-29 01:27:57 +0000156}
157
158void invalidate_dcache_range(ulong start_addr, ulong stop)
159{
160 unsigned long lsize = dcache_line_size();
Paul Burton4baa0ab2016-09-21 11:18:54 +0100161 unsigned long slsize = scache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +0000162
Marek Vasutfbb0de02016-01-27 03:13:59 +0100163 /* aend will be miscalculated when size is zero, so we return here */
164 if (start_addr == stop)
165 return;
166
Paul Burton4baa0ab2016-09-21 11:18:54 +0100167 /* invalidate L2 cache */
168 if (slsize)
169 cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
170
Paul Burtona95800e2016-06-09 13:09:51 +0100171 cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
Paul Burton219c2db2017-11-21 11:18:37 -0800172
173 /* ensure cache ops complete before any further memory accesses */
174 sync();
Paul Burton30374f92015-01-29 01:27:57 +0000175}