Home | History | Annotate | Line # | Download | only in dev
gpio_opb.c revision 1.1
      1  1.1  shige /*	$NetBSD: gpio_opb.c,v 1.1 2005/01/23 19:22:22 shige 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.1  shige #include <powerpc/ibm4xx/dcr405gp.h>
     42  1.1  shige #include <powerpc/ibm4xx/dev/opbvar.h>
     43  1.1  shige #include <powerpc/ibm4xx/dev/gpioreg.h>
     44  1.1  shige #include <powerpc/ibm4xx/dev/gpiovar.h>
     45  1.1  shige 
     46  1.1  shige struct gpio_softc {
     47  1.1  shige 	struct device		sc_dev;		/* device generic */
     48  1.1  shige 	struct gpio_controller	sc_gpio;	/* GPIO controller */
     49  1.1  shige         u_long			sc_addr;	/* GPIO controller address */
     50  1.1  shige };
     51  1.1  shige 
     52  1.1  shige static int	gpio_print(void *, const char *);
     53  1.1  shige static int	gpio_search(struct device *, struct cfdata *,
     54  1.1  shige 			const locdesc_t *ldesc, void *aux);
     55  1.1  shige static int	gpio_match(struct device *, struct cfdata *, void *);
     56  1.1  shige static void	gpio_attach(struct device *, struct device *, void *);
     57  1.1  shige 
     58  1.1  shige static int	gpio_or_read(void *, int);
     59  1.1  shige static int	gpio_tcr_read(void *, int);
     60  1.1  shige static int	gpio_odr_read(void *, int);
     61  1.1  shige static int	gpio_ir_read(void *, int);
     62  1.1  shige static void	gpio_or_write(void *arg, int addr, int bit);
     63  1.1  shige static void	gpio_tcr_write(void *arg, int addr, int bit);
     64  1.1  shige static void	gpio_odr_write(void *arg, int addr, int bit);
     65  1.1  shige 
     66  1.1  shige static int	gpio_read_bit(void *, int, int);
     67  1.1  shige static void	gpio_write_bit(void *, int, int, int);
     68  1.1  shige static uint32_t	gpio_read(void *, int);
     69  1.1  shige static void	gpio_write(void *, int, uint32_t);
     70  1.1  shige 
     71  1.1  shige CFATTACH_DECL(gpio, sizeof(struct gpio_softc),
     72  1.1  shige     gpio_match, gpio_attach, NULL, NULL);
     73  1.1  shige 
     74  1.1  shige static int
     75  1.1  shige gpio_print(void *aux, const char *pnp)
     76  1.1  shige {
     77  1.1  shige 	struct gpio_attach_args *gaa = aux;
     78  1.1  shige 
     79  1.1  shige 	aprint_normal(" addr GPIO#%d", gaa->ga_addr);
     80  1.1  shige 
     81  1.1  shige 	return (UNCONF);
     82  1.1  shige }
     83  1.1  shige 
     84  1.1  shige static int
     85  1.1  shige gpio_search(struct device *parent, struct cfdata *cf,
     86  1.1  shige 	const locdesc_t *ldesc, void *aux)
     87  1.1  shige {
     88  1.1  shige 	struct gpio_softc *sc = (void *)parent;
     89  1.1  shige 	struct gpio_attach_args gaa;
     90  1.1  shige 
     91  1.1  shige 	gaa.ga_tag = &sc->sc_gpio;
     92  1.1  shige 	gaa.ga_addr = cf->cf_loc[GPIOCF_ADDR];
     93  1.1  shige 
     94  1.1  shige 	if (config_match(parent, cf, &gaa) > 0)
     95  1.1  shige 		config_attach(parent, cf, &gaa, gpio_print);
     96  1.1  shige 
     97  1.1  shige 	return (0);
     98  1.1  shige }
     99  1.1  shige 
    100  1.1  shige static int
    101  1.1  shige gpio_match(struct device *parent, struct cfdata *cf, void *args)
    102  1.1  shige {
    103  1.1  shige 	struct opb_attach_args *oaa = args;
    104  1.1  shige 
    105  1.1  shige 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
    106  1.1  shige 		return (0);
    107  1.1  shige 
    108  1.1  shige 	return (1);
    109  1.1  shige }
    110  1.1  shige 
    111  1.1  shige static void
    112  1.1  shige gpio_attach(struct device *parent, struct device *self, void *aux)
    113  1.1  shige {
    114  1.1  shige 	struct gpio_softc *sc = (struct gpio_softc *)self;
    115  1.1  shige 	struct opb_attach_args *oaa = aux;
    116  1.1  shige 
    117  1.1  shige 	aprint_naive(": GPIO controller\n");
    118  1.1  shige 	aprint_normal(": On-Chip GPIO controller\n");
    119  1.1  shige 
    120  1.1  shige 	sc->sc_addr = oaa->opb_addr;
    121  1.1  shige 	sc->sc_gpio.cookie = sc;
    122  1.1  shige 	sc->sc_gpio.io_or_read = gpio_or_read;
    123  1.1  shige 	sc->sc_gpio.io_tcr_read = gpio_tcr_read;
    124  1.1  shige 	sc->sc_gpio.io_odr_read = gpio_odr_read;
    125  1.1  shige 	sc->sc_gpio.io_ir_read = gpio_ir_read;
    126  1.1  shige 	sc->sc_gpio.io_or_write = gpio_or_write;
    127  1.1  shige 	sc->sc_gpio.io_tcr_write = gpio_tcr_write;
    128  1.1  shige 	sc->sc_gpio.io_odr_write = gpio_odr_write;
    129  1.1  shige 
    130  1.1  shige 	(void) config_search_ia(gpio_search, self, "gpio", NULL);
    131  1.1  shige }
    132  1.1  shige 
    133  1.1  shige static int
    134  1.1  shige gpio_or_read(void *arg, int addr)
    135  1.1  shige {
    136  1.1  shige 	return (gpio_read_bit(arg, GPIO_OR, addr));
    137  1.1  shige }
    138  1.1  shige 
    139  1.1  shige static int
    140  1.1  shige gpio_tcr_read(void *arg, int addr)
    141  1.1  shige {
    142  1.1  shige 	return (gpio_read_bit(arg, GPIO_TCR, addr));
    143  1.1  shige }
    144  1.1  shige 
    145  1.1  shige static int
    146  1.1  shige gpio_odr_read(void *arg, int addr)
    147  1.1  shige {
    148  1.1  shige 	return (gpio_read_bit(arg, GPIO_ODR, addr));
    149  1.1  shige }
    150  1.1  shige 
    151  1.1  shige static int
    152  1.1  shige gpio_ir_read(void *arg, int addr)
    153  1.1  shige {
    154  1.1  shige 	gpio_write_bit(arg, GPIO_ODR, addr, 0);
    155  1.1  shige 	gpio_write_bit(arg, GPIO_TCR, addr, 0);
    156  1.1  shige 	return (gpio_read_bit(arg, GPIO_ODR, addr));
    157  1.1  shige }
    158  1.1  shige 
    159  1.1  shige static void
    160  1.1  shige gpio_or_write(void *arg, int addr, int bit)
    161  1.1  shige {
    162  1.1  shige 	gpio_write_bit(arg, GPIO_ODR, addr, 0);
    163  1.1  shige 	gpio_write_bit(arg, GPIO_TCR, addr, 1);
    164  1.1  shige 	gpio_write_bit(arg, GPIO_OR, addr, bit);
    165  1.1  shige }
    166  1.1  shige 
    167  1.1  shige static void
    168  1.1  shige gpio_tcr_write(void *arg, int addr, int bit)
    169  1.1  shige {
    170  1.1  shige 	gpio_write_bit(arg, GPIO_TCR, addr, bit);
    171  1.1  shige }
    172  1.1  shige 
    173  1.1  shige static void
    174  1.1  shige gpio_odr_write(void *arg, int addr, int bit)
    175  1.1  shige {
    176  1.1  shige 	gpio_write_bit(arg, GPIO_ODR, addr, bit);
    177  1.1  shige }
    178  1.1  shige 
    179  1.1  shige static int
    180  1.1  shige gpio_read_bit(void *arg, int offset, int addr)
    181  1.1  shige {
    182  1.1  shige 	uint32_t rv = gpio_read(arg, offset);
    183  1.1  shige 	uint32_t mask = GPIO_SBIT(addr);
    184  1.1  shige 
    185  1.1  shige 	return ((rv & mask) >> GPIO_SHIFT(addr));
    186  1.1  shige }
    187  1.1  shige 
    188  1.1  shige void
    189  1.1  shige gpio_write_bit(void *arg, int offset, int addr, int bit)
    190  1.1  shige {
    191  1.1  shige 	uint32_t rv = gpio_read(arg, offset);
    192  1.1  shige 	uint32_t mask = GPIO_SBIT(addr);
    193  1.1  shige 
    194  1.1  shige 	rv = rv & ~mask;
    195  1.1  shige 	rv = rv | ((bit << GPIO_SHIFT(addr)) & mask);
    196  1.1  shige 	gpio_write(arg, offset, rv);
    197  1.1  shige }
    198  1.1  shige 
    199  1.1  shige static uint32_t
    200  1.1  shige gpio_read(void *arg, int offset)
    201  1.1  shige {
    202  1.1  shige 	struct gpio_softc *sc = arg;
    203  1.1  shige 	uint32_t rv;
    204  1.1  shige 
    205  1.1  shige         rv = inl(sc->sc_addr + offset);
    206  1.1  shige 
    207  1.1  shige 	return rv;
    208  1.1  shige }
    209  1.1  shige 
    210  1.1  shige static void
    211  1.1  shige gpio_write(void *arg, int offset, uint32_t out)
    212  1.1  shige {
    213  1.1  shige 	struct gpio_softc *sc = arg;
    214  1.1  shige 
    215  1.1  shige         outl((sc->sc_addr + offset), out);
    216  1.1  shige }
    217