ofw_gpiobus.c revision 273652
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: stable/10/sys/dev/gpio/ofw_gpiobus.c 273652 2014-10-26 01:30:46Z ian $");
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
42static int ofw_gpiobus_parse_gpios(struct gpiobus_softc *,
43    struct gpiobus_ivar *, phandle_t);
44static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
45    phandle_t);
46static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *);
47
48device_t
49ofw_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
72static int
73ofw_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
91static void
92ofw_gpiobus_free_ivars(struct gpiobus_ivar *dinfo)
93{
94
95	free(dinfo->flags, M_DEVBUF);
96	free(dinfo->pins, M_DEVBUF);
97}
98
99static int
100ofw_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
220static struct ofw_gpiobus_devinfo *
221ofw_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
245static void
246ofw_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
253static int
254ofw_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
264static int
265ofw_gpiobus_attach(device_t dev)
266{
267	struct gpiobus_softc *sc;
268	phandle_t child;
269
270	sc = GPIOBUS_SOFTC(dev);
271	sc->sc_busdev = dev;
272	sc->sc_dev = device_get_parent(dev);
273
274	/* Read the pin max. value */
275	if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
276		return (ENXIO);
277
278	KASSERT(sc->sc_npins != 0, ("GPIO device with no pins"));
279
280	/*
281	 * Increase to get number of pins.
282	 */
283	sc->sc_npins++;
284
285	sc->sc_pins_mapped = malloc(sizeof(int) * sc->sc_npins, M_DEVBUF,
286	    M_NOWAIT | M_ZERO);
287
288	if (!sc->sc_pins_mapped)
289		return (ENOMEM);
290
291	/* Init the bus lock. */
292	GPIOBUS_LOCK_INIT(sc);
293
294	bus_generic_probe(dev);
295	bus_enumerate_hinted_children(dev);
296
297	/*
298	 * Attach the children represented in the device tree.
299	 */
300	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
301	    child = OF_peer(child))
302		if (ofw_gpiobus_add_fdt_child(dev, child) == NULL)
303			continue;
304
305	return (bus_generic_attach(dev));
306}
307
308static device_t
309ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
310{
311	device_t child;
312	struct ofw_gpiobus_devinfo *devi;
313
314	child = device_add_child_ordered(dev, order, name, unit);
315	if (child == NULL)
316		return (child);
317	devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
318	    M_NOWAIT | M_ZERO);
319	if (devi == NULL) {
320		device_delete_child(dev, child);
321		return (0);
322	}
323
324	/*
325	 * NULL all the OFW-related parts of the ivars for non-OFW
326	 * children.
327	 */
328	devi->opd_obdinfo.obd_node = -1;
329	devi->opd_obdinfo.obd_name = NULL;
330	devi->opd_obdinfo.obd_compat = NULL;
331	devi->opd_obdinfo.obd_type = NULL;
332	devi->opd_obdinfo.obd_model = NULL;
333
334	device_set_ivars(child, devi);
335
336	return (child);
337}
338
339static int
340ofw_gpiobus_print_child(device_t dev, device_t child)
341{
342	struct ofw_gpiobus_devinfo *devi;
343	int retval = 0;
344
345	devi = device_get_ivars(child);
346	retval += bus_print_child_header(dev, child);
347	retval += printf(" at pin(s) ");
348	gpiobus_print_pins(&devi->opd_dinfo);
349	retval += bus_print_child_footer(dev, child);
350
351	return (retval);
352}
353
354static const struct ofw_bus_devinfo *
355ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
356{
357	struct ofw_gpiobus_devinfo *dinfo;
358
359	dinfo = device_get_ivars(dev);
360
361	return (&dinfo->opd_obdinfo);
362}
363
364static device_method_t ofw_gpiobus_methods[] = {
365	/* Device interface */
366	DEVMETHOD(device_probe,		ofw_gpiobus_probe),
367	DEVMETHOD(device_attach,	ofw_gpiobus_attach),
368
369	/* Bus interface */
370	DEVMETHOD(bus_child_pnpinfo_str,	ofw_bus_gen_child_pnpinfo_str),
371	DEVMETHOD(bus_print_child,	ofw_gpiobus_print_child),
372	DEVMETHOD(bus_add_child,	ofw_gpiobus_add_child),
373
374	/* ofw_bus interface */
375	DEVMETHOD(ofw_bus_get_devinfo,	ofw_gpiobus_get_devinfo),
376	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
377	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
378	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
379	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
380	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
381
382	DEVMETHOD_END
383};
384
385static devclass_t ofwgpiobus_devclass;
386
387DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
388    sizeof(struct gpiobus_softc), gpiobus_driver);
389DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0);
390MODULE_VERSION(ofw_gpiobus, 1);
391MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);
392