2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Amazon.com, Inc. or its affiliates.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
40 #include <sys/mutex.h>
42 #include <sys/interrupt.h>
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 #include <machine/resource.h>
48 #include <dev/gpio/gpiobusvar.h>
55 #define PL061_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
56 #define PL061_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
57 #define PL061_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
58 #define PL061_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
59 #define PL061_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
62 #define dprintf(fmt, args...) do { \
63 printf(fmt, ##args); \
66 #define dprintf(fmt, args...)
69 #define PL061_PIN_TO_ADDR(pin) (1 << (pin + 2))
70 #define PL061_DATA 0x3FC
71 #define PL061_DIR 0x400
72 #define PL061_INTSENSE 0x404
73 #define PL061_INTBOTHEDGES 0x408
74 #define PL061_INTEVENT 0x40C
75 #define PL061_INTMASK 0x410
76 #define PL061_RAWSTATUS 0x414
77 #define PL061_STATUS 0x418
78 #define PL061_INTCLR 0x41C
79 #define PL061_MODECTRL 0x420
81 #define PL061_ALLOWED_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_INTR_EDGE_BOTH | \
82 GPIO_INTR_EDGE_RISING | GPIO_INTR_EDGE_FALLING | \
83 GPIO_INTR_LEVEL_HIGH | GPIO_INTR_LEVEL_LOW )
85 #define PIC_INTR_ISRC(sc, irq) (&(sc->sc_isrcs[irq].isrc))
88 pl061_get_bus(device_t dev)
90 struct pl061_softc *sc;
92 sc = device_get_softc(dev);
93 return (sc->sc_busdev);
97 pl061_pin_max(device_t dev, int *maxpin)
99 *maxpin = PL061_NUM_GPIO - 1;
104 pl061_pin_getname(device_t dev, uint32_t pin, char *name)
106 struct pl061_softc *sc;
108 sc = device_get_softc(dev);
109 if (pin >= PL061_NUM_GPIO)
112 snprintf(name, GPIOMAXNAME, "p%u", pin);
113 name[GPIOMAXNAME - 1] = '\0';
119 pl061_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
121 struct pl061_softc *sc;
122 uint8_t mask = 1 << pin;
124 sc = device_get_softc(dev);
125 if (pin >= PL061_NUM_GPIO)
131 if (mask & bus_read_1(sc->sc_mem_res, PL061_DIR))
132 *flags |= GPIO_PIN_OUTPUT;
134 *flags |= GPIO_PIN_INPUT;
141 pl061_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
143 struct pl061_softc *sc;
145 sc = device_get_softc(dev);
146 if (pin >= PL061_NUM_GPIO)
149 *caps = PL061_ALLOWED_CAPS;
155 mask_and_set(struct pl061_softc *sc, long a, uint8_t m, uint8_t b)
159 tmp = bus_read_1(sc->sc_mem_res, a);
162 bus_write_1(sc->sc_mem_res, a, tmp);
163 dprintf("%s: writing %#x to register %#lx\n", __func__, tmp, a);
167 pl061_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
169 struct pl061_softc *sc;
170 uint8_t mask = 1 << pin;
171 const uint32_t in_out = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
173 sc = device_get_softc(dev);
174 if (pin >= PL061_NUM_GPIO)
177 if (flags & ~PL061_ALLOWED_CAPS)
180 /* can't be both input and output */
181 if ((flags & in_out) == in_out)
186 mask_and_set(sc, PL061_DIR, mask, flags & GPIO_PIN_OUTPUT ? mask : 0);
192 pl061_pin_get(device_t dev, uint32_t pin, uint32_t *value)
194 struct pl061_softc *sc;
196 sc = device_get_softc(dev);
197 if (pin >= PL061_NUM_GPIO)
201 if (bus_read_1(sc->sc_mem_res, PL061_PIN_TO_ADDR(pin)))
202 *value = GPIO_PIN_HIGH;
204 *value = GPIO_PIN_LOW;
211 pl061_pin_set(device_t dev, uint32_t pin, uint32_t value)
213 struct pl061_softc *sc;
214 uint8_t d = (value == GPIO_PIN_HIGH) ? 0xff : 0x00;
216 sc = device_get_softc(dev);
217 if (pin >= PL061_NUM_GPIO)
221 bus_write_1(sc->sc_mem_res, PL061_PIN_TO_ADDR(pin), d);
228 pl061_pin_toggle(device_t dev, uint32_t pin)
230 struct pl061_softc *sc;
233 sc = device_get_softc(dev);
234 if (pin >= PL061_NUM_GPIO)
238 d = ~bus_read_1(sc->sc_mem_res, PL061_PIN_TO_ADDR(pin));
239 bus_write_1(sc->sc_mem_res, PL061_PIN_TO_ADDR(pin), d);
246 pl061_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
248 struct pl061_softc *sc;
251 sc = device_get_softc(dev);
252 mask = 1 << ((struct pl061_pin_irqsrc *)isrc)->irq;
254 dprintf("%s: calling disable interrupt %#x\n", __func__, mask);
256 mask_and_set(sc, PL061_INTMASK, mask, 0);
263 pl061_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
265 struct pl061_softc *sc;
268 sc = device_get_softc(dev);
269 mask = 1 << ((struct pl061_pin_irqsrc *)isrc)->irq;
272 dprintf("%s: calling enable interrupt %#x\n", __func__, mask);
274 mask_and_set(sc, PL061_INTMASK, mask, mask);
279 pl061_pic_map_intr(device_t dev, struct intr_map_data *data,
280 struct intr_irqsrc **isrcp)
282 struct pl061_softc *sc;
283 struct intr_map_data_gpio *gdata;
286 sc = device_get_softc(dev);
287 if (data->type != INTR_MAP_DATA_GPIO)
290 gdata = (struct intr_map_data_gpio *)data;
291 irq = gdata->gpio_pin_num;
292 if (irq >= PL061_NUM_GPIO) {
293 device_printf(dev, "invalid interrupt number %u\n", irq);
297 dprintf("%s: calling map interrupt %u\n", __func__, irq);
298 *isrcp = PIC_INTR_ISRC(sc, irq);
304 pl061_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
305 struct resource *res, struct intr_map_data *data)
307 struct pl061_softc *sc;
308 struct intr_map_data_gpio *gdata;
309 struct pl061_pin_irqsrc *irqsrc;
316 sc = device_get_softc(dev);
317 gdata = (struct intr_map_data_gpio *)data;
318 irqsrc = (struct pl061_pin_irqsrc *)isrc;
320 mode = gdata->gpio_intr_mode;
321 mask = 1 << gdata->gpio_pin_num;
323 dprintf("%s: calling setup interrupt %u mode %#x\n", __func__,
325 if (irqsrc->irq != gdata->gpio_pin_num) {
326 dprintf("%s: interrupts don't match\n", __func__);
330 if (isrc->isrc_handlers != 0) {
331 dprintf("%s: handler already attached\n", __func__);
332 return (irqsrc->mode == mode ? 0 : EINVAL);
338 if (mode & GPIO_INTR_EDGE_BOTH) {
339 mask_and_set(sc, PL061_INTBOTHEDGES, mask, mask);
340 mask_and_set(sc, PL061_INTSENSE, mask, 0);
341 } else if (mode & GPIO_INTR_EDGE_RISING) {
342 mask_and_set(sc, PL061_INTBOTHEDGES, mask, 0);
343 mask_and_set(sc, PL061_INTSENSE, mask, 0);
344 mask_and_set(sc, PL061_INTEVENT, mask, mask);
345 } else if (mode & GPIO_INTR_EDGE_FALLING) {
346 mask_and_set(sc, PL061_INTBOTHEDGES, mask, 0);
347 mask_and_set(sc, PL061_INTSENSE, mask, 0);
348 mask_and_set(sc, PL061_INTEVENT, mask, 0);
349 } else if (mode & GPIO_INTR_LEVEL_HIGH) {
350 mask_and_set(sc, PL061_INTBOTHEDGES, mask, 0);
351 mask_and_set(sc, PL061_INTSENSE, mask, mask);
352 mask_and_set(sc, PL061_INTEVENT, mask, mask);
353 } else if (mode & GPIO_INTR_LEVEL_LOW) {
354 mask_and_set(sc, PL061_INTBOTHEDGES, mask, 0);
355 mask_and_set(sc, PL061_INTSENSE, mask, mask);
356 mask_and_set(sc, PL061_INTEVENT, mask, 0);
363 pl061_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
364 struct resource *res, struct intr_map_data *data)
366 struct pl061_softc *sc;
367 struct pl061_pin_irqsrc *irqsrc;
370 irqsrc = (struct pl061_pin_irqsrc *)isrc;
371 mask = 1 << irqsrc->irq;
372 dprintf("%s: calling teardown interrupt %#x\n", __func__, mask);
374 sc = device_get_softc(dev);
375 if (isrc->isrc_handlers == 0) {
376 irqsrc->mode = GPIO_INTR_CONFORM;
378 mask_and_set(sc, PL061_INTMASK, mask, 0);
385 pl061_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
387 struct pl061_softc *sc;
390 sc = device_get_softc(dev);
391 mask = 1 << ((struct pl061_pin_irqsrc *)isrc)->irq;
392 dprintf("%s: calling post filter %#x\n", __func__, mask);
394 bus_write_1(sc->sc_mem_res, PL061_INTCLR, mask);
398 pl061_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
400 struct pl061_softc *sc;
403 sc = device_get_softc(dev);
404 mask = 1 << ((struct pl061_pin_irqsrc *)isrc)->irq;
405 dprintf("%s: calling post ithread %#x\n", __func__, mask);
406 bus_write_1(sc->sc_mem_res, PL061_INTCLR, mask);
408 pl061_pic_enable_intr(dev, isrc);
412 pl061_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
414 pl061_pic_disable_intr(dev, isrc);
418 pl061_intr(void *arg)
420 struct pl061_softc *sc;
421 struct trapframe *tf;
425 sc = (struct pl061_softc *)arg;
426 tf = curthread->td_intr_frame;
428 status = bus_read_1(sc->sc_mem_res, PL061_STATUS);
430 while (status != 0) {
431 pin = ffs(status) - 1;
432 status &= ~(1 << pin);
434 if (intr_isrc_dispatch(PIC_INTR_ISRC(sc, pin), tf) != 0)
435 device_printf(sc->sc_dev, "spurious interrupt %d\n",
438 dprintf("got IRQ on %d\n", pin);
441 return (FILTER_HANDLED);
445 pl061_attach(device_t dev)
447 struct pl061_softc *sc;
452 sc = device_get_softc(dev);
456 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
457 &sc->sc_mem_rid, RF_ACTIVE);
458 if (sc->sc_mem_res == NULL) {
459 device_printf(dev, "can't allocate memory resource\n");
464 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
465 &sc->sc_irq_rid, RF_ACTIVE);
467 if (sc->sc_irq_res == NULL) {
468 device_printf(dev, "can't allocate IRQ resource\n");
472 ret = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
473 pl061_intr, NULL, sc, &sc->sc_irq_hdlr);
475 device_printf(dev, "can't setup IRQ\n");
479 name = device_get_nameunit(dev);
481 for (irq = 0; irq < PL061_NUM_GPIO; irq++) {
484 "trying to register pin %d name %s\n", irq, name);
486 sc->sc_isrcs[irq].irq = irq;
487 sc->sc_isrcs[irq].mode = GPIO_INTR_CONFORM;
488 ret = intr_isrc_register(PIC_INTR_ISRC(sc, irq), dev, 0,
491 device_printf(dev, "can't register isrc %d\n", ret);
496 sc->sc_busdev = gpiobus_attach_bus(dev);
497 if (sc->sc_busdev == NULL) {
498 device_printf(dev, "couldn't attach gpio bus\n");
502 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), "pl061", MTX_SPIN);
508 * XXX isrc_release_counters() not implemented
509 * for (irq = 0; irq < PL061_NUM_GPIO; irq++)
510 * intr_isrc_deregister(PIC_INTR_ISRC(sc, irq));
512 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
516 * XXX intr_pic_deregister: not implemented
517 * intr_pic_deregister(dev, 0);
521 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid,
529 pl061_detach(device_t dev)
531 struct pl061_softc *sc;
532 sc = device_get_softc(dev);
535 gpiobus_detach_bus(dev);
537 if (sc->sc_irq_hdlr != NULL)
538 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_hdlr);
540 if (sc->sc_irq_res != NULL)
541 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
544 if (sc->sc_mem_res != NULL)
545 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid,
547 PL061_LOCK_DESTROY(sc);
551 static device_method_t pl061_methods[] = {
552 /* Device interface */
553 DEVMETHOD(device_attach, pl061_attach),
554 DEVMETHOD(device_detach, pl061_detach),
557 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
558 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
559 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
562 DEVMETHOD(gpio_get_bus, pl061_get_bus),
563 DEVMETHOD(gpio_pin_max, pl061_pin_max),
564 DEVMETHOD(gpio_pin_getname, pl061_pin_getname),
565 DEVMETHOD(gpio_pin_getflags, pl061_pin_getflags),
566 DEVMETHOD(gpio_pin_getcaps, pl061_pin_getcaps),
567 DEVMETHOD(gpio_pin_setflags, pl061_pin_setflags),
568 DEVMETHOD(gpio_pin_get, pl061_pin_get),
569 DEVMETHOD(gpio_pin_set, pl061_pin_set),
570 DEVMETHOD(gpio_pin_toggle, pl061_pin_toggle),
572 /* Interrupt controller interface */
573 DEVMETHOD(pic_disable_intr, pl061_pic_disable_intr),
574 DEVMETHOD(pic_enable_intr, pl061_pic_enable_intr),
575 DEVMETHOD(pic_map_intr, pl061_pic_map_intr),
576 DEVMETHOD(pic_setup_intr, pl061_pic_setup_intr),
577 DEVMETHOD(pic_teardown_intr, pl061_pic_teardown_intr),
578 DEVMETHOD(pic_post_filter, pl061_pic_post_filter),
579 DEVMETHOD(pic_post_ithread, pl061_pic_post_ithread),
580 DEVMETHOD(pic_pre_ithread, pl061_pic_pre_ithread),
585 DEFINE_CLASS_0(gpio, pl061_driver, pl061_methods, sizeof(struct pl061_softc));