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