Home | History | Annotate | Line # | Download | only in imx
imxgpio.c revision 1.4
      1  1.4  hkenken /*	$NetBSD: imxgpio.c,v 1.4 2014/03/22 05:19:18 hkenken Exp $ */
      2  1.1      bsh 
      3  1.1      bsh /*-
      4  1.1      bsh  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1      bsh  * All rights reserved.
      6  1.1      bsh  *
      7  1.1      bsh  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1      bsh  * by Matt Thomas
      9  1.1      bsh  *
     10  1.1      bsh  * Redistribution and use in source and binary forms, with or without
     11  1.1      bsh  * modification, are permitted provided that the following conditions
     12  1.1      bsh  * are met:
     13  1.1      bsh  * 1. Redistributions of source code must retain the above copyright
     14  1.1      bsh  *    notice, this list of conditions and the following disclaimer.
     15  1.1      bsh  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      bsh  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      bsh  *    documentation and/or other materials provided with the distribution.
     18  1.1      bsh  *
     19  1.1      bsh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1      bsh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1      bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1      bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1      bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1      bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1      bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1      bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1      bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1      bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1      bsh  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1      bsh  */
     31  1.1      bsh #include <sys/cdefs.h>
     32  1.4  hkenken __KERNEL_RCSID(0, "$NetBSD: imxgpio.c,v 1.4 2014/03/22 05:19:18 hkenken Exp $");
     33  1.1      bsh 
     34  1.1      bsh #define	_INTR_PRIVATE
     35  1.1      bsh 
     36  1.1      bsh #include "locators.h"
     37  1.1      bsh #include "gpio.h"
     38  1.4  hkenken #include "opt_imxgpio.h"
     39  1.1      bsh 
     40  1.1      bsh #include <sys/param.h>
     41  1.1      bsh #include <sys/evcnt.h>
     42  1.1      bsh #include <sys/atomic.h>
     43  1.1      bsh 
     44  1.1      bsh #include <uvm/uvm_extern.h>
     45  1.1      bsh 
     46  1.1      bsh #include <machine/intr.h>
     47  1.1      bsh 
     48  1.1      bsh #include <arm/cpu.h>
     49  1.1      bsh #include <arm/armreg.h>
     50  1.1      bsh #include <arm/cpufunc.h>
     51  1.1      bsh 
     52  1.2   dyoung #include <sys/bus.h>
     53  1.1      bsh 
     54  1.1      bsh #include <arm/imx/imx31reg.h>
     55  1.1      bsh #include <arm/imx/imx31var.h>
     56  1.1      bsh #include <arm/imx/imxgpioreg.h>
     57  1.1      bsh #include <arm/pic/picvar.h>
     58  1.1      bsh 
     59  1.1      bsh #include <arm/imx/imxgpioreg.h>
     60  1.1      bsh #include <arm/imx/imxgpiovar.h>
     61  1.1      bsh 
     62  1.1      bsh #if NGPIO > 0
     63  1.1      bsh /* GPIO access from userland */
     64  1.1      bsh #include <sys/gpio.h>
     65  1.1      bsh #include <dev/gpio/gpiovar.h>
     66  1.1      bsh #endif
     67  1.1      bsh 
     68  1.1      bsh #define	MAX_NGROUP	4
     69  1.1      bsh 
     70  1.1      bsh static void gpio_pic_block_irqs(struct pic_softc *, size_t, uint32_t);
     71  1.1      bsh static void gpio_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     72  1.1      bsh static int gpio_pic_find_pending_irqs(struct pic_softc *);
     73  1.1      bsh static void gpio_pic_establish_irq(struct pic_softc *, struct intrsource *);
     74  1.1      bsh 
     75  1.1      bsh const struct pic_ops gpio_pic_ops = {
     76  1.1      bsh 	.pic_unblock_irqs = gpio_pic_unblock_irqs,
     77  1.1      bsh 	.pic_block_irqs = gpio_pic_block_irqs,
     78  1.1      bsh 	.pic_find_pending_irqs = gpio_pic_find_pending_irqs,
     79  1.1      bsh 	.pic_establish_irq = gpio_pic_establish_irq,
     80  1.1      bsh 	.pic_source_name = NULL
     81  1.1      bsh };
     82  1.1      bsh 
     83  1.1      bsh struct gpio_softc {
     84  1.1      bsh 	device_t gpio_dev;
     85  1.1      bsh 	struct pic_softc gpio_pic;
     86  1.4  hkenken #if defined(IMX_GPIO_INTR_SPLIT)
     87  1.4  hkenken 	struct intrsource *gpio_is_0_15;
     88  1.4  hkenken 	struct intrsource *gpio_is_16_31;
     89  1.4  hkenken #else
     90  1.1      bsh 	struct intrsource *gpio_is;
     91  1.4  hkenken #endif
     92  1.1      bsh 	bus_space_tag_t gpio_memt;
     93  1.1      bsh 	bus_space_handle_t gpio_memh;
     94  1.1      bsh 	uint32_t gpio_enable_mask;
     95  1.1      bsh 	uint32_t gpio_edge_mask;
     96  1.1      bsh 	uint32_t gpio_level_mask;
     97  1.1      bsh #if NGPIO > 0
     98  1.1      bsh 	struct gpio_chipset_tag gpio_chipset;
     99  1.1      bsh 	gpio_pin_t gpio_pins[32];
    100  1.1      bsh #endif
    101  1.1      bsh };
    102  1.1      bsh 
    103  1.1      bsh static struct {
    104  1.1      bsh 	bus_space_tag_t	iot;
    105  1.1      bsh 	struct {
    106  1.1      bsh 		bus_space_handle_t ioh;
    107  1.1      bsh 		struct gpio_softc *softc;
    108  1.1      bsh 	} unit[MAX_NGROUP];
    109  1.1      bsh } gpio_handles;
    110  1.1      bsh 
    111  1.1      bsh extern struct cfdriver imxgpio_cd;
    112  1.1      bsh 
    113  1.1      bsh CFATTACH_DECL_NEW(imxgpio,
    114  1.1      bsh 	sizeof(struct gpio_softc),
    115  1.1      bsh 	imxgpio_match, imxgpio_attach,
    116  1.1      bsh 	NULL, NULL);
    117  1.1      bsh 
    118  1.1      bsh 
    119  1.1      bsh #define	PIC_TO_SOFTC(pic) \
    120  1.1      bsh 	((struct gpio_softc *)((char *)(pic) - \
    121  1.1      bsh 		offsetof(struct gpio_softc, gpio_pic)))
    122  1.1      bsh 
    123  1.1      bsh #define	GPIO_READ(gpio, reg) \
    124  1.1      bsh 	bus_space_read_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg))
    125  1.1      bsh #define	GPIO_WRITE(gpio, reg, val) \
    126  1.1      bsh 	bus_space_write_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg), (val))
    127  1.1      bsh 
    128  1.1      bsh void
    129  1.1      bsh gpio_pic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    130  1.1      bsh {
    131  1.1      bsh 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    132  1.1      bsh 	KASSERT(irq_base == 0);
    133  1.1      bsh 
    134  1.1      bsh 	gpio->gpio_enable_mask |= irq_mask;
    135  1.1      bsh 
    136  1.1      bsh 	GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
    137  1.1      bsh 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    138  1.1      bsh }
    139  1.1      bsh 
    140  1.1      bsh void
    141  1.1      bsh gpio_pic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    142  1.1      bsh {
    143  1.1      bsh 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    144  1.1      bsh 	KASSERT(irq_base == 0);
    145  1.1      bsh 
    146  1.1      bsh 	gpio->gpio_enable_mask &= ~irq_mask;
    147  1.1      bsh 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    148  1.1      bsh }
    149  1.1      bsh 
    150  1.1      bsh int
    151  1.1      bsh gpio_pic_find_pending_irqs(struct pic_softc *pic)
    152  1.1      bsh {
    153  1.1      bsh 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    154  1.1      bsh 	uint32_t v;
    155  1.1      bsh 	uint32_t pending;
    156  1.1      bsh 
    157  1.1      bsh 	v = GPIO_READ(gpio, GPIO_ISR);
    158  1.1      bsh 	pending = (v & gpio->gpio_enable_mask);
    159  1.1      bsh 	if (pending == 0)
    160  1.1      bsh 		return 0;
    161  1.1      bsh 
    162  1.1      bsh 	/*
    163  1.1      bsh 	 * Disable the pending interrupts.
    164  1.1      bsh 	 */
    165  1.1      bsh 	gpio->gpio_enable_mask &= ~pending;
    166  1.1      bsh 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    167  1.1      bsh 
    168  1.1      bsh 	/*
    169  1.1      bsh 	 * If any of the sources are edge triggered, ack them now so
    170  1.1      bsh 	 * we won't lose them.
    171  1.1      bsh 	 */
    172  1.1      bsh 	if (v & gpio->gpio_edge_mask)
    173  1.1      bsh 		GPIO_WRITE(gpio, GPIO_ISR, v & gpio->gpio_edge_mask);
    174  1.1      bsh 
    175  1.1      bsh 	/*
    176  1.1      bsh 	 * Now find all the pending bits and mark them as pending.
    177  1.1      bsh 	 */
    178  1.1      bsh 	do {
    179  1.1      bsh 		int irq;
    180  1.1      bsh 		KASSERT(pending != 0);
    181  1.1      bsh 		irq = 31 - __builtin_clz(pending);
    182  1.1      bsh 		pending &= ~__BIT(irq);
    183  1.4  hkenken 
    184  1.4  hkenken 		const struct intrsource *is = pic->pic_sources[irq];
    185  1.4  hkenken 		if (is->is_type == IST_EDGE_BOTH) {
    186  1.4  hkenken 			/*
    187  1.4  hkenken 			 * for both edge
    188  1.4  hkenken 			 */
    189  1.4  hkenken 			uint32_t icr_reg = GPIO_ICR1 + ((is->is_irq & 0x10) >> 2);
    190  1.4  hkenken 			v = GPIO_READ(gpio, icr_reg);
    191  1.4  hkenken 			uint32_t icr_shift = (is->is_irq & 0x0f) << 1;
    192  1.4  hkenken 			uint32_t mask = (3 << icr_shift);
    193  1.4  hkenken 			int gtype = __SHIFTOUT(v, mask);
    194  1.4  hkenken 			if (gtype == GPIO_ICR_EDGE_RISING)
    195  1.4  hkenken 				gtype = GPIO_ICR_EDGE_FALLING;
    196  1.4  hkenken 			else if (gtype == GPIO_ICR_EDGE_FALLING)
    197  1.4  hkenken 				gtype = GPIO_ICR_EDGE_RISING;
    198  1.4  hkenken 			v &= ~mask;
    199  1.4  hkenken 			v |= __SHIFTIN(gtype, mask);
    200  1.4  hkenken 			GPIO_WRITE(gpio, icr_reg, v);
    201  1.4  hkenken 		}
    202  1.1      bsh 		pic_mark_pending(&gpio->gpio_pic, irq);
    203  1.1      bsh 	} while (pending != 0);
    204  1.1      bsh 
    205  1.1      bsh 	return 1;
    206  1.1      bsh }
    207  1.1      bsh 
    208  1.1      bsh #define	GPIO_TYPEMAP \
    209  1.1      bsh 	((GPIO_ICR_LEVEL_LOW << (2*IST_LEVEL_LOW)) | \
    210  1.1      bsh 	 (GPIO_ICR_LEVEL_HIGH << (2*IST_LEVEL_HIGH)) | \
    211  1.1      bsh 	 (GPIO_ICR_EDGE_RISING << (2*IST_EDGE_RISING)) | \
    212  1.4  hkenken 	 (GPIO_ICR_EDGE_FALLING << (2*IST_EDGE_FALLING)) | \
    213  1.4  hkenken 	 (GPIO_ICR_EDGE_RISING << (2*IST_EDGE_BOTH)))
    214  1.1      bsh 
    215  1.1      bsh void
    216  1.1      bsh gpio_pic_establish_irq(struct pic_softc *pic, struct intrsource *is)
    217  1.1      bsh {
    218  1.1      bsh 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    219  1.1      bsh 	KASSERT(is->is_irq < 32);
    220  1.1      bsh 	uint32_t irq_mask = __BIT(is->is_irq);
    221  1.1      bsh 	uint32_t v;
    222  1.1      bsh 	unsigned int icr_shift, icr_reg;
    223  1.1      bsh 	unsigned int gtype;
    224  1.1      bsh 
    225  1.1      bsh 	/*
    226  1.1      bsh 	 * Make sure the irq isn't enabled and not asserting.
    227  1.1      bsh 	 */
    228  1.1      bsh 	gpio->gpio_enable_mask &= ~irq_mask;
    229  1.1      bsh 	GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
    230  1.1      bsh 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    231  1.1      bsh 	/*
    232  1.1      bsh 	 * Convert the type to a gpio type and figure out which bits in what
    233  1.1      bsh 	 * register we have to tweak.
    234  1.1      bsh 	 */
    235  1.1      bsh 	gtype = (GPIO_TYPEMAP >> (2 * is->is_type)) & 3;
    236  1.1      bsh 	icr_shift = (is->is_irq & 0x0f) << 1;
    237  1.1      bsh 	icr_reg = GPIO_ICR1 + ((is->is_irq & 0x10) >> 2);
    238  1.1      bsh 
    239  1.1      bsh 	/*
    240  1.1      bsh 	 * Set the interrupt type.
    241  1.1      bsh 	 */
    242  1.1      bsh 	v = GPIO_READ(gpio, icr_reg);
    243  1.1      bsh 	v &= ~(3 << icr_shift);
    244  1.1      bsh 	v |= gtype << icr_shift;
    245  1.1      bsh 	GPIO_WRITE(gpio, icr_reg, v);
    246  1.1      bsh 
    247  1.1      bsh 	/*
    248  1.1      bsh 	 * Mark it as input.
    249  1.1      bsh 	 */
    250  1.1      bsh 	v = GPIO_READ(gpio, GPIO_DIR);
    251  1.1      bsh 	v &= ~irq_mask;
    252  1.1      bsh 	GPIO_WRITE(gpio, GPIO_DIR, v);
    253  1.1      bsh 
    254  1.1      bsh 	/*
    255  1.1      bsh 	 * Now record the type of interrupt.
    256  1.1      bsh 	 */
    257  1.1      bsh 	if (gtype == GPIO_ICR_EDGE_RISING || gtype == GPIO_ICR_EDGE_FALLING) {
    258  1.1      bsh 		gpio->gpio_edge_mask |= irq_mask;
    259  1.1      bsh 		gpio->gpio_level_mask &= ~irq_mask;
    260  1.1      bsh 	} else {
    261  1.1      bsh 		gpio->gpio_edge_mask &= ~irq_mask;
    262  1.1      bsh 		gpio->gpio_level_mask |= irq_mask;
    263  1.1      bsh 	}
    264  1.1      bsh }
    265  1.1      bsh 
    266  1.1      bsh #if NGPIO > 0
    267  1.1      bsh 
    268  1.1      bsh static int
    269  1.1      bsh imxgpio_pin_read(void *arg, int pin)
    270  1.1      bsh {
    271  1.1      bsh 	struct gpio_softc * const gpio = arg;
    272  1.1      bsh 
    273  1.1      bsh 	return (GPIO_READ(gpio, GPIO_DR) >> pin) & 1;
    274  1.1      bsh }
    275  1.1      bsh 
    276  1.1      bsh static void
    277  1.1      bsh imxgpio_pin_write(void *arg, int pin, int value)
    278  1.1      bsh {
    279  1.1      bsh 	struct gpio_softc * const gpio = arg;
    280  1.1      bsh 	uint32_t mask = 1 << pin;
    281  1.1      bsh 	uint32_t old, new;
    282  1.1      bsh 
    283  1.1      bsh 	old = GPIO_READ(gpio, GPIO_DR);
    284  1.1      bsh 	if (value)
    285  1.1      bsh 		new = old | mask;
    286  1.1      bsh 	else
    287  1.1      bsh 		new = old & ~mask;
    288  1.1      bsh 
    289  1.1      bsh 	if (old != new)
    290  1.1      bsh 		GPIO_WRITE(gpio, GPIO_DR, new);
    291  1.1      bsh }
    292  1.1      bsh 
    293  1.1      bsh static void
    294  1.1      bsh imxgpio_pin_ctl(void *arg, int pin, int flags)
    295  1.1      bsh {
    296  1.1      bsh 	struct gpio_softc * const gpio = arg;
    297  1.1      bsh 	uint32_t mask = 1 << pin;
    298  1.1      bsh 	uint32_t old, new;
    299  1.1      bsh 
    300  1.1      bsh 	old = GPIO_READ(gpio, GPIO_DIR);
    301  1.1      bsh 	new = old;
    302  1.1      bsh 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    303  1.1      bsh 	case GPIO_PIN_INPUT:	new &= ~mask; break;
    304  1.1      bsh 	case GPIO_PIN_OUTPUT:	new |= mask; break;
    305  1.1      bsh 	default:		return;
    306  1.1      bsh 	}
    307  1.1      bsh 	if (old != new)
    308  1.1      bsh 		GPIO_WRITE(gpio, GPIO_DIR, new);
    309  1.1      bsh }
    310  1.1      bsh 
    311  1.1      bsh static void
    312  1.1      bsh gpio_defer(device_t self)
    313  1.1      bsh {
    314  1.3      chs 	struct gpio_softc * const gpio = device_private(self);
    315  1.1      bsh 	struct gpio_chipset_tag * const gp = &gpio->gpio_chipset;
    316  1.1      bsh 	struct gpiobus_attach_args gba;
    317  1.1      bsh 	gpio_pin_t *pins;
    318  1.1      bsh 	uint32_t mask, dir, value;
    319  1.1      bsh 	int pin;
    320  1.1      bsh 
    321  1.1      bsh 	gp->gp_cookie = gpio;
    322  1.1      bsh 	gp->gp_pin_read = imxgpio_pin_read;
    323  1.1      bsh 	gp->gp_pin_write = imxgpio_pin_write;
    324  1.1      bsh 	gp->gp_pin_ctl = imxgpio_pin_ctl;
    325  1.1      bsh 
    326  1.1      bsh 	gba.gba_gc = gp;
    327  1.1      bsh 	gba.gba_pins = gpio->gpio_pins;
    328  1.1      bsh 	gba.gba_npins = __arraycount(gpio->gpio_pins);
    329  1.1      bsh 
    330  1.1      bsh 	dir = GPIO_READ(gpio, GPIO_DIR);
    331  1.1      bsh 	value = GPIO_READ(gpio, GPIO_DR);
    332  1.1      bsh 	for (pin = 0, mask = 1, pins = gpio->gpio_pins;
    333  1.1      bsh 	     pin < 32; pin++, mask <<= 1, pins++) {
    334  1.1      bsh 		pins->pin_num = pin;
    335  1.1      bsh 		if ((gpio->gpio_edge_mask|gpio->gpio_level_mask) & mask)
    336  1.1      bsh 			pins->pin_caps = GPIO_PIN_INPUT;
    337  1.1      bsh 		else
    338  1.1      bsh 			pins->pin_caps = GPIO_PIN_INPUT|GPIO_PIN_OUTPUT;
    339  1.1      bsh 		pins->pin_flags =
    340  1.1      bsh 		    (dir & mask) ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
    341  1.1      bsh 		pins->pin_state =
    342  1.1      bsh 		    (value & mask) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
    343  1.1      bsh 	}
    344  1.1      bsh 
    345  1.1      bsh 	config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    346  1.1      bsh }
    347  1.1      bsh #endif /* NGPIO > 0 */
    348  1.1      bsh 
    349  1.1      bsh void
    350  1.1      bsh imxgpio_attach_common(device_t self, bus_space_tag_t iot,
    351  1.1      bsh     bus_space_handle_t ioh, int index, int intr, int irqbase)
    352  1.1      bsh {
    353  1.1      bsh 	struct gpio_softc * const gpio = device_private(self);
    354  1.1      bsh 
    355  1.1      bsh 	gpio->gpio_dev = self;
    356  1.1      bsh 	gpio->gpio_memt = iot;
    357  1.1      bsh 	gpio->gpio_memh = ioh;
    358  1.1      bsh 
    359  1.1      bsh 	if (irqbase > 0) {
    360  1.1      bsh 		gpio->gpio_pic.pic_ops = &gpio_pic_ops;
    361  1.3      chs 		strlcpy(gpio->gpio_pic.pic_name, device_xname(self),
    362  1.1      bsh 		    sizeof(gpio->gpio_pic.pic_name));
    363  1.1      bsh 		gpio->gpio_pic.pic_maxsources = 32;
    364  1.1      bsh 
    365  1.1      bsh 		pic_add(&gpio->gpio_pic, irqbase);
    366  1.1      bsh 
    367  1.1      bsh 		aprint_normal(": interrupts %d..%d",
    368  1.1      bsh 		    irqbase, irqbase + GPIO_NPINS - 1);
    369  1.1      bsh 
    370  1.4  hkenken #if defined(IMX_GPIO_INTR_SPLIT)
    371  1.4  hkenken 		gpio->gpio_is_0_15 = intr_establish(intr,
    372  1.4  hkenken 		    IPL_NET, IST_LEVEL, pic_handle_intr, &gpio->gpio_pic);
    373  1.4  hkenken 		KASSERT( gpio->gpio_is_0_15 != NULL );
    374  1.4  hkenken 		gpio->gpio_is_16_31 = intr_establish(intr + 1,
    375  1.4  hkenken 		    IPL_NET, IST_LEVEL, pic_handle_intr, &gpio->gpio_pic);
    376  1.4  hkenken 		KASSERT( gpio->gpio_is_16_31 != NULL );
    377  1.4  hkenken #else
    378  1.1      bsh 		gpio->gpio_is = intr_establish(intr,
    379  1.1      bsh 		    IPL_NET, IST_LEVEL, pic_handle_intr, &gpio->gpio_pic);
    380  1.1      bsh 		KASSERT( gpio->gpio_is != NULL );
    381  1.4  hkenken #endif
    382  1.1      bsh 	}
    383  1.1      bsh 	aprint_normal("\n");
    384  1.1      bsh 
    385  1.1      bsh 
    386  1.1      bsh 	gpio_handles.iot = iot;
    387  1.1      bsh 	gpio_handles.unit[index].softc = gpio;
    388  1.1      bsh 	gpio_handles.unit[index].ioh = ioh;
    389  1.1      bsh 
    390  1.1      bsh #if NGPIO > 0
    391  1.1      bsh 	config_interrupts(self, gpio_defer);
    392  1.1      bsh #endif
    393  1.1      bsh }
    394  1.1      bsh 
    395  1.1      bsh #define	GPIO_GROUP_READ(index,offset)	\
    396  1.1      bsh 	bus_space_read_4(gpio_handles.iot, gpio_handles.unit[index].ioh, \
    397  1.1      bsh 	    (offset))
    398  1.1      bsh #define	GPIO_GROUP_WRITE(index,offset,value)				\
    399  1.1      bsh 	bus_space_write_4(gpio_handles.iot, gpio_handles.unit[index].ioh, \
    400  1.1      bsh 	    (offset), (value))
    401  1.1      bsh 
    402  1.1      bsh void
    403  1.1      bsh gpio_set_direction(u_int gpio, u_int dir)
    404  1.1      bsh {
    405  1.1      bsh 	int index = gpio / GPIO_NPINS;
    406  1.1      bsh 	int bit = gpio % GPIO_NPINS;
    407  1.1      bsh 	uint32_t reg;
    408  1.1      bsh 
    409  1.1      bsh 	KDASSERT(index < imxgpio_ngroups);
    410  1.1      bsh 
    411  1.1      bsh 	/* XXX lock */
    412  1.1      bsh 
    413  1.1      bsh 
    414  1.1      bsh 	reg = GPIO_GROUP_READ(index, GPIO_DIR);
    415  1.1      bsh 	if (dir == GPIO_DIR_OUT)
    416  1.1      bsh 		reg |= __BIT(bit);
    417  1.1      bsh 	else
    418  1.1      bsh 		reg &= ~__BIT(bit);
    419  1.1      bsh 	GPIO_GROUP_WRITE(index, GPIO_DIR, reg);
    420  1.1      bsh 
    421  1.1      bsh 	/* XXX unlock */
    422  1.1      bsh }
    423  1.1      bsh 
    424  1.1      bsh 
    425  1.1      bsh void
    426  1.1      bsh gpio_data_write(u_int gpio, u_int value)
    427  1.1      bsh {
    428  1.1      bsh 	int index = gpio / GPIO_NPINS;
    429  1.1      bsh 	int bit = gpio % GPIO_NPINS;
    430  1.1      bsh 	uint32_t reg;
    431  1.1      bsh 
    432  1.1      bsh 	KDASSERT(index < imxgpio_ngroups);
    433  1.1      bsh 
    434  1.1      bsh 	/* XXX lock */
    435  1.1      bsh 	reg = GPIO_GROUP_READ(index, GPIO_DR);
    436  1.1      bsh 	if (value)
    437  1.1      bsh 		reg |= __BIT(bit);
    438  1.1      bsh 	else
    439  1.1      bsh 		reg &= ~__BIT(bit);
    440  1.1      bsh 	GPIO_GROUP_WRITE(index, GPIO_DR, reg);
    441  1.1      bsh 
    442  1.1      bsh 	/* XXX unlock */
    443  1.1      bsh }
    444  1.1      bsh 
    445  1.1      bsh bool
    446  1.1      bsh gpio_data_read(u_int gpio)
    447  1.1      bsh {
    448  1.1      bsh 	int index = gpio / GPIO_NPINS;
    449  1.1      bsh 	int bit = gpio % GPIO_NPINS;
    450  1.1      bsh 	uint32_t reg;
    451  1.1      bsh 
    452  1.1      bsh 	KDASSERT(index < imxgpio_ngroups);
    453  1.1      bsh 
    454  1.1      bsh 	reg = GPIO_GROUP_READ(index, GPIO_DR);
    455  1.1      bsh 
    456  1.1      bsh 	return reg & __BIT(bit) ? true : false;
    457  1.1      bsh }
    458