Home | History | Annotate | Line # | Download | only in dev
gpio_opb.c revision 1.5.10.1
      1  1.5.10.1     ad /*	$NetBSD: gpio_opb.c,v 1.5.10.1 2007/02/09 21:03:50 ad Exp $	*/
      2       1.1  shige 
      3       1.1  shige /*
      4       1.1  shige  * Copyright (c) 2004 Shigeyuki Fukushima.
      5       1.1  shige  * All rights reserved.
      6       1.1  shige  *
      7       1.1  shige  * Redistribution and use in source and binary forms, with or without
      8       1.1  shige  * modification, are permitted provided that the following conditions
      9       1.1  shige  * are met:
     10       1.1  shige  * 1. Redistributions of source code must retain the above copyright
     11       1.1  shige  *    notice, this list of conditions and the following disclaimer.
     12       1.1  shige  * 2. Redistributions in binary form must reproduce the above
     13       1.1  shige  *    copyright notice, this list of conditions and the following
     14       1.1  shige  *    disclaimer in the documentation and/or other materials provided
     15       1.1  shige  *    with the distribution.
     16       1.1  shige  * 3. The name of the author may not be used to endorse or promote
     17       1.1  shige  *    products derived from this software without specific prior
     18       1.1  shige  *    written permission.
     19       1.1  shige  *
     20       1.1  shige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     21       1.1  shige  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22       1.1  shige  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23       1.1  shige  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     24       1.1  shige  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25       1.1  shige  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     26       1.1  shige  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1  shige  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     28       1.1  shige  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29       1.1  shige  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30       1.1  shige  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1  shige  */
     32       1.1  shige 
     33       1.1  shige #include "locators.h"
     34       1.1  shige 
     35       1.1  shige #include <sys/param.h>
     36       1.1  shige #include <sys/device.h>
     37       1.1  shige #include <sys/systm.h>
     38       1.1  shige 
     39       1.1  shige #include <machine/pio.h>
     40       1.1  shige 
     41       1.5  shige #include <sys/gpio.h>
     42       1.5  shige #include <dev/gpio/gpiovar.h>
     43       1.5  shige 
     44       1.1  shige #include <powerpc/ibm4xx/dcr405gp.h>
     45       1.1  shige #include <powerpc/ibm4xx/dev/opbvar.h>
     46       1.1  shige #include <powerpc/ibm4xx/dev/gpioreg.h>
     47       1.1  shige 
     48       1.5  shige struct gpio_opb_softc {
     49       1.1  shige 	struct device		sc_dev;		/* device generic */
     50       1.5  shige 	/* GPIO interface */
     51       1.5  shige 	bus_space_tag_t		sc_gpio_iot;
     52       1.5  shige 	bus_space_handle_t	sc_gpio_ioh;
     53       1.5  shige 	struct gpio_chipset_tag	sc_gpio_gc;
     54       1.5  shige 	gpio_pin_t		sc_gpio_pins[GPIO_NPINS];
     55       1.1  shige };
     56       1.1  shige 
     57       1.5  shige static int	gpio_opb_match(struct device *, struct cfdata *, void *);
     58       1.5  shige static void	gpio_opb_attach(struct device *, struct device *, void *);
     59       1.1  shige 
     60       1.5  shige CFATTACH_DECL(opbgpio, sizeof(struct gpio_opb_softc),
     61       1.5  shige 	gpio_opb_match, gpio_opb_attach, NULL, NULL);
     62       1.1  shige 
     63       1.5  shige static int	gpio_opb_pin_read(void *, int);
     64       1.5  shige static void	gpio_opb_pin_write(void *, int, int);
     65       1.5  shige static void	gpio_opb_pin_ctl(void *, int, int);
     66       1.1  shige 
     67       1.1  shige 
     68       1.1  shige static int
     69       1.5  shige gpio_opb_match(struct device *parent, struct cfdata *cf, void *aux)
     70       1.1  shige {
     71       1.5  shige 	struct opb_attach_args *oaa = aux;
     72       1.1  shige 
     73       1.1  shige 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
     74       1.5  shige 		return 0;
     75       1.1  shige 
     76       1.5  shige 	return 1;
     77       1.1  shige }
     78       1.1  shige 
     79       1.1  shige static void
     80       1.5  shige gpio_opb_attach(struct device *parent, struct device *self, void *aux)
     81       1.1  shige {
     82       1.5  shige 	struct gpio_opb_softc *sc = (struct gpio_opb_softc *)self;
     83       1.1  shige 	struct opb_attach_args *oaa = aux;
     84       1.5  shige 	struct gpiobus_attach_args gba;
     85       1.5  shige 	int i;
     86       1.5  shige 	uint32_t reg1, reg2, reg3;
     87       1.1  shige 
     88       1.1  shige 	aprint_naive(": GPIO controller\n");
     89       1.1  shige 	aprint_normal(": On-Chip GPIO controller\n");
     90       1.1  shige 
     91       1.5  shige 	/* Map GPIO I/O space */
     92       1.5  shige 	sc->sc_gpio_iot = oaa->opb_bt;
     93       1.5  shige 	bus_space_map(sc->sc_gpio_iot, oaa->opb_addr,
     94       1.5  shige 		GPIO_NREG, 0, &sc->sc_gpio_ioh);
     95       1.5  shige 
     96       1.5  shige 	/* Read current register status */
     97       1.5  shige 	reg1 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_IR);
     98       1.5  shige 	reg2 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_TCR);
     99       1.5  shige 	reg3 = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_ODR);
    100       1.5  shige 
    101  1.5.10.1     ad 	/* Initialize pins array */
    102       1.5  shige 	for (i = 0 ; i < GPIO_NPINS ; i++) {
    103       1.5  shige 		int p = i + 1;
    104       1.5  shige 		sc->sc_gpio_pins[i].pin_num = i;
    105       1.5  shige 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INOUT
    106       1.5  shige 						| GPIO_PIN_OPENDRAIN
    107       1.5  shige 						| GPIO_PIN_TRISTATE;
    108       1.5  shige 
    109       1.5  shige 		/* current defaults */
    110       1.5  shige 		sc->sc_gpio_pins[i].pin_flags =
    111       1.5  shige 			((reg3 >> GPIO_PIN_SHIFT(p)) & 0x01)
    112       1.5  shige 			? GPIO_PIN_OPENDRAIN
    113       1.5  shige 			: (((reg2 >> GPIO_PIN_SHIFT(p)) & 0x01)
    114       1.5  shige 				? GPIO_PIN_INOUT
    115       1.5  shige 				: GPIO_PIN_TRISTATE);
    116       1.5  shige 		sc->sc_gpio_pins[i].pin_state =
    117       1.5  shige 			((reg1 >> GPIO_PIN_SHIFT(p)) & 0x01);
    118       1.5  shige 		sc->sc_gpio_pins[i].pin_mapped = 0;
    119       1.5  shige 	}
    120       1.5  shige 
    121       1.5  shige 	/* Create controller tag */
    122       1.5  shige 	sc->sc_gpio_gc.gp_cookie = sc;
    123       1.5  shige 	sc->sc_gpio_gc.gp_pin_read = gpio_opb_pin_read;
    124       1.5  shige 	sc->sc_gpio_gc.gp_pin_write = gpio_opb_pin_write;
    125       1.5  shige 	sc->sc_gpio_gc.gp_pin_ctl = gpio_opb_pin_ctl;
    126       1.5  shige 
    127       1.5  shige 	gba.gba_gc = &sc->sc_gpio_gc;
    128       1.5  shige 	gba.gba_pins = sc->sc_gpio_pins;
    129       1.5  shige 	gba.gba_npins = GPIO_NPINS;
    130       1.5  shige 
    131       1.5  shige 	/* Attach GPIO framework */
    132       1.5  shige 	(void) config_found(&sc->sc_dev, &gba, gpiobus_print);
    133       1.1  shige }
    134       1.1  shige 
    135       1.1  shige static int
    136       1.5  shige gpio_opb_pin_read(void *arg, int pin)
    137       1.5  shige {
    138       1.5  shige 	struct gpio_opb_softc *sc = arg;
    139       1.5  shige 	uint32_t data;
    140       1.5  shige 	int p;
    141       1.1  shige 
    142       1.5  shige 	p = pin % GPIO_NPINS;
    143       1.5  shige 	p = p + 1;
    144       1.1  shige 
    145       1.5  shige 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_IR);
    146       1.1  shige 
    147       1.5  shige 	return (data >> GPIO_PIN_SHIFT(p)) & 0x01;
    148       1.1  shige }
    149       1.1  shige 
    150       1.1  shige static void
    151       1.5  shige gpio_opb_pin_write(void *arg, int pin, int value)
    152       1.1  shige {
    153       1.5  shige 	struct gpio_opb_softc *sc = arg;
    154       1.5  shige 	uint32_t data;
    155       1.5  shige 	int p;
    156       1.5  shige 
    157       1.5  shige 	p = pin % GPIO_NPINS;
    158       1.5  shige 	p = p + 1;
    159       1.5  shige 
    160       1.5  shige 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_OR);
    161       1.5  shige 	if (value == 0) {
    162       1.5  shige 		data &= ~(1 << GPIO_PIN_SHIFT(p));
    163       1.5  shige 	} else if (value == 1) {
    164       1.5  shige 		data |= (1 << GPIO_PIN_SHIFT(p));
    165       1.5  shige 	}
    166       1.1  shige 
    167       1.5  shige 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GPIO_OR, data);
    168       1.1  shige }
    169       1.1  shige 
    170       1.1  shige static void
    171       1.5  shige gpio_opb_pin_ctl(void *arg, int pin, int flags)
    172       1.1  shige {
    173       1.5  shige 	struct gpio_opb_softc *sc = arg;
    174       1.5  shige 	uint32_t data;
    175       1.5  shige 	int p;
    176       1.5  shige 
    177       1.5  shige 	p = pin % GPIO_NPINS;
    178       1.5  shige 	p = p + 1;
    179       1.5  shige 
    180       1.5  shige 	if (flags & GPIO_PIN_INOUT) {
    181       1.5  shige 		/* GPIOn_ODR register bit is 0 */
    182       1.5  shige 		data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    183       1.5  shige 					GPIO_ODR);
    184       1.5  shige 		data &= ~(1 << GPIO_PIN_SHIFT(p));
    185       1.5  shige 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    186       1.5  shige 					GPIO_ODR, data);
    187       1.5  shige 		/* GPIOn_TCR register bit is 1 */
    188       1.5  shige 		data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    189       1.5  shige 					GPIO_TCR);
    190       1.5  shige 		data |= (1 << GPIO_PIN_SHIFT(p));
    191       1.5  shige 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    192       1.5  shige 					GPIO_TCR, data);
    193       1.5  shige 	}
    194       1.5  shige 
    195       1.5  shige 	if (flags & GPIO_PIN_TRISTATE) {
    196       1.5  shige 		/* GPIOn_ODR register bit is 0 */
    197       1.5  shige 		data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    198       1.5  shige 					GPIO_ODR);
    199       1.5  shige 		data &= ~(1 << GPIO_PIN_SHIFT(p));
    200       1.5  shige 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    201       1.5  shige 					GPIO_ODR, data);
    202       1.5  shige 		/* GPIOn_TCR register bit is 0 */
    203       1.5  shige 		data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    204       1.5  shige 					GPIO_TCR);
    205       1.5  shige 		data &= ~(1 << GPIO_PIN_SHIFT(p));
    206       1.5  shige 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    207       1.5  shige 					GPIO_TCR, data);
    208       1.5  shige 	}
    209       1.5  shige 
    210       1.5  shige 	if (flags & GPIO_PIN_OPENDRAIN) {
    211       1.5  shige 		/* GPIOn_ODR register bit is 1 */
    212       1.5  shige 		data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    213       1.5  shige 					GPIO_ODR);
    214       1.5  shige 		data |= (1 << GPIO_PIN_SHIFT(p));
    215       1.5  shige 		bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    216       1.5  shige 					GPIO_ODR, data);
    217       1.5  shige 	}
    218       1.1  shige }
    219