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
33 * struct funnel_drvdata - specifics associated to a funnel component
34 * @base: memory mapped base address for this component.
35 * @dev: the device entity associated to this component.
36 * @atclk: optional clock for the core parts of the funnel.
37 * @csdev: component vitals needed by the framework.
38 * @priority: port selection order.
40 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(drvdata->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(drvdata->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(drvdata->dev);
170 val = get_funnel_ctrl_hw(drvdata);
172 pm_runtime_put(drvdata->dev);
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 };
192 struct device_node *np = dev->of_node;
195 pdata = of_get_coresight_platform_data(dev, np);
197 return PTR_ERR(pdata);
198 dev->platform_data = pdata;
201 if (of_device_is_compatible(np, "arm,coresight-funnel"))
202 pr_warn_once("Uses OBSOLETE CoreSight funnel binding\n");
204 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
209 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
210 if (!IS_ERR(drvdata->atclk)) {
211 ret = clk_prepare_enable(drvdata->atclk);
217 * Map the device base for dynamic-funnel, which has been
218 * validated by AMBA core.
221 base = devm_ioremap_resource(dev, res);
224 goto out_disable_clk;
226 drvdata->base = base;
227 desc.groups = coresight_funnel_groups;
230 dev_set_drvdata(dev, drvdata);
232 desc.type = CORESIGHT_DEV_TYPE_LINK;
233 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
234 desc.ops = &funnel_cs_ops;
237 drvdata->csdev = coresight_register(&desc);
238 if (IS_ERR(drvdata->csdev)) {
239 ret = PTR_ERR(drvdata->csdev);
240 goto out_disable_clk;
246 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
247 clk_disable_unprepare(drvdata->atclk);
252 static int funnel_runtime_suspend(struct device *dev)
254 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
256 if (drvdata && !IS_ERR(drvdata->atclk))
257 clk_disable_unprepare(drvdata->atclk);
262 static int funnel_runtime_resume(struct device *dev)
264 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
266 if (drvdata && !IS_ERR(drvdata->atclk))
267 clk_prepare_enable(drvdata->atclk);
273 static const struct dev_pm_ops funnel_dev_pm_ops = {
274 SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
277 static int static_funnel_probe(struct platform_device *pdev)
281 pm_runtime_get_noresume(&pdev->dev);
282 pm_runtime_set_active(&pdev->dev);
283 pm_runtime_enable(&pdev->dev);
285 /* Static funnel do not have programming base */
286 ret = funnel_probe(&pdev->dev, NULL);
289 pm_runtime_put_noidle(&pdev->dev);
290 pm_runtime_disable(&pdev->dev);
296 static const struct of_device_id static_funnel_match[] = {
297 {.compatible = "arm,coresight-static-funnel"},
301 static struct platform_driver static_funnel_driver = {
302 .probe = static_funnel_probe,
304 .name = "coresight-static-funnel",
305 .of_match_table = static_funnel_match,
306 .pm = &funnel_dev_pm_ops,
307 .suppress_bind_attrs = true,
310 builtin_platform_driver(static_funnel_driver);
312 static int dynamic_funnel_probe(struct amba_device *adev,
313 const struct amba_id *id)
315 return funnel_probe(&adev->dev, &adev->res);
318 static const struct amba_id dynamic_funnel_ids[] = {
324 /* Coresight SoC-600 */
331 static struct amba_driver dynamic_funnel_driver = {
333 .name = "coresight-dynamic-funnel",
334 .owner = THIS_MODULE,
335 .pm = &funnel_dev_pm_ops,
336 .suppress_bind_attrs = true,
338 .probe = dynamic_funnel_probe,
339 .id_table = dynamic_funnel_ids,
341 builtin_amba_driver(dynamic_funnel_driver);