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