1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
5 * Description: CoreSight Replicator driver
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>
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>
20 #include <linux/coresight.h>
22 #include "coresight-priv.h"
24 #define REPLICATOR_IDFILTER0 0x000
25 #define REPLICATOR_IDFILTER1 0x004
27 DEFINE_CORESIGHT_DEVLIST(replicator_devs, "replicator");
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.
38 struct replicator_drvdata {
41 struct coresight_device *csdev;
43 bool check_idfilter_val;
46 static void dynamic_replicator_reset(struct replicator_drvdata *drvdata)
48 struct coresight_device *csdev = drvdata->csdev;
50 CS_UNLOCK(drvdata->base);
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);
58 CS_LOCK(drvdata->base);
62 * replicator_reset : Reset the replicator configuration to sane values.
64 static inline void replicator_reset(struct replicator_drvdata *drvdata)
67 dynamic_replicator_reset(drvdata);
70 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
71 int inport, int outport)
75 struct coresight_device *csdev = drvdata->csdev;
77 CS_UNLOCK(drvdata->base);
79 id0val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0);
80 id1val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1);
83 * Some replicator designs lose context when AMBA clocks are removed,
84 * so have a check for this.
86 if (drvdata->check_idfilter_val && id0val == 0x0 && id1val == 0x0)
87 id0val = id1val = 0xff;
89 if (id0val == 0xff && id1val == 0xff)
90 rc = coresight_claim_device_unlocked(csdev);
106 /* Ensure that the outport is enabled. */
108 writel_relaxed(id0val, drvdata->base + REPLICATOR_IDFILTER0);
109 writel_relaxed(id1val, drvdata->base + REPLICATOR_IDFILTER1);
112 CS_LOCK(drvdata->base);
117 static int replicator_enable(struct coresight_device *csdev,
118 struct coresight_connection *in,
119 struct coresight_connection *out)
122 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
124 bool first_enable = false;
126 spin_lock_irqsave(&drvdata->spinlock, flags);
127 if (atomic_read(&out->src_refcnt) == 0) {
129 rc = dynamic_replicator_enable(drvdata, in->dest_port,
135 atomic_inc(&out->src_refcnt);
136 spin_unlock_irqrestore(&drvdata->spinlock, flags);
139 dev_dbg(&csdev->dev, "REPLICATOR enabled\n");
143 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
144 int inport, int outport)
147 struct coresight_device *csdev = drvdata->csdev;
151 reg = REPLICATOR_IDFILTER0;
154 reg = REPLICATOR_IDFILTER1;
161 CS_UNLOCK(drvdata->base);
163 /* disable the flow of ATB data through port */
164 writel_relaxed(0xff, drvdata->base + reg);
166 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
167 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
168 coresight_disclaim_device_unlocked(csdev);
169 CS_LOCK(drvdata->base);
172 static void replicator_disable(struct coresight_device *csdev,
173 struct coresight_connection *in,
174 struct coresight_connection *out)
176 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
178 bool last_disable = false;
180 spin_lock_irqsave(&drvdata->spinlock, flags);
181 if (atomic_dec_return(&out->src_refcnt) == 0) {
183 dynamic_replicator_disable(drvdata, in->dest_port,
187 spin_unlock_irqrestore(&drvdata->spinlock, flags);
190 dev_dbg(&csdev->dev, "REPLICATOR disabled\n");
193 static const struct coresight_ops_link replicator_link_ops = {
194 .enable = replicator_enable,
195 .disable = replicator_disable,
198 static const struct coresight_ops replicator_cs_ops = {
199 .link_ops = &replicator_link_ops,
202 static struct attribute *replicator_mgmt_attrs[] = {
203 coresight_simple_reg32(idfilter0, REPLICATOR_IDFILTER0),
204 coresight_simple_reg32(idfilter1, REPLICATOR_IDFILTER1),
208 static const struct attribute_group replicator_mgmt_group = {
209 .attrs = replicator_mgmt_attrs,
213 static const struct attribute_group *replicator_groups[] = {
214 &replicator_mgmt_group,
218 static int replicator_probe(struct device *dev, struct resource *res)
221 struct coresight_platform_data *pdata = NULL;
222 struct replicator_drvdata *drvdata;
223 struct coresight_desc desc = { 0 };
226 if (is_of_node(dev_fwnode(dev)) &&
227 of_device_is_compatible(dev->of_node, "arm,coresight-replicator"))
229 "Uses OBSOLETE CoreSight replicator binding\n");
231 desc.name = coresight_alloc_device_name(&replicator_devs, dev);
235 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
239 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
240 if (!IS_ERR(drvdata->atclk)) {
241 ret = clk_prepare_enable(drvdata->atclk);
247 * Map the device base for dynamic-replicator, which has been
248 * validated by AMBA core
251 base = devm_ioremap_resource(dev, res);
254 goto out_disable_clk;
256 drvdata->base = base;
257 desc.groups = replicator_groups;
258 desc.access = CSDEV_ACCESS_IOMEM(base);
261 if (fwnode_property_present(dev_fwnode(dev),
262 "qcom,replicator-loses-context"))
263 drvdata->check_idfilter_val = true;
265 dev_set_drvdata(dev, drvdata);
267 pdata = coresight_get_platform_data(dev);
269 ret = PTR_ERR(pdata);
270 goto out_disable_clk;
272 dev->platform_data = pdata;
274 spin_lock_init(&drvdata->spinlock);
275 desc.type = CORESIGHT_DEV_TYPE_LINK;
276 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
277 desc.ops = &replicator_cs_ops;
278 desc.pdata = dev->platform_data;
281 drvdata->csdev = coresight_register(&desc);
282 if (IS_ERR(drvdata->csdev)) {
283 ret = PTR_ERR(drvdata->csdev);
284 goto out_disable_clk;
287 replicator_reset(drvdata);
291 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
292 clk_disable_unprepare(drvdata->atclk);
296 static int replicator_remove(struct device *dev)
298 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
300 coresight_unregister(drvdata->csdev);
304 static int static_replicator_probe(struct platform_device *pdev)
308 pm_runtime_get_noresume(&pdev->dev);
309 pm_runtime_set_active(&pdev->dev);
310 pm_runtime_enable(&pdev->dev);
312 /* Static replicators do not have programming base */
313 ret = replicator_probe(&pdev->dev, NULL);
316 pm_runtime_put_noidle(&pdev->dev);
317 pm_runtime_disable(&pdev->dev);
323 static int static_replicator_remove(struct platform_device *pdev)
325 replicator_remove(&pdev->dev);
326 pm_runtime_disable(&pdev->dev);
331 static int replicator_runtime_suspend(struct device *dev)
333 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
335 if (drvdata && !IS_ERR(drvdata->atclk))
336 clk_disable_unprepare(drvdata->atclk);
341 static int replicator_runtime_resume(struct device *dev)
343 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
345 if (drvdata && !IS_ERR(drvdata->atclk))
346 clk_prepare_enable(drvdata->atclk);
352 static const struct dev_pm_ops replicator_dev_pm_ops = {
353 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
354 replicator_runtime_resume, NULL)
357 static const struct of_device_id static_replicator_match[] = {
358 {.compatible = "arm,coresight-replicator"},
359 {.compatible = "arm,coresight-static-replicator"},
363 MODULE_DEVICE_TABLE(of, static_replicator_match);
366 static const struct acpi_device_id static_replicator_acpi_ids[] = {
367 {"ARMHC985", 0}, /* ARM CoreSight Static Replicator */
371 MODULE_DEVICE_TABLE(acpi, static_replicator_acpi_ids);
374 static struct platform_driver static_replicator_driver = {
375 .probe = static_replicator_probe,
376 .remove = static_replicator_remove,
378 .name = "coresight-static-replicator",
379 /* THIS_MODULE is taken care of by platform_driver_register() */
380 .of_match_table = of_match_ptr(static_replicator_match),
381 .acpi_match_table = ACPI_PTR(static_replicator_acpi_ids),
382 .pm = &replicator_dev_pm_ops,
383 .suppress_bind_attrs = true,
387 static int dynamic_replicator_probe(struct amba_device *adev,
388 const struct amba_id *id)
390 return replicator_probe(&adev->dev, &adev->res);
393 static void dynamic_replicator_remove(struct amba_device *adev)
395 replicator_remove(&adev->dev);
398 static const struct amba_id dynamic_replicator_ids[] = {
399 CS_AMBA_ID(0x000bb909),
400 CS_AMBA_ID(0x000bb9ec), /* Coresight SoC-600 */
404 MODULE_DEVICE_TABLE(amba, dynamic_replicator_ids);
406 static struct amba_driver dynamic_replicator_driver = {
408 .name = "coresight-dynamic-replicator",
409 .pm = &replicator_dev_pm_ops,
410 .owner = THIS_MODULE,
411 .suppress_bind_attrs = true,
413 .probe = dynamic_replicator_probe,
414 .remove = dynamic_replicator_remove,
415 .id_table = dynamic_replicator_ids,
418 static int __init replicator_init(void)
422 ret = platform_driver_register(&static_replicator_driver);
424 pr_info("Error registering platform driver\n");
428 ret = amba_driver_register(&dynamic_replicator_driver);
430 pr_info("Error registering amba driver\n");
431 platform_driver_unregister(&static_replicator_driver);
437 static void __exit replicator_exit(void)
439 platform_driver_unregister(&static_replicator_driver);
440 amba_driver_unregister(&dynamic_replicator_driver);
443 module_init(replicator_init);
444 module_exit(replicator_exit);
446 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
447 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
448 MODULE_DESCRIPTION("Arm CoreSight Replicator Driver");
449 MODULE_LICENSE("GPL v2");