Home | History | Annotate | Line # | Download | only in acpi
plgpio_acpi.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: plgpio_acpi.c,v 1.2 2018/10/21 18:31:58 jmcneill 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.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.2 2018/10/21 18:31:58 jmcneill 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.1  jmcneill 
     44  1.1  jmcneill #include <dev/gpio/gpiovar.h>
     45  1.1  jmcneill #include <dev/ic/pl061var.h>
     46  1.2  jmcneill #include <dev/ic/pl061reg.h>
     47  1.2  jmcneill 
     48  1.2  jmcneill struct plgpio_acpi_softc;
     49  1.2  jmcneill 
     50  1.2  jmcneill struct plgpio_acpi_event {
     51  1.2  jmcneill 	struct plgpio_acpi_softc *ev_sc;
     52  1.2  jmcneill 	u_int			ev_pin;
     53  1.2  jmcneill 	ACPI_HANDLE		ev_method;
     54  1.2  jmcneill 	bool			ev_method_evt;
     55  1.2  jmcneill };
     56  1.2  jmcneill 
     57  1.2  jmcneill struct plgpio_acpi_softc {
     58  1.2  jmcneill 	struct plgpio_softc	sc_base;
     59  1.2  jmcneill 
     60  1.2  jmcneill 	ACPI_HANDLE		sc_handle;
     61  1.2  jmcneill 	uint32_t		sc_aei_pins;		/* bitmask */
     62  1.2  jmcneill 	ACPI_RESOURCE_GPIO	sc_aei_res[8];
     63  1.2  jmcneill 
     64  1.2  jmcneill 	struct plgpio_acpi_event sc_event[8];
     65  1.2  jmcneill };
     66  1.1  jmcneill 
     67  1.1  jmcneill static int	plgpio_acpi_match(device_t, cfdata_t, void *);
     68  1.1  jmcneill static void	plgpio_acpi_attach(device_t, device_t, void *);
     69  1.1  jmcneill 
     70  1.2  jmcneill static void	plgpio_acpi_init(struct plgpio_acpi_softc *);
     71  1.2  jmcneill static void	plgpio_acpi_notify(void *);
     72  1.2  jmcneill static int	plgpio_acpi_intr(void *);
     73  1.2  jmcneill 
     74  1.2  jmcneill CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
     75  1.1  jmcneill 
     76  1.1  jmcneill static const char * const compatible[] = {
     77  1.1  jmcneill 	"ARMH0061",
     78  1.1  jmcneill 	NULL
     79  1.1  jmcneill };
     80  1.1  jmcneill 
     81  1.1  jmcneill static int
     82  1.1  jmcneill plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
     83  1.1  jmcneill {
     84  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     85  1.1  jmcneill 
     86  1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     87  1.1  jmcneill 		return 0;
     88  1.1  jmcneill 
     89  1.1  jmcneill 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
     90  1.1  jmcneill }
     91  1.1  jmcneill 
     92  1.1  jmcneill static void
     93  1.1  jmcneill plgpio_acpi_attach(device_t parent, device_t self, void *aux)
     94  1.1  jmcneill {
     95  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = device_private(self);
     96  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
     97  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     98  1.1  jmcneill 	struct acpi_resources res;
     99  1.1  jmcneill 	struct acpi_mem *mem;
    100  1.2  jmcneill 	struct acpi_irq *irq;
    101  1.1  jmcneill 	ACPI_STATUS rv;
    102  1.1  jmcneill 	int error;
    103  1.2  jmcneill 	void *ih;
    104  1.1  jmcneill 
    105  1.1  jmcneill 	sc->sc_dev = self;
    106  1.2  jmcneill 	asc->sc_handle = aa->aa_node->ad_handle;
    107  1.1  jmcneill 
    108  1.1  jmcneill 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    109  1.1  jmcneill 	    &res, &acpi_resource_parse_ops_default);
    110  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    111  1.1  jmcneill 		return;
    112  1.1  jmcneill 
    113  1.1  jmcneill 	mem = acpi_res_mem(&res, 0);
    114  1.1  jmcneill 	if (mem == NULL) {
    115  1.1  jmcneill 		aprint_error_dev(self, "couldn't find mem resource\n");
    116  1.1  jmcneill 		goto done;
    117  1.1  jmcneill 	}
    118  1.1  jmcneill 
    119  1.2  jmcneill 	irq = acpi_res_irq(&res, 0);
    120  1.2  jmcneill 	if (mem == NULL) {
    121  1.2  jmcneill 		aprint_error_dev(self, "couldn't find irq resource\n");
    122  1.2  jmcneill 		goto done;
    123  1.2  jmcneill 	}
    124  1.2  jmcneill 
    125  1.1  jmcneill 	sc->sc_dev = self;
    126  1.1  jmcneill 	sc->sc_bst = aa->aa_memt;
    127  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
    128  1.1  jmcneill 	if (error) {
    129  1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    130  1.1  jmcneill 		return;
    131  1.1  jmcneill 	}
    132  1.1  jmcneill 
    133  1.1  jmcneill 	plgpio_attach(sc);
    134  1.1  jmcneill 
    135  1.2  jmcneill 	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    136  1.2  jmcneill 	ih = intr_establish(irq->ar_irq, IPL_VM, type, plgpio_acpi_intr, asc);
    137  1.2  jmcneill 	if (ih == NULL)
    138  1.2  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt\n");
    139  1.2  jmcneill 
    140  1.2  jmcneill 	plgpio_acpi_init(asc);
    141  1.2  jmcneill 
    142  1.1  jmcneill done:
    143  1.1  jmcneill 	acpi_resource_cleanup(&res);
    144  1.1  jmcneill }
    145  1.2  jmcneill 
    146  1.2  jmcneill static ACPI_STATUS
    147  1.2  jmcneill plgpio_acpi_resource_cb(ACPI_RESOURCE *res, void *ctx)
    148  1.2  jmcneill {
    149  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = ctx;
    150  1.2  jmcneill 	ACPI_RESOURCE_GPIO *gpio;
    151  1.2  jmcneill 	UINT16 pin;
    152  1.2  jmcneill 
    153  1.2  jmcneill 	if (res->Type != ACPI_RESOURCE_TYPE_GPIO)
    154  1.2  jmcneill 		return AE_OK;
    155  1.2  jmcneill 
    156  1.2  jmcneill 	gpio = &res->Data.Gpio;
    157  1.2  jmcneill 	if (gpio->ConnectionType != ACPI_RESOURCE_GPIO_TYPE_INT ||
    158  1.2  jmcneill 	    gpio->PinTableLength != 1)
    159  1.2  jmcneill 		return AE_OK;
    160  1.2  jmcneill 
    161  1.2  jmcneill 	pin = gpio->PinTable[0];
    162  1.2  jmcneill 	if (pin >= __arraycount(asc->sc_aei_res)) {
    163  1.2  jmcneill 		aprint_error_dev(asc->sc_base.sc_dev, "_AEI pin %u out of range\n", pin);
    164  1.2  jmcneill 		return AE_OK;
    165  1.2  jmcneill 	}
    166  1.2  jmcneill 
    167  1.2  jmcneill 	asc->sc_aei_pins |= __BIT(pin);
    168  1.2  jmcneill 	asc->sc_aei_res[pin] = *gpio;
    169  1.2  jmcneill 
    170  1.2  jmcneill 	return AE_OK;
    171  1.2  jmcneill }
    172  1.2  jmcneill 
    173  1.2  jmcneill static void
    174  1.2  jmcneill plgpio_acpi_init(struct plgpio_acpi_softc *asc)
    175  1.2  jmcneill {
    176  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    177  1.2  jmcneill 	ACPI_RESOURCE_GPIO *gpio;
    178  1.2  jmcneill 	ACPI_HANDLE handle;
    179  1.2  jmcneill 	char namebuf[5];
    180  1.2  jmcneill 	ACPI_STATUS rv;
    181  1.2  jmcneill 	uint32_t ibe, iev, is, ie;
    182  1.2  jmcneill 	int pin;
    183  1.2  jmcneill 
    184  1.2  jmcneill 	rv = AcpiWalkResources(asc->sc_handle, "_AEI", plgpio_acpi_resource_cb, asc);
    185  1.2  jmcneill 	if (ACPI_FAILURE(rv)) {
    186  1.2  jmcneill 		if (rv != AE_NOT_FOUND)
    187  1.2  jmcneill 			aprint_error_dev(asc->sc_base.sc_dev, "failed to parse _AEI: %s\n",
    188  1.2  jmcneill 			    AcpiFormatException(rv));
    189  1.2  jmcneill 		return;
    190  1.2  jmcneill 	}
    191  1.2  jmcneill 
    192  1.2  jmcneill 	if (!asc->sc_aei_pins)
    193  1.2  jmcneill 		return;
    194  1.2  jmcneill 
    195  1.2  jmcneill 	aprint_verbose_dev(asc->sc_base.sc_dev, "ACPI event pins: %#x\n", asc->sc_aei_pins);
    196  1.2  jmcneill 
    197  1.2  jmcneill 	sc->sc_reserved_mask = asc->sc_aei_pins;
    198  1.2  jmcneill 
    199  1.2  jmcneill 	/*
    200  1.2  jmcneill 	 * For each event pin, find the corresponding event method (_Exx, _Lxx, or _EVT).
    201  1.2  jmcneill 	 */
    202  1.2  jmcneill 	for (pin = 0; pin < __arraycount(asc->sc_aei_res); pin++) {
    203  1.2  jmcneill 		if ((asc->sc_aei_pins & __BIT(pin)) == 0)
    204  1.2  jmcneill 			continue;
    205  1.2  jmcneill 
    206  1.2  jmcneill 		gpio = &asc->sc_aei_res[pin];
    207  1.2  jmcneill 
    208  1.2  jmcneill 		const char trig = gpio->Triggering == ACPI_LEVEL_SENSITIVE ? 'L' : 'E';
    209  1.2  jmcneill 		handle = NULL;
    210  1.2  jmcneill 		snprintf(namebuf, sizeof(namebuf), "_%c%02X", trig, pin);
    211  1.2  jmcneill 		if (ACPI_FAILURE(AcpiGetHandle(asc->sc_handle, namebuf, &handle))) {
    212  1.2  jmcneill 			(void)AcpiGetHandle(asc->sc_handle, "_EVT", &handle);
    213  1.2  jmcneill 			if (handle != NULL)
    214  1.2  jmcneill 				asc->sc_event[pin].ev_method_evt = true;
    215  1.2  jmcneill 		}
    216  1.2  jmcneill 
    217  1.2  jmcneill 		if (handle == NULL)
    218  1.2  jmcneill 			continue;
    219  1.2  jmcneill 
    220  1.2  jmcneill 		asc->sc_event[pin].ev_sc = asc;
    221  1.2  jmcneill 		asc->sc_event[pin].ev_pin = pin;
    222  1.2  jmcneill 		asc->sc_event[pin].ev_method = handle;
    223  1.2  jmcneill 
    224  1.2  jmcneill 		/*
    225  1.2  jmcneill 		 * Configure and enable interrupts for this pin.
    226  1.2  jmcneill 		 */
    227  1.2  jmcneill 
    228  1.2  jmcneill 		ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG);
    229  1.2  jmcneill 		iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG);
    230  1.2  jmcneill 		switch (gpio->Polarity) {
    231  1.2  jmcneill 		case ACPI_ACTIVE_HIGH:
    232  1.2  jmcneill 			ibe &= ~__BIT(pin);
    233  1.2  jmcneill 			iev |= __BIT(pin);
    234  1.2  jmcneill 			break;
    235  1.2  jmcneill 		case ACPI_ACTIVE_LOW:
    236  1.2  jmcneill 			ibe &= ~__BIT(pin);
    237  1.2  jmcneill 			iev &= ~__BIT(pin);
    238  1.2  jmcneill 			break;
    239  1.2  jmcneill 		case ACPI_ACTIVE_BOTH:
    240  1.2  jmcneill 			ibe |= __BIT(pin);
    241  1.2  jmcneill 			break;
    242  1.2  jmcneill 		}
    243  1.2  jmcneill 		PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe);
    244  1.2  jmcneill 		PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev);
    245  1.2  jmcneill 
    246  1.2  jmcneill 		is = PLGPIO_READ(sc, PL061_GPIOIS_REG);
    247  1.2  jmcneill 		switch (gpio->Triggering) {
    248  1.2  jmcneill 		case ACPI_LEVEL_SENSITIVE:
    249  1.2  jmcneill 			is |= __BIT(pin);
    250  1.2  jmcneill 			break;
    251  1.2  jmcneill 		case ACPI_EDGE_SENSITIVE:
    252  1.2  jmcneill 			is &= ~__BIT(pin);
    253  1.2  jmcneill 			break;
    254  1.2  jmcneill 		}
    255  1.2  jmcneill 		PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is);
    256  1.2  jmcneill 
    257  1.2  jmcneill 		delay(20);
    258  1.2  jmcneill 
    259  1.2  jmcneill 		PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin));
    260  1.2  jmcneill 
    261  1.2  jmcneill 		ie = PLGPIO_READ(sc, PL061_GPIOIE_REG);
    262  1.2  jmcneill 		ie |= __BIT(pin);
    263  1.2  jmcneill 		PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie);
    264  1.2  jmcneill 	}
    265  1.2  jmcneill }
    266  1.2  jmcneill 
    267  1.2  jmcneill static void
    268  1.2  jmcneill plgpio_acpi_notify(void *priv)
    269  1.2  jmcneill {
    270  1.2  jmcneill 	struct plgpio_acpi_event * const ev = priv;
    271  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = ev->ev_sc;
    272  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    273  1.2  jmcneill 	ACPI_STATUS rv;
    274  1.2  jmcneill 
    275  1.2  jmcneill 	if (ev->ev_method_evt) {
    276  1.2  jmcneill 		ACPI_OBJECT_LIST objs;
    277  1.2  jmcneill 		ACPI_OBJECT obj[1];
    278  1.2  jmcneill 		objs.Count = 1;
    279  1.2  jmcneill 		objs.Pointer = obj;
    280  1.2  jmcneill 		obj[0].Type = ACPI_TYPE_INTEGER;
    281  1.2  jmcneill 		obj[0].Integer.Value = ev->ev_pin;
    282  1.2  jmcneill 		rv = AcpiEvaluateObject(ev->ev_method, NULL, &objs, NULL);
    283  1.2  jmcneill 	} else {
    284  1.2  jmcneill 		rv = AcpiEvaluateObject(ev->ev_method, NULL, NULL, NULL);
    285  1.2  jmcneill 	}
    286  1.2  jmcneill 
    287  1.2  jmcneill 	if (ACPI_FAILURE(rv))
    288  1.2  jmcneill 		device_printf(sc->sc_dev, "failed to handle %s event: %s\n",
    289  1.2  jmcneill 		    acpi_name(ev->ev_method), AcpiFormatException(rv));
    290  1.2  jmcneill }
    291  1.2  jmcneill 
    292  1.2  jmcneill static int
    293  1.2  jmcneill plgpio_acpi_intr(void *priv)
    294  1.2  jmcneill {
    295  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = priv;
    296  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    297  1.2  jmcneill 	uint32_t mis;
    298  1.2  jmcneill 	int bit;
    299  1.2  jmcneill 
    300  1.2  jmcneill 	mis = PLGPIO_READ(sc, PL061_GPIOMIS_REG);
    301  1.2  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIC_REG, mis);
    302  1.2  jmcneill 
    303  1.2  jmcneill 	while ((bit = __builtin_ffs(mis)) != 0) {
    304  1.2  jmcneill 		const int pin = bit - 1;
    305  1.2  jmcneill 		struct plgpio_acpi_event * const ev = &asc->sc_event[pin];
    306  1.2  jmcneill 
    307  1.2  jmcneill 		KASSERT(ev->ev_method != NULL);
    308  1.2  jmcneill 
    309  1.2  jmcneill 		AcpiOsExecute(OSL_NOTIFY_HANDLER, plgpio_acpi_notify, ev);
    310  1.2  jmcneill 
    311  1.2  jmcneill 		mis &= ~__BIT(pin);
    312  1.2  jmcneill 	}
    313  1.2  jmcneill 
    314  1.2  jmcneill 	return 1;
    315  1.2  jmcneill }
    316