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