blob: 30f045a864b68c44524005db417b326812205bd8 [file] [log] [blame]
Alexey Brodkin3a59d912014-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>
8#include <asm/arcregs.h>
Alexey Brodkin6b95cca2015-02-03 13:58:13 +03009#include <asm/cache.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040010
11/* Bit values in IC_CTRL */
12#define IC_CTRL_CACHE_DISABLE (1 << 0)
13
14/* Bit values in DC_CTRL */
15#define DC_CTRL_CACHE_DISABLE (1 << 0)
16#define DC_CTRL_INV_MODE_FLUSH (1 << 6)
17#define DC_CTRL_FLUSH_STATUS (1 << 8)
Igor Guryanovbd889f92014-12-24 16:07:07 +030018#define CACHE_VER_NUM_MASK 0xF
Alexey Brodkin3a59d912014-02-04 12:56:14 +040019
20int icache_status(void)
21{
Igor Guryanovbd889f92014-12-24 16:07:07 +030022 /* If no cache in CPU exit immediately */
23 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
24 return 0;
25
Alexey Brodkin3a59d912014-02-04 12:56:14 +040026 return (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE) !=
27 IC_CTRL_CACHE_DISABLE;
28}
29
30void icache_enable(void)
31{
Igor Guryanovbd889f92014-12-24 16:07:07 +030032 /* If no cache in CPU exit immediately */
33 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
34 return;
35
Alexey Brodkin3a59d912014-02-04 12:56:14 +040036 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
37 ~IC_CTRL_CACHE_DISABLE);
38}
39
40void icache_disable(void)
41{
Igor Guryanovbd889f92014-12-24 16:07:07 +030042 /* If no cache in CPU exit immediately */
43 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
44 return;
45
Alexey Brodkin3a59d912014-02-04 12:56:14 +040046 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
47 IC_CTRL_CACHE_DISABLE);
48}
49
50void invalidate_icache_all(void)
51{
Alexey Brodkin35221a62015-03-27 12:47:29 +030052 /* If no cache in CPU exit immediately */
53 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
54 return;
55
Alexey Brodkin3a59d912014-02-04 12:56:14 +040056 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
57 write_aux_reg(ARC_AUX_IC_IVIC, 1);
Alexey Brodkin3a59d912014-02-04 12:56:14 +040058}
59
60int dcache_status(void)
61{
Igor Guryanovbd889f92014-12-24 16:07:07 +030062 /* If no cache in CPU exit immediately */
63 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
64 return 0;
65
Alexey Brodkin3a59d912014-02-04 12:56:14 +040066 return (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE) !=
67 DC_CTRL_CACHE_DISABLE;
68}
69
70void dcache_enable(void)
71{
Igor Guryanovbd889f92014-12-24 16:07:07 +030072 /* If no cache in CPU exit immediately */
73 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
74 return;
75
Alexey Brodkin3a59d912014-02-04 12:56:14 +040076 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
77 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
78}
79
80void dcache_disable(void)
81{
Igor Guryanovbd889f92014-12-24 16:07:07 +030082 /* If no cache in CPU exit immediately */
83 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
84 return;
85
Alexey Brodkin3a59d912014-02-04 12:56:14 +040086 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
87 DC_CTRL_CACHE_DISABLE);
88}
89
90void flush_dcache_all(void)
91{
Igor Guryanovbd889f92014-12-24 16:07:07 +030092 /* If no cache in CPU exit immediately */
93 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
94 return;
95
Alexey Brodkin3a59d912014-02-04 12:56:14 +040096 /* Do flush of entire cache */
97 write_aux_reg(ARC_AUX_DC_FLSH, 1);
98
99 /* Wait flush end */
100 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
101 ;
102}
103
104#ifndef CONFIG_SYS_DCACHE_OFF
105static void dcache_flush_line(unsigned addr)
106{
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300107#if (CONFIG_ARC_MMU_VER == 3)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400108 write_aux_reg(ARC_AUX_DC_PTAG, addr);
109#endif
110 write_aux_reg(ARC_AUX_DC_FLDL, addr);
111
112 /* Wait flush end */
113 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
114 ;
115
116#ifndef CONFIG_SYS_ICACHE_OFF
117 /*
118 * Invalidate I$ for addresses range just flushed from D$.
119 * If we try to execute data flushed above it will be valid/correct
120 */
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300121#if (CONFIG_ARC_MMU_VER == 3)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400122 write_aux_reg(ARC_AUX_IC_PTAG, addr);
123#endif
124 write_aux_reg(ARC_AUX_IC_IVIL, addr);
125#endif /* CONFIG_SYS_ICACHE_OFF */
126}
127#endif /* CONFIG_SYS_DCACHE_OFF */
128
129void flush_dcache_range(unsigned long start, unsigned long end)
130{
131#ifndef CONFIG_SYS_DCACHE_OFF
132 unsigned int addr;
133
134 start = start & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
135 end = end & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
136
137 for (addr = start; addr <= end; addr += CONFIG_SYS_CACHELINE_SIZE)
138 dcache_flush_line(addr);
139#endif /* CONFIG_SYS_DCACHE_OFF */
140}
141
142void invalidate_dcache_range(unsigned long start, unsigned long end)
143{
144#ifndef CONFIG_SYS_DCACHE_OFF
145 unsigned int addr;
146
147 start = start & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
148 end = end & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
149
150 for (addr = start; addr <= end; addr += CONFIG_SYS_CACHELINE_SIZE) {
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300151#if (CONFIG_ARC_MMU_VER == 3)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400152 write_aux_reg(ARC_AUX_DC_PTAG, addr);
153#endif
154 write_aux_reg(ARC_AUX_DC_IVDL, addr);
155 }
156#endif /* CONFIG_SYS_DCACHE_OFF */
157}
158
159void invalidate_dcache_all(void)
160{
Alexey Brodkin35221a62015-03-27 12:47:29 +0300161 /* If no cache in CPU exit immediately */
162 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
163 return;
164
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400165 /* Write 1 to DC_IVDC register triggers invalidation of entire D$ */
166 write_aux_reg(ARC_AUX_DC_IVDC, 1);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400167}
168
169void flush_cache(unsigned long start, unsigned long size)
170{
171 flush_dcache_range(start, start + size);
172}