coresight: replicator: Add Qualcomm CoreSight Replicator driver
[firefly-linux-kernel-4.4.55.git] / drivers / hwtracing / coresight / coresight-replicator-qcom.c
1 /*
2  * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/amba/bus.h>
15 #include <linux/clk.h>
16 #include <linux/coresight.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/init.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/of.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/slab.h>
25
26 #include "coresight-priv.h"
27
28 #define REPLICATOR_IDFILTER0            0x000
29 #define REPLICATOR_IDFILTER1            0x004
30
31 /**
32  * struct replicator_state - specifics associated to a replicator component
33  * @base:       memory mapped base address for this component.
34  * @dev:        the device entity associated with this component
35  * @atclk:      optional clock for the core parts of the replicator.
36  * @csdev:      component vitals needed by the framework
37  */
38 struct replicator_state {
39         void __iomem            *base;
40         struct device           *dev;
41         struct clk              *atclk;
42         struct coresight_device *csdev;
43 };
44
45 static int replicator_enable(struct coresight_device *csdev, int inport,
46                               int outport)
47 {
48         struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
49
50         pm_runtime_get_sync(drvdata->dev);
51
52         CS_UNLOCK(drvdata->base);
53
54         /*
55          * Ensure that the other port is disabled
56          * 0x00 - passing through the replicator unimpeded
57          * 0xff - disable (or impede) the flow of ATB data
58          */
59         if (outport == 0) {
60                 writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER0);
61                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
62         } else {
63                 writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER1);
64                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
65         }
66
67         CS_LOCK(drvdata->base);
68
69         dev_info(drvdata->dev, "REPLICATOR enabled\n");
70         return 0;
71 }
72
73 static void replicator_disable(struct coresight_device *csdev, int inport,
74                                 int outport)
75 {
76         struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
77
78         CS_UNLOCK(drvdata->base);
79
80         /* disable the flow of ATB data through port */
81         if (outport == 0)
82                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
83         else
84                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
85
86         CS_LOCK(drvdata->base);
87
88         pm_runtime_put(drvdata->dev);
89
90         dev_info(drvdata->dev, "REPLICATOR disabled\n");
91 }
92
93 static const struct coresight_ops_link replicator_link_ops = {
94         .enable         = replicator_enable,
95         .disable        = replicator_disable,
96 };
97
98 static const struct coresight_ops replicator_cs_ops = {
99         .link_ops       = &replicator_link_ops,
100 };
101
102 static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
103 {
104         int ret;
105         struct device *dev = &adev->dev;
106         struct resource *res = &adev->res;
107         struct coresight_platform_data *pdata = NULL;
108         struct replicator_state *drvdata;
109         struct coresight_desc *desc;
110         struct device_node *np = adev->dev.of_node;
111         void __iomem *base;
112
113         if (np) {
114                 pdata = of_get_coresight_platform_data(dev, np);
115                 if (IS_ERR(pdata))
116                         return PTR_ERR(pdata);
117                 adev->dev.platform_data = pdata;
118         }
119
120         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
121         if (!drvdata)
122                 return -ENOMEM;
123
124         drvdata->dev = &adev->dev;
125         drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
126         if (!IS_ERR(drvdata->atclk)) {
127                 ret = clk_prepare_enable(drvdata->atclk);
128                 if (ret)
129                         return ret;
130         }
131
132         /* Validity for the resource is already checked by the AMBA core */
133         base = devm_ioremap_resource(dev, res);
134         if (IS_ERR(base))
135                 return PTR_ERR(base);
136
137         drvdata->base = base;
138         dev_set_drvdata(dev, drvdata);
139         pm_runtime_put(&adev->dev);
140
141         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
142         if (!desc)
143                 return -ENOMEM;
144
145         desc->type = CORESIGHT_DEV_TYPE_LINK;
146         desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
147         desc->ops = &replicator_cs_ops;
148         desc->pdata = adev->dev.platform_data;
149         desc->dev = &adev->dev;
150         drvdata->csdev = coresight_register(desc);
151         if (IS_ERR(drvdata->csdev))
152                 return PTR_ERR(drvdata->csdev);
153
154         dev_info(dev, "%s initialized\n", (char *)id->data);
155         return 0;
156 }
157
158 static int replicator_remove(struct amba_device *adev)
159 {
160         struct replicator_state *drvdata = amba_get_drvdata(adev);
161
162         pm_runtime_disable(&adev->dev);
163         coresight_unregister(drvdata->csdev);
164         return 0;
165 }
166
167 #ifdef CONFIG_PM
168 static int replicator_runtime_suspend(struct device *dev)
169 {
170         struct replicator_state *drvdata = dev_get_drvdata(dev);
171
172         if (drvdata && !IS_ERR(drvdata->atclk))
173                 clk_disable_unprepare(drvdata->atclk);
174
175         return 0;
176 }
177
178 static int replicator_runtime_resume(struct device *dev)
179 {
180         struct replicator_state *drvdata = dev_get_drvdata(dev);
181
182         if (drvdata && !IS_ERR(drvdata->atclk))
183                 clk_prepare_enable(drvdata->atclk);
184
185         return 0;
186 }
187 #endif
188
189 static const struct dev_pm_ops replicator_dev_pm_ops = {
190         SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
191                            replicator_runtime_resume,
192                            NULL)
193 };
194
195 static struct amba_id replicator_ids[] = {
196         {
197                 .id     = 0x0003b909,
198                 .mask   = 0x0003ffff,
199                 .data   = "REPLICATOR 1.0",
200         },
201         { 0, 0 },
202 };
203
204 static struct amba_driver replicator_driver = {
205         .drv = {
206                 .name   = "coresight-replicator-qcom",
207                 .pm     = &replicator_dev_pm_ops,
208         },
209         .probe          = replicator_probe,
210         .remove         = replicator_remove,
211         .id_table       = replicator_ids,
212 };
213
214 module_amba_driver(replicator_driver);