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/kernel.h>
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
14 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/coresight.h>
19 #include <linux/amba/bus.h>
20 #include <linux/clk.h>
22 #include "coresight-priv.h"
24 #define FUNNEL_FUNCTL 0x000
25 #define FUNNEL_PRICTL 0x004
27 #define FUNNEL_HOLDTIME_MASK 0xf00
28 #define FUNNEL_HOLDTIME_SHFT 0x8
29 #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
30 #define FUNNEL_ENSx_MASK 0xff
32 DEFINE_CORESIGHT_DEVLIST(funnel_devs, "funnel");
35 * struct funnel_drvdata - specifics associated to a funnel component
36 * @base: memory mapped base address for this component.
37 * @atclk: optional clock for the core parts of the funnel.
38 * @csdev: component vitals needed by the framework.
39 * @priority: port selection order.
41 struct funnel_drvdata {
44 struct coresight_device *csdev;
45 unsigned long priority;
48 static int dynamic_funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
53 CS_UNLOCK(drvdata->base);
55 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
56 /* Claim the device only when we enable the first slave */
57 if (!(functl & FUNNEL_ENSx_MASK)) {
58 rc = coresight_claim_device_unlocked(drvdata->base);
63 functl &= ~FUNNEL_HOLDTIME_MASK;
64 functl |= FUNNEL_HOLDTIME;
65 functl |= (1 << port);
66 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
67 writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
69 CS_LOCK(drvdata->base);
73 static int funnel_enable(struct coresight_device *csdev, int inport,
77 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
80 rc = dynamic_funnel_enable_hw(drvdata, inport);
83 dev_dbg(&csdev->dev, "FUNNEL inport %d enabled\n", inport);
87 static void dynamic_funnel_disable_hw(struct funnel_drvdata *drvdata,
92 CS_UNLOCK(drvdata->base);
94 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
95 functl &= ~(1 << inport);
96 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
98 /* Disclaim the device if none of the slaves are now active */
99 if (!(functl & FUNNEL_ENSx_MASK))
100 coresight_disclaim_device_unlocked(drvdata->base);
102 CS_LOCK(drvdata->base);
105 static void funnel_disable(struct coresight_device *csdev, int inport,
108 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
111 dynamic_funnel_disable_hw(drvdata, inport);
113 dev_dbg(&csdev->dev, "FUNNEL inport %d disabled\n", inport);
116 static const struct coresight_ops_link funnel_link_ops = {
117 .enable = funnel_enable,
118 .disable = funnel_disable,
121 static const struct coresight_ops funnel_cs_ops = {
122 .link_ops = &funnel_link_ops,
125 static ssize_t priority_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
128 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
129 unsigned long val = drvdata->priority;
131 return sprintf(buf, "%#lx\n", val);
134 static ssize_t priority_store(struct device *dev,
135 struct device_attribute *attr,
136 const char *buf, size_t size)
140 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
142 ret = kstrtoul(buf, 16, &val);
146 drvdata->priority = val;
149 static DEVICE_ATTR_RW(priority);
151 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
155 CS_UNLOCK(drvdata->base);
156 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
157 CS_LOCK(drvdata->base);
162 static ssize_t funnel_ctrl_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
166 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
168 pm_runtime_get_sync(dev->parent);
170 val = get_funnel_ctrl_hw(drvdata);
172 pm_runtime_put(dev->parent);
174 return sprintf(buf, "%#x\n", val);
176 static DEVICE_ATTR_RO(funnel_ctrl);
178 static struct attribute *coresight_funnel_attrs[] = {
179 &dev_attr_funnel_ctrl.attr,
180 &dev_attr_priority.attr,
183 ATTRIBUTE_GROUPS(coresight_funnel);
185 static int funnel_probe(struct device *dev, struct resource *res)
189 struct coresight_platform_data *pdata = NULL;
190 struct funnel_drvdata *drvdata;
191 struct coresight_desc desc = { 0 };
193 if (is_of_node(dev_fwnode(dev)) &&
194 of_device_is_compatible(dev->of_node, "arm,coresight-funnel"))
195 pr_warn_once("Uses OBSOLETE CoreSight funnel binding\n");
197 desc.name = coresight_alloc_device_name(&funnel_devs, dev);
201 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
205 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
206 if (!IS_ERR(drvdata->atclk)) {
207 ret = clk_prepare_enable(drvdata->atclk);
213 * Map the device base for dynamic-funnel, which has been
214 * validated by AMBA core.
217 base = devm_ioremap_resource(dev, res);
220 goto out_disable_clk;
222 drvdata->base = base;
223 desc.groups = coresight_funnel_groups;
226 dev_set_drvdata(dev, drvdata);
228 pdata = coresight_get_platform_data(dev);
230 ret = PTR_ERR(pdata);
231 goto out_disable_clk;
233 dev->platform_data = pdata;
235 desc.type = CORESIGHT_DEV_TYPE_LINK;
236 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
237 desc.ops = &funnel_cs_ops;
240 drvdata->csdev = coresight_register(&desc);
241 if (IS_ERR(drvdata->csdev)) {
242 ret = PTR_ERR(drvdata->csdev);
243 goto out_disable_clk;
250 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
251 clk_disable_unprepare(drvdata->atclk);
256 static int funnel_runtime_suspend(struct device *dev)
258 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
260 if (drvdata && !IS_ERR(drvdata->atclk))
261 clk_disable_unprepare(drvdata->atclk);
266 static int funnel_runtime_resume(struct device *dev)
268 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
270 if (drvdata && !IS_ERR(drvdata->atclk))
271 clk_prepare_enable(drvdata->atclk);
277 static const struct dev_pm_ops funnel_dev_pm_ops = {
278 SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
281 static int static_funnel_probe(struct platform_device *pdev)
285 pm_runtime_get_noresume(&pdev->dev);
286 pm_runtime_set_active(&pdev->dev);
287 pm_runtime_enable(&pdev->dev);
289 /* Static funnel do not have programming base */
290 ret = funnel_probe(&pdev->dev, NULL);
293 pm_runtime_put_noidle(&pdev->dev);
294 pm_runtime_disable(&pdev->dev);
300 static const struct of_device_id static_funnel_match[] = {
301 {.compatible = "arm,coresight-static-funnel"},
305 static struct platform_driver static_funnel_driver = {
306 .probe = static_funnel_probe,
308 .name = "coresight-static-funnel",
309 .of_match_table = static_funnel_match,
310 .pm = &funnel_dev_pm_ops,
311 .suppress_bind_attrs = true,
314 builtin_platform_driver(static_funnel_driver);
316 static int dynamic_funnel_probe(struct amba_device *adev,
317 const struct amba_id *id)
319 return funnel_probe(&adev->dev, &adev->res);
322 static const struct amba_id dynamic_funnel_ids[] = {
328 /* Coresight SoC-600 */
335 static struct amba_driver dynamic_funnel_driver = {
337 .name = "coresight-dynamic-funnel",
338 .owner = THIS_MODULE,
339 .pm = &funnel_dev_pm_ops,
340 .suppress_bind_attrs = true,
342 .probe = dynamic_funnel_probe,
343 .id_table = dynamic_funnel_ids,
345 builtin_amba_driver(dynamic_funnel_driver);