Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: gpio.c,v 1.17 2023/12/20 15:29:04 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: gpio.c,v 1.17 2023/12/20 15:29:04 thorpej Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <machine/autoconf.h>
     43 #include <machine/pio.h>
     44 
     45 #include <dev/ofw/openfirm.h>
     46 
     47 #include "adb.h"
     48 
     49 static void gpio_obio_attach (device_t, device_t, void *);
     50 static int gpio_obio_match (device_t, cfdata_t, void *);
     51 static int gpio_obio_print (void *aux, const char *gpio);
     52 
     53 static void gpio_gpio_attach (device_t, device_t, void *);
     54 static int gpio_gpio_match (device_t, cfdata_t, void *);
     55 static int gpio_intr (void *);
     56 
     57 struct gpio_softc {
     58 	u_int8_t *sc_port;
     59 };
     60 
     61 CFATTACH_DECL_NEW(gpio_obio, sizeof(struct gpio_softc),
     62     gpio_obio_match, gpio_obio_attach, NULL, NULL);
     63 
     64 CFATTACH_DECL_NEW(gpio_gpio, sizeof(struct gpio_softc),
     65     gpio_gpio_match, gpio_gpio_attach, NULL, NULL);
     66 
     67 extern struct cfdriver gpio_cd;
     68 
     69 int
     70 gpio_obio_match(device_t parent, cfdata_t cf, void *aux)
     71 {
     72 	struct confargs *ca = aux;
     73 
     74 	if (strcmp(ca->ca_name, "gpio") != 0)
     75 		return 0;
     76 
     77 	if (ca->ca_nreg == 0)
     78 		return 0;
     79 
     80 	return 1;
     81 }
     82 
     83 void
     84 gpio_obio_attach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct gpio_softc *sc = device_private(self);
     87 	struct confargs *ca = aux, ca2;
     88 	int child;
     89 	int namelen;
     90 	int intr[6];
     91 	u_int reg[20];
     92 	char name[32];
     93 
     94 	printf("\n");
     95 
     96 	sc->sc_port = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1],
     97 	    false);
     98 
     99 	devhandle_t selfh = device_handle(self);
    100 	ca2.ca_baseaddr = ca->ca_baseaddr;
    101 	for (child = OF_child(ca->ca_node); child; child = OF_peer(child)) {
    102 		namelen = OF_getprop(child, "name", name, sizeof(name));
    103 		if (namelen < 0)
    104 			continue;
    105 		if (namelen >= sizeof(name))
    106 			continue;
    107 
    108 		name[namelen] = 0;
    109 		ca2.ca_name = name;
    110 		ca2.ca_node = child;
    111 
    112 		ca2.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
    113 		ca2.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    114 				sizeof(intr));
    115 		if (ca2.ca_nintr == -1)
    116 			ca2.ca_nintr = OF_getprop(child, "interrupts", intr,
    117 					sizeof(intr));
    118 
    119 		ca2.ca_reg = reg;
    120 		ca2.ca_intr = intr;
    121 
    122 		config_found(self, &ca2, gpio_obio_print,
    123 		    CFARGS(.devhandle = devhandle_from_of(selfh, child)));
    124 	}
    125 }
    126 
    127 int
    128 gpio_obio_print(void *aux, const char *gpio)
    129 {
    130 	struct confargs *ca = aux;
    131 
    132 	if (gpio)
    133 		aprint_normal("%s at %s", ca->ca_name, gpio);
    134 
    135 	if (ca->ca_nreg > 0)
    136 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
    137 
    138 	return UNCONF;
    139 }
    140 
    141 int
    142 gpio_gpio_match(device_t parent, cfdata_t cf, void *aux)
    143 {
    144 	struct confargs *ca = aux;
    145 
    146 	if (strcmp(ca->ca_name, "extint-gpio1") != 0)
    147 		return 0;
    148 
    149 	if (ca->ca_nintr < 0)
    150 		return 0;
    151 
    152 	return 1;
    153 }
    154 
    155 void
    156 gpio_gpio_attach(device_t parent, device_t self, void *aux)
    157 {
    158 	struct gpio_softc *sc = device_private(self);
    159 	struct confargs *ca = aux;
    160 
    161 
    162 	sc->sc_port = device_private(parent)->sc_port;
    163 	intr_establish_xname(ca->ca_intr[0], IST_LEVEL, IPL_HIGH, gpio_intr, sc,
    164 	    device_xname(self));
    165 
    166 	printf(" irq %d\n", ca->ca_intr[0]);
    167 }
    168 
    169 #if NADB > 0
    170 extern int adb_intr (void *);
    171 extern struct cfdriver adb_cd;
    172 #endif
    173 
    174 int
    175 gpio_intr(void *arg)
    176 {
    177 	int rv = 0;
    178 
    179 #if NADB > 0
    180 	struct gpio_softc *sc;
    181 
    182 	sc = device_lookup_private(&adb_cd, 0);
    183 	if (sc != NULL)
    184 		rv = adb_intr(sc);
    185 #endif
    186 
    187 	return rv;
    188 }
    189