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 * @dev: the device entity associated with this component
30 * @atclk: optional clock for the core parts of the replicator.
31 * @csdev: component vitals needed by the framework
33 struct replicator_drvdata {
37 struct coresight_device *csdev;
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);
54 * replicator_reset : Reset the replicator configuration to sane values.
56 static inline void replicator_reset(struct replicator_drvdata *drvdata)
59 dynamic_replicator_reset(drvdata);
62 static int dynamic_replicator_enable(struct replicator_drvdata *drvdata,
63 int inport, int outport)
70 reg = REPLICATOR_IDFILTER0;
73 reg = REPLICATOR_IDFILTER1;
80 CS_UNLOCK(drvdata->base);
82 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
83 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
84 rc = coresight_claim_device_unlocked(drvdata->base);
86 /* Ensure that the outport is enabled. */
88 writel_relaxed(0x00, drvdata->base + reg);
89 CS_LOCK(drvdata->base);
94 static int replicator_enable(struct coresight_device *csdev, int inport,
98 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
101 rc = dynamic_replicator_enable(drvdata, inport, outport);
103 dev_dbg(drvdata->dev, "REPLICATOR enabled\n");
107 static void dynamic_replicator_disable(struct replicator_drvdata *drvdata,
108 int inport, int outport)
114 reg = REPLICATOR_IDFILTER0;
117 reg = REPLICATOR_IDFILTER1;
124 CS_UNLOCK(drvdata->base);
126 /* disable the flow of ATB data through port */
127 writel_relaxed(0xff, drvdata->base + reg);
129 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
130 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
131 coresight_disclaim_device_unlocked(drvdata->base);
132 CS_LOCK(drvdata->base);
135 static void replicator_disable(struct coresight_device *csdev, int inport,
138 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
141 dynamic_replicator_disable(drvdata, inport, outport);
142 dev_dbg(drvdata->dev, "REPLICATOR disabled\n");
145 static const struct coresight_ops_link replicator_link_ops = {
146 .enable = replicator_enable,
147 .disable = replicator_disable,
150 static const struct coresight_ops replicator_cs_ops = {
151 .link_ops = &replicator_link_ops,
154 #define coresight_replicator_reg(name, offset) \
155 coresight_simple_reg32(struct replicator_drvdata, name, offset)
157 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
158 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
160 static struct attribute *replicator_mgmt_attrs[] = {
161 &dev_attr_idfilter0.attr,
162 &dev_attr_idfilter1.attr,
166 static const struct attribute_group replicator_mgmt_group = {
167 .attrs = replicator_mgmt_attrs,
171 static const struct attribute_group *replicator_groups[] = {
172 &replicator_mgmt_group,
176 static int replicator_probe(struct device *dev, struct resource *res)
179 struct coresight_platform_data *pdata = NULL;
180 struct replicator_drvdata *drvdata;
181 struct coresight_desc desc = { 0 };
182 struct device_node *np = dev->of_node;
186 pdata = of_get_coresight_platform_data(dev, np);
188 return PTR_ERR(pdata);
189 dev->platform_data = pdata;
192 if (of_device_is_compatible(np, "arm,coresight-replicator"))
193 pr_warn_once("Uses OBSOLETE CoreSight replicator binding\n");
195 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
200 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
201 if (!IS_ERR(drvdata->atclk)) {
202 ret = clk_prepare_enable(drvdata->atclk);
208 * Map the device base for dynamic-replicator, which has been
209 * validated by AMBA core
212 base = devm_ioremap_resource(dev, res);
215 goto out_disable_clk;
217 drvdata->base = base;
218 desc.groups = replicator_groups;
221 dev_set_drvdata(dev, drvdata);
223 desc.type = CORESIGHT_DEV_TYPE_LINK;
224 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
225 desc.ops = &replicator_cs_ops;
226 desc.pdata = dev->platform_data;
228 drvdata->csdev = coresight_register(&desc);
229 if (IS_ERR(drvdata->csdev)) {
230 ret = PTR_ERR(drvdata->csdev);
231 goto out_disable_clk;
234 replicator_reset(drvdata);
238 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
239 clk_disable_unprepare(drvdata->atclk);
243 static int static_replicator_probe(struct platform_device *pdev)
247 pm_runtime_get_noresume(&pdev->dev);
248 pm_runtime_set_active(&pdev->dev);
249 pm_runtime_enable(&pdev->dev);
251 /* Static replicators do not have programming base */
252 ret = replicator_probe(&pdev->dev, NULL);
255 pm_runtime_put_noidle(&pdev->dev);
256 pm_runtime_disable(&pdev->dev);
263 static int replicator_runtime_suspend(struct device *dev)
265 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
267 if (drvdata && !IS_ERR(drvdata->atclk))
268 clk_disable_unprepare(drvdata->atclk);
273 static int replicator_runtime_resume(struct device *dev)
275 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
277 if (drvdata && !IS_ERR(drvdata->atclk))
278 clk_prepare_enable(drvdata->atclk);
284 static const struct dev_pm_ops replicator_dev_pm_ops = {
285 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
286 replicator_runtime_resume, NULL)
289 static const struct of_device_id static_replicator_match[] = {
290 {.compatible = "arm,coresight-replicator"},
291 {.compatible = "arm,coresight-static-replicator"},
295 static struct platform_driver static_replicator_driver = {
296 .probe = static_replicator_probe,
298 .name = "coresight-static-replicator",
299 .of_match_table = static_replicator_match,
300 .pm = &replicator_dev_pm_ops,
301 .suppress_bind_attrs = true,
304 builtin_platform_driver(static_replicator_driver);
306 static int dynamic_replicator_probe(struct amba_device *adev,
307 const struct amba_id *id)
309 return replicator_probe(&adev->dev, &adev->res);
312 static const struct amba_id dynamic_replicator_ids[] = {
318 /* Coresight SoC-600 */
325 static struct amba_driver dynamic_replicator_driver = {
327 .name = "coresight-dynamic-replicator",
328 .pm = &replicator_dev_pm_ops,
329 .suppress_bind_attrs = true,
331 .probe = dynamic_replicator_probe,
332 .id_table = dynamic_replicator_ids,
334 builtin_amba_driver(dynamic_replicator_driver);