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