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 CS_UNLOCK(drvdata->base);
50 if (!coresight_claim_device_unlocked(drvdata->base)) {
51 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
52 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
53 coresight_disclaim_device_unlocked(drvdata->base);
56 CS_LOCK(drvdata->base);
60 * replicator_reset : Reset the replicator configuration to sane values.
62 static inline void replicator_reset(struct replicator_drvdata *drvdata)
65 dynamic_replicator_reset(drvdata);
68 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
69 int inport, int outport)
74 CS_UNLOCK(drvdata->base);
76 id0val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0);
77 id1val = readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1);
80 * Some replicator designs lose context when AMBA clocks are removed,
81 * so have a check for this.
83 if (drvdata->check_idfilter_val && id0val == 0x0 && id1val == 0x0)
84 id0val = id1val = 0xff;
86 if (id0val == 0xff && id1val == 0xff)
87 rc = coresight_claim_device_unlocked(drvdata->base);
103 /* Ensure that the outport is enabled. */
105 writel_relaxed(id0val, drvdata->base + REPLICATOR_IDFILTER0);
106 writel_relaxed(id1val, drvdata->base + REPLICATOR_IDFILTER1);
109 CS_LOCK(drvdata->base);
114 static int replicator_enable(struct coresight_device *csdev, int inport,
118 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
120 bool first_enable = false;
122 spin_lock_irqsave(&drvdata->spinlock, flags);
123 if (atomic_read(&csdev->refcnt[outport]) == 0) {
125 rc = dynamic_replicator_enable(drvdata, inport,
131 atomic_inc(&csdev->refcnt[outport]);
132 spin_unlock_irqrestore(&drvdata->spinlock, flags);
135 dev_dbg(&csdev->dev, "REPLICATOR enabled\n");
139 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
140 int inport, int outport)
146 reg = REPLICATOR_IDFILTER0;
149 reg = REPLICATOR_IDFILTER1;
156 CS_UNLOCK(drvdata->base);
158 /* disable the flow of ATB data through port */
159 writel_relaxed(0xff, drvdata->base + reg);
161 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
162 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
163 coresight_disclaim_device_unlocked(drvdata->base);
164 CS_LOCK(drvdata->base);
167 static void replicator_disable(struct coresight_device *csdev, int inport,
170 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
172 bool last_disable = false;
174 spin_lock_irqsave(&drvdata->spinlock, flags);
175 if (atomic_dec_return(&csdev->refcnt[outport]) == 0) {
177 dynamic_replicator_disable(drvdata, inport, outport);
180 spin_unlock_irqrestore(&drvdata->spinlock, flags);
183 dev_dbg(&csdev->dev, "REPLICATOR disabled\n");
186 static const struct coresight_ops_link replicator_link_ops = {
187 .enable = replicator_enable,
188 .disable = replicator_disable,
191 static const struct coresight_ops replicator_cs_ops = {
192 .link_ops = &replicator_link_ops,
195 #define coresight_replicator_reg(name, offset) \
196 coresight_simple_reg32(struct replicator_drvdata, name, offset)
198 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
199 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
201 static struct attribute *replicator_mgmt_attrs[] = {
202 &dev_attr_idfilter0.attr,
203 &dev_attr_idfilter1.attr,
207 static const struct attribute_group replicator_mgmt_group = {
208 .attrs = replicator_mgmt_attrs,
212 static const struct attribute_group *replicator_groups[] = {
213 &replicator_mgmt_group,
217 static int replicator_probe(struct device *dev, struct resource *res)
220 struct coresight_platform_data *pdata = NULL;
221 struct replicator_drvdata *drvdata;
222 struct coresight_desc desc = { 0 };
225 if (is_of_node(dev_fwnode(dev)) &&
226 of_device_is_compatible(dev->of_node, "arm,coresight-replicator"))
228 "Uses OBSOLETE CoreSight replicator binding\n");
230 desc.name = coresight_alloc_device_name(&replicator_devs, dev);
234 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
238 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
239 if (!IS_ERR(drvdata->atclk)) {
240 ret = clk_prepare_enable(drvdata->atclk);
246 * Map the device base for dynamic-replicator, which has been
247 * validated by AMBA core
250 base = devm_ioremap_resource(dev, res);
253 goto out_disable_clk;
255 drvdata->base = base;
256 desc.groups = replicator_groups;
259 if (fwnode_property_present(dev_fwnode(dev),
260 "qcom,replicator-loses-context"))
261 drvdata->check_idfilter_val = true;
263 dev_set_drvdata(dev, drvdata);
265 pdata = coresight_get_platform_data(dev);
267 ret = PTR_ERR(pdata);
268 goto out_disable_clk;
270 dev->platform_data = pdata;
272 spin_lock_init(&drvdata->spinlock);
273 desc.type = CORESIGHT_DEV_TYPE_LINK;
274 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
275 desc.ops = &replicator_cs_ops;
276 desc.pdata = dev->platform_data;
279 drvdata->csdev = coresight_register(&desc);
280 if (IS_ERR(drvdata->csdev)) {
281 ret = PTR_ERR(drvdata->csdev);
282 goto out_disable_clk;
285 replicator_reset(drvdata);
289 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
290 clk_disable_unprepare(drvdata->atclk);
294 static int replicator_remove(struct device *dev)
296 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
298 coresight_unregister(drvdata->csdev);
302 static int static_replicator_probe(struct platform_device *pdev)
306 pm_runtime_get_noresume(&pdev->dev);
307 pm_runtime_set_active(&pdev->dev);
308 pm_runtime_enable(&pdev->dev);
310 /* Static replicators do not have programming base */
311 ret = replicator_probe(&pdev->dev, NULL);
314 pm_runtime_put_noidle(&pdev->dev);
315 pm_runtime_disable(&pdev->dev);
321 static int static_replicator_remove(struct platform_device *pdev)
323 replicator_remove(&pdev->dev);
324 pm_runtime_disable(&pdev->dev);
329 static int replicator_runtime_suspend(struct device *dev)
331 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
333 if (drvdata && !IS_ERR(drvdata->atclk))
334 clk_disable_unprepare(drvdata->atclk);
339 static int replicator_runtime_resume(struct device *dev)
341 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
343 if (drvdata && !IS_ERR(drvdata->atclk))
344 clk_prepare_enable(drvdata->atclk);
350 static const struct dev_pm_ops replicator_dev_pm_ops = {
351 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
352 replicator_runtime_resume, NULL)
355 static const struct of_device_id static_replicator_match[] = {
356 {.compatible = "arm,coresight-replicator"},
357 {.compatible = "arm,coresight-static-replicator"},
361 MODULE_DEVICE_TABLE(of, static_replicator_match);
364 static const struct acpi_device_id static_replicator_acpi_ids[] = {
365 {"ARMHC985", 0}, /* ARM CoreSight Static Replicator */
369 MODULE_DEVICE_TABLE(acpi, static_replicator_acpi_ids);
372 static struct platform_driver static_replicator_driver = {
373 .probe = static_replicator_probe,
374 .remove = static_replicator_remove,
376 .name = "coresight-static-replicator",
377 .owner = THIS_MODULE,
378 .of_match_table = of_match_ptr(static_replicator_match),
379 .acpi_match_table = ACPI_PTR(static_replicator_acpi_ids),
380 .pm = &replicator_dev_pm_ops,
381 .suppress_bind_attrs = true,
385 static int dynamic_replicator_probe(struct amba_device *adev,
386 const struct amba_id *id)
388 return replicator_probe(&adev->dev, &adev->res);
391 static void dynamic_replicator_remove(struct amba_device *adev)
393 replicator_remove(&adev->dev);
396 static const struct amba_id dynamic_replicator_ids[] = {
397 CS_AMBA_ID(0x000bb909),
398 CS_AMBA_ID(0x000bb9ec), /* Coresight SoC-600 */
402 MODULE_DEVICE_TABLE(amba, dynamic_replicator_ids);
404 static struct amba_driver dynamic_replicator_driver = {
406 .name = "coresight-dynamic-replicator",
407 .pm = &replicator_dev_pm_ops,
408 .owner = THIS_MODULE,
409 .suppress_bind_attrs = true,
411 .probe = dynamic_replicator_probe,
412 .remove = dynamic_replicator_remove,
413 .id_table = dynamic_replicator_ids,
416 static int __init replicator_init(void)
420 ret = platform_driver_register(&static_replicator_driver);
422 pr_info("Error registering platform driver\n");
426 ret = amba_driver_register(&dynamic_replicator_driver);
428 pr_info("Error registering amba driver\n");
429 platform_driver_unregister(&static_replicator_driver);
435 static void __exit replicator_exit(void)
437 platform_driver_unregister(&static_replicator_driver);
438 amba_driver_unregister(&dynamic_replicator_driver);
441 module_init(replicator_init);
442 module_exit(replicator_exit);
444 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
445 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
446 MODULE_DESCRIPTION("Arm CoreSight Replicator Driver");
447 MODULE_LICENSE("GPL v2");