Home | History | Annotate | Line # | Download | only in dev
augpio.c revision 1.4
      1 /* $NetBSD: augpio.c,v 1.4 2006/03/24 02:58:36 gdamore Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Garrett D'Amore for Itronix Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: augpio.c,v 1.4 2006/03/24 02:58:36 gdamore Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/errno.h>
     41 #include <sys/device.h>
     42 #include <sys/gpio.h>
     43 #include <sys/kernel.h>
     44 
     45 #include <dev/gpio/gpiovar.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/cpu.h>
     49 
     50 #include <mips/alchemy/include/aubusvar.h>
     51 #include <mips/alchemy/include/aureg.h>
     52 #include <mips/alchemy/dev/augpioreg.h>
     53 #include <mips/alchemy/dev/augpiovar.h>
     54 
     55 struct augpio_softc {
     56 	struct device			sc_dev;
     57 	struct gpio_chipset_tag		sc_gc;
     58 	gpio_pin_t			sc_pins[AUGPIO_NPINS];
     59 	int				sc_npins;
     60 	bus_space_tag_t			sc_bst;
     61 	int				sc_caps;
     62 	const char 			*sc_name;
     63 	int				(*sc_getctl)(void *, int);
     64 };
     65 
     66 static int augpio_match(struct device *, struct cfdata *, void *);
     67 static void augpio_attach(struct device *, struct device *, void *);
     68 
     69 CFATTACH_DECL(augpio, sizeof(struct augpio_softc),
     70     augpio_match, augpio_attach, NULL, NULL);
     71 
     72 #define	GETREG(x)	\
     73 	(*(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(x))
     74 #define	PUTREG(x, v)	\
     75 	((*(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(x)) = (v))
     76 
     77 #define	GETGPIO(x)	GETREG(GPIO_BASE + (x))
     78 #define	PUTGPIO(x,v)	PUTREG(GPIO_BASE + (x), (v))
     79 #define	GETGPIO2(x)	GETREG(GPIO2_BASE + (x))
     80 #define	PUTGPIO2(x,v)	PUTREG(GPIO2_BASE + (x), (v))
     81 
     82 static int augpio_found = 0;
     83 
     84 int
     85 augpio_match(struct device *parent, struct cfdata *match, void *aux)
     86 {
     87 	struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;
     88 
     89 	if (strcmp(aa->aa_name, "augpio") != 0)
     90 		return 0;
     91 
     92 	if (augpio_found)
     93 		return 0;
     94 
     95 	return 1;
     96 }
     97 
     98 void
     99 augpio_attach(struct device *parent, struct device *self, void *aux)
    100 {
    101 	int	pin;
    102 
    103 	struct augpio_softc *sc = (struct augpio_softc *)self;
    104 	struct aubus_attach_args *aa = aux;
    105 	struct gpiobus_attach_args gba;
    106 
    107 	sc->sc_bst = aa->aa_st;
    108 	sc->sc_npins = aa->aa_addrs[1];
    109 	sc->sc_gc.gp_cookie = sc;
    110 
    111 	if (aa->aa_addrs[0] == GPIO_BASE) {
    112 
    113 		sc->sc_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    114 		    GPIO_PIN_TRISTATE;
    115 		sc->sc_gc.gp_pin_read = augpio_read;
    116 		sc->sc_gc.gp_pin_write = augpio_write;
    117 		sc->sc_gc.gp_pin_ctl = augpio_ctl;
    118 		sc->sc_getctl = augpio_getctl;
    119 		sc->sc_name = "primary block";
    120 
    121 	} else if (aa->aa_addrs[0] == GPIO2_BASE) {
    122 		/*
    123 		 * We rely on firmware (or platform init code) to initialize
    124 		 * the GPIO2 block.  We can't do it ourselves, because
    125 		 * resetting the GPIO2 block can have nasty effects (e.g.
    126 		 * reset PCI bus...)
    127 		 */
    128 		sc->sc_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
    129 		sc->sc_gc.gp_pin_read = augpio2_read;
    130 		sc->sc_gc.gp_pin_write = augpio2_write;
    131 		sc->sc_gc.gp_pin_ctl = augpio2_ctl;
    132 		sc->sc_getctl = augpio2_getctl;
    133 		sc->sc_name = "secondary block";
    134 
    135 	} else {
    136 		printf(": unidentified block\n");
    137 		return;
    138 	}
    139 
    140 	for (pin = 0; pin < sc->sc_npins; pin++) {
    141 		gpio_pin_t	*pp = &sc->sc_pins[pin];
    142 
    143 		pp->pin_num = pin;
    144 		pp->pin_caps = sc->sc_caps;
    145 		pp->pin_flags = sc->sc_getctl(sc, pin);
    146 		pp->pin_state = sc->sc_gc.gp_pin_read(sc, pin);
    147 	}
    148 
    149 	gba.gba_gc = &sc->sc_gc;
    150 	gba.gba_pins = sc->sc_pins;
    151 	gba.gba_npins = sc->sc_npins;
    152 
    153 	printf(": Alchemy GPIO, %s\n", sc->sc_name);
    154 	config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    155 }
    156 
    157 int
    158 augpio_read(void *arg, int pin)
    159 {
    160 
    161 	pin = 1 << pin;
    162 
    163 	if (GETGPIO(AUGPIO_PINSTATERD) & pin)
    164 		return GPIO_PIN_HIGH;
    165 	else
    166 		return GPIO_PIN_LOW;
    167 }
    168 
    169 void
    170 augpio_write(void *arg, int pin, int value)
    171 {
    172 
    173 	pin = 1 << pin;
    174 	PUTGPIO(value ? AUGPIO_OUTPUTSET : AUGPIO_OUTPUTCLR, pin);
    175 }
    176 
    177 void
    178 augpio_ctl(void *arg, int pin, int flags)
    179 {
    180 	bus_addr_t		reg;
    181 
    182 	pin = 1 << pin;
    183 
    184 	if (flags & (GPIO_PIN_TRISTATE|GPIO_PIN_INPUT)) {
    185 		reg = AUGPIO_TRIOUTCLR;
    186 	} else if (flags & GPIO_PIN_OUTPUT) {
    187 		uint32_t		out;
    188 		out = GETGPIO(AUGPIO_OUTPUTRD);
    189 		reg = pin & out ? AUGPIO_OUTPUTSET : AUGPIO_OUTPUTCLR;
    190 	} else {
    191 		return;
    192 	}
    193 
    194 	PUTGPIO(reg, pin);
    195 }
    196 
    197 int
    198 augpio_getctl(void *arg, int pin)
    199 {
    200 
    201 	if (GETGPIO(AUGPIO_TRIOUTRD) & pin)
    202 		return GPIO_PIN_OUTPUT;
    203 	else
    204 		return GPIO_PIN_INPUT;
    205 }
    206 
    207 int
    208 augpio2_read(void *arg, int pin)
    209 {
    210 
    211 	pin = 1 << pin;
    212 
    213 	if (GETGPIO2(AUGPIO2_PINSTATE) & pin)
    214 		return GPIO_PIN_HIGH;
    215 	else
    216 		return GPIO_PIN_LOW;
    217 }
    218 
    219 void
    220 augpio2_write(void *arg, int pin, int value)
    221 {
    222 
    223 	pin = 1 << pin;
    224 
    225 	if (value) {
    226 		pin = pin | (pin << 16);
    227 	} else {
    228 		pin = (pin << 16);
    229 	}
    230 
    231 	PUTGPIO2(AUGPIO2_OUTPUT, pin);
    232 }
    233 
    234 void
    235 augpio2_ctl(void *arg, int pin, int flags)
    236 {
    237 	uint32_t		dir;
    238 
    239 	pin = 1 << pin;
    240 
    241 	dir = GETGPIO2(AUGPIO2_DIR);
    242 
    243 	if (flags & GPIO_PIN_INPUT) {
    244 		dir |= pin;
    245 	} else if (flags & GPIO_PIN_OUTPUT) {
    246 		dir &= ~pin;
    247 	}
    248 	PUTGPIO2(AUGPIO2_DIR, dir);
    249 }
    250 
    251 int
    252 augpio2_getctl(void *arg, int pin)
    253 {
    254 	uint32_t		dir;
    255 
    256 	pin = 1 << pin;
    257 
    258 	dir = GETGPIO2(AUGPIO2_DIR);
    259 	if (dir & (uint32_t)pin) {
    260 		return GPIO_PIN_OUTPUT;
    261 	} else {
    262 		return GPIO_PIN_INPUT;
    263 	}
    264 }
    265 
    266