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