7e051455792023d07e355f5305fe0ec9b7275b94
[linux-2.6-microblaze.git] / drivers / hwtracing / coresight / coresight-replicator.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
4  *
5  * Description: CoreSight Replicator driver
6  */
7
8 #include <linux/amba/bus.h>
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/platform_device.h>
12 #include <linux/io.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/clk.h>
17 #include <linux/of.h>
18 #include <linux/coresight.h>
19
20 #include "coresight-priv.h"
21
22 #define REPLICATOR_IDFILTER0            0x000
23 #define REPLICATOR_IDFILTER1            0x004
24
25 /**
26  * struct replicator_drvdata - specifics associated to a replicator component
27  * @base:       memory mapped base address for this component. Also indicates
28  *              whether this one is programmable or not.
29  * @atclk:      optional clock for the core parts of the replicator.
30  * @csdev:      component vitals needed by the framework
31  */
32 struct replicator_drvdata {
33         void __iomem            *base;
34         struct clk              *atclk;
35         struct coresight_device *csdev;
36 };
37
38 static void dynamic_replicator_reset(struct replicator_drvdata *drvdata)
39 {
40         CS_UNLOCK(drvdata->base);
41
42         if (!coresight_claim_device_unlocked(drvdata->base)) {
43                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
44                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
45                 coresight_disclaim_device_unlocked(drvdata->base);
46         }
47
48         CS_LOCK(drvdata->base);
49 }
50
51 /*
52  * replicator_reset : Reset the replicator configuration to sane values.
53  */
54 static inline void replicator_reset(struct replicator_drvdata *drvdata)
55 {
56         if (drvdata->base)
57                 dynamic_replicator_reset(drvdata);
58 }
59
60 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
61                                      int inport, int outport)
62 {
63         int rc = 0;
64         u32 reg;
65
66         switch (outport) {
67         case 0:
68                 reg = REPLICATOR_IDFILTER0;
69                 break;
70         case 1:
71                 reg = REPLICATOR_IDFILTER1;
72                 break;
73         default:
74                 WARN_ON(1);
75                 return -EINVAL;
76         }
77
78         CS_UNLOCK(drvdata->base);
79
80         if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
81             (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
82                 rc = coresight_claim_device_unlocked(drvdata->base);
83
84         /* Ensure that the outport is enabled. */
85         if (!rc)
86                 writel_relaxed(0x00, drvdata->base + reg);
87         CS_LOCK(drvdata->base);
88
89         return rc;
90 }
91
92 static int replicator_enable(struct coresight_device *csdev, int inport,
93                              int outport)
94 {
95         int rc = 0;
96         struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
97
98         if (drvdata->base)
99                 rc = dynamic_replicator_enable(drvdata, inport, outport);
100         if (!rc)
101                 dev_dbg(&csdev->dev, "REPLICATOR enabled\n");
102         return rc;
103 }
104
105 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
106                                        int inport, int outport)
107 {
108         u32 reg;
109
110         switch (outport) {
111         case 0:
112                 reg = REPLICATOR_IDFILTER0;
113                 break;
114         case 1:
115                 reg = REPLICATOR_IDFILTER1;
116                 break;
117         default:
118                 WARN_ON(1);
119                 return;
120         }
121
122         CS_UNLOCK(drvdata->base);
123
124         /* disable the flow of ATB data through port */
125         writel_relaxed(0xff, drvdata->base + reg);
126
127         if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
128             (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
129                 coresight_disclaim_device_unlocked(drvdata->base);
130         CS_LOCK(drvdata->base);
131 }
132
133 static void replicator_disable(struct coresight_device *csdev, int inport,
134                                int outport)
135 {
136         struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
137
138         if (drvdata->base)
139                 dynamic_replicator_disable(drvdata, inport, outport);
140         dev_dbg(&csdev->dev, "REPLICATOR disabled\n");
141 }
142
143 static const struct coresight_ops_link replicator_link_ops = {
144         .enable         = replicator_enable,
145         .disable        = replicator_disable,
146 };
147
148 static const struct coresight_ops replicator_cs_ops = {
149         .link_ops       = &replicator_link_ops,
150 };
151
152 #define coresight_replicator_reg(name, offset) \
153         coresight_simple_reg32(struct replicator_drvdata, name, offset)
154
155 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
156 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
157
158 static struct attribute *replicator_mgmt_attrs[] = {
159         &dev_attr_idfilter0.attr,
160         &dev_attr_idfilter1.attr,
161         NULL,
162 };
163
164 static const struct attribute_group replicator_mgmt_group = {
165         .attrs = replicator_mgmt_attrs,
166         .name = "mgmt",
167 };
168
169 static const struct attribute_group *replicator_groups[] = {
170         &replicator_mgmt_group,
171         NULL,
172 };
173
174 static int replicator_probe(struct device *dev, struct resource *res)
175 {
176         int ret = 0;
177         struct coresight_platform_data *pdata = NULL;
178         struct replicator_drvdata *drvdata;
179         struct coresight_desc desc = { 0 };
180         struct device_node *np = dev->of_node;
181         void __iomem *base;
182
183         if (np) {
184                 pdata = of_get_coresight_platform_data(dev, np);
185                 if (IS_ERR(pdata))
186                         return PTR_ERR(pdata);
187                 dev->platform_data = pdata;
188         }
189
190         if (is_of_node(dev_fwnode(dev)) &&
191             of_device_is_compatible(dev->of_node, "arm,coresight-replicator"))
192                 pr_warn_once("Uses OBSOLETE CoreSight replicator binding\n");
193
194         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
195         if (!drvdata)
196                 return -ENOMEM;
197
198         drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
199         if (!IS_ERR(drvdata->atclk)) {
200                 ret = clk_prepare_enable(drvdata->atclk);
201                 if (ret)
202                         return ret;
203         }
204
205         /*
206          * Map the device base for dynamic-replicator, which has been
207          * validated by AMBA core
208          */
209         if (res) {
210                 base = devm_ioremap_resource(dev, res);
211                 if (IS_ERR(base)) {
212                         ret = PTR_ERR(base);
213                         goto out_disable_clk;
214                 }
215                 drvdata->base = base;
216                 desc.groups = replicator_groups;
217         }
218
219         dev_set_drvdata(dev, drvdata);
220
221         desc.type = CORESIGHT_DEV_TYPE_LINK;
222         desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
223         desc.ops = &replicator_cs_ops;
224         desc.pdata = dev->platform_data;
225         desc.dev = dev;
226         drvdata->csdev = coresight_register(&desc);
227         if (IS_ERR(drvdata->csdev)) {
228                 ret = PTR_ERR(drvdata->csdev);
229                 goto out_disable_clk;
230         }
231
232         replicator_reset(drvdata);
233         pm_runtime_put(dev);
234
235 out_disable_clk:
236         if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
237                 clk_disable_unprepare(drvdata->atclk);
238         return ret;
239 }
240
241 static int static_replicator_probe(struct platform_device *pdev)
242 {
243         int ret;
244
245         pm_runtime_get_noresume(&pdev->dev);
246         pm_runtime_set_active(&pdev->dev);
247         pm_runtime_enable(&pdev->dev);
248
249         /* Static replicators do not have programming base */
250         ret = replicator_probe(&pdev->dev, NULL);
251
252         if (ret) {
253                 pm_runtime_put_noidle(&pdev->dev);
254                 pm_runtime_disable(&pdev->dev);
255         }
256
257         return ret;
258 }
259
260 #ifdef CONFIG_PM
261 static int replicator_runtime_suspend(struct device *dev)
262 {
263         struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
264
265         if (drvdata && !IS_ERR(drvdata->atclk))
266                 clk_disable_unprepare(drvdata->atclk);
267
268         return 0;
269 }
270
271 static int replicator_runtime_resume(struct device *dev)
272 {
273         struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
274
275         if (drvdata && !IS_ERR(drvdata->atclk))
276                 clk_prepare_enable(drvdata->atclk);
277
278         return 0;
279 }
280 #endif
281
282 static const struct dev_pm_ops replicator_dev_pm_ops = {
283         SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
284                            replicator_runtime_resume, NULL)
285 };
286
287 static const struct of_device_id static_replicator_match[] = {
288         {.compatible = "arm,coresight-replicator"},
289         {.compatible = "arm,coresight-static-replicator"},
290         {}
291 };
292
293 static struct platform_driver static_replicator_driver = {
294         .probe          = static_replicator_probe,
295         .driver         = {
296                 .name   = "coresight-static-replicator",
297                 .of_match_table = static_replicator_match,
298                 .pm     = &replicator_dev_pm_ops,
299                 .suppress_bind_attrs = true,
300         },
301 };
302 builtin_platform_driver(static_replicator_driver);
303
304 static int dynamic_replicator_probe(struct amba_device *adev,
305                                     const struct amba_id *id)
306 {
307         return replicator_probe(&adev->dev, &adev->res);
308 }
309
310 static const struct amba_id dynamic_replicator_ids[] = {
311         {
312                 .id     = 0x000bb909,
313                 .mask   = 0x000fffff,
314         },
315         {
316                 /* Coresight SoC-600 */
317                 .id     = 0x000bb9ec,
318                 .mask   = 0x000fffff,
319         },
320         { 0, 0 },
321 };
322
323 static struct amba_driver dynamic_replicator_driver = {
324         .drv = {
325                 .name   = "coresight-dynamic-replicator",
326                 .pm     = &replicator_dev_pm_ops,
327                 .suppress_bind_attrs = true,
328         },
329         .probe          = dynamic_replicator_probe,
330         .id_table       = dynamic_replicator_ids,
331 };
332 builtin_amba_driver(dynamic_replicator_driver);