xref: /freebsd/sys/dev/gpio/ofw_gpiobus.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #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 int
59 gpio_pin_get_by_ofw_propidx(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_propidx(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_propidx(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 /*
140  * OFW_GPIOBUS driver.
141  */
142 device_t
143 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
144 {
145 	device_t childdev;
146 	int i;
147 	struct gpiobus_ivar *devi;
148 	struct ofw_gpiobus_devinfo *dinfo;
149 
150 	/*
151 	 * Check to see if we already have a child for @p child, and if so
152 	 * return it.
153 	 */
154 	childdev = ofw_bus_find_child_device_by_phandle(bus, child);
155 	if (childdev != NULL)
156 		return (childdev);
157 
158 	/*
159 	 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
160 	 */
161 	childdev = device_add_child(bus, drvname, -1);
162 	if (childdev == NULL)
163 		return (NULL);
164 	dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
165 	if (dinfo == NULL) {
166 		device_delete_child(bus, childdev);
167 		return (NULL);
168 	}
169 	if (device_probe_and_attach(childdev) != 0) {
170 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
171 		device_delete_child(bus, childdev);
172 		return (NULL);
173 	}
174 	/* Use the child name as pin name. */
175 	devi = &dinfo->opd_dinfo;
176 	for (i = 0; i < devi->npins; i++)
177 		GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
178 		    device_get_nameunit(childdev));
179 
180 	return (childdev);
181 }
182 
183 int
184 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
185 	struct gpiobus_pin **pins)
186 {
187 
188 	return (ofw_gpiobus_parse_gpios_impl(consumer,
189 	    ofw_bus_get_node(consumer), pname, NULL, pins));
190 }
191 
192 void
193 ofw_gpiobus_register_provider(device_t provider)
194 {
195 	phandle_t node;
196 
197 	node = ofw_bus_get_node(provider);
198 	if (node != -1)
199 		OF_device_register_xref(OF_xref_from_node(node), provider);
200 }
201 
202 void
203 ofw_gpiobus_unregister_provider(device_t provider)
204 {
205 	phandle_t node;
206 
207 	node = ofw_bus_get_node(provider);
208 	if (node != -1)
209 		OF_device_register_xref(OF_xref_from_node(node), NULL);
210 }
211 
212 static struct ofw_gpiobus_devinfo *
213 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
214 {
215 	int i, npins;
216 	struct gpiobus_ivar *devi;
217 	struct gpiobus_pin *pins;
218 	struct gpiobus_softc *sc;
219 	struct ofw_gpiobus_devinfo *dinfo;
220 
221 	sc = device_get_softc(bus);
222 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
223 	if (dinfo == NULL)
224 		return (NULL);
225 	if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
226 		free(dinfo, M_DEVBUF);
227 		return (NULL);
228 	}
229 	/* Parse the gpios property for the child. */
230 	npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
231 	if (npins <= 0) {
232 		ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
233 		free(dinfo, M_DEVBUF);
234 		return (NULL);
235 	}
236 	/* Initialize the irq resource list. */
237 	resource_list_init(&dinfo->opd_dinfo.rl);
238 	/* Allocate the child ivars and copy the parsed pin data. */
239 	devi = &dinfo->opd_dinfo;
240 	devi->npins = (uint32_t)npins;
241 	if (gpiobus_alloc_ivars(devi) != 0) {
242 		free(pins, M_DEVBUF);
243 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
244 		return (NULL);
245 	}
246 	for (i = 0; i < devi->npins; i++)
247 		devi->pins[i] = pins[i].pin;
248 	free(pins, M_DEVBUF);
249 	/* Parse the interrupt resources. */
250 	if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
251 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
252 		return (NULL);
253 	}
254 	device_set_ivars(child, dinfo);
255 
256 	return (dinfo);
257 }
258 
259 static void
260 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
261 {
262 	int i;
263 	struct gpiobus_ivar *devi;
264 	struct gpiobus_softc *sc;
265 
266 	sc = device_get_softc(bus);
267 	devi = &dinfo->opd_dinfo;
268 	for (i = 0; i < devi->npins; i++) {
269 		if (devi->pins[i] > sc->sc_npins)
270 			continue;
271 		sc->sc_pins[devi->pins[i]].mapped = 0;
272 	}
273 	gpiobus_free_ivars(devi);
274 	resource_list_free(&dinfo->opd_dinfo.rl);
275 	ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
276 	free(dinfo, M_DEVBUF);
277 }
278 
279 static int
280 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
281 	struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
282 {
283 	int gpiocells, i, j, ncells, npins;
284 	pcell_t *gpios;
285 	phandle_t gpio;
286 
287 	ncells = OF_getencprop_alloc_multi(cnode, pname, sizeof(*gpios),
288             (void **)&gpios);
289 	if (ncells == -1) {
290 		device_printf(consumer,
291 		    "Warning: No %s specified in fdt data; "
292 		    "device may not function.\n", pname);
293 		return (-1);
294 	}
295 	/*
296 	 * The gpio-specifier is controller independent, the first pcell has
297 	 * the reference to the GPIO controller phandler.
298 	 * Count the number of encoded gpio-specifiers on the first pass.
299 	 */
300 	i = 0;
301 	npins = 0;
302 	while (i < ncells) {
303 		/* Allow NULL specifiers. */
304 		if (gpios[i] == 0) {
305 			npins++;
306 			i++;
307 			continue;
308 		}
309 		gpio = OF_node_from_xref(gpios[i]);
310 		/* If we have bussc, ignore devices from other gpios. */
311 		if (bussc != NULL)
312 			if (ofw_bus_get_node(bussc->sc_dev) != gpio)
313 				return (0);
314 		/*
315 		 * Check for gpio-controller property and read the #gpio-cells
316 		 * for this GPIO controller.
317 		 */
318 		if (!OF_hasprop(gpio, "gpio-controller") ||
319 		    OF_getencprop(gpio, "#gpio-cells", &gpiocells,
320 		    sizeof(gpiocells)) < 0) {
321 			device_printf(consumer,
322 			    "gpio reference is not a gpio-controller.\n");
323 			OF_prop_free(gpios);
324 			return (-1);
325 		}
326 		if (ncells - i < gpiocells + 1) {
327 			device_printf(consumer,
328 			    "%s cells doesn't match #gpio-cells.\n", pname);
329 			return (-1);
330 		}
331 		npins++;
332 		i += gpiocells + 1;
333 	}
334 	if (npins == 0 || pins == NULL) {
335 		if (npins == 0)
336 			device_printf(consumer, "no pin specified in %s.\n",
337 			    pname);
338 		OF_prop_free(gpios);
339 		return (npins);
340 	}
341 	*pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
342 	    M_NOWAIT | M_ZERO);
343 	if (*pins == NULL) {
344 		OF_prop_free(gpios);
345 		return (-1);
346 	}
347 	/* Decode the gpio specifier on the second pass. */
348 	i = 0;
349 	j = 0;
350 	while (i < ncells) {
351 		/* Allow NULL specifiers. */
352 		if (gpios[i] == 0) {
353 			j++;
354 			i++;
355 			continue;
356 		}
357 		gpio = OF_node_from_xref(gpios[i]);
358 		/* Read gpio-cells property for this GPIO controller. */
359 		if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
360 		    sizeof(gpiocells)) < 0) {
361 			device_printf(consumer,
362 			    "gpio does not have the #gpio-cells property.\n");
363 			goto fail;
364 		}
365 		/* Return the device reference for the GPIO controller. */
366 		(*pins)[j].dev = OF_device_from_xref(gpios[i]);
367 		if ((*pins)[j].dev == NULL) {
368 			device_printf(consumer,
369 			    "no device registered for the gpio controller.\n");
370 			goto fail;
371 		}
372 		/*
373 		 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
374 		 * retrieve it.  The GPIO_GET_BUS() method is only valid after
375 		 * the child is probed and attached.
376 		 */
377 		if (bussc == NULL) {
378 			if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
379 				device_printf(consumer,
380 				    "no gpiobus reference for %s.\n",
381 				    device_get_nameunit((*pins)[j].dev));
382 				goto fail;
383 			}
384 			bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
385 		}
386 		/* Get the GPIO pin number and flags. */
387 		if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
388 		    &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
389 			device_printf(consumer,
390 			    "cannot map the gpios specifier.\n");
391 			goto fail;
392 		}
393 		/* Reserve the GPIO pin. */
394 		if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
395 			goto fail;
396 		j++;
397 		i += gpiocells + 1;
398 	}
399 	OF_prop_free(gpios);
400 
401 	return (npins);
402 
403 fail:
404 	OF_prop_free(gpios);
405 	free(*pins, M_DEVBUF);
406 	return (-1);
407 }
408 
409 static int
410 ofw_gpiobus_probe(device_t dev)
411 {
412 
413 	if (ofw_bus_get_node(dev) == -1)
414 		return (ENXIO);
415 	device_set_desc(dev, "OFW GPIO bus");
416 
417 	return (BUS_PROBE_DEFAULT);
418 }
419 
420 static int
421 ofw_gpiobus_attach(device_t dev)
422 {
423 	int err;
424 	phandle_t child;
425 
426 	err = gpiobus_init_softc(dev);
427 	if (err != 0)
428 		return (err);
429 	bus_generic_probe(dev);
430 	bus_enumerate_hinted_children(dev);
431 	/*
432 	 * Attach the children represented in the device tree.
433 	 */
434 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
435 	    child = OF_peer(child)) {
436 		if (OF_hasprop(child, "gpio-hog"))
437 			continue;
438 		if (!OF_hasprop(child, "gpios"))
439 			continue;
440 		if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
441 			continue;
442 	}
443 
444 	return (bus_generic_attach(dev));
445 }
446 
447 static device_t
448 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
449 {
450 	device_t child;
451 	struct ofw_gpiobus_devinfo *devi;
452 
453 	child = device_add_child_ordered(dev, order, name, unit);
454 	if (child == NULL)
455 		return (child);
456 	devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
457 	    M_NOWAIT | M_ZERO);
458 	if (devi == NULL) {
459 		device_delete_child(dev, child);
460 		return (0);
461 	}
462 
463 	/*
464 	 * NULL all the OFW-related parts of the ivars for non-OFW
465 	 * children.
466 	 */
467 	devi->opd_obdinfo.obd_node = -1;
468 	devi->opd_obdinfo.obd_name = NULL;
469 	devi->opd_obdinfo.obd_compat = NULL;
470 	devi->opd_obdinfo.obd_type = NULL;
471 	devi->opd_obdinfo.obd_model = NULL;
472 
473 	device_set_ivars(child, devi);
474 
475 	return (child);
476 }
477 
478 static const struct ofw_bus_devinfo *
479 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
480 {
481 	struct ofw_gpiobus_devinfo *dinfo;
482 
483 	dinfo = device_get_ivars(dev);
484 
485 	return (&dinfo->opd_obdinfo);
486 }
487 
488 static device_method_t ofw_gpiobus_methods[] = {
489 	/* Device interface */
490 	DEVMETHOD(device_probe,		ofw_gpiobus_probe),
491 	DEVMETHOD(device_attach,	ofw_gpiobus_attach),
492 
493 	/* Bus interface */
494 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
495 	DEVMETHOD(bus_add_child,	ofw_gpiobus_add_child),
496 
497 	/* ofw_bus interface */
498 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_gpiobus_get_devinfo),
499 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
500 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
501 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
502 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
503 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
504 
505 	DEVMETHOD_END
506 };
507 
508 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
509     sizeof(struct gpiobus_softc), gpiobus_driver);
510 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, 0, 0, BUS_PASS_BUS);
511 MODULE_VERSION(ofw_gpiobus, 1);
512 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);
513