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