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
46static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
47	device_t, phandle_t);
48static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
49static 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 */
60int
61gpio_pin_get_by_ofw_propidx(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
110int
111gpio_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_propidx(consumer, node, "gpios", idx, pin));
116}
117
118int
119gpio_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_propidx(consumer, node, name, 0, pin));
124}
125
126int
127gpio_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/*
142 * OFW_GPIOBUS driver.
143 */
144device_t
145ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
146{
147	device_t childdev;
148	int i;
149	struct gpiobus_ivar *devi;
150	struct ofw_gpiobus_devinfo *dinfo;
151
152	/*
153	 * Check to see if we already have a child for @p child, and if so
154	 * return it.
155	 */
156	childdev = ofw_bus_find_child_device_by_phandle(bus, child);
157	if (childdev != NULL)
158		return (childdev);
159
160	/*
161	 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
162	 */
163	childdev = device_add_child(bus, drvname, -1);
164	if (childdev == NULL)
165		return (NULL);
166	dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
167	if (dinfo == NULL) {
168		device_delete_child(bus, childdev);
169		return (NULL);
170	}
171	if (device_probe_and_attach(childdev) != 0) {
172		ofw_gpiobus_destroy_devinfo(bus, dinfo);
173		device_delete_child(bus, childdev);
174		return (NULL);
175	}
176	/* Use the child name as pin name. */
177	devi = &dinfo->opd_dinfo;
178	for (i = 0; i < devi->npins; i++)
179		GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
180		    device_get_nameunit(childdev));
181
182	return (childdev);
183}
184
185int
186ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
187	struct gpiobus_pin **pins)
188{
189
190	return (ofw_gpiobus_parse_gpios_impl(consumer,
191	    ofw_bus_get_node(consumer), pname, NULL, pins));
192}
193
194void
195ofw_gpiobus_register_provider(device_t provider)
196{
197	phandle_t node;
198
199	node = ofw_bus_get_node(provider);
200	OF_device_register_xref(OF_xref_from_node(node), provider);
201}
202
203void
204ofw_gpiobus_unregister_provider(device_t provider)
205{
206	phandle_t node;
207
208	node = ofw_bus_get_node(provider);
209	OF_device_register_xref(OF_xref_from_node(node), NULL);
210}
211
212static struct ofw_gpiobus_devinfo *
213ofw_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
259static void
260ofw_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
279static int
280ofw_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
403fail:
404	OF_prop_free(gpios);
405	free(*pins, M_DEVBUF);
406	return (-1);
407}
408
409static int
410ofw_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
420static int
421ofw_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
447static device_t
448ofw_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
478static const struct ofw_bus_devinfo *
479ofw_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
488static 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_str,	ofw_bus_gen_child_pnpinfo_str),
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
508devclass_t ofwgpiobus_devclass;
509
510DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
511    sizeof(struct gpiobus_softc), gpiobus_driver);
512EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass,
513    0, 0, BUS_PASS_BUS);
514MODULE_VERSION(ofw_gpiobus, 1);
515MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);
516