blob: fa2a1e77a2c446497af3422d9789e4f340ec350d [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>
8#include <asm/arcregs.h>
9
10/* Bit values in IC_CTRL */
11#define IC_CTRL_CACHE_DISABLE (1 << 0)
12
13/* Bit values in DC_CTRL */
14#define DC_CTRL_CACHE_DISABLE (1 << 0)
15#define DC_CTRL_INV_MODE_FLUSH (1 << 6)
16#define DC_CTRL_FLUSH_STATUS (1 << 8)
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030017#define CACHE_VER_NUM_MASK 0xF
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040018
19int icache_status(void)
20{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030021 /* If no cache in CPU exit immediately */
22 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
23 return 0;
24
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040025 return (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE) !=
26 IC_CTRL_CACHE_DISABLE;
27}
28
29void icache_enable(void)
30{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030031 /* If no cache in CPU exit immediately */
32 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
33 return;
34
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040035 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
36 ~IC_CTRL_CACHE_DISABLE);
37}
38
39void icache_disable(void)
40{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030041 /* If no cache in CPU exit immediately */
42 if (!(read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK))
43 return;
44
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040045 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
46 IC_CTRL_CACHE_DISABLE);
47}
48
49void invalidate_icache_all(void)
50{
51#ifndef CONFIG_SYS_ICACHE_OFF
52 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
53 write_aux_reg(ARC_AUX_IC_IVIC, 1);
54#endif /* CONFIG_SYS_ICACHE_OFF */
55}
56
57int dcache_status(void)
58{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030059 /* If no cache in CPU exit immediately */
60 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
61 return 0;
62
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040063 return (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE) !=
64 DC_CTRL_CACHE_DISABLE;
65}
66
67void dcache_enable(void)
68{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030069 /* If no cache in CPU exit immediately */
70 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
71 return;
72
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040073 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
74 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
75}
76
77void dcache_disable(void)
78{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030079 /* If no cache in CPU exit immediately */
80 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
81 return;
82
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040083 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
84 DC_CTRL_CACHE_DISABLE);
85}
86
87void flush_dcache_all(void)
88{
Igor Guryanovf8cf3d12014-12-24 16:07:07 +030089 /* If no cache in CPU exit immediately */
90 if (!(read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK))
91 return;
92
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040093 /* Do flush of entire cache */
94 write_aux_reg(ARC_AUX_DC_FLSH, 1);
95
96 /* Wait flush end */
97 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
98 ;
99}
100
101#ifndef CONFIG_SYS_DCACHE_OFF
102static void dcache_flush_line(unsigned addr)
103{
Alexey Brodkin5ff40f32015-02-03 13:58:12 +0300104#if (CONFIG_ARC_MMU_VER == 3)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400105 write_aux_reg(ARC_AUX_DC_PTAG, addr);
106#endif
107 write_aux_reg(ARC_AUX_DC_FLDL, addr);
108
109 /* Wait flush end */
110 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
111 ;
112
113#ifndef CONFIG_SYS_ICACHE_OFF
114 /*
115 * Invalidate I$ for addresses range just flushed from D$.
116 * If we try to execute data flushed above it will be valid/correct
117 */
Alexey Brodkin5ff40f32015-02-03 13:58:12 +0300118#if (CONFIG_ARC_MMU_VER == 3)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400119 write_aux_reg(ARC_AUX_IC_PTAG, addr);
120#endif
121 write_aux_reg(ARC_AUX_IC_IVIL, addr);
122#endif /* CONFIG_SYS_ICACHE_OFF */
123}
124#endif /* CONFIG_SYS_DCACHE_OFF */
125
126void flush_dcache_range(unsigned long start, unsigned long end)
127{
128#ifndef CONFIG_SYS_DCACHE_OFF
129 unsigned int addr;
130
131 start = start & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
132 end = end & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
133
134 for (addr = start; addr <= end; addr += CONFIG_SYS_CACHELINE_SIZE)
135 dcache_flush_line(addr);
136#endif /* CONFIG_SYS_DCACHE_OFF */
137}
138
139void invalidate_dcache_range(unsigned long start, unsigned long end)
140{
141#ifndef CONFIG_SYS_DCACHE_OFF
142 unsigned int addr;
143
144 start = start & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
145 end = end & (~(CONFIG_SYS_CACHELINE_SIZE - 1));
146
147 for (addr = start; addr <= end; addr += CONFIG_SYS_CACHELINE_SIZE) {
Alexey Brodkin5ff40f32015-02-03 13:58:12 +0300148#if (CONFIG_ARC_MMU_VER == 3)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400149 write_aux_reg(ARC_AUX_DC_PTAG, addr);
150#endif
151 write_aux_reg(ARC_AUX_DC_IVDL, addr);
152 }
153#endif /* CONFIG_SYS_DCACHE_OFF */
154}
155
156void invalidate_dcache_all(void)
157{
158#ifndef CONFIG_SYS_DCACHE_OFF
159 /* Write 1 to DC_IVDC register triggers invalidation of entire D$ */
160 write_aux_reg(ARC_AUX_DC_IVDC, 1);
161#endif /* CONFIG_SYS_DCACHE_OFF */
162}
163
164void flush_cache(unsigned long start, unsigned long size)
165{
166 flush_dcache_range(start, start + size);
167}