blob: 8e5b028c66c7abdb75815c2744c70c1929d3ffb6 [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>
Paul Burtond8b32692017-11-21 11:18:38 -080015#include <asm/system.h>
Paul Burton30374f92015-01-29 01:27:57 +000016
Paul Burton8cb48172016-09-21 11:18:48 +010017DECLARE_GLOBAL_DATA_PTR;
Paul Burton37228622016-05-27 14:28:05 +010018
Paul Burton4baa0ab2016-09-21 11:18:54 +010019static void probe_l2(void)
20{
21#ifdef CONFIG_MIPS_L2_CACHE
22 unsigned long conf2, sl;
23 bool l2c = false;
24
25 if (!(read_c0_config1() & MIPS_CONF_M))
26 return;
27
28 conf2 = read_c0_config2();
29
30 if (__mips_isa_rev >= 6) {
31 l2c = conf2 & MIPS_CONF_M;
32 if (l2c)
33 l2c = read_c0_config3() & MIPS_CONF_M;
34 if (l2c)
35 l2c = read_c0_config4() & MIPS_CONF_M;
36 if (l2c)
37 l2c = read_c0_config5() & MIPS_CONF5_L2C;
38 }
39
40 if (l2c && config_enabled(CONFIG_MIPS_CM)) {
41 gd->arch.l2_line_size = mips_cm_l2_line_size();
42 } else if (l2c) {
43 /* We don't know how to retrieve L2 config on this system */
44 BUG();
45 } else {
46 sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF;
47 gd->arch.l2_line_size = sl ? (2 << sl) : 0;
48 }
49#endif
50}
51
Paul Burton8cb48172016-09-21 11:18:48 +010052void mips_cache_probe(void)
53{
54#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
55 unsigned long conf1, il, dl;
Paul Burton37228622016-05-27 14:28:05 +010056
Paul Burton30374f92015-01-29 01:27:57 +000057 conf1 = read_c0_config1();
Paul Burton8cb48172016-09-21 11:18:48 +010058
Daniel Schwierzecka3ab2ae2016-01-12 21:48:26 +010059 il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF;
Paul Burton8cb48172016-09-21 11:18:48 +010060 dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF;
61
62 gd->arch.l1i_line_size = il ? (2 << il) : 0;
63 gd->arch.l1d_line_size = dl ? (2 << dl) : 0;
64#endif
Paul Burton4baa0ab2016-09-21 11:18:54 +010065 probe_l2();
Paul Burton8cb48172016-09-21 11:18:48 +010066}
67
68static inline unsigned long icache_line_size(void)
69{
70#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
71 return gd->arch.l1i_line_size;
72#else
73 return CONFIG_SYS_ICACHE_LINE_SIZE;
74#endif
Paul Burton30374f92015-01-29 01:27:57 +000075}
76
77static inline unsigned long dcache_line_size(void)
78{
Paul Burton8cb48172016-09-21 11:18:48 +010079#ifdef CONFIG_SYS_CACHE_SIZE_AUTO
80 return gd->arch.l1d_line_size;
81#else
82 return CONFIG_SYS_DCACHE_LINE_SIZE;
83#endif
Paul Burton30374f92015-01-29 01:27:57 +000084}
85
Paul Burton4baa0ab2016-09-21 11:18:54 +010086static inline unsigned long scache_line_size(void)
87{
88#ifdef CONFIG_MIPS_L2_CACHE
89 return gd->arch.l2_line_size;
90#else
91 return 0;
92#endif
93}
94
Paul Burtonfb64cda2016-05-27 14:28:06 +010095#define cache_loop(start, end, lsize, ops...) do { \
96 const void *addr = (const void *)(start & ~(lsize - 1)); \
97 const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \
98 const unsigned int cache_ops[] = { ops }; \
99 unsigned int i; \
100 \
101 for (; addr <= aend; addr += lsize) { \
102 for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \
103 mips_cache(cache_ops[i], addr); \
104 } \
105} while (0)
106
Paul Burton30374f92015-01-29 01:27:57 +0000107void flush_cache(ulong start_addr, ulong size)
108{
109 unsigned long ilsize = icache_line_size();
110 unsigned long dlsize = dcache_line_size();
Paul Burton4baa0ab2016-09-21 11:18:54 +0100111 unsigned long slsize = scache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +0000112
113 /* aend will be miscalculated when size is zero, so we return here */
114 if (size == 0)
115 return;
116
Paul Burton4baa0ab2016-09-21 11:18:54 +0100117 if ((ilsize == dlsize) && !slsize) {
Paul Burton30374f92015-01-29 01:27:57 +0000118 /* flush I-cache & D-cache simultaneously */
Paul Burtonfb64cda2016-05-27 14:28:06 +0100119 cache_loop(start_addr, start_addr + size, ilsize,
120 HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I);
Paul Burton219c2db2017-11-21 11:18:37 -0800121 goto ops_done;
Paul Burton30374f92015-01-29 01:27:57 +0000122 }
123
124 /* flush D-cache */
Paul Burtonfb64cda2016-05-27 14:28:06 +0100125 cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D);
Paul Burton30374f92015-01-29 01:27:57 +0000126
Paul Burton4baa0ab2016-09-21 11:18:54 +0100127 /* flush L2 cache */
128 if (slsize)
129 cache_loop(start_addr, start_addr + size, slsize,
130 HIT_WRITEBACK_INV_SD);
131
Paul Burton30374f92015-01-29 01:27:57 +0000132 /* flush I-cache */
Paul Burtonfb64cda2016-05-27 14:28:06 +0100133 cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I);
Paul Burton219c2db2017-11-21 11:18:37 -0800134
135ops_done:
136 /* ensure cache ops complete before any further memory accesses */
137 sync();
Paul Burtond8b32692017-11-21 11:18:38 -0800138
139 /* ensure the pipeline doesn't contain now-invalid instructions */
140 instruction_hazard_barrier();
Paul Burton30374f92015-01-29 01:27:57 +0000141}
142
143void flush_dcache_range(ulong start_addr, ulong stop)
144{
145 unsigned long lsize = dcache_line_size();
Paul Burton4baa0ab2016-09-21 11:18:54 +0100146 unsigned long slsize = scache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +0000147
Marek Vasutfbb0de02016-01-27 03:13:59 +0100148 /* aend will be miscalculated when size is zero, so we return here */
149 if (start_addr == stop)
150 return;
151
Paul Burtonfb64cda2016-05-27 14:28:06 +0100152 cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D);
Paul Burton4baa0ab2016-09-21 11:18:54 +0100153
154 /* flush L2 cache */
155 if (slsize)
156 cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD);
Paul Burton219c2db2017-11-21 11:18:37 -0800157
158 /* ensure cache ops complete before any further memory accesses */
159 sync();
Paul Burton30374f92015-01-29 01:27:57 +0000160}
161
162void invalidate_dcache_range(ulong start_addr, ulong stop)
163{
164 unsigned long lsize = dcache_line_size();
Paul Burton4baa0ab2016-09-21 11:18:54 +0100165 unsigned long slsize = scache_line_size();
Paul Burton30374f92015-01-29 01:27:57 +0000166
Marek Vasutfbb0de02016-01-27 03:13:59 +0100167 /* aend will be miscalculated when size is zero, so we return here */
168 if (start_addr == stop)
169 return;
170
Paul Burton4baa0ab2016-09-21 11:18:54 +0100171 /* invalidate L2 cache */
172 if (slsize)
173 cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD);
174
Paul Burtona95800e2016-06-09 13:09:51 +0100175 cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D);
Paul Burton219c2db2017-11-21 11:18:37 -0800176
177 /* ensure cache ops complete before any further memory accesses */
178 sync();
Paul Burton30374f92015-01-29 01:27:57 +0000179}