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