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 * @atclk: optional clock for the core parts of the funnel.
36 * @csdev: component vitals needed by the framework.
37 * @priority: port selection order.
39 struct funnel_drvdata {
42 struct coresight_device *csdev;
43 unsigned long priority;
46 static int dynamic_funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
51 CS_UNLOCK(drvdata->base);
53 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
54 /* Claim the device only when we enable the first slave */
55 if (!(functl & FUNNEL_ENSx_MASK)) {
56 rc = coresight_claim_device_unlocked(drvdata->base);
61 functl &= ~FUNNEL_HOLDTIME_MASK;
62 functl |= FUNNEL_HOLDTIME;
63 functl |= (1 << port);
64 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
65 writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
67 CS_LOCK(drvdata->base);
71 static int funnel_enable(struct coresight_device *csdev, int inport,
75 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
78 rc = dynamic_funnel_enable_hw(drvdata, inport);
81 dev_dbg(&csdev->dev, "FUNNEL inport %d enabled\n", inport);
85 static void dynamic_funnel_disable_hw(struct funnel_drvdata *drvdata,
90 CS_UNLOCK(drvdata->base);
92 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
93 functl &= ~(1 << inport);
94 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
96 /* Disclaim the device if none of the slaves are now active */
97 if (!(functl & FUNNEL_ENSx_MASK))
98 coresight_disclaim_device_unlocked(drvdata->base);
100 CS_LOCK(drvdata->base);
103 static void funnel_disable(struct coresight_device *csdev, int inport,
106 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
109 dynamic_funnel_disable_hw(drvdata, inport);
111 dev_dbg(&csdev->dev, "FUNNEL inport %d disabled\n", inport);
114 static const struct coresight_ops_link funnel_link_ops = {
115 .enable = funnel_enable,
116 .disable = funnel_disable,
119 static const struct coresight_ops funnel_cs_ops = {
120 .link_ops = &funnel_link_ops,
123 static ssize_t priority_show(struct device *dev,
124 struct device_attribute *attr, char *buf)
126 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
127 unsigned long val = drvdata->priority;
129 return sprintf(buf, "%#lx\n", val);
132 static ssize_t priority_store(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf, size_t size)
138 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
140 ret = kstrtoul(buf, 16, &val);
144 drvdata->priority = val;
147 static DEVICE_ATTR_RW(priority);
149 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
153 CS_UNLOCK(drvdata->base);
154 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
155 CS_LOCK(drvdata->base);
160 static ssize_t funnel_ctrl_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
164 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
166 pm_runtime_get_sync(dev->parent);
168 val = get_funnel_ctrl_hw(drvdata);
170 pm_runtime_put(dev->parent);
172 return sprintf(buf, "%#x\n", val);
174 static DEVICE_ATTR_RO(funnel_ctrl);
176 static struct attribute *coresight_funnel_attrs[] = {
177 &dev_attr_funnel_ctrl.attr,
178 &dev_attr_priority.attr,
181 ATTRIBUTE_GROUPS(coresight_funnel);
183 static int funnel_probe(struct device *dev, struct resource *res)
187 struct coresight_platform_data *pdata = NULL;
188 struct funnel_drvdata *drvdata;
189 struct coresight_desc desc = { 0 };
190 struct device_node *np = dev->of_node;
193 pdata = of_get_coresight_platform_data(dev, np);
195 return PTR_ERR(pdata);
196 dev->platform_data = pdata;
199 if (is_of_node(dev_fwnode(dev)) &&
200 of_device_is_compatible(dev->of_node, "arm,coresight-funnel"))
201 pr_warn_once("Uses OBSOLETE CoreSight funnel binding\n");
203 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
207 drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
208 if (!IS_ERR(drvdata->atclk)) {
209 ret = clk_prepare_enable(drvdata->atclk);
215 * Map the device base for dynamic-funnel, which has been
216 * validated by AMBA core.
219 base = devm_ioremap_resource(dev, res);
222 goto out_disable_clk;
224 drvdata->base = base;
225 desc.groups = coresight_funnel_groups;
228 dev_set_drvdata(dev, drvdata);
230 desc.type = CORESIGHT_DEV_TYPE_LINK;
231 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
232 desc.ops = &funnel_cs_ops;
235 drvdata->csdev = coresight_register(&desc);
236 if (IS_ERR(drvdata->csdev)) {
237 ret = PTR_ERR(drvdata->csdev);
238 goto out_disable_clk;
244 if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
245 clk_disable_unprepare(drvdata->atclk);
250 static int funnel_runtime_suspend(struct device *dev)
252 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
254 if (drvdata && !IS_ERR(drvdata->atclk))
255 clk_disable_unprepare(drvdata->atclk);
260 static int funnel_runtime_resume(struct device *dev)
262 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
264 if (drvdata && !IS_ERR(drvdata->atclk))
265 clk_prepare_enable(drvdata->atclk);
271 static const struct dev_pm_ops funnel_dev_pm_ops = {
272 SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
275 static int static_funnel_probe(struct platform_device *pdev)
279 pm_runtime_get_noresume(&pdev->dev);
280 pm_runtime_set_active(&pdev->dev);
281 pm_runtime_enable(&pdev->dev);
283 /* Static funnel do not have programming base */
284 ret = funnel_probe(&pdev->dev, NULL);
287 pm_runtime_put_noidle(&pdev->dev);
288 pm_runtime_disable(&pdev->dev);
294 static const struct of_device_id static_funnel_match[] = {
295 {.compatible = "arm,coresight-static-funnel"},
299 static struct platform_driver static_funnel_driver = {
300 .probe = static_funnel_probe,
302 .name = "coresight-static-funnel",
303 .of_match_table = static_funnel_match,
304 .pm = &funnel_dev_pm_ops,
305 .suppress_bind_attrs = true,
308 builtin_platform_driver(static_funnel_driver);
310 static int dynamic_funnel_probe(struct amba_device *adev,
311 const struct amba_id *id)
313 return funnel_probe(&adev->dev, &adev->res);
316 static const struct amba_id dynamic_funnel_ids[] = {
322 /* Coresight SoC-600 */
329 static struct amba_driver dynamic_funnel_driver = {
331 .name = "coresight-dynamic-funnel",
332 .owner = THIS_MODULE,
333 .pm = &funnel_dev_pm_ops,
334 .suppress_bind_attrs = true,
336 .probe = dynamic_funnel_probe,
337 .id_table = dynamic_funnel_ids,
339 builtin_amba_driver(dynamic_funnel_driver);