1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
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 <rtems/bsd/local/gpiobus_if.h> |
---|
45 | |
---|
46 | #define GPIO_ACTIVE_LOW 1 |
---|
47 | |
---|
48 | static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t, |
---|
49 | device_t, phandle_t); |
---|
50 | static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *); |
---|
51 | static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *, |
---|
52 | struct gpiobus_softc *, struct gpiobus_pin **); |
---|
53 | |
---|
54 | /* |
---|
55 | * Utility functions for easier handling of OFW GPIO pins. |
---|
56 | * |
---|
57 | * !!! BEWARE !!! |
---|
58 | * GPIOBUS uses children's IVARs, so we cannot use this interface for cross |
---|
59 | * tree consumers. |
---|
60 | * |
---|
61 | */ |
---|
62 | static int |
---|
63 | gpio_pin_get_by_ofw_impl(device_t consumer, phandle_t cnode, |
---|
64 | char *prop_name, int idx, gpio_pin_t *out_pin) |
---|
65 | { |
---|
66 | phandle_t xref; |
---|
67 | pcell_t *cells; |
---|
68 | device_t busdev; |
---|
69 | struct gpiobus_pin pin; |
---|
70 | int ncells, rv; |
---|
71 | |
---|
72 | KASSERT(consumer != NULL && cnode > 0, |
---|
73 | ("both consumer and cnode required")); |
---|
74 | |
---|
75 | rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells", |
---|
76 | idx, &xref, &ncells, &cells); |
---|
77 | if (rv != 0) |
---|
78 | return (rv); |
---|
79 | |
---|
80 | /* Translate provider to device. */ |
---|
81 | pin.dev = OF_device_from_xref(xref); |
---|
82 | if (pin.dev == NULL) { |
---|
83 | OF_prop_free(cells); |
---|
84 | return (ENODEV); |
---|
85 | } |
---|
86 | |
---|
87 | /* Test if GPIO bus already exist. */ |
---|
88 | busdev = GPIO_GET_BUS(pin.dev); |
---|
89 | if (busdev == NULL) { |
---|
90 | OF_prop_free(cells); |
---|
91 | return (ENODEV); |
---|
92 | } |
---|
93 | |
---|
94 | /* Map GPIO pin. */ |
---|
95 | rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells, |
---|
96 | cells, &pin.pin, &pin.flags); |
---|
97 | OF_prop_free(cells); |
---|
98 | if (rv != 0) |
---|
99 | return (ENXIO); |
---|
100 | |
---|
101 | /* Reserve GPIO pin. */ |
---|
102 | rv = gpiobus_acquire_pin(busdev, pin.pin); |
---|
103 | if (rv != 0) |
---|
104 | return (EBUSY); |
---|
105 | |
---|
106 | *out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF, |
---|
107 | M_WAITOK | M_ZERO); |
---|
108 | **out_pin = pin; |
---|
109 | return (0); |
---|
110 | } |
---|
111 | |
---|
112 | int |
---|
113 | gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node, |
---|
114 | int idx, gpio_pin_t *pin) |
---|
115 | { |
---|
116 | |
---|
117 | return (gpio_pin_get_by_ofw_impl(consumer, node, "gpios", idx, pin)); |
---|
118 | } |
---|
119 | |
---|
120 | int |
---|
121 | gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node, |
---|
122 | char *name, gpio_pin_t *pin) |
---|
123 | { |
---|
124 | |
---|
125 | return (gpio_pin_get_by_ofw_impl(consumer, node, name, 0, pin)); |
---|
126 | } |
---|
127 | |
---|
128 | int |
---|
129 | gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node, |
---|
130 | char *name, gpio_pin_t *pin) |
---|
131 | { |
---|
132 | int rv, idx; |
---|
133 | |
---|
134 | KASSERT(consumer != NULL && node > 0, |
---|
135 | ("both consumer and node required")); |
---|
136 | |
---|
137 | rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx); |
---|
138 | if (rv != 0) |
---|
139 | return (rv); |
---|
140 | return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin)); |
---|
141 | } |
---|
142 | |
---|
143 | void |
---|
144 | gpio_pin_release(gpio_pin_t gpio) |
---|
145 | { |
---|
146 | device_t busdev; |
---|
147 | |
---|
148 | if (gpio == NULL) |
---|
149 | return; |
---|
150 | |
---|
151 | KASSERT(gpio->dev != NULL, ("invalid pin state")); |
---|
152 | |
---|
153 | busdev = GPIO_GET_BUS(gpio->dev); |
---|
154 | if (busdev != NULL) |
---|
155 | gpiobus_release_pin(busdev, gpio->pin); |
---|
156 | |
---|
157 | /* XXXX Unreserve pin. */ |
---|
158 | free(gpio, M_DEVBUF); |
---|
159 | } |
---|
160 | |
---|
161 | int |
---|
162 | gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps) |
---|
163 | { |
---|
164 | |
---|
165 | KASSERT(pin != NULL, ("GPIO pin is NULL.")); |
---|
166 | KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); |
---|
167 | return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps)); |
---|
168 | } |
---|
169 | |
---|
170 | int |
---|
171 | gpio_pin_is_active(gpio_pin_t pin, bool *active) |
---|
172 | { |
---|
173 | int rv; |
---|
174 | uint32_t tmp; |
---|
175 | |
---|
176 | KASSERT(pin != NULL, ("GPIO pin is NULL.")); |
---|
177 | KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); |
---|
178 | rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp); |
---|
179 | if (rv != 0) { |
---|
180 | return (rv); |
---|
181 | } |
---|
182 | |
---|
183 | if (pin->flags & GPIO_ACTIVE_LOW) |
---|
184 | *active = tmp == 0; |
---|
185 | else |
---|
186 | *active = tmp != 0; |
---|
187 | return (0); |
---|
188 | } |
---|
189 | |
---|
190 | int |
---|
191 | gpio_pin_set_active(gpio_pin_t pin, bool active) |
---|
192 | { |
---|
193 | int rv; |
---|
194 | uint32_t tmp; |
---|
195 | |
---|
196 | if (pin->flags & GPIO_ACTIVE_LOW) |
---|
197 | tmp = active ? 0 : 1; |
---|
198 | else |
---|
199 | tmp = active ? 1 : 0; |
---|
200 | |
---|
201 | KASSERT(pin != NULL, ("GPIO pin is NULL.")); |
---|
202 | KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); |
---|
203 | rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp); |
---|
204 | return (rv); |
---|
205 | } |
---|
206 | |
---|
207 | int |
---|
208 | gpio_pin_setflags(gpio_pin_t pin, uint32_t flags) |
---|
209 | { |
---|
210 | int rv; |
---|
211 | |
---|
212 | KASSERT(pin != NULL, ("GPIO pin is NULL.")); |
---|
213 | KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); |
---|
214 | |
---|
215 | rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags); |
---|
216 | return (rv); |
---|
217 | } |
---|
218 | |
---|
219 | /* |
---|
220 | * OFW_GPIOBUS driver. |
---|
221 | */ |
---|
222 | device_t |
---|
223 | ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child) |
---|
224 | { |
---|
225 | device_t childdev; |
---|
226 | int i; |
---|
227 | struct gpiobus_ivar *devi; |
---|
228 | struct ofw_gpiobus_devinfo *dinfo; |
---|
229 | |
---|
230 | /* |
---|
231 | * Check to see if we already have a child for @p child, and if so |
---|
232 | * return it. |
---|
233 | */ |
---|
234 | childdev = ofw_bus_find_child_device_by_phandle(bus, child); |
---|
235 | if (childdev != NULL) |
---|
236 | return (childdev); |
---|
237 | |
---|
238 | /* |
---|
239 | * Set up the GPIO child and OFW bus layer devinfo and add it to bus. |
---|
240 | */ |
---|
241 | childdev = device_add_child(bus, drvname, -1); |
---|
242 | if (childdev == NULL) |
---|
243 | return (NULL); |
---|
244 | dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child); |
---|
245 | if (dinfo == NULL) { |
---|
246 | device_delete_child(bus, childdev); |
---|
247 | return (NULL); |
---|
248 | } |
---|
249 | if (device_probe_and_attach(childdev) != 0) { |
---|
250 | ofw_gpiobus_destroy_devinfo(bus, dinfo); |
---|
251 | device_delete_child(bus, childdev); |
---|
252 | return (NULL); |
---|
253 | } |
---|
254 | /* Use the child name as pin name. */ |
---|
255 | devi = &dinfo->opd_dinfo; |
---|
256 | for (i = 0; i < devi->npins; i++) |
---|
257 | GPIOBUS_PIN_SETNAME(bus, devi->pins[i], |
---|
258 | device_get_nameunit(childdev)); |
---|
259 | |
---|
260 | return (childdev); |
---|
261 | } |
---|
262 | |
---|
263 | int |
---|
264 | ofw_gpiobus_parse_gpios(device_t consumer, char *pname, |
---|
265 | struct gpiobus_pin **pins) |
---|
266 | { |
---|
267 | |
---|
268 | return (ofw_gpiobus_parse_gpios_impl(consumer, |
---|
269 | ofw_bus_get_node(consumer), pname, NULL, pins)); |
---|
270 | } |
---|
271 | |
---|
272 | void |
---|
273 | ofw_gpiobus_register_provider(device_t provider) |
---|
274 | { |
---|
275 | phandle_t node; |
---|
276 | |
---|
277 | node = ofw_bus_get_node(provider); |
---|
278 | OF_device_register_xref(OF_xref_from_node(node), provider); |
---|
279 | } |
---|
280 | |
---|
281 | void |
---|
282 | ofw_gpiobus_unregister_provider(device_t provider) |
---|
283 | { |
---|
284 | phandle_t node; |
---|
285 | |
---|
286 | node = ofw_bus_get_node(provider); |
---|
287 | OF_device_register_xref(OF_xref_from_node(node), NULL); |
---|
288 | } |
---|
289 | |
---|
290 | static struct ofw_gpiobus_devinfo * |
---|
291 | ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node) |
---|
292 | { |
---|
293 | int i, npins; |
---|
294 | struct gpiobus_ivar *devi; |
---|
295 | struct gpiobus_pin *pins; |
---|
296 | struct gpiobus_softc *sc; |
---|
297 | struct ofw_gpiobus_devinfo *dinfo; |
---|
298 | |
---|
299 | sc = device_get_softc(bus); |
---|
300 | dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO); |
---|
301 | if (dinfo == NULL) |
---|
302 | return (NULL); |
---|
303 | if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) { |
---|
304 | free(dinfo, M_DEVBUF); |
---|
305 | return (NULL); |
---|
306 | } |
---|
307 | /* Parse the gpios property for the child. */ |
---|
308 | npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins); |
---|
309 | if (npins <= 0) { |
---|
310 | ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); |
---|
311 | free(dinfo, M_DEVBUF); |
---|
312 | return (NULL); |
---|
313 | } |
---|
314 | /* Initialize the irq resource list. */ |
---|
315 | resource_list_init(&dinfo->opd_dinfo.rl); |
---|
316 | /* Allocate the child ivars and copy the parsed pin data. */ |
---|
317 | devi = &dinfo->opd_dinfo; |
---|
318 | devi->npins = (uint32_t)npins; |
---|
319 | if (gpiobus_alloc_ivars(devi) != 0) { |
---|
320 | free(pins, M_DEVBUF); |
---|
321 | ofw_gpiobus_destroy_devinfo(bus, dinfo); |
---|
322 | return (NULL); |
---|
323 | } |
---|
324 | for (i = 0; i < devi->npins; i++) { |
---|
325 | devi->flags[i] = pins[i].flags; |
---|
326 | devi->pins[i] = pins[i].pin; |
---|
327 | } |
---|
328 | free(pins, M_DEVBUF); |
---|
329 | /* Parse the interrupt resources. */ |
---|
330 | if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) { |
---|
331 | ofw_gpiobus_destroy_devinfo(bus, dinfo); |
---|
332 | return (NULL); |
---|
333 | } |
---|
334 | device_set_ivars(child, dinfo); |
---|
335 | |
---|
336 | return (dinfo); |
---|
337 | } |
---|
338 | |
---|
339 | static void |
---|
340 | ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo) |
---|
341 | { |
---|
342 | int i; |
---|
343 | struct gpiobus_ivar *devi; |
---|
344 | struct gpiobus_softc *sc; |
---|
345 | |
---|
346 | sc = device_get_softc(bus); |
---|
347 | devi = &dinfo->opd_dinfo; |
---|
348 | for (i = 0; i < devi->npins; i++) { |
---|
349 | if (devi->pins[i] > sc->sc_npins) |
---|
350 | continue; |
---|
351 | sc->sc_pins[devi->pins[i]].mapped = 0; |
---|
352 | } |
---|
353 | gpiobus_free_ivars(devi); |
---|
354 | resource_list_free(&dinfo->opd_dinfo.rl); |
---|
355 | ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); |
---|
356 | free(dinfo, M_DEVBUF); |
---|
357 | } |
---|
358 | |
---|
359 | static int |
---|
360 | ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname, |
---|
361 | struct gpiobus_softc *bussc, struct gpiobus_pin **pins) |
---|
362 | { |
---|
363 | int gpiocells, i, j, ncells, npins; |
---|
364 | pcell_t *gpios; |
---|
365 | phandle_t gpio; |
---|
366 | |
---|
367 | ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios), |
---|
368 | (void **)&gpios); |
---|
369 | if (ncells == -1) { |
---|
370 | device_printf(consumer, |
---|
371 | "Warning: No %s specified in fdt data; " |
---|
372 | "device may not function.\n", pname); |
---|
373 | return (-1); |
---|
374 | } |
---|
375 | /* |
---|
376 | * The gpio-specifier is controller independent, the first pcell has |
---|
377 | * the reference to the GPIO controller phandler. |
---|
378 | * Count the number of encoded gpio-specifiers on the first pass. |
---|
379 | */ |
---|
380 | i = 0; |
---|
381 | npins = 0; |
---|
382 | while (i < ncells) { |
---|
383 | /* Allow NULL specifiers. */ |
---|
384 | if (gpios[i] == 0) { |
---|
385 | npins++; |
---|
386 | i++; |
---|
387 | continue; |
---|
388 | } |
---|
389 | gpio = OF_node_from_xref(gpios[i]); |
---|
390 | /* If we have bussc, ignore devices from other gpios. */ |
---|
391 | if (bussc != NULL) |
---|
392 | if (ofw_bus_get_node(bussc->sc_dev) != gpio) |
---|
393 | return (0); |
---|
394 | /* |
---|
395 | * Check for gpio-controller property and read the #gpio-cells |
---|
396 | * for this GPIO controller. |
---|
397 | */ |
---|
398 | if (!OF_hasprop(gpio, "gpio-controller") || |
---|
399 | OF_getencprop(gpio, "#gpio-cells", &gpiocells, |
---|
400 | sizeof(gpiocells)) < 0) { |
---|
401 | device_printf(consumer, |
---|
402 | "gpio reference is not a gpio-controller.\n"); |
---|
403 | OF_prop_free(gpios); |
---|
404 | return (-1); |
---|
405 | } |
---|
406 | if (ncells - i < gpiocells + 1) { |
---|
407 | device_printf(consumer, |
---|
408 | "%s cells doesn't match #gpio-cells.\n", pname); |
---|
409 | return (-1); |
---|
410 | } |
---|
411 | npins++; |
---|
412 | i += gpiocells + 1; |
---|
413 | } |
---|
414 | if (npins == 0 || pins == NULL) { |
---|
415 | if (npins == 0) |
---|
416 | device_printf(consumer, "no pin specified in %s.\n", |
---|
417 | pname); |
---|
418 | OF_prop_free(gpios); |
---|
419 | return (npins); |
---|
420 | } |
---|
421 | *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF, |
---|
422 | M_NOWAIT | M_ZERO); |
---|
423 | if (*pins == NULL) { |
---|
424 | OF_prop_free(gpios); |
---|
425 | return (-1); |
---|
426 | } |
---|
427 | /* Decode the gpio specifier on the second pass. */ |
---|
428 | i = 0; |
---|
429 | j = 0; |
---|
430 | while (i < ncells) { |
---|
431 | /* Allow NULL specifiers. */ |
---|
432 | if (gpios[i] == 0) { |
---|
433 | j++; |
---|
434 | i++; |
---|
435 | continue; |
---|
436 | } |
---|
437 | gpio = OF_node_from_xref(gpios[i]); |
---|
438 | /* Read gpio-cells property for this GPIO controller. */ |
---|
439 | if (OF_getencprop(gpio, "#gpio-cells", &gpiocells, |
---|
440 | sizeof(gpiocells)) < 0) { |
---|
441 | device_printf(consumer, |
---|
442 | "gpio does not have the #gpio-cells property.\n"); |
---|
443 | goto fail; |
---|
444 | } |
---|
445 | /* Return the device reference for the GPIO controller. */ |
---|
446 | (*pins)[j].dev = OF_device_from_xref(gpios[i]); |
---|
447 | if ((*pins)[j].dev == NULL) { |
---|
448 | device_printf(consumer, |
---|
449 | "no device registered for the gpio controller.\n"); |
---|
450 | goto fail; |
---|
451 | } |
---|
452 | /* |
---|
453 | * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to |
---|
454 | * retrieve it. The GPIO_GET_BUS() method is only valid after |
---|
455 | * the child is probed and attached. |
---|
456 | */ |
---|
457 | if (bussc == NULL) { |
---|
458 | if (GPIO_GET_BUS((*pins)[j].dev) == NULL) { |
---|
459 | device_printf(consumer, |
---|
460 | "no gpiobus reference for %s.\n", |
---|
461 | device_get_nameunit((*pins)[j].dev)); |
---|
462 | goto fail; |
---|
463 | } |
---|
464 | bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev)); |
---|
465 | } |
---|
466 | /* Get the GPIO pin number and flags. */ |
---|
467 | if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells, |
---|
468 | &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) { |
---|
469 | device_printf(consumer, |
---|
470 | "cannot map the gpios specifier.\n"); |
---|
471 | goto fail; |
---|
472 | } |
---|
473 | /* Reserve the GPIO pin. */ |
---|
474 | if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0) |
---|
475 | goto fail; |
---|
476 | j++; |
---|
477 | i += gpiocells + 1; |
---|
478 | } |
---|
479 | OF_prop_free(gpios); |
---|
480 | |
---|
481 | return (npins); |
---|
482 | |
---|
483 | fail: |
---|
484 | OF_prop_free(gpios); |
---|
485 | free(*pins, M_DEVBUF); |
---|
486 | return (-1); |
---|
487 | } |
---|
488 | |
---|
489 | static int |
---|
490 | ofw_gpiobus_probe(device_t dev) |
---|
491 | { |
---|
492 | |
---|
493 | if (ofw_bus_get_node(dev) == -1) |
---|
494 | return (ENXIO); |
---|
495 | device_set_desc(dev, "OFW GPIO bus"); |
---|
496 | |
---|
497 | return (0); |
---|
498 | } |
---|
499 | |
---|
500 | static int |
---|
501 | ofw_gpiobus_attach(device_t dev) |
---|
502 | { |
---|
503 | int err; |
---|
504 | phandle_t child; |
---|
505 | |
---|
506 | err = gpiobus_init_softc(dev); |
---|
507 | if (err != 0) |
---|
508 | return (err); |
---|
509 | bus_generic_probe(dev); |
---|
510 | bus_enumerate_hinted_children(dev); |
---|
511 | /* |
---|
512 | * Attach the children represented in the device tree. |
---|
513 | */ |
---|
514 | for (child = OF_child(ofw_bus_get_node(dev)); child != 0; |
---|
515 | child = OF_peer(child)) { |
---|
516 | if (!OF_hasprop(child, "gpios")) |
---|
517 | continue; |
---|
518 | if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL) |
---|
519 | continue; |
---|
520 | } |
---|
521 | |
---|
522 | return (bus_generic_attach(dev)); |
---|
523 | } |
---|
524 | |
---|
525 | static device_t |
---|
526 | ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) |
---|
527 | { |
---|
528 | device_t child; |
---|
529 | struct ofw_gpiobus_devinfo *devi; |
---|
530 | |
---|
531 | child = device_add_child_ordered(dev, order, name, unit); |
---|
532 | if (child == NULL) |
---|
533 | return (child); |
---|
534 | devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF, |
---|
535 | M_NOWAIT | M_ZERO); |
---|
536 | if (devi == NULL) { |
---|
537 | device_delete_child(dev, child); |
---|
538 | return (0); |
---|
539 | } |
---|
540 | |
---|
541 | /* |
---|
542 | * NULL all the OFW-related parts of the ivars for non-OFW |
---|
543 | * children. |
---|
544 | */ |
---|
545 | devi->opd_obdinfo.obd_node = -1; |
---|
546 | devi->opd_obdinfo.obd_name = NULL; |
---|
547 | devi->opd_obdinfo.obd_compat = NULL; |
---|
548 | devi->opd_obdinfo.obd_type = NULL; |
---|
549 | devi->opd_obdinfo.obd_model = NULL; |
---|
550 | |
---|
551 | device_set_ivars(child, devi); |
---|
552 | |
---|
553 | return (child); |
---|
554 | } |
---|
555 | |
---|
556 | static const struct ofw_bus_devinfo * |
---|
557 | ofw_gpiobus_get_devinfo(device_t bus, device_t dev) |
---|
558 | { |
---|
559 | struct ofw_gpiobus_devinfo *dinfo; |
---|
560 | |
---|
561 | dinfo = device_get_ivars(dev); |
---|
562 | |
---|
563 | return (&dinfo->opd_obdinfo); |
---|
564 | } |
---|
565 | |
---|
566 | static device_method_t ofw_gpiobus_methods[] = { |
---|
567 | /* Device interface */ |
---|
568 | DEVMETHOD(device_probe, ofw_gpiobus_probe), |
---|
569 | DEVMETHOD(device_attach, ofw_gpiobus_attach), |
---|
570 | |
---|
571 | /* Bus interface */ |
---|
572 | DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), |
---|
573 | DEVMETHOD(bus_add_child, ofw_gpiobus_add_child), |
---|
574 | |
---|
575 | /* ofw_bus interface */ |
---|
576 | DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo), |
---|
577 | DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), |
---|
578 | DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), |
---|
579 | DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), |
---|
580 | DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), |
---|
581 | DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), |
---|
582 | |
---|
583 | DEVMETHOD_END |
---|
584 | }; |
---|
585 | |
---|
586 | devclass_t ofwgpiobus_devclass; |
---|
587 | |
---|
588 | DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods, |
---|
589 | sizeof(struct gpiobus_softc), gpiobus_driver); |
---|
590 | EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, |
---|
591 | 0, 0, BUS_PASS_BUS); |
---|
592 | MODULE_VERSION(ofw_gpiobus, 1); |
---|
593 | MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1); |
---|