Home | History | Annotate | Line # | Download | only in acpi
plgpio_acpi.c revision 1.7
      1  1.7   thorpej /* $NetBSD: plgpio_acpi.c,v 1.7 2021/01/29 15:49:55 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmcneill  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  1.1  jmcneill  *
     10  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
     11  1.1  jmcneill  * modification, are permitted provided that the following conditions
     12  1.1  jmcneill  * are met:
     13  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     18  1.1  jmcneill  *
     19  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/cdefs.h>
     33  1.7   thorpej __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.7 2021/01/29 15:49:55 thorpej Exp $");
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <sys/param.h>
     36  1.1  jmcneill #include <sys/bus.h>
     37  1.1  jmcneill #include <sys/cpu.h>
     38  1.1  jmcneill #include <sys/device.h>
     39  1.1  jmcneill #include <sys/gpio.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/acpi/acpireg.h>
     42  1.1  jmcneill #include <dev/acpi/acpivar.h>
     43  1.5  jmcneill #include <dev/acpi/acpi_intr.h>
     44  1.3  jmcneill #include <dev/acpi/acpi_event.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <dev/gpio/gpiovar.h>
     47  1.1  jmcneill #include <dev/ic/pl061var.h>
     48  1.2  jmcneill #include <dev/ic/pl061reg.h>
     49  1.2  jmcneill 
     50  1.2  jmcneill struct plgpio_acpi_softc;
     51  1.2  jmcneill 
     52  1.2  jmcneill struct plgpio_acpi_softc {
     53  1.2  jmcneill 	struct plgpio_softc	sc_base;
     54  1.2  jmcneill 
     55  1.2  jmcneill 	ACPI_HANDLE		sc_handle;
     56  1.2  jmcneill 
     57  1.3  jmcneill 	struct acpi_event *	sc_event[8];
     58  1.2  jmcneill };
     59  1.1  jmcneill 
     60  1.1  jmcneill static int	plgpio_acpi_match(device_t, cfdata_t, void *);
     61  1.1  jmcneill static void	plgpio_acpi_attach(device_t, device_t, void *);
     62  1.1  jmcneill 
     63  1.3  jmcneill static void	plgpio_acpi_register_event(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
     64  1.2  jmcneill static int	plgpio_acpi_intr(void *);
     65  1.2  jmcneill 
     66  1.2  jmcneill CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
     67  1.1  jmcneill 
     68  1.7   thorpej static const struct device_compatible_entry compat_data[] = {
     69  1.7   thorpej 	{ .compat = "ARMH0061" },
     70  1.7   thorpej 	DEVICE_COMPAT_EOL
     71  1.1  jmcneill };
     72  1.1  jmcneill 
     73  1.1  jmcneill static int
     74  1.1  jmcneill plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
     75  1.1  jmcneill {
     76  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     77  1.1  jmcneill 
     78  1.7   thorpej 	return acpi_compatible_match(aa, compat_data);
     79  1.1  jmcneill }
     80  1.1  jmcneill 
     81  1.1  jmcneill static void
     82  1.1  jmcneill plgpio_acpi_attach(device_t parent, device_t self, void *aux)
     83  1.1  jmcneill {
     84  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = device_private(self);
     85  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
     86  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     87  1.1  jmcneill 	struct acpi_resources res;
     88  1.1  jmcneill 	struct acpi_mem *mem;
     89  1.2  jmcneill 	struct acpi_irq *irq;
     90  1.1  jmcneill 	ACPI_STATUS rv;
     91  1.1  jmcneill 	int error;
     92  1.2  jmcneill 	void *ih;
     93  1.1  jmcneill 
     94  1.1  jmcneill 	sc->sc_dev = self;
     95  1.2  jmcneill 	asc->sc_handle = aa->aa_node->ad_handle;
     96  1.1  jmcneill 
     97  1.1  jmcneill 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
     98  1.1  jmcneill 	    &res, &acpi_resource_parse_ops_default);
     99  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    100  1.1  jmcneill 		return;
    101  1.1  jmcneill 
    102  1.1  jmcneill 	mem = acpi_res_mem(&res, 0);
    103  1.1  jmcneill 	if (mem == NULL) {
    104  1.1  jmcneill 		aprint_error_dev(self, "couldn't find mem resource\n");
    105  1.1  jmcneill 		goto done;
    106  1.1  jmcneill 	}
    107  1.1  jmcneill 
    108  1.2  jmcneill 	irq = acpi_res_irq(&res, 0);
    109  1.5  jmcneill 	if (irq == NULL) {
    110  1.2  jmcneill 		aprint_error_dev(self, "couldn't find irq resource\n");
    111  1.2  jmcneill 		goto done;
    112  1.2  jmcneill 	}
    113  1.2  jmcneill 
    114  1.1  jmcneill 	sc->sc_dev = self;
    115  1.1  jmcneill 	sc->sc_bst = aa->aa_memt;
    116  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
    117  1.1  jmcneill 	if (error) {
    118  1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    119  1.1  jmcneill 		return;
    120  1.1  jmcneill 	}
    121  1.1  jmcneill 
    122  1.1  jmcneill 	plgpio_attach(sc);
    123  1.1  jmcneill 
    124  1.4  jmcneill 	rv = acpi_event_create_gpio(self, asc->sc_handle, plgpio_acpi_register_event, asc);
    125  1.4  jmcneill 	if (ACPI_FAILURE(rv)) {
    126  1.4  jmcneill 		if (rv != AE_NOT_FOUND)
    127  1.4  jmcneill 			aprint_error_dev(self, "failed to create events: %s\n", AcpiFormatException(rv));
    128  1.3  jmcneill 		goto done;
    129  1.3  jmcneill 	}
    130  1.3  jmcneill 
    131  1.6  jmcneill 	ih = acpi_intr_establish(self,
    132  1.6  jmcneill 	    (uint64_t)(uintptr_t)asc->sc_handle,
    133  1.5  jmcneill 	    IPL_VM, false, plgpio_acpi_intr, asc, device_xname(self));
    134  1.2  jmcneill 	if (ih == NULL)
    135  1.2  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt\n");
    136  1.2  jmcneill 
    137  1.1  jmcneill done:
    138  1.1  jmcneill 	acpi_resource_cleanup(&res);
    139  1.1  jmcneill }
    140  1.2  jmcneill 
    141  1.2  jmcneill static void
    142  1.3  jmcneill plgpio_acpi_register_event(void *priv, struct acpi_event *ev, ACPI_RESOURCE_GPIO *gpio)
    143  1.2  jmcneill {
    144  1.3  jmcneill 	struct plgpio_acpi_softc * const asc = priv;
    145  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    146  1.2  jmcneill 	uint32_t ibe, iev, is, ie;
    147  1.2  jmcneill 
    148  1.3  jmcneill 	const int pin = gpio->PinTable[0];
    149  1.3  jmcneill 
    150  1.3  jmcneill 	if (pin >= __arraycount(asc->sc_event)) {
    151  1.3  jmcneill 		aprint_error_dev(asc->sc_base.sc_dev,
    152  1.3  jmcneill 		    "ignoring event for pin %u (out of range)\n", pin);
    153  1.2  jmcneill 		return;
    154  1.2  jmcneill 	}
    155  1.3  jmcneill 	if (asc->sc_event[pin] != NULL) {
    156  1.3  jmcneill 		aprint_error_dev(asc->sc_base.sc_dev,
    157  1.3  jmcneill 		    "ignoring duplicate pin %u\n", pin);
    158  1.2  jmcneill 		return;
    159  1.3  jmcneill 	}
    160  1.2  jmcneill 
    161  1.3  jmcneill 	asc->sc_event[pin] = ev;
    162  1.3  jmcneill 	asc->sc_base.sc_reserved_mask |= __BIT(pin);
    163  1.2  jmcneill 
    164  1.2  jmcneill 	/*
    165  1.3  jmcneill 	 * Configure and enable interrupts for this pin.
    166  1.2  jmcneill 	 */
    167  1.2  jmcneill 
    168  1.3  jmcneill 	ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG);
    169  1.3  jmcneill 	iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG);
    170  1.3  jmcneill 	switch (gpio->Polarity) {
    171  1.3  jmcneill 	case ACPI_ACTIVE_HIGH:
    172  1.3  jmcneill 		ibe &= ~__BIT(pin);
    173  1.3  jmcneill 		iev |= __BIT(pin);
    174  1.3  jmcneill 		break;
    175  1.3  jmcneill 	case ACPI_ACTIVE_LOW:
    176  1.3  jmcneill 		ibe &= ~__BIT(pin);
    177  1.3  jmcneill 		iev &= ~__BIT(pin);
    178  1.3  jmcneill 		break;
    179  1.3  jmcneill 	case ACPI_ACTIVE_BOTH:
    180  1.3  jmcneill 		ibe |= __BIT(pin);
    181  1.3  jmcneill 		break;
    182  1.3  jmcneill 	}
    183  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe);
    184  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev);
    185  1.3  jmcneill 
    186  1.3  jmcneill 	is = PLGPIO_READ(sc, PL061_GPIOIS_REG);
    187  1.3  jmcneill 	switch (gpio->Triggering) {
    188  1.3  jmcneill 	case ACPI_LEVEL_SENSITIVE:
    189  1.3  jmcneill 		is |= __BIT(pin);
    190  1.3  jmcneill 		break;
    191  1.3  jmcneill 	case ACPI_EDGE_SENSITIVE:
    192  1.3  jmcneill 		is &= ~__BIT(pin);
    193  1.3  jmcneill 		break;
    194  1.3  jmcneill 	}
    195  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is);
    196  1.3  jmcneill 
    197  1.3  jmcneill 	delay(20);
    198  1.3  jmcneill 
    199  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin));
    200  1.3  jmcneill 
    201  1.3  jmcneill 	ie = PLGPIO_READ(sc, PL061_GPIOIE_REG);
    202  1.3  jmcneill 	ie |= __BIT(pin);
    203  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie);
    204  1.2  jmcneill }
    205  1.2  jmcneill 
    206  1.2  jmcneill static int
    207  1.2  jmcneill plgpio_acpi_intr(void *priv)
    208  1.2  jmcneill {
    209  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = priv;
    210  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    211  1.2  jmcneill 	uint32_t mis;
    212  1.2  jmcneill 	int bit;
    213  1.2  jmcneill 
    214  1.2  jmcneill 	mis = PLGPIO_READ(sc, PL061_GPIOMIS_REG);
    215  1.2  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIC_REG, mis);
    216  1.2  jmcneill 
    217  1.2  jmcneill 	while ((bit = __builtin_ffs(mis)) != 0) {
    218  1.2  jmcneill 		const int pin = bit - 1;
    219  1.3  jmcneill 		struct acpi_event * const ev = asc->sc_event[pin];
    220  1.3  jmcneill 		KASSERT(ev != NULL);
    221  1.2  jmcneill 
    222  1.3  jmcneill 		acpi_event_notify(ev);
    223  1.2  jmcneill 
    224  1.2  jmcneill 		mis &= ~__BIT(pin);
    225  1.2  jmcneill 	}
    226  1.2  jmcneill 
    227  1.2  jmcneill 	return 1;
    228  1.2  jmcneill }
    229