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 pm_runtime_get_sync(drvdata
->dev
);
73 funnel_enable_hw(drvdata
, inport
);
75 dev_info(drvdata
->dev
, "FUNNEL inport %d enabled\n", inport
);
79 static void funnel_disable_hw(struct funnel_drvdata
*drvdata
, int inport
)
83 CS_UNLOCK(drvdata
->base
);
85 functl
= readl_relaxed(drvdata
->base
+ FUNNEL_FUNCTL
);
86 functl
&= ~(1 << inport
);
87 writel_relaxed(functl
, drvdata
->base
+ FUNNEL_FUNCTL
);
89 CS_LOCK(drvdata
->base
);
92 static void funnel_disable(struct coresight_device
*csdev
, int inport
,
95 struct funnel_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
97 funnel_disable_hw(drvdata
, inport
);
98 pm_runtime_put(drvdata
->dev
);
100 dev_info(drvdata
->dev
, "FUNNEL inport %d disabled\n", inport
);
103 static const struct coresight_ops_link funnel_link_ops
= {
104 .enable
= funnel_enable
,
105 .disable
= funnel_disable
,
108 static const struct coresight_ops funnel_cs_ops
= {
109 .link_ops
= &funnel_link_ops
,
112 static ssize_t
priority_show(struct device
*dev
,
113 struct device_attribute
*attr
, char *buf
)
115 struct funnel_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
116 unsigned long val
= drvdata
->priority
;
118 return sprintf(buf
, "%#lx\n", val
);
121 static ssize_t
priority_store(struct device
*dev
,
122 struct device_attribute
*attr
,
123 const char *buf
, size_t size
)
127 struct funnel_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
129 ret
= kstrtoul(buf
, 16, &val
);
133 drvdata
->priority
= val
;
136 static DEVICE_ATTR_RW(priority
);
138 static u32
get_funnel_ctrl_hw(struct funnel_drvdata
*drvdata
)
142 CS_UNLOCK(drvdata
->base
);
143 functl
= readl_relaxed(drvdata
->base
+ FUNNEL_FUNCTL
);
144 CS_LOCK(drvdata
->base
);
149 static ssize_t
funnel_ctrl_show(struct device
*dev
,
150 struct device_attribute
*attr
, char *buf
)
153 struct funnel_drvdata
*drvdata
= dev_get_drvdata(dev
->parent
);
155 pm_runtime_get_sync(drvdata
->dev
);
157 val
= get_funnel_ctrl_hw(drvdata
);
159 pm_runtime_put(drvdata
->dev
);
161 return sprintf(buf
, "%#x\n", val
);
163 static DEVICE_ATTR_RO(funnel_ctrl
);
165 static struct attribute
*coresight_funnel_attrs
[] = {
166 &dev_attr_funnel_ctrl
.attr
,
167 &dev_attr_priority
.attr
,
170 ATTRIBUTE_GROUPS(coresight_funnel
);
172 static int funnel_probe(struct amba_device
*adev
, const struct amba_id
*id
)
176 struct device
*dev
= &adev
->dev
;
177 struct coresight_platform_data
*pdata
= NULL
;
178 struct funnel_drvdata
*drvdata
;
179 struct resource
*res
= &adev
->res
;
180 struct coresight_desc
*desc
;
181 struct device_node
*np
= adev
->dev
.of_node
;
184 pdata
= of_get_coresight_platform_data(dev
, np
);
186 return PTR_ERR(pdata
);
187 adev
->dev
.platform_data
= pdata
;
190 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
194 drvdata
->dev
= &adev
->dev
;
195 drvdata
->atclk
= devm_clk_get(&adev
->dev
, "atclk"); /* optional */
196 if (!IS_ERR(drvdata
->atclk
)) {
197 ret
= clk_prepare_enable(drvdata
->atclk
);
201 dev_set_drvdata(dev
, drvdata
);
203 /* Validity for the resource is already checked by the AMBA core */
204 base
= devm_ioremap_resource(dev
, res
);
206 return PTR_ERR(base
);
208 drvdata
->base
= base
;
209 pm_runtime_put(&adev
->dev
);
211 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
215 desc
->type
= CORESIGHT_DEV_TYPE_LINK
;
216 desc
->subtype
.link_subtype
= CORESIGHT_DEV_SUBTYPE_LINK_MERG
;
217 desc
->ops
= &funnel_cs_ops
;
220 desc
->groups
= coresight_funnel_groups
;
221 drvdata
->csdev
= coresight_register(desc
);
222 if (IS_ERR(drvdata
->csdev
))
223 return PTR_ERR(drvdata
->csdev
);
225 dev_info(dev
, "FUNNEL initialized\n");
229 static int funnel_remove(struct amba_device
*adev
)
231 struct funnel_drvdata
*drvdata
= amba_get_drvdata(adev
);
233 coresight_unregister(drvdata
->csdev
);
238 static int funnel_runtime_suspend(struct device
*dev
)
240 struct funnel_drvdata
*drvdata
= dev_get_drvdata(dev
);
242 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
243 clk_disable_unprepare(drvdata
->atclk
);
248 static int funnel_runtime_resume(struct device
*dev
)
250 struct funnel_drvdata
*drvdata
= dev_get_drvdata(dev
);
252 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
253 clk_prepare_enable(drvdata
->atclk
);
259 static const struct dev_pm_ops funnel_dev_pm_ops
= {
260 SET_RUNTIME_PM_OPS(funnel_runtime_suspend
, funnel_runtime_resume
, NULL
)
263 static struct amba_id funnel_ids
[] = {
271 static struct amba_driver funnel_driver
= {
273 .name
= "coresight-funnel",
274 .owner
= THIS_MODULE
,
275 .pm
= &funnel_dev_pm_ops
,
277 .probe
= funnel_probe
,
278 .remove
= funnel_remove
,
279 .id_table
= funnel_ids
,
282 module_amba_driver(funnel_driver
);
284 MODULE_LICENSE("GPL v2");
285 MODULE_DESCRIPTION("CoreSight Funnel driver");