xref: /freebsd/sys/powerpc/powermac/macgpio.c (revision 2dd1bdf1834c53d048d3d9a7079b45afea5cecd7)
1b4dbc599SNathan Whitehorn /*-
2ecf558fdSNathan Whitehorn  * Copyright 2008 by Nathan Whitehorn. All rights reserved.
3b4dbc599SNathan Whitehorn  *
4b4dbc599SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
5b4dbc599SNathan Whitehorn  * modification, are permitted provided that the following conditions
6b4dbc599SNathan Whitehorn  * are met:
7b4dbc599SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
8b4dbc599SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
9b4dbc599SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
10b4dbc599SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
11b4dbc599SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
12b4dbc599SNathan Whitehorn  *
13b4dbc599SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14b4dbc599SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15b4dbc599SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16b4dbc599SNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17b4dbc599SNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18b4dbc599SNathan Whitehorn  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19b4dbc599SNathan Whitehorn  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20b4dbc599SNathan Whitehorn  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21b4dbc599SNathan Whitehorn  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22b4dbc599SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23b4dbc599SNathan Whitehorn  * SUCH DAMAGE.
24b4dbc599SNathan Whitehorn  *
25b4dbc599SNathan Whitehorn  * $FreeBSD$
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>
45eaef5f0aSNathan Whitehorn #include <machine/pmap.h>
46b4dbc599SNathan Whitehorn #include <machine/resource.h>
47eaef5f0aSNathan Whitehorn #include <machine/vmparam.h>
48b4dbc599SNathan Whitehorn 
49b4dbc599SNathan Whitehorn #include <dev/ofw/ofw_bus.h>
50b4dbc599SNathan Whitehorn #include <dev/ofw/ofw_bus_subr.h>
51b4dbc599SNathan Whitehorn #include <dev/ofw/openfirm.h>
52b4dbc599SNathan Whitehorn 
53ecf558fdSNathan Whitehorn #include <powerpc/powermac/macgpiovar.h>
54ecf558fdSNathan Whitehorn 
55b4dbc599SNathan Whitehorn /*
56b4dbc599SNathan Whitehorn  * Macgpio softc
57b4dbc599SNathan Whitehorn  */
58b4dbc599SNathan Whitehorn struct macgpio_softc {
59b4dbc599SNathan Whitehorn 	phandle_t	sc_node;
60b4dbc599SNathan Whitehorn 	struct resource	*sc_gpios;
61b4dbc599SNathan Whitehorn 	int		sc_gpios_rid;
62e68afcedSJustin Hibbits 	uint32_t	sc_saved_gpio_levels[2];
63e68afcedSJustin Hibbits 	uint32_t	sc_saved_gpios[GPIO_COUNT];
64e68afcedSJustin Hibbits 	uint32_t	sc_saved_extint_gpios[GPIO_EXTINT_COUNT];
65b4dbc599SNathan Whitehorn };
66b4dbc599SNathan Whitehorn 
67b4dbc599SNathan Whitehorn static MALLOC_DEFINE(M_MACGPIO, "macgpio", "macgpio device information");
68b4dbc599SNathan Whitehorn 
69b4dbc599SNathan Whitehorn static int	macgpio_probe(device_t);
70b4dbc599SNathan Whitehorn static int	macgpio_attach(device_t);
71b4dbc599SNathan Whitehorn static int	macgpio_print_child(device_t dev, device_t child);
72b4dbc599SNathan Whitehorn static void	macgpio_probe_nomatch(device_t, device_t);
73b4dbc599SNathan Whitehorn static struct resource *macgpio_alloc_resource(device_t, device_t, int, int *,
74*2dd1bdf1SJustin Hibbits 		    rman_res_t, rman_res_t, rman_res_t, u_int);
75b4dbc599SNathan Whitehorn static int	macgpio_activate_resource(device_t, device_t, int, int,
76b4dbc599SNathan Whitehorn 		    struct resource *);
77b4dbc599SNathan Whitehorn static int	macgpio_deactivate_resource(device_t, device_t, int, int,
78b4dbc599SNathan Whitehorn 		    struct resource *);
79b4dbc599SNathan Whitehorn static ofw_bus_get_devinfo_t macgpio_get_devinfo;
80e68afcedSJustin Hibbits static int	macgpio_suspend(device_t dev);
81e68afcedSJustin Hibbits static int	macgpio_resume(device_t dev);
82b4dbc599SNathan Whitehorn 
83b4dbc599SNathan Whitehorn /*
84b4dbc599SNathan Whitehorn  * Bus interface definition
85b4dbc599SNathan Whitehorn  */
86b4dbc599SNathan Whitehorn static device_method_t macgpio_methods[] = {
87b4dbc599SNathan Whitehorn 	/* Device interface */
88b4dbc599SNathan Whitehorn 	DEVMETHOD(device_probe,         macgpio_probe),
89b4dbc599SNathan Whitehorn 	DEVMETHOD(device_attach,        macgpio_attach),
90b4dbc599SNathan Whitehorn 	DEVMETHOD(device_detach,        bus_generic_detach),
91b4dbc599SNathan Whitehorn 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
92e68afcedSJustin Hibbits 	DEVMETHOD(device_suspend,       macgpio_suspend),
93e68afcedSJustin Hibbits 	DEVMETHOD(device_resume,        macgpio_resume),
94b4dbc599SNathan Whitehorn 
95b4dbc599SNathan Whitehorn 	/* Bus interface */
96b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_print_child,      macgpio_print_child),
97b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_probe_nomatch,    macgpio_probe_nomatch),
98b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
99b4dbc599SNathan Whitehorn 	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
100b4dbc599SNathan Whitehorn 
101b4dbc599SNathan Whitehorn         DEVMETHOD(bus_alloc_resource,   macgpio_alloc_resource),
102b4dbc599SNathan Whitehorn         DEVMETHOD(bus_activate_resource, macgpio_activate_resource),
103b4dbc599SNathan Whitehorn         DEVMETHOD(bus_deactivate_resource, macgpio_deactivate_resource),
104b4dbc599SNathan Whitehorn         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
105b4dbc599SNathan Whitehorn 
1064fe8025dSNathan Whitehorn 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
1074fe8025dSNathan Whitehorn 
108b4dbc599SNathan Whitehorn 	/* ofw_bus interface */
109b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_devinfo,	macgpio_get_devinfo),
110b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
111b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
112b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
113b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
114b4dbc599SNathan Whitehorn 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
115b4dbc599SNathan Whitehorn 
116b4dbc599SNathan Whitehorn 	{ 0, 0 }
117b4dbc599SNathan Whitehorn };
118b4dbc599SNathan Whitehorn 
119b4dbc599SNathan Whitehorn static driver_t macgpio_pci_driver = {
120b4dbc599SNathan Whitehorn         "macgpio",
121b4dbc599SNathan Whitehorn         macgpio_methods,
122b4dbc599SNathan Whitehorn 	sizeof(struct macgpio_softc)
123b4dbc599SNathan Whitehorn };
124b4dbc599SNathan Whitehorn 
125b4dbc599SNathan Whitehorn devclass_t macgpio_devclass;
126b4dbc599SNathan Whitehorn 
127b4dbc599SNathan Whitehorn DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, macgpio_devclass, 0, 0);
128b4dbc599SNathan Whitehorn 
129b4dbc599SNathan Whitehorn struct macgpio_devinfo {
130b4dbc599SNathan Whitehorn 	struct ofw_bus_devinfo mdi_obdinfo;
131b4dbc599SNathan Whitehorn 	struct resource_list mdi_resources;
132b4dbc599SNathan Whitehorn 
133b4dbc599SNathan Whitehorn 	int gpio_num;
134b4dbc599SNathan Whitehorn };
135b4dbc599SNathan Whitehorn 
136b4dbc599SNathan Whitehorn static int
137b4dbc599SNathan Whitehorn macgpio_probe(device_t dev)
138b4dbc599SNathan Whitehorn {
139b4dbc599SNathan Whitehorn 	const char *name;
140b4dbc599SNathan Whitehorn 
141b4dbc599SNathan Whitehorn 	name = ofw_bus_get_name(dev);
142b4dbc599SNathan Whitehorn 	if (name && strcmp(name, "gpio") == 0) {
143b4dbc599SNathan Whitehorn 		device_set_desc(dev, "MacIO GPIO Controller");
144b4dbc599SNathan Whitehorn 		return (0);
145b4dbc599SNathan Whitehorn 	}
146b4dbc599SNathan Whitehorn 
147b4dbc599SNathan Whitehorn         return (ENXIO);
148b4dbc599SNathan Whitehorn }
149b4dbc599SNathan Whitehorn 
150b4dbc599SNathan Whitehorn /*
151b4dbc599SNathan Whitehorn  * Scan Open Firmware child nodes, and attach these as children
152b4dbc599SNathan Whitehorn  * of the macgpio bus
153b4dbc599SNathan Whitehorn  */
154b4dbc599SNathan Whitehorn static int
155b4dbc599SNathan Whitehorn macgpio_attach(device_t dev)
156b4dbc599SNathan Whitehorn {
157b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
158b4dbc599SNathan Whitehorn         struct macgpio_devinfo *dinfo;
159eaef5f0aSNathan Whitehorn         phandle_t root, child, iparent;
160b4dbc599SNathan Whitehorn         device_t cdev;
161b4dbc599SNathan Whitehorn 	uint32_t irq;
162b4dbc599SNathan Whitehorn 
163b4dbc599SNathan Whitehorn 	sc = device_get_softc(dev);
164b4dbc599SNathan Whitehorn 	root = sc->sc_node = ofw_bus_get_node(dev);
165b4dbc599SNathan Whitehorn 
166b4dbc599SNathan Whitehorn 	sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
167b4dbc599SNathan Whitehorn 	    &sc->sc_gpios_rid, RF_ACTIVE);
168b4dbc599SNathan Whitehorn 
169b4dbc599SNathan Whitehorn 	/*
170b4dbc599SNathan Whitehorn 	 * Iterate through the sub-devices
171b4dbc599SNathan Whitehorn 	 */
172b4dbc599SNathan Whitehorn 	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
173b4dbc599SNathan Whitehorn 		dinfo = malloc(sizeof(*dinfo), M_MACGPIO, M_WAITOK | M_ZERO);
174b4dbc599SNathan Whitehorn 		if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
175b4dbc599SNathan Whitehorn 		    0) {
176b4dbc599SNathan Whitehorn 			free(dinfo, M_MACGPIO);
177b4dbc599SNathan Whitehorn 			continue;
178b4dbc599SNathan Whitehorn 		}
179b4dbc599SNathan Whitehorn 
180509142e1SNathan Whitehorn 		if (OF_getencprop(child, "reg", &dinfo->gpio_num,
181b4dbc599SNathan Whitehorn 		    sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) {
18248ed71b9SNathan Whitehorn 			/*
18348ed71b9SNathan Whitehorn 			 * Some early GPIO controllers don't provide GPIO
18448ed71b9SNathan Whitehorn 			 * numbers for GPIOs designed only to provide
18548ed71b9SNathan Whitehorn 			 * interrupt resources.  We should still allow these
18648ed71b9SNathan Whitehorn 			 * to attach, but with caution.
18748ed71b9SNathan Whitehorn 			 */
18848ed71b9SNathan Whitehorn 
18948ed71b9SNathan Whitehorn 			dinfo->gpio_num = -1;
190b4dbc599SNathan Whitehorn 		}
191b4dbc599SNathan Whitehorn 
192b4dbc599SNathan Whitehorn 		resource_list_init(&dinfo->mdi_resources);
193b4dbc599SNathan Whitehorn 
194509142e1SNathan Whitehorn 		if (OF_getencprop(child, "interrupts", &irq, sizeof(irq)) ==
195b4dbc599SNathan Whitehorn 		    sizeof(irq)) {
196509142e1SNathan Whitehorn 			OF_searchencprop(child, "interrupt-parent", &iparent,
197eaef5f0aSNathan Whitehorn 			    sizeof(iparent));
198b4dbc599SNathan Whitehorn 			resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
199607ebaafSMarcel Moolenaar 			    0, MAP_IRQ(iparent, irq), MAP_IRQ(iparent, irq),
200eaef5f0aSNathan Whitehorn 			    1);
201b4dbc599SNathan Whitehorn 		}
202b4dbc599SNathan Whitehorn 
203b4dbc599SNathan Whitehorn 		/* Fix messed-up offsets */
204b4dbc599SNathan Whitehorn 		if (dinfo->gpio_num > 0x50)
205b4dbc599SNathan Whitehorn 			dinfo->gpio_num -= 0x50;
206b4dbc599SNathan Whitehorn 
207b4dbc599SNathan Whitehorn 		cdev = device_add_child(dev, NULL, -1);
208b4dbc599SNathan Whitehorn 		if (cdev == NULL) {
209b4dbc599SNathan Whitehorn 			device_printf(dev, "<%s>: device_add_child failed\n",
210b4dbc599SNathan Whitehorn 			    dinfo->mdi_obdinfo.obd_name);
211b4dbc599SNathan Whitehorn 			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
212b4dbc599SNathan Whitehorn 			free(dinfo, M_MACGPIO);
213b4dbc599SNathan Whitehorn 			continue;
214b4dbc599SNathan Whitehorn 		}
215b4dbc599SNathan Whitehorn 		device_set_ivars(cdev, dinfo);
216b4dbc599SNathan Whitehorn 	}
217b4dbc599SNathan Whitehorn 
218b4dbc599SNathan Whitehorn 	return (bus_generic_attach(dev));
219b4dbc599SNathan Whitehorn }
220b4dbc599SNathan Whitehorn 
221b4dbc599SNathan Whitehorn 
222b4dbc599SNathan Whitehorn static int
223b4dbc599SNathan Whitehorn macgpio_print_child(device_t dev, device_t child)
224b4dbc599SNathan Whitehorn {
225b4dbc599SNathan Whitehorn         struct macgpio_devinfo *dinfo;
226b4dbc599SNathan Whitehorn         int retval = 0;
227b4dbc599SNathan Whitehorn 
228b4dbc599SNathan Whitehorn         dinfo = device_get_ivars(child);
229b4dbc599SNathan Whitehorn 
230b4dbc599SNathan Whitehorn         retval += bus_print_child_header(dev, child);
231b4dbc599SNathan Whitehorn 
232ecf558fdSNathan Whitehorn 	if (dinfo->gpio_num >= GPIO_BASE)
233ecf558fdSNathan Whitehorn 		printf(" gpio %d", dinfo->gpio_num - GPIO_BASE);
234ecf558fdSNathan Whitehorn 	else if (dinfo->gpio_num >= GPIO_EXTINT_BASE)
235ecf558fdSNathan Whitehorn 		printf(" extint-gpio %d", dinfo->gpio_num - GPIO_EXTINT_BASE);
23648ed71b9SNathan Whitehorn 	else if (dinfo->gpio_num >= 0)
237ecf558fdSNathan Whitehorn 		printf(" addr 0x%02x", dinfo->gpio_num); /* should not happen */
238ecf558fdSNathan Whitehorn 
239b4dbc599SNathan Whitehorn 	resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ,
240b4dbc599SNathan Whitehorn 	    "%ld");
241b4dbc599SNathan Whitehorn         retval += bus_print_child_footer(dev, child);
242b4dbc599SNathan Whitehorn 
243b4dbc599SNathan Whitehorn         return (retval);
244b4dbc599SNathan Whitehorn }
245b4dbc599SNathan Whitehorn 
246b4dbc599SNathan Whitehorn 
247b4dbc599SNathan Whitehorn static void
248b4dbc599SNathan Whitehorn macgpio_probe_nomatch(device_t dev, device_t child)
249b4dbc599SNathan Whitehorn {
250b4dbc599SNathan Whitehorn         struct macgpio_devinfo *dinfo;
251b4dbc599SNathan Whitehorn 	const char *type;
252b4dbc599SNathan Whitehorn 
253b4dbc599SNathan Whitehorn 	if (bootverbose) {
254b4dbc599SNathan Whitehorn 		dinfo = device_get_ivars(child);
255b4dbc599SNathan Whitehorn 
256b4dbc599SNathan Whitehorn 		if ((type = ofw_bus_get_type(child)) == NULL)
257b4dbc599SNathan Whitehorn 			type = "(unknown)";
258b4dbc599SNathan Whitehorn 		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
25948ed71b9SNathan Whitehorn 		if (dinfo->gpio_num >= 0)
260b4dbc599SNathan Whitehorn 			printf(" gpio %d",dinfo->gpio_num);
261b4dbc599SNathan Whitehorn 		resource_list_print_type(&dinfo->mdi_resources, "irq",
262b4dbc599SNathan Whitehorn 		    SYS_RES_IRQ, "%ld");
263b4dbc599SNathan Whitehorn 		printf(" (no driver attached)\n");
264b4dbc599SNathan Whitehorn 	}
265b4dbc599SNathan Whitehorn }
266b4dbc599SNathan Whitehorn 
267b4dbc599SNathan Whitehorn 
268b4dbc599SNathan Whitehorn static struct resource *
269b4dbc599SNathan Whitehorn macgpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
270*2dd1bdf1SJustin Hibbits 		     rman_res_t start, rman_res_t end, rman_res_t count,
271*2dd1bdf1SJustin Hibbits 		     u_int flags)
272b4dbc599SNathan Whitehorn {
273b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
274b4dbc599SNathan Whitehorn 
275b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
276b4dbc599SNathan Whitehorn 
277b4dbc599SNathan Whitehorn 	if (type != SYS_RES_IRQ)
278b4dbc599SNathan Whitehorn 		return (NULL);
279b4dbc599SNathan Whitehorn 
280b4dbc599SNathan Whitehorn 	return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type,
281b4dbc599SNathan Whitehorn 	    rid, start, end, count, flags));
282b4dbc599SNathan Whitehorn }
283b4dbc599SNathan Whitehorn 
284b4dbc599SNathan Whitehorn static int
285b4dbc599SNathan Whitehorn macgpio_activate_resource(device_t bus, device_t child, int type, int rid,
286b4dbc599SNathan Whitehorn 			   struct resource *res)
287b4dbc599SNathan Whitehorn {
288b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
289b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
290b4dbc599SNathan Whitehorn 	u_char val;
291b4dbc599SNathan Whitehorn 
292b4dbc599SNathan Whitehorn 	sc = device_get_softc(bus);
293b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
294b4dbc599SNathan Whitehorn 
295b4dbc599SNathan Whitehorn 	if (type != SYS_RES_IRQ)
296b4dbc599SNathan Whitehorn 		return ENXIO;
297b4dbc599SNathan Whitehorn 
29848ed71b9SNathan Whitehorn 	if (dinfo->gpio_num >= 0) {
299b4dbc599SNathan Whitehorn 		val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
300b4dbc599SNathan Whitehorn 		val |= 0x80;
301b4dbc599SNathan Whitehorn 		bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
30248ed71b9SNathan Whitehorn 	}
303b4dbc599SNathan Whitehorn 
304b4dbc599SNathan Whitehorn 	return (bus_activate_resource(bus, type, rid, res));
305b4dbc599SNathan Whitehorn }
306b4dbc599SNathan Whitehorn 
307b4dbc599SNathan Whitehorn 
308b4dbc599SNathan Whitehorn static int
309b4dbc599SNathan Whitehorn macgpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
310b4dbc599SNathan Whitehorn 			  struct resource *res)
311b4dbc599SNathan Whitehorn {
312b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
313b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
314b4dbc599SNathan Whitehorn 	u_char val;
315b4dbc599SNathan Whitehorn 
316b4dbc599SNathan Whitehorn 	sc = device_get_softc(bus);
317b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
318b4dbc599SNathan Whitehorn 
319b4dbc599SNathan Whitehorn 	if (type != SYS_RES_IRQ)
320b4dbc599SNathan Whitehorn 		return ENXIO;
321b4dbc599SNathan Whitehorn 
32248ed71b9SNathan Whitehorn 	if (dinfo->gpio_num >= 0) {
323b4dbc599SNathan Whitehorn 		val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
324b4dbc599SNathan Whitehorn 		val &= ~0x80;
325b4dbc599SNathan Whitehorn 		bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
32648ed71b9SNathan Whitehorn 	}
327b4dbc599SNathan Whitehorn 
328b4dbc599SNathan Whitehorn 	return (bus_deactivate_resource(bus, type, rid, res));
329b4dbc599SNathan Whitehorn }
330b4dbc599SNathan Whitehorn 
331b4dbc599SNathan Whitehorn uint8_t
332b4dbc599SNathan Whitehorn macgpio_read(device_t dev)
333b4dbc599SNathan Whitehorn {
334b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
335b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
336b4dbc599SNathan Whitehorn 
337b4dbc599SNathan Whitehorn 	sc = device_get_softc(device_get_parent(dev));
338b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(dev);
339b4dbc599SNathan Whitehorn 
34048ed71b9SNathan Whitehorn 	if (dinfo->gpio_num < 0)
34148ed71b9SNathan Whitehorn 		return (0);
34248ed71b9SNathan Whitehorn 
343b4dbc599SNathan Whitehorn 	return (bus_read_1(sc->sc_gpios,dinfo->gpio_num));
344b4dbc599SNathan Whitehorn }
345b4dbc599SNathan Whitehorn 
346b4dbc599SNathan Whitehorn void
347b4dbc599SNathan Whitehorn macgpio_write(device_t dev, uint8_t val)
348b4dbc599SNathan Whitehorn {
349b4dbc599SNathan Whitehorn 	struct macgpio_softc *sc;
350b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
351b4dbc599SNathan Whitehorn 
352b4dbc599SNathan Whitehorn 	sc = device_get_softc(device_get_parent(dev));
353b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(dev);
354b4dbc599SNathan Whitehorn 
35548ed71b9SNathan Whitehorn 	if (dinfo->gpio_num < 0)
35648ed71b9SNathan Whitehorn 		return;
35748ed71b9SNathan Whitehorn 
358b4dbc599SNathan Whitehorn 	bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
359b4dbc599SNathan Whitehorn }
360b4dbc599SNathan Whitehorn 
361b4dbc599SNathan Whitehorn static const struct ofw_bus_devinfo *
362b4dbc599SNathan Whitehorn macgpio_get_devinfo(device_t dev, device_t child)
363b4dbc599SNathan Whitehorn {
364b4dbc599SNathan Whitehorn 	struct macgpio_devinfo *dinfo;
365b4dbc599SNathan Whitehorn 
366b4dbc599SNathan Whitehorn 	dinfo = device_get_ivars(child);
367b4dbc599SNathan Whitehorn 	return (&dinfo->mdi_obdinfo);
368b4dbc599SNathan Whitehorn }
369b4dbc599SNathan Whitehorn 
370e68afcedSJustin Hibbits static int
371e68afcedSJustin Hibbits macgpio_suspend(device_t dev)
372e68afcedSJustin Hibbits {
373e68afcedSJustin Hibbits 	struct macgpio_softc *sc;
374e68afcedSJustin Hibbits 	int i;
375e68afcedSJustin Hibbits 
376e68afcedSJustin Hibbits 	sc = device_get_softc(dev);
377e68afcedSJustin Hibbits 	sc->sc_saved_gpio_levels[0] = bus_read_4(sc->sc_gpios, GPIO_LEVELS_0);
378e68afcedSJustin Hibbits 	sc->sc_saved_gpio_levels[1] = bus_read_4(sc->sc_gpios, GPIO_LEVELS_1);
379e68afcedSJustin Hibbits 
380e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_COUNT; i++)
381e68afcedSJustin Hibbits 		sc->sc_saved_gpios[i] = bus_read_1(sc->sc_gpios, GPIO_BASE + i);
382e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_EXTINT_COUNT; i++)
383e68afcedSJustin Hibbits 		sc->sc_saved_extint_gpios[i] = bus_read_1(sc->sc_gpios, GPIO_EXTINT_BASE + i);
384e68afcedSJustin Hibbits 
385e68afcedSJustin Hibbits 	return (0);
386e68afcedSJustin Hibbits }
387e68afcedSJustin Hibbits 
388e68afcedSJustin Hibbits static int
389e68afcedSJustin Hibbits macgpio_resume(device_t dev)
390e68afcedSJustin Hibbits {
391e68afcedSJustin Hibbits 	struct macgpio_softc *sc;
392e68afcedSJustin Hibbits 	int i;
393e68afcedSJustin Hibbits 
394e68afcedSJustin Hibbits 	sc = device_get_softc(dev);
395e68afcedSJustin Hibbits 	bus_write_4(sc->sc_gpios, GPIO_LEVELS_0, sc->sc_saved_gpio_levels[0]);
396e68afcedSJustin Hibbits 	bus_write_4(sc->sc_gpios, GPIO_LEVELS_1, sc->sc_saved_gpio_levels[1]);
397e68afcedSJustin Hibbits 
398e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_COUNT; i++)
399e68afcedSJustin Hibbits 		bus_write_1(sc->sc_gpios, GPIO_BASE + i, sc->sc_saved_gpios[i]);
400e68afcedSJustin Hibbits 	for (i = 0; i < GPIO_EXTINT_COUNT; i++)
401e68afcedSJustin Hibbits 		bus_write_1(sc->sc_gpios, GPIO_EXTINT_BASE + i, sc->sc_saved_extint_gpios[i]);
402e68afcedSJustin Hibbits 
403e68afcedSJustin Hibbits 	return (0);
404e68afcedSJustin Hibbits }
405