1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
4 */
5
6 #include <linux/amba/bus.h>
7 #include <linux/clk.h>
8 #include <linux/coresight.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17
18 #include "coresight-priv.h"
19
20 #define REPLICATOR_IDFILTER0 0x000
21 #define REPLICATOR_IDFILTER1 0x004
22
23 /**
24 * struct replicator_state - specifics associated to a replicator component
25 * @base: memory mapped base address for this component.
26 * @dev: the device entity associated with this component
27 * @atclk: optional clock for the core parts of the replicator.
28 * @csdev: component vitals needed by the framework
29 */
30 struct replicator_state {
31 void __iomem *base;
32 struct device *dev;
33 struct clk *atclk;
34 struct coresight_device *csdev;
35 };
36
37 /*
38 * replicator_reset : Reset the replicator configuration to sane values.
39 */
replicator_reset(struct replicator_state * drvdata)40 static void replicator_reset(struct replicator_state *drvdata)
41 {
42 CS_UNLOCK(drvdata->base);
43
44 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
45 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
46
47 CS_LOCK(drvdata->base);
48 }
49
replicator_enable(struct coresight_device * csdev,int inport,int outport)50 static int replicator_enable(struct coresight_device *csdev, int inport,
51 int outport)
52 {
53 u32 reg;
54 struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
55
56 switch (outport) {
57 case 0:
58 reg = REPLICATOR_IDFILTER0;
59 break;
60 case 1:
61 reg = REPLICATOR_IDFILTER1;
62 break;
63 default:
64 WARN_ON(1);
65 return -EINVAL;
66 }
67
68 CS_UNLOCK(drvdata->base);
69
70
71 /* Ensure that the outport is enabled. */
72 writel_relaxed(0x00, drvdata->base + reg);
73 CS_LOCK(drvdata->base);
74
75 dev_info(drvdata->dev, "REPLICATOR enabled\n");
76 return 0;
77 }
78
replicator_disable(struct coresight_device * csdev,int inport,int outport)79 static void replicator_disable(struct coresight_device *csdev, int inport,
80 int outport)
81 {
82 u32 reg;
83 struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
84
85 switch (outport) {
86 case 0:
87 reg = REPLICATOR_IDFILTER0;
88 break;
89 case 1:
90 reg = REPLICATOR_IDFILTER1;
91 break;
92 default:
93 WARN_ON(1);
94 return;
95 }
96
97 CS_UNLOCK(drvdata->base);
98
99 /* disable the flow of ATB data through port */
100 writel_relaxed(0xff, drvdata->base + reg);
101
102 CS_LOCK(drvdata->base);
103
104 dev_info(drvdata->dev, "REPLICATOR disabled\n");
105 }
106
107 static const struct coresight_ops_link replicator_link_ops = {
108 .enable = replicator_enable,
109 .disable = replicator_disable,
110 };
111
112 static const struct coresight_ops replicator_cs_ops = {
113 .link_ops = &replicator_link_ops,
114 };
115
116 #define coresight_replicator_reg(name, offset) \
117 coresight_simple_reg32(struct replicator_state, name, offset)
118
119 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
120 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
121
122 static struct attribute *replicator_mgmt_attrs[] = {
123 &dev_attr_idfilter0.attr,
124 &dev_attr_idfilter1.attr,
125 NULL,
126 };
127
128 static const struct attribute_group replicator_mgmt_group = {
129 .attrs = replicator_mgmt_attrs,
130 .name = "mgmt",
131 };
132
133 static const struct attribute_group *replicator_groups[] = {
134 &replicator_mgmt_group,
135 NULL,
136 };
137
replicator_probe(struct amba_device * adev,const struct amba_id * id)138 static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
139 {
140 int ret;
141 struct device *dev = &adev->dev;
142 struct resource *res = &adev->res;
143 struct coresight_platform_data *pdata = NULL;
144 struct replicator_state *drvdata;
145 struct coresight_desc desc = { 0 };
146 struct device_node *np = adev->dev.of_node;
147 void __iomem *base;
148
149 if (np) {
150 pdata = of_get_coresight_platform_data(dev, np);
151 if (IS_ERR(pdata))
152 return PTR_ERR(pdata);
153 adev->dev.platform_data = pdata;
154 }
155
156 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
157 if (!drvdata)
158 return -ENOMEM;
159
160 drvdata->dev = &adev->dev;
161 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
162 if (!IS_ERR(drvdata->atclk)) {
163 ret = clk_prepare_enable(drvdata->atclk);
164 if (ret)
165 return ret;
166 }
167
168 /* Validity for the resource is already checked by the AMBA core */
169 base = devm_ioremap_resource(dev, res);
170 if (IS_ERR(base))
171 return PTR_ERR(base);
172
173 drvdata->base = base;
174 dev_set_drvdata(dev, drvdata);
175 pm_runtime_put(&adev->dev);
176
177 desc.type = CORESIGHT_DEV_TYPE_LINK;
178 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
179 desc.ops = &replicator_cs_ops;
180 desc.pdata = adev->dev.platform_data;
181 desc.dev = &adev->dev;
182 desc.groups = replicator_groups;
183 drvdata->csdev = coresight_register(&desc);
184
185 if (!IS_ERR(drvdata->csdev)) {
186 replicator_reset(drvdata);
187 return 0;
188 }
189 return PTR_ERR(drvdata->csdev);
190 }
191
192 #ifdef CONFIG_PM
replicator_runtime_suspend(struct device * dev)193 static int replicator_runtime_suspend(struct device *dev)
194 {
195 struct replicator_state *drvdata = dev_get_drvdata(dev);
196
197 if (drvdata && !IS_ERR(drvdata->atclk))
198 clk_disable_unprepare(drvdata->atclk);
199
200 return 0;
201 }
202
replicator_runtime_resume(struct device * dev)203 static int replicator_runtime_resume(struct device *dev)
204 {
205 struct replicator_state *drvdata = dev_get_drvdata(dev);
206
207 if (drvdata && !IS_ERR(drvdata->atclk))
208 clk_prepare_enable(drvdata->atclk);
209
210 return 0;
211 }
212 #endif
213
214 static const struct dev_pm_ops replicator_dev_pm_ops = {
215 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
216 replicator_runtime_resume,
217 NULL)
218 };
219
220 static const struct amba_id replicator_ids[] = {
221 {
222 .id = 0x000bb909,
223 .mask = 0x000fffff,
224 },
225 {
226 /* Coresight SoC-600 */
227 .id = 0x000bb9ec,
228 .mask = 0x000fffff,
229 },
230 { 0, 0 },
231 };
232
233 static struct amba_driver replicator_driver = {
234 .drv = {
235 .name = "coresight-dynamic-replicator",
236 .pm = &replicator_dev_pm_ops,
237 .suppress_bind_attrs = true,
238 },
239 .probe = replicator_probe,
240 .id_table = replicator_ids,
241 };
242 builtin_amba_driver(replicator_driver);
243