coresight: etm3x: implementing perf_enable/disable() API
[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/module.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/of.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26
27 #include "coresight-priv.h"
28
29 #define REPLICATOR_IDFILTER0            0x000
30 #define REPLICATOR_IDFILTER1            0x004
31
32 /**
33  * struct replicator_state - specifics associated to a replicator component
34  * @base:       memory mapped base address for this component.
35  * @dev:        the device entity associated with this component
36  * @atclk:      optional clock for the core parts of the replicator.
37  * @csdev:      component vitals needed by the framework
38  */
39 struct replicator_state {
40         void __iomem            *base;
41         struct device           *dev;
42         struct clk              *atclk;
43         struct coresight_device *csdev;
44 };
45
46 static int replicator_enable(struct coresight_device *csdev, int inport,
47                               int outport)
48 {
49         struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
50
51         CS_UNLOCK(drvdata->base);
52
53         /*
54          * Ensure that the other port is disabled
55          * 0x00 - passing through the replicator unimpeded
56          * 0xff - disable (or impede) the flow of ATB data
57          */
58         if (outport == 0) {
59                 writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER0);
60                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
61         } else {
62                 writel_relaxed(0x00, drvdata->base + REPLICATOR_IDFILTER1);
63                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
64         }
65
66         CS_LOCK(drvdata->base);
67
68         dev_info(drvdata->dev, "REPLICATOR enabled\n");
69         return 0;
70 }
71
72 static void replicator_disable(struct coresight_device *csdev, int inport,
73                                 int outport)
74 {
75         struct replicator_state *drvdata = dev_get_drvdata(csdev->dev.parent);
76
77         CS_UNLOCK(drvdata->base);
78
79         /* disable the flow of ATB data through port */
80         if (outport == 0)
81                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER0);
82         else
83                 writel_relaxed(0xff, drvdata->base + REPLICATOR_IDFILTER1);
84
85         CS_LOCK(drvdata->base);
86
87         dev_info(drvdata->dev, "REPLICATOR disabled\n");
88 }
89
90 static const struct coresight_ops_link replicator_link_ops = {
91         .enable         = replicator_enable,
92         .disable        = replicator_disable,
93 };
94
95 static const struct coresight_ops replicator_cs_ops = {
96         .link_ops       = &replicator_link_ops,
97 };
98
99 static int replicator_probe(struct amba_device *adev, const struct amba_id *id)
100 {
101         int ret;
102         struct device *dev = &adev->dev;
103         struct resource *res = &adev->res;
104         struct coresight_platform_data *pdata = NULL;
105         struct replicator_state *drvdata;
106         struct coresight_desc *desc;
107         struct device_node *np = adev->dev.of_node;
108         void __iomem *base;
109
110         if (np) {
111                 pdata = of_get_coresight_platform_data(dev, np);
112                 if (IS_ERR(pdata))
113                         return PTR_ERR(pdata);
114                 adev->dev.platform_data = pdata;
115         }
116
117         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
118         if (!drvdata)
119                 return -ENOMEM;
120
121         drvdata->dev = &adev->dev;
122         drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
123         if (!IS_ERR(drvdata->atclk)) {
124                 ret = clk_prepare_enable(drvdata->atclk);
125                 if (ret)
126                         return ret;
127         }
128
129         /* Validity for the resource is already checked by the AMBA core */
130         base = devm_ioremap_resource(dev, res);
131         if (IS_ERR(base))
132                 return PTR_ERR(base);
133
134         drvdata->base = base;
135         dev_set_drvdata(dev, drvdata);
136         pm_runtime_put(&adev->dev);
137
138         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
139         if (!desc)
140                 return -ENOMEM;
141
142         desc->type = CORESIGHT_DEV_TYPE_LINK;
143         desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
144         desc->ops = &replicator_cs_ops;
145         desc->pdata = adev->dev.platform_data;
146         desc->dev = &adev->dev;
147         drvdata->csdev = coresight_register(desc);
148         if (IS_ERR(drvdata->csdev))
149                 return PTR_ERR(drvdata->csdev);
150
151         dev_info(dev, "%s initialized\n", (char *)id->data);
152         return 0;
153 }
154
155 #ifdef CONFIG_PM
156 static int replicator_runtime_suspend(struct device *dev)
157 {
158         struct replicator_state *drvdata = dev_get_drvdata(dev);
159
160         if (drvdata && !IS_ERR(drvdata->atclk))
161                 clk_disable_unprepare(drvdata->atclk);
162
163         return 0;
164 }
165
166 static int replicator_runtime_resume(struct device *dev)
167 {
168         struct replicator_state *drvdata = dev_get_drvdata(dev);
169
170         if (drvdata && !IS_ERR(drvdata->atclk))
171                 clk_prepare_enable(drvdata->atclk);
172
173         return 0;
174 }
175 #endif
176
177 static const struct dev_pm_ops replicator_dev_pm_ops = {
178         SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
179                            replicator_runtime_resume,
180                            NULL)
181 };
182
183 static struct amba_id replicator_ids[] = {
184         {
185                 .id     = 0x0003b909,
186                 .mask   = 0x0003ffff,
187                 .data   = "REPLICATOR 1.0",
188         },
189         { 0, 0 },
190 };
191
192 static struct amba_driver replicator_driver = {
193         .drv = {
194                 .name   = "coresight-replicator-qcom",
195                 .pm     = &replicator_dev_pm_ops,
196                 .suppress_bind_attrs = true,
197         },
198         .probe          = replicator_probe,
199         .id_table       = replicator_ids,
200 };
201
202 module_amba_driver(replicator_driver);