1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
5 * Description: CoreSight Funnel driver
8 #include <linux/acpi.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/types.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
15 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/coresight.h>
20 #include <linux/amba/bus.h>
21 #include <linux/clk.h>
23 #include "coresight-priv.h"
25 #define FUNNEL_FUNCTL 0x000
26 #define FUNNEL_PRICTL 0x004
28 #define FUNNEL_HOLDTIME_MASK 0xf00
29 #define FUNNEL_HOLDTIME_SHFT 0x8
30 #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
31 #define FUNNEL_ENSx_MASK 0xff
33 DEFINE_CORESIGHT_DEVLIST(funnel_devs, "funnel");
36 * struct funnel_drvdata - specifics associated to a funnel component
37 * @base: memory mapped base address for this component.
38 * @atclk: optional clock for the core parts of the funnel.
39 * @csdev: component vitals needed by the framework.
40 * @priority: port selection order.
41 * @spinlock: serialize enable/disable operations.
43 struct funnel_drvdata {
46 struct coresight_device *csdev;
47 unsigned long priority;
51 static int dynamic_funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
55 struct coresight_device *csdev = drvdata->csdev;
57 CS_UNLOCK(drvdata->base);
59 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
60 /* Claim the device only when we enable the first slave */
61 if (!(functl & FUNNEL_ENSx_MASK)) {
62 rc = coresight_claim_device_unlocked(csdev);
67 functl &= ~FUNNEL_HOLDTIME_MASK;
68 functl |= FUNNEL_HOLDTIME;
69 functl |= (1 << port);
70 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
71 writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
73 CS_LOCK(drvdata->base);
77 static int funnel_enable(struct coresight_device *csdev,
78 struct coresight_connection *in,
79 struct coresight_connection *out)
82 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
84 bool first_enable = false;
86 spin_lock_irqsave(&drvdata->spinlock, flags);
87 if (atomic_read(&in->dest_refcnt) == 0) {
89 rc = dynamic_funnel_enable_hw(drvdata, in->dest_port);
94 atomic_inc(&in->dest_refcnt);
95 spin_unlock_irqrestore(&drvdata->spinlock, flags);
98 dev_dbg(&csdev->dev, "FUNNEL inport %d enabled\n",
103 static void dynamic_funnel_disable_hw(struct funnel_drvdata *drvdata,
107 struct coresight_device *csdev = drvdata->csdev;
109 CS_UNLOCK(drvdata->base);
111 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
112 functl &= ~(1 << inport);
113 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
115 /* Disclaim the device if none of the slaves are now active */
116 if (!(functl & FUNNEL_ENSx_MASK))
117 coresight_disclaim_device_unlocked(csdev);
119 CS_LOCK(drvdata->base);
122 static void funnel_disable(struct coresight_device *csdev,
123 struct coresight_connection *in,
124 struct coresight_connection *out)
126 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
128 bool last_disable = false;
130 spin_lock_irqsave(&drvdata->spinlock, flags);
131 if (atomic_dec_return(&in->dest_refcnt) == 0) {
133 dynamic_funnel_disable_hw(drvdata, in->dest_port);
136 spin_unlock_irqrestore(&drvdata->spinlock, flags);
139 dev_dbg(&csdev->dev, "FUNNEL inport %d disabled\n",
143 static const struct coresight_ops_link funnel_link_ops = {
144 .enable = funnel_enable,
145 .disable = funnel_disable,
148 static const struct coresight_ops funnel_cs_ops = {
149 .link_ops = &funnel_link_ops,
152 static ssize_t priority_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
156 unsigned long val = drvdata->priority;
158 return sprintf(buf, "%#lx\n", val);
161 static ssize_t priority_store(struct device *dev,
162 struct device_attribute *attr,
163 const char *buf, size_t size)
167 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
169 ret = kstrtoul(buf, 16, &val);
173 drvdata->priority = val;
176 static DEVICE_ATTR_RW(priority);
178 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
182 CS_UNLOCK(drvdata->base);
183 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
184 CS_LOCK(drvdata->base);
189 static ssize_t funnel_ctrl_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
193 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
195 pm_runtime_get_sync(dev->parent);
197 val = get_funnel_ctrl_hw(drvdata);
199 pm_runtime_put(dev->parent);
201 return sprintf(buf, "%#x\n", val);
203 static DEVICE_ATTR_RO(funnel_ctrl);
205 static struct attribute *coresight_funnel_attrs[] = {
206 &dev_attr_funnel_ctrl.attr,
207 &dev_attr_priority.attr,
210 ATTRIBUTE_GROUPS(coresight_funnel);
212 static int funnel_probe(struct device *dev, struct resource *res)
216 struct coresight_platform_data *pdata = NULL;
217 struct funnel_drvdata *drvdata;
218 struct coresight_desc desc = { 0 };
220 if (is_of_node(dev_fwnode(dev)) &&
221 of_device_is_compatible(dev->of_node, "arm,coresight-funnel"))
222 dev_warn_once(dev, "Uses OBSOLETE CoreSight funnel binding\n");
224 desc.name = coresight_alloc_device_name(&funnel_devs, dev);
228 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
232 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
233 if (!IS_ERR(drvdata->atclk)) {
234 ret = clk_prepare_enable(drvdata->atclk);
240 * Map the device base for dynamic-funnel, which has been
241 * validated by AMBA core.
244 base = devm_ioremap_resource(dev, res);
247 goto out_disable_clk;
249 drvdata->base = base;
250 desc.groups = coresight_funnel_groups;
251 desc.access = CSDEV_ACCESS_IOMEM(base);
254 dev_set_drvdata(dev, drvdata);
256 pdata = coresight_get_platform_data(dev);
258 ret = PTR_ERR(pdata);
259 goto out_disable_clk;
261 dev->platform_data = pdata;
263 spin_lock_init(&drvdata->spinlock);
264 desc.type = CORESIGHT_DEV_TYPE_LINK;
265 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
266 desc.ops = &funnel_cs_ops;
269 drvdata->csdev = coresight_register(&desc);
270 if (IS_ERR(drvdata->csdev)) {
271 ret = PTR_ERR(drvdata->csdev);
272 goto out_disable_clk;
279 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
280 clk_disable_unprepare(drvdata->atclk);
284 static int funnel_remove(struct device *dev)
286 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
288 coresight_unregister(drvdata->csdev);
294 static int funnel_runtime_suspend(struct device *dev)
296 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
298 if (drvdata && !IS_ERR(drvdata->atclk))
299 clk_disable_unprepare(drvdata->atclk);
304 static int funnel_runtime_resume(struct device *dev)
306 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
308 if (drvdata && !IS_ERR(drvdata->atclk))
309 clk_prepare_enable(drvdata->atclk);
315 static const struct dev_pm_ops funnel_dev_pm_ops = {
316 SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
319 static int static_funnel_probe(struct platform_device *pdev)
323 pm_runtime_get_noresume(&pdev->dev);
324 pm_runtime_set_active(&pdev->dev);
325 pm_runtime_enable(&pdev->dev);
327 /* Static funnel do not have programming base */
328 ret = funnel_probe(&pdev->dev, NULL);
331 pm_runtime_put_noidle(&pdev->dev);
332 pm_runtime_disable(&pdev->dev);
338 static int static_funnel_remove(struct platform_device *pdev)
340 funnel_remove(&pdev->dev);
341 pm_runtime_disable(&pdev->dev);
345 static const struct of_device_id static_funnel_match[] = {
346 {.compatible = "arm,coresight-static-funnel"},
350 MODULE_DEVICE_TABLE(of, static_funnel_match);
353 static const struct acpi_device_id static_funnel_ids[] = {
358 MODULE_DEVICE_TABLE(acpi, static_funnel_ids);
361 static struct platform_driver static_funnel_driver = {
362 .probe = static_funnel_probe,
363 .remove = static_funnel_remove,
365 .name = "coresight-static-funnel",
366 /* THIS_MODULE is taken care of by platform_driver_register() */
367 .of_match_table = static_funnel_match,
368 .acpi_match_table = ACPI_PTR(static_funnel_ids),
369 .pm = &funnel_dev_pm_ops,
370 .suppress_bind_attrs = true,
374 static int dynamic_funnel_probe(struct amba_device *adev,
375 const struct amba_id *id)
377 return funnel_probe(&adev->dev, &adev->res);
380 static void dynamic_funnel_remove(struct amba_device *adev)
382 funnel_remove(&adev->dev);
385 static const struct amba_id dynamic_funnel_ids[] = {
391 /* Coresight SoC-600 */
398 MODULE_DEVICE_TABLE(amba, dynamic_funnel_ids);
400 static struct amba_driver dynamic_funnel_driver = {
402 .name = "coresight-dynamic-funnel",
403 .owner = THIS_MODULE,
404 .pm = &funnel_dev_pm_ops,
405 .suppress_bind_attrs = true,
407 .probe = dynamic_funnel_probe,
408 .remove = dynamic_funnel_remove,
409 .id_table = dynamic_funnel_ids,
412 static int __init funnel_init(void)
416 ret = platform_driver_register(&static_funnel_driver);
418 pr_info("Error registering platform driver\n");
422 ret = amba_driver_register(&dynamic_funnel_driver);
424 pr_info("Error registering amba driver\n");
425 platform_driver_unregister(&static_funnel_driver);
431 static void __exit funnel_exit(void)
433 platform_driver_unregister(&static_funnel_driver);
434 amba_driver_unregister(&dynamic_funnel_driver);
437 module_init(funnel_init);
438 module_exit(funnel_exit);
440 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
441 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
442 MODULE_DESCRIPTION("Arm CoreSight Funnel Driver");
443 MODULE_LICENSE("GPL v2");