xref: /freebsd/sys/dev/gpio/ofw_gpiobus.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
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 int ofw_gpiobus_parse_gpios(struct gpiobus_softc *,
43     struct gpiobus_ivar *, phandle_t);
44 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
45     phandle_t);
46 static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *);
47 
48 device_t
49 ofw_gpiobus_add_fdt_child(device_t bus, phandle_t child)
50 {
51 	struct ofw_gpiobus_devinfo *dinfo;
52 	device_t childdev;
53 
54 	/*
55 	 * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
56 	 */
57 	dinfo = ofw_gpiobus_setup_devinfo(bus, child);
58 	if (dinfo == NULL)
59 		return (NULL);
60 	childdev = device_add_child(bus, NULL, -1);
61 	if (childdev == NULL) {
62 		device_printf(bus, "could not add child: %s\n",
63 		    dinfo->opd_obdinfo.obd_name);
64 		ofw_gpiobus_destroy_devinfo(dinfo);
65 		return (NULL);
66 	}
67 	device_set_ivars(childdev, dinfo);
68 
69 	return (childdev);
70 }
71 
72 static int
73 ofw_gpiobus_alloc_ivars(struct gpiobus_ivar *dinfo)
74 {
75 
76 	/* Allocate pins and flags memory. */
77 	dinfo->pins = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
78 	    M_NOWAIT | M_ZERO);
79 	if (dinfo->pins == NULL)
80 		return (ENOMEM);
81 	dinfo->flags = malloc(sizeof(uint32_t) * dinfo->npins, M_DEVBUF,
82 	    M_NOWAIT | M_ZERO);
83 	if (dinfo->flags == NULL) {
84 		free(dinfo->pins, M_DEVBUF);
85 		return (ENOMEM);
86 	}
87 
88 	return (0);
89 }
90 
91 static void
92 ofw_gpiobus_free_ivars(struct gpiobus_ivar *dinfo)
93 {
94 
95 	free(dinfo->flags, M_DEVBUF);
96 	free(dinfo->pins, M_DEVBUF);
97 }
98 
99 static int
100 ofw_gpiobus_parse_gpios(struct gpiobus_softc *sc, struct gpiobus_ivar *dinfo,
101 	phandle_t child)
102 {
103 	int cells, i, j, len;
104 	pcell_t *gpios;
105 	phandle_t gpio;
106 
107 	/* Retrieve the gpios property. */
108 	if ((len = OF_getproplen(child, "gpios")) < 0)
109 		return (EINVAL);
110 	gpios = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
111 	if (gpios == NULL)
112 		return (ENOMEM);
113 	if (OF_getencprop(child, "gpios", gpios, len) < 0) {
114 		free(gpios, M_DEVBUF);
115 		return (EINVAL);
116 	}
117 
118 	/*
119 	 * The gpio-specifier is controller independent, but the first pcell
120 	 * has the reference to the GPIO controller phandler.
121 	 * One the first pass we count the number of encoded gpio-specifiers.
122 	 */
123 	i = 0;
124 	len /= sizeof(pcell_t);
125 	while (i < len) {
126 		/* Allow NULL specifiers. */
127 		if (gpios[i] == 0) {
128 			dinfo->npins++;
129 			i++;
130 			continue;
131 		}
132 		gpio = OF_node_from_xref(gpios[i]);
133 		/* Verify if we're attaching to the correct GPIO controller. */
134 		if (!OF_hasprop(gpio, "gpio-controller") ||
135 		    gpio != ofw_bus_get_node(sc->sc_dev)) {
136 			free(gpios, M_DEVBUF);
137 			return (EINVAL);
138 		}
139 		/* Read gpio-cells property for this GPIO controller. */
140 		if (OF_getencprop(gpio, "#gpio-cells", &cells,
141 		    sizeof(cells)) < 0) {
142 			free(gpios, M_DEVBUF);
143 			return (EINVAL);
144 		}
145 		dinfo->npins++;
146 		i += cells + 1;
147 	}
148 
149 	if (dinfo->npins == 0) {
150 		free(gpios, M_DEVBUF);
151 		return (EINVAL);
152 	}
153 
154 	/* Allocate the child resources. */
155 	if (ofw_gpiobus_alloc_ivars(dinfo) != 0) {
156 		free(gpios, M_DEVBUF);
157 		return (ENOMEM);
158 	}
159 
160 	/* Decode the gpio specifier on the second pass. */
161 	i = 0;
162 	j = 0;
163 	while (i < len) {
164 		/* Allow NULL specifiers. */
165 		if (gpios[i] == 0) {
166 			i++;
167 			j++;
168 			continue;
169 		}
170 
171 		gpio = OF_node_from_xref(gpios[i]);
172 		/* Read gpio-cells property for this GPIO controller. */
173 		if (OF_getencprop(gpio, "#gpio-cells", &cells,
174 		    sizeof(cells)) < 0) {
175 			ofw_gpiobus_free_ivars(dinfo);
176 			free(gpios, M_DEVBUF);
177 			return (EINVAL);
178 		}
179 
180 		/* Get the GPIO pin number and flags. */
181 		if (gpio_map_gpios(sc->sc_dev, child, gpio, cells,
182 		    &gpios[i + 1], &dinfo->pins[j], &dinfo->flags[j]) != 0) {
183 			ofw_gpiobus_free_ivars(dinfo);
184 			free(gpios, M_DEVBUF);
185 			return (EINVAL);
186 		}
187 
188 		/* Consistency check. */
189 		if (dinfo->pins[j] > sc->sc_npins) {
190 			device_printf(sc->sc_busdev,
191 			    "invalid pin %d, max: %d\n",
192 			    dinfo->pins[j], sc->sc_npins - 1);
193 			ofw_gpiobus_free_ivars(dinfo);
194 			free(gpios, M_DEVBUF);
195 			return (EINVAL);
196 		}
197 
198 		/*
199 		 * Mark pin as mapped and give warning if it's already mapped.
200 		 */
201 		if (sc->sc_pins_mapped[dinfo->pins[j]]) {
202 			device_printf(sc->sc_busdev,
203 			    "warning: pin %d is already mapped\n",
204 			    dinfo->pins[j]);
205 			ofw_gpiobus_free_ivars(dinfo);
206 			free(gpios, M_DEVBUF);
207 			return (EINVAL);
208 		}
209 		sc->sc_pins_mapped[dinfo->pins[j]] = 1;
210 
211 		i += cells + 1;
212 		j++;
213 	}
214 
215 	free(gpios, M_DEVBUF);
216 
217 	return (0);
218 }
219 
220 static struct ofw_gpiobus_devinfo *
221 ofw_gpiobus_setup_devinfo(device_t dev, phandle_t node)
222 {
223 	struct gpiobus_softc *sc;
224 	struct ofw_gpiobus_devinfo *dinfo;
225 
226 	sc = device_get_softc(dev);
227 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
228 	if (dinfo == NULL)
229 		return (NULL);
230 	if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
231 		free(dinfo, M_DEVBUF);
232 		return (NULL);
233 	}
234 
235 	/* Parse the gpios property for the child. */
236 	if (ofw_gpiobus_parse_gpios(sc, &dinfo->opd_dinfo, node) != 0) {
237 		ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
238 		free(dinfo, M_DEVBUF);
239 		return (NULL);
240 	}
241 
242 	return (dinfo);
243 }
244 
245 static void
246 ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *dinfo)
247 {
248 
249 	ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
250 	free(dinfo, M_DEVBUF);
251 }
252 
253 static int
254 ofw_gpiobus_probe(device_t dev)
255 {
256 
257 	if (ofw_bus_get_node(dev) == -1)
258 		return (ENXIO);
259 	device_set_desc(dev, "OFW GPIO bus");
260 
261 	return (0);
262 }
263 
264 static int
265 ofw_gpiobus_attach(device_t dev)
266 {
267 	int err;
268 	phandle_t child;
269 
270 	err = gpiobus_init_softc(dev);
271 	if (err != 0)
272 		return (err);
273 
274 	bus_generic_probe(dev);
275 	bus_enumerate_hinted_children(dev);
276 
277 	/*
278 	 * Attach the children represented in the device tree.
279 	 */
280 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
281 	    child = OF_peer(child))
282 		if (ofw_gpiobus_add_fdt_child(dev, child) == NULL)
283 			continue;
284 
285 	return (bus_generic_attach(dev));
286 }
287 
288 static device_t
289 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
290 {
291 	device_t child;
292 	struct ofw_gpiobus_devinfo *devi;
293 
294 	child = device_add_child_ordered(dev, order, name, unit);
295 	if (child == NULL)
296 		return (child);
297 	devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
298 	    M_NOWAIT | M_ZERO);
299 	if (devi == NULL) {
300 		device_delete_child(dev, child);
301 		return (0);
302 	}
303 
304 	/*
305 	 * NULL all the OFW-related parts of the ivars for non-OFW
306 	 * children.
307 	 */
308 	devi->opd_obdinfo.obd_node = -1;
309 	devi->opd_obdinfo.obd_name = NULL;
310 	devi->opd_obdinfo.obd_compat = NULL;
311 	devi->opd_obdinfo.obd_type = NULL;
312 	devi->opd_obdinfo.obd_model = NULL;
313 
314 	device_set_ivars(child, devi);
315 
316 	return (child);
317 }
318 
319 static int
320 ofw_gpiobus_print_child(device_t dev, device_t child)
321 {
322 	struct ofw_gpiobus_devinfo *devi;
323 	int retval = 0;
324 
325 	devi = device_get_ivars(child);
326 	retval += bus_print_child_header(dev, child);
327 	retval += printf(" at pin(s) ");
328 	gpiobus_print_pins(&devi->opd_dinfo);
329 	retval += bus_print_child_footer(dev, child);
330 
331 	return (retval);
332 }
333 
334 static const struct ofw_bus_devinfo *
335 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
336 {
337 	struct ofw_gpiobus_devinfo *dinfo;
338 
339 	dinfo = device_get_ivars(dev);
340 
341 	return (&dinfo->opd_obdinfo);
342 }
343 
344 static device_method_t ofw_gpiobus_methods[] = {
345 	/* Device interface */
346 	DEVMETHOD(device_probe,		ofw_gpiobus_probe),
347 	DEVMETHOD(device_attach,	ofw_gpiobus_attach),
348 
349 	/* Bus interface */
350 	DEVMETHOD(bus_child_pnpinfo_str,	ofw_bus_gen_child_pnpinfo_str),
351 	DEVMETHOD(bus_print_child,	ofw_gpiobus_print_child),
352 	DEVMETHOD(bus_add_child,	ofw_gpiobus_add_child),
353 
354 	/* ofw_bus interface */
355 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_gpiobus_get_devinfo),
356 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
357 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
358 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
359 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
360 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
361 
362 	DEVMETHOD_END
363 };
364 
365 static devclass_t ofwgpiobus_devclass;
366 
367 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
368     sizeof(struct gpiobus_softc), gpiobus_driver);
369 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
370 MODULE_VERSION(ofw_gpiobus, 1);
371 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);
372