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/clk.h>
19 #include <linux/coresight.h>
21 #include "coresight-priv.h"
23 #define REPLICATOR_IDFILTER0 0x000
24 #define REPLICATOR_IDFILTER1 0x004
26 DEFINE_CORESIGHT_DEVLIST(replicator_devs
, "replicator");
29 * struct replicator_drvdata - specifics associated to a replicator component
30 * @base: memory mapped base address for this component. Also indicates
31 * whether this one is programmable or not.
32 * @atclk: optional clock for the core parts of the replicator.
33 * @csdev: component vitals needed by the framework
34 * @spinlock: serialize enable/disable operations.
36 struct replicator_drvdata
{
39 struct coresight_device
*csdev
;
43 static void dynamic_replicator_reset(struct replicator_drvdata
*drvdata
)
45 CS_UNLOCK(drvdata
->base
);
47 if (!coresight_claim_device_unlocked(drvdata
->base
)) {
48 writel_relaxed(0xff, drvdata
->base
+ REPLICATOR_IDFILTER0
);
49 writel_relaxed(0xff, drvdata
->base
+ REPLICATOR_IDFILTER1
);
50 coresight_disclaim_device_unlocked(drvdata
->base
);
53 CS_LOCK(drvdata
->base
);
57 * replicator_reset : Reset the replicator configuration to sane values.
59 static inline void replicator_reset(struct replicator_drvdata
*drvdata
)
62 dynamic_replicator_reset(drvdata
);
65 static int dynamic_replicator_enable(struct replicator_drvdata
*drvdata
,
66 int inport
, int outport
)
73 reg
= REPLICATOR_IDFILTER0
;
76 reg
= REPLICATOR_IDFILTER1
;
83 CS_UNLOCK(drvdata
->base
);
85 if ((readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER0
) == 0xff) &&
86 (readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER1
) == 0xff))
87 rc
= coresight_claim_device_unlocked(drvdata
->base
);
89 /* Ensure that the outport is enabled. */
91 writel_relaxed(0x00, drvdata
->base
+ reg
);
92 CS_LOCK(drvdata
->base
);
97 static int replicator_enable(struct coresight_device
*csdev
, int inport
,
101 struct replicator_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
103 bool first_enable
= false;
105 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
106 if (atomic_read(&csdev
->refcnt
[outport
]) == 0) {
108 rc
= dynamic_replicator_enable(drvdata
, inport
,
114 atomic_inc(&csdev
->refcnt
[outport
]);
115 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
118 dev_dbg(&csdev
->dev
, "REPLICATOR enabled\n");
122 static void dynamic_replicator_disable(struct replicator_drvdata
*drvdata
,
123 int inport
, int outport
)
129 reg
= REPLICATOR_IDFILTER0
;
132 reg
= REPLICATOR_IDFILTER1
;
139 CS_UNLOCK(drvdata
->base
);
141 /* disable the flow of ATB data through port */
142 writel_relaxed(0xff, drvdata
->base
+ reg
);
144 if ((readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER0
) == 0xff) &&
145 (readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER1
) == 0xff))
146 coresight_disclaim_device_unlocked(drvdata
->base
);
147 CS_LOCK(drvdata
->base
);
150 static void replicator_disable(struct coresight_device
*csdev
, int inport
,
153 struct replicator_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
155 bool last_disable
= false;
157 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
158 if (atomic_dec_return(&csdev
->refcnt
[outport
]) == 0) {
160 dynamic_replicator_disable(drvdata
, inport
, outport
);
163 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
166 dev_dbg(&csdev
->dev
, "REPLICATOR disabled\n");
169 static const struct coresight_ops_link replicator_link_ops
= {
170 .enable
= replicator_enable
,
171 .disable
= replicator_disable
,
174 static const struct coresight_ops replicator_cs_ops
= {
175 .link_ops
= &replicator_link_ops
,
178 #define coresight_replicator_reg(name, offset) \
179 coresight_simple_reg32(struct replicator_drvdata, name, offset)
181 coresight_replicator_reg(idfilter0
, REPLICATOR_IDFILTER0
);
182 coresight_replicator_reg(idfilter1
, REPLICATOR_IDFILTER1
);
184 static struct attribute
*replicator_mgmt_attrs
[] = {
185 &dev_attr_idfilter0
.attr
,
186 &dev_attr_idfilter1
.attr
,
190 static const struct attribute_group replicator_mgmt_group
= {
191 .attrs
= replicator_mgmt_attrs
,
195 static const struct attribute_group
*replicator_groups
[] = {
196 &replicator_mgmt_group
,
200 static int replicator_probe(struct device
*dev
, struct resource
*res
)
203 struct coresight_platform_data
*pdata
= NULL
;
204 struct replicator_drvdata
*drvdata
;
205 struct coresight_desc desc
= { 0 };
208 if (is_of_node(dev_fwnode(dev
)) &&
209 of_device_is_compatible(dev
->of_node
, "arm,coresight-replicator"))
211 "Uses OBSOLETE CoreSight replicator binding\n");
213 desc
.name
= coresight_alloc_device_name(&replicator_devs
, dev
);
217 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
221 drvdata
->atclk
= devm_clk_get(dev
, "atclk"); /* optional */
222 if (!IS_ERR(drvdata
->atclk
)) {
223 ret
= clk_prepare_enable(drvdata
->atclk
);
229 * Map the device base for dynamic-replicator, which has been
230 * validated by AMBA core
233 base
= devm_ioremap_resource(dev
, res
);
236 goto out_disable_clk
;
238 drvdata
->base
= base
;
239 desc
.groups
= replicator_groups
;
242 dev_set_drvdata(dev
, drvdata
);
244 pdata
= coresight_get_platform_data(dev
);
246 ret
= PTR_ERR(pdata
);
247 goto out_disable_clk
;
249 dev
->platform_data
= pdata
;
251 spin_lock_init(&drvdata
->spinlock
);
252 desc
.type
= CORESIGHT_DEV_TYPE_LINK
;
253 desc
.subtype
.link_subtype
= CORESIGHT_DEV_SUBTYPE_LINK_SPLIT
;
254 desc
.ops
= &replicator_cs_ops
;
255 desc
.pdata
= dev
->platform_data
;
258 drvdata
->csdev
= coresight_register(&desc
);
259 if (IS_ERR(drvdata
->csdev
)) {
260 ret
= PTR_ERR(drvdata
->csdev
);
261 goto out_disable_clk
;
264 replicator_reset(drvdata
);
268 if (ret
&& !IS_ERR_OR_NULL(drvdata
->atclk
))
269 clk_disable_unprepare(drvdata
->atclk
);
273 static int static_replicator_probe(struct platform_device
*pdev
)
277 pm_runtime_get_noresume(&pdev
->dev
);
278 pm_runtime_set_active(&pdev
->dev
);
279 pm_runtime_enable(&pdev
->dev
);
281 /* Static replicators do not have programming base */
282 ret
= replicator_probe(&pdev
->dev
, NULL
);
285 pm_runtime_put_noidle(&pdev
->dev
);
286 pm_runtime_disable(&pdev
->dev
);
293 static int replicator_runtime_suspend(struct device
*dev
)
295 struct replicator_drvdata
*drvdata
= dev_get_drvdata(dev
);
297 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
298 clk_disable_unprepare(drvdata
->atclk
);
303 static int replicator_runtime_resume(struct device
*dev
)
305 struct replicator_drvdata
*drvdata
= dev_get_drvdata(dev
);
307 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
308 clk_prepare_enable(drvdata
->atclk
);
314 static const struct dev_pm_ops replicator_dev_pm_ops
= {
315 SET_RUNTIME_PM_OPS(replicator_runtime_suspend
,
316 replicator_runtime_resume
, NULL
)
319 static const struct of_device_id static_replicator_match
[] = {
320 {.compatible
= "arm,coresight-replicator"},
321 {.compatible
= "arm,coresight-static-replicator"},
326 static const struct acpi_device_id static_replicator_acpi_ids
[] = {
327 {"ARMHC985", 0}, /* ARM CoreSight Static Replicator */
332 static struct platform_driver static_replicator_driver
= {
333 .probe
= static_replicator_probe
,
335 .name
= "coresight-static-replicator",
336 .of_match_table
= of_match_ptr(static_replicator_match
),
337 .acpi_match_table
= ACPI_PTR(static_replicator_acpi_ids
),
338 .pm
= &replicator_dev_pm_ops
,
339 .suppress_bind_attrs
= true,
342 builtin_platform_driver(static_replicator_driver
);
344 static int dynamic_replicator_probe(struct amba_device
*adev
,
345 const struct amba_id
*id
)
347 return replicator_probe(&adev
->dev
, &adev
->res
);
350 static const struct amba_id dynamic_replicator_ids
[] = {
356 /* Coresight SoC-600 */
363 static struct amba_driver dynamic_replicator_driver
= {
365 .name
= "coresight-dynamic-replicator",
366 .pm
= &replicator_dev_pm_ops
,
367 .suppress_bind_attrs
= true,
369 .probe
= dynamic_replicator_probe
,
370 .id_table
= dynamic_replicator_ids
,
372 builtin_amba_driver(dynamic_replicator_driver
);