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