2 * Copyright 2014-2016 Freescale Semiconductor Inc.
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.
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
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.
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>
42 #include "../../include/mc.h"
43 #include "../../include/dpaa2-io.h"
45 #include "qbman-portal.h"
49 MODULE_LICENSE("Dual BSD/GPL");
50 MODULE_AUTHOR("Freescale Semiconductor, Inc");
51 MODULE_DESCRIPTION("DPIO Driver");
57 static irqreturn_t dpio_irq_handler(int irq_num, void *arg)
59 struct device *dev = (struct device *)arg;
60 struct dpio_priv *priv = dev_get_drvdata(dev);
62 return dpaa2_io_irq(priv->io);
65 static void unregister_dpio_irq_handlers(struct fsl_mc_device *dpio_dev)
67 struct fsl_mc_device_irq *irq;
69 irq = dpio_dev->irqs[0];
71 /* clear the affinity hint */
72 irq_set_affinity_hint(irq->msi_desc->irq, NULL);
75 static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu)
77 struct dpio_priv *priv;
79 struct fsl_mc_device_irq *irq;
81 priv = dev_get_drvdata(&dpio_dev->dev);
83 irq = dpio_dev->irqs[0];
84 error = devm_request_irq(&dpio_dev->dev,
88 dev_name(&dpio_dev->dev),
91 dev_err(&dpio_dev->dev,
92 "devm_request_irq() failed: %d\n",
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);
106 static int dpaa2_dpio_probe(struct fsl_mc_device *dpio_dev)
108 struct dpio_attr dpio_attrs;
109 struct dpaa2_io_desc desc;
110 struct dpio_priv *priv;
112 struct device *dev = &dpio_dev->dev;
113 static int next_cpu = -1;
115 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
119 dev_set_drvdata(dev, priv);
121 err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
123 dev_dbg(dev, "MC portal allocation failed\n");
128 err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
129 &dpio_dev->mc_handle);
131 dev_err(dev, "dpio_open() failed\n");
135 err = dpio_get_attributes(dpio_dev->mc_io, 0, dpio_dev->mc_handle,
138 dev_err(dev, "dpio_get_attributes() failed %d\n", err);
141 desc.qman_version = dpio_attrs.qbman_version;
143 err = dpio_enable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
145 dev_err(dev, "dpio_enable() failed %d\n", err);
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;
154 /* get the cpu to use for the affinity hint */
156 next_cpu = cpumask_first(cpu_online_mask);
158 next_cpu = cpumask_next(next_cpu, cpu_online_mask);
160 if (!cpu_possible(next_cpu)) {
161 dev_err(dev, "probe failed. Number of DPIOs exceeds NR_CPUS.\n");
163 goto err_allocate_irqs;
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.
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]));
176 err = fsl_mc_allocate_irqs(dpio_dev);
178 dev_err(dev, "fsl_mc_allocate_irqs failed. err=%d\n", err);
179 goto err_allocate_irqs;
182 err = register_dpio_irq_handlers(dpio_dev, desc.cpu);
184 goto err_register_dpio_irq;
186 priv->io = dpaa2_io_create(&desc);
188 dev_err(dev, "dpaa2_io_create failed\n");
189 goto err_dpaa2_io_create;
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);
201 unregister_dpio_irq_handlers(dpio_dev);
202 err_register_dpio_irq:
203 fsl_mc_free_irqs(dpio_dev);
205 dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
207 dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
209 fsl_mc_portal_free(dpio_dev->mc_io);
211 dev_set_drvdata(dev, NULL);
216 /* Tear down interrupts for a given DPIO object */
217 static void dpio_teardown_irqs(struct fsl_mc_device *dpio_dev)
219 unregister_dpio_irq_handlers(dpio_dev);
220 fsl_mc_free_irqs(dpio_dev);
223 static int dpaa2_dpio_remove(struct fsl_mc_device *dpio_dev)
226 struct dpio_priv *priv;
229 dev = &dpio_dev->dev;
230 priv = dev_get_drvdata(dev);
232 dpaa2_io_down(priv->io);
234 dpio_teardown_irqs(dpio_dev);
236 err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
238 dev_err(dev, "MC portal allocation failed\n");
242 err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
243 &dpio_dev->mc_handle);
245 dev_err(dev, "dpio_open() failed\n");
249 dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
251 dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
253 fsl_mc_portal_free(dpio_dev->mc_io);
255 dev_set_drvdata(dev, NULL);
260 fsl_mc_portal_free(dpio_dev->mc_io);
265 static const struct fsl_mc_device_id dpaa2_dpio_match_id_table[] = {
267 .vendor = FSL_MC_VENDOR_FREESCALE,
273 static struct fsl_mc_driver dpaa2_dpio_driver = {
275 .name = KBUILD_MODNAME,
276 .owner = THIS_MODULE,
278 .probe = dpaa2_dpio_probe,
279 .remove = dpaa2_dpio_remove,
280 .match_id_table = dpaa2_dpio_match_id_table
283 static int dpio_driver_init(void)
285 return fsl_mc_driver_register(&dpaa2_dpio_driver);
288 static void dpio_driver_exit(void)
290 fsl_mc_driver_unregister(&dpaa2_dpio_driver);
292 module_init(dpio_driver_init);
293 module_exit(dpio_driver_exit);