coresight: Remove name from platform description
[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
191         pdata = coresight_get_platform_data(dev);
192         if (IS_ERR(pdata))
193                 return PTR_ERR(pdata);
194         dev->platform_data = pdata;
195
196         if (is_of_node(dev_fwnode(dev)) &&
197             of_device_is_compatible(dev->of_node, "arm,coresight-funnel"))
198                 pr_warn_once("Uses OBSOLETE CoreSight funnel binding\n");
199
200         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
201         if (!drvdata)
202                 return -ENOMEM;
203
204         drvdata->atclk = devm_clk_get(dev, "atclk"); /* optional */
205         if (!IS_ERR(drvdata->atclk)) {
206                 ret = clk_prepare_enable(drvdata->atclk);
207                 if (ret)
208                         return ret;
209         }
210
211         /*
212          * Map the device base for dynamic-funnel, which has been
213          * validated by AMBA core.
214          */
215         if (res) {
216                 base = devm_ioremap_resource(dev, res);
217                 if (IS_ERR(base)) {
218                         ret = PTR_ERR(base);
219                         goto out_disable_clk;
220                 }
221                 drvdata->base = base;
222                 desc.groups = coresight_funnel_groups;
223         }
224
225         dev_set_drvdata(dev, drvdata);
226
227         desc.type = CORESIGHT_DEV_TYPE_LINK;
228         desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
229         desc.ops = &funnel_cs_ops;
230         desc.pdata = pdata;
231         desc.dev = dev;
232         desc.name = dev_name(dev);
233         drvdata->csdev = coresight_register(&desc);
234         if (IS_ERR(drvdata->csdev)) {
235                 ret = PTR_ERR(drvdata->csdev);
236                 goto out_disable_clk;
237         }
238
239         pm_runtime_put(dev);
240
241 out_disable_clk:
242         if (ret && !IS_ERR_OR_NULL(drvdata->atclk))
243                 clk_disable_unprepare(drvdata->atclk);
244         return ret;
245 }
246
247 #ifdef CONFIG_PM
248 static int funnel_runtime_suspend(struct device *dev)
249 {
250         struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
251
252         if (drvdata && !IS_ERR(drvdata->atclk))
253                 clk_disable_unprepare(drvdata->atclk);
254
255         return 0;
256 }
257
258 static int funnel_runtime_resume(struct device *dev)
259 {
260         struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
261
262         if (drvdata && !IS_ERR(drvdata->atclk))
263                 clk_prepare_enable(drvdata->atclk);
264
265         return 0;
266 }
267 #endif
268
269 static const struct dev_pm_ops funnel_dev_pm_ops = {
270         SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
271 };
272
273 static int static_funnel_probe(struct platform_device *pdev)
274 {
275         int ret;
276
277         pm_runtime_get_noresume(&pdev->dev);
278         pm_runtime_set_active(&pdev->dev);
279         pm_runtime_enable(&pdev->dev);
280
281         /* Static funnel do not have programming base */
282         ret = funnel_probe(&pdev->dev, NULL);
283
284         if (ret) {
285                 pm_runtime_put_noidle(&pdev->dev);
286                 pm_runtime_disable(&pdev->dev);
287         }
288
289         return ret;
290 }
291
292 static const struct of_device_id static_funnel_match[] = {
293         {.compatible = "arm,coresight-static-funnel"},
294         {}
295 };
296
297 static struct platform_driver static_funnel_driver = {
298         .probe          = static_funnel_probe,
299         .driver         = {
300                 .name   = "coresight-static-funnel",
301                 .of_match_table = static_funnel_match,
302                 .pm     = &funnel_dev_pm_ops,
303                 .suppress_bind_attrs = true,
304         },
305 };
306 builtin_platform_driver(static_funnel_driver);
307
308 static int dynamic_funnel_probe(struct amba_device *adev,
309                                 const struct amba_id *id)
310 {
311         return funnel_probe(&adev->dev, &adev->res);
312 }
313
314 static const struct amba_id dynamic_funnel_ids[] = {
315         {
316                 .id     = 0x000bb908,
317                 .mask   = 0x000fffff,
318         },
319         {
320                 /* Coresight SoC-600 */
321                 .id     = 0x000bb9eb,
322                 .mask   = 0x000fffff,
323         },
324         { 0, 0},
325 };
326
327 static struct amba_driver dynamic_funnel_driver = {
328         .drv = {
329                 .name   = "coresight-dynamic-funnel",
330                 .owner  = THIS_MODULE,
331                 .pm     = &funnel_dev_pm_ops,
332                 .suppress_bind_attrs = true,
333         },
334         .probe          = dynamic_funnel_probe,
335         .id_table       = dynamic_funnel_ids,
336 };
337 builtin_amba_driver(dynamic_funnel_driver);