xref: /freebsd/sys/dev/gpio/ofw_gpiobus.c (revision e12ff891366cf94db4bfe4c2c810b26a5531053d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
5  * Copyright (c) 2013, Luiz Otavio O Souza <loos@FreeBSD.org>
6  * Copyright (c) 2013 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 
41 #include <dev/gpio/gpiobusvar.h>
42 #include <dev/ofw/ofw_bus.h>
43 
44 #include "gpiobus_if.h"
45 
46 #define	GPIO_ACTIVE_LOW		1
47 
48 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
49 	device_t, phandle_t);
50 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
51 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
52 	struct gpiobus_softc *, struct gpiobus_pin **);
53 
54 /*
55  * Utility functions for easier handling of OFW GPIO pins.
56  *
57  * !!! BEWARE !!!
58  * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
59  * tree consumers.
60  *
61  */
62 int
63 gpio_pin_get_by_ofw_propidx(device_t consumer, phandle_t cnode,
64     char *prop_name, int idx, gpio_pin_t *out_pin)
65 {
66 	phandle_t xref;
67 	pcell_t *cells;
68 	device_t busdev;
69 	struct gpiobus_pin pin;
70 	int ncells, rv;
71 
72 	KASSERT(consumer != NULL && cnode > 0,
73 	    ("both consumer and cnode required"));
74 
75 	rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
76 	    idx, &xref, &ncells, &cells);
77 	if (rv != 0)
78 		return (rv);
79 
80 	/* Translate provider to device. */
81 	pin.dev = OF_device_from_xref(xref);
82 	if (pin.dev == NULL) {
83 		OF_prop_free(cells);
84 		return (ENODEV);
85 	}
86 
87 	/* Test if GPIO bus already exist. */
88 	busdev = GPIO_GET_BUS(pin.dev);
89 	if (busdev == NULL) {
90 		OF_prop_free(cells);
91 		return (ENODEV);
92 	}
93 
94 	/* Map GPIO pin. */
95 	rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
96 	    cells, &pin.pin, &pin.flags);
97 	OF_prop_free(cells);
98 	if (rv != 0)
99 		return (ENXIO);
100 
101 	/* Reserve GPIO pin. */
102 	rv = gpiobus_acquire_pin(busdev, pin.pin);
103 	if (rv != 0)
104 		return (EBUSY);
105 
106 	*out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
107 	    M_WAITOK | M_ZERO);
108 	**out_pin = pin;
109 	return (0);
110 }
111 
112 int
113 gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node,
114     int idx, gpio_pin_t *pin)
115 {
116 
117 	return (gpio_pin_get_by_ofw_propidx(consumer, node, "gpios", idx, pin));
118 }
119 
120 int
121 gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node,
122     char *name, gpio_pin_t *pin)
123 {
124 
125 	return (gpio_pin_get_by_ofw_propidx(consumer, node, name, 0, pin));
126 }
127 
128 int
129 gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node,
130     char *name, gpio_pin_t *pin)
131 {
132 	int rv, idx;
133 
134 	KASSERT(consumer != NULL && node > 0,
135 	    ("both consumer and node required"));
136 
137 	rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx);
138 	if (rv != 0)
139 		return (rv);
140 	return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin));
141 }
142 
143 void
144 gpio_pin_release(gpio_pin_t gpio)
145 {
146 	device_t busdev;
147 
148 	if (gpio == NULL)
149 		return;
150 
151 	KASSERT(gpio->dev != NULL, ("invalid pin state"));
152 
153 	busdev = GPIO_GET_BUS(gpio->dev);
154 	if (busdev != NULL)
155 		gpiobus_release_pin(busdev, gpio->pin);
156 
157 	/* XXXX Unreserve pin. */
158 	free(gpio, M_DEVBUF);
159 }
160 
161 int
162 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps)
163 {
164 
165 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
166 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
167 	return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps));
168 }
169 
170 int
171 gpio_pin_is_active(gpio_pin_t pin, bool *active)
172 {
173 	int rv;
174 	uint32_t tmp;
175 
176 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
177 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
178 	rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
179 	if (rv  != 0) {
180 		return (rv);
181 	}
182 
183 	if (pin->flags & GPIO_ACTIVE_LOW)
184 		*active = tmp == 0;
185 	else
186 		*active = tmp != 0;
187 	return (0);
188 }
189 
190 int
191 gpio_pin_set_active(gpio_pin_t pin, bool active)
192 {
193 	int rv;
194 	uint32_t tmp;
195 
196 	if (pin->flags & GPIO_ACTIVE_LOW)
197 		tmp = active ? 0 : 1;
198 	else
199 		tmp = active ? 1 : 0;
200 
201 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
202 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
203 	rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
204 	return (rv);
205 }
206 
207 int
208 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
209 {
210 	int rv;
211 
212 	KASSERT(pin != NULL, ("GPIO pin is NULL."));
213 	KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
214 
215 	rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
216 	return (rv);
217 }
218 
219 /*
220  * OFW_GPIOBUS driver.
221  */
222 device_t
223 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
224 {
225 	device_t childdev;
226 	int i;
227 	struct gpiobus_ivar *devi;
228 	struct ofw_gpiobus_devinfo *dinfo;
229 
230 	/*
231 	 * Check to see if we already have a child for @p child, and if so
232 	 * return it.
233 	 */
234 	childdev = ofw_bus_find_child_device_by_phandle(bus, child);
235 	if (childdev != NULL)
236 		return (childdev);
237 
238 	/*
239 	 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
240 	 */
241 	childdev = device_add_child(bus, drvname, -1);
242 	if (childdev == NULL)
243 		return (NULL);
244 	dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
245 	if (dinfo == NULL) {
246 		device_delete_child(bus, childdev);
247 		return (NULL);
248 	}
249 	if (device_probe_and_attach(childdev) != 0) {
250 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
251 		device_delete_child(bus, childdev);
252 		return (NULL);
253 	}
254 	/* Use the child name as pin name. */
255 	devi = &dinfo->opd_dinfo;
256 	for (i = 0; i < devi->npins; i++)
257 		GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
258 		    device_get_nameunit(childdev));
259 
260 	return (childdev);
261 }
262 
263 int
264 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
265 	struct gpiobus_pin **pins)
266 {
267 
268 	return (ofw_gpiobus_parse_gpios_impl(consumer,
269 	    ofw_bus_get_node(consumer), pname, NULL, pins));
270 }
271 
272 void
273 ofw_gpiobus_register_provider(device_t provider)
274 {
275 	phandle_t node;
276 
277 	node = ofw_bus_get_node(provider);
278 	OF_device_register_xref(OF_xref_from_node(node), provider);
279 }
280 
281 void
282 ofw_gpiobus_unregister_provider(device_t provider)
283 {
284 	phandle_t node;
285 
286 	node = ofw_bus_get_node(provider);
287 	OF_device_register_xref(OF_xref_from_node(node), NULL);
288 }
289 
290 static struct ofw_gpiobus_devinfo *
291 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
292 {
293 	int i, npins;
294 	struct gpiobus_ivar *devi;
295 	struct gpiobus_pin *pins;
296 	struct gpiobus_softc *sc;
297 	struct ofw_gpiobus_devinfo *dinfo;
298 
299 	sc = device_get_softc(bus);
300 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
301 	if (dinfo == NULL)
302 		return (NULL);
303 	if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
304 		free(dinfo, M_DEVBUF);
305 		return (NULL);
306 	}
307 	/* Parse the gpios property for the child. */
308 	npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
309 	if (npins <= 0) {
310 		ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
311 		free(dinfo, M_DEVBUF);
312 		return (NULL);
313 	}
314 	/* Initialize the irq resource list. */
315 	resource_list_init(&dinfo->opd_dinfo.rl);
316 	/* Allocate the child ivars and copy the parsed pin data. */
317 	devi = &dinfo->opd_dinfo;
318 	devi->npins = (uint32_t)npins;
319 	if (gpiobus_alloc_ivars(devi) != 0) {
320 		free(pins, M_DEVBUF);
321 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
322 		return (NULL);
323 	}
324 	for (i = 0; i < devi->npins; i++)
325 		devi->pins[i] = pins[i].pin;
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_multi(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);
592