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>
15 #include <linux/pm_runtime.h>
16 #include <linux/coresight.h>
17 #include <linux/amba/bus.h>
18 #include <linux/clk.h>
20 #include "coresight-priv.h"
22 #define FUNNEL_FUNCTL 0x000
23 #define FUNNEL_PRICTL 0x004
25 #define FUNNEL_HOLDTIME_MASK 0xf00
26 #define FUNNEL_HOLDTIME_SHFT 0x8
27 #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
28 #define FUNNEL_ENSx_MASK 0xff
31 * struct funnel_drvdata - specifics associated to a funnel component
32 * @base: memory mapped base address for this component.
33 * @dev: the device entity associated to this component.
34 * @atclk: optional clock for the core parts of the funnel.
35 * @csdev: component vitals needed by the framework.
36 * @priority: port selection order.
38 struct funnel_drvdata {
42 struct coresight_device *csdev;
43 unsigned long priority;
46 static int 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);
77 rc = funnel_enable_hw(drvdata, inport);
80 dev_dbg(drvdata->dev, "FUNNEL inport %d enabled\n", inport);
84 static void funnel_disable_hw(struct funnel_drvdata *drvdata, int inport)
88 CS_UNLOCK(drvdata->base);
90 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
91 functl &= ~(1 << inport);
92 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
94 /* Disclaim the device if none of the slaves are now active */
95 if (!(functl & FUNNEL_ENSx_MASK))
96 coresight_disclaim_device_unlocked(drvdata->base);
98 CS_LOCK(drvdata->base);
101 static void funnel_disable(struct coresight_device *csdev, int inport,
104 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
106 funnel_disable_hw(drvdata, inport);
108 dev_dbg(drvdata->dev, "FUNNEL inport %d disabled\n", inport);
111 static const struct coresight_ops_link funnel_link_ops = {
112 .enable = funnel_enable,
113 .disable = funnel_disable,
116 static const struct coresight_ops funnel_cs_ops = {
117 .link_ops = &funnel_link_ops,
120 static ssize_t priority_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
123 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
124 unsigned long val = drvdata->priority;
126 return sprintf(buf, "%#lx\n", val);
129 static ssize_t priority_store(struct device *dev,
130 struct device_attribute *attr,
131 const char *buf, size_t size)
135 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
137 ret = kstrtoul(buf, 16, &val);
141 drvdata->priority = val;
144 static DEVICE_ATTR_RW(priority);
146 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
150 CS_UNLOCK(drvdata->base);
151 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
152 CS_LOCK(drvdata->base);
157 static ssize_t funnel_ctrl_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
161 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
163 pm_runtime_get_sync(drvdata->dev);
165 val = get_funnel_ctrl_hw(drvdata);
167 pm_runtime_put(drvdata->dev);
169 return sprintf(buf, "%#x\n", val);
171 static DEVICE_ATTR_RO(funnel_ctrl);
173 static struct attribute *coresight_funnel_attrs[] = {
174 &dev_attr_funnel_ctrl.attr,
175 &dev_attr_priority.attr,
178 ATTRIBUTE_GROUPS(coresight_funnel);
180 static int funnel_probe(struct amba_device *adev, const struct amba_id *id)
184 struct device *dev = &adev->dev;
185 struct coresight_platform_data *pdata = NULL;
186 struct funnel_drvdata *drvdata;
187 struct resource *res = &adev->res;
188 struct coresight_desc desc = { 0 };
189 struct device_node *np = adev->dev.of_node;
192 pdata = of_get_coresight_platform_data(dev, np);
194 return PTR_ERR(pdata);
195 adev->dev.platform_data = pdata;
198 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
202 drvdata->dev = &adev->dev;
203 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
204 if (!IS_ERR(drvdata->atclk)) {
205 ret = clk_prepare_enable(drvdata->atclk);
209 dev_set_drvdata(dev, drvdata);
211 /* Validity for the resource is already checked by the AMBA core */
212 base = devm_ioremap_resource(dev, res);
214 return PTR_ERR(base);
216 drvdata->base = base;
217 pm_runtime_put(&adev->dev);
219 desc.type = CORESIGHT_DEV_TYPE_LINK;
220 desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
221 desc.ops = &funnel_cs_ops;
224 desc.groups = coresight_funnel_groups;
225 drvdata->csdev = coresight_register(&desc);
227 return PTR_ERR_OR_ZERO(drvdata->csdev);
231 static int funnel_runtime_suspend(struct device *dev)
233 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
235 if (drvdata && !IS_ERR(drvdata->atclk))
236 clk_disable_unprepare(drvdata->atclk);
241 static int funnel_runtime_resume(struct device *dev)
243 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
245 if (drvdata && !IS_ERR(drvdata->atclk))
246 clk_prepare_enable(drvdata->atclk);
252 static const struct dev_pm_ops funnel_dev_pm_ops = {
253 SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
256 static const struct amba_id funnel_ids[] = {
262 /* Coresight SoC-600 */
269 static struct amba_driver funnel_driver = {
271 .name = "coresight-funnel",
272 .owner = THIS_MODULE,
273 .pm = &funnel_dev_pm_ops,
274 .suppress_bind_attrs = true,
276 .probe = funnel_probe,
277 .id_table = funnel_ids,
279 builtin_amba_driver(funnel_driver);