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
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <dev/gpio/gpiobusvar.h>
40 #include <dev/ofw/ofw_bus.h>
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,
46 static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *);
49 ofw_gpiobus_add_fdt_child(device_t bus, phandle_t child)
51 struct ofw_gpiobus_devinfo *dinfo;
55 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
57 dinfo = ofw_gpiobus_setup_devinfo(bus, child);
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);
67 device_set_ivars(childdev, dinfo);
73 ofw_gpiobus_alloc_ivars(struct gpiobus_ivar *dinfo)
76 /* Allocate pins and flags memory. */
77 dinfo->pins = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
79 if (dinfo->pins == NULL)
81 dinfo->flags = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
83 if (dinfo->flags == NULL) {
84 free(dinfo->pins, M_DEVBUF);
92 ofw_gpiobus_free_ivars(struct gpiobus_ivar *dinfo)
95 free(dinfo->flags, M_DEVBUF);
96 free(dinfo->pins, M_DEVBUF);
100 ofw_gpiobus_parse_gpios(struct gpiobus_softc *sc, struct gpiobus_ivar *dinfo,
103 int cells, i, j, len;
107 /* Retrieve the gpios property. */
108 if ((len = OF_getproplen(child, "gpios")) < 0)
110 gpios = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
113 if (OF_getencprop(child, "gpios", gpios, len) < 0) {
114 free(gpios, M_DEVBUF);
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.
124 len /= sizeof(pcell_t);
126 /* Allow NULL specifiers. */
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);
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);
149 if (dinfo->npins == 0) {
150 free(gpios, M_DEVBUF);
154 /* Allocate the child resources. */
155 if (ofw_gpiobus_alloc_ivars(dinfo) != 0) {
156 free(gpios, M_DEVBUF);
160 /* Decode the gpio specifier on the second pass. */
164 /* Allow NULL specifiers. */
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);
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);
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);
199 * Mark pin as mapped and give warning if it's already mapped.
201 if (sc->sc_pins_mapped[dinfo->pins[j]]) {
202 device_printf(sc->sc_busdev,
203 "warning: pin %d is already mapped\n",
205 ofw_gpiobus_free_ivars(dinfo);
206 free(gpios, M_DEVBUF);
209 sc->sc_pins_mapped[dinfo->pins[j]] = 1;
215 free(gpios, M_DEVBUF);
220 static struct ofw_gpiobus_devinfo *
221 ofw_gpiobus_setup_devinfo(device_t dev, phandle_t node)
223 struct gpiobus_softc *sc;
224 struct ofw_gpiobus_devinfo *dinfo;
226 sc = device_get_softc(dev);
227 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
230 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
231 free(dinfo, M_DEVBUF);
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);
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);
253 ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *dinfo)
256 resource_list_free(&dinfo->opd_dinfo.rl);
257 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
258 free(dinfo, M_DEVBUF);
262 ofw_gpiobus_probe(device_t dev)
265 if (ofw_bus_get_node(dev) == -1)
267 device_set_desc(dev, "OFW GPIO bus");
273 ofw_gpiobus_attach(device_t dev)
278 err = gpiobus_init_softc(dev);
282 bus_generic_probe(dev);
283 bus_enumerate_hinted_children(dev);
286 * Attach the children represented in the device tree.
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)
293 return (bus_generic_attach(dev));
297 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
300 struct ofw_gpiobus_devinfo *devi;
302 child = device_add_child_ordered(dev, order, name, unit);
305 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
308 device_delete_child(dev, child);
313 * NULL all the OFW-related parts of the ivars for non-OFW
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;
322 device_set_ivars(child, devi);
327 static const struct ofw_bus_devinfo *
328 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
330 struct ofw_gpiobus_devinfo *dinfo;
332 dinfo = device_get_ivars(dev);
334 return (&dinfo->opd_obdinfo);
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),
343 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
344 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child),
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),
357 static devclass_t ofwgpiobus_devclass;
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);