]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/dev/gpio/ofw_gpiobus.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / dev / gpio / ofw_gpiobus.c
1 /*-
2  * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
3  * Copyright (c) 2013, Luiz Otavio O Souza <loos@FreeBSD.org>
4  * Copyright (c) 2013 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38
39 #include <dev/gpio/gpiobusvar.h>
40 #include <dev/ofw/ofw_bus.h>
41
42 static int ofw_gpiobus_parse_gpios(struct gpiobus_softc *,
43     struct gpiobus_ivar *, phandle_t);
44 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
45     phandle_t);
46 static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *);
47
48 device_t
49 ofw_gpiobus_add_fdt_child(device_t bus, phandle_t child)
50 {
51         struct ofw_gpiobus_devinfo *dinfo;
52         device_t childdev;
53
54         /*
55          * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
56          */
57         dinfo = ofw_gpiobus_setup_devinfo(bus, child);
58         if (dinfo == NULL)
59                 return (NULL);
60         childdev = device_add_child(bus, NULL, -1);
61         if (childdev == NULL) {
62                 device_printf(bus, "could not add child: %s\n",
63                     dinfo->opd_obdinfo.obd_name);
64                 ofw_gpiobus_destroy_devinfo(dinfo);
65                 return (NULL);
66         }
67         device_set_ivars(childdev, dinfo);
68
69         return (childdev);
70 }
71
72 static int
73 ofw_gpiobus_alloc_ivars(struct gpiobus_ivar *dinfo)
74 {
75
76         /* Allocate pins and flags memory. */
77         dinfo->pins = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
78             M_NOWAIT | M_ZERO);
79         if (dinfo->pins == NULL)
80                 return (ENOMEM);
81         dinfo->flags = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
82             M_NOWAIT | M_ZERO);
83         if (dinfo->flags == NULL) {
84                 free(dinfo->pins, M_DEVBUF);
85                 return (ENOMEM);
86         }
87
88         return (0);
89 }
90
91 static void
92 ofw_gpiobus_free_ivars(struct gpiobus_ivar *dinfo)
93 {
94
95         free(dinfo->flags, M_DEVBUF);
96         free(dinfo->pins, M_DEVBUF);
97 }
98
99 static int
100 ofw_gpiobus_parse_gpios(struct gpiobus_softc *sc, struct gpiobus_ivar *dinfo,
101         phandle_t child)
102 {
103         int cells, i, j, len;
104         pcell_t *gpios;
105         phandle_t gpio;
106
107         /* Retrieve the gpios property. */
108         if ((len = OF_getproplen(child, "gpios")) < 0)
109                 return (EINVAL);
110         gpios = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
111         if (gpios == NULL)
112                 return (ENOMEM);
113         if (OF_getencprop(child, "gpios", gpios, len) < 0) {
114                 free(gpios, M_DEVBUF);
115                 return (EINVAL);
116         }
117
118         /*
119          * The gpio-specifier is controller independent, but the first pcell
120          * has the reference to the GPIO controller phandler.
121          * One the first pass we count the number of encoded gpio-specifiers.
122          */
123         i = 0;
124         len /= sizeof(pcell_t);
125         while (i < len) {
126                 /* Allow NULL specifiers. */
127                 if (gpios[i] == 0) {
128                         dinfo->npins++;
129                         i++;
130                         continue;
131                 }
132                 gpio = OF_node_from_xref(gpios[i]);
133                 /* Verify if we're attaching to the correct GPIO controller. */
134                 if (!OF_hasprop(gpio, "gpio-controller") ||
135                     gpio != ofw_bus_get_node(sc->sc_dev)) {
136                         free(gpios, M_DEVBUF);
137                         return (EINVAL);
138                 }
139                 /* Read gpio-cells property for this GPIO controller. */
140                 if (OF_getencprop(gpio, "#gpio-cells", &cells,
141                     sizeof(cells)) < 0) {
142                         free(gpios, M_DEVBUF);
143                         return (EINVAL);
144                 }
145                 dinfo->npins++;
146                 i += cells + 1;
147         }
148
149         if (dinfo->npins == 0) {
150                 free(gpios, M_DEVBUF);
151                 return (EINVAL);
152         }
153
154         /* Allocate the child resources. */
155         if (ofw_gpiobus_alloc_ivars(dinfo) != 0) {
156                 free(gpios, M_DEVBUF);
157                 return (ENOMEM);
158         }
159
160         /* Decode the gpio specifier on the second pass. */
161         i = 0;
162         j = 0;
163         while (i < len) {
164                 /* Allow NULL specifiers. */
165                 if (gpios[i] == 0) {
166                         i++;
167                         j++;
168                         continue;
169                 }
170
171                 gpio = OF_node_from_xref(gpios[i]);
172                 /* Read gpio-cells property for this GPIO controller. */
173                 if (OF_getencprop(gpio, "#gpio-cells", &cells,
174                     sizeof(cells)) < 0) {
175                         ofw_gpiobus_free_ivars(dinfo);
176                         free(gpios, M_DEVBUF);
177                         return (EINVAL);
178                 }
179
180                 /* Get the GPIO pin number and flags. */
181                 if (gpio_map_gpios(sc->sc_dev, child, gpio, cells,
182                     &gpios[i + 1], &dinfo->pins[j], &dinfo->flags[j]) != 0) {
183                         ofw_gpiobus_free_ivars(dinfo);
184                         free(gpios, M_DEVBUF);
185                         return (EINVAL);
186                 }
187
188                 /* Consistency check. */
189                 if (dinfo->pins[j] > sc->sc_npins) {
190                         device_printf(sc->sc_busdev,
191                             "invalid pin %d, max: %d\n",
192                             dinfo->pins[j], sc->sc_npins - 1);
193                         ofw_gpiobus_free_ivars(dinfo);
194                         free(gpios, M_DEVBUF);
195                         return (EINVAL);
196                 }
197
198                 /*
199                  * Mark pin as mapped and give warning if it's already mapped.
200                  */
201                 if (sc->sc_pins_mapped[dinfo->pins[j]]) {
202                         device_printf(sc->sc_busdev,
203                             "warning: pin %d is already mapped\n",
204                             dinfo->pins[j]);
205                         ofw_gpiobus_free_ivars(dinfo);
206                         free(gpios, M_DEVBUF);
207                         return (EINVAL);
208                 }
209                 sc->sc_pins_mapped[dinfo->pins[j]] = 1;
210
211                 i += cells + 1;
212                 j++;
213         }
214
215         free(gpios, M_DEVBUF);
216
217         return (0);
218 }
219
220 static struct ofw_gpiobus_devinfo *
221 ofw_gpiobus_setup_devinfo(device_t dev, phandle_t node)
222 {
223         struct gpiobus_softc *sc;
224         struct ofw_gpiobus_devinfo *dinfo;
225
226         sc = device_get_softc(dev);
227         dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
228         if (dinfo == NULL)
229                 return (NULL);
230         if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
231                 free(dinfo, M_DEVBUF);
232                 return (NULL);
233         }
234
235         /* Parse the gpios property for the child. */
236         if (ofw_gpiobus_parse_gpios(sc, &dinfo->opd_dinfo, node) != 0) {
237                 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
238                 free(dinfo, M_DEVBUF);
239                 return (NULL);
240         }
241         /* And now the interrupt resources. */
242         resource_list_init(&dinfo->opd_dinfo.rl);
243         if (ofw_bus_intr_to_rl(dev, node, &dinfo->opd_dinfo.rl) != 0) {
244                 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
245                 free(dinfo, M_DEVBUF);
246                 return (NULL);
247         }
248
249         return (dinfo);
250 }
251
252 static void
253 ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *dinfo)
254 {
255
256         resource_list_free(&dinfo->opd_dinfo.rl);
257         ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
258         free(dinfo, M_DEVBUF);
259 }
260
261 static int
262 ofw_gpiobus_probe(device_t dev)
263 {
264
265         if (ofw_bus_get_node(dev) == -1)
266                 return (ENXIO);
267         device_set_desc(dev, "OFW GPIO bus");
268
269         return (0);
270 }
271
272 static int
273 ofw_gpiobus_attach(device_t dev)
274 {
275         int err;
276         phandle_t child;
277
278         err = gpiobus_init_softc(dev);
279         if (err != 0)
280                 return (err);
281  
282         bus_generic_probe(dev);
283         bus_enumerate_hinted_children(dev);
284
285         /*
286          * Attach the children represented in the device tree.
287          */
288         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
289             child = OF_peer(child))
290                 if (ofw_gpiobus_add_fdt_child(dev, child) == NULL)
291                         continue;
292
293         return (bus_generic_attach(dev));
294 }
295
296 static device_t
297 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
298 {
299         device_t child;
300         struct ofw_gpiobus_devinfo *devi;
301
302         child = device_add_child_ordered(dev, order, name, unit);
303         if (child == NULL)
304                 return (child);
305         devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
306             M_NOWAIT | M_ZERO);
307         if (devi == NULL) {
308                 device_delete_child(dev, child);
309                 return (0);
310         }
311
312         /*
313          * NULL all the OFW-related parts of the ivars for non-OFW
314          * children.
315          */
316         devi->opd_obdinfo.obd_node = -1;
317         devi->opd_obdinfo.obd_name = NULL;
318         devi->opd_obdinfo.obd_compat = NULL;
319         devi->opd_obdinfo.obd_type = NULL;
320         devi->opd_obdinfo.obd_model = NULL;
321
322         device_set_ivars(child, devi);
323
324         return (child);
325 }
326
327 static const struct ofw_bus_devinfo *
328 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
329 {
330         struct ofw_gpiobus_devinfo *dinfo;
331
332         dinfo = device_get_ivars(dev);
333
334         return (&dinfo->opd_obdinfo);
335 }
336
337 static device_method_t ofw_gpiobus_methods[] = {
338         /* Device interface */
339         DEVMETHOD(device_probe,         ofw_gpiobus_probe),
340         DEVMETHOD(device_attach,        ofw_gpiobus_attach),
341
342         /* Bus interface */
343         DEVMETHOD(bus_child_pnpinfo_str,        ofw_bus_gen_child_pnpinfo_str),
344         DEVMETHOD(bus_add_child,        ofw_gpiobus_add_child),
345
346         /* ofw_bus interface */
347         DEVMETHOD(ofw_bus_get_devinfo,  ofw_gpiobus_get_devinfo),
348         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
349         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
350         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
351         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
352         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
353
354         DEVMETHOD_END
355 };
356
357 static devclass_t ofwgpiobus_devclass;
358
359 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
360     sizeof(struct gpiobus_softc), gpiobus_driver);
361 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
362 MODULE_VERSION(ofw_gpiobus, 1);
363 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);