xref: /openbsd-src/sys/arch/macppc/dev/macgpio.c (revision 89ed722c33692ad18452b9d394c650f8adc43407)
1*89ed722cSmpi /*	$OpenBSD: macgpio.c,v 1.10 2022/03/13 12:33:01 mpi Exp $	*/
208f18d2cSdrahn /*	$NetBSD: gpio.c,v 1.2 2001/02/27 05:16:33 matt Exp $	*/
308f18d2cSdrahn 
408f18d2cSdrahn /*-
508f18d2cSdrahn  * Copyright (C) 1998	Internet Research Institute, Inc.
608f18d2cSdrahn  * All rights reserved.
708f18d2cSdrahn  *
808f18d2cSdrahn  * Redistribution and use in source and binary forms, with or without
908f18d2cSdrahn  * modification, are permitted provided that the following conditions
1008f18d2cSdrahn  * are met:
1108f18d2cSdrahn  * 1. Redistributions of source code must retain the above copyright
1208f18d2cSdrahn  *    notice, this list of conditions and the following disclaimer.
1308f18d2cSdrahn  * 2. Redistributions in binary form must reproduce the above copyright
1408f18d2cSdrahn  *    notice, this list of conditions and the following disclaimer in the
1508f18d2cSdrahn  *    documentation and/or other materials provided with the distribution.
1608f18d2cSdrahn  * 3. All advertising materials mentioning features or use of this software
1708f18d2cSdrahn  *    must display the following acknowledgement:
1808f18d2cSdrahn  *	This product includes software developed by
1908f18d2cSdrahn  *	Internet Research Institute, Inc.
2008f18d2cSdrahn  * 4. The name of the author may not be used to endorse or promote products
2108f18d2cSdrahn  *    derived from this software without specific prior written permission.
2208f18d2cSdrahn  *
2308f18d2cSdrahn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2408f18d2cSdrahn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2508f18d2cSdrahn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2608f18d2cSdrahn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2708f18d2cSdrahn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2808f18d2cSdrahn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2908f18d2cSdrahn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3008f18d2cSdrahn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3108f18d2cSdrahn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3208f18d2cSdrahn  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3308f18d2cSdrahn  */
3408f18d2cSdrahn 
3508f18d2cSdrahn #include <sys/param.h>
3608f18d2cSdrahn #include <sys/systm.h>
3708f18d2cSdrahn #include <sys/kernel.h>
3808f18d2cSdrahn #include <sys/device.h>
3908f18d2cSdrahn #include <sys/malloc.h>
4008f18d2cSdrahn 
4108f18d2cSdrahn #include <dev/ofw/openfirm.h>
4208f18d2cSdrahn 
4308f18d2cSdrahn #include <machine/autoconf.h>
4408f18d2cSdrahn #include <machine/pio.h>
4508f18d2cSdrahn 
4608f18d2cSdrahn #include "adb.h"
4708f18d2cSdrahn 
4808f18d2cSdrahn static void macgpio_attach (struct device *, struct device *, void *);
4908f18d2cSdrahn static int macgpio_match (struct device *, void *, void *);
5008f18d2cSdrahn static int macgpio_print (void *aux, const char *gpio);
5108f18d2cSdrahn 
5208f18d2cSdrahn static void macgpio_gpio_attach (struct device *, struct device *, void *);
5308f18d2cSdrahn static int macgpio_gpio_match (struct device *, void *, void *);
5408f18d2cSdrahn static int gpio_intr (void *);
5508f18d2cSdrahn 
5608f18d2cSdrahn struct gpio_softc {
5708f18d2cSdrahn 	struct device sc_dev;
5808f18d2cSdrahn 	u_int8_t *sc_port;
5908f18d2cSdrahn };
6008f18d2cSdrahn 
61*89ed722cSmpi const struct cfattach macgpio_ca = {
6208f18d2cSdrahn 	sizeof(struct gpio_softc), macgpio_match, macgpio_attach
6308f18d2cSdrahn };
6408f18d2cSdrahn 
65*89ed722cSmpi const struct cfattach macgpio_gpio_ca = {
6608f18d2cSdrahn 	sizeof(struct gpio_softc), macgpio_gpio_match, macgpio_gpio_attach
6708f18d2cSdrahn };
6808f18d2cSdrahn 
6908f18d2cSdrahn struct cfdriver macgpio_cd = {
7008f18d2cSdrahn 	NULL, "macgpio", DV_DULL
7108f18d2cSdrahn };
7208f18d2cSdrahn 
7308f18d2cSdrahn int
macgpio_match(struct device * parent,void * cf,void * aux)7408f18d2cSdrahn macgpio_match(struct device *parent, void *cf, void *aux)
7508f18d2cSdrahn {
7608f18d2cSdrahn 	struct confargs *ca = aux;
7708f18d2cSdrahn 
7808f18d2cSdrahn 	if (strcmp(ca->ca_name, "gpio") != 0)
7908f18d2cSdrahn 		return 0;
8008f18d2cSdrahn 
8184b94647Smiod 	if (ca->ca_nreg < 8)
8208f18d2cSdrahn 		return 0;
8308f18d2cSdrahn 
8408f18d2cSdrahn 	return 1;
8508f18d2cSdrahn }
8608f18d2cSdrahn 
8708f18d2cSdrahn void
macgpio_attach(struct device * parent,struct device * self,void * aux)8808f18d2cSdrahn macgpio_attach(struct device *parent, struct device *self, void *aux)
8908f18d2cSdrahn {
9008f18d2cSdrahn 	struct gpio_softc *sc = (struct gpio_softc *)self;
9108f18d2cSdrahn 	struct confargs *ca = aux, ca2;
9208f18d2cSdrahn 	int child;
9308f18d2cSdrahn 	int namelen;
9408f18d2cSdrahn 	int intr[6];
9508f18d2cSdrahn 	u_int reg[20];
9608f18d2cSdrahn 	char name[32];
9708f18d2cSdrahn 
9808f18d2cSdrahn 	printf("\n");
9908f18d2cSdrahn 
10008f18d2cSdrahn 	sc->sc_port = mapiodev(ca->ca_baseaddr + ca->ca_reg[0], ca->ca_reg[1]);
10108f18d2cSdrahn 
10208f18d2cSdrahn 	ca2.ca_baseaddr = ca->ca_baseaddr;
10308f18d2cSdrahn 	for (child = OF_child(ca->ca_node); child; child = OF_peer(child)) {
10408f18d2cSdrahn 		namelen = OF_getprop(child, "name", name, sizeof(name));
10508f18d2cSdrahn 		if (namelen < 0)
10608f18d2cSdrahn 			continue;
10708f18d2cSdrahn 		if (namelen >= sizeof(name))
10808f18d2cSdrahn 			continue;
10908f18d2cSdrahn 
11008f18d2cSdrahn 		name[namelen] = 0;
11108f18d2cSdrahn 		ca2.ca_name = name;
11208f18d2cSdrahn 		ca2.ca_node = child;
11308f18d2cSdrahn 
11408f18d2cSdrahn 		ca2.ca_nreg  = OF_getprop(child, "reg", reg, sizeof(reg));
11508f18d2cSdrahn 		ca2.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
11608f18d2cSdrahn 				sizeof(intr));
11708f18d2cSdrahn 		if (ca2.ca_nintr == -1)
11808f18d2cSdrahn 			ca2.ca_nintr = OF_getprop(child, "interrupts", intr,
11908f18d2cSdrahn 					sizeof(intr));
12008f18d2cSdrahn 
12108f18d2cSdrahn 		ca2.ca_reg = reg;
12208f18d2cSdrahn 		ca2.ca_intr = intr;
12308f18d2cSdrahn 
12408f18d2cSdrahn 		config_found(self, &ca2, macgpio_print);
12508f18d2cSdrahn 	}
12608f18d2cSdrahn }
12708f18d2cSdrahn 
12808f18d2cSdrahn int
macgpio_print(void * aux,const char * gpio)12908f18d2cSdrahn macgpio_print(void *aux, const char *gpio)
13008f18d2cSdrahn {
13108f18d2cSdrahn 	struct confargs *ca = aux;
13208f18d2cSdrahn 	if (gpio)
133f93ad0cbSkettenis 		printf("\"%s\" at %s", ca->ca_name, gpio);
13408f18d2cSdrahn 
13508f18d2cSdrahn 	if (ca->ca_nreg > 0)
13608f18d2cSdrahn 		printf(" offset 0x%x", ca->ca_reg[0]);
13708f18d2cSdrahn 
13808f18d2cSdrahn 	return UNCONF;
13908f18d2cSdrahn }
14008f18d2cSdrahn 
14108f18d2cSdrahn int
macgpio_gpio_match(struct device * parent,void * cf,void * aux)14208f18d2cSdrahn macgpio_gpio_match(struct device *parent, void *cf, void *aux)
14308f18d2cSdrahn {
14408f18d2cSdrahn 	struct confargs *ca = aux;
14508f18d2cSdrahn 
14608f18d2cSdrahn 	if (strcmp(ca->ca_name, "extint-gpio1") != 0)
14708f18d2cSdrahn 		return 0;
14808f18d2cSdrahn 
14984b94647Smiod 	if (ca->ca_nintr < 4)
15008f18d2cSdrahn 		return 0;
15108f18d2cSdrahn 
15208f18d2cSdrahn 	return 1;
15308f18d2cSdrahn }
15408f18d2cSdrahn 
15508f18d2cSdrahn void
macgpio_gpio_attach(struct device * parent,struct device * self,void * aux)15608f18d2cSdrahn macgpio_gpio_attach(struct device *parent, struct device *self, void *aux)
15708f18d2cSdrahn {
15808f18d2cSdrahn 	struct gpio_softc *sc = (struct gpio_softc *)self;
15908f18d2cSdrahn 	struct confargs *ca = aux;
16008f18d2cSdrahn 
16108f18d2cSdrahn 
16208f18d2cSdrahn 	sc->sc_port = ((struct gpio_softc *) parent)->sc_port;
163bb536b7dSmpi 	mac_intr_establish(parent, ca->ca_intr[0], IST_LEVEL, IPL_TTY,
1641ac74572Sderaadt 	    gpio_intr, sc, sc->sc_dev.dv_xname);
16508f18d2cSdrahn 
166c767eae2Smpi 	printf(": irq %d\n", ca->ca_intr[0]);
16708f18d2cSdrahn }
16808f18d2cSdrahn 
16908f18d2cSdrahn #if NADB > 0
17008f18d2cSdrahn extern int adb_intr (void *);
17108f18d2cSdrahn extern struct cfdriver adb_cd;
17208f18d2cSdrahn #endif
17308f18d2cSdrahn 
17408f18d2cSdrahn int
gpio_intr(void * arg)17508f18d2cSdrahn gpio_intr(void *arg)
17608f18d2cSdrahn {
17708f18d2cSdrahn 	int rv = 0;
17808f18d2cSdrahn 
17908f18d2cSdrahn #if NADB > 0
18008f18d2cSdrahn 	if (adb_cd.cd_devs[0] != NULL)
18108f18d2cSdrahn 		rv = adb_intr(adb_cd.cd_devs[0]);
18208f18d2cSdrahn #endif
18308f18d2cSdrahn 
18408f18d2cSdrahn 	return rv;
18508f18d2cSdrahn }
186