lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * linux/arch/arm/mach-sunxi/sun8i-cci.c
 *
 * Copyright(c) 2013-2015 Allwinnertech Co., Ltd.
 *         http://www.allwinnertech.com
 *
 * Author: sunny <sunny@allwinnertech.com>
 *
 * allwinner sun9i soc platform CCI(Cache Coherent Interconnect) driver.
 * this init version clone from samsung exynos platform.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */
 
#include <linux/init.h>
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/cache.h>
#include <linux/syscore_ops.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <asm/cacheflush.h>
#include <linux/of_address.h>
 
/* Control interface register offsets */
#define CTLR_OVERRIDE_REG    0x0
#define SPEC_CTLR_REG        0x4
#define SECURE_ACCESS_REG    0x8
#define STATUS_REG        0xc
#define IMPRECISE_ERR_REG    0x10
#define PERF_MON_CTRL_REG    0x100
 
/* Slave interface */
#define CCI_C1_SL_IFACE(x)    ((x) + 0x5000)
#define CCI_C0_SL_IFACE(x)    ((x) + 0x4000)
 
/* Slave interface register */
#define SNOOP_CTLR_REG        0x0
 
/* CORE_MISC SFR */
#define BACKBONE_SEL_REG    0x0
#define MDMA_SHARED_CTRL    0x10
#define SSS_SHARED_CTRL        0x20
#define G2D_SHARED_CTRL        0x30
 
static void __iomem *cci_base;
static int cci_enabled __read_mostly;
 
void enable_cci_snoops(unsigned int cluster_id)
{
   void __iomem *control_reg;
   unsigned int value;
 
   if (!cci_enabled)
       return;
 
   /* pr_info("sunxi cci: enable cluster[%d] snoop\n", cluster_id); */
 
   if (cluster_id)
       control_reg = CCI_C1_SL_IFACE(cci_base) + SNOOP_CTLR_REG;
   else
       control_reg = CCI_C0_SL_IFACE(cci_base) + SNOOP_CTLR_REG;
 
   if ((readl(control_reg) & 0x3) == 0x3)
       return;
 
   /* Turn on CCI snoops */
   value = readl(control_reg);
   value |= 0x3;
   writel(value, control_reg);
   dsb();
 
   /* Wait for the dust to settle down */
   while (readl(cci_base + STATUS_REG) & 0x1)
       ;
}
 
void disable_cci_snoops(unsigned int cluster_id)
{
   void __iomem *control_reg;
   unsigned int value;
 
   if (!cci_enabled)
       return;
 
   if (cluster_id)
       control_reg = CCI_C1_SL_IFACE(cci_base) + SNOOP_CTLR_REG;
   else
       control_reg = CCI_C0_SL_IFACE(cci_base) + SNOOP_CTLR_REG;
 
   if (!(readl(control_reg) & 0x3))
       return;
 
   /* Turn off CCI snoops */
   value = readl(control_reg);
   value &= (~0x3);
   writel(value, control_reg);
   dsb();
 
   /* Wait for the dust to settle down */
   while (readl(cci_base + STATUS_REG) & 0x1)
       ;
}
 
/*
 * Use our own MPIDR accessors as the generic ones in asm/cputype.h have
 * __attribute_const__ and we don't want the compiler to assume any
 * constness here.
 */
 
static int read_mpidr(void)
{
   unsigned int id;
   asm volatile ("mrc\tp15, 0, %0, c0, c0, 5" : "=r" (id));
   return id;
}
 
#if defined(CONFIG_PM)
static int cci_status[2];
 
static int get_cci_snoop_status(unsigned int cluster_id)
{
   void __iomem *control_reg;
 
   if (!cci_enabled)
       return 0;
 
   if (cluster_id)
       control_reg = CCI_C1_SL_IFACE(cci_base) + SNOOP_CTLR_REG;
   else
       control_reg = CCI_C0_SL_IFACE(cci_base) + SNOOP_CTLR_REG;
 
   if ((readl(control_reg) & 0x3))
       return 1;
 
   return 0;
}
 
static int cci_suspend(void)
{
   int i;
 
   if (!cci_enabled)
       return 0;
 
   for (i = 0; i < 2; i++)
       cci_status[i] = get_cci_snoop_status(i);
 
   if (cci_status[0] && (!cci_status[1]))
       disable_cci_snoops(0);
 
   return 0;
}
 
static void cci_resume(void)
{
   unsigned int cluster_id = (read_mpidr() >> 8) & 0xf;
 
   if ((cluster_id != 0) && (cluster_id != 1))
       pr_err("[%s]cluster id error! cluster id %d\n",
               __func__, cluster_id);
 
   if (cci_enabled) {
       if (cci_status[cluster_id])
           enable_cci_snoops(cluster_id);
   }
}
 
#else
#define cci_suspend NULL
#define cci_resume  NULL
#endif
 
static struct syscore_ops cci_syscore_ops = {
   .suspend    = cci_suspend,
   .resume        = cci_resume,
};
 
int __init sun8i_cci_init(void)
{
   unsigned int cluster_id = (read_mpidr() >> 8) & 0xf;
   struct device_node *np;
 
#if defined(CONFIG_SUN8I_CCI)
   cci_enabled = 1;
#endif
   if (!cci_enabled) {
       pr_info("sunxi cci is not supported\n");
       goto disabled;
   }
 
   np = of_find_compatible_node(NULL, NULL, "allwinner,sunxi-cci");
   if (!np) {
       pr_err("Can not find sunxi cci device tree\n");
   }
 
   cci_base = of_iomap(np, 0);
   if (!cci_base) {
       pr_err("cci mem base iomap Failed\n");
   }
   of_node_put(np);
 
   enable_cci_snoops(cluster_id);
   register_syscore_ops(&cci_syscore_ops);
 
disabled:
   return 0;
}