Home | History | Annotate | Line # | Download | only in sociox
sni_gpio.c revision 1.7.4.2
      1  1.7.4.2  martin /*	$NetBSD: sni_gpio.c,v 1.7.4.2 2020/04/13 08:03:37 martin Exp $	*/
      2  1.7.4.2  martin 
      3  1.7.4.2  martin /*-
      4  1.7.4.2  martin  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.7.4.2  martin  * All rights reserved.
      6  1.7.4.2  martin  *
      7  1.7.4.2  martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.7.4.2  martin  * by Tohru Nishimura.
      9  1.7.4.2  martin  *
     10  1.7.4.2  martin  * Redistribution and use in source and binary forms, with or without
     11  1.7.4.2  martin  * modification, are permitted provided that the following conditions
     12  1.7.4.2  martin  * are met:
     13  1.7.4.2  martin  * 1. Redistributions of source code must retain the above copyright
     14  1.7.4.2  martin  *    notice, this list of conditions and the following disclaimer.
     15  1.7.4.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.7.4.2  martin  *    notice, this list of conditions and the following disclaimer in the
     17  1.7.4.2  martin  *    documentation and/or other materials provided with the distribution.
     18  1.7.4.2  martin  *
     19  1.7.4.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.7.4.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.7.4.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.7.4.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.7.4.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.7.4.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.7.4.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.7.4.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.7.4.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.7.4.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.7.4.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     30  1.7.4.2  martin  */
     31  1.7.4.2  martin 
     32  1.7.4.2  martin /*
     33  1.7.4.2  martin  * Socionext SC2A11 SynQuacer GPIO driver
     34  1.7.4.2  martin  */
     35  1.7.4.2  martin 
     36  1.7.4.2  martin #include <sys/cdefs.h>
     37  1.7.4.2  martin __KERNEL_RCSID(0, "$NetBSD: sni_gpio.c,v 1.7.4.2 2020/04/13 08:03:37 martin Exp $");
     38  1.7.4.2  martin 
     39  1.7.4.2  martin #include <sys/param.h>
     40  1.7.4.2  martin #include <sys/device.h>
     41  1.7.4.2  martin #include <sys/systm.h>
     42  1.7.4.2  martin #include <sys/gpio.h>
     43  1.7.4.2  martin #include <sys/kernel.h>
     44  1.7.4.2  martin #include <sys/systm.h>
     45  1.7.4.2  martin 
     46  1.7.4.2  martin #include <machine/endian.h>
     47  1.7.4.2  martin #include <sys/bus.h>
     48  1.7.4.2  martin #include <sys/intr.h>
     49  1.7.4.2  martin 
     50  1.7.4.2  martin #include <dev/gpio/gpiovar.h>
     51  1.7.4.2  martin #include <dev/fdt/fdtvar.h>
     52  1.7.4.2  martin #include <dev/acpi/acpireg.h>
     53  1.7.4.2  martin #include <dev/acpi/acpivar.h>
     54  1.7.4.2  martin #include <dev/acpi/acpi_intr.h>
     55  1.7.4.2  martin 
     56  1.7.4.2  martin static int snigpio_fdt_match(device_t, struct cfdata *, void *);
     57  1.7.4.2  martin static void snigpio_fdt_attach(device_t, device_t, void *);
     58  1.7.4.2  martin static int snigpio_acpi_match(device_t, struct cfdata *, void *);
     59  1.7.4.2  martin static void snigpio_acpi_attach(device_t, device_t, void *);
     60  1.7.4.2  martin 
     61  1.7.4.2  martin struct snigpio_softc {
     62  1.7.4.2  martin 	device_t		sc_dev;
     63  1.7.4.2  martin 	bus_space_tag_t		sc_iot;
     64  1.7.4.2  martin 	bus_space_handle_t	sc_ioh;
     65  1.7.4.2  martin 	bus_addr_t		sc_iob;
     66  1.7.4.2  martin 	bus_size_t		sc_ios;
     67  1.7.4.2  martin 	void			*sc_ih;
     68  1.7.4.2  martin 	kmutex_t		sc_lock;
     69  1.7.4.2  martin 	struct gpio_chipset_tag	sc_gpio_gc;
     70  1.7.4.2  martin 	gpio_pin_t		sc_gpio_pins[32];
     71  1.7.4.2  martin 	int			sc_maxpins;
     72  1.7.4.2  martin 	int			sc_phandle;
     73  1.7.4.2  martin };
     74  1.7.4.2  martin 
     75  1.7.4.2  martin CFATTACH_DECL_NEW(snigpio_fdt, sizeof(struct snigpio_softc),
     76  1.7.4.2  martin     snigpio_fdt_match, snigpio_fdt_attach, NULL, NULL);
     77  1.7.4.2  martin 
     78  1.7.4.2  martin CFATTACH_DECL_NEW(snigpio_acpi, sizeof(struct snigpio_softc),
     79  1.7.4.2  martin     snigpio_acpi_match, snigpio_acpi_attach, NULL, NULL);
     80  1.7.4.2  martin 
     81  1.7.4.2  martin /*
     82  1.7.4.2  martin  * "DevelopmentBox" implementation
     83  1.7.4.2  martin  *    DSW3-PIN1,  DSW3-PIN2,  DSW3-PIN3,    DSW3-PIN4,
     84  1.7.4.2  martin  *    DSW3-PIN5,  DSW3-PIN6,  DSW3-PIN7,    DSW3-PIN8,
     85  1.7.4.2  martin  *    PSIN#,      PWROFF#,    GPIO-A,       GPIO-B,
     86  1.7.4.2  martin  *    GPIO-C,     GPIO-D,     PCIE1EXTINT,  PCIE0EXTINT,
     87  1.7.4.2  martin  *    PHY2-INT#,  PHY1-INT#,  GPIO-E,       GPIO-F,
     88  1.7.4.2  martin  *    GPIO-G,     GPIO-H,     GPIO-I,       GPIO-J,
     89  1.7.4.2  martin  *    GPIO-K,     GPIO-L,     PEC-PD26,     PEC-PD27,
     90  1.7.4.2  martin  *    PEC-PD28,   PEC-PD29,   PEC-PD30,     PEC-PD31
     91  1.7.4.2  martin  *
     92  1.7.4.2  martin  *    DSW3-PIN1 -- what's "varstore" really this
     93  1.7.4.2  martin  *    DSW3-PIN3 -- tweek PCIe bus implementation error toggle
     94  1.7.4.2  martin  *    PowerButton (PWROFF#) can be detectable.
     95  1.7.4.2  martin  *
     96  1.7.4.2  martin  *  96board mezzanine
     97  1.7.4.2  martin  *    i2c  "/i2c@51221000"
     98  1.7.4.2  martin  *    spi  "/spi@54810000"
     99  1.7.4.2  martin  *    gpio "/gpio@51000000" pinA-L (10-25) down edge sensitive
    100  1.7.4.2  martin  */
    101  1.7.4.2  martin static void snigpio_attach_i(struct snigpio_softc *);
    102  1.7.4.2  martin static int snigpio_intr(void *);
    103  1.7.4.2  martin 
    104  1.7.4.2  martin static int
    105  1.7.4.2  martin snigpio_fdt_match(device_t parent, struct cfdata *match, void *aux)
    106  1.7.4.2  martin {
    107  1.7.4.2  martin 	static const char * compatible[] = {
    108  1.7.4.2  martin 		"socionext,synquacer-gpio",
    109  1.7.4.2  martin 		"fujitsu,mb86s70-gpio",
    110  1.7.4.2  martin 		NULL
    111  1.7.4.2  martin 	};
    112  1.7.4.2  martin 	struct fdt_attach_args * const faa = aux;
    113  1.7.4.2  martin 
    114  1.7.4.2  martin 	return of_match_compatible(faa->faa_phandle, compatible);
    115  1.7.4.2  martin }
    116  1.7.4.2  martin 
    117  1.7.4.2  martin static void
    118  1.7.4.2  martin snigpio_fdt_attach(device_t parent, device_t self, void *aux)
    119  1.7.4.2  martin {
    120  1.7.4.2  martin 	struct snigpio_softc * const sc = device_private(self);
    121  1.7.4.2  martin 	struct fdt_attach_args * const faa = aux;
    122  1.7.4.2  martin 	prop_dictionary_t dict = device_properties(self);
    123  1.7.4.2  martin 	const int phandle = faa->faa_phandle;
    124  1.7.4.2  martin 	bus_space_handle_t ioh;
    125  1.7.4.2  martin 	bus_addr_t addr;
    126  1.7.4.2  martin 	bus_size_t size;
    127  1.7.4.2  martin 	char intrstr[128];
    128  1.7.4.2  martin 	const char *list;
    129  1.7.4.2  martin 	_Bool disable;
    130  1.7.4.2  martin 
    131  1.7.4.2  martin 	prop_dictionary_get_bool(dict, "disable", &disable);
    132  1.7.4.2  martin 	if (disable) {
    133  1.7.4.2  martin 		aprint_naive(": disabled\n");
    134  1.7.4.2  martin 		aprint_normal(": disabled\n");
    135  1.7.4.2  martin 		return;
    136  1.7.4.2  martin 	}
    137  1.7.4.2  martin 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
    138  1.7.4.2  martin 	    || bus_space_map(faa->faa_bst, addr, size, 0, &ioh) != 0) {
    139  1.7.4.2  martin 		aprint_error(": unable to map device\n");
    140  1.7.4.2  martin 		return;
    141  1.7.4.2  martin 	}
    142  1.7.4.2  martin 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    143  1.7.4.2  martin 		aprint_error(": failed to decode interrupt\n");
    144  1.7.4.2  martin 		goto fail;
    145  1.7.4.2  martin 	}
    146  1.7.4.2  martin 	sc->sc_ih = fdtbus_intr_establish(phandle,
    147  1.7.4.2  martin 			0, IPL_VM, 0, snigpio_intr, sc);
    148  1.7.4.2  martin 	if (sc->sc_ih == NULL) {
    149  1.7.4.2  martin 		aprint_error_dev(self, "couldn't establish interrupt\n");
    150  1.7.4.2  martin 		goto fail;
    151  1.7.4.2  martin 	}
    152  1.7.4.2  martin 
    153  1.7.4.2  martin 	aprint_naive("\n");
    154  1.7.4.2  martin 	aprint_normal_dev(self, "Socionext GPIO controller\n");
    155  1.7.4.2  martin 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    156  1.7.4.2  martin 	list = fdtbus_get_string(phandle, "gpio-line-names");
    157  1.7.4.2  martin 	if (list)
    158  1.7.4.2  martin 		aprint_normal_dev(self, "%s\n", list);
    159  1.7.4.2  martin 
    160  1.7.4.2  martin 	sc->sc_dev = self;
    161  1.7.4.2  martin 	sc->sc_phandle = phandle;
    162  1.7.4.2  martin 	sc->sc_iot = faa->faa_bst;
    163  1.7.4.2  martin 	sc->sc_ioh = ioh;
    164  1.7.4.2  martin 	sc->sc_iob = addr;
    165  1.7.4.2  martin 	sc->sc_ios = size;
    166  1.7.4.2  martin 
    167  1.7.4.2  martin 	snigpio_attach_i(sc);
    168  1.7.4.2  martin 
    169  1.7.4.2  martin 	return;
    170  1.7.4.2  martin  fail:
    171  1.7.4.2  martin 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    172  1.7.4.2  martin 	return;
    173  1.7.4.2  martin }
    174  1.7.4.2  martin 
    175  1.7.4.2  martin static int
    176  1.7.4.2  martin snigpio_acpi_match(device_t parent, struct cfdata *match, void *aux)
    177  1.7.4.2  martin {
    178  1.7.4.2  martin 	static const char * compatible[] = {
    179  1.7.4.2  martin 		"SCX0007",
    180  1.7.4.2  martin 		NULL
    181  1.7.4.2  martin 	};
    182  1.7.4.2  martin 	struct acpi_attach_args *aa = aux;
    183  1.7.4.2  martin 
    184  1.7.4.2  martin 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    185  1.7.4.2  martin 		return 0;
    186  1.7.4.2  martin 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
    187  1.7.4.2  martin }
    188  1.7.4.2  martin 
    189  1.7.4.2  martin static void
    190  1.7.4.2  martin snigpio_acpi_attach(device_t parent, device_t self, void *aux)
    191  1.7.4.2  martin {
    192  1.7.4.2  martin 	struct snigpio_softc * const sc = device_private(self);
    193  1.7.4.2  martin 	struct acpi_attach_args *aa = aux;
    194  1.7.4.2  martin 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
    195  1.7.4.2  martin 	bus_space_handle_t ioh;
    196  1.7.4.2  martin 	struct acpi_resources res;
    197  1.7.4.2  martin 	struct acpi_mem *mem;
    198  1.7.4.2  martin 	struct acpi_irq *irq;
    199  1.7.4.2  martin 	ACPI_STATUS rv;
    200  1.7.4.2  martin 	char *list;
    201  1.7.4.2  martin 
    202  1.7.4.2  martin 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    203  1.7.4.2  martin 	    &res, &acpi_resource_parse_ops_default);
    204  1.7.4.2  martin 	if (ACPI_FAILURE(rv))
    205  1.7.4.2  martin 		return;
    206  1.7.4.2  martin 	mem = acpi_res_mem(&res, 0);
    207  1.7.4.2  martin 	irq = acpi_res_irq(&res, 0);
    208  1.7.4.2  martin 	if (mem == NULL || irq == NULL || mem->ar_length == 0) {
    209  1.7.4.2  martin 		aprint_error(": incomplete resources\n");
    210  1.7.4.2  martin 		return;
    211  1.7.4.2  martin 	}
    212  1.7.4.2  martin 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
    213  1.7.4.2  martin 	    &ioh)) {
    214  1.7.4.2  martin 		aprint_error(": couldn't map registers\n");
    215  1.7.4.2  martin 		return;
    216  1.7.4.2  martin 	}
    217  1.7.4.2  martin 	sc->sc_ih = acpi_intr_establish(self,
    218  1.7.4.2  martin 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    219  1.7.4.2  martin 	    IPL_VM, false, snigpio_intr, sc, device_xname(self));
    220  1.7.4.2  martin 	if (sc->sc_ih == NULL) {
    221  1.7.4.2  martin 		aprint_error_dev(self, "couldn't establish interrupt\n");
    222  1.7.4.2  martin 		goto fail;
    223  1.7.4.2  martin 	}
    224  1.7.4.2  martin 
    225  1.7.4.2  martin 	aprint_normal_dev(self, "Socionext GPIO controller\n");
    226  1.7.4.2  martin 	rv = acpi_dsd_string(handle, "gpio-line-names", &list);
    227  1.7.4.2  martin 	if (ACPI_SUCCESS(rv))
    228  1.7.4.2  martin 		aprint_normal_dev(self, "%s\n", list);
    229  1.7.4.2  martin 
    230  1.7.4.2  martin 	sc->sc_dev = self;
    231  1.7.4.2  martin 	sc->sc_iot = aa->aa_memt;
    232  1.7.4.2  martin 	sc->sc_ioh = ioh;
    233  1.7.4.2  martin 	sc->sc_ios = mem->ar_length;
    234  1.7.4.2  martin 
    235  1.7.4.2  martin 	snigpio_attach_i(sc);
    236  1.7.4.2  martin 
    237  1.7.4.2  martin 	acpi_resource_cleanup(&res);
    238  1.7.4.2  martin 	return;
    239  1.7.4.2  martin  fail:
    240  1.7.4.2  martin 	acpi_resource_cleanup(&res);
    241  1.7.4.2  martin 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    242  1.7.4.2  martin 	return;
    243  1.7.4.2  martin }
    244  1.7.4.2  martin 
    245  1.7.4.2  martin static void
    246  1.7.4.2  martin snigpio_attach_i(struct snigpio_softc *sc)
    247  1.7.4.2  martin {
    248  1.7.4.2  martin 	struct gpio_chipset_tag	*gc;
    249  1.7.4.2  martin 	struct gpiobus_attach_args gba;
    250  1.7.4.2  martin 
    251  1.7.4.2  martin 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    252  1.7.4.2  martin 	sc->sc_maxpins = 32;
    253  1.7.4.2  martin 
    254  1.7.4.2  martin 	/* create controller tag */
    255  1.7.4.2  martin 	gc = &sc->sc_gpio_gc;
    256  1.7.4.2  martin 	gc->gp_cookie = sc;
    257  1.7.4.2  martin 	gc->gp_pin_read = NULL; /* AAA */
    258  1.7.4.2  martin 	gc->gp_pin_write = NULL; /* AAA */
    259  1.7.4.2  martin 	gc->gp_pin_ctl = NULL; /* AAA */
    260  1.7.4.2  martin 	gc->gp_intr_establish = NULL; /* AAA */
    261  1.7.4.2  martin 	gc->gp_intr_disestablish = NULL; /* AAA */
    262  1.7.4.2  martin 	gc->gp_intr_str = NULL; /* AAA */
    263  1.7.4.2  martin 
    264  1.7.4.2  martin 	gba.gba_gc = gc;
    265  1.7.4.2  martin 	gba.gba_pins = &sc->sc_gpio_pins[0];
    266  1.7.4.2  martin 	gba.gba_npins = sc->sc_maxpins;
    267  1.7.4.2  martin 
    268  1.7.4.2  martin 	(void) config_found_ia(sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    269  1.7.4.2  martin }
    270  1.7.4.2  martin 
    271  1.7.4.2  martin static int
    272  1.7.4.2  martin snigpio_intr(void *arg)
    273  1.7.4.2  martin {
    274  1.7.4.2  martin 	return 1;
    275  1.7.4.2  martin }
    276