Home | History | Annotate | Line # | Download | only in acpi
plgpio_acpi.c revision 1.3
      1  1.3  jmcneill /* $NetBSD: plgpio_acpi.c,v 1.3 2018/10/22 22:29:35 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.3  jmcneill __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.3 2018/10/22 22:29:35 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.3  jmcneill #include <dev/acpi/acpi_event.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <dev/gpio/gpiovar.h>
     46  1.1  jmcneill #include <dev/ic/pl061var.h>
     47  1.2  jmcneill #include <dev/ic/pl061reg.h>
     48  1.2  jmcneill 
     49  1.2  jmcneill struct plgpio_acpi_softc;
     50  1.2  jmcneill 
     51  1.2  jmcneill struct plgpio_acpi_softc {
     52  1.2  jmcneill 	struct plgpio_softc	sc_base;
     53  1.2  jmcneill 
     54  1.2  jmcneill 	ACPI_HANDLE		sc_handle;
     55  1.2  jmcneill 
     56  1.3  jmcneill 	struct acpi_event *	sc_event[8];
     57  1.2  jmcneill };
     58  1.1  jmcneill 
     59  1.1  jmcneill static int	plgpio_acpi_match(device_t, cfdata_t, void *);
     60  1.1  jmcneill static void	plgpio_acpi_attach(device_t, device_t, void *);
     61  1.1  jmcneill 
     62  1.3  jmcneill static void	plgpio_acpi_register_event(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
     63  1.2  jmcneill static int	plgpio_acpi_intr(void *);
     64  1.2  jmcneill 
     65  1.2  jmcneill CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL);
     66  1.1  jmcneill 
     67  1.1  jmcneill static const char * const compatible[] = {
     68  1.1  jmcneill 	"ARMH0061",
     69  1.1  jmcneill 	NULL
     70  1.1  jmcneill };
     71  1.1  jmcneill 
     72  1.1  jmcneill static int
     73  1.1  jmcneill plgpio_acpi_match(device_t parent, cfdata_t cf, void *aux)
     74  1.1  jmcneill {
     75  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     76  1.1  jmcneill 
     77  1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     78  1.1  jmcneill 		return 0;
     79  1.1  jmcneill 
     80  1.1  jmcneill 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
     81  1.1  jmcneill }
     82  1.1  jmcneill 
     83  1.1  jmcneill static void
     84  1.1  jmcneill plgpio_acpi_attach(device_t parent, device_t self, void *aux)
     85  1.1  jmcneill {
     86  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = device_private(self);
     87  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
     88  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     89  1.1  jmcneill 	struct acpi_resources res;
     90  1.1  jmcneill 	struct acpi_mem *mem;
     91  1.2  jmcneill 	struct acpi_irq *irq;
     92  1.1  jmcneill 	ACPI_STATUS rv;
     93  1.1  jmcneill 	int error;
     94  1.2  jmcneill 	void *ih;
     95  1.1  jmcneill 
     96  1.1  jmcneill 	sc->sc_dev = self;
     97  1.2  jmcneill 	asc->sc_handle = aa->aa_node->ad_handle;
     98  1.1  jmcneill 
     99  1.1  jmcneill 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    100  1.1  jmcneill 	    &res, &acpi_resource_parse_ops_default);
    101  1.1  jmcneill 	if (ACPI_FAILURE(rv))
    102  1.1  jmcneill 		return;
    103  1.1  jmcneill 
    104  1.1  jmcneill 	mem = acpi_res_mem(&res, 0);
    105  1.1  jmcneill 	if (mem == NULL) {
    106  1.1  jmcneill 		aprint_error_dev(self, "couldn't find mem resource\n");
    107  1.1  jmcneill 		goto done;
    108  1.1  jmcneill 	}
    109  1.1  jmcneill 
    110  1.2  jmcneill 	irq = acpi_res_irq(&res, 0);
    111  1.2  jmcneill 	if (mem == NULL) {
    112  1.2  jmcneill 		aprint_error_dev(self, "couldn't find irq resource\n");
    113  1.2  jmcneill 		goto done;
    114  1.2  jmcneill 	}
    115  1.2  jmcneill 
    116  1.1  jmcneill 	sc->sc_dev = self;
    117  1.1  jmcneill 	sc->sc_bst = aa->aa_memt;
    118  1.1  jmcneill 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0, &sc->sc_bsh);
    119  1.1  jmcneill 	if (error) {
    120  1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    121  1.1  jmcneill 		return;
    122  1.1  jmcneill 	}
    123  1.1  jmcneill 
    124  1.1  jmcneill 	plgpio_attach(sc);
    125  1.1  jmcneill 
    126  1.3  jmcneill 	if (ACPI_FAILURE(acpi_event_create_gpio(self, asc->sc_handle, plgpio_acpi_register_event, asc))) {
    127  1.3  jmcneill 		aprint_error_dev(self, "failed to create events\n");
    128  1.3  jmcneill 		goto done;
    129  1.3  jmcneill 	}
    130  1.3  jmcneill 
    131  1.2  jmcneill 	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    132  1.2  jmcneill 	ih = intr_establish(irq->ar_irq, IPL_VM, type, plgpio_acpi_intr, asc);
    133  1.2  jmcneill 	if (ih == NULL)
    134  1.2  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt\n");
    135  1.2  jmcneill 
    136  1.1  jmcneill done:
    137  1.1  jmcneill 	acpi_resource_cleanup(&res);
    138  1.1  jmcneill }
    139  1.2  jmcneill 
    140  1.2  jmcneill static void
    141  1.3  jmcneill plgpio_acpi_register_event(void *priv, struct acpi_event *ev, ACPI_RESOURCE_GPIO *gpio)
    142  1.2  jmcneill {
    143  1.3  jmcneill 	struct plgpio_acpi_softc * const asc = priv;
    144  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    145  1.2  jmcneill 	uint32_t ibe, iev, is, ie;
    146  1.2  jmcneill 
    147  1.3  jmcneill 	const int pin = gpio->PinTable[0];
    148  1.3  jmcneill 
    149  1.3  jmcneill 	if (pin >= __arraycount(asc->sc_event)) {
    150  1.3  jmcneill 		aprint_error_dev(asc->sc_base.sc_dev,
    151  1.3  jmcneill 		    "ignoring event for pin %u (out of range)\n", pin);
    152  1.2  jmcneill 		return;
    153  1.2  jmcneill 	}
    154  1.3  jmcneill 	if (asc->sc_event[pin] != NULL) {
    155  1.3  jmcneill 		aprint_error_dev(asc->sc_base.sc_dev,
    156  1.3  jmcneill 		    "ignoring duplicate pin %u\n", pin);
    157  1.2  jmcneill 		return;
    158  1.3  jmcneill 	}
    159  1.2  jmcneill 
    160  1.3  jmcneill 	asc->sc_event[pin] = ev;
    161  1.3  jmcneill 	asc->sc_base.sc_reserved_mask |= __BIT(pin);
    162  1.2  jmcneill 
    163  1.2  jmcneill 	/*
    164  1.3  jmcneill 	 * Configure and enable interrupts for this pin.
    165  1.2  jmcneill 	 */
    166  1.2  jmcneill 
    167  1.3  jmcneill 	ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG);
    168  1.3  jmcneill 	iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG);
    169  1.3  jmcneill 	switch (gpio->Polarity) {
    170  1.3  jmcneill 	case ACPI_ACTIVE_HIGH:
    171  1.3  jmcneill 		ibe &= ~__BIT(pin);
    172  1.3  jmcneill 		iev |= __BIT(pin);
    173  1.3  jmcneill 		break;
    174  1.3  jmcneill 	case ACPI_ACTIVE_LOW:
    175  1.3  jmcneill 		ibe &= ~__BIT(pin);
    176  1.3  jmcneill 		iev &= ~__BIT(pin);
    177  1.3  jmcneill 		break;
    178  1.3  jmcneill 	case ACPI_ACTIVE_BOTH:
    179  1.3  jmcneill 		ibe |= __BIT(pin);
    180  1.3  jmcneill 		break;
    181  1.3  jmcneill 	}
    182  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe);
    183  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev);
    184  1.3  jmcneill 
    185  1.3  jmcneill 	is = PLGPIO_READ(sc, PL061_GPIOIS_REG);
    186  1.3  jmcneill 	switch (gpio->Triggering) {
    187  1.3  jmcneill 	case ACPI_LEVEL_SENSITIVE:
    188  1.3  jmcneill 		is |= __BIT(pin);
    189  1.3  jmcneill 		break;
    190  1.3  jmcneill 	case ACPI_EDGE_SENSITIVE:
    191  1.3  jmcneill 		is &= ~__BIT(pin);
    192  1.3  jmcneill 		break;
    193  1.3  jmcneill 	}
    194  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is);
    195  1.3  jmcneill 
    196  1.3  jmcneill 	delay(20);
    197  1.3  jmcneill 
    198  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin));
    199  1.3  jmcneill 
    200  1.3  jmcneill 	ie = PLGPIO_READ(sc, PL061_GPIOIE_REG);
    201  1.3  jmcneill 	ie |= __BIT(pin);
    202  1.3  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie);
    203  1.2  jmcneill }
    204  1.2  jmcneill 
    205  1.2  jmcneill static int
    206  1.2  jmcneill plgpio_acpi_intr(void *priv)
    207  1.2  jmcneill {
    208  1.2  jmcneill 	struct plgpio_acpi_softc * const asc = priv;
    209  1.2  jmcneill 	struct plgpio_softc * const sc = &asc->sc_base;
    210  1.2  jmcneill 	uint32_t mis;
    211  1.2  jmcneill 	int bit;
    212  1.2  jmcneill 
    213  1.2  jmcneill 	mis = PLGPIO_READ(sc, PL061_GPIOMIS_REG);
    214  1.2  jmcneill 	PLGPIO_WRITE(sc, PL061_GPIOIC_REG, mis);
    215  1.2  jmcneill 
    216  1.2  jmcneill 	while ((bit = __builtin_ffs(mis)) != 0) {
    217  1.2  jmcneill 		const int pin = bit - 1;
    218  1.3  jmcneill 		struct acpi_event * const ev = asc->sc_event[pin];
    219  1.3  jmcneill 		KASSERT(ev != NULL);
    220  1.2  jmcneill 
    221  1.3  jmcneill 		acpi_event_notify(ev);
    222  1.2  jmcneill 
    223  1.2  jmcneill 		mis &= ~__BIT(pin);
    224  1.2  jmcneill 	}
    225  1.2  jmcneill 
    226  1.2  jmcneill 	return 1;
    227  1.2  jmcneill }
    228