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