xref: /freebsd/sys/powerpc/powermac/macgpio.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
1b4dbc599SNathan Whitehorn /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
4ecf558fdSNathan Whitehorn  * Copyright 2008 by Nathan Whitehorn. All rights reserved.
5b4dbc599SNathan Whitehorn  *
6b4dbc599SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
7b4dbc599SNathan Whitehorn  * modification, are permitted provided that the following conditions
8b4dbc599SNathan Whitehorn  * are met:
9b4dbc599SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
10b4dbc599SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
11b4dbc599SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
12b4dbc599SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
13b4dbc599SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
14b4dbc599SNathan Whitehorn  *
15b4dbc599SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16b4dbc599SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17b4dbc599SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18b4dbc599SNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19b4dbc599SNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20b4dbc599SNathan Whitehorn  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21b4dbc599SNathan Whitehorn  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22b4dbc599SNathan Whitehorn  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23b4dbc599SNathan Whitehorn  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b4dbc599SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b4dbc599SNathan Whitehorn  * SUCH DAMAGE.
26b4dbc599SNathan Whitehorn  */
27b4dbc599SNathan Whitehorn 
28b4dbc599SNathan Whitehorn /*
29b4dbc599SNathan Whitehorn  * Driver for MacIO GPIO controller
30b4dbc599SNathan Whitehorn  */
31b4dbc599SNathan Whitehorn 
32b4dbc599SNathan Whitehorn #include <sys/param.h>
33b4dbc599SNathan Whitehorn #include <sys/systm.h>
34b4dbc599SNathan Whitehorn #include <sys/kernel.h>
35b4dbc599SNathan Whitehorn #include <sys/malloc.h>
36b4dbc599SNathan Whitehorn #include <sys/module.h>
37b4dbc599SNathan Whitehorn #include <sys/bus.h>
38b4dbc599SNathan Whitehorn #include <sys/rman.h>
39b4dbc599SNathan Whitehorn 
40b4dbc599SNathan Whitehorn #include <vm/vm.h>
41b4dbc599SNathan Whitehorn #include <vm/pmap.h>
42b4dbc599SNathan Whitehorn 
43eaef5f0aSNathan Whitehorn #include <machine/bus.h>
44eaef5f0aSNathan Whitehorn #include <machine/intr_machdep.h>
45b4dbc599SNathan Whitehorn #include <machine/resource.h>
46eaef5f0aSNathan Whitehorn #include <machine/vmparam.h>
47b4dbc599SNathan Whitehorn 
48b4dbc599SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
49b4dbc599SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
50b4dbc599SNathan Whitehorn #include <dev/ofw/openfirm.h>
51b4dbc599SNathan Whitehorn 
52ecf558fdSNathan Whitehorn #include <powerpc/powermac/macgpiovar.h>
53ecf558fdSNathan Whitehorn 
54b4dbc599SNathan Whitehorn /*
55b4dbc599SNathan Whitehorn  * Macgpio softc
56b4dbc599SNathan Whitehorn  */
57b4dbc599SNathan Whitehorn struct macgpio_softc {
58b4dbc599SNathan Whitehorn 	phandle_t	sc_node;
59b4dbc599SNathan Whitehorn 	struct resource	*sc_gpios;
60b4dbc599SNathan Whitehorn 	int		sc_gpios_rid;
61e68afcedSJustin Hibbits 	uint32_t	sc_saved_gpio_levels[2];
62e68afcedSJustin Hibbits 	uint32_t	sc_saved_gpios[GPIO_COUNT];
63e68afcedSJustin Hibbits 	uint32_t	sc_saved_extint_gpios[GPIO_EXTINT_COUNT];
64b4dbc599SNathan Whitehorn };
65b4dbc599SNathan Whitehorn 
66b4dbc599SNathan Whitehorn static MALLOC_DEFINE(M_MACGPIO, "macgpio", "macgpio device information");
67b4dbc599SNathan Whitehorn 
68b4dbc599SNathan Whitehorn static int	macgpio_probe(device_t);
69b4dbc599SNathan Whitehorn static int	macgpio_attach(device_t);
70b4dbc599SNathan Whitehorn static int	macgpio_print_child(device_t dev, device_t child);
71b4dbc599SNathan Whitehorn static void	macgpio_probe_nomatch(device_t, device_t);
72b4dbc599SNathan Whitehorn static struct resource *macgpio_alloc_resource(device_t, device_t, int, int *,
732dd1bdf1SJustin Hibbits 		    rman_res_t, rman_res_t, rman_res_t, u_int);
742baed46eSJohn Baldwin static int	macgpio_activate_resource(device_t, device_t,
75b4dbc599SNathan Whitehorn 		    struct resource *);
762baed46eSJohn Baldwin static int	macgpio_deactivate_resource(device_t, device_t,
77b4dbc599SNathan Whitehorn 		    struct resource *);
78b4dbc599SNathan Whitehorn static ofw_bus_get_devinfo_t macgpio_get_devinfo;
79e68afcedSJustin Hibbits static int	macgpio_suspend(device_t dev);
80e68afcedSJustin Hibbits static int	macgpio_resume(device_t dev);
81b4dbc599SNathan Whitehorn 
82b4dbc599SNathan Whitehorn /*
83b4dbc599SNathan Whitehorn  * Bus interface definition
84b4dbc599SNathan Whitehorn  */
85b4dbc599SNathan Whitehorn static device_method_t macgpio_methods[] = {
86b4dbc599SNathan Whitehorn 	/* Device interface */
87b4dbc599SNathan Whitehorn 	DEVMETHOD(device_probe,         macgpio_probe),
88b4dbc599SNathan Whitehorn 	DEVMETHOD(device_attach,        macgpio_attach),
89b4dbc599SNathan Whitehorn 	DEVMETHOD(device_detach,        bus_generic_detach),
90b4dbc599SNathan Whitehorn 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
91e68afcedSJustin Hibbits 	DEVMETHOD(device_suspend,       macgpio_suspend),
92e68afcedSJustin Hibbits 	DEVMETHOD(device_resume,        macgpio_resume),
93b4dbc599SNathan Whitehorn 
94b4dbc599SNathan Whitehorn 	/* Bus interface */
95b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_print_child,      macgpio_print_child),
96b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_probe_nomatch,    macgpio_probe_nomatch),
97b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
98b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
99b4dbc599SNathan Whitehorn 
100b4dbc599SNathan Whitehorn         DEVMETHOD(bus_alloc_resource,   macgpio_alloc_resource),
101b4dbc599SNathan Whitehorn         DEVMETHOD(bus_activate_resource, macgpio_activate_resource),
102b4dbc599SNathan Whitehorn         DEVMETHOD(bus_deactivate_resource, macgpio_deactivate_resource),
103b4dbc599SNathan Whitehorn         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
104b4dbc599SNathan Whitehorn 
105ddfc9c4cSWarner Losh 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
1064fe8025dSNathan Whitehorn 
107b4dbc599SNathan Whitehorn 	/* ofw_bus interface */
108b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_devinfo,	macgpio_get_devinfo),
109b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
110b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
111b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
112b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
113b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
114b4dbc599SNathan Whitehorn 	{ 0, 0 }
115b4dbc599SNathan Whitehorn };
116b4dbc599SNathan Whitehorn 
117b4dbc599SNathan Whitehorn static driver_t macgpio_pci_driver = {
118b4dbc599SNathan Whitehorn         "macgpio",
119b4dbc599SNathan Whitehorn         macgpio_methods,
120b4dbc599SNathan Whitehorn 	sizeof(struct macgpio_softc)
121b4dbc599SNathan Whitehorn };
122b4dbc599SNathan Whitehorn 
123992ae60bSJohn Baldwin EARLY_DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, 0, 0, BUS_PASS_BUS);
124b4dbc599SNathan Whitehorn 
125b4dbc599SNathan Whitehorn struct macgpio_devinfo {
126b4dbc599SNathan Whitehorn 	struct ofw_bus_devinfo mdi_obdinfo;
127b4dbc599SNathan Whitehorn 	struct resource_list mdi_resources;
128b4dbc599SNathan Whitehorn 
129b4dbc599SNathan Whitehorn 	int gpio_num;
130b4dbc599SNathan Whitehorn };
131b4dbc599SNathan Whitehorn 
132b4dbc599SNathan Whitehorn static int
macgpio_probe(device_t dev)133b4dbc599SNathan Whitehorn macgpio_probe(device_t dev)
134b4dbc599SNathan Whitehorn {
135b4dbc599SNathan Whitehorn 	const char *name;
136b4dbc599SNathan Whitehorn 
137b4dbc599SNathan Whitehorn 	name = ofw_bus_get_name(dev);
138b4dbc599SNathan Whitehorn 	if (name && strcmp(name, "gpio") == 0) {
139b4dbc599SNathan Whitehorn 		device_set_desc(dev, "MacIO GPIO Controller");
140b4dbc599SNathan Whitehorn 		return (0);
141b4dbc599SNathan Whitehorn 	}
142b4dbc599SNathan Whitehorn 
143b4dbc599SNathan Whitehorn         return (ENXIO);
144b4dbc599SNathan Whitehorn }
145b4dbc599SNathan Whitehorn 
146b4dbc599SNathan Whitehorn /*
147b4dbc599SNathan Whitehorn  * Scan Open Firmware child nodes, and attach these as children
148b4dbc599SNathan Whitehorn  * of the macgpio bus
149b4dbc599SNathan Whitehorn  */
150b4dbc599SNathan Whitehorn static int
macgpio_attach(device_t dev)151b4dbc599SNathan Whitehorn macgpio_attach(device_t dev)
152b4dbc599SNathan Whitehorn {
153b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
154b4dbc599SNathan Whitehorn         struct macgpio_devinfo *dinfo;
155eaef5f0aSNathan Whitehorn         phandle_t root, child, iparent;
156b4dbc599SNathan Whitehorn         device_t cdev;
1577c4f1a1cSJustin Hibbits 	uint32_t irq[2];
158b4dbc599SNathan Whitehorn 
159b4dbc599SNathan Whitehorn 	sc = device_get_softc(dev);
160b4dbc599SNathan Whitehorn 	root = sc->sc_node = ofw_bus_get_node(dev);
161b4dbc599SNathan Whitehorn 
162b4dbc599SNathan Whitehorn 	sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
163b4dbc599SNathan Whitehorn 	    &sc->sc_gpios_rid, RF_ACTIVE);
164b4dbc599SNathan Whitehorn 
165b4dbc599SNathan Whitehorn 	/*
166b4dbc599SNathan Whitehorn 	 * Iterate through the sub-devices
167b4dbc599SNathan Whitehorn 	 */
168b4dbc599SNathan Whitehorn 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
169b4dbc599SNathan Whitehorn 		dinfo = malloc(sizeof(*dinfo), M_MACGPIO, M_WAITOK | M_ZERO);
170b4dbc599SNathan Whitehorn 		if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
171b4dbc599SNathan Whitehorn 		    0) {
172b4dbc599SNathan Whitehorn 			free(dinfo, M_MACGPIO);
173b4dbc599SNathan Whitehorn 			continue;
174b4dbc599SNathan Whitehorn 		}
175b4dbc599SNathan Whitehorn 
176509142e1SNathan Whitehorn 		if (OF_getencprop(child, "reg", &dinfo->gpio_num,
177b4dbc599SNathan Whitehorn 		    sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) {
17848ed71b9SNathan Whitehorn 			/*
17948ed71b9SNathan Whitehorn 			 * Some early GPIO controllers don't provide GPIO
18048ed71b9SNathan Whitehorn 			 * numbers for GPIOs designed only to provide
18148ed71b9SNathan Whitehorn 			 * interrupt resources.  We should still allow these
18248ed71b9SNathan Whitehorn 			 * to attach, but with caution.
18348ed71b9SNathan Whitehorn 			 */
18448ed71b9SNathan Whitehorn 
18548ed71b9SNathan Whitehorn 			dinfo->gpio_num = -1;
186b4dbc599SNathan Whitehorn 		}
187b4dbc599SNathan Whitehorn 
188b4dbc599SNathan Whitehorn 		resource_list_init(&dinfo->mdi_resources);
189b4dbc599SNathan Whitehorn 
1907c4f1a1cSJustin Hibbits 		if (OF_getencprop(child, "interrupts", irq, sizeof(irq)) ==
191b4dbc599SNathan Whitehorn 		    sizeof(irq)) {
192509142e1SNathan Whitehorn 			OF_searchencprop(child, "interrupt-parent", &iparent,
193eaef5f0aSNathan Whitehorn 			    sizeof(iparent));
194b4dbc599SNathan Whitehorn 			resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
1957c4f1a1cSJustin Hibbits 			    0, MAP_IRQ(iparent, irq[0]),
1967c4f1a1cSJustin Hibbits 			    MAP_IRQ(iparent, irq[0]), 1);
197b4dbc599SNathan Whitehorn 		}
198b4dbc599SNathan Whitehorn 
199b4dbc599SNathan Whitehorn 		/* Fix messed-up offsets */
200b4dbc599SNathan Whitehorn 		if (dinfo->gpio_num > 0x50)
201b4dbc599SNathan Whitehorn 			dinfo->gpio_num -= 0x50;
202b4dbc599SNathan Whitehorn 
2035b56413dSWarner Losh 		cdev = device_add_child(dev, NULL, DEVICE_UNIT_ANY);
204b4dbc599SNathan Whitehorn 		if (cdev == NULL) {
205b4dbc599SNathan Whitehorn 			device_printf(dev, "<%s>: device_add_child failed\n",
206b4dbc599SNathan Whitehorn 			    dinfo->mdi_obdinfo.obd_name);
207b4dbc599SNathan Whitehorn 			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
208b4dbc599SNathan Whitehorn 			free(dinfo, M_MACGPIO);
209b4dbc599SNathan Whitehorn 			continue;
210b4dbc599SNathan Whitehorn 		}
211b4dbc599SNathan Whitehorn 		device_set_ivars(cdev, dinfo);
212b4dbc599SNathan Whitehorn 	}
213b4dbc599SNathan Whitehorn 
214*18250ec6SJohn Baldwin 	bus_attach_children(dev);
215*18250ec6SJohn Baldwin 	return (0);
216b4dbc599SNathan Whitehorn }
217b4dbc599SNathan Whitehorn 
218b4dbc599SNathan Whitehorn static int
macgpio_print_child(device_t dev,device_t child)219b4dbc599SNathan Whitehorn macgpio_print_child(device_t dev, device_t child)
220b4dbc599SNathan Whitehorn {
221b4dbc599SNathan Whitehorn         struct macgpio_devinfo *dinfo;
222b4dbc599SNathan Whitehorn         int retval = 0;
223b4dbc599SNathan Whitehorn 
224b4dbc599SNathan Whitehorn         dinfo = device_get_ivars(child);
225b4dbc599SNathan Whitehorn 
226b4dbc599SNathan Whitehorn         retval += bus_print_child_header(dev, child);
227b4dbc599SNathan Whitehorn 
228ecf558fdSNathan Whitehorn 	if (dinfo->gpio_num >= GPIO_BASE)
229ecf558fdSNathan Whitehorn 		printf(" gpio %d", dinfo->gpio_num - GPIO_BASE);
230ecf558fdSNathan Whitehorn 	else if (dinfo->gpio_num >= GPIO_EXTINT_BASE)
231ecf558fdSNathan Whitehorn 		printf(" extint-gpio %d", dinfo->gpio_num - GPIO_EXTINT_BASE);
23248ed71b9SNathan Whitehorn 	else if (dinfo->gpio_num >= 0)
233ecf558fdSNathan Whitehorn 		printf(" addr 0x%02x", dinfo->gpio_num); /* should not happen */
234ecf558fdSNathan Whitehorn 
235b4dbc599SNathan Whitehorn 	resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ,
236f8fd3fb5SJustin Hibbits 	    "%jd");
237b4dbc599SNathan Whitehorn         retval += bus_print_child_footer(dev, child);
238b4dbc599SNathan Whitehorn 
239b4dbc599SNathan Whitehorn         return (retval);
240b4dbc599SNathan Whitehorn }
241b4dbc599SNathan Whitehorn 
242b4dbc599SNathan Whitehorn static void
macgpio_probe_nomatch(device_t dev,device_t child)243b4dbc599SNathan Whitehorn macgpio_probe_nomatch(device_t dev, device_t child)
244b4dbc599SNathan Whitehorn {
245b4dbc599SNathan Whitehorn         struct macgpio_devinfo *dinfo;
246b4dbc599SNathan Whitehorn 	const char *type;
247b4dbc599SNathan Whitehorn 
248b4dbc599SNathan Whitehorn 	if (bootverbose) {
249b4dbc599SNathan Whitehorn 		dinfo = device_get_ivars(child);
250b4dbc599SNathan Whitehorn 
251b4dbc599SNathan Whitehorn 		if ((type = ofw_bus_get_type(child)) == NULL)
252b4dbc599SNathan Whitehorn 			type = "(unknown)";
253b4dbc599SNathan Whitehorn 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
25448ed71b9SNathan Whitehorn 		if (dinfo->gpio_num >= 0)
255b4dbc599SNathan Whitehorn 			printf(" gpio %d",dinfo->gpio_num);
256b4dbc599SNathan Whitehorn 		resource_list_print_type(&dinfo->mdi_resources, "irq",
257f8fd3fb5SJustin Hibbits 		    SYS_RES_IRQ, "%jd");
258b4dbc599SNathan Whitehorn 		printf(" (no driver attached)\n");
259b4dbc599SNathan Whitehorn 	}
260b4dbc599SNathan Whitehorn }
261b4dbc599SNathan Whitehorn 
262b4dbc599SNathan Whitehorn static struct resource *
macgpio_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)263b4dbc599SNathan Whitehorn macgpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
2642dd1bdf1SJustin Hibbits 		     rman_res_t start, rman_res_t end, rman_res_t count,
2652dd1bdf1SJustin Hibbits 		     u_int flags)
266b4dbc599SNathan Whitehorn {
267b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
268b4dbc599SNathan Whitehorn 
269b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
270b4dbc599SNathan Whitehorn 
271b4dbc599SNathan Whitehorn 	if (type != SYS_RES_IRQ)
272b4dbc599SNathan Whitehorn 		return (NULL);
273b4dbc599SNathan Whitehorn 
274b4dbc599SNathan Whitehorn 	return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type,
275b4dbc599SNathan Whitehorn 	    rid, start, end, count, flags));
276b4dbc599SNathan Whitehorn }
277b4dbc599SNathan Whitehorn 
278b4dbc599SNathan Whitehorn static int
macgpio_activate_resource(device_t bus,device_t child,struct resource * res)2792baed46eSJohn Baldwin macgpio_activate_resource(device_t bus, device_t child, struct resource *res)
280b4dbc599SNathan Whitehorn {
281b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
282b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
283b4dbc599SNathan Whitehorn 	u_char val;
284b4dbc599SNathan Whitehorn 
285b4dbc599SNathan Whitehorn 	sc = device_get_softc(bus);
286b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
287b4dbc599SNathan Whitehorn 
2882baed46eSJohn Baldwin 	if (rman_get_type(res) != SYS_RES_IRQ)
289b4dbc599SNathan Whitehorn 		return ENXIO;
290b4dbc599SNathan Whitehorn 
29148ed71b9SNathan Whitehorn 	if (dinfo->gpio_num >= 0) {
292b4dbc599SNathan Whitehorn 		val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
293b4dbc599SNathan Whitehorn 		val |= 0x80;
294b4dbc599SNathan Whitehorn 		bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
29548ed71b9SNathan Whitehorn 	}
296b4dbc599SNathan Whitehorn 
2972baed46eSJohn Baldwin 	return (bus_generic_activate_resource(bus, child, res));
298b4dbc599SNathan Whitehorn }
299b4dbc599SNathan Whitehorn 
300b4dbc599SNathan Whitehorn static int
macgpio_deactivate_resource(device_t bus,device_t child,struct resource * res)3012baed46eSJohn Baldwin macgpio_deactivate_resource(device_t bus, device_t child, struct resource *res)
302b4dbc599SNathan Whitehorn {
303b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
304b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
305b4dbc599SNathan Whitehorn 	u_char val;
306b4dbc599SNathan Whitehorn 
307b4dbc599SNathan Whitehorn 	sc = device_get_softc(bus);
308b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
309b4dbc599SNathan Whitehorn 
3102baed46eSJohn Baldwin 	if (rman_get_type(res) != SYS_RES_IRQ)
311b4dbc599SNathan Whitehorn 		return ENXIO;
312b4dbc599SNathan Whitehorn 
31348ed71b9SNathan Whitehorn 	if (dinfo->gpio_num >= 0) {
314b4dbc599SNathan Whitehorn 		val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
315b4dbc599SNathan Whitehorn 		val &= ~0x80;
316b4dbc599SNathan Whitehorn 		bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
31748ed71b9SNathan Whitehorn 	}
318b4dbc599SNathan Whitehorn 
3192baed46eSJohn Baldwin 	return (bus_generic_deactivate_resource(bus, child, res));
320b4dbc599SNathan Whitehorn }
321b4dbc599SNathan Whitehorn 
322b4dbc599SNathan Whitehorn uint8_t
macgpio_read(device_t dev)323b4dbc599SNathan Whitehorn macgpio_read(device_t dev)
324b4dbc599SNathan Whitehorn {
325b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
326b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
327b4dbc599SNathan Whitehorn 
328b4dbc599SNathan Whitehorn 	sc = device_get_softc(device_get_parent(dev));
329b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(dev);
330b4dbc599SNathan Whitehorn 
33148ed71b9SNathan Whitehorn 	if (dinfo->gpio_num < 0)
33248ed71b9SNathan Whitehorn 		return (0);
33348ed71b9SNathan Whitehorn 
334b4dbc599SNathan Whitehorn 	return (bus_read_1(sc->sc_gpios,dinfo->gpio_num));
335b4dbc599SNathan Whitehorn }
336b4dbc599SNathan Whitehorn 
337b4dbc599SNathan Whitehorn void
macgpio_write(device_t dev,uint8_t val)338b4dbc599SNathan Whitehorn macgpio_write(device_t dev, uint8_t val)
339b4dbc599SNathan Whitehorn {
340b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
341b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
342b4dbc599SNathan Whitehorn 
343b4dbc599SNathan Whitehorn 	sc = device_get_softc(device_get_parent(dev));
344b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(dev);
345b4dbc599SNathan Whitehorn 
34648ed71b9SNathan Whitehorn 	if (dinfo->gpio_num < 0)
34748ed71b9SNathan Whitehorn 		return;
34848ed71b9SNathan Whitehorn 
349b4dbc599SNathan Whitehorn 	bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
350b4dbc599SNathan Whitehorn }
351b4dbc599SNathan Whitehorn 
352b4dbc599SNathan Whitehorn static const struct ofw_bus_devinfo *
macgpio_get_devinfo(device_t dev,device_t child)353b4dbc599SNathan Whitehorn macgpio_get_devinfo(device_t dev, device_t child)
354b4dbc599SNathan Whitehorn {
355b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
356b4dbc599SNathan Whitehorn 
357b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
358b4dbc599SNathan Whitehorn 	return (&dinfo->mdi_obdinfo);
359b4dbc599SNathan Whitehorn }
360b4dbc599SNathan Whitehorn 
361e68afcedSJustin Hibbits static int
macgpio_suspend(device_t dev)362e68afcedSJustin Hibbits macgpio_suspend(device_t dev)
363e68afcedSJustin Hibbits {
364e68afcedSJustin Hibbits 	struct macgpio_softc *sc;
365e68afcedSJustin Hibbits 	int i;
366e68afcedSJustin Hibbits 
367e68afcedSJustin Hibbits 	sc = device_get_softc(dev);
368e68afcedSJustin Hibbits 	sc->sc_saved_gpio_levels[0] = bus_read_4(sc->sc_gpios, GPIO_LEVELS_0);
369e68afcedSJustin Hibbits 	sc->sc_saved_gpio_levels[1] = bus_read_4(sc->sc_gpios, GPIO_LEVELS_1);
370e68afcedSJustin Hibbits 
371e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_COUNT; i++)
372e68afcedSJustin Hibbits 		sc->sc_saved_gpios[i] = bus_read_1(sc->sc_gpios, GPIO_BASE + i);
373e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_EXTINT_COUNT; i++)
374e68afcedSJustin Hibbits 		sc->sc_saved_extint_gpios[i] = bus_read_1(sc->sc_gpios, GPIO_EXTINT_BASE + i);
375e68afcedSJustin Hibbits 
376e68afcedSJustin Hibbits 	return (0);
377e68afcedSJustin Hibbits }
378e68afcedSJustin Hibbits 
379e68afcedSJustin Hibbits static int
macgpio_resume(device_t dev)380e68afcedSJustin Hibbits macgpio_resume(device_t dev)
381e68afcedSJustin Hibbits {
382e68afcedSJustin Hibbits 	struct macgpio_softc *sc;
383e68afcedSJustin Hibbits 	int i;
384e68afcedSJustin Hibbits 
385e68afcedSJustin Hibbits 	sc = device_get_softc(dev);
386e68afcedSJustin Hibbits 	bus_write_4(sc->sc_gpios, GPIO_LEVELS_0, sc->sc_saved_gpio_levels[0]);
387e68afcedSJustin Hibbits 	bus_write_4(sc->sc_gpios, GPIO_LEVELS_1, sc->sc_saved_gpio_levels[1]);
388e68afcedSJustin Hibbits 
389e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_COUNT; i++)
390e68afcedSJustin Hibbits 		bus_write_1(sc->sc_gpios, GPIO_BASE + i, sc->sc_saved_gpios[i]);
391e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_EXTINT_COUNT; i++)
392e68afcedSJustin Hibbits 		bus_write_1(sc->sc_gpios, GPIO_EXTINT_BASE + i, sc->sc_saved_extint_gpios[i]);
393e68afcedSJustin Hibbits 
394e68afcedSJustin Hibbits 	return (0);
395e68afcedSJustin Hibbits }
396