Home | History | Annotate | Line # | Download | only in sociox
sni_gpio.c revision 1.2
      1 /*	$NetBSD: sni_gpio.c,v 1.2 2020/03/19 20:53:53 nisimura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Socionext SC2A11 SynQuacer GPIO driver
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: sni_gpio.c,v 1.2 2020/03/19 20:53:53 nisimura Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/device.h>
     41 #include <sys/systm.h>
     42 #include <sys/gpio.h>
     43 #include <sys/kernel.h>
     44 #include <sys/systm.h>
     45 
     46 #include <machine/endian.h>
     47 #include <sys/bus.h>
     48 #include <sys/intr.h>
     49 
     50 #include <dev/gpio/gpiovar.h>
     51 #include <dev/fdt/fdtvar.h>
     52 #include <dev/acpi/acpireg.h>
     53 #include <dev/acpi/acpivar.h>
     54 #include <dev/acpi/acpi_intr.h>
     55 
     56 static int snigpio_fdt_match(device_t, struct cfdata *, void *);
     57 static void snigpio_fdt_attach(device_t, device_t, void *);
     58 static int snigpio_acpi_match(device_t, struct cfdata *, void *);
     59 static void snigpio_acpi_attach(device_t, device_t, void *);
     60 
     61 struct snigpio_softc {
     62 	device_t		sc_dev;
     63 	bus_space_tag_t		sc_iot;
     64 	bus_space_handle_t	sc_ioh;
     65 	bus_addr_t		sc_iob;
     66 	bus_size_t		sc_ios;
     67 	kmutex_t		sc_lock;
     68 	struct gpio_chipset_tag	sc_gpio_gc;
     69 	gpio_pin_t		sc_gpio_pins[32];
     70 	int			sc_maxpins;
     71 	int			sc_phandle;
     72 };
     73 
     74 CFATTACH_DECL_NEW(snigpio_fdt, sizeof(struct snigpio_softc),
     75     snigpio_fdt_match, snigpio_fdt_attach, NULL, NULL);
     76 
     77 CFATTACH_DECL_NEW(snigpio_acpi, sizeof(struct snigpio_softc),
     78     snigpio_acpi_match, snigpio_acpi_attach, NULL, NULL);
     79 
     80 static void snigpio_attach_i(struct snigpio_softc *);
     81 
     82 static int
     83 snigpio_fdt_match(device_t parent, struct cfdata *match, void *aux)
     84 {
     85 	static const char * compatible[] = {
     86 		"socionext,synquacer-gpio",
     87 		"fujitsu,mb86s70-gpio",
     88 		NULL
     89 	};
     90 	struct fdt_attach_args * const faa = aux;
     91 
     92 	return of_match_compatible(faa->faa_phandle, compatible);
     93 }
     94 
     95 static void
     96 snigpio_fdt_attach(device_t parent, device_t self, void *aux)
     97 {
     98 	struct snigpio_softc * const sc = device_private(self);
     99 	struct fdt_attach_args * const faa = aux;
    100 	prop_dictionary_t dict = device_properties(self);
    101 	const int phandle = faa->faa_phandle;
    102 	bus_space_handle_t ioh;
    103 	bus_addr_t addr;
    104 	bus_size_t size;
    105 	_Bool disable;
    106 	int error;
    107 
    108 	prop_dictionary_get_bool(dict, "disable", &disable);
    109 	if (disable) {
    110 		aprint_naive(": disabled\n");
    111 		aprint_normal(": disabled\n");
    112 		return;
    113 	}
    114 	error = fdtbus_get_reg(phandle, 0, &addr, &size);
    115 	if (error) {
    116 		aprint_error(": couldn't get registers\n");
    117 		return;
    118 	}
    119 	error = bus_space_map(faa->faa_bst, addr, size, 0, &ioh);
    120 	if (error) {
    121 		aprint_error(": unable to map device\n");
    122 		return;
    123 	}
    124 
    125 	sc->sc_dev = self;
    126 	sc->sc_phandle = phandle;
    127 	sc->sc_iot = faa->faa_bst;
    128 	sc->sc_ioh = ioh;
    129 	sc->sc_iob = addr;
    130 	sc->sc_ios = size;
    131 
    132 	snigpio_attach_i(sc);
    133 
    134 	return;
    135 }
    136 
    137 static int
    138 snigpio_acpi_match(device_t parent, struct cfdata *match, void *aux)
    139 {
    140 	static const char * compatible[] = {
    141 		"SCX0007",
    142 		NULL
    143 	};
    144 	struct acpi_attach_args *aa = aux;
    145 
    146 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    147 		return 0;
    148 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
    149 }
    150 
    151 static void
    152 snigpio_acpi_attach(device_t parent, device_t self, void *aux)
    153 {
    154 	struct snigpio_softc * const sc = device_private(self);
    155 	struct acpi_attach_args *aa = aux;
    156 	bus_space_handle_t ioh;
    157 	struct acpi_resources res;
    158 	struct acpi_mem *mem;
    159 	struct acpi_irq *irq;
    160 	ACPI_STATUS rv;
    161 
    162 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    163 	    &res, &acpi_resource_parse_ops_default);
    164 	if (ACPI_FAILURE(rv))
    165 		return;
    166 
    167 	mem = acpi_res_mem(&res, 0);
    168 	irq = acpi_res_irq(&res, 0);
    169 	if (mem == NULL || irq == NULL) {
    170 		aprint_error(": incomplete resources\n");
    171 		return;
    172 	}
    173 	if (mem->ar_length == 0) {
    174 		aprint_error(": zero length memory resource\n");
    175 		return;
    176 	}
    177 	if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0,
    178 	    &ioh)) {
    179 		aprint_error(": couldn't map registers\n");
    180 		return;
    181 	}
    182 
    183 	sc->sc_dev = self;
    184 	sc->sc_iot = aa->aa_memt;
    185 	sc->sc_ioh = ioh;
    186 	sc->sc_ios = mem->ar_length;
    187 
    188 	snigpio_attach_i(sc);
    189 }
    190 
    191 static void
    192 snigpio_attach_i(struct snigpio_softc *sc)
    193 {
    194 	struct gpiobus_attach_args gba;
    195 
    196 	aprint_naive(": GPIO controller\n");
    197 	aprint_normal(": GPIO controller\n");
    198 
    199 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    200 	sc->sc_maxpins = 32;
    201 
    202 	/* create controller tag */
    203 	sc->sc_gpio_gc.gp_cookie = sc;
    204 	sc->sc_gpio_gc.gp_pin_read = NULL; /* AAA */
    205 	sc->sc_gpio_gc.gp_pin_write = NULL; /* AAA */
    206 	sc->sc_gpio_gc.gp_pin_ctl = NULL; /* AAA */
    207 	sc->sc_gpio_gc.gp_intr_establish = NULL; /* AAA */
    208 	sc->sc_gpio_gc.gp_intr_disestablish = NULL; /* AAA */
    209 	sc->sc_gpio_gc.gp_intr_str = NULL; /* AAA */
    210 
    211 	gba.gba_gc = &sc->sc_gpio_gc;
    212 	gba.gba_pins = &sc->sc_gpio_pins[0];
    213 	gba.gba_npins = sc->sc_maxpins;
    214 
    215 	(void) config_found_ia(sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    216 }
    217