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