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