Home | History | Annotate | Line # | Download | only in imx
imx31_gpio.c revision 1.4.8.1
      1  1.4.8.1  rmind /*	$NetBSD: imx31_gpio.c,v 1.4.8.1 2011/03/05 20:49:34 rmind Exp $	*/
      2      1.2   matt /*-
      3      1.2   matt  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      4      1.2   matt  * All rights reserved.
      5      1.2   matt  *
      6      1.2   matt  * This code is derived from software contributed to The NetBSD Foundation
      7      1.2   matt  * by Matt Thomas
      8      1.2   matt  *
      9      1.2   matt  * Redistribution and use in source and binary forms, with or without
     10      1.2   matt  * modification, are permitted provided that the following conditions
     11      1.2   matt  * are met:
     12      1.2   matt  * 1. Redistributions of source code must retain the above copyright
     13      1.2   matt  *    notice, this list of conditions and the following disclaimer.
     14      1.2   matt  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.2   matt  *    notice, this list of conditions and the following disclaimer in the
     16      1.2   matt  *    documentation and/or other materials provided with the distribution.
     17      1.2   matt  *
     18      1.2   matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19      1.2   matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20      1.2   matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21      1.2   matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22      1.2   matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23      1.2   matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24      1.2   matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25      1.2   matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26      1.2   matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27      1.2   matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28      1.2   matt  * POSSIBILITY OF SUCH DAMAGE.
     29      1.2   matt  */
     30      1.2   matt #include <sys/cdefs.h>
     31  1.4.8.1  rmind __KERNEL_RCSID(0, "$NetBSD: imx31_gpio.c,v 1.4.8.1 2011/03/05 20:49:34 rmind Exp $");
     32      1.2   matt 
     33      1.2   matt #define _INTR_PRIVATE
     34      1.2   matt 
     35      1.2   matt #include "locators.h"
     36      1.2   matt #include "gpio.h"
     37      1.2   matt 
     38      1.2   matt #include <sys/param.h>
     39      1.2   matt #include <sys/evcnt.h>
     40      1.4   matt #include <sys/atomic.h>
     41      1.2   matt 
     42      1.2   matt #include <uvm/uvm_extern.h>
     43      1.2   matt 
     44      1.2   matt #include <machine/intr.h>
     45      1.2   matt 
     46      1.2   matt #include <arm/cpu.h>
     47      1.2   matt #include <arm/armreg.h>
     48      1.2   matt #include <arm/cpufunc.h>
     49      1.2   matt 
     50      1.2   matt #include <machine/bus.h>
     51      1.2   matt 
     52      1.2   matt #include <arm/imx/imx31reg.h>
     53      1.2   matt #include <arm/imx/imx31var.h>
     54  1.4.8.1  rmind #include <arm/imx/imxgpioreg.h>
     55      1.2   matt #include <arm/pic/picvar.h>
     56      1.2   matt 
     57      1.2   matt #if NGPIO > 0
     58      1.2   matt #include <sys/gpio.h>
     59      1.2   matt #include <dev/gpio/gpiovar.h>
     60      1.2   matt #endif
     61      1.2   matt 
     62      1.2   matt static void gpio_pic_block_irqs(struct pic_softc *, size_t, uint32_t);
     63      1.2   matt static void gpio_pic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     64      1.2   matt static int gpio_pic_find_pending_irqs(struct pic_softc *);
     65      1.2   matt static void gpio_pic_establish_irq(struct pic_softc *, struct intrsource *);
     66      1.2   matt 
     67      1.2   matt const struct pic_ops gpio_pic_ops = {
     68      1.2   matt 	.pic_block_irqs = gpio_pic_block_irqs,
     69      1.2   matt 	.pic_unblock_irqs = gpio_pic_unblock_irqs,
     70      1.2   matt 	.pic_find_pending_irqs = gpio_pic_find_pending_irqs,
     71      1.2   matt 	.pic_establish_irq = gpio_pic_establish_irq,
     72      1.2   matt };
     73      1.2   matt 
     74      1.2   matt struct gpio_softc {
     75      1.2   matt 	struct device gpio_dev;
     76      1.2   matt 	struct pic_softc gpio_pic;
     77      1.2   matt 	bus_space_tag_t gpio_memt;
     78      1.2   matt 	bus_space_handle_t gpio_memh;
     79      1.2   matt 	uint32_t gpio_enable_mask;
     80      1.2   matt 	uint32_t gpio_edge_mask;
     81      1.2   matt 	uint32_t gpio_level_mask;
     82      1.2   matt #if NGPIO > 0
     83      1.2   matt 	struct gpio_chipset_tag gpio_chipset;
     84      1.2   matt 	gpio_pin_t gpio_pins[32];
     85      1.2   matt #endif
     86      1.2   matt };
     87      1.2   matt 
     88      1.2   matt #define	PIC_TO_SOFTC(pic) \
     89      1.2   matt 	((struct gpio_softc *)((char *)(pic) - \
     90      1.2   matt 		offsetof(struct gpio_softc, gpio_pic)))
     91      1.2   matt 
     92      1.2   matt #define	GPIO_READ(gpio, reg) \
     93      1.2   matt 	bus_space_read_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg))
     94      1.2   matt #define	GPIO_WRITE(gpio, reg, val) \
     95      1.2   matt 	bus_space_write_4((gpio)->gpio_memt, (gpio)->gpio_memh, (reg), (val))
     96      1.2   matt 
     97      1.2   matt void
     98      1.2   matt gpio_pic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
     99      1.2   matt {
    100      1.2   matt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    101      1.2   matt 	KASSERT(irq_base == 0);
    102      1.2   matt 
    103      1.2   matt 	gpio->gpio_enable_mask |= irq_mask;
    104      1.2   matt 	/*
    105      1.2   matt 	 * If this a level source, ack it now.  If it's still asserted
    106      1.2   matt 	 * it'll come back.
    107      1.2   matt 	 */
    108      1.2   matt 	if (irq_mask & gpio->gpio_level_mask)
    109      1.2   matt 		GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
    110      1.2   matt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    111      1.2   matt }
    112      1.2   matt 
    113      1.2   matt void
    114      1.2   matt gpio_pic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    115      1.2   matt {
    116      1.2   matt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    117      1.2   matt 	KASSERT(irq_base == 0);
    118      1.2   matt 
    119      1.2   matt 	gpio->gpio_enable_mask &= ~irq_mask;
    120      1.2   matt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    121      1.2   matt }
    122      1.2   matt 
    123      1.2   matt int
    124      1.2   matt gpio_pic_find_pending_irqs(struct pic_softc *pic)
    125      1.2   matt {
    126      1.2   matt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    127      1.2   matt 	uint32_t v;
    128      1.2   matt 	uint32_t pending;
    129      1.2   matt 
    130      1.2   matt 	v = GPIO_READ(gpio, GPIO_ISR);
    131      1.2   matt 	pending = (v & gpio->gpio_enable_mask);
    132      1.2   matt 	if (pending == 0)
    133      1.2   matt 		return 0;
    134      1.2   matt 
    135      1.2   matt 	/*
    136      1.2   matt 	 * Disable the pending interrupts.
    137      1.2   matt 	 */
    138      1.2   matt 	gpio->gpio_enable_mask &= ~pending;
    139      1.2   matt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    140      1.2   matt 
    141      1.2   matt 	/*
    142      1.2   matt 	 * If any of the sources are edge triggered, ack them now so
    143      1.2   matt 	 * we won't lose them.
    144      1.2   matt 	 */
    145      1.2   matt 	if (v & gpio->gpio_edge_mask)
    146      1.2   matt 		GPIO_WRITE(gpio, GPIO_ISR, v & gpio->gpio_edge_mask);
    147      1.2   matt 
    148      1.2   matt 	/*
    149      1.2   matt 	 * Now find all the pending bits and mark them as pending.
    150      1.2   matt 	 */
    151      1.2   matt 	do {
    152      1.2   matt 		int irq;
    153      1.2   matt 		KASSERT(pending != 0);
    154      1.2   matt 		irq = 31 - __builtin_clz(pending);
    155      1.2   matt 		pending &= ~__BIT(irq);
    156      1.2   matt 		pic_mark_pending(&gpio->gpio_pic, irq);
    157      1.2   matt 	} while (pending != 0);
    158      1.2   matt 
    159      1.2   matt 	return 1;
    160      1.2   matt }
    161      1.2   matt 
    162      1.2   matt #define GPIO_TYPEMAP \
    163      1.2   matt 	((GPIO_ICR_LEVEL_LOW << (2*IST_LEVEL_LOW)) | \
    164      1.2   matt 	 (GPIO_ICR_LEVEL_HIGH << (2*IST_LEVEL_HIGH)) | \
    165      1.2   matt 	 (GPIO_ICR_EDGE_RISING << (2*IST_EDGE_RISING)) | \
    166      1.2   matt 	 (GPIO_ICR_EDGE_FALLING << (2*IST_EDGE_FALLING)))
    167      1.2   matt 
    168      1.2   matt void
    169      1.2   matt gpio_pic_establish_irq(struct pic_softc *pic, struct intrsource *is)
    170      1.2   matt {
    171      1.2   matt 	struct gpio_softc * const gpio = PIC_TO_SOFTC(pic);
    172      1.2   matt 	KASSERT(is->is_irq < 32);
    173      1.2   matt 	uint32_t irq_mask = __BIT(is->is_irq);
    174      1.2   matt 	uint32_t v;
    175      1.2   matt 	unsigned int icr_shift, icr_reg;
    176      1.2   matt 	unsigned int gtype;
    177      1.2   matt 
    178      1.2   matt 	/*
    179      1.2   matt 	 * Make sure the irq isn't enabled and not asserting.
    180      1.2   matt 	 */
    181      1.2   matt 	gpio->gpio_enable_mask &= ~irq_mask;
    182      1.2   matt 	GPIO_WRITE(gpio, GPIO_IMR, gpio->gpio_enable_mask);
    183      1.2   matt 	GPIO_WRITE(gpio, GPIO_ISR, irq_mask);
    184      1.2   matt 
    185      1.2   matt 	/*
    186      1.2   matt 	 * Convert the type to a gpio type and figure out which bits in what
    187      1.2   matt 	 * register we have to tweak.
    188      1.2   matt 	 */
    189      1.2   matt 	gtype = (GPIO_TYPEMAP >> (2 * is->is_type)) & 3;
    190      1.2   matt 	icr_shift = (is->is_irq & 0x0f) << 1;
    191      1.2   matt 	icr_reg = GPIO_ICR1 + ((is->is_irq & 0x10) >> 2);
    192      1.2   matt 
    193      1.2   matt 	/*
    194      1.2   matt 	 * Set the interrupt type.
    195      1.2   matt 	 */
    196      1.2   matt 	v = GPIO_READ(gpio, icr_reg);
    197      1.2   matt 	v &= ~(3 << icr_shift);
    198      1.2   matt 	v |= gtype << icr_shift;
    199      1.2   matt 	GPIO_WRITE(gpio, icr_reg, v);
    200      1.2   matt 
    201      1.2   matt 	/*
    202      1.2   matt 	 * Mark it as input.
    203      1.2   matt 	 */
    204      1.2   matt 	v = GPIO_READ(gpio, GPIO_DIR);
    205      1.2   matt 	v &= ~irq_mask;
    206      1.2   matt 	GPIO_WRITE(gpio, GPIO_DIR, v);
    207      1.2   matt 
    208      1.2   matt 	/*
    209      1.2   matt 	 * Now record the type of interrupt.
    210      1.2   matt 	 */
    211      1.2   matt 	if (gtype == GPIO_ICR_EDGE_RISING || gtype == GPIO_ICR_EDGE_FALLING) {
    212      1.2   matt 		gpio->gpio_edge_mask |= irq_mask;
    213      1.2   matt 		gpio->gpio_level_mask &= ~irq_mask;
    214      1.2   matt 	} else {
    215      1.2   matt 		gpio->gpio_edge_mask &= ~irq_mask;
    216      1.2   matt 		gpio->gpio_level_mask |= irq_mask;
    217      1.2   matt 	}
    218      1.2   matt }
    219      1.2   matt 
    220      1.2   matt static int gpio_match(device_t, cfdata_t, void *);
    221      1.2   matt static void gpio_attach(device_t, device_t, void *);
    222      1.2   matt 
    223      1.2   matt CFATTACH_DECL(imxgpio,
    224      1.2   matt 	sizeof(struct gpio_softc),
    225      1.2   matt 	gpio_match, gpio_attach,
    226      1.2   matt 	NULL, NULL);
    227      1.2   matt 
    228      1.2   matt #if NGPIO > 0
    229      1.2   matt 
    230      1.2   matt static int
    231      1.2   matt imxgpio_pin_read(void *arg, int pin)
    232      1.2   matt {
    233      1.2   matt 	struct gpio_softc * const gpio = arg;
    234      1.2   matt 
    235      1.2   matt 	return (GPIO_READ(gpio, GPIO_DR) >> pin) & 1;
    236      1.2   matt }
    237      1.2   matt 
    238      1.2   matt static void
    239      1.2   matt imxgpio_pin_write(void *arg, int pin, int value)
    240      1.2   matt {
    241      1.2   matt 	struct gpio_softc * const gpio = arg;
    242      1.2   matt 	uint32_t mask = 1 << pin;
    243      1.2   matt 	uint32_t old, new;
    244      1.2   matt 
    245      1.2   matt 	old = GPIO_READ(gpio, GPIO_DR);
    246      1.2   matt 	if (value)
    247      1.2   matt 		new = old | mask;
    248      1.2   matt 	else
    249      1.2   matt 		new = old & ~mask;
    250      1.2   matt 
    251      1.2   matt 	if (old != new)
    252      1.2   matt 		GPIO_WRITE(gpio, GPIO_DR, new);
    253      1.2   matt }
    254      1.2   matt 
    255      1.2   matt static void
    256      1.2   matt imxgpio_pin_ctl(void *arg, int pin, int flags)
    257      1.2   matt {
    258      1.2   matt 	struct gpio_softc * const gpio = arg;
    259      1.2   matt 	uint32_t mask = 1 << pin;
    260      1.2   matt 	uint32_t old, new;
    261      1.2   matt 
    262      1.2   matt 	old = GPIO_READ(gpio, GPIO_DIR);
    263      1.2   matt 	new = old;
    264      1.2   matt 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    265      1.2   matt 	case GPIO_PIN_INPUT:	new |= mask; break;
    266      1.2   matt 	case GPIO_PIN_OUTPUT:	new &= ~mask; break;
    267      1.2   matt 	default:		return;
    268      1.2   matt 	}
    269      1.2   matt 	if (old != new)
    270      1.2   matt 		GPIO_WRITE(gpio, GPIO_DIR, new);
    271      1.2   matt }
    272      1.2   matt 
    273      1.2   matt static void
    274      1.2   matt gpio_defer(device_t self)
    275      1.2   matt {
    276      1.2   matt 	struct gpio_softc * const gpio = (void *) self;
    277      1.2   matt 	struct gpio_chipset_tag * const gp = &gpio->gpio_chipset;
    278      1.2   matt 	struct gpiobus_attach_args gba;
    279      1.2   matt 	gpio_pin_t *pins;
    280      1.2   matt 	uint32_t mask, dir, value;
    281      1.2   matt 	int pin;
    282      1.2   matt 
    283      1.2   matt 	gp->gp_cookie = gpio;
    284      1.2   matt 	gp->gp_pin_read = imxgpio_pin_read;
    285      1.2   matt 	gp->gp_pin_write = imxgpio_pin_write;
    286      1.2   matt 	gp->gp_pin_ctl = imxgpio_pin_ctl;
    287      1.2   matt 
    288      1.2   matt 	gba.gba_gc = gp;
    289      1.2   matt 	gba.gba_pins = gpio->gpio_pins;
    290      1.2   matt 	gba.gba_npins = __arraycount(gpio->gpio_pins);
    291      1.2   matt 
    292      1.2   matt 	dir = GPIO_READ(gpio, GPIO_DIR);
    293      1.2   matt 	value = GPIO_READ(gpio, GPIO_DR);
    294      1.2   matt 	for (pin = 0, mask = 1, pins = gpio->gpio_pins;
    295      1.2   matt 	     pin < 32; pin++, mask <<= 1, pins++) {
    296      1.2   matt 		pins->pin_num = pin;
    297      1.2   matt 		if ((gpio->gpio_edge_mask|gpio->gpio_level_mask) & mask)
    298      1.2   matt 			pins->pin_caps = GPIO_PIN_INPUT;
    299      1.2   matt 		else
    300      1.2   matt 			pins->pin_caps = GPIO_PIN_INPUT|GPIO_PIN_OUTPUT;
    301      1.2   matt 		pins->pin_flags =
    302      1.2   matt 		    (dir & mask) ? GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
    303      1.2   matt 		pins->pin_state =
    304      1.2   matt 		    (value & mask) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
    305      1.2   matt 	}
    306      1.2   matt 
    307      1.2   matt 	config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    308      1.2   matt }
    309      1.2   matt #endif /* NGPIO > 0 */
    310      1.2   matt 
    311      1.2   matt int
    312      1.2   matt gpio_match(device_t parent, cfdata_t cfdata, void *aux)
    313      1.2   matt {
    314      1.2   matt 	struct ahb_attach_args *ahba = aux;
    315      1.2   matt 	bus_space_handle_t memh;
    316      1.2   matt 	bus_size_t size;
    317      1.2   matt 	int error;
    318      1.2   matt 
    319      1.2   matt 	if (ahba->ahba_addr != GPIO1_BASE
    320      1.2   matt 	    && ahba->ahba_addr != GPIO2_BASE
    321      1.2   matt 	    && ahba->ahba_addr != GPIO3_BASE)
    322      1.2   matt 		return 0;
    323      1.2   matt 
    324      1.2   matt 	size = (ahba->ahba_size == AHBCF_SIZE_DEFAULT) ? GPIO_SIZE : ahba->ahba_size;
    325      1.2   matt 
    326      1.2   matt 	error = bus_space_map(ahba->ahba_memt, ahba->ahba_addr, size, 0, &memh);
    327      1.2   matt 	if (error)
    328      1.2   matt 		return 0;
    329      1.2   matt 
    330      1.2   matt 	bus_space_unmap(ahba->ahba_memt, memh, size);
    331      1.2   matt 	return 1;
    332      1.2   matt }
    333      1.2   matt 
    334      1.2   matt void
    335      1.2   matt gpio_attach(device_t parent, device_t self, void *aux)
    336      1.2   matt {
    337      1.2   matt 	struct ahb_attach_args * const ahba = aux;
    338      1.2   matt 	struct gpio_softc * const gpio = (void *) self;
    339      1.2   matt 	int error;
    340      1.2   matt 
    341      1.2   matt 	if (ahba->ahba_size == AHBCF_SIZE_DEFAULT)
    342      1.2   matt 		ahba->ahba_size = GPIO_SIZE;
    343      1.2   matt 
    344      1.2   matt 	gpio->gpio_memt = ahba->ahba_memt;
    345      1.2   matt 	error = bus_space_map(ahba->ahba_memt, ahba->ahba_addr, ahba->ahba_size,
    346      1.2   matt 	    0, &gpio->gpio_memh);
    347      1.2   matt 
    348      1.2   matt 	if (error) {
    349      1.2   matt 		aprint_error(": failed to map register %#lx@%#lx: %d\n",
    350      1.2   matt 		    ahba->ahba_size, ahba->ahba_addr, error);
    351      1.2   matt 		return;
    352      1.2   matt 	}
    353      1.2   matt 
    354      1.2   matt 	if (ahba->ahba_irqbase != AHBCF_IRQBASE_DEFAULT) {
    355      1.2   matt 		gpio->gpio_pic.pic_ops = &gpio_pic_ops;
    356      1.2   matt 		strlcpy(gpio->gpio_pic.pic_name, self->dv_xname,
    357      1.2   matt 		    sizeof(gpio->gpio_pic.pic_name));
    358      1.2   matt 		gpio->gpio_pic.pic_maxsources = 32;
    359      1.2   matt 		pic_add(&gpio->gpio_pic, ahba->ahba_irqbase);
    360      1.2   matt 		aprint_normal(": interrupts %d..%d",
    361      1.2   matt 		    ahba->ahba_irqbase, ahba->ahba_irqbase + 31);
    362      1.2   matt 	}
    363      1.2   matt 	aprint_normal("\n");
    364      1.2   matt #if NGPIO > 0
    365      1.2   matt 	config_interrupts(self, gpio_defer);
    366      1.2   matt #endif
    367      1.2   matt }
    368