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/amba/bus.h>
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/platform_device.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/clk.h>
18 #include <linux/coresight.h>
20 #include "coresight-priv.h"
22 #define REPLICATOR_IDFILTER0 0x000
23 #define REPLICATOR_IDFILTER1 0x004
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
32 struct replicator_drvdata {
35 struct coresight_device *csdev;
38 static void dynamic_replicator_reset(struct replicator_drvdata *drvdata)
40 CS_UNLOCK(drvdata->base);
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);
48 CS_LOCK(drvdata->base);
52 * replicator_reset : Reset the replicator configuration to sane values.
54 static inline void replicator_reset(struct replicator_drvdata *drvdata)
57 dynamic_replicator_reset(drvdata);
60 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
61 int inport, int outport)
68 reg = REPLICATOR_IDFILTER0;
71 reg = REPLICATOR_IDFILTER1;
78 CS_UNLOCK(drvdata->base);
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);
84 /* Ensure that the outport is enabled. */
86 writel_relaxed(0x00, drvdata->base + reg);
87 CS_LOCK(drvdata->base);
92 static int replicator_enable(struct coresight_device *csdev, int inport,
96 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
99 rc = dynamic_replicator_enable(drvdata, inport, outport);
101 dev_dbg(&csdev->dev, "REPLICATOR enabled\n");
105 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
106 int inport, int outport)
112 reg = REPLICATOR_IDFILTER0;
115 reg = REPLICATOR_IDFILTER1;
122 CS_UNLOCK(drvdata->base);
124 /* disable the flow of ATB data through port */
125 writel_relaxed(0xff, drvdata->base + reg);
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);
133 static void replicator_disable(struct coresight_device *csdev, int inport,
136 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
139 dynamic_replicator_disable(drvdata, inport, outport);
140 dev_dbg(&csdev->dev, "REPLICATOR disabled\n");
143 static const struct coresight_ops_link replicator_link_ops = {
144 .enable = replicator_enable,
145 .disable = replicator_disable,
148 static const struct coresight_ops replicator_cs_ops = {
149 .link_ops = &replicator_link_ops,
152 #define coresight_replicator_reg(name, offset) \
153 coresight_simple_reg32(struct replicator_drvdata, name, offset)
155 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
156 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
158 static struct attribute *replicator_mgmt_attrs[] = {
159 &dev_attr_idfilter0.attr,
160 &dev_attr_idfilter1.attr,
164 static const struct attribute_group replicator_mgmt_group = {
165 .attrs = replicator_mgmt_attrs,
169 static const struct attribute_group *replicator_groups[] = {
170 &replicator_mgmt_group,
174 static int replicator_probe(struct device *dev, struct resource *res)
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;
184 pdata = of_get_coresight_platform_data(dev, np);
186 return PTR_ERR(pdata);
187 dev->platform_data = pdata;
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");
194 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
198 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
199 if (!IS_ERR(drvdata->atclk)) {
200 ret = clk_prepare_enable(drvdata->atclk);
206 * Map the device base for dynamic-replicator, which has been
207 * validated by AMBA core
210 base = devm_ioremap_resource(dev, res);
213 goto out_disable_clk;
215 drvdata->base = base;
216 desc.groups = replicator_groups;
219 dev_set_drvdata(dev, drvdata);
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;
226 drvdata->csdev = coresight_register(&desc);
227 if (IS_ERR(drvdata->csdev)) {
228 ret = PTR_ERR(drvdata->csdev);
229 goto out_disable_clk;
232 replicator_reset(drvdata);
236 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
237 clk_disable_unprepare(drvdata->atclk);
241 static int static_replicator_probe(struct platform_device *pdev)
245 pm_runtime_get_noresume(&pdev->dev);
246 pm_runtime_set_active(&pdev->dev);
247 pm_runtime_enable(&pdev->dev);
249 /* Static replicators do not have programming base */
250 ret = replicator_probe(&pdev->dev, NULL);
253 pm_runtime_put_noidle(&pdev->dev);
254 pm_runtime_disable(&pdev->dev);
261 static int replicator_runtime_suspend(struct device *dev)
263 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
265 if (drvdata && !IS_ERR(drvdata->atclk))
266 clk_disable_unprepare(drvdata->atclk);
271 static int replicator_runtime_resume(struct device *dev)
273 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
275 if (drvdata && !IS_ERR(drvdata->atclk))
276 clk_prepare_enable(drvdata->atclk);
282 static const struct dev_pm_ops replicator_dev_pm_ops = {
283 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
284 replicator_runtime_resume, NULL)
287 static const struct of_device_id static_replicator_match[] = {
288 {.compatible = "arm,coresight-replicator"},
289 {.compatible = "arm,coresight-static-replicator"},
293 static struct platform_driver static_replicator_driver = {
294 .probe = static_replicator_probe,
296 .name = "coresight-static-replicator",
297 .of_match_table = static_replicator_match,
298 .pm = &replicator_dev_pm_ops,
299 .suppress_bind_attrs = true,
302 builtin_platform_driver(static_replicator_driver);
304 static int dynamic_replicator_probe(struct amba_device *adev,
305 const struct amba_id *id)
307 return replicator_probe(&adev->dev, &adev->res);
310 static const struct amba_id dynamic_replicator_ids[] = {
316 /* Coresight SoC-600 */
323 static struct amba_driver dynamic_replicator_driver = {
325 .name = "coresight-dynamic-replicator",
326 .pm = &replicator_dev_pm_ops,
327 .suppress_bind_attrs = true,
329 .probe = dynamic_replicator_probe,
330 .id_table = dynamic_replicator_ids,
332 builtin_amba_driver(dynamic_replicator_driver);