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/property.h>
18 #include <linux/clk.h>
20 #include <linux/coresight.h>
22 #include "coresight-priv.h"
24 #define REPLICATOR_IDFILTER0 0x000
25 #define REPLICATOR_IDFILTER1 0x004
27 DEFINE_CORESIGHT_DEVLIST(replicator_devs
, "replicator");
30 * struct replicator_drvdata - specifics associated to a replicator component
31 * @base: memory mapped base address for this component. Also indicates
32 * whether this one is programmable or not.
33 * @atclk: optional clock for the core parts of the replicator.
34 * @pclk: APB clock if present, otherwise NULL
35 * @csdev: component vitals needed by the framework
36 * @spinlock: serialize enable/disable operations.
37 * @check_idfilter_val: check if the context is lost upon clock removal.
39 struct replicator_drvdata
{
43 struct coresight_device
*csdev
;
45 bool check_idfilter_val
;
48 static void dynamic_replicator_reset(struct replicator_drvdata
*drvdata
)
50 struct coresight_device
*csdev
= drvdata
->csdev
;
52 CS_UNLOCK(drvdata
->base
);
54 if (!coresight_claim_device_unlocked(csdev
)) {
55 writel_relaxed(0xff, drvdata
->base
+ REPLICATOR_IDFILTER0
);
56 writel_relaxed(0xff, drvdata
->base
+ REPLICATOR_IDFILTER1
);
57 coresight_disclaim_device_unlocked(csdev
);
60 CS_LOCK(drvdata
->base
);
64 * replicator_reset : Reset the replicator configuration to sane values.
66 static inline void replicator_reset(struct replicator_drvdata
*drvdata
)
69 dynamic_replicator_reset(drvdata
);
72 static int dynamic_replicator_enable(struct replicator_drvdata
*drvdata
,
73 int inport
, int outport
)
77 struct coresight_device
*csdev
= drvdata
->csdev
;
79 CS_UNLOCK(drvdata
->base
);
81 id0val
= readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER0
);
82 id1val
= readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER1
);
85 * Some replicator designs lose context when AMBA clocks are removed,
86 * so have a check for this.
88 if (drvdata
->check_idfilter_val
&& id0val
== 0x0 && id1val
== 0x0)
89 id0val
= id1val
= 0xff;
91 if (id0val
== 0xff && id1val
== 0xff)
92 rc
= coresight_claim_device_unlocked(csdev
);
108 /* Ensure that the outport is enabled. */
110 writel_relaxed(id0val
, drvdata
->base
+ REPLICATOR_IDFILTER0
);
111 writel_relaxed(id1val
, drvdata
->base
+ REPLICATOR_IDFILTER1
);
114 CS_LOCK(drvdata
->base
);
119 static int replicator_enable(struct coresight_device
*csdev
,
120 struct coresight_connection
*in
,
121 struct coresight_connection
*out
)
124 struct replicator_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
126 bool first_enable
= false;
128 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
129 if (atomic_read(&out
->src_refcnt
) == 0) {
131 rc
= dynamic_replicator_enable(drvdata
, in
->dest_port
,
137 atomic_inc(&out
->src_refcnt
);
138 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
141 dev_dbg(&csdev
->dev
, "REPLICATOR enabled\n");
145 static void dynamic_replicator_disable(struct replicator_drvdata
*drvdata
,
146 int inport
, int outport
)
149 struct coresight_device
*csdev
= drvdata
->csdev
;
153 reg
= REPLICATOR_IDFILTER0
;
156 reg
= REPLICATOR_IDFILTER1
;
163 CS_UNLOCK(drvdata
->base
);
165 /* disable the flow of ATB data through port */
166 writel_relaxed(0xff, drvdata
->base
+ reg
);
168 if ((readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER0
) == 0xff) &&
169 (readl_relaxed(drvdata
->base
+ REPLICATOR_IDFILTER1
) == 0xff))
170 coresight_disclaim_device_unlocked(csdev
);
171 CS_LOCK(drvdata
->base
);
174 static void replicator_disable(struct coresight_device
*csdev
,
175 struct coresight_connection
*in
,
176 struct coresight_connection
*out
)
178 struct replicator_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
180 bool last_disable
= false;
182 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
183 if (atomic_dec_return(&out
->src_refcnt
) == 0) {
185 dynamic_replicator_disable(drvdata
, in
->dest_port
,
189 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
192 dev_dbg(&csdev
->dev
, "REPLICATOR disabled\n");
195 static const struct coresight_ops_link replicator_link_ops
= {
196 .enable
= replicator_enable
,
197 .disable
= replicator_disable
,
200 static const struct coresight_ops replicator_cs_ops
= {
201 .link_ops
= &replicator_link_ops
,
204 static struct attribute
*replicator_mgmt_attrs
[] = {
205 coresight_simple_reg32(idfilter0
, REPLICATOR_IDFILTER0
),
206 coresight_simple_reg32(idfilter1
, REPLICATOR_IDFILTER1
),
210 static const struct attribute_group replicator_mgmt_group
= {
211 .attrs
= replicator_mgmt_attrs
,
215 static const struct attribute_group
*replicator_groups
[] = {
216 &replicator_mgmt_group
,
220 static int replicator_probe(struct device
*dev
, struct resource
*res
)
223 struct coresight_platform_data
*pdata
= NULL
;
224 struct replicator_drvdata
*drvdata
;
225 struct coresight_desc desc
= { 0 };
228 if (is_of_node(dev_fwnode(dev
)) &&
229 of_device_is_compatible(dev
->of_node
, "arm,coresight-replicator"))
231 "Uses OBSOLETE CoreSight replicator binding\n");
233 desc
.name
= coresight_alloc_device_name(&replicator_devs
, dev
);
237 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
241 drvdata
->atclk
= devm_clk_get(dev
, "atclk"); /* optional */
242 if (!IS_ERR(drvdata
->atclk
)) {
243 ret
= clk_prepare_enable(drvdata
->atclk
);
248 drvdata
->pclk
= coresight_get_enable_apb_pclk(dev
);
249 if (IS_ERR(drvdata
->pclk
))
253 * Map the device base for dynamic-replicator, which has been
254 * validated by AMBA core
257 base
= devm_ioremap_resource(dev
, res
);
260 goto out_disable_clk
;
262 drvdata
->base
= base
;
263 desc
.groups
= replicator_groups
;
264 desc
.access
= CSDEV_ACCESS_IOMEM(base
);
267 if (fwnode_property_present(dev_fwnode(dev
),
268 "qcom,replicator-loses-context"))
269 drvdata
->check_idfilter_val
= true;
271 dev_set_drvdata(dev
, drvdata
);
273 pdata
= coresight_get_platform_data(dev
);
275 ret
= PTR_ERR(pdata
);
276 goto out_disable_clk
;
278 dev
->platform_data
= pdata
;
280 spin_lock_init(&drvdata
->spinlock
);
281 desc
.type
= CORESIGHT_DEV_TYPE_LINK
;
282 desc
.subtype
.link_subtype
= CORESIGHT_DEV_SUBTYPE_LINK_SPLIT
;
283 desc
.ops
= &replicator_cs_ops
;
284 desc
.pdata
= dev
->platform_data
;
287 drvdata
->csdev
= coresight_register(&desc
);
288 if (IS_ERR(drvdata
->csdev
)) {
289 ret
= PTR_ERR(drvdata
->csdev
);
290 goto out_disable_clk
;
293 replicator_reset(drvdata
);
296 if (ret
&& !IS_ERR_OR_NULL(drvdata
->atclk
))
297 clk_disable_unprepare(drvdata
->atclk
);
298 if (ret
&& !IS_ERR_OR_NULL(drvdata
->pclk
))
299 clk_disable_unprepare(drvdata
->pclk
);
303 static int replicator_remove(struct device
*dev
)
305 struct replicator_drvdata
*drvdata
= dev_get_drvdata(dev
);
307 coresight_unregister(drvdata
->csdev
);
311 static int replicator_platform_probe(struct platform_device
*pdev
)
313 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
316 pm_runtime_get_noresume(&pdev
->dev
);
317 pm_runtime_set_active(&pdev
->dev
);
318 pm_runtime_enable(&pdev
->dev
);
320 ret
= replicator_probe(&pdev
->dev
, res
);
321 pm_runtime_put(&pdev
->dev
);
323 pm_runtime_disable(&pdev
->dev
);
328 static void replicator_platform_remove(struct platform_device
*pdev
)
330 struct replicator_drvdata
*drvdata
= dev_get_drvdata(&pdev
->dev
);
332 if (WARN_ON(!drvdata
))
335 replicator_remove(&pdev
->dev
);
336 pm_runtime_disable(&pdev
->dev
);
337 if (!IS_ERR_OR_NULL(drvdata
->pclk
))
338 clk_put(drvdata
->pclk
);
342 static int replicator_runtime_suspend(struct device
*dev
)
344 struct replicator_drvdata
*drvdata
= dev_get_drvdata(dev
);
346 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
347 clk_disable_unprepare(drvdata
->atclk
);
349 if (drvdata
&& !IS_ERR_OR_NULL(drvdata
->pclk
))
350 clk_disable_unprepare(drvdata
->pclk
);
354 static int replicator_runtime_resume(struct device
*dev
)
356 struct replicator_drvdata
*drvdata
= dev_get_drvdata(dev
);
358 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
359 clk_prepare_enable(drvdata
->atclk
);
361 if (drvdata
&& !IS_ERR_OR_NULL(drvdata
->pclk
))
362 clk_prepare_enable(drvdata
->pclk
);
367 static const struct dev_pm_ops replicator_dev_pm_ops
= {
368 SET_RUNTIME_PM_OPS(replicator_runtime_suspend
,
369 replicator_runtime_resume
, NULL
)
372 static const struct of_device_id replicator_match
[] = {
373 {.compatible
= "arm,coresight-replicator"},
374 {.compatible
= "arm,coresight-static-replicator"},
378 MODULE_DEVICE_TABLE(of
, replicator_match
);
381 static const struct acpi_device_id replicator_acpi_ids
[] = {
382 {"ARMHC985", 0, 0, 0}, /* ARM CoreSight Static Replicator */
383 {"ARMHC98D", 0, 0, 0}, /* ARM CoreSight Dynamic Replicator */
387 MODULE_DEVICE_TABLE(acpi
, replicator_acpi_ids
);
390 static struct platform_driver replicator_driver
= {
391 .probe
= replicator_platform_probe
,
392 .remove
= replicator_platform_remove
,
394 .name
= "coresight-replicator",
395 /* THIS_MODULE is taken care of by platform_driver_register() */
396 .of_match_table
= of_match_ptr(replicator_match
),
397 .acpi_match_table
= ACPI_PTR(replicator_acpi_ids
),
398 .pm
= &replicator_dev_pm_ops
,
399 .suppress_bind_attrs
= true,
403 static int dynamic_replicator_probe(struct amba_device
*adev
,
404 const struct amba_id
*id
)
408 ret
= replicator_probe(&adev
->dev
, &adev
->res
);
410 pm_runtime_put(&adev
->dev
);
415 static void dynamic_replicator_remove(struct amba_device
*adev
)
417 replicator_remove(&adev
->dev
);
420 static const struct amba_id dynamic_replicator_ids
[] = {
421 CS_AMBA_ID(0x000bb909),
422 CS_AMBA_ID(0x000bb9ec), /* Coresight SoC-600 */
426 MODULE_DEVICE_TABLE(amba
, dynamic_replicator_ids
);
428 static struct amba_driver dynamic_replicator_driver
= {
430 .name
= "coresight-dynamic-replicator",
431 .pm
= &replicator_dev_pm_ops
,
432 .suppress_bind_attrs
= true,
434 .probe
= dynamic_replicator_probe
,
435 .remove
= dynamic_replicator_remove
,
436 .id_table
= dynamic_replicator_ids
,
439 static int __init
replicator_init(void)
441 return coresight_init_driver("replicator", &dynamic_replicator_driver
, &replicator_driver
);
444 static void __exit
replicator_exit(void)
446 coresight_remove_driver(&dynamic_replicator_driver
, &replicator_driver
);
449 module_init(replicator_init
);
450 module_exit(replicator_exit
);
452 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
453 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
454 MODULE_DESCRIPTION("Arm CoreSight Replicator Driver");
455 MODULE_LICENSE("GPL v2");