]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/gpio/ofw_gpiobus.c
MFV r315791: ntp 4.2.8p10.
[FreeBSD/FreeBSD.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 #include "gpiobus_if.h"
43
44 #define GPIO_ACTIVE_LOW         1
45
46 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
47         device_t, phandle_t);
48 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
49 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
50         struct gpiobus_softc *, struct gpiobus_pin **);
51
52 /*
53  * Utility functions for easier handling of OFW GPIO pins.
54  *
55  * !!! BEWARE !!!
56  * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
57  * tree consumers.
58  *
59  */
60 static int
61 gpio_pin_get_by_ofw_impl(device_t consumer, phandle_t cnode,
62     char *prop_name, int idx, gpio_pin_t *out_pin)
63 {
64         phandle_t xref;
65         pcell_t *cells;
66         device_t busdev;
67         struct gpiobus_pin pin;
68         int ncells, rv;
69
70         KASSERT(consumer != NULL && cnode > 0,
71             ("both consumer and cnode required"));
72
73         rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
74             idx, &xref, &ncells, &cells);
75         if (rv != 0)
76                 return (rv);
77
78         /* Translate provider to device. */
79         pin.dev = OF_device_from_xref(xref);
80         if (pin.dev == NULL) {
81                 OF_prop_free(cells);
82                 return (ENODEV);
83         }
84
85         /* Test if GPIO bus already exist. */
86         busdev = GPIO_GET_BUS(pin.dev);
87         if (busdev == NULL) {
88                 OF_prop_free(cells);
89                 return (ENODEV);
90         }
91
92         /* Map GPIO pin. */
93         rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
94             cells, &pin.pin, &pin.flags);
95         OF_prop_free(cells);
96         if (rv != 0)
97                 return (ENXIO);
98
99         /* Reserve GPIO pin. */
100         rv = gpiobus_acquire_pin(busdev, pin.pin);
101         if (rv != 0)
102                 return (EBUSY);
103
104         *out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
105             M_WAITOK | M_ZERO);
106         **out_pin = pin;
107         return (0);
108 }
109
110 int
111 gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node,
112     int idx, gpio_pin_t *pin)
113 {
114
115         return (gpio_pin_get_by_ofw_impl(consumer, node, "gpios", idx, pin));
116 }
117
118 int
119 gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node,
120     char *name, gpio_pin_t *pin)
121 {
122
123         return (gpio_pin_get_by_ofw_impl(consumer, node, name, 0, pin));
124 }
125
126 int
127 gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node,
128     char *name, gpio_pin_t *pin)
129 {
130         int rv, idx;
131
132         KASSERT(consumer != NULL && node > 0,
133             ("both consumer and node required"));
134
135         rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx);
136         if (rv != 0)
137                 return (rv);
138         return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin));
139 }
140
141 void
142 gpio_pin_release(gpio_pin_t gpio)
143 {
144         device_t busdev;
145
146         if (gpio == NULL)
147                 return;
148
149         KASSERT(gpio->dev != NULL, ("invalid pin state"));
150
151         busdev = GPIO_GET_BUS(gpio->dev);
152         if (busdev != NULL)
153                 gpiobus_release_pin(busdev, gpio->pin);
154
155         /* XXXX Unreserve pin. */
156         free(gpio, M_DEVBUF);
157 }
158
159 int
160 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps)
161 {
162
163         KASSERT(pin != NULL, ("GPIO pin is NULL."));
164         KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
165         return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps));
166 }
167
168 int
169 gpio_pin_is_active(gpio_pin_t pin, bool *active)
170 {
171         int rv;
172         uint32_t tmp;
173
174         KASSERT(pin != NULL, ("GPIO pin is NULL."));
175         KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
176         rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
177         if (rv  != 0) {
178                 return (rv);
179         }
180
181         if (pin->flags & GPIO_ACTIVE_LOW)
182                 *active = tmp == 0;
183         else
184                 *active = tmp != 0;
185         return (0);
186 }
187
188 int
189 gpio_pin_set_active(gpio_pin_t pin, bool active)
190 {
191         int rv;
192         uint32_t tmp;
193
194         if (pin->flags & GPIO_ACTIVE_LOW)
195                 tmp = active ? 0 : 1;
196         else
197                 tmp = active ? 1 : 0;
198
199         KASSERT(pin != NULL, ("GPIO pin is NULL."));
200         KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
201         rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
202         return (rv);
203 }
204
205 int
206 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
207 {
208         int rv;
209
210         KASSERT(pin != NULL, ("GPIO pin is NULL."));
211         KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
212
213         rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
214         return (rv);
215 }
216
217 /*
218  * OFW_GPIOBUS driver.
219  */
220 device_t
221 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
222 {
223         device_t childdev;
224         int i;
225         struct gpiobus_ivar *devi;
226         struct ofw_gpiobus_devinfo *dinfo;
227
228         /*
229          * Check to see if we already have a child for @p child, and if so
230          * return it.
231          */
232         childdev = ofw_bus_find_child_device_by_phandle(bus, child);
233         if (childdev != NULL)
234                 return (childdev);
235
236         /*
237          * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
238          */
239         childdev = device_add_child(bus, drvname, -1);
240         if (childdev == NULL)
241                 return (NULL);
242         dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
243         if (dinfo == NULL) {
244                 device_delete_child(bus, childdev);
245                 return (NULL);
246         }
247         if (device_probe_and_attach(childdev) != 0) {
248                 ofw_gpiobus_destroy_devinfo(bus, dinfo);
249                 device_delete_child(bus, childdev);
250                 return (NULL);
251         }
252         /* Use the child name as pin name. */
253         devi = &dinfo->opd_dinfo;
254         for (i = 0; i < devi->npins; i++)
255                 GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
256                     device_get_nameunit(childdev));
257
258         return (childdev);
259 }
260
261 int
262 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
263         struct gpiobus_pin **pins)
264 {
265
266         return (ofw_gpiobus_parse_gpios_impl(consumer,
267             ofw_bus_get_node(consumer), pname, NULL, pins));
268 }
269
270 void
271 ofw_gpiobus_register_provider(device_t provider)
272 {
273         phandle_t node;
274
275         node = ofw_bus_get_node(provider);
276         OF_device_register_xref(OF_xref_from_node(node), provider);
277 }
278
279 void
280 ofw_gpiobus_unregister_provider(device_t provider)
281 {
282         phandle_t node;
283
284         node = ofw_bus_get_node(provider);
285         OF_device_register_xref(OF_xref_from_node(node), NULL);
286 }
287
288 static struct ofw_gpiobus_devinfo *
289 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
290 {
291         int i, npins;
292         struct gpiobus_ivar *devi;
293         struct gpiobus_pin *pins;
294         struct gpiobus_softc *sc;
295         struct ofw_gpiobus_devinfo *dinfo;
296
297         sc = device_get_softc(bus);
298         dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
299         if (dinfo == NULL)
300                 return (NULL);
301         if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
302                 free(dinfo, M_DEVBUF);
303                 return (NULL);
304         }
305         /* Parse the gpios property for the child. */
306         npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
307         if (npins <= 0) {
308                 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
309                 free(dinfo, M_DEVBUF);
310                 return (NULL);
311         }
312         /* Initialize the irq resource list. */
313         resource_list_init(&dinfo->opd_dinfo.rl);
314         /* Allocate the child ivars and copy the parsed pin data. */
315         devi = &dinfo->opd_dinfo;
316         devi->npins = (uint32_t)npins;
317         if (gpiobus_alloc_ivars(devi) != 0) {
318                 free(pins, M_DEVBUF);
319                 ofw_gpiobus_destroy_devinfo(bus, dinfo);
320                 return (NULL);
321         }
322         for (i = 0; i < devi->npins; i++) {
323                 devi->flags[i] = pins[i].flags;
324                 devi->pins[i] = pins[i].pin;
325         }
326         free(pins, M_DEVBUF);
327         /* Parse the interrupt resources. */
328         if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
329                 ofw_gpiobus_destroy_devinfo(bus, dinfo);
330                 return (NULL);
331         }
332         device_set_ivars(child, dinfo);
333
334         return (dinfo);
335 }
336
337 static void
338 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
339 {
340         int i;
341         struct gpiobus_ivar *devi;
342         struct gpiobus_softc *sc;
343
344         sc = device_get_softc(bus);
345         devi = &dinfo->opd_dinfo;
346         for (i = 0; i < devi->npins; i++) {
347                 if (devi->pins[i] > sc->sc_npins)
348                         continue;
349                 sc->sc_pins[devi->pins[i]].mapped = 0;
350         }
351         gpiobus_free_ivars(devi);
352         resource_list_free(&dinfo->opd_dinfo.rl);
353         ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
354         free(dinfo, M_DEVBUF);
355 }
356
357 static int
358 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
359         struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
360 {
361         int gpiocells, i, j, ncells, npins;
362         pcell_t *gpios;
363         phandle_t gpio;
364
365         ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios),
366             (void **)&gpios);
367         if (ncells == -1) {
368                 device_printf(consumer,
369                     "Warning: No %s specified in fdt data; "
370                     "device may not function.\n", pname);
371                 return (-1);
372         }
373         /*
374          * The gpio-specifier is controller independent, the first pcell has
375          * the reference to the GPIO controller phandler.
376          * Count the number of encoded gpio-specifiers on the first pass.
377          */
378         i = 0;
379         npins = 0;
380         while (i < ncells) {
381                 /* Allow NULL specifiers. */
382                 if (gpios[i] == 0) {
383                         npins++;
384                         i++;
385                         continue;
386                 }
387                 gpio = OF_node_from_xref(gpios[i]);
388                 /* If we have bussc, ignore devices from other gpios. */
389                 if (bussc != NULL)
390                         if (ofw_bus_get_node(bussc->sc_dev) != gpio)
391                                 return (0);
392                 /*
393                  * Check for gpio-controller property and read the #gpio-cells
394                  * for this GPIO controller.
395                  */
396                 if (!OF_hasprop(gpio, "gpio-controller") ||
397                     OF_getencprop(gpio, "#gpio-cells", &gpiocells,
398                     sizeof(gpiocells)) < 0) {
399                         device_printf(consumer,
400                             "gpio reference is not a gpio-controller.\n");
401                         OF_prop_free(gpios);
402                         return (-1);
403                 }
404                 if (ncells - i < gpiocells + 1) {
405                         device_printf(consumer,
406                             "%s cells doesn't match #gpio-cells.\n", pname);
407                         return (-1);
408                 }
409                 npins++;
410                 i += gpiocells + 1;
411         }
412         if (npins == 0 || pins == NULL) {
413                 if (npins == 0)
414                         device_printf(consumer, "no pin specified in %s.\n",
415                             pname);
416                 OF_prop_free(gpios);
417                 return (npins);
418         }
419         *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
420             M_NOWAIT | M_ZERO);
421         if (*pins == NULL) {
422                 OF_prop_free(gpios);
423                 return (-1);
424         }
425         /* Decode the gpio specifier on the second pass. */
426         i = 0;
427         j = 0;
428         while (i < ncells) {
429                 /* Allow NULL specifiers. */
430                 if (gpios[i] == 0) {
431                         j++;
432                         i++;
433                         continue;
434                 }
435                 gpio = OF_node_from_xref(gpios[i]);
436                 /* Read gpio-cells property for this GPIO controller. */
437                 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
438                     sizeof(gpiocells)) < 0) {
439                         device_printf(consumer,
440                             "gpio does not have the #gpio-cells property.\n");
441                         goto fail;
442                 }
443                 /* Return the device reference for the GPIO controller. */
444                 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
445                 if ((*pins)[j].dev == NULL) {
446                         device_printf(consumer,
447                             "no device registered for the gpio controller.\n");
448                         goto fail;
449                 }
450                 /*
451                  * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
452                  * retrieve it.  The GPIO_GET_BUS() method is only valid after
453                  * the child is probed and attached.
454                  */
455                 if (bussc == NULL) {
456                         if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
457                                 device_printf(consumer,
458                                     "no gpiobus reference for %s.\n",
459                                     device_get_nameunit((*pins)[j].dev));
460                                 goto fail;
461                         }
462                         bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
463                 }
464                 /* Get the GPIO pin number and flags. */
465                 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
466                     &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
467                         device_printf(consumer,
468                             "cannot map the gpios specifier.\n");
469                         goto fail;
470                 }
471                 /* Reserve the GPIO pin. */
472                 if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
473                         goto fail;
474                 j++;
475                 i += gpiocells + 1;
476         }
477         OF_prop_free(gpios);
478
479         return (npins);
480
481 fail:
482         OF_prop_free(gpios);
483         free(*pins, M_DEVBUF);
484         return (-1);
485 }
486
487 static int
488 ofw_gpiobus_probe(device_t dev)
489 {
490
491         if (ofw_bus_get_node(dev) == -1)
492                 return (ENXIO);
493         device_set_desc(dev, "OFW GPIO bus");
494
495         return (0);
496 }
497
498 static int
499 ofw_gpiobus_attach(device_t dev)
500 {
501         int err;
502         phandle_t child;
503
504         err = gpiobus_init_softc(dev);
505         if (err != 0)
506                 return (err);
507         bus_generic_probe(dev);
508         bus_enumerate_hinted_children(dev);
509         /*
510          * Attach the children represented in the device tree.
511          */
512         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
513             child = OF_peer(child)) {
514                 if (!OF_hasprop(child, "gpios"))
515                         continue;
516                 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
517                         continue;
518         }
519
520         return (bus_generic_attach(dev));
521 }
522
523 static device_t
524 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
525 {
526         device_t child;
527         struct ofw_gpiobus_devinfo *devi;
528
529         child = device_add_child_ordered(dev, order, name, unit);
530         if (child == NULL)
531                 return (child);
532         devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
533             M_NOWAIT | M_ZERO);
534         if (devi == NULL) {
535                 device_delete_child(dev, child);
536                 return (0);
537         }
538
539         /*
540          * NULL all the OFW-related parts of the ivars for non-OFW
541          * children.
542          */
543         devi->opd_obdinfo.obd_node = -1;
544         devi->opd_obdinfo.obd_name = NULL;
545         devi->opd_obdinfo.obd_compat = NULL;
546         devi->opd_obdinfo.obd_type = NULL;
547         devi->opd_obdinfo.obd_model = NULL;
548
549         device_set_ivars(child, devi);
550
551         return (child);
552 }
553
554 static const struct ofw_bus_devinfo *
555 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
556 {
557         struct ofw_gpiobus_devinfo *dinfo;
558
559         dinfo = device_get_ivars(dev);
560
561         return (&dinfo->opd_obdinfo);
562 }
563
564 static device_method_t ofw_gpiobus_methods[] = {
565         /* Device interface */
566         DEVMETHOD(device_probe,         ofw_gpiobus_probe),
567         DEVMETHOD(device_attach,        ofw_gpiobus_attach),
568
569         /* Bus interface */
570         DEVMETHOD(bus_child_pnpinfo_str,        ofw_bus_gen_child_pnpinfo_str),
571         DEVMETHOD(bus_add_child,        ofw_gpiobus_add_child),
572
573         /* ofw_bus interface */
574         DEVMETHOD(ofw_bus_get_devinfo,  ofw_gpiobus_get_devinfo),
575         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
576         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
577         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
578         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
579         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
580
581         DEVMETHOD_END
582 };
583
584 devclass_t ofwgpiobus_devclass;
585
586 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
587     sizeof(struct gpiobus_softc), gpiobus_driver);
588 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass,
589     0, 0, BUS_PASS_BUS);
590 MODULE_VERSION(ofw_gpiobus, 1);
591 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);