Loading...
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/acpi.h>
9#include <linux/amba/bus.h>
10#include <linux/kernel.h>
11#include <linux/device.h>
12#include <linux/platform_device.h>
13#include <linux/io.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/pm_runtime.h>
17#include <linux/property.h>
18#include <linux/clk.h>
19#include <linux/of.h>
20#include <linux/coresight.h>
21
22#include "coresight-priv.h"
23
24#define REPLICATOR_IDFILTER0 0x000
25#define REPLICATOR_IDFILTER1 0x004
26
27DEFINE_CORESIGHT_DEVLIST(replicator_devs, "replicator");
28
29/**
30 * struct replicator_drvdata - specifics associated to a replicator component
31 * @base: memory mapped base address for this component. Also indicates
32 * whether this one is programmable or not.
33 * @atclk: optional clock for the core parts of the replicator.
34 * @csdev: component vitals needed by the framework
35 * @spinlock: serialize enable/disable operations.
36 * @check_idfilter_val: check if the context is lost upon clock removal.
37 */
38struct replicator_drvdata {
39 void __iomem *base;
40 struct clk *atclk;
41 struct coresight_device *csdev;
42 spinlock_t spinlock;
43 bool check_idfilter_val;
44};
45
46static void dynamic_replicator_reset(struct replicator_drvdata *drvdata)
47{
48 struct coresight_device *csdev = drvdata->csdev;
49
50 CS_UNLOCK(drvdata->base);
51
52 if (!coresight_claim_device_unlocked(csdev)) {
53 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
54 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
55 coresight_disclaim_device_unlocked(csdev);
56 }
57
58 CS_LOCK(drvdata->base);
59}
60
61/*
62 * replicator_reset : Reset the replicator configuration to sane values.
63 */
64static inline void replicator_reset(struct replicator_drvdata *drvdata)
65{
66 if (drvdata->base)
67 dynamic_replicator_reset(drvdata);
68}
69
70static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
71 int inport, int outport)
72{
73 int rc = 0;
74 u32 id0val, id1val;
75 struct coresight_device *csdev = drvdata->csdev;
76
77 CS_UNLOCK(drvdata->base);
78
79 id0val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0);
80 id1val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1);
81
82 /*
83 * Some replicator designs lose context when AMBA clocks are removed,
84 * so have a check for this.
85 */
86 if (drvdata->check_idfilter_val && id0val == 0x0 && id1val == 0x0)
87 id0val = id1val = 0xff;
88
89 if (id0val == 0xff && id1val == 0xff)
90 rc = coresight_claim_device_unlocked(csdev);
91
92 if (!rc) {
93 switch (outport) {
94 case 0:
95 id0val = 0x0;
96 break;
97 case 1:
98 id1val = 0x0;
99 break;
100 default:
101 WARN_ON(1);
102 rc = -EINVAL;
103 }
104 }
105
106 /* Ensure that the outport is enabled. */
107 if (!rc) {
108 writel_relaxed(id0val, drvdata->base + REPLICATOR_IDFILTER0);
109 writel_relaxed(id1val, drvdata->base + REPLICATOR_IDFILTER1);
110 }
111
112 CS_LOCK(drvdata->base);
113
114 return rc;
115}
116
117static int replicator_enable(struct coresight_device *csdev, int inport,
118 int outport)
119{
120 int rc = 0;
121 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
122 unsigned long flags;
123 bool first_enable = false;
124
125 spin_lock_irqsave(&drvdata->spinlock, flags);
126 if (atomic_read(&csdev->refcnt[outport]) == 0) {
127 if (drvdata->base)
128 rc = dynamic_replicator_enable(drvdata, inport,
129 outport);
130 if (!rc)
131 first_enable = true;
132 }
133 if (!rc)
134 atomic_inc(&csdev->refcnt[outport]);
135 spin_unlock_irqrestore(&drvdata->spinlock, flags);
136
137 if (first_enable)
138 dev_dbg(&csdev->dev, "REPLICATOR enabled\n");
139 return rc;
140}
141
142static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
143 int inport, int outport)
144{
145 u32 reg;
146 struct coresight_device *csdev = drvdata->csdev;
147
148 switch (outport) {
149 case 0:
150 reg = REPLICATOR_IDFILTER0;
151 break;
152 case 1:
153 reg = REPLICATOR_IDFILTER1;
154 break;
155 default:
156 WARN_ON(1);
157 return;
158 }
159
160 CS_UNLOCK(drvdata->base);
161
162 /* disable the flow of ATB data through port */
163 writel_relaxed(0xff, drvdata->base + reg);
164
165 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
166 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
167 coresight_disclaim_device_unlocked(csdev);
168 CS_LOCK(drvdata->base);
169}
170
171static void replicator_disable(struct coresight_device *csdev, int inport,
172 int outport)
173{
174 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
175 unsigned long flags;
176 bool last_disable = false;
177
178 spin_lock_irqsave(&drvdata->spinlock, flags);
179 if (atomic_dec_return(&csdev->refcnt[outport]) == 0) {
180 if (drvdata->base)
181 dynamic_replicator_disable(drvdata, inport, outport);
182 last_disable = true;
183 }
184 spin_unlock_irqrestore(&drvdata->spinlock, flags);
185
186 if (last_disable)
187 dev_dbg(&csdev->dev, "REPLICATOR disabled\n");
188}
189
190static const struct coresight_ops_link replicator_link_ops = {
191 .enable = replicator_enable,
192 .disable = replicator_disable,
193};
194
195static const struct coresight_ops replicator_cs_ops = {
196 .link_ops = &replicator_link_ops,
197};
198
199static struct attribute *replicator_mgmt_attrs[] = {
200 coresight_simple_reg32(idfilter0, REPLICATOR_IDFILTER0),
201 coresight_simple_reg32(idfilter1, REPLICATOR_IDFILTER1),
202 NULL,
203};
204
205static const struct attribute_group replicator_mgmt_group = {
206 .attrs = replicator_mgmt_attrs,
207 .name = "mgmt",
208};
209
210static const struct attribute_group *replicator_groups[] = {
211 &replicator_mgmt_group,
212 NULL,
213};
214
215static int replicator_probe(struct device *dev, struct resource *res)
216{
217 int ret = 0;
218 struct coresight_platform_data *pdata = NULL;
219 struct replicator_drvdata *drvdata;
220 struct coresight_desc desc = { 0 };
221 void __iomem *base;
222
223 if (is_of_node(dev_fwnode(dev)) &&
224 of_device_is_compatible(dev->of_node, "arm,coresight-replicator"))
225 dev_warn_once(dev,
226 "Uses OBSOLETE CoreSight replicator binding\n");
227
228 desc.name = coresight_alloc_device_name(&replicator_devs, dev);
229 if (!desc.name)
230 return -ENOMEM;
231
232 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
233 if (!drvdata)
234 return -ENOMEM;
235
236 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
237 if (!IS_ERR(drvdata->atclk)) {
238 ret = clk_prepare_enable(drvdata->atclk);
239 if (ret)
240 return ret;
241 }
242
243 /*
244 * Map the device base for dynamic-replicator, which has been
245 * validated by AMBA core
246 */
247 if (res) {
248 base = devm_ioremap_resource(dev, res);
249 if (IS_ERR(base)) {
250 ret = PTR_ERR(base);
251 goto out_disable_clk;
252 }
253 drvdata->base = base;
254 desc.groups = replicator_groups;
255 desc.access = CSDEV_ACCESS_IOMEM(base);
256 }
257
258 if (fwnode_property_present(dev_fwnode(dev),
259 "qcom,replicator-loses-context"))
260 drvdata->check_idfilter_val = true;
261
262 dev_set_drvdata(dev, drvdata);
263
264 pdata = coresight_get_platform_data(dev);
265 if (IS_ERR(pdata)) {
266 ret = PTR_ERR(pdata);
267 goto out_disable_clk;
268 }
269 dev->platform_data = pdata;
270
271 spin_lock_init(&drvdata->spinlock);
272 desc.type = CORESIGHT_DEV_TYPE_LINK;
273 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
274 desc.ops = &replicator_cs_ops;
275 desc.pdata = dev->platform_data;
276 desc.dev = dev;
277
278 drvdata->csdev = coresight_register(&desc);
279 if (IS_ERR(drvdata->csdev)) {
280 ret = PTR_ERR(drvdata->csdev);
281 goto out_disable_clk;
282 }
283
284 replicator_reset(drvdata);
285 pm_runtime_put(dev);
286
287out_disable_clk:
288 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
289 clk_disable_unprepare(drvdata->atclk);
290 return ret;
291}
292
293static int replicator_remove(struct device *dev)
294{
295 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
296
297 coresight_unregister(drvdata->csdev);
298 return 0;
299}
300
301static int static_replicator_probe(struct platform_device *pdev)
302{
303 int ret;
304
305 pm_runtime_get_noresume(&pdev->dev);
306 pm_runtime_set_active(&pdev->dev);
307 pm_runtime_enable(&pdev->dev);
308
309 /* Static replicators do not have programming base */
310 ret = replicator_probe(&pdev->dev, NULL);
311
312 if (ret) {
313 pm_runtime_put_noidle(&pdev->dev);
314 pm_runtime_disable(&pdev->dev);
315 }
316
317 return ret;
318}
319
320static int static_replicator_remove(struct platform_device *pdev)
321{
322 replicator_remove(&pdev->dev);
323 pm_runtime_disable(&pdev->dev);
324 return 0;
325}
326
327#ifdef CONFIG_PM
328static int replicator_runtime_suspend(struct device *dev)
329{
330 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
331
332 if (drvdata && !IS_ERR(drvdata->atclk))
333 clk_disable_unprepare(drvdata->atclk);
334
335 return 0;
336}
337
338static int replicator_runtime_resume(struct device *dev)
339{
340 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
341
342 if (drvdata && !IS_ERR(drvdata->atclk))
343 clk_prepare_enable(drvdata->atclk);
344
345 return 0;
346}
347#endif
348
349static const struct dev_pm_ops replicator_dev_pm_ops = {
350 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
351 replicator_runtime_resume, NULL)
352};
353
354static const struct of_device_id static_replicator_match[] = {
355 {.compatible = "arm,coresight-replicator"},
356 {.compatible = "arm,coresight-static-replicator"},
357 {}
358};
359
360MODULE_DEVICE_TABLE(of, static_replicator_match);
361
362#ifdef CONFIG_ACPI
363static const struct acpi_device_id static_replicator_acpi_ids[] = {
364 {"ARMHC985", 0}, /* ARM CoreSight Static Replicator */
365 {}
366};
367
368MODULE_DEVICE_TABLE(acpi, static_replicator_acpi_ids);
369#endif
370
371static struct platform_driver static_replicator_driver = {
372 .probe = static_replicator_probe,
373 .remove = static_replicator_remove,
374 .driver = {
375 .name = "coresight-static-replicator",
376 /* THIS_MODULE is taken care of by platform_driver_register() */
377 .of_match_table = of_match_ptr(static_replicator_match),
378 .acpi_match_table = ACPI_PTR(static_replicator_acpi_ids),
379 .pm = &replicator_dev_pm_ops,
380 .suppress_bind_attrs = true,
381 },
382};
383
384static int dynamic_replicator_probe(struct amba_device *adev,
385 const struct amba_id *id)
386{
387 return replicator_probe(&adev->dev, &adev->res);
388}
389
390static void dynamic_replicator_remove(struct amba_device *adev)
391{
392 replicator_remove(&adev->dev);
393}
394
395static const struct amba_id dynamic_replicator_ids[] = {
396 CS_AMBA_ID(0x000bb909),
397 CS_AMBA_ID(0x000bb9ec), /* Coresight SoC-600 */
398 {},
399};
400
401MODULE_DEVICE_TABLE(amba, dynamic_replicator_ids);
402
403static struct amba_driver dynamic_replicator_driver = {
404 .drv = {
405 .name = "coresight-dynamic-replicator",
406 .pm = &replicator_dev_pm_ops,
407 .owner = THIS_MODULE,
408 .suppress_bind_attrs = true,
409 },
410 .probe = dynamic_replicator_probe,
411 .remove = dynamic_replicator_remove,
412 .id_table = dynamic_replicator_ids,
413};
414
415static int __init replicator_init(void)
416{
417 int ret;
418
419 ret = platform_driver_register(&static_replicator_driver);
420 if (ret) {
421 pr_info("Error registering platform driver\n");
422 return ret;
423 }
424
425 ret = amba_driver_register(&dynamic_replicator_driver);
426 if (ret) {
427 pr_info("Error registering amba driver\n");
428 platform_driver_unregister(&static_replicator_driver);
429 }
430
431 return ret;
432}
433
434static void __exit replicator_exit(void)
435{
436 platform_driver_unregister(&static_replicator_driver);
437 amba_driver_unregister(&dynamic_replicator_driver);
438}
439
440module_init(replicator_init);
441module_exit(replicator_exit);
442
443MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
444MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
445MODULE_DESCRIPTION("Arm CoreSight Replicator Driver");
446MODULE_LICENSE("GPL v2");
1/* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
2 *
3 * Description: CoreSight Replicator driver
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
19#include <linux/err.h>
20#include <linux/slab.h>
21#include <linux/pm_runtime.h>
22#include <linux/clk.h>
23#include <linux/of.h>
24#include <linux/coresight.h>
25
26#include "coresight-priv.h"
27
28/**
29 * struct replicator_drvdata - specifics associated to a replicator component
30 * @dev: the device entity associated with this component
31 * @atclk: optional clock for the core parts of the replicator.
32 * @csdev: component vitals needed by the framework
33 */
34struct replicator_drvdata {
35 struct device *dev;
36 struct clk *atclk;
37 struct coresight_device *csdev;
38};
39
40static int replicator_enable(struct coresight_device *csdev, int inport,
41 int outport)
42{
43 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
44
45 dev_info(drvdata->dev, "REPLICATOR enabled\n");
46 return 0;
47}
48
49static void replicator_disable(struct coresight_device *csdev, int inport,
50 int outport)
51{
52 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
53
54 dev_info(drvdata->dev, "REPLICATOR disabled\n");
55}
56
57static const struct coresight_ops_link replicator_link_ops = {
58 .enable = replicator_enable,
59 .disable = replicator_disable,
60};
61
62static const struct coresight_ops replicator_cs_ops = {
63 .link_ops = &replicator_link_ops,
64};
65
66static int replicator_probe(struct platform_device *pdev)
67{
68 int ret;
69 struct device *dev = &pdev->dev;
70 struct coresight_platform_data *pdata = NULL;
71 struct replicator_drvdata *drvdata;
72 struct coresight_desc *desc;
73 struct device_node *np = pdev->dev.of_node;
74
75 if (np) {
76 pdata = of_get_coresight_platform_data(dev, np);
77 if (IS_ERR(pdata))
78 return PTR_ERR(pdata);
79 pdev->dev.platform_data = pdata;
80 }
81
82 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
83 if (!drvdata)
84 return -ENOMEM;
85
86 drvdata->dev = &pdev->dev;
87 drvdata->atclk = devm_clk_get(&pdev->dev, "atclk"); /* optional */
88 if (!IS_ERR(drvdata->atclk)) {
89 ret = clk_prepare_enable(drvdata->atclk);
90 if (ret)
91 return ret;
92 }
93 pm_runtime_get_noresume(&pdev->dev);
94 pm_runtime_set_active(&pdev->dev);
95 pm_runtime_enable(&pdev->dev);
96 platform_set_drvdata(pdev, drvdata);
97
98 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
99 if (!desc) {
100 ret = -ENOMEM;
101 goto out_disable_pm;
102 }
103
104 desc->type = CORESIGHT_DEV_TYPE_LINK;
105 desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
106 desc->ops = &replicator_cs_ops;
107 desc->pdata = pdev->dev.platform_data;
108 desc->dev = &pdev->dev;
109 drvdata->csdev = coresight_register(desc);
110 if (IS_ERR(drvdata->csdev)) {
111 ret = PTR_ERR(drvdata->csdev);
112 goto out_disable_pm;
113 }
114
115 pm_runtime_put(&pdev->dev);
116
117 dev_info(dev, "REPLICATOR initialized\n");
118 return 0;
119
120out_disable_pm:
121 if (!IS_ERR(drvdata->atclk))
122 clk_disable_unprepare(drvdata->atclk);
123 pm_runtime_put_noidle(&pdev->dev);
124 pm_runtime_disable(&pdev->dev);
125
126 return ret;
127}
128
129#ifdef CONFIG_PM
130static int replicator_runtime_suspend(struct device *dev)
131{
132 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
133
134 if (drvdata && !IS_ERR(drvdata->atclk))
135 clk_disable_unprepare(drvdata->atclk);
136
137 return 0;
138}
139
140static int replicator_runtime_resume(struct device *dev)
141{
142 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
143
144 if (drvdata && !IS_ERR(drvdata->atclk))
145 clk_prepare_enable(drvdata->atclk);
146
147 return 0;
148}
149#endif
150
151static const struct dev_pm_ops replicator_dev_pm_ops = {
152 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
153 replicator_runtime_resume, NULL)
154};
155
156static const struct of_device_id replicator_match[] = {
157 {.compatible = "arm,coresight-replicator"},
158 {}
159};
160
161static struct platform_driver replicator_driver = {
162 .probe = replicator_probe,
163 .driver = {
164 .name = "coresight-replicator",
165 .of_match_table = replicator_match,
166 .pm = &replicator_dev_pm_ops,
167 .suppress_bind_attrs = true,
168 },
169};
170builtin_platform_driver(replicator_driver);