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