xref: /freebsd/sys/dev/gpio/ofw_gpiobus.c (revision 5b9c547c)
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 device_t
51 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
52 {
53 	device_t childdev;
54 	int i;
55 	struct gpiobus_ivar *devi;
56 	struct ofw_gpiobus_devinfo *dinfo;
57 
58 	/*
59 	 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
60 	 */
61 	childdev = device_add_child(bus, drvname, -1);
62 	if (childdev == NULL)
63 		return (NULL);
64 	dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
65 	if (dinfo == NULL) {
66 		device_delete_child(bus, childdev);
67 		return (NULL);
68 	}
69 	if (device_probe_and_attach(childdev) != 0) {
70 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
71 		device_delete_child(bus, childdev);
72 		return (NULL);
73 	}
74 	/* Use the child name as pin name. */
75 	devi = &dinfo->opd_dinfo;
76 	for (i = 0; i < devi->npins; i++)
77 		GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
78 		    device_get_nameunit(childdev));
79 
80 	return (childdev);
81 }
82 
83 int
84 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
85 	struct gpiobus_pin **pins)
86 {
87 
88 	return (ofw_gpiobus_parse_gpios_impl(consumer,
89 	    ofw_bus_get_node(consumer), pname, NULL, pins));
90 }
91 
92 void
93 ofw_gpiobus_register_provider(device_t provider)
94 {
95 	phandle_t node;
96 
97 	node = ofw_bus_get_node(provider);
98 	OF_device_register_xref(OF_xref_from_node(node), provider);
99 }
100 
101 void
102 ofw_gpiobus_unregister_provider(device_t provider)
103 {
104 	phandle_t node;
105 
106 	node = ofw_bus_get_node(provider);
107 	OF_device_register_xref(OF_xref_from_node(node), NULL);
108 }
109 
110 static struct ofw_gpiobus_devinfo *
111 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
112 {
113 	int i, npins;
114 	struct gpiobus_ivar *devi;
115 	struct gpiobus_pin *pins;
116 	struct gpiobus_softc *sc;
117 	struct ofw_gpiobus_devinfo *dinfo;
118 
119 	sc = device_get_softc(bus);
120 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
121 	if (dinfo == NULL)
122 		return (NULL);
123 	if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
124 		free(dinfo, M_DEVBUF);
125 		return (NULL);
126 	}
127 	/* Parse the gpios property for the child. */
128 	npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
129 	if (npins <= 0) {
130 		ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
131 		free(dinfo, M_DEVBUF);
132 		return (NULL);
133 	}
134 	/* Initialize the irq resource list. */
135 	resource_list_init(&dinfo->opd_dinfo.rl);
136 	/* Allocate the child ivars and copy the parsed pin data. */
137 	devi = &dinfo->opd_dinfo;
138 	devi->npins = (uint32_t)npins;
139 	if (gpiobus_alloc_ivars(devi) != 0) {
140 		free(pins, M_DEVBUF);
141 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
142 		return (NULL);
143 	}
144 	for (i = 0; i < devi->npins; i++) {
145 		devi->flags[i] = pins[i].flags;
146 		devi->pins[i] = pins[i].pin;
147 	}
148 	free(pins, M_DEVBUF);
149 	/* Parse the interrupt resources. */
150 	if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl) != 0) {
151 		ofw_gpiobus_destroy_devinfo(bus, dinfo);
152 		return (NULL);
153 	}
154 	device_set_ivars(child, dinfo);
155 
156 	return (dinfo);
157 }
158 
159 static void
160 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
161 {
162 	int i;
163 	struct gpiobus_ivar *devi;
164 	struct gpiobus_softc *sc;
165 
166 	sc = device_get_softc(bus);
167 	devi = &dinfo->opd_dinfo;
168 	for (i = 0; i < devi->npins; i++) {
169 		if (devi->pins[i] > sc->sc_npins)
170 			continue;
171 		sc->sc_pins[devi->pins[i]].mapped = 0;
172 	}
173 	gpiobus_free_ivars(devi);
174 	resource_list_free(&dinfo->opd_dinfo.rl);
175 	ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
176 	free(dinfo, M_DEVBUF);
177 }
178 
179 static int
180 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
181 	struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
182 {
183 	int gpiocells, i, j, ncells, npins;
184 	pcell_t *gpios;
185 	phandle_t gpio;
186 
187 	ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios),
188             (void **)&gpios);
189 	if (ncells == -1) {
190 		device_printf(consumer,
191 		    "Warning: No %s specified in fdt data; "
192 		    "device may not function.\n", pname);
193 		return (-1);
194 	}
195 	/*
196 	 * The gpio-specifier is controller independent, the first pcell has
197 	 * the reference to the GPIO controller phandler.
198 	 * Count the number of encoded gpio-specifiers on the first pass.
199 	 */
200 	i = 0;
201 	npins = 0;
202 	while (i < ncells) {
203 		/* Allow NULL specifiers. */
204 		if (gpios[i] == 0) {
205 			npins++;
206 			i++;
207 			continue;
208 		}
209 		gpio = OF_node_from_xref(gpios[i]);
210 		/* If we have bussc, ignore devices from other gpios. */
211 		if (bussc != NULL)
212 			if (ofw_bus_get_node(bussc->sc_dev) != gpio)
213 				return (0);
214 		/*
215 		 * Check for gpio-controller property and read the #gpio-cells
216 		 * for this GPIO controller.
217 		 */
218 		if (!OF_hasprop(gpio, "gpio-controller") ||
219 		    OF_getencprop(gpio, "#gpio-cells", &gpiocells,
220 		    sizeof(gpiocells)) < 0) {
221 			device_printf(consumer,
222 			    "gpio reference is not a gpio-controller.\n");
223 			free(gpios, M_OFWPROP);
224 			return (-1);
225 		}
226 		if (ncells - i < gpiocells + 1) {
227 			device_printf(consumer,
228 			    "%s cells doesn't match #gpio-cells.\n", pname);
229 			return (-1);
230 		}
231 		npins++;
232 		i += gpiocells + 1;
233 	}
234 	if (npins == 0 || pins == NULL) {
235 		if (npins == 0)
236 			device_printf(consumer, "no pin specified in %s.\n",
237 			    pname);
238 		free(gpios, M_OFWPROP);
239 		return (npins);
240 	}
241 	*pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
242 	    M_NOWAIT | M_ZERO);
243 	if (*pins == NULL) {
244 		free(gpios, M_OFWPROP);
245 		return (-1);
246 	}
247 	/* Decode the gpio specifier on the second pass. */
248 	i = 0;
249 	j = 0;
250 	while (i < ncells) {
251 		/* Allow NULL specifiers. */
252 		if (gpios[i] == 0) {
253 			j++;
254 			i++;
255 			continue;
256 		}
257 		gpio = OF_node_from_xref(gpios[i]);
258 		/* Read gpio-cells property for this GPIO controller. */
259 		if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
260 		    sizeof(gpiocells)) < 0) {
261 			device_printf(consumer,
262 			    "gpio does not have the #gpio-cells property.\n");
263 			goto fail;
264 		}
265 		/* Return the device reference for the GPIO controller. */
266 		(*pins)[j].dev = OF_device_from_xref(gpios[i]);
267 		if ((*pins)[j].dev == NULL) {
268 			device_printf(consumer,
269 			    "no device registered for the gpio controller.\n");
270 			goto fail;
271 		}
272 		/*
273 		 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
274 		 * retrieve it.  The GPIO_GET_BUS() method is only valid after
275 		 * the child is probed and attached.
276 		 */
277 		if (bussc == NULL) {
278 			if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
279 				device_printf(consumer,
280 				    "no gpiobus reference for %s.\n",
281 				    device_get_nameunit((*pins)[j].dev));
282 				goto fail;
283 			}
284 			bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
285 		}
286 		/* Get the GPIO pin number and flags. */
287 		if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
288 		    &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
289 			device_printf(consumer,
290 			    "cannot map the gpios specifier.\n");
291 			goto fail;
292 		}
293 		/* Reserve the GPIO pin. */
294 		if (gpiobus_map_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
295 			goto fail;
296 		j++;
297 		i += gpiocells + 1;
298 	}
299 	free(gpios, M_OFWPROP);
300 
301 	return (npins);
302 
303 fail:
304 	free(gpios, M_OFWPROP);
305 	free(*pins, M_DEVBUF);
306 	return (-1);
307 }
308 
309 static int
310 ofw_gpiobus_probe(device_t dev)
311 {
312 
313 	if (ofw_bus_get_node(dev) == -1)
314 		return (ENXIO);
315 	device_set_desc(dev, "OFW GPIO bus");
316 
317 	return (0);
318 }
319 
320 static int
321 ofw_gpiobus_attach(device_t dev)
322 {
323 	int err;
324 	phandle_t child;
325 
326 	err = gpiobus_init_softc(dev);
327 	if (err != 0)
328 		return (err);
329 	bus_generic_probe(dev);
330 	bus_enumerate_hinted_children(dev);
331 	/*
332 	 * Attach the children represented in the device tree.
333 	 */
334 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
335 	    child = OF_peer(child)) {
336 		if (!OF_hasprop(child, "gpios"))
337 			continue;
338 		if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
339 			continue;
340 	}
341 
342 	return (bus_generic_attach(dev));
343 }
344 
345 static device_t
346 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
347 {
348 	device_t child;
349 	struct ofw_gpiobus_devinfo *devi;
350 
351 	child = device_add_child_ordered(dev, order, name, unit);
352 	if (child == NULL)
353 		return (child);
354 	devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
355 	    M_NOWAIT | M_ZERO);
356 	if (devi == NULL) {
357 		device_delete_child(dev, child);
358 		return (0);
359 	}
360 
361 	/*
362 	 * NULL all the OFW-related parts of the ivars for non-OFW
363 	 * children.
364 	 */
365 	devi->opd_obdinfo.obd_node = -1;
366 	devi->opd_obdinfo.obd_name = NULL;
367 	devi->opd_obdinfo.obd_compat = NULL;
368 	devi->opd_obdinfo.obd_type = NULL;
369 	devi->opd_obdinfo.obd_model = NULL;
370 
371 	device_set_ivars(child, devi);
372 
373 	return (child);
374 }
375 
376 static const struct ofw_bus_devinfo *
377 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
378 {
379 	struct ofw_gpiobus_devinfo *dinfo;
380 
381 	dinfo = device_get_ivars(dev);
382 
383 	return (&dinfo->opd_obdinfo);
384 }
385 
386 static device_method_t ofw_gpiobus_methods[] = {
387 	/* Device interface */
388 	DEVMETHOD(device_probe,		ofw_gpiobus_probe),
389 	DEVMETHOD(device_attach,	ofw_gpiobus_attach),
390 
391 	/* Bus interface */
392 	DEVMETHOD(bus_child_pnpinfo_str,	ofw_bus_gen_child_pnpinfo_str),
393 	DEVMETHOD(bus_add_child,	ofw_gpiobus_add_child),
394 
395 	/* ofw_bus interface */
396 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_gpiobus_get_devinfo),
397 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
398 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
399 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
400 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
401 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
402 
403 	DEVMETHOD_END
404 };
405 
406 static devclass_t ofwgpiobus_devclass;
407 
408 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
409     sizeof(struct gpiobus_softc), gpiobus_driver);
410 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
411 MODULE_VERSION(ofw_gpiobus, 1);
412 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);
413