Home | History | Annotate | Line # | Download | only in ep93xx
      1  1.8     skrll /*	$NetBSD: epgpio.c,v 1.8 2021/11/21 08:25:26 skrll Exp $	*/
      2  1.1  hamajima 
      3  1.1  hamajima /*
      4  1.1  hamajima  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
      5  1.1  hamajima  *
      6  1.1  hamajima  * Redistribution and use in source and binary forms, with or without
      7  1.1  hamajima  * modification, are permitted provided that the following conditions
      8  1.1  hamajima  * are met:
      9  1.1  hamajima  * 1. Redistributions of source code must retain the above copyright
     10  1.1  hamajima  *    notice, this list of conditions and the following disclaimer.
     11  1.1  hamajima  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  hamajima  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  hamajima  *    documentation and/or other materials provided with the distribution.
     14  1.1  hamajima  *
     15  1.1  hamajima  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1  hamajima  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1  hamajima  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1  hamajima  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  hamajima  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  hamajima  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1  hamajima  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  hamajima  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  hamajima  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  hamajima  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  hamajima  * SUCH DAMAGE.
     26  1.1  hamajima  */
     27  1.1  hamajima 
     28  1.1  hamajima #include <sys/cdefs.h>
     29  1.8     skrll __KERNEL_RCSID(0, "$NetBSD: epgpio.c,v 1.8 2021/11/21 08:25:26 skrll Exp $");
     30  1.1  hamajima 
     31  1.1  hamajima #include <sys/param.h>
     32  1.1  hamajima #include <sys/systm.h>
     33  1.1  hamajima #include <sys/kernel.h>
     34  1.1  hamajima #include <sys/device.h>
     35  1.4    dyoung #include <sys/bus.h>
     36  1.1  hamajima #include <machine/intr.h>
     37  1.3        he #include <sys/gpio.h>
     38  1.1  hamajima #include <dev/gpio/gpiovar.h>
     39  1.8     skrll #include <arm/ep93xx/ep93xxvar.h>
     40  1.8     skrll #include <arm/ep93xx/epsocvar.h>
     41  1.1  hamajima #include <arm/ep93xx/epgpioreg.h>
     42  1.1  hamajima #include <arm/ep93xx/epgpiovar.h>
     43  1.2      kenh #include "opt_ep93xx_gpio_mask.h"
     44  1.1  hamajima #include "gpio.h"
     45  1.1  hamajima #include "locators.h"
     46  1.1  hamajima 
     47  1.1  hamajima #ifdef EPGPIO_DEBUG
     48  1.1  hamajima int epgpio_debug = EPGPIO_DEBUG;
     49  1.1  hamajima #define DPRINTFN(n,x)	if (epgpio_debug>(n)) printf x;
     50  1.1  hamajima #else
     51  1.1  hamajima #define DPRINTFN(n,x)
     52  1.1  hamajima #endif
     53  1.1  hamajima 
     54  1.1  hamajima #define	EPGPIO_NPORTS	8
     55  1.1  hamajima #define	EPGPIO_NPINS	8
     56  1.1  hamajima 
     57  1.1  hamajima struct port_info {
     58  1.1  hamajima 	struct epgpio_softc	*sc;
     59  1.1  hamajima 	int			unit;
     60  1.1  hamajima #if NGPIO > 0
     61  1.1  hamajima 	struct gpio_chipset_tag	gpio_chipset;
     62  1.1  hamajima 	gpio_pin_t		pins[EPGPIO_NPINS];
     63  1.2      kenh 	int			gpio_mask;
     64  1.2      kenh 	int			gpio_npins;
     65  1.1  hamajima #endif
     66  1.1  hamajima 	bus_size_t		pxdr;
     67  1.1  hamajima 	bus_size_t		pxddr;
     68  1.1  hamajima 	bus_size_t		xinten;
     69  1.1  hamajima 	bus_size_t		xinttype1;
     70  1.1  hamajima 	bus_size_t		xinttype2;
     71  1.1  hamajima 	bus_size_t		xeoi;
     72  1.1  hamajima 	bus_size_t		xdb;
     73  1.1  hamajima };
     74  1.1  hamajima 
     75  1.1  hamajima struct intr_req {
     76  1.1  hamajima 	int		irq;
     77  1.1  hamajima 	int		(*ih_func)(void *);
     78  1.1  hamajima 	int		(*ireq_func)(void *);
     79  1.1  hamajima 	void		*ireq_arg;
     80  1.1  hamajima 	void		*cookie;
     81  1.1  hamajima };
     82  1.1  hamajima 
     83  1.1  hamajima struct epgpio_softc {
     84  1.1  hamajima 	bus_space_tag_t		sc_iot;
     85  1.1  hamajima 	bus_space_handle_t	sc_ioh;
     86  1.1  hamajima 	struct port_info	sc_port[EPGPIO_NPORTS];
     87  1.1  hamajima 	struct intr_req		sc_ireq_combine;
     88  1.1  hamajima 	struct intr_req		sc_ireq_f[EPGPIO_NPINS];
     89  1.1  hamajima };
     90  1.1  hamajima 
     91  1.5       chs static int epgpio_match(device_t, cfdata_t, void *);
     92  1.5       chs static void epgpio_attach(device_t, device_t, void *);
     93  1.1  hamajima 
     94  1.1  hamajima #if NGPIO > 0
     95  1.1  hamajima static int epgpiobus_print(void *, const char *);
     96  1.1  hamajima static int epgpio_pin_read(void *, int);
     97  1.1  hamajima static void epgpio_pin_write(void *, int, int);
     98  1.1  hamajima static void epgpio_pin_ctl(void *, int, int);
     99  1.1  hamajima #endif
    100  1.1  hamajima 
    101  1.5       chs static int epgpio_search(device_t, cfdata_t, const int *, void *);
    102  1.1  hamajima static int epgpio_print(void *, const char *);
    103  1.1  hamajima 
    104  1.1  hamajima static int epgpio_intr_combine(void* arg);
    105  1.1  hamajima static int epgpio_intr_f(void* arg, int);
    106  1.1  hamajima static int epgpio_intr_0(void* arg);
    107  1.1  hamajima static int epgpio_intr_1(void* arg);
    108  1.1  hamajima static int epgpio_intr_2(void* arg);
    109  1.1  hamajima static int epgpio_intr_3(void* arg);
    110  1.1  hamajima static int epgpio_intr_4(void* arg);
    111  1.1  hamajima static int epgpio_intr_5(void* arg);
    112  1.1  hamajima static int epgpio_intr_6(void* arg);
    113  1.1  hamajima static int epgpio_intr_7(void* arg);
    114  1.1  hamajima 
    115  1.1  hamajima static void epgpio_bit_set(struct epgpio_softc *, bus_size_t, int);
    116  1.1  hamajima static void epgpio_bit_clear(struct epgpio_softc *, bus_size_t, int);
    117  1.1  hamajima 
    118  1.5       chs CFATTACH_DECL_NEW(epgpio, sizeof(struct epgpio_softc),
    119  1.1  hamajima 	      epgpio_match, epgpio_attach, NULL, NULL);
    120  1.1  hamajima 
    121  1.1  hamajima static int
    122  1.5       chs epgpio_match(device_t parent, cfdata_t match, void *aux)
    123  1.1  hamajima {
    124  1.1  hamajima 	return 2;
    125  1.1  hamajima }
    126  1.1  hamajima 
    127  1.1  hamajima static void
    128  1.5       chs epgpio_attach(device_t parent, device_t self, void *aux)
    129  1.1  hamajima {
    130  1.5       chs 	struct epgpio_softc *sc = device_private(self);
    131  1.1  hamajima 	struct epsoc_attach_args *sa = aux;
    132  1.1  hamajima 	struct port_info *pi;
    133  1.1  hamajima #if NGPIO > 0
    134  1.1  hamajima 	struct gpiobus_attach_args gba;
    135  1.1  hamajima 	int dir, val;
    136  1.2      kenh 	int i, j, pin;
    137  1.1  hamajima #endif
    138  1.1  hamajima 
    139  1.1  hamajima 	printf("\n");
    140  1.1  hamajima 	sc->sc_iot = sa->sa_iot;
    141  1.1  hamajima 
    142  1.1  hamajima 	if (bus_space_map(sa->sa_iot, sa->sa_addr,
    143  1.1  hamajima 			  sa->sa_size, 0, &sc->sc_ioh)){
    144  1.5       chs 		printf("%s: Cannot map registers", device_xname(self));
    145  1.1  hamajima 		return;
    146  1.1  hamajima 	}
    147  1.1  hamajima 
    148  1.1  hamajima 	/* PORT A */
    149  1.1  hamajima 	pi = &sc->sc_port[0];
    150  1.1  hamajima 	pi->unit = 0;
    151  1.1  hamajima 	pi->sc = sc;
    152  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PADR;
    153  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PADDR;
    154  1.1  hamajima 	pi->xinten = EP93XX_GPIO_AIntEn;
    155  1.1  hamajima 	pi->xinttype1 = EP93XX_GPIO_AIntType1;
    156  1.1  hamajima 	pi->xinttype2 = EP93XX_GPIO_AIntType2;
    157  1.1  hamajima 	pi->xeoi = EP93XX_GPIO_AEOI;
    158  1.1  hamajima 	pi->xdb = EP93XX_GPIO_ADB;
    159  1.2      kenh #if NGPIO > 0
    160  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_A_MASK;
    161  1.2      kenh #endif
    162  1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xinten, 0);
    163  1.1  hamajima 	/* PORT B */
    164  1.1  hamajima 	pi = &sc->sc_port[1];
    165  1.1  hamajima 	pi->unit = 1;
    166  1.1  hamajima 	pi->sc = sc;
    167  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PBDR;
    168  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PBDDR;
    169  1.1  hamajima 	pi->xinten = EP93XX_GPIO_BIntEn;
    170  1.1  hamajima 	pi->xinttype1 = EP93XX_GPIO_BIntType1;
    171  1.1  hamajima 	pi->xinttype2 = EP93XX_GPIO_BIntType2;
    172  1.1  hamajima 	pi->xeoi = EP93XX_GPIO_BEOI;
    173  1.1  hamajima 	pi->xdb = EP93XX_GPIO_BDB;
    174  1.2      kenh #if NGPIO > 0
    175  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_B_MASK;
    176  1.2      kenh #endif
    177  1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xinten, 0);
    178  1.1  hamajima 	/* PORT C */
    179  1.1  hamajima 	pi = &sc->sc_port[2];
    180  1.1  hamajima 	pi->unit = 2;
    181  1.1  hamajima 	pi->sc = sc;
    182  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PCDR;
    183  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PCDDR;
    184  1.1  hamajima 	pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
    185  1.2      kenh #if NGPIO > 0
    186  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_C_MASK;
    187  1.2      kenh #endif
    188  1.1  hamajima 	/* PORT D */
    189  1.1  hamajima 	pi = &sc->sc_port[3];
    190  1.1  hamajima 	pi->unit = 3;
    191  1.1  hamajima 	pi->sc = sc;
    192  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PDDR;
    193  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PDDDR;
    194  1.1  hamajima 	pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
    195  1.2      kenh #if NGPIO > 0
    196  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_D_MASK;
    197  1.2      kenh #endif
    198  1.1  hamajima 	/* PORT E */
    199  1.1  hamajima 	pi = &sc->sc_port[4];
    200  1.1  hamajima 	pi->unit = 4;
    201  1.1  hamajima 	pi->sc = sc;
    202  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PEDR;
    203  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PEDDR;
    204  1.1  hamajima 	pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
    205  1.2      kenh #if NGPIO > 0
    206  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_E_MASK;
    207  1.2      kenh #endif
    208  1.1  hamajima 	/* PORT F */
    209  1.1  hamajima 	pi = &sc->sc_port[5];
    210  1.1  hamajima 	pi->unit = 5;
    211  1.1  hamajima 	pi->sc = sc;
    212  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PFDR;
    213  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PFDDR;
    214  1.1  hamajima 	pi->xinten = EP93XX_GPIO_FIntEn;
    215  1.1  hamajima 	pi->xinttype1 = EP93XX_GPIO_FIntType1;
    216  1.1  hamajima 	pi->xinttype2 = EP93XX_GPIO_FIntType2;
    217  1.1  hamajima 	pi->xeoi = EP93XX_GPIO_FEOI;
    218  1.1  hamajima 	pi->xdb = EP93XX_GPIO_FDB;
    219  1.2      kenh #if NGPIO > 0
    220  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_F_MASK;
    221  1.2      kenh #endif
    222  1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xinten, 0);
    223  1.1  hamajima 	/* PORT G */
    224  1.1  hamajima 	pi = &sc->sc_port[6];
    225  1.1  hamajima 	pi->unit = 6;
    226  1.1  hamajima 	pi->sc = sc;
    227  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PGDR;
    228  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PGDDR;
    229  1.1  hamajima 	pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
    230  1.2      kenh #if NGPIO > 0
    231  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_G_MASK;
    232  1.2      kenh #endif
    233  1.1  hamajima 	/* PORT H */
    234  1.1  hamajima 	pi = &sc->sc_port[7];
    235  1.1  hamajima 	pi->unit = 7;
    236  1.1  hamajima 	pi->sc = sc;
    237  1.1  hamajima 	pi->pxdr = EP93XX_GPIO_PHDR;
    238  1.1  hamajima 	pi->pxddr = EP93XX_GPIO_PHDDR;
    239  1.1  hamajima 	pi->xinten = pi->xinttype1 = pi->xinttype2 = pi->xeoi = pi->xdb = -1;
    240  1.2      kenh #if NGPIO > 0
    241  1.2      kenh 	pi->gpio_mask = EPGPIO_PORT_H_MASK;
    242  1.2      kenh #endif
    243  1.1  hamajima 
    244  1.1  hamajima 	/* PORT A & B */
    245  1.1  hamajima 	sc->sc_ireq_combine.irq = EP93XX_GPIO_INTR;
    246  1.1  hamajima 	sc->sc_ireq_combine.ih_func = epgpio_intr_combine;
    247  1.1  hamajima 	/* PORT F */
    248  1.1  hamajima 	sc->sc_ireq_f[0].irq = EP93XX_GPIO0_INTR;
    249  1.1  hamajima 	sc->sc_ireq_f[0].ih_func = epgpio_intr_0;
    250  1.1  hamajima 	sc->sc_ireq_f[1].irq = EP93XX_GPIO1_INTR;
    251  1.1  hamajima 	sc->sc_ireq_f[1].ih_func = epgpio_intr_1;
    252  1.1  hamajima 	sc->sc_ireq_f[2].irq = EP93XX_GPIO2_INTR;
    253  1.1  hamajima 	sc->sc_ireq_f[2].ih_func = epgpio_intr_2;
    254  1.1  hamajima 	sc->sc_ireq_f[3].irq = EP93XX_GPIO3_INTR;
    255  1.1  hamajima 	sc->sc_ireq_f[3].ih_func = epgpio_intr_3;
    256  1.1  hamajima 	sc->sc_ireq_f[4].irq = EP93XX_GPIO4_INTR;
    257  1.1  hamajima 	sc->sc_ireq_f[4].ih_func = epgpio_intr_4;
    258  1.1  hamajima 	sc->sc_ireq_f[5].irq = EP93XX_GPIO5_INTR;
    259  1.1  hamajima 	sc->sc_ireq_f[5].ih_func = epgpio_intr_5;
    260  1.1  hamajima 	sc->sc_ireq_f[6].irq = EP93XX_GPIO6_INTR;
    261  1.1  hamajima 	sc->sc_ireq_f[6].ih_func = epgpio_intr_6;
    262  1.1  hamajima 	sc->sc_ireq_f[7].irq = EP93XX_GPIO7_INTR;
    263  1.1  hamajima 	sc->sc_ireq_f[7].ih_func = epgpio_intr_7;
    264  1.1  hamajima 
    265  1.1  hamajima #if NGPIO > 0
    266  1.1  hamajima 	/* initialize and attach gpio(4) */
    267  1.1  hamajima 	for (i = 0; i < EPGPIO_NPORTS; i++) {
    268  1.1  hamajima 		pi = &sc->sc_port[i];
    269  1.2      kenh 		/*
    270  1.2      kenh 		 * If this port is completely disabled for gpio attachment,
    271  1.2      kenh 		 * then skip it.
    272  1.2      kenh 		 */
    273  1.2      kenh 		if (pi->gpio_mask == 0x00)
    274  1.2      kenh 			continue;
    275  1.2      kenh 
    276  1.1  hamajima 		dir = bus_space_read_4(sc->sc_iot, sc->sc_ioh, pi->pxddr) & 0xff;
    277  1.1  hamajima 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, pi->pxdr) & 0xff;
    278  1.2      kenh 
    279  1.2      kenh 		/*
    280  1.2      kenh 		 * pin_num doesn't seem to be used for anything in the GPIO
    281  1.2      kenh 		 * code.  So we're going to use it to refer to the REAL pin
    282  1.2      kenh 		 * on the port.  Just to keep things straight below:
    283  1.2      kenh 		 *
    284  1.2      kenh 		 * pin - The pin number as seen by the GPIO code
    285  1.2      kenh 		 * j   - The ACTUAL pin on the port
    286  1.2      kenh 		 */
    287  1.2      kenh 
    288  1.2      kenh 		for (j = 0, pin = 0; j < EPGPIO_NPINS; j++) {
    289  1.2      kenh 			if (pi->gpio_mask & (1 << j)) {
    290  1.2      kenh 				pi->pins[pin].pin_num = j;
    291  1.2      kenh 				pi->pins[pin].pin_caps = (GPIO_PIN_INPUT
    292  1.2      kenh 							| GPIO_PIN_OUTPUT);
    293  1.2      kenh 				if((dir >> j) & 0x01)
    294  1.2      kenh 					pi->pins[pin].pin_flags =
    295  1.2      kenh 							GPIO_PIN_OUTPUT;
    296  1.2      kenh 				else
    297  1.2      kenh 					pi->pins[pin].pin_flags =
    298  1.2      kenh 						GPIO_PIN_INPUT;
    299  1.2      kenh 				if((val >> j) & 0x01)
    300  1.2      kenh 					pi->pins[pin].pin_state = GPIO_PIN_HIGH;
    301  1.2      kenh 				else
    302  1.2      kenh 					pi->pins[pin].pin_state = GPIO_PIN_LOW;
    303  1.2      kenh 				pin++;
    304  1.2      kenh 			}
    305  1.1  hamajima 		}
    306  1.1  hamajima 		pi->gpio_chipset.gp_cookie = pi;
    307  1.1  hamajima 		pi->gpio_chipset.gp_pin_read = epgpio_pin_read;
    308  1.1  hamajima 		pi->gpio_chipset.gp_pin_write = epgpio_pin_write;
    309  1.1  hamajima 		pi->gpio_chipset.gp_pin_ctl = epgpio_pin_ctl;
    310  1.1  hamajima 		gba.gba_gc = &pi->gpio_chipset;
    311  1.1  hamajima 		gba.gba_pins = pi->pins;
    312  1.2      kenh 		gba.gba_npins = pin;
    313  1.6   thorpej 		config_found(self, &gba, epgpiobus_print,
    314  1.7   thorpej 		    CFARGS(.iattr = "gpiobus"));
    315  1.1  hamajima 	}
    316  1.1  hamajima #endif
    317  1.1  hamajima 
    318  1.6   thorpej 	config_search(self, NULL,
    319  1.7   thorpej 	    CFARGS(.search = epgpio_search,
    320  1.7   thorpej 		   .iattr = "epgpio"));
    321  1.1  hamajima }
    322  1.1  hamajima 
    323  1.1  hamajima #if NGPIO > 0
    324  1.1  hamajima static int
    325  1.1  hamajima epgpiobus_print(void *aux, const char *name)
    326  1.1  hamajima {
    327  1.1  hamajima 	struct gpiobus_attach_args *gba = aux;
    328  1.1  hamajima 	struct port_info *pi = (struct port_info *)gba->gba_gc->gp_cookie;
    329  1.1  hamajima 
    330  1.1  hamajima 	gpiobus_print(aux, name);
    331  1.1  hamajima 	aprint_normal(": port %c", pi->unit+'A');
    332  1.1  hamajima 
    333  1.1  hamajima 	return (UNCONF);
    334  1.1  hamajima }
    335  1.1  hamajima #endif
    336  1.1  hamajima 
    337  1.1  hamajima 
    338  1.1  hamajima static int
    339  1.5       chs epgpio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    340  1.1  hamajima {
    341  1.5       chs 	struct epgpio_softc *sc = device_private(parent);
    342  1.1  hamajima 	struct epgpio_attach_args ga;
    343  1.1  hamajima 
    344  1.1  hamajima 	ga.ga_gc = sc;
    345  1.1  hamajima 	ga.ga_iot = sc->sc_iot;
    346  1.1  hamajima 	ga.ga_port = cf->cf_loc[EPGPIOCF_PORT];
    347  1.1  hamajima 	ga.ga_bit1 = cf->cf_loc[EPGPIOCF_BIT1];
    348  1.1  hamajima 	ga.ga_bit2 = cf->cf_loc[EPGPIOCF_BIT2];
    349  1.1  hamajima 
    350  1.6   thorpej 	if (config_probe(parent, cf, &ga))
    351  1.7   thorpej 		config_attach(parent, cf, &ga, epgpio_print, CFARGS_NONE);
    352  1.1  hamajima 
    353  1.1  hamajima 	return 0;
    354  1.1  hamajima }
    355  1.1  hamajima 
    356  1.1  hamajima static int
    357  1.1  hamajima epgpio_print(void *aux, const char *name)
    358  1.1  hamajima {
    359  1.5       chs 	struct epgpio_attach_args *ga = aux;
    360  1.5       chs 	struct epgpio_softc *sc = ga->ga_gc;
    361  1.1  hamajima 
    362  1.1  hamajima 	aprint_normal(":");
    363  1.1  hamajima 	if (ga->ga_port > -1)
    364  1.1  hamajima 		aprint_normal(" port %c", sc->sc_port[ga->ga_port].unit+'A');
    365  1.1  hamajima 	if (ga->ga_bit1 > -1)
    366  1.1  hamajima 		aprint_normal(" bit1 %d", ga->ga_bit1);
    367  1.1  hamajima 	if (ga->ga_bit2 > -1)
    368  1.1  hamajima 		aprint_normal(" bit2 %d", ga->ga_bit2);
    369  1.1  hamajima 
    370  1.1  hamajima 	return (UNCONF);
    371  1.1  hamajima }
    372  1.1  hamajima 
    373  1.1  hamajima int
    374  1.1  hamajima epgpio_read(struct epgpio_softc *sc, epgpio_port port, int bit)
    375  1.1  hamajima {
    376  1.1  hamajima 	struct port_info *pi = &sc->sc_port[port];
    377  1.1  hamajima 
    378  1.1  hamajima #if NGPIO > 0
    379  1.1  hamajima 	pi->pins[bit].pin_caps = 0;
    380  1.1  hamajima #endif
    381  1.1  hamajima 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, pi->pxdr) >> bit) & 1;
    382  1.1  hamajima }
    383  1.1  hamajima 
    384  1.1  hamajima void
    385  1.1  hamajima epgpio_set(struct epgpio_softc *sc, epgpio_port port, int bit)
    386  1.1  hamajima {
    387  1.1  hamajima 	struct port_info *pi = &sc->sc_port[port];
    388  1.1  hamajima 
    389  1.1  hamajima #if NGPIO > 0
    390  1.1  hamajima 	pi->pins[bit].pin_caps = 0;
    391  1.1  hamajima #endif
    392  1.1  hamajima 	epgpio_bit_set(sc, pi->pxdr, bit);
    393  1.1  hamajima }
    394  1.1  hamajima 
    395  1.1  hamajima void
    396  1.1  hamajima epgpio_clear(struct epgpio_softc *sc, epgpio_port port, int bit)
    397  1.1  hamajima {
    398  1.1  hamajima 	struct port_info *pi = &sc->sc_port[port];
    399  1.1  hamajima 
    400  1.1  hamajima #if NGPIO > 0
    401  1.1  hamajima 	pi->pins[bit].pin_caps = 0;
    402  1.1  hamajima #endif
    403  1.1  hamajima 	epgpio_bit_clear(sc, pi->pxdr, bit);
    404  1.1  hamajima }
    405  1.1  hamajima 
    406  1.1  hamajima void
    407  1.1  hamajima epgpio_in(struct epgpio_softc *sc, epgpio_port port, int bit)
    408  1.1  hamajima {
    409  1.1  hamajima 	struct port_info *pi = &sc->sc_port[port];
    410  1.1  hamajima 
    411  1.1  hamajima #if NGPIO > 0
    412  1.1  hamajima 	pi->pins[bit].pin_caps = 0;
    413  1.1  hamajima #endif
    414  1.1  hamajima 	epgpio_bit_clear(sc, pi->pxddr, bit);
    415  1.1  hamajima }
    416  1.1  hamajima 
    417  1.1  hamajima void
    418  1.1  hamajima epgpio_out(struct epgpio_softc *sc, epgpio_port port, int bit)
    419  1.1  hamajima {
    420  1.1  hamajima 	struct port_info *pi = &sc->sc_port[port];
    421  1.1  hamajima 
    422  1.1  hamajima #if NGPIO > 0
    423  1.1  hamajima 	pi->pins[bit].pin_caps = 0;
    424  1.1  hamajima #endif
    425  1.1  hamajima 	epgpio_bit_set(sc, pi->pxddr, bit);
    426  1.1  hamajima }
    427  1.1  hamajima 
    428  1.1  hamajima void *
    429  1.1  hamajima epgpio_intr_establish(struct epgpio_softc *sc, epgpio_port port, int bit,
    430  1.1  hamajima 		      int flag, int ipl, int (*ireq_func)(void *), void *arg) {
    431  1.1  hamajima 	struct port_info *pi;
    432  1.1  hamajima 	struct intr_req *intq;
    433  1.1  hamajima 
    434  1.1  hamajima 	DPRINTFN(1, ("epgpio_intr_establish: port=%d, bit=%d, flag=%#x\n",port,bit,flag));
    435  1.1  hamajima 
    436  1.1  hamajima 	if (bit < 0 || bit >= EPGPIO_NPINS)
    437  1.1  hamajima 		return 0;
    438  1.1  hamajima 
    439  1.1  hamajima 	switch (port) {
    440  1.1  hamajima 	case PORT_A:
    441  1.1  hamajima 	case PORT_B:
    442  1.1  hamajima 		intq = &sc->sc_ireq_combine;
    443  1.1  hamajima 		break;
    444  1.1  hamajima 	case PORT_F:
    445  1.1  hamajima 		intq = &sc->sc_ireq_f[bit];
    446  1.1  hamajima 		break;
    447  1.1  hamajima 	default:
    448  1.1  hamajima 		return 0;
    449  1.1  hamajima 	};
    450  1.1  hamajima 
    451  1.1  hamajima 	if (intq->ireq_func)
    452  1.1  hamajima 		return 0;	/* already used */
    453  1.1  hamajima 
    454  1.1  hamajima 	intq->ireq_func = ireq_func;
    455  1.1  hamajima 	intq->ireq_arg = arg;
    456  1.1  hamajima 
    457  1.1  hamajima 	pi = &sc->sc_port[port];
    458  1.1  hamajima 	epgpio_bit_clear(sc, pi->xinten, bit);
    459  1.1  hamajima 	epgpio_in(sc, port, bit);
    460  1.1  hamajima #if NGPIO > 0
    461  1.1  hamajima 	pi->pins[bit].pin_caps = 0;
    462  1.1  hamajima #endif
    463  1.1  hamajima 
    464  1.1  hamajima 	if (flag & EDGE_TRIGGER)
    465  1.1  hamajima 		epgpio_bit_set(sc, pi->xinttype1, bit);
    466  1.1  hamajima 	else	/* LEVEL_SENSE */
    467  1.1  hamajima 		epgpio_bit_clear(sc, pi->xinttype1, bit);
    468  1.1  hamajima 	if (flag & RISING_EDGE)	/* or HIGH_LEVEL */
    469  1.1  hamajima 		epgpio_bit_set(sc, pi->xinttype2, bit);
    470  1.1  hamajima 	else	/* FALLING_EDGE or LOW_LEVEL */
    471  1.1  hamajima 		epgpio_bit_clear(sc, pi->xinttype2, bit);
    472  1.1  hamajima 	if (flag & DEBOUNCE)
    473  1.1  hamajima 		epgpio_bit_set(sc, pi->xdb, bit);
    474  1.1  hamajima 	else
    475  1.1  hamajima 		epgpio_bit_clear(sc, pi->xdb, bit);
    476  1.1  hamajima 
    477  1.1  hamajima 	if (!intq->cookie)
    478  1.1  hamajima 		intq->cookie = ep93xx_intr_establish(intq->irq, ipl,
    479  1.1  hamajima 						     intq->ih_func, pi);
    480  1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, pi->xeoi, 1 << bit);
    481  1.1  hamajima 	epgpio_bit_set(sc, pi->xinten, bit);
    482  1.1  hamajima 	return intq->cookie;
    483  1.1  hamajima }
    484  1.1  hamajima 
    485  1.1  hamajima void
    486  1.1  hamajima epgpio_intr_disestablish(struct epgpio_softc *sc, epgpio_port port, int bit)
    487  1.1  hamajima {
    488  1.1  hamajima 	struct port_info *pi;
    489  1.1  hamajima 	struct intr_req *intq;
    490  1.1  hamajima 
    491  1.1  hamajima 	DPRINTFN(1, ("epgpio_intr_disestablish: port=%d, bit=%d\n",port,bit));
    492  1.1  hamajima 
    493  1.1  hamajima 	if (bit < 0 || bit >= EPGPIO_NPINS)
    494  1.1  hamajima 		return;
    495  1.1  hamajima 
    496  1.1  hamajima 	switch (port) {
    497  1.1  hamajima 	case PORT_A:
    498  1.1  hamajima 	case PORT_B:
    499  1.1  hamajima 		intq = &sc->sc_ireq_combine;
    500  1.1  hamajima 		break;
    501  1.1  hamajima 	case PORT_F:
    502  1.1  hamajima 		intq = &sc->sc_ireq_f[bit];
    503  1.1  hamajima 		break;
    504  1.1  hamajima 	default:
    505  1.1  hamajima 		return;
    506  1.1  hamajima 	};
    507  1.1  hamajima 
    508  1.1  hamajima 	if (!intq->ireq_func)
    509  1.1  hamajima 		return;
    510  1.1  hamajima 
    511  1.1  hamajima 	pi = &sc->sc_port[port];
    512  1.1  hamajima 	epgpio_bit_clear(sc, pi->xinten, bit);
    513  1.1  hamajima 	intq->ireq_func = 0;
    514  1.1  hamajima 	intq->ireq_arg = 0;
    515  1.1  hamajima 	ep93xx_intr_disestablish(intq->cookie);
    516  1.1  hamajima 	intq->cookie = 0;
    517  1.1  hamajima }
    518  1.1  hamajima 
    519  1.1  hamajima static int
    520  1.1  hamajima epgpio_intr_combine(void *arg)
    521  1.1  hamajima {
    522  1.1  hamajima 	struct port_info *pi = arg;
    523  1.1  hamajima 	struct epgpio_softc *sc = pi->sc;
    524  1.1  hamajima 	struct intr_req *intq = &sc->sc_ireq_combine;
    525  1.1  hamajima 	int err = 0;
    526  1.1  hamajima 
    527  1.1  hamajima 	DPRINTFN(1, ("epgpio_intr_combine\n"));
    528  1.1  hamajima 
    529  1.1  hamajima 	if (intq->ireq_func)
    530  1.1  hamajima 		err = (*intq->ireq_func)(intq->ireq_arg);
    531  1.1  hamajima 	epgpio_bit_set(sc, pi->xeoi, 0xff);
    532  1.1  hamajima 	return err;
    533  1.1  hamajima }
    534  1.1  hamajima 
    535  1.1  hamajima static int
    536  1.1  hamajima epgpio_intr_f(void *arg, int bit)
    537  1.1  hamajima {
    538  1.1  hamajima 	struct port_info *pi = arg;
    539  1.1  hamajima 	struct epgpio_softc *sc = pi->sc;
    540  1.1  hamajima 	struct intr_req *intq = &sc->sc_ireq_f[bit];
    541  1.1  hamajima 	int err = 0;
    542  1.1  hamajima 
    543  1.1  hamajima 	DPRINTFN(1, ("epgpio_intr_%d\n", bit));
    544  1.1  hamajima 
    545  1.1  hamajima 	if (intq->ireq_func)
    546  1.1  hamajima 		err = (*intq->ireq_func)(intq->ireq_arg);
    547  1.1  hamajima 	epgpio_bit_set(sc, pi->xeoi, bit);
    548  1.1  hamajima 	return err;
    549  1.1  hamajima }
    550  1.1  hamajima 
    551  1.1  hamajima static int
    552  1.1  hamajima epgpio_intr_0(void *arg)
    553  1.1  hamajima {
    554  1.1  hamajima 	return epgpio_intr_f(arg, 0);
    555  1.1  hamajima }
    556  1.1  hamajima 
    557  1.1  hamajima static int
    558  1.1  hamajima epgpio_intr_1(void *arg)
    559  1.1  hamajima {
    560  1.1  hamajima 	return epgpio_intr_f(arg, 1);
    561  1.1  hamajima }
    562  1.1  hamajima 
    563  1.1  hamajima static int
    564  1.1  hamajima epgpio_intr_2(void *arg)
    565  1.1  hamajima {
    566  1.1  hamajima 	return epgpio_intr_f(arg, 2);
    567  1.1  hamajima }
    568  1.1  hamajima 
    569  1.1  hamajima static int
    570  1.1  hamajima epgpio_intr_3(void *arg)
    571  1.1  hamajima {
    572  1.1  hamajima 	return epgpio_intr_f(arg, 3);
    573  1.1  hamajima }
    574  1.1  hamajima 
    575  1.1  hamajima static int
    576  1.1  hamajima epgpio_intr_4(void *arg)
    577  1.1  hamajima {
    578  1.1  hamajima 	return epgpio_intr_f(arg, 4);
    579  1.1  hamajima }
    580  1.1  hamajima 
    581  1.1  hamajima static int
    582  1.1  hamajima epgpio_intr_5(void *arg)
    583  1.1  hamajima {
    584  1.1  hamajima 	return epgpio_intr_f(arg, 5);
    585  1.1  hamajima }
    586  1.1  hamajima 
    587  1.1  hamajima static int
    588  1.1  hamajima epgpio_intr_6(void *arg)
    589  1.1  hamajima {
    590  1.1  hamajima 	return epgpio_intr_f(arg, 6);
    591  1.1  hamajima }
    592  1.1  hamajima 
    593  1.1  hamajima static int
    594  1.1  hamajima epgpio_intr_7(void *arg)
    595  1.1  hamajima {
    596  1.1  hamajima 	return epgpio_intr_f(arg, 7);
    597  1.1  hamajima }
    598  1.1  hamajima 
    599  1.1  hamajima #if NGPIO > 0
    600  1.1  hamajima static int
    601  1.1  hamajima epgpio_pin_read(void *arg, int pin)
    602  1.1  hamajima {
    603  1.1  hamajima 	struct port_info *pi = arg;
    604  1.1  hamajima 	struct epgpio_softc *sc = pi->sc;
    605  1.1  hamajima 
    606  1.2      kenh 	pin %= pi->gpio_npins;
    607  1.1  hamajima 	if (!pi->pins[pin].pin_caps)
    608  1.1  hamajima 		return 0; /* EBUSY? */
    609  1.1  hamajima 
    610  1.1  hamajima 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    611  1.2      kenh 				 pi->pxdr) >> pi->pins[pin].pin_num) & 1;
    612  1.1  hamajima }
    613  1.1  hamajima 
    614  1.1  hamajima static void
    615  1.1  hamajima epgpio_pin_write(void *arg, int pin, int val)
    616  1.1  hamajima {
    617  1.1  hamajima 	struct port_info *pi = arg;
    618  1.1  hamajima 	struct epgpio_softc *sc = pi->sc;
    619  1.1  hamajima 
    620  1.2      kenh 	pin %= pi->gpio_npins;
    621  1.1  hamajima 	if (!pi->pins[pin].pin_caps)
    622  1.1  hamajima 		return;
    623  1.1  hamajima 
    624  1.1  hamajima 	if (val)
    625  1.2      kenh 		epgpio_bit_set(sc, pi->pxdr, pi->pins[pin].pin_num);
    626  1.1  hamajima 	else
    627  1.2      kenh 		epgpio_bit_clear(sc, pi->pxdr, pi->pins[pin].pin_num);
    628  1.1  hamajima }
    629  1.1  hamajima 
    630  1.1  hamajima static void
    631  1.1  hamajima epgpio_pin_ctl(void *arg, int pin, int flags)
    632  1.1  hamajima {
    633  1.1  hamajima 	struct port_info *pi = arg;
    634  1.1  hamajima 	struct epgpio_softc *sc = pi->sc;
    635  1.1  hamajima 
    636  1.2      kenh 	pin %= pi->gpio_npins;
    637  1.1  hamajima 	if (!pi->pins[pin].pin_caps)
    638  1.1  hamajima 		return;
    639  1.1  hamajima 
    640  1.1  hamajima 	if (flags & GPIO_PIN_INPUT)
    641  1.2      kenh 		epgpio_bit_clear(sc, pi->pxddr, pi->pins[pin].pin_num);
    642  1.1  hamajima 	else if (flags & GPIO_PIN_OUTPUT)
    643  1.2      kenh 		epgpio_bit_set(sc, pi->pxddr, pi->pins[pin].pin_num);
    644  1.1  hamajima }
    645  1.1  hamajima #endif
    646  1.1  hamajima 
    647  1.1  hamajima static void
    648  1.1  hamajima epgpio_bit_set(struct epgpio_softc *sc, bus_size_t reg, int bit)
    649  1.1  hamajima {
    650  1.1  hamajima 	int t = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg) & 0xff;
    651  1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, t | (1 << bit));
    652  1.1  hamajima }
    653  1.1  hamajima 
    654  1.1  hamajima static void
    655  1.1  hamajima epgpio_bit_clear(struct epgpio_softc *sc, bus_size_t reg, int bit)
    656  1.1  hamajima {
    657  1.1  hamajima 	int t = bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg) & 0xff;
    658  1.1  hamajima 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, t & ~(1 << bit));
    659  1.1  hamajima }
    660