Home | History | Annotate | Line # | Download | only in ebus
gpio_ebus.c revision 1.2
      1 /*	$NetBSD: gpio_ebus.c,v 1.2 2011/06/12 05:27:56 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code was written by Alessandro Forin and Neil Pittman
      8  * at Microsoft Research and contributed to The NetBSD Foundation
      9  * by Microsoft Corporation.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34 __KERNEL_RCSID(0, "$NetBSD: gpio_ebus.c,v 1.2 2011/06/12 05:27:56 tsutsui Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/device.h>
     38 #include <sys/systm.h>
     39 #include <sys/gpio.h>
     40 #include <dev/gpio/gpiovar.h>
     41 
     42 #include <emips/ebus/ebusvar.h>
     43 #include <emips/emips/machdep.h>
     44 #include <machine/emipsreg.h>
     45 
     46 /*
     47  * Device softc
     48  */
     49 #define GPIO_NPINS 32
     50 
     51 struct epio_softc {
     52 	struct device sc_dev;
     53 	struct _Pio *sc_dp;
     54 	struct gpio_chipset_tag	sc_gpio_gc;
     55 	gpio_pin_t sc_gpio_pins[GPIO_NPINS];
     56 };
     57 
     58 static int	epio_ebus_match(device_t, cfdata_t, void *);
     59 static void	epio_ebus_attach(device_t, device_t, void *);
     60 
     61 CFATTACH_DECL(epio, sizeof (struct epio_softc),
     62     epio_ebus_match, epio_ebus_attach, NULL, NULL);
     63 
     64 static int	epio_pin_read(void *, int);
     65 static void	epio_pin_write(void *, int, int);
     66 static void	epio_pin_ctl(void *, int, int);
     67 
     68 static int
     69 epio_ebus_match(device_t parent, cfdata_t cf, void *aux)
     70 {
     71 	struct ebus_attach_args *ia = aux;
     72 	struct _Pio *f = (struct _Pio *)ia->ia_vaddr;
     73 
     74 	if (strcmp("gpio", ia->ia_name) != 0)
     75 		return 0;
     76 	if ((f == NULL) ||
     77 		(f->Tag != PMTTAG_GPIO))
     78 		return 0;
     79 
     80 	return 1;
     81 }
     82 
     83 static void
     84 epio_ebus_attach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct epio_softc *sc = (struct epio_softc *)self;
     87 	struct ebus_attach_args *ia =aux;
     88 	struct gpiobus_attach_args gba;
     89 	int i;
     90 	uint32_t data;
     91 
     92 	sc->sc_dp = (struct _Pio *)ia->ia_vaddr;
     93 	data = sc->sc_dp->PinData;
     94 
     95 #if DEBUG
     96 	printf(" virt=%p data=%zx", (void*)sc->sc_dp, data);
     97 #endif
     98 	printf(": GPIO controller\n");
     99 
    100 	/* BUGBUG Initialize pins properly */
    101 	for (i = 0 ; i < GPIO_NPINS ; i++) {
    102 		sc->sc_gpio_pins[i].pin_num = i;
    103 		sc->sc_gpio_pins[i].pin_caps =
    104 		    GPIO_PIN_INOUT |
    105 		    GPIO_PIN_OPENDRAIN |
    106 		    GPIO_PIN_TRISTATE;
    107 
    108 		/* current defaults */
    109 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_INOUT;
    110 		sc->sc_gpio_pins[i].pin_state =
    111 		    (data & (1 << i)) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
    112 		sc->sc_gpio_pins[i].pin_mapped = 0;
    113 	}
    114 
    115 	/* Create controller tag */
    116 	sc->sc_gpio_gc.gp_cookie = sc;
    117 	sc->sc_gpio_gc.gp_pin_read = epio_pin_read;
    118 	sc->sc_gpio_gc.gp_pin_write = epio_pin_write;
    119 	sc->sc_gpio_gc.gp_pin_ctl = epio_pin_ctl;
    120 
    121 	gba.gba_gc = &sc->sc_gpio_gc;
    122 	gba.gba_pins = sc->sc_gpio_pins;
    123 	gba.gba_npins = GPIO_NPINS;
    124 
    125 	/* Attach GPIO framework */
    126 	(void)config_found(&sc->sc_dev, &gba, gpiobus_print);
    127 }
    128 
    129 static int
    130 epio_pin_read(void *arg, int pin)
    131 {
    132 	struct epio_softc *sc = arg;
    133 	uint32_t data = sc->sc_dp->PinData;
    134 	int p;
    135 
    136 	p = pin % GPIO_NPINS;
    137 	return (data >> p) & 0x01;
    138 }
    139 
    140 static void
    141 epio_pin_write(void *arg, int pin, int value)
    142 {
    143 	struct epio_softc *sc = arg;
    144 	uint32_t data;
    145 	int p;
    146 
    147 	p = pin % GPIO_NPINS;
    148 	data = 1 << p;
    149 	if (value)
    150 		sc->sc_dp->PinData = data;
    151 	else
    152 		sc->sc_dp->ClearData = data;
    153 }
    154 
    155 static void
    156 epio_pin_ctl(void *arg, int pin, int flags)
    157 {
    158 	struct epio_softc *sc = arg;
    159 	uint32_t data;
    160 	int p;
    161 
    162 	p = pin % GPIO_NPINS;
    163 	data = (1 << p);
    164 
    165 	if (flags & GPIO_PIN_INOUT) {
    166 		sc->sc_dp->Direction = data;
    167 	}
    168 
    169 	if (flags & GPIO_PIN_TRISTATE) {
    170 		sc->sc_dp->OutDisable = data;
    171 	}
    172 
    173 	if (flags & GPIO_PIN_OPENDRAIN) {
    174 		sc->sc_dp->Enable = data;
    175 	}
    176 }
    177