2 * Copyright 2008 by Nathan Whitehorn. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
29 * Driver for MacIO GPIO controller
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>
43 #include <machine/bus.h>
44 #include <machine/intr_machdep.h>
45 #include <machine/pmap.h>
46 #include <machine/resource.h>
47 #include <machine/vmparam.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/ofw/openfirm.h>
53 #include <powerpc/powermac/macgpiovar.h>
58 struct macgpio_softc {
60 struct resource *sc_gpios;
64 static MALLOC_DEFINE(M_MACGPIO, "macgpio", "macgpio device information");
66 static int macgpio_probe(device_t);
67 static int macgpio_attach(device_t);
68 static int macgpio_print_child(device_t dev, device_t child);
69 static void macgpio_probe_nomatch(device_t, device_t);
70 static struct resource *macgpio_alloc_resource(device_t, device_t, int, int *,
71 u_long, u_long, u_long, u_int);
72 static int macgpio_activate_resource(device_t, device_t, int, int,
74 static int macgpio_deactivate_resource(device_t, device_t, int, int,
76 static ofw_bus_get_devinfo_t macgpio_get_devinfo;
79 * Bus interface definition
81 static device_method_t macgpio_methods[] = {
82 /* Device interface */
83 DEVMETHOD(device_probe, macgpio_probe),
84 DEVMETHOD(device_attach, macgpio_attach),
85 DEVMETHOD(device_detach, bus_generic_detach),
86 DEVMETHOD(device_shutdown, bus_generic_shutdown),
87 DEVMETHOD(device_suspend, bus_generic_suspend),
88 DEVMETHOD(device_resume, bus_generic_resume),
91 DEVMETHOD(bus_print_child, macgpio_print_child),
92 DEVMETHOD(bus_probe_nomatch, macgpio_probe_nomatch),
93 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
94 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
96 DEVMETHOD(bus_alloc_resource, macgpio_alloc_resource),
97 DEVMETHOD(bus_activate_resource, macgpio_activate_resource),
98 DEVMETHOD(bus_deactivate_resource, macgpio_deactivate_resource),
99 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
101 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
103 /* ofw_bus interface */
104 DEVMETHOD(ofw_bus_get_devinfo, macgpio_get_devinfo),
105 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
106 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
107 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
108 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
109 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
114 static driver_t macgpio_pci_driver = {
117 sizeof(struct macgpio_softc)
120 devclass_t macgpio_devclass;
122 DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, macgpio_devclass, 0, 0);
124 struct macgpio_devinfo {
125 struct ofw_bus_devinfo mdi_obdinfo;
126 struct resource_list mdi_resources;
132 macgpio_probe(device_t dev)
136 name = ofw_bus_get_name(dev);
137 if (name && strcmp(name, "gpio") == 0) {
138 device_set_desc(dev, "MacIO GPIO Controller");
146 * Scan Open Firmware child nodes, and attach these as children
150 macgpio_attach(device_t dev)
152 struct macgpio_softc *sc;
153 struct macgpio_devinfo *dinfo;
154 phandle_t root, child, iparent;
158 sc = device_get_softc(dev);
159 root = sc->sc_node = ofw_bus_get_node(dev);
161 sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
162 &sc->sc_gpios_rid, RF_ACTIVE);
165 * Iterate through the sub-devices
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) !=
171 free(dinfo, M_MACGPIO);
175 if (OF_getprop(child,"reg",&dinfo->gpio_num,
176 sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) {
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.
184 dinfo->gpio_num = -1;
187 resource_list_init(&dinfo->mdi_resources);
189 if (OF_getprop(child, "interrupts", &irq, sizeof(irq)) ==
191 OF_searchprop(child, "interrupt-parent", &iparent,
193 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
194 0, MAP_IRQ(iparent, irq), MAP_IRQ(iparent, irq),
198 /* Fix messed-up offsets */
199 if (dinfo->gpio_num > 0x50)
200 dinfo->gpio_num -= 0x50;
202 cdev = device_add_child(dev, NULL, -1);
204 device_printf(dev, "<%s>: device_add_child failed\n",
205 dinfo->mdi_obdinfo.obd_name);
206 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
207 free(dinfo, M_MACGPIO);
210 device_set_ivars(cdev, dinfo);
213 return (bus_generic_attach(dev));
218 macgpio_print_child(device_t dev, device_t child)
220 struct macgpio_devinfo *dinfo;
223 dinfo = device_get_ivars(child);
225 retval += bus_print_child_header(dev, child);
227 if (dinfo->gpio_num >= GPIO_BASE)
228 printf(" gpio %d", dinfo->gpio_num - GPIO_BASE);
229 else if (dinfo->gpio_num >= GPIO_EXTINT_BASE)
230 printf(" extint-gpio %d", dinfo->gpio_num - GPIO_EXTINT_BASE);
231 else if (dinfo->gpio_num >= 0)
232 printf(" addr 0x%02x", dinfo->gpio_num); /* should not happen */
234 resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ,
236 retval += bus_print_child_footer(dev, child);
243 macgpio_probe_nomatch(device_t dev, device_t child)
245 struct macgpio_devinfo *dinfo;
249 dinfo = device_get_ivars(child);
251 if ((type = ofw_bus_get_type(child)) == NULL)
253 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
254 if (dinfo->gpio_num >= 0)
255 printf(" gpio %d",dinfo->gpio_num);
256 resource_list_print_type(&dinfo->mdi_resources, "irq",
258 printf(" (no driver attached)\n");
263 static struct resource *
264 macgpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
265 u_long start, u_long end, u_long count, u_int flags)
267 struct macgpio_devinfo *dinfo;
269 dinfo = device_get_ivars(child);
271 if (type != SYS_RES_IRQ)
274 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type,
275 rid, start, end, count, flags));
279 macgpio_activate_resource(device_t bus, device_t child, int type, int rid,
280 struct resource *res)
282 struct macgpio_softc *sc;
283 struct macgpio_devinfo *dinfo;
286 sc = device_get_softc(bus);
287 dinfo = device_get_ivars(child);
289 if (type != SYS_RES_IRQ)
292 if (dinfo->gpio_num >= 0) {
293 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
295 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
298 return (bus_activate_resource(bus, type, rid, res));
303 macgpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
304 struct resource *res)
306 struct macgpio_softc *sc;
307 struct macgpio_devinfo *dinfo;
310 sc = device_get_softc(bus);
311 dinfo = device_get_ivars(child);
313 if (type != SYS_RES_IRQ)
316 if (dinfo->gpio_num >= 0) {
317 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
319 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
322 return (bus_deactivate_resource(bus, type, rid, res));
326 macgpio_read(device_t dev)
328 struct macgpio_softc *sc;
329 struct macgpio_devinfo *dinfo;
331 sc = device_get_softc(device_get_parent(dev));
332 dinfo = device_get_ivars(dev);
334 if (dinfo->gpio_num < 0)
337 return (bus_read_1(sc->sc_gpios,dinfo->gpio_num));
341 macgpio_write(device_t dev, uint8_t val)
343 struct macgpio_softc *sc;
344 struct macgpio_devinfo *dinfo;
346 sc = device_get_softc(device_get_parent(dev));
347 dinfo = device_get_ivars(dev);
349 if (dinfo->gpio_num < 0)
352 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
355 static const struct ofw_bus_devinfo *
356 macgpio_get_devinfo(device_t dev, device_t child)
358 struct macgpio_devinfo *dinfo;
360 dinfo = device_get_ivars(child);
361 return (&dinfo->mdi_obdinfo);