1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/gpio.h>
33 #ifdef INTRNG
34 #include <sys/intr.h>
35 #endif
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/sbuf.h>
40
41 #include <dev/gpio/gpiobusvar.h>
42 #include <dev/gpio/gpiobus_internal.h>
43
44 #include "gpiobus_if.h"
45
46 #undef GPIOBUS_DEBUG
47 #ifdef GPIOBUS_DEBUG
48 #define dprintf printf
49 #else
50 #define dprintf(x, arg...)
51 #endif
52
53 static void gpiobus_print_pins(struct gpiobus_ivar *, struct sbuf *);
54 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
55 static int gpiobus_probe(device_t);
56 static int gpiobus_suspend(device_t);
57 static int gpiobus_resume(device_t);
58 static void gpiobus_probe_nomatch(device_t, device_t);
59 static int gpiobus_print_child(device_t, device_t);
60 static int gpiobus_child_location(device_t, device_t, struct sbuf *);
61 static device_t gpiobus_add_child(device_t, u_int, const char *, int);
62 static void gpiobus_hinted_child(device_t, const char *, int);
63
64 /*
65 * GPIOBUS interface
66 */
67 static int gpiobus_acquire_bus(device_t, device_t, int);
68 static void gpiobus_release_bus(device_t, device_t);
69 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
70 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
71 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
72 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
73 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
74 static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
75
76 /*
77 * gpiobus_pin flags
78 * The flags in struct gpiobus_pin are not related to the flags used by the
79 * low-level controller driver in struct gpio_pin. Currently, only pins
80 * acquired via FDT data have gpiobus_pin.flags set, sourced from the flags in
81 * the FDT properties. In theory, these flags are defined per-platform. In
82 * practice they are always the flags from the dt-bindings/gpio/gpio.h file.
83 * The only one of those flags we currently support is for handling active-low
84 * pins, so we just define that flag here instead of including a GPL'd header.
85 */
86 #define GPIO_ACTIVE_LOW 1
87
88 /*
89 * XXX -> Move me to better place - gpio_subr.c?
90 * Also, this function must be changed when interrupt configuration
91 * data will be moved into struct resource.
92 */
93 #ifdef INTRNG
94
95 struct resource *
gpio_alloc_intr_resource(device_t consumer_dev,int * rid,u_int alloc_flags,gpio_pin_t pin,uint32_t intr_mode)96 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
97 gpio_pin_t pin, uint32_t intr_mode)
98 {
99 u_int irq;
100 struct intr_map_data_gpio *gpio_data;
101 struct resource *res;
102
103 gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data(
104 INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO);
105 gpio_data->gpio_pin_num = pin->pin;
106 gpio_data->gpio_pin_flags = pin->flags;
107 gpio_data->gpio_intr_mode = intr_mode;
108
109 irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data);
110 res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1,
111 alloc_flags);
112 if (res == NULL) {
113 intr_free_intr_map_data((struct intr_map_data *)gpio_data);
114 return (NULL);
115 }
116 rman_set_virtual(res, gpio_data);
117 return (res);
118 }
119 #else
120 struct resource *
gpio_alloc_intr_resource(device_t consumer_dev,int * rid,u_int alloc_flags,gpio_pin_t pin,uint32_t intr_mode)121 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
122 gpio_pin_t pin, uint32_t intr_mode)
123 {
124
125 return (NULL);
126 }
127 #endif
128
129 int
gpio_check_flags(uint32_t caps,uint32_t flags)130 gpio_check_flags(uint32_t caps, uint32_t flags)
131 {
132
133 /* Filter unwanted flags. */
134 flags &= caps;
135
136 /* Cannot mix input/output together. */
137 if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT)
138 return (EINVAL);
139 /* Cannot mix pull-up/pull-down together. */
140 if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN)
141 return (EINVAL);
142 /* Cannot mix output and interrupt flags together */
143 if (flags & GPIO_PIN_OUTPUT && flags & GPIO_INTR_MASK)
144 return (EINVAL);
145 /* Only one interrupt flag can be defined at once */
146 if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1))
147 return (EINVAL);
148 /* The interrupt attached flag cannot be set */
149 if (flags & GPIO_INTR_ATTACHED)
150 return (EINVAL);
151
152 return (0);
153 }
154
155 int
gpio_pin_get_by_bus_pinnum(device_t busdev,uint32_t pinnum,gpio_pin_t * ppin)156 gpio_pin_get_by_bus_pinnum(device_t busdev, uint32_t pinnum, gpio_pin_t *ppin)
157 {
158 gpio_pin_t pin;
159 int err;
160
161 err = gpiobus_acquire_pin(busdev, pinnum);
162 if (err != 0)
163 return (EBUSY);
164
165 pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO);
166
167 pin->dev = device_get_parent(busdev);
168 pin->pin = pinnum;
169 pin->flags = 0;
170
171 *ppin = pin;
172 return (0);
173 }
174
175 int
gpio_pin_get_by_child_index(device_t childdev,uint32_t idx,gpio_pin_t * ppin)176 gpio_pin_get_by_child_index(device_t childdev, uint32_t idx, gpio_pin_t *ppin)
177 {
178 struct gpiobus_ivar *devi;
179
180 devi = GPIOBUS_IVAR(childdev);
181 if (idx >= devi->npins)
182 return (EINVAL);
183
184 return (gpio_pin_get_by_bus_pinnum(device_get_parent(childdev),
185 devi->pins[idx], ppin));
186 }
187
188 int
gpio_pin_getcaps(gpio_pin_t pin,uint32_t * caps)189 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps)
190 {
191
192 KASSERT(pin != NULL, ("GPIO pin is NULL."));
193 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
194 return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps));
195 }
196
197 int
gpio_pin_is_active(gpio_pin_t pin,bool * active)198 gpio_pin_is_active(gpio_pin_t pin, bool *active)
199 {
200 int rv;
201 uint32_t tmp;
202
203 KASSERT(pin != NULL, ("GPIO pin is NULL."));
204 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
205 rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
206 if (rv != 0) {
207 return (rv);
208 }
209
210 if (pin->flags & GPIO_ACTIVE_LOW)
211 *active = tmp == 0;
212 else
213 *active = tmp != 0;
214 return (0);
215 }
216
217 /*
218 * Note that this function should only
219 * be used in cases where a pre-existing
220 * gpiobus_pin structure exists. In most
221 * cases, the gpio_pin_get_by_* functions
222 * suffice.
223 */
224 int
gpio_pin_acquire(gpio_pin_t gpio)225 gpio_pin_acquire(gpio_pin_t gpio)
226 {
227 device_t busdev;
228
229 KASSERT(gpio != NULL, ("GPIO pin is NULL."));
230 KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL."));
231
232 busdev = GPIO_GET_BUS(gpio->dev);
233 if (busdev == NULL)
234 return (ENXIO);
235
236 return (gpiobus_acquire_pin(busdev, gpio->pin));
237 }
238
239 void
gpio_pin_release(gpio_pin_t gpio)240 gpio_pin_release(gpio_pin_t gpio)
241 {
242 device_t busdev;
243
244 KASSERT(gpio != NULL, ("GPIO pin is NULL."));
245 KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL."));
246
247 busdev = GPIO_GET_BUS(gpio->dev);
248 KASSERT(busdev != NULL, ("gpiobus dev is NULL."));
249
250 gpiobus_release_pin(busdev, gpio->pin);
251 free(gpio, M_DEVBUF);
252 }
253
254 int
gpio_pin_set_active(gpio_pin_t pin,bool active)255 gpio_pin_set_active(gpio_pin_t pin, bool active)
256 {
257 int rv;
258 uint32_t tmp;
259
260 if (pin->flags & GPIO_ACTIVE_LOW)
261 tmp = active ? 0 : 1;
262 else
263 tmp = active ? 1 : 0;
264
265 KASSERT(pin != NULL, ("GPIO pin is NULL."));
266 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
267 rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
268 return (rv);
269 }
270
271 int
gpio_pin_setflags(gpio_pin_t pin,uint32_t flags)272 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
273 {
274 int rv;
275
276 KASSERT(pin != NULL, ("GPIO pin is NULL."));
277 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
278
279 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
280 return (rv);
281 }
282
283 static void
gpiobus_print_pins(struct gpiobus_ivar * devi,struct sbuf * sb)284 gpiobus_print_pins(struct gpiobus_ivar *devi, struct sbuf *sb)
285 {
286 int i, range_start, range_stop, need_coma;
287
288 if (devi->npins == 0)
289 return;
290
291 need_coma = 0;
292 range_start = range_stop = devi->pins[0];
293 for (i = 1; i < devi->npins; i++) {
294 if (devi->pins[i] != (range_stop + 1)) {
295 if (need_coma)
296 sbuf_cat(sb, ",");
297 if (range_start != range_stop)
298 sbuf_printf(sb, "%d-%d", range_start, range_stop);
299 else
300 sbuf_printf(sb, "%d", range_start);
301 range_start = range_stop = devi->pins[i];
302 need_coma = 1;
303 }
304 else
305 range_stop++;
306 }
307
308 if (need_coma)
309 sbuf_cat(sb, ",");
310 if (range_start != range_stop)
311 sbuf_printf(sb, "%d-%d", range_start, range_stop);
312 else
313 sbuf_printf(sb, "%d", range_start);
314 }
315
316 device_t
gpiobus_add_bus(device_t dev)317 gpiobus_add_bus(device_t dev)
318 {
319 device_t busdev;
320
321 busdev = device_add_child(dev, "gpiobus", DEVICE_UNIT_ANY);
322 if (busdev == NULL)
323 return (NULL);
324 if (device_add_child(dev, "gpioc", DEVICE_UNIT_ANY) == NULL) {
325 device_delete_child(dev, busdev);
326 return (NULL);
327 }
328 #ifdef FDT
329 ofw_gpiobus_register_provider(dev);
330 #endif
331 return (busdev);
332 }
333
334 /*
335 * Attach a gpiobus child.
336 * Note that the controller is expected
337 * to be fully initialized at this point.
338 */
339 device_t
gpiobus_attach_bus(device_t dev)340 gpiobus_attach_bus(device_t dev)
341 {
342 device_t busdev;
343
344 busdev = gpiobus_add_bus(dev);
345 if (busdev == NULL)
346 return (NULL);
347
348 bus_attach_children(dev);
349 return (busdev);
350 }
351
352 int
gpiobus_detach_bus(device_t dev)353 gpiobus_detach_bus(device_t dev)
354 {
355 #ifdef FDT
356 ofw_gpiobus_unregister_provider(dev);
357 #endif
358 return (bus_generic_detach(dev));
359 }
360
361 int
gpiobus_init_softc(device_t dev)362 gpiobus_init_softc(device_t dev)
363 {
364 struct gpiobus_softc *sc;
365
366 sc = GPIOBUS_SOFTC(dev);
367 sc->sc_busdev = dev;
368 sc->sc_dev = device_get_parent(dev);
369 sc->sc_intr_rman.rm_type = RMAN_ARRAY;
370 sc->sc_intr_rman.rm_descr = "GPIO Interrupts";
371 if (rman_init(&sc->sc_intr_rman) != 0 ||
372 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0)
373 panic("%s: failed to set up rman.", __func__);
374
375 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
376 return (ENXIO);
377
378 KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins"));
379
380 /* Pins = GPIO_PIN_MAX() + 1 */
381 sc->sc_npins++;
382
383 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF,
384 M_NOWAIT | M_ZERO);
385 if (sc->sc_pins == NULL)
386 return (ENOMEM);
387
388 /* Initialize the bus lock. */
389 GPIOBUS_LOCK_INIT(sc);
390
391 return (0);
392 }
393
394 int
gpiobus_alloc_ivars(struct gpiobus_ivar * devi)395 gpiobus_alloc_ivars(struct gpiobus_ivar *devi)
396 {
397
398 /* Allocate pins and flags memory. */
399 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
400 M_NOWAIT | M_ZERO);
401 if (devi->pins == NULL)
402 return (ENOMEM);
403 return (0);
404 }
405
406 void
gpiobus_free_ivars(struct gpiobus_ivar * devi)407 gpiobus_free_ivars(struct gpiobus_ivar *devi)
408 {
409
410 if (devi->pins) {
411 free(devi->pins, M_DEVBUF);
412 devi->pins = NULL;
413 }
414 devi->npins = 0;
415 }
416
417 int
gpiobus_acquire_pin(device_t bus,uint32_t pin)418 gpiobus_acquire_pin(device_t bus, uint32_t pin)
419 {
420 struct gpiobus_softc *sc;
421
422 sc = device_get_softc(bus);
423 /* Consistency check. */
424 if (pin >= sc->sc_npins) {
425 panic("%s: invalid pin %d, max: %d",
426 device_get_nameunit(bus), pin, sc->sc_npins - 1);
427 }
428 /* Mark pin as mapped and give warning if it's already mapped. */
429 if (sc->sc_pins[pin].mapped) {
430 device_printf(bus, "warning: pin %d is already mapped\n", pin);
431 return (EBUSY);
432 }
433 sc->sc_pins[pin].mapped = 1;
434
435 return (0);
436 }
437
438 /* Release mapped pin */
439 void
gpiobus_release_pin(device_t bus,uint32_t pin)440 gpiobus_release_pin(device_t bus, uint32_t pin)
441 {
442 struct gpiobus_softc *sc;
443
444 sc = device_get_softc(bus);
445 /* Consistency check. */
446 if (pin >= sc->sc_npins) {
447 panic("%s: invalid pin %d, max: %d",
448 device_get_nameunit(bus), pin, sc->sc_npins - 1);
449 }
450
451 if (!sc->sc_pins[pin].mapped)
452 panic("%s: pin %d is not mapped", device_get_nameunit(bus),
453 pin);
454
455 sc->sc_pins[pin].mapped = 0;
456 }
457
458 static int
gpiobus_acquire_child_pins(device_t dev,device_t child)459 gpiobus_acquire_child_pins(device_t dev, device_t child)
460 {
461 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
462 int i;
463
464 for (i = 0; i < devi->npins; i++) {
465 /* Reserve the GPIO pin. */
466 if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) {
467 device_printf(child, "cannot acquire pin %d\n",
468 devi->pins[i]);
469 while (--i >= 0) {
470 gpiobus_release_pin(dev, devi->pins[i]);
471 }
472 gpiobus_free_ivars(devi);
473 return (EBUSY);
474 }
475 }
476 for (i = 0; i < devi->npins; i++) {
477 /* Use the child name as pin name. */
478 GPIOBUS_PIN_SETNAME(dev, devi->pins[i],
479 device_get_nameunit(child));
480
481 }
482 return (0);
483 }
484
485 static int
gpiobus_parse_pins(struct gpiobus_softc * sc,device_t child,int mask)486 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
487 {
488 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
489 int i, npins;
490
491 npins = 0;
492 for (i = 0; i < 32; i++) {
493 if (mask & (1 << i))
494 npins++;
495 }
496 if (npins == 0) {
497 device_printf(child, "empty pin mask\n");
498 return (EINVAL);
499 }
500 devi->npins = npins;
501 if (gpiobus_alloc_ivars(devi) != 0) {
502 device_printf(child, "cannot allocate device ivars\n");
503 return (EINVAL);
504 }
505 npins = 0;
506 for (i = 0; i < 32; i++) {
507 if ((mask & (1 << i)) == 0)
508 continue;
509 devi->pins[npins++] = i;
510 }
511
512 return (0);
513 }
514
515 static int
gpiobus_parse_pin_list(struct gpiobus_softc * sc,device_t child,const char * pins)516 gpiobus_parse_pin_list(struct gpiobus_softc *sc, device_t child,
517 const char *pins)
518 {
519 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
520 const char *p;
521 char *endp;
522 unsigned long pin;
523 int i, npins;
524
525 npins = 0;
526 p = pins;
527 for (;;) {
528 pin = strtoul(p, &endp, 0);
529 if (endp == p)
530 break;
531 npins++;
532 if (*endp == '\0')
533 break;
534 p = endp + 1;
535 }
536
537 if (*endp != '\0') {
538 device_printf(child, "garbage in the pin list: %s\n", endp);
539 return (EINVAL);
540 }
541 if (npins == 0) {
542 device_printf(child, "empty pin list\n");
543 return (EINVAL);
544 }
545
546 devi->npins = npins;
547 if (gpiobus_alloc_ivars(devi) != 0) {
548 device_printf(child, "cannot allocate device ivars\n");
549 return (EINVAL);
550 }
551
552 i = 0;
553 p = pins;
554 for (;;) {
555 pin = strtoul(p, &endp, 0);
556
557 devi->pins[i] = pin;
558
559 if (*endp == '\0')
560 break;
561 i++;
562 p = endp + 1;
563 }
564
565 return (0);
566 }
567
568 static int
gpiobus_probe(device_t dev)569 gpiobus_probe(device_t dev)
570 {
571 device_set_desc(dev, "GPIO bus");
572
573 return (BUS_PROBE_GENERIC);
574 }
575
576 int
gpiobus_attach(device_t dev)577 gpiobus_attach(device_t dev)
578 {
579 int err;
580
581 err = gpiobus_init_softc(dev);
582 if (err != 0)
583 return (err);
584
585 /*
586 * Get parent's pins and mark them as unmapped
587 */
588 bus_identify_children(dev);
589 bus_enumerate_hinted_children(dev);
590
591 bus_attach_children(dev);
592 return (0);
593 }
594
595 /*
596 * Since this is not a self-enumerating bus, and since we always add
597 * children in attach, we have to always delete children here.
598 */
599 int
gpiobus_detach(device_t dev)600 gpiobus_detach(device_t dev)
601 {
602 struct gpiobus_softc *sc;
603 int i, err;
604
605 sc = GPIOBUS_SOFTC(dev);
606 KASSERT(mtx_initialized(&sc->sc_mtx),
607 ("gpiobus mutex not initialized"));
608 GPIOBUS_LOCK_DESTROY(sc);
609
610 if ((err = bus_detach_children(dev)) != 0)
611 return (err);
612
613 rman_fini(&sc->sc_intr_rman);
614 if (sc->sc_pins) {
615 for (i = 0; i < sc->sc_npins; i++) {
616 if (sc->sc_pins[i].name != NULL)
617 free(sc->sc_pins[i].name, M_DEVBUF);
618 sc->sc_pins[i].name = NULL;
619 }
620 free(sc->sc_pins, M_DEVBUF);
621 sc->sc_pins = NULL;
622 }
623
624 return (0);
625 }
626
627 static int
gpiobus_suspend(device_t dev)628 gpiobus_suspend(device_t dev)
629 {
630
631 return (bus_generic_suspend(dev));
632 }
633
634 static int
gpiobus_resume(device_t dev)635 gpiobus_resume(device_t dev)
636 {
637
638 return (bus_generic_resume(dev));
639 }
640
641 static void
gpiobus_probe_nomatch(device_t dev,device_t child)642 gpiobus_probe_nomatch(device_t dev, device_t child)
643 {
644 char pins[128];
645 struct sbuf sb;
646 struct gpiobus_ivar *devi;
647
648 devi = GPIOBUS_IVAR(child);
649 sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN);
650 gpiobus_print_pins(devi, &sb);
651 sbuf_finish(&sb);
652 device_printf(dev, "<unknown device> at pin%s %s",
653 devi->npins > 1 ? "s" : "", sbuf_data(&sb));
654 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
655 printf("\n");
656 }
657
658 static int
gpiobus_print_child(device_t dev,device_t child)659 gpiobus_print_child(device_t dev, device_t child)
660 {
661 char pins[128];
662 struct sbuf sb;
663 int retval = 0;
664 struct gpiobus_ivar *devi;
665
666 devi = GPIOBUS_IVAR(child);
667 retval += bus_print_child_header(dev, child);
668 if (devi->npins > 0) {
669 if (devi->npins > 1)
670 retval += printf(" at pins ");
671 else
672 retval += printf(" at pin ");
673 sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN);
674 gpiobus_print_pins(devi, &sb);
675 sbuf_finish(&sb);
676 retval += printf("%s", sbuf_data(&sb));
677 }
678 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
679 retval += bus_print_child_footer(dev, child);
680
681 return (retval);
682 }
683
684 static int
gpiobus_child_location(device_t bus,device_t child,struct sbuf * sb)685 gpiobus_child_location(device_t bus, device_t child, struct sbuf *sb)
686 {
687 struct gpiobus_ivar *devi;
688
689 devi = GPIOBUS_IVAR(child);
690 sbuf_printf(sb, "pins=");
691 gpiobus_print_pins(devi, sb);
692
693 return (0);
694 }
695
696 static device_t
gpiobus_add_child(device_t dev,u_int order,const char * name,int unit)697 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
698 {
699 device_t child;
700 struct gpiobus_ivar *devi;
701
702 child = device_add_child_ordered(dev, order, name, unit);
703 if (child == NULL)
704 return (child);
705 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
706 if (devi == NULL) {
707 device_delete_child(dev, child);
708 return (NULL);
709 }
710 resource_list_init(&devi->rl);
711 device_set_ivars(child, devi);
712
713 return (child);
714 }
715
716 static void
gpiobus_child_deleted(device_t dev,device_t child)717 gpiobus_child_deleted(device_t dev, device_t child)
718 {
719 struct gpiobus_ivar *devi;
720
721 devi = GPIOBUS_IVAR(child);
722 if (devi == NULL)
723 return;
724 gpiobus_free_ivars(devi);
725 resource_list_free(&devi->rl);
726 free(devi, M_DEVBUF);
727 }
728
729 static int
gpiobus_rescan(device_t dev)730 gpiobus_rescan(device_t dev)
731 {
732
733 /*
734 * Re-scan is supposed to remove and add children, but if someone has
735 * deleted the hints for a child we attached earlier, we have no easy
736 * way to handle that. So this just attaches new children for whom new
737 * hints or drivers have arrived since we last tried.
738 */
739 bus_enumerate_hinted_children(dev);
740 bus_attach_children(dev);
741 return (0);
742 }
743
744 static void
gpiobus_hinted_child(device_t bus,const char * dname,int dunit)745 gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
746 {
747 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
748 device_t child;
749 const char *pins;
750 int irq, pinmask;
751
752 if (device_find_child(bus, dname, dunit) != NULL) {
753 return;
754 }
755
756 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
757 if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) {
758 if (gpiobus_parse_pins(sc, child, pinmask)) {
759 device_delete_child(bus, child);
760 return;
761 }
762 }
763 else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) {
764 if (gpiobus_parse_pin_list(sc, child, pins)) {
765 device_delete_child(bus, child);
766 return;
767 }
768 }
769 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
770 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
771 device_printf(bus,
772 "warning: bus_set_resource() failed\n");
773 }
774 }
775
776 int
gpiobus_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)777 gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
778 {
779 struct gpiobus_ivar *devi;
780
781 devi = GPIOBUS_IVAR(child);
782 switch (which) {
783 case GPIOBUS_IVAR_NPINS:
784 *result = devi->npins;
785 break;
786 case GPIOBUS_IVAR_PINS:
787 /* Children do not ever need to directly examine this. */
788 return (ENOTSUP);
789 default:
790 return (ENOENT);
791 }
792
793 return (0);
794 }
795
796 static int
gpiobus_write_ivar(device_t dev,device_t child,int which,uintptr_t value)797 gpiobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
798 {
799 struct gpiobus_ivar *devi;
800 const uint32_t *ptr;
801 int i;
802
803 devi = GPIOBUS_IVAR(child);
804 switch (which) {
805 case GPIOBUS_IVAR_NPINS:
806 /* GPIO ivars are set once. */
807 if (devi->npins != 0) {
808 return (EBUSY);
809 }
810 devi->npins = value;
811 if (gpiobus_alloc_ivars(devi) != 0) {
812 device_printf(child, "cannot allocate device ivars\n");
813 devi->npins = 0;
814 return (ENOMEM);
815 }
816 break;
817 case GPIOBUS_IVAR_PINS:
818 ptr = (const uint32_t *)value;
819 for (i = 0; i < devi->npins; i++)
820 devi->pins[i] = ptr[i];
821 if (gpiobus_acquire_child_pins(dev, child) != 0)
822 return (EBUSY);
823 break;
824 default:
825 return (ENOENT);
826 }
827
828 return (0);
829 }
830
831 static struct rman *
gpiobus_get_rman(device_t bus,int type,u_int flags)832 gpiobus_get_rman(device_t bus, int type, u_int flags)
833 {
834 struct gpiobus_softc *sc;
835
836 sc = device_get_softc(bus);
837 switch (type) {
838 case SYS_RES_IRQ:
839 return (&sc->sc_intr_rman);
840 default:
841 return (NULL);
842 }
843 }
844
845 static struct resource *
gpiobus_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)846 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
847 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
848 {
849 struct resource_list *rl;
850 struct resource_list_entry *rle;
851 int isdefault;
852
853 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
854 if (isdefault) {
855 rl = BUS_GET_RESOURCE_LIST(bus, child);
856 if (rl == NULL)
857 return (NULL);
858 rle = resource_list_find(rl, type, *rid);
859 if (rle == NULL)
860 return (NULL);
861 start = rle->start;
862 count = rle->count;
863 end = rle->end;
864 }
865 return (bus_generic_rman_alloc_resource(bus, child, type, rid, start,
866 end, count, flags));
867 }
868
869 static struct resource_list *
gpiobus_get_resource_list(device_t bus __unused,device_t child)870 gpiobus_get_resource_list(device_t bus __unused, device_t child)
871 {
872 struct gpiobus_ivar *ivar;
873
874 ivar = GPIOBUS_IVAR(child);
875
876 return (&ivar->rl);
877 }
878
879 static int
gpiobus_acquire_bus(device_t busdev,device_t child,int how)880 gpiobus_acquire_bus(device_t busdev, device_t child, int how)
881 {
882 struct gpiobus_softc *sc;
883
884 sc = device_get_softc(busdev);
885 GPIOBUS_ASSERT_UNLOCKED(sc);
886 GPIOBUS_LOCK(sc);
887 if (sc->sc_owner != NULL) {
888 if (sc->sc_owner == child)
889 panic("%s: %s still owns the bus.",
890 device_get_nameunit(busdev),
891 device_get_nameunit(child));
892 if (how == GPIOBUS_DONTWAIT) {
893 GPIOBUS_UNLOCK(sc);
894 return (EWOULDBLOCK);
895 }
896 while (sc->sc_owner != NULL)
897 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0);
898 }
899 sc->sc_owner = child;
900 GPIOBUS_UNLOCK(sc);
901
902 return (0);
903 }
904
905 static void
gpiobus_release_bus(device_t busdev,device_t child)906 gpiobus_release_bus(device_t busdev, device_t child)
907 {
908 struct gpiobus_softc *sc;
909
910 sc = device_get_softc(busdev);
911 GPIOBUS_ASSERT_UNLOCKED(sc);
912 GPIOBUS_LOCK(sc);
913 if (sc->sc_owner == NULL)
914 panic("%s: %s releasing unowned bus.",
915 device_get_nameunit(busdev),
916 device_get_nameunit(child));
917 if (sc->sc_owner != child)
918 panic("%s: %s trying to release bus owned by %s",
919 device_get_nameunit(busdev),
920 device_get_nameunit(child),
921 device_get_nameunit(sc->sc_owner));
922 sc->sc_owner = NULL;
923 wakeup(sc);
924 GPIOBUS_UNLOCK(sc);
925 }
926
927 static int
gpiobus_pin_setflags(device_t dev,device_t child,uint32_t pin,uint32_t flags)928 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
929 uint32_t flags)
930 {
931 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
932 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
933 uint32_t caps;
934
935 if (pin >= devi->npins)
936 return (EINVAL);
937 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0)
938 return (EINVAL);
939 if (gpio_check_flags(caps, flags) != 0)
940 return (EINVAL);
941
942 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags));
943 }
944
945 static int
gpiobus_pin_getflags(device_t dev,device_t child,uint32_t pin,uint32_t * flags)946 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
947 uint32_t *flags)
948 {
949 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
950 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
951
952 if (pin >= devi->npins)
953 return (EINVAL);
954
955 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
956 }
957
958 static int
gpiobus_pin_getcaps(device_t dev,device_t child,uint32_t pin,uint32_t * caps)959 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
960 uint32_t *caps)
961 {
962 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
963 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
964
965 if (pin >= devi->npins)
966 return (EINVAL);
967
968 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
969 }
970
971 static int
gpiobus_pin_set(device_t dev,device_t child,uint32_t pin,unsigned int value)972 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
973 unsigned int value)
974 {
975 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
976 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
977
978 if (pin >= devi->npins)
979 return (EINVAL);
980
981 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
982 }
983
984 static int
gpiobus_pin_get(device_t dev,device_t child,uint32_t pin,unsigned int * value)985 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
986 unsigned int *value)
987 {
988 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
989 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
990
991 if (pin >= devi->npins)
992 return (EINVAL);
993
994 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
995 }
996
997 static int
gpiobus_pin_toggle(device_t dev,device_t child,uint32_t pin)998 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
999 {
1000 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
1001 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
1002
1003 if (pin >= devi->npins)
1004 return (EINVAL);
1005
1006 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
1007 }
1008
1009 static int
gpiobus_pin_getname(device_t dev,uint32_t pin,char * name)1010 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name)
1011 {
1012 struct gpiobus_softc *sc;
1013
1014 sc = GPIOBUS_SOFTC(dev);
1015 if (pin > sc->sc_npins)
1016 return (EINVAL);
1017 /* Did we have a name for this pin ? */
1018 if (sc->sc_pins[pin].name != NULL) {
1019 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME);
1020 return (0);
1021 }
1022
1023 /* Return the default pin name. */
1024 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name));
1025 }
1026
1027 static int
gpiobus_pin_setname(device_t dev,uint32_t pin,const char * name)1028 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name)
1029 {
1030 struct gpiobus_softc *sc;
1031
1032 sc = GPIOBUS_SOFTC(dev);
1033 if (pin > sc->sc_npins)
1034 return (EINVAL);
1035 if (name == NULL)
1036 return (EINVAL);
1037 /* Save the pin name. */
1038 if (sc->sc_pins[pin].name == NULL)
1039 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF,
1040 M_WAITOK | M_ZERO);
1041 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME);
1042
1043 return (0);
1044 }
1045
1046 static device_method_t gpiobus_methods[] = {
1047 /* Device interface */
1048 DEVMETHOD(device_probe, gpiobus_probe),
1049 DEVMETHOD(device_attach, gpiobus_attach),
1050 DEVMETHOD(device_detach, gpiobus_detach),
1051 DEVMETHOD(device_shutdown, bus_generic_shutdown),
1052 DEVMETHOD(device_suspend, gpiobus_suspend),
1053 DEVMETHOD(device_resume, gpiobus_resume),
1054
1055 /* Bus interface */
1056 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
1057 DEVMETHOD(bus_config_intr, bus_generic_config_intr),
1058 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
1059 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
1060 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
1061 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
1062 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource),
1063 DEVMETHOD(bus_release_resource, bus_generic_rman_release_resource),
1064 DEVMETHOD(bus_activate_resource, bus_generic_rman_activate_resource),
1065 DEVMETHOD(bus_deactivate_resource, bus_generic_rman_deactivate_resource),
1066 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list),
1067 DEVMETHOD(bus_get_rman, gpiobus_get_rman),
1068 DEVMETHOD(bus_add_child, gpiobus_add_child),
1069 DEVMETHOD(bus_child_deleted, gpiobus_child_deleted),
1070 DEVMETHOD(bus_rescan, gpiobus_rescan),
1071 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch),
1072 DEVMETHOD(bus_print_child, gpiobus_print_child),
1073 DEVMETHOD(bus_child_location, gpiobus_child_location),
1074 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child),
1075 DEVMETHOD(bus_read_ivar, gpiobus_read_ivar),
1076 DEVMETHOD(bus_write_ivar, gpiobus_write_ivar),
1077
1078 /* GPIO protocol */
1079 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus),
1080 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus),
1081 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags),
1082 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps),
1083 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags),
1084 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get),
1085 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set),
1086 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle),
1087 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname),
1088 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname),
1089
1090 DEVMETHOD_END
1091 };
1092
1093 driver_t gpiobus_driver = {
1094 "gpiobus",
1095 gpiobus_methods,
1096 sizeof(struct gpiobus_softc)
1097 };
1098
1099 EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, 0, 0,
1100 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
1101 MODULE_VERSION(gpiobus, 1);
1102