1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
6 #include <linux/amba/bus.h>
8 #include <linux/coresight.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
13 #include <linux/kernel.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
18 #include "coresight-priv.h"
20 #define REPLICATOR_IDFILTER0 0x000
21 #define REPLICATOR_IDFILTER1 0x004
24 * struct replicator_drvdata - specifics associated to a replicator component
25 * @base: memory mapped base address for this component.
26 * @dev: the device entity associated with this component
27 * @atclk: optional clock for the core parts of the replicator.
28 * @csdev: component vitals needed by the framework
30 struct replicator_drvdata {
34 struct coresight_device *csdev;
38 * dynamic_replicator_reset : Reset the replicator configuration to sane values.
40 static void dynamic_replicator_reset(struct replicator_drvdata *drvdata)
42 CS_UNLOCK(drvdata->base);
44 if (!coresight_claim_device_unlocked(drvdata->base)) {
45 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
46 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
47 coresight_disclaim_device_unlocked(drvdata->base);
50 CS_LOCK(drvdata->base);
53 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
54 int inport, int outport)
61 reg = REPLICATOR_IDFILTER0;
64 reg = REPLICATOR_IDFILTER1;
71 CS_UNLOCK(drvdata->base);
73 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
74 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
75 rc = coresight_claim_device_unlocked(drvdata->base);
77 /* Ensure that the outport is enabled. */
79 writel_relaxed(0x00, drvdata->base + reg);
80 CS_LOCK(drvdata->base);
85 static int replicator_enable(struct coresight_device *csdev, int inport,
89 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
91 rc = dynamic_replicator_enable(drvdata, inport, outport);
92 dev_dbg(drvdata->dev, "REPLICATOR enabled\n");
96 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
97 int inport, int outport)
103 reg = REPLICATOR_IDFILTER0;
106 reg = REPLICATOR_IDFILTER1;
113 CS_UNLOCK(drvdata->base);
115 /* disable the flow of ATB data through port */
116 writel_relaxed(0xff, drvdata->base + reg);
118 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
119 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
120 coresight_disclaim_device_unlocked(drvdata->base);
121 CS_LOCK(drvdata->base);
124 static void replicator_disable(struct coresight_device *csdev, int inport,
127 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
129 dynamic_replicator_disable(drvdata, inport, outport);
130 dev_dbg(drvdata->dev, "REPLICATOR disabled\n");
133 static const struct coresight_ops_link replicator_link_ops = {
134 .enable = replicator_enable,
135 .disable = replicator_disable,
138 static const struct coresight_ops replicator_cs_ops = {
139 .link_ops = &replicator_link_ops,
142 #define coresight_replicator_reg(name, offset) \
143 coresight_simple_reg32(struct replicator_drvdata, name, offset)
145 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
146 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
148 static struct attribute *replicator_mgmt_attrs[] = {
149 &dev_attr_idfilter0.attr,
150 &dev_attr_idfilter1.attr,
154 static const struct attribute_group replicator_mgmt_group = {
155 .attrs = replicator_mgmt_attrs,
159 static const struct attribute_group *replicator_groups[] = {
160 &replicator_mgmt_group,
164 static int replicator_probe(struct device *dev, struct resource *res)
167 struct coresight_platform_data *pdata = NULL;
168 struct replicator_drvdata *drvdata;
169 struct coresight_desc desc = { 0 };
170 struct device_node *np = dev->of_node;
174 pdata = of_get_coresight_platform_data(dev, np);
176 return PTR_ERR(pdata);
177 dev->platform_data = pdata;
180 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
185 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
186 if (!IS_ERR(drvdata->atclk)) {
187 ret = clk_prepare_enable(drvdata->atclk);
193 * Map the device base for dynamic-replicator, which has been
194 * validated by AMBA core
197 base = devm_ioremap_resource(dev, res);
200 goto out_disable_clk;
202 drvdata->base = base;
203 desc.groups = replicator_groups;
206 dev_set_drvdata(dev, drvdata);
208 desc.type = CORESIGHT_DEV_TYPE_LINK;
209 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
210 desc.ops = &replicator_cs_ops;
211 desc.pdata = dev->platform_data;
213 drvdata->csdev = coresight_register(&desc);
215 if (IS_ERR(drvdata->csdev)) {
216 ret = PTR_ERR(drvdata->csdev);
217 goto out_disable_clk;
220 dynamic_replicator_reset(drvdata);
224 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
225 clk_disable_unprepare(drvdata->atclk);
229 static int dynamic_replicator_probe(struct amba_device *adev,
230 const struct amba_id *id)
232 return replicator_probe(&adev->dev, &adev->res);
236 static int replicator_runtime_suspend(struct device *dev)
238 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
240 if (drvdata && !IS_ERR(drvdata->atclk))
241 clk_disable_unprepare(drvdata->atclk);
246 static int replicator_runtime_resume(struct device *dev)
248 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
250 if (drvdata && !IS_ERR(drvdata->atclk))
251 clk_prepare_enable(drvdata->atclk);
257 static const struct dev_pm_ops replicator_dev_pm_ops = {
258 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
259 replicator_runtime_resume,
263 static const struct amba_id dynamic_replicator_ids[] = {
269 /* Coresight SoC-600 */
276 static struct amba_driver dynamic_replicator_driver = {
278 .name = "coresight-dynamic-replicator",
279 .pm = &replicator_dev_pm_ops,
280 .suppress_bind_attrs = true,
282 .probe = dynamic_replicator_probe,
283 .id_table = dynamic_replicator_ids,
285 builtin_amba_driver(dynamic_replicator_driver);