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_state - 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_state {
34 struct coresight_device *csdev;
38 * replicator_reset : Reset the replicator configuration to sane values.
40 static void replicator_reset(struct replicator_state *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 replicator_enable(struct coresight_device *csdev, int inport,
58 struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
62 reg = REPLICATOR_IDFILTER0;
65 reg = REPLICATOR_IDFILTER1;
72 CS_UNLOCK(drvdata->base);
74 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
75 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
76 rc = coresight_claim_device_unlocked(drvdata->base);
78 /* Ensure that the outport is enabled. */
80 writel_relaxed(0x00, drvdata->base + reg);
81 dev_dbg(drvdata->dev, "REPLICATOR enabled\n");
84 CS_LOCK(drvdata->base);
89 static void replicator_disable(struct coresight_device *csdev, int inport,
93 struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
97 reg = REPLICATOR_IDFILTER0;
100 reg = REPLICATOR_IDFILTER1;
107 CS_UNLOCK(drvdata->base);
109 /* disable the flow of ATB data through port */
110 writel_relaxed(0xff, drvdata->base + reg);
112 if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
113 (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
114 coresight_disclaim_device_unlocked(drvdata->base);
115 CS_LOCK(drvdata->base);
117 dev_dbg(drvdata->dev, "REPLICATOR disabled\n");
120 static const struct coresight_ops_link replicator_link_ops = {
121 .enable = replicator_enable,
122 .disable = replicator_disable,
125 static const struct coresight_ops replicator_cs_ops = {
126 .link_ops = &replicator_link_ops,
129 #define coresight_replicator_reg(name, offset) \
130 coresight_simple_reg32(struct replicator_state, name, offset)
132 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
133 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
135 static struct attribute *replicator_mgmt_attrs[] = {
136 &dev_attr_idfilter0.attr,
137 &dev_attr_idfilter1.attr,
141 static const struct attribute_group replicator_mgmt_group = {
142 .attrs = replicator_mgmt_attrs,
146 static const struct attribute_group *replicator_groups[] = {
147 &replicator_mgmt_group,
151 static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
154 struct device *dev = &adev->dev;
155 struct resource *res = &adev->res;
156 struct coresight_platform_data *pdata = NULL;
157 struct replicator_state *drvdata;
158 struct coresight_desc desc = { 0 };
159 struct device_node *np = adev->dev.of_node;
163 pdata = of_get_coresight_platform_data(dev, np);
165 return PTR_ERR(pdata);
166 adev->dev.platform_data = pdata;
169 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
173 drvdata->dev = &adev->dev;
174 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
175 if (!IS_ERR(drvdata->atclk)) {
176 ret = clk_prepare_enable(drvdata->atclk);
181 /* Validity for the resource is already checked by the AMBA core */
182 base = devm_ioremap_resource(dev, res);
184 return PTR_ERR(base);
186 drvdata->base = base;
187 dev_set_drvdata(dev, drvdata);
188 pm_runtime_put(&adev->dev);
190 desc.type = CORESIGHT_DEV_TYPE_LINK;
191 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
192 desc.ops = &replicator_cs_ops;
193 desc.pdata = adev->dev.platform_data;
194 desc.dev = &adev->dev;
195 desc.groups = replicator_groups;
196 drvdata->csdev = coresight_register(&desc);
198 if (!IS_ERR(drvdata->csdev)) {
199 replicator_reset(drvdata);
202 return PTR_ERR(drvdata->csdev);
206 static int replicator_runtime_suspend(struct device *dev)
208 struct replicator_state *drvdata = dev_get_drvdata(dev);
210 if (drvdata && !IS_ERR(drvdata->atclk))
211 clk_disable_unprepare(drvdata->atclk);
216 static int replicator_runtime_resume(struct device *dev)
218 struct replicator_state *drvdata = dev_get_drvdata(dev);
220 if (drvdata && !IS_ERR(drvdata->atclk))
221 clk_prepare_enable(drvdata->atclk);
227 static const struct dev_pm_ops replicator_dev_pm_ops = {
228 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
229 replicator_runtime_resume,
233 static const struct amba_id replicator_ids[] = {
239 /* Coresight SoC-600 */
246 static struct amba_driver replicator_driver = {
248 .name = "coresight-dynamic-replicator",
249 .pm = &replicator_dev_pm_ops,
250 .suppress_bind_attrs = true,
252 .probe = replicator_probe,
253 .id_table = replicator_ids,
255 builtin_amba_driver(replicator_driver);