Home | History | Annotate | Line # | Download | only in dev
gpio.c revision 1.5
      1 /*	$NetBSD: gpio.c,v 1.5 2002/10/02 05:30:41 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1998	Internet Research Institute, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by
     18  *	Internet Research Institute, Inc.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/types.h>
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 #include <machine/autoconf.h>
     41 #include <machine/pio.h>
     42 
     43 #include <dev/ofw/openfirm.h>
     44 
     45 #include "adb.h"
     46 
     47 static void gpio_obio_attach (struct device *, struct device *, void *);
     48 static int gpio_obio_match (struct device *, struct cfdata *, void *);
     49 static int gpio_obio_print (void *aux, const char *gpio);
     50 
     51 static void gpio_gpio_attach (struct device *, struct device *, void *);
     52 static int gpio_gpio_match (struct device *, struct cfdata *, void *);
     53 static int gpio_intr (void *);
     54 
     55 struct gpio_softc {
     56 	struct device sc_dev;
     57 	u_int8_t *sc_port;
     58 };
     59 
     60 CFATTACH_DECL(gpio_obio, sizeof(struct gpio_softc),
     61     gpio_obio_match, gpio_obio_attach, NULL, NULL);
     62 
     63 CFATTACH_DECL(gpio_gpio, sizeof(struct gpio_softc),
     64     gpio_gpio_match, gpio_gpio_attach, NULL, NULL);
     65 
     66 extern struct cfdriver gpio_cd;
     67 
     68 int
     69 gpio_obio_match(struct device *parent, struct cfdata *cf, void *aux)
     70 {
     71 	struct confargs *ca = aux;
     72 
     73 	if (strcmp(ca->ca_name, "gpio") != 0)
     74 		return 0;
     75 
     76 	if (ca->ca_nreg == 0)
     77 		return 0;
     78 
     79 	return 1;
     80 }
     81 
     82 void
     83 gpio_obio_attach(struct device *parent, struct device *self, void *aux)
     84 {
     85 	struct gpio_softc *sc = (struct gpio_softc *)self;
     86 	struct confargs *ca = aux, ca2;
     87 	int child;
     88 	int namelen;
     89 	int intr[6];
     90 	u_int reg[20];
     91 	char name[32];
     92 
     93 	printf("\n");
     94 
     95 	sc->sc_port = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1]);
     96 
     97 	ca2.ca_baseaddr = ca->ca_baseaddr;
     98 	for (child = OF_child(ca->ca_node); child; child = OF_peer(child)) {
     99 		namelen = OF_getprop(child, "name", name, sizeof(name));
    100 		if (namelen < 0)
    101 			continue;
    102 		if (namelen >= sizeof(name))
    103 			continue;
    104 
    105 		name[namelen] = 0;
    106 		ca2.ca_name = name;
    107 		ca2.ca_node = child;
    108 
    109 		ca2.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    110 		ca2.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    111 				sizeof(intr));
    112 		if (ca2.ca_nintr == -1)
    113 			ca2.ca_nintr = OF_getprop(child, "interrupts", intr,
    114 					sizeof(intr));
    115 
    116 		ca2.ca_reg = reg;
    117 		ca2.ca_intr = intr;
    118 
    119 		config_found(self, &ca2, gpio_obio_print);
    120 	}
    121 }
    122 
    123 int
    124 gpio_obio_print(void *aux, const char *gpio)
    125 {
    126 	struct confargs *ca = aux;
    127 
    128 	if (gpio)
    129 		printf("%s at %s", ca->ca_name, gpio);
    130 
    131 	if (ca->ca_nreg > 0)
    132 		printf(" offset 0x%x", ca->ca_reg[0]);
    133 
    134 	return UNCONF;
    135 }
    136 
    137 int
    138 gpio_gpio_match(struct device *parent, struct cfdata *cf, void *aux)
    139 {
    140 	struct confargs *ca = aux;
    141 
    142 	if (strcmp(ca->ca_name, "extint-gpio1") != 0)
    143 		return 0;
    144 
    145 	if (ca->ca_nintr < 0)
    146 		return 0;
    147 
    148 	return 1;
    149 }
    150 
    151 void
    152 gpio_gpio_attach(struct device *parent, struct device *self, void *aux)
    153 {
    154 	struct gpio_softc *sc = (struct gpio_softc *)self;
    155 	struct confargs *ca = aux;
    156 
    157 
    158 	sc->sc_port = ((struct gpio_softc *) parent)->sc_port;
    159 	intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_HIGH, gpio_intr, sc);
    160 
    161 	printf(" irq %d\n", ca->ca_intr[0]);
    162 }
    163 
    164 #if NADB > 0
    165 extern int adb_intr (void *);
    166 extern struct cfdriver adb_cd;
    167 #endif
    168 
    169 int
    170 gpio_intr(void *arg)
    171 {
    172 	int rv = 0;
    173 
    174 #if NADB > 0
    175 	if (adb_cd.cd_devs[0] != NULL)
    176 		rv = adb_intr(adb_cd.cd_devs[0]);
    177 #endif
    178 
    179 	return rv;
    180 }
    181