blob: 7052895bb7209d20980c365ac045b12bbd34e3b4 [file] [log] [blame]
Alexey Brodkin2f16ac92014-02-04 12:56:14 +04001/*
2 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <config.h>
Alexey Brodkin379b3282015-12-14 17:14:46 +03008#include <common.h>
Alexey Brodkinef639e62015-05-18 16:56:26 +03009#include <linux/compiler.h>
10#include <linux/kernel.h>
Alexey Brodkin97a63142017-06-26 11:46:47 +030011#include <linux/log2.h>
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040012#include <asm/arcregs.h>
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +030013#include <asm/arc-bcr.h>
Alexey Brodkin205e7a72015-02-03 13:58:13 +030014#include <asm/cache.h>
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040015
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +030016/*
17 * [ NOTE 1 ]:
18 * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
19 * operation may result in unexpected behavior and data loss even if we flush
20 * data cache right before invalidation. That may happens if we store any context
21 * on stack (like we store BLINK register on stack before function call).
22 * BLINK register is the register where return address is automatically saved
23 * when we do function call with instructions like 'bl'.
24 *
25 * There is the real example:
26 * We may hang in the next code as we store any BLINK register on stack in
27 * invalidate_dcache_all() function.
28 *
29 * void flush_dcache_all() {
30 * __dc_entire_op(OP_FLUSH);
31 * // Other code //
32 * }
33 *
34 * void invalidate_dcache_all() {
35 * __dc_entire_op(OP_INV);
36 * // Other code //
37 * }
38 *
39 * void foo(void) {
40 * flush_dcache_all();
41 * invalidate_dcache_all();
42 * }
43 *
44 * Now let's see what really happens during that code execution:
45 *
46 * foo()
47 * |->> call flush_dcache_all
48 * [return address is saved to BLINK register]
49 * [push BLINK] (save to stack) ![point 1]
50 * |->> call __dc_entire_op(OP_FLUSH)
51 * [return address is saved to BLINK register]
52 * [flush L1 D$]
53 * return [jump to BLINK]
54 * <<------
55 * [other flush_dcache_all code]
56 * [pop BLINK] (get from stack)
57 * return [jump to BLINK]
58 * <<------
59 * |->> call invalidate_dcache_all
60 * [return address is saved to BLINK register]
61 * [push BLINK] (save to stack) ![point 2]
62 * |->> call __dc_entire_op(OP_FLUSH)
63 * [return address is saved to BLINK register]
64 * [invalidate L1 D$] ![point 3]
65 * // Oops!!!
66 * // We lose return address from invalidate_dcache_all function:
67 * // we save it to stack and invalidate L1 D$ after that!
68 * return [jump to BLINK]
69 * <<------
70 * [other invalidate_dcache_all code]
71 * [pop BLINK] (get from stack)
72 * // we don't have this data in L1 dcache as we invalidated it in [point 3]
73 * // so we get it from next memory level (for example DDR memory)
74 * // but in the memory we have value which we save in [point 1], which
75 * // is return address from flush_dcache_all function (instead of
76 * // address from current invalidate_dcache_all function which we
77 * // saved in [point 2] !)
78 * return [jump to BLINK]
79 * <<------
80 * // As BLINK points to invalidate_dcache_all, we call it again and
81 * // loop forever.
82 *
83 * Fortunately we may fix that by using flush & invalidation of D$ with a single
84 * one instruction (instead of flush and invalidation instructions pair) and
85 * enabling force function inline with '__attribute__((always_inline))' gcc
86 * attribute to avoid any function call (and BLINK store) between cache flush
87 * and disable.
88 */
89
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +030090DECLARE_GLOBAL_DATA_PTR;
91
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040092/* Bit values in IC_CTRL */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +030093#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040094
95/* Bit values in DC_CTRL */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +030096#define DC_CTRL_CACHE_DISABLE BIT(0)
97#define DC_CTRL_INV_MODE_FLUSH BIT(6)
98#define DC_CTRL_FLUSH_STATUS BIT(8)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040099
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300100#define OP_INV BIT(0)
101#define OP_FLUSH BIT(1)
102#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300103
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300104/* Bit val in SLC_CONTROL */
105#define SLC_CTRL_DIS 0x001
106#define SLC_CTRL_IM 0x040
107#define SLC_CTRL_BUSY 0x100
108#define SLC_CTRL_RGN_OP_INV 0x200
109
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300110#define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
Alexey Brodkin379b3282015-12-14 17:14:46 +0300111
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300112static inline bool pae_exists(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300113{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300114 /* TODO: should we compare mmu version from BCR and from CONFIG? */
115#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300116 union bcr_mmu_4 mmu4;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300117
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300118 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300119
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300120 if (mmu4.fields.pae)
121 return true;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300122#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300123
124 return false;
125}
126
127static inline bool icache_exists(void)
128{
129 union bcr_di_cache ibcr;
130
131 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
132 return !!ibcr.fields.ver;
133}
134
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300135static inline bool icache_enabled(void)
136{
137 if (!icache_exists())
138 return false;
139
140 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
141}
142
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300143static inline bool dcache_exists(void)
144{
145 union bcr_di_cache dbcr;
146
147 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
148 return !!dbcr.fields.ver;
149}
150
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300151static inline bool dcache_enabled(void)
152{
153 if (!dcache_exists())
154 return false;
155
156 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
157}
158
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300159static inline bool slc_exists(void)
160{
161 if (is_isa_arcv2()) {
162 union bcr_generic sbcr;
163
164 sbcr.word = read_aux_reg(ARC_BCR_SLC);
165 return !!sbcr.fields.ver;
166 }
167
168 return false;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300169}
170
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300171static inline bool ioc_exists(void)
172{
173 if (is_isa_arcv2()) {
174 union bcr_clust_cfg cbcr;
175
176 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
177 return cbcr.fields.c;
178 }
179
180 return false;
181}
182
183static inline bool ioc_enabled(void)
184{
185 /*
186 * We check only CONFIG option instead of IOC HW state check as IOC
187 * must be disabled by default.
188 */
189 if (is_ioc_enabled())
190 return ioc_exists();
191
192 return false;
193}
194
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300195static void __slc_entire_op(const int op)
196{
197 unsigned int ctrl;
198
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300199 if (!slc_exists())
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300200 return;
201
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300202 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
203
204 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
205 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
206 else
207 ctrl |= SLC_CTRL_IM;
208
209 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
210
211 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
212 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
213 else
214 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
215
216 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
217 read_aux_reg(ARC_AUX_SLC_CTRL);
218
219 /* Important to wait for flush to complete */
220 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
221}
222
223static void slc_upper_region_init(void)
224{
225 /*
Eugeniy Paltsev246ba282018-03-21 15:58:58 +0300226 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
227 * only if PAE exists in current HW. So we had to check pae_exist
228 * before using them.
229 */
230 if (!pae_exists())
231 return;
232
233 /*
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300234 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
235 * as we don't use PAE40.
236 */
237 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
238 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
239}
240
241static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
242{
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300243#ifdef CONFIG_ISA_ARCV2
244
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300245 unsigned int ctrl;
246 unsigned long end;
247
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300248 if (!slc_exists())
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300249 return;
250
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300251 /*
252 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
253 * - b'000 (default) is Flush,
254 * - b'001 is Invalidate if CTRL.IM == 0
255 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
256 */
257 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
258
259 /* Don't rely on default value of IM bit */
260 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
261 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
262 else
263 ctrl |= SLC_CTRL_IM;
264
265 if (op & OP_INV)
266 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
267 else
268 ctrl &= ~SLC_CTRL_RGN_OP_INV;
269
270 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
271
272 /*
273 * Lower bits are ignored, no need to clip
274 * END needs to be setup before START (latter triggers the operation)
275 * END can't be same as START, so add (l2_line_sz - 1) to sz
276 */
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300277 end = paddr + sz + gd->arch.slc_line_sz - 1;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300278
279 /*
280 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
281 * are always == 0 as we don't use PAE40, so we only setup lower ones
282 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
283 */
284 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
285 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
286
287 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
288 read_aux_reg(ARC_AUX_SLC_CTRL);
289
290 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300291
292#endif /* CONFIG_ISA_ARCV2 */
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300293}
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300294
295static void arc_ioc_setup(void)
296{
297 /* IOC Aperture start is equal to DDR start */
298 unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
299 /* IOC Aperture size is equal to DDR size */
300 long ap_size = CONFIG_SYS_SDRAM_SIZE;
301
302 flush_n_invalidate_dcache_all();
303
304 if (!is_power_of_2(ap_size) || ap_size < 4096)
305 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
306
307 /*
308 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
309 * so setting 0x11 implies 512M, 0x12 implies 1G...
310 */
311 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
312 order_base_2(ap_size / 1024) - 2);
313
314 /* IOC Aperture start must be aligned to the size of the aperture */
315 if (ap_base % ap_size != 0)
316 panic("IOC Aperture start must be aligned to the size of the aperture");
317
318 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
319 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
320 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
321}
Alexey Brodkinef639e62015-05-18 16:56:26 +0300322
Alexey Brodkin379b3282015-12-14 17:14:46 +0300323static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300324{
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300325#ifdef CONFIG_ISA_ARCV2
326
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300327 union bcr_slc_cfg slc_cfg;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300328
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300329 if (slc_exists()) {
Alexey Brodkin379b3282015-12-14 17:14:46 +0300330 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300331 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300332 }
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300333
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300334#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin379b3282015-12-14 17:14:46 +0300335}
Alexey Brodkin379b3282015-12-14 17:14:46 +0300336
337void read_decode_cache_bcr(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300338{
Alexey Brodkin379b3282015-12-14 17:14:46 +0300339 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300340 union bcr_di_cache ibcr, dbcr;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300341
342 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
343 if (ibcr.fields.ver) {
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300344 gd->arch.l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300345 if (!ic_line_sz)
346 panic("Instruction exists but line length is 0\n");
347 }
348
349 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300350 if (dbcr.fields.ver) {
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300351 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300352 if (!dc_line_sz)
353 panic("Data cache exists but line length is 0\n");
354 }
355
356 if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
357 panic("Instruction and data cache line lengths differ\n");
Alexey Brodkinef639e62015-05-18 16:56:26 +0300358}
359
360void cache_init(void)
361{
Alexey Brodkin379b3282015-12-14 17:14:46 +0300362 read_decode_cache_bcr();
363
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300364 if (is_isa_arcv2())
365 read_decode_cache_bcr_arcv2();
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300366
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300367 if (is_isa_arcv2() && ioc_enabled())
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300368 arc_ioc_setup();
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300369
Eugeniy Paltsev246ba282018-03-21 15:58:58 +0300370 if (is_isa_arcv2() && slc_exists())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300371 slc_upper_region_init();
Alexey Brodkinef639e62015-05-18 16:56:26 +0300372}
373
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400374int icache_status(void)
375{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300376 return icache_enabled();
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400377}
378
379void icache_enable(void)
380{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300381 if (icache_exists())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300382 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
383 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400384}
385
386void icache_disable(void)
387{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300388 if (icache_exists())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300389 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
390 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400391}
392
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300393/* IC supports only invalidation */
394static inline void __ic_entire_invalidate(void)
395{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300396 if (!icache_enabled())
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300397 return;
398
399 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
400 write_aux_reg(ARC_AUX_IC_IVIC, 1);
401 /*
402 * As per ARC HS databook (see chapter 5.3.3.2)
403 * it is required to add 3 NOPs after each write to IC_IVIC.
404 */
405 __builtin_arc_nop();
406 __builtin_arc_nop();
407 __builtin_arc_nop();
408 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
409}
410
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400411void invalidate_icache_all(void)
412{
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300413 __ic_entire_invalidate();
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300414
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300415 if (is_isa_arcv2())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300416 __slc_entire_op(OP_INV);
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300417}
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400418
419int dcache_status(void)
420{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300421 return dcache_enabled();
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400422}
423
424void dcache_enable(void)
425{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300426 if (!dcache_exists())
Igor Guryanovf8cf3d12014-12-24 16:07:07 +0300427 return;
428
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400429 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
430 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
431}
432
433void dcache_disable(void)
434{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300435 if (!dcache_exists())
Igor Guryanovf8cf3d12014-12-24 16:07:07 +0300436 return;
437
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400438 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
439 DC_CTRL_CACHE_DISABLE);
440}
441
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300442/* Common Helper for Line Operations on D-cache */
443static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
444 const int cacheop)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400445{
Alexey Brodkinef639e62015-05-18 16:56:26 +0300446 unsigned int aux_cmd;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300447 int num_lines;
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400448
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300449 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
450 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300451
452 sz += paddr & ~CACHE_LINE_MASK;
453 paddr &= CACHE_LINE_MASK;
454
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300455 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300456
457 while (num_lines-- > 0) {
458#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300459 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300460#endif
461 write_aux_reg(aux_cmd, paddr);
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300462 paddr += gd->arch.l1_line_sz;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300463 }
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400464}
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400465
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300466static void __before_dc_op(const int op)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400467{
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300468 unsigned int ctrl;
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400469
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300470 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400471
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300472 /* IM bit implies flush-n-inv, instead of vanilla inv */
473 if (op == OP_INV)
474 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
475 else
476 ctrl |= DC_CTRL_INV_MODE_FLUSH;
477
478 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400479}
480
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300481static void __after_dc_op(const int op)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300482{
483 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300484 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300485}
486
487static inline void __dc_entire_op(const int cacheop)
488{
489 int aux;
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300490
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300491 if (!dcache_enabled())
Eugeniy Paltsevc877a892018-03-21 15:58:53 +0300492 return;
493
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300494 __before_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300495
496 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
497 aux = ARC_AUX_DC_IVDC;
498 else
499 aux = ARC_AUX_DC_FLSH;
500
501 write_aux_reg(aux, 0x1);
502
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300503 __after_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300504}
505
506static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
507 const int cacheop)
508{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300509 if (!dcache_enabled())
Eugeniy Paltsevc877a892018-03-21 15:58:53 +0300510 return;
511
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300512 __before_dc_op(cacheop);
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300513 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300514 __after_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300515}
Alexey Brodkinef639e62015-05-18 16:56:26 +0300516
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400517void invalidate_dcache_range(unsigned long start, unsigned long end)
518{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300519 if (start >= end)
520 return;
521
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300522 /*
523 * ARCv1 -> call __dc_line_op
524 * ARCv2 && no IOC -> call __dc_line_op; call __slc_rgn_op
525 * ARCv2 && IOC enabled -> nothing
526 */
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300527 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300528 __dc_line_op(start, end - start, OP_INV);
529
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300530 if (is_isa_arcv2() && !ioc_enabled())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300531 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400532}
533
Alexey Brodkinef639e62015-05-18 16:56:26 +0300534void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400535{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300536 if (start >= end)
537 return;
538
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300539 /*
540 * ARCv1 -> call __dc_line_op
541 * ARCv2 && no IOC -> call __dc_line_op; call __slc_rgn_op
542 * ARCv2 && IOC enabled -> nothing
543 */
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300544 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300545 __dc_line_op(start, end - start, OP_FLUSH);
546
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300547 if (is_isa_arcv2() && !ioc_enabled())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300548 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400549}
550
551void flush_cache(unsigned long start, unsigned long size)
552{
553 flush_dcache_range(start, start + size);
554}
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300555
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300556/*
557 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
558 * don't need it in arch/arc code alone (invalidate without flush) we implement
559 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
560 * it's much safer. See [ NOTE 1 ] for more details.
561 */
562void flush_n_invalidate_dcache_all(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300563{
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300564 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300565
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300566 if (is_isa_arcv2())
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300567 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300568}
569
Alexey Brodkinef639e62015-05-18 16:56:26 +0300570void flush_dcache_all(void)
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300571{
Alexey Brodkin2a8382c2016-04-16 15:28:30 +0300572 __dc_entire_op(OP_FLUSH);
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300573
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300574 if (is_isa_arcv2())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300575 __slc_entire_op(OP_FLUSH);
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300576}