GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / fsl-mc / bus / dpio / dpio-driver.c
1 /*
2  * Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright NXP 2016
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Freescale Semiconductor nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * ALTERNATIVELY, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") as published by the Free Software
18  * Foundation, either version 2 of that License or (at your option) any
19  * later version.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <linux/types.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/platform_device.h>
37 #include <linux/interrupt.h>
38 #include <linux/msi.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/delay.h>
41
42 #include "../../include/mc.h"
43 #include "../../include/dpaa2-io.h"
44
45 #include "qbman-portal.h"
46 #include "dpio.h"
47 #include "dpio-cmd.h"
48
49 MODULE_LICENSE("Dual BSD/GPL");
50 MODULE_AUTHOR("Freescale Semiconductor, Inc");
51 MODULE_DESCRIPTION("DPIO Driver");
52
53 struct dpio_priv {
54         struct dpaa2_io *io;
55 };
56
57 static irqreturn_t dpio_irq_handler(int irq_num, void *arg)
58 {
59         struct device *dev = (struct device *)arg;
60         struct dpio_priv *priv = dev_get_drvdata(dev);
61
62         return dpaa2_io_irq(priv->io);
63 }
64
65 static void unregister_dpio_irq_handlers(struct fsl_mc_device *dpio_dev)
66 {
67         struct fsl_mc_device_irq *irq;
68
69         irq = dpio_dev->irqs[0];
70
71         /* clear the affinity hint */
72         irq_set_affinity_hint(irq->msi_desc->irq, NULL);
73 }
74
75 static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu)
76 {
77         struct dpio_priv *priv;
78         int error;
79         struct fsl_mc_device_irq *irq;
80
81         priv = dev_get_drvdata(&dpio_dev->dev);
82
83         irq = dpio_dev->irqs[0];
84         error = devm_request_irq(&dpio_dev->dev,
85                                  irq->msi_desc->irq,
86                                  dpio_irq_handler,
87                                  0,
88                                  dev_name(&dpio_dev->dev),
89                                  &dpio_dev->dev);
90         if (error < 0) {
91                 dev_err(&dpio_dev->dev,
92                         "devm_request_irq() failed: %d\n",
93                         error);
94                 return error;
95         }
96
97         /* set the affinity hint */
98         if (irq_set_affinity_hint(irq->msi_desc->irq, cpumask_of(cpu)))
99                 dev_err(&dpio_dev->dev,
100                         "irq_set_affinity failed irq %d cpu %d\n",
101                         irq->msi_desc->irq, cpu);
102
103         return 0;
104 }
105
106 static int dpaa2_dpio_probe(struct fsl_mc_device *dpio_dev)
107 {
108         struct dpio_attr dpio_attrs;
109         struct dpaa2_io_desc desc;
110         struct dpio_priv *priv;
111         int err = -ENOMEM;
112         struct device *dev = &dpio_dev->dev;
113         static int next_cpu = -1;
114
115         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
116         if (!priv)
117                 goto err_priv_alloc;
118
119         dev_set_drvdata(dev, priv);
120
121         err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
122         if (err) {
123                 dev_dbg(dev, "MC portal allocation failed\n");
124                 err = -EPROBE_DEFER;
125                 goto err_mcportal;
126         }
127
128         err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
129                         &dpio_dev->mc_handle);
130         if (err) {
131                 dev_err(dev, "dpio_open() failed\n");
132                 goto err_open;
133         }
134
135         err = dpio_get_attributes(dpio_dev->mc_io, 0, dpio_dev->mc_handle,
136                                   &dpio_attrs);
137         if (err) {
138                 dev_err(dev, "dpio_get_attributes() failed %d\n", err);
139                 goto err_get_attr;
140         }
141         desc.qman_version = dpio_attrs.qbman_version;
142
143         err = dpio_enable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
144         if (err) {
145                 dev_err(dev, "dpio_enable() failed %d\n", err);
146                 goto err_get_attr;
147         }
148
149         /* initialize DPIO descriptor */
150         desc.receives_notifications = dpio_attrs.num_priorities ? 1 : 0;
151         desc.has_8prio = dpio_attrs.num_priorities == 8 ? 1 : 0;
152         desc.dpio_id = dpio_dev->obj_desc.id;
153
154         /* get the cpu to use for the affinity hint */
155         if (next_cpu == -1)
156                 next_cpu = cpumask_first(cpu_online_mask);
157         else
158                 next_cpu = cpumask_next(next_cpu, cpu_online_mask);
159
160         if (!cpu_possible(next_cpu)) {
161                 dev_err(dev, "probe failed. Number of DPIOs exceeds NR_CPUS.\n");
162                 err = -ERANGE;
163                 goto err_allocate_irqs;
164         }
165         desc.cpu = next_cpu;
166
167         /*
168          * Set the CENA regs to be the cache inhibited area of the portal to
169          * avoid coherency issues if a user migrates to another core.
170          */
171         desc.regs_cena = ioremap_wc(dpio_dev->regions[1].start,
172                 resource_size(&dpio_dev->regions[1]));
173         desc.regs_cinh = ioremap(dpio_dev->regions[1].start,
174                 resource_size(&dpio_dev->regions[1]));
175
176         err = fsl_mc_allocate_irqs(dpio_dev);
177         if (err) {
178                 dev_err(dev, "fsl_mc_allocate_irqs failed. err=%d\n", err);
179                 goto err_allocate_irqs;
180         }
181
182         err = register_dpio_irq_handlers(dpio_dev, desc.cpu);
183         if (err)
184                 goto err_register_dpio_irq;
185
186         priv->io = dpaa2_io_create(&desc);
187         if (!priv->io) {
188                 dev_err(dev, "dpaa2_io_create failed\n");
189                 goto err_dpaa2_io_create;
190         }
191
192         dev_info(dev, "probed\n");
193         dev_dbg(dev, "   receives_notifications = %d\n",
194                 desc.receives_notifications);
195         dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
196         fsl_mc_portal_free(dpio_dev->mc_io);
197
198         return 0;
199
200 err_dpaa2_io_create:
201         unregister_dpio_irq_handlers(dpio_dev);
202 err_register_dpio_irq:
203         fsl_mc_free_irqs(dpio_dev);
204 err_allocate_irqs:
205         dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
206 err_get_attr:
207         dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
208 err_open:
209         fsl_mc_portal_free(dpio_dev->mc_io);
210 err_mcportal:
211         dev_set_drvdata(dev, NULL);
212 err_priv_alloc:
213         return err;
214 }
215
216 /* Tear down interrupts for a given DPIO object */
217 static void dpio_teardown_irqs(struct fsl_mc_device *dpio_dev)
218 {
219         unregister_dpio_irq_handlers(dpio_dev);
220         fsl_mc_free_irqs(dpio_dev);
221 }
222
223 static int dpaa2_dpio_remove(struct fsl_mc_device *dpio_dev)
224 {
225         struct device *dev;
226         struct dpio_priv *priv;
227         int err;
228
229         dev = &dpio_dev->dev;
230         priv = dev_get_drvdata(dev);
231
232         dpaa2_io_down(priv->io);
233
234         dpio_teardown_irqs(dpio_dev);
235
236         err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
237         if (err) {
238                 dev_err(dev, "MC portal allocation failed\n");
239                 goto err_mcportal;
240         }
241
242         err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
243                         &dpio_dev->mc_handle);
244         if (err) {
245                 dev_err(dev, "dpio_open() failed\n");
246                 goto err_open;
247         }
248
249         dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
250
251         dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
252
253         fsl_mc_portal_free(dpio_dev->mc_io);
254
255         dev_set_drvdata(dev, NULL);
256
257         return 0;
258
259 err_open:
260         fsl_mc_portal_free(dpio_dev->mc_io);
261 err_mcportal:
262         return err;
263 }
264
265 static const struct fsl_mc_device_id dpaa2_dpio_match_id_table[] = {
266         {
267                 .vendor = FSL_MC_VENDOR_FREESCALE,
268                 .obj_type = "dpio",
269         },
270         { .vendor = 0x0 }
271 };
272
273 static struct fsl_mc_driver dpaa2_dpio_driver = {
274         .driver = {
275                 .name           = KBUILD_MODNAME,
276                 .owner          = THIS_MODULE,
277         },
278         .probe          = dpaa2_dpio_probe,
279         .remove         = dpaa2_dpio_remove,
280         .match_id_table = dpaa2_dpio_match_id_table
281 };
282
283 static int dpio_driver_init(void)
284 {
285         return fsl_mc_driver_register(&dpaa2_dpio_driver);
286 }
287
288 static void dpio_driver_exit(void)
289 {
290         fsl_mc_driver_unregister(&dpaa2_dpio_driver);
291 }
292 module_init(dpio_driver_init);
293 module_exit(dpio_driver_exit);