]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/hwtracing/coresight/coresight-dynamic-replicator.c
Merge tag 'sound-5.1-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[linux.git] / drivers / hwtracing / coresight / coresight-dynamic-replicator.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
4  */
5
6 #include <linux/amba/bus.h>
7 #include <linux/clk.h>
8 #include <linux/coresight.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17
18 #include "coresight-priv.h"
19
20 #define REPLICATOR_IDFILTER0            0x000
21 #define REPLICATOR_IDFILTER1            0x004
22
23 /**
24  * struct replicator_state - specifics associated to a replicator component
25  * @base:       memory mapped base address for this component.
26  * @dev:        the device entity associated with this component
27  * @atclk:      optional clock for the core parts of the replicator.
28  * @csdev:      component vitals needed by the framework
29  */
30 struct replicator_state {
31         void __iomem            *base;
32         struct device           *dev;
33         struct clk              *atclk;
34         struct coresight_device *csdev;
35 };
36
37 /*
38  * replicator_reset : Reset the replicator configuration to sane values.
39  */
40 static void replicator_reset(struct replicator_state *drvdata)
41 {
42         CS_UNLOCK(drvdata->base);
43
44         if (!coresight_claim_device_unlocked(drvdata->base)) {
45                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
46                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
47                 coresight_disclaim_device_unlocked(drvdata->base);
48         }
49
50         CS_LOCK(drvdata->base);
51 }
52
53 static int replicator_enable(struct coresight_device *csdev, int inport,
54                               int outport)
55 {
56         int rc = 0;
57         u32 reg;
58         struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
59
60         switch (outport) {
61         case 0:
62                 reg = REPLICATOR_IDFILTER0;
63                 break;
64         case 1:
65                 reg = REPLICATOR_IDFILTER1;
66                 break;
67         default:
68                 WARN_ON(1);
69                 return -EINVAL;
70         }
71
72         CS_UNLOCK(drvdata->base);
73
74         if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
75             (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
76                 rc = coresight_claim_device_unlocked(drvdata->base);
77
78         /* Ensure that the outport is enabled. */
79         if (!rc) {
80                 writel_relaxed(0x00, drvdata->base + reg);
81                 dev_dbg(drvdata->dev, "REPLICATOR enabled\n");
82         }
83
84         CS_LOCK(drvdata->base);
85
86         return rc;
87 }
88
89 static void replicator_disable(struct coresight_device *csdev, int inport,
90                                 int outport)
91 {
92         u32 reg;
93         struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
94
95         switch (outport) {
96         case 0:
97                 reg = REPLICATOR_IDFILTER0;
98                 break;
99         case 1:
100                 reg = REPLICATOR_IDFILTER1;
101                 break;
102         default:
103                 WARN_ON(1);
104                 return;
105         }
106
107         CS_UNLOCK(drvdata->base);
108
109         /* disable the flow of ATB data through port */
110         writel_relaxed(0xff, drvdata->base + reg);
111
112         if ((readl_relaxed(drvdata->base + REPLICATOR_IDFILTER0) == 0xff) &&
113             (readl_relaxed(drvdata->base + REPLICATOR_IDFILTER1) == 0xff))
114                 coresight_disclaim_device_unlocked(drvdata->base);
115         CS_LOCK(drvdata->base);
116
117         dev_dbg(drvdata->dev, "REPLICATOR disabled\n");
118 }
119
120 static const struct coresight_ops_link replicator_link_ops = {
121         .enable         = replicator_enable,
122         .disable        = replicator_disable,
123 };
124
125 static const struct coresight_ops replicator_cs_ops = {
126         .link_ops       = &replicator_link_ops,
127 };
128
129 #define coresight_replicator_reg(name, offset) \
130         coresight_simple_reg32(struct replicator_state, name, offset)
131
132 coresight_replicator_reg(idfilter0, REPLICATOR_IDFILTER0);
133 coresight_replicator_reg(idfilter1, REPLICATOR_IDFILTER1);
134
135 static struct attribute *replicator_mgmt_attrs[] = {
136         &dev_attr_idfilter0.attr,
137         &dev_attr_idfilter1.attr,
138         NULL,
139 };
140
141 static const struct attribute_group replicator_mgmt_group = {
142         .attrs = replicator_mgmt_attrs,
143         .name = "mgmt",
144 };
145
146 static const struct attribute_group *replicator_groups[] = {
147         &replicator_mgmt_group,
148         NULL,
149 };
150
151 static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
152 {
153         int ret;
154         struct device *dev = &adev->dev;
155         struct resource *res = &adev->res;
156         struct coresight_platform_data *pdata = NULL;
157         struct replicator_state *drvdata;
158         struct coresight_desc desc = { 0 };
159         struct device_node *np = adev->dev.of_node;
160         void __iomem *base;
161
162         if (np) {
163                 pdata = of_get_coresight_platform_data(dev, np);
164                 if (IS_ERR(pdata))
165                         return PTR_ERR(pdata);
166                 adev->dev.platform_data = pdata;
167         }
168
169         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
170         if (!drvdata)
171                 return -ENOMEM;
172
173         drvdata->dev = &adev->dev;
174         drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
175         if (!IS_ERR(drvdata->atclk)) {
176                 ret = clk_prepare_enable(drvdata->atclk);
177                 if (ret)
178                         return ret;
179         }
180
181         /* Validity for the resource is already checked by the AMBA core */
182         base = devm_ioremap_resource(dev, res);
183         if (IS_ERR(base))
184                 return PTR_ERR(base);
185
186         drvdata->base = base;
187         dev_set_drvdata(dev, drvdata);
188         pm_runtime_put(&adev->dev);
189
190         desc.type = CORESIGHT_DEV_TYPE_LINK;
191         desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
192         desc.ops = &replicator_cs_ops;
193         desc.pdata = adev->dev.platform_data;
194         desc.dev = &adev->dev;
195         desc.groups = replicator_groups;
196         drvdata->csdev = coresight_register(&desc);
197
198         if (!IS_ERR(drvdata->csdev)) {
199                 replicator_reset(drvdata);
200                 return 0;
201         }
202         return PTR_ERR(drvdata->csdev);
203 }
204
205 #ifdef CONFIG_PM
206 static int replicator_runtime_suspend(struct device *dev)
207 {
208         struct replicator_state *drvdata = dev_get_drvdata(dev);
209
210         if (drvdata && !IS_ERR(drvdata->atclk))
211                 clk_disable_unprepare(drvdata->atclk);
212
213         return 0;
214 }
215
216 static int replicator_runtime_resume(struct device *dev)
217 {
218         struct replicator_state *drvdata = dev_get_drvdata(dev);
219
220         if (drvdata && !IS_ERR(drvdata->atclk))
221                 clk_prepare_enable(drvdata->atclk);
222
223         return 0;
224 }
225 #endif
226
227 static const struct dev_pm_ops replicator_dev_pm_ops = {
228         SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
229                            replicator_runtime_resume,
230                            NULL)
231 };
232
233 static const struct amba_id replicator_ids[] = {
234         {
235                 .id     = 0x000bb909,
236                 .mask   = 0x000fffff,
237         },
238         {
239                 /* Coresight SoC-600 */
240                 .id     = 0x000bb9ec,
241                 .mask   = 0x000fffff,
242         },
243         { 0, 0 },
244 };
245
246 static struct amba_driver replicator_driver = {
247         .drv = {
248                 .name   = "coresight-dynamic-replicator",
249                 .pm     = &replicator_dev_pm_ops,
250                 .suppress_bind_attrs = true,
251         },
252         .probe          = replicator_probe,
253         .id_table       = replicator_ids,
254 };
255 builtin_amba_driver(replicator_driver);