3423042e7a528e94dbe08f6170314007eeec5fef
[linux-2.6-microblaze.git] / drivers / hwtracing / coresight / coresight-funnel.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
4  *
5  * Description: CoreSight Funnel driver
6  */
7
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>
13 #include <linux/fs.h>
14 #include <linux/slab.h>
15 #include <linux/of.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>
21
22 #include "coresight-priv.h"
23
24 #define FUNNEL_FUNCTL           0x000
25 #define FUNNEL_PRICTL           0x004
26
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
31
32 /**
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.
38  */
39 struct funnel_drvdata {
40         void __iomem            *base;
41         struct clk              *atclk;
42         struct coresight_device *csdev;
43         unsigned long           priority;
44 };
45
46 static int dynamic_funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
47 {
48         u32 functl;
49         int rc = 0;
50
51         CS_UNLOCK(drvdata->base);
52
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);
57                 if (rc)
58                         goto done;
59         }
60
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);
66 done:
67         CS_LOCK(drvdata->base);
68         return rc;
69 }
70
71 static int funnel_enable(struct coresight_device *csdev, int inport,
72                          int outport)
73 {
74         int rc = 0;
75         struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
76
77         if (drvdata->base)
78                 rc = dynamic_funnel_enable_hw(drvdata, inport);
79
80         if (!rc)
81                 dev_dbg(&csdev->dev, "FUNNEL inport %d enabled\n", inport);
82         return rc;
83 }
84
85 static void dynamic_funnel_disable_hw(struct funnel_drvdata *drvdata,
86                                       int inport)
87 {
88         u32 functl;
89
90         CS_UNLOCK(drvdata->base);
91
92         functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
93         functl &= ~(1 << inport);
94         writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
95
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);
99
100         CS_LOCK(drvdata->base);
101 }
102
103 static void funnel_disable(struct coresight_device *csdev, int inport,
104                            int outport)
105 {
106         struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
107
108         if (drvdata->base)
109                 dynamic_funnel_disable_hw(drvdata, inport);
110
111         dev_dbg(&csdev->dev, "FUNNEL inport %d disabled\n", inport);
112 }
113
114 static const struct coresight_ops_link funnel_link_ops = {
115         .enable         = funnel_enable,
116         .disable        = funnel_disable,
117 };
118
119 static const struct coresight_ops funnel_cs_ops = {
120         .link_ops       = &funnel_link_ops,
121 };
122
123 static ssize_t priority_show(struct device *dev,
124                              struct device_attribute *attr, char *buf)
125 {
126         struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
127         unsigned long val = drvdata->priority;
128
129         return sprintf(buf, "%#lx\n", val);
130 }
131
132 static ssize_t priority_store(struct device *dev,
133                               struct device_attribute *attr,
134                               const char *buf, size_t size)
135 {
136         int ret;
137         unsigned long val;
138         struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
139
140         ret = kstrtoul(buf, 16, &val);
141         if (ret)
142                 return ret;
143
144         drvdata->priority = val;
145         return size;
146 }
147 static DEVICE_ATTR_RW(priority);
148
149 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
150 {
151         u32 functl;
152
153         CS_UNLOCK(drvdata->base);
154         functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
155         CS_LOCK(drvdata->base);
156
157         return functl;
158 }
159
160 static ssize_t funnel_ctrl_show(struct device *dev,
161                              struct device_attribute *attr, char *buf)
162 {
163         u32 val;
164         struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
165
166         pm_runtime_get_sync(dev->parent);
167
168         val = get_funnel_ctrl_hw(drvdata);
169
170         pm_runtime_put(dev->parent);
171
172         return sprintf(buf, "%#x\n", val);
173 }
174 static DEVICE_ATTR_RO(funnel_ctrl);
175
176 static struct attribute *coresight_funnel_attrs[] = {
177         &dev_attr_funnel_ctrl.attr,
178         &dev_attr_priority.attr,
179         NULL,
180 };
181 ATTRIBUTE_GROUPS(coresight_funnel);
182
183 static int funnel_probe(struct device *dev, struct resource *res)
184 {
185         int ret;
186         void __iomem *base;
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;
191
192         if (np) {
193                 pdata = of_get_coresight_platform_data(dev, np);
194                 if (IS_ERR(pdata))
195                         return PTR_ERR(pdata);
196                 dev->platform_data = pdata;
197         }
198
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");
202
203         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
204         if (!drvdata)
205                 return -ENOMEM;
206
207         drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
208         if (!IS_ERR(drvdata->atclk)) {
209                 ret = clk_prepare_enable(drvdata->atclk);
210                 if (ret)
211                         return ret;
212         }
213
214         /*
215          * Map the device base for dynamic-funnel, which has been
216          * validated by AMBA core.
217          */
218         if (res) {
219                 base = devm_ioremap_resource(dev, res);
220                 if (IS_ERR(base)) {
221                         ret = PTR_ERR(base);
222                         goto out_disable_clk;
223                 }
224                 drvdata->base = base;
225                 desc.groups = coresight_funnel_groups;
226         }
227
228         dev_set_drvdata(dev, drvdata);
229
230         desc.type = CORESIGHT_DEV_TYPE_LINK;
231         desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
232         desc.ops = &funnel_cs_ops;
233         desc.pdata = pdata;
234         desc.dev = dev;
235         drvdata->csdev = coresight_register(&desc);
236         if (IS_ERR(drvdata->csdev)) {
237                 ret = PTR_ERR(drvdata->csdev);
238                 goto out_disable_clk;
239         }
240
241         pm_runtime_put(dev);
242
243 out_disable_clk:
244         if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
245                 clk_disable_unprepare(drvdata->atclk);
246         return ret;
247 }
248
249 #ifdef CONFIG_PM
250 static int funnel_runtime_suspend(struct device *dev)
251 {
252         struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
253
254         if (drvdata && !IS_ERR(drvdata->atclk))
255                 clk_disable_unprepare(drvdata->atclk);
256
257         return 0;
258 }
259
260 static int funnel_runtime_resume(struct device *dev)
261 {
262         struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
263
264         if (drvdata && !IS_ERR(drvdata->atclk))
265                 clk_prepare_enable(drvdata->atclk);
266
267         return 0;
268 }
269 #endif
270
271 static const struct dev_pm_ops funnel_dev_pm_ops = {
272         SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
273 };
274
275 static int static_funnel_probe(struct platform_device *pdev)
276 {
277         int ret;
278
279         pm_runtime_get_noresume(&pdev->dev);
280         pm_runtime_set_active(&pdev->dev);
281         pm_runtime_enable(&pdev->dev);
282
283         /* Static funnel do not have programming base */
284         ret = funnel_probe(&pdev->dev, NULL);
285
286         if (ret) {
287                 pm_runtime_put_noidle(&pdev->dev);
288                 pm_runtime_disable(&pdev->dev);
289         }
290
291         return ret;
292 }
293
294 static const struct of_device_id static_funnel_match[] = {
295         {.compatible = "arm,coresight-static-funnel"},
296         {}
297 };
298
299 static struct platform_driver static_funnel_driver = {
300         .probe          = static_funnel_probe,
301         .driver         = {
302                 .name   = "coresight-static-funnel",
303                 .of_match_table = static_funnel_match,
304                 .pm     = &funnel_dev_pm_ops,
305                 .suppress_bind_attrs = true,
306         },
307 };
308 builtin_platform_driver(static_funnel_driver);
309
310 static int dynamic_funnel_probe(struct amba_device *adev,
311                                 const struct amba_id *id)
312 {
313         return funnel_probe(&adev->dev, &adev->res);
314 }
315
316 static const struct amba_id dynamic_funnel_ids[] = {
317         {
318                 .id     = 0x000bb908,
319                 .mask   = 0x000fffff,
320         },
321         {
322                 /* Coresight SoC-600 */
323                 .id     = 0x000bb9eb,
324                 .mask   = 0x000fffff,
325         },
326         { 0, 0},
327 };
328
329 static struct amba_driver dynamic_funnel_driver = {
330         .drv = {
331                 .name   = "coresight-dynamic-funnel",
332                 .owner  = THIS_MODULE,
333                 .pm     = &funnel_dev_pm_ops,
334                 .suppress_bind_attrs = true,
335         },
336         .probe          = dynamic_funnel_probe,
337         .id_table       = dynamic_funnel_ids,
338 };
339 builtin_amba_driver(dynamic_funnel_driver);