1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/coresight.h>
23 #include <linux/amba/bus.h>
24 #include <linux/clk.h>
26 #include "coresight-priv.h"
28 #define FUNNEL_FUNCTL 0x000
29 #define FUNNEL_PRICTL 0x004
31 #define FUNNEL_HOLDTIME_MASK 0xf00
32 #define FUNNEL_HOLDTIME_SHFT 0x8
33 #define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)
36 * struct funnel_drvdata - specifics associated to a funnel component
37 * @base: memory mapped base address for this component.
38 * @dev: the device entity associated to this component.
39 * @atclk: optional clock for the core parts of the funnel.
40 * @csdev: component vitals needed by the framework.
41 * @priority: port selection order.
43 struct funnel_drvdata {
47 struct coresight_device *csdev;
48 unsigned long priority;
51 static void funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
55 CS_UNLOCK(drvdata->base);
57 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
58 functl &= ~FUNNEL_HOLDTIME_MASK;
59 functl |= FUNNEL_HOLDTIME;
60 functl |= (1 << port);
61 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
62 writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
64 CS_LOCK(drvdata->base);
67 static int funnel_enable(struct coresight_device *csdev, int inport,
70 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
72 funnel_enable_hw(drvdata, inport);
74 dev_info(drvdata->dev, "FUNNEL inport %d enabled\n", inport);
78 static void funnel_disable_hw(struct funnel_drvdata *drvdata, int inport)
82 CS_UNLOCK(drvdata->base);
84 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
85 functl &= ~(1 << inport);
86 writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
88 CS_LOCK(drvdata->base);
91 static void funnel_disable(struct coresight_device *csdev, int inport,
94 struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
96 funnel_disable_hw(drvdata, inport);
98 dev_info(drvdata->dev, "FUNNEL inport %d disabled\n", inport);
101 static const struct coresight_ops_link funnel_link_ops = {
102 .enable = funnel_enable,
103 .disable = funnel_disable,
106 static const struct coresight_ops funnel_cs_ops = {
107 .link_ops = &funnel_link_ops,
110 static ssize_t priority_show(struct device *dev,
111 struct device_attribute *attr, char *buf)
113 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
114 unsigned long val = drvdata->priority;
116 return sprintf(buf, "%#lx\n", val);
119 static ssize_t priority_store(struct device *dev,
120 struct device_attribute *attr,
121 const char *buf, size_t size)
125 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
127 ret = kstrtoul(buf, 16, &val);
131 drvdata->priority = val;
134 static DEVICE_ATTR_RW(priority);
136 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
140 CS_UNLOCK(drvdata->base);
141 functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
142 CS_LOCK(drvdata->base);
147 static ssize_t funnel_ctrl_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
151 struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
153 pm_runtime_get_sync(drvdata->dev);
155 val = get_funnel_ctrl_hw(drvdata);
157 pm_runtime_put(drvdata->dev);
159 return sprintf(buf, "%#x\n", val);
161 static DEVICE_ATTR_RO(funnel_ctrl);
163 static struct attribute *coresight_funnel_attrs[] = {
164 &dev_attr_funnel_ctrl.attr,
165 &dev_attr_priority.attr,
168 ATTRIBUTE_GROUPS(coresight_funnel);
170 static int funnel_probe(struct amba_device *adev, const struct amba_id *id)
174 struct device *dev = &adev->dev;
175 struct coresight_platform_data *pdata = NULL;
176 struct funnel_drvdata *drvdata;
177 struct resource *res = &adev->res;
178 struct coresight_desc *desc;
179 struct device_node *np = adev->dev.of_node;
182 pdata = of_get_coresight_platform_data(dev, np);
184 return PTR_ERR(pdata);
185 adev->dev.platform_data = pdata;
188 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
192 drvdata->dev = &adev->dev;
193 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
194 if (!IS_ERR(drvdata->atclk)) {
195 ret = clk_prepare_enable(drvdata->atclk);
199 dev_set_drvdata(dev, drvdata);
201 /* Validity for the resource is already checked by the AMBA core */
202 base = devm_ioremap_resource(dev, res);
204 return PTR_ERR(base);
206 drvdata->base = base;
207 pm_runtime_put(&adev->dev);
209 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
213 desc->type = CORESIGHT_DEV_TYPE_LINK;
214 desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
215 desc->ops = &funnel_cs_ops;
218 desc->groups = coresight_funnel_groups;
219 drvdata->csdev = coresight_register(desc);
220 if (IS_ERR(drvdata->csdev))
221 return PTR_ERR(drvdata->csdev);
223 dev_info(dev, "FUNNEL initialized\n");
228 static int funnel_runtime_suspend(struct device *dev)
230 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
232 if (drvdata && !IS_ERR(drvdata->atclk))
233 clk_disable_unprepare(drvdata->atclk);
238 static int funnel_runtime_resume(struct device *dev)
240 struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
242 if (drvdata && !IS_ERR(drvdata->atclk))
243 clk_prepare_enable(drvdata->atclk);
249 static const struct dev_pm_ops funnel_dev_pm_ops = {
250 SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
253 static struct amba_id funnel_ids[] = {
261 static struct amba_driver funnel_driver = {
263 .name = "coresight-funnel",
264 .owner = THIS_MODULE,
265 .pm = &funnel_dev_pm_ops,
266 .suppress_bind_attrs = true,
268 .probe = funnel_probe,
269 .id_table = funnel_ids,
272 module_amba_driver(funnel_driver);
274 MODULE_LICENSE("GPL v2");
275 MODULE_DESCRIPTION("CoreSight Funnel driver");