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>
38 #include <machine/bus.h>
41 #include <machine/vmparam.h>
44 #include <machine/pmap.h>
46 #include <machine/resource.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/ofw/openfirm.h>
52 #include <powerpc/powermac/macgpiovar.h>
57 struct macgpio_softc {
59 struct resource *sc_gpios;
63 static MALLOC_DEFINE(M_MACGPIO, "macgpio", "macgpio device information");
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,
73 static int macgpio_deactivate_resource(device_t, device_t, int, int,
75 static ofw_bus_get_devinfo_t macgpio_get_devinfo;
78 * Bus interface definition
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),
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),
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),
100 /* ofw_bus interface */
101 DEVMETHOD(ofw_bus_get_devinfo, macgpio_get_devinfo),
102 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
103 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
104 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
105 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
106 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
111 static driver_t macgpio_pci_driver = {
114 sizeof(struct macgpio_softc)
117 devclass_t macgpio_devclass;
119 DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, macgpio_devclass, 0, 0);
121 struct macgpio_devinfo {
122 struct ofw_bus_devinfo mdi_obdinfo;
123 struct resource_list mdi_resources;
129 macgpio_probe(device_t dev)
133 name = ofw_bus_get_name(dev);
134 if (name && strcmp(name, "gpio") == 0) {
135 device_set_desc(dev, "MacIO GPIO Controller");
143 * Scan Open Firmware child nodes, and attach these as children
147 macgpio_attach(device_t dev)
149 struct macgpio_softc *sc;
150 struct macgpio_devinfo *dinfo;
156 sc = device_get_softc(dev);
157 root = sc->sc_node = ofw_bus_get_node(dev);
159 sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
160 &sc->sc_gpios_rid, RF_ACTIVE);
163 * Iterate through the sub-devices
165 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
166 dinfo = malloc(sizeof(*dinfo), M_MACGPIO, M_WAITOK | M_ZERO);
167 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
169 free(dinfo, M_MACGPIO);
173 if (OF_getprop(child,"reg",&dinfo->gpio_num,
174 sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) {
176 * Some early GPIO controllers don't provide GPIO
177 * numbers for GPIOs designed only to provide
178 * interrupt resources. We should still allow these
179 * to attach, but with caution.
182 dinfo->gpio_num = -1;
185 resource_list_init(&dinfo->mdi_resources);
187 if (OF_getprop(child,"interrupts",&irq, sizeof(irq)) ==
189 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
193 /* Fix messed-up offsets */
194 if (dinfo->gpio_num > 0x50)
195 dinfo->gpio_num -= 0x50;
197 cdev = device_add_child(dev, NULL, -1);
199 device_printf(dev, "<%s>: device_add_child failed\n",
200 dinfo->mdi_obdinfo.obd_name);
201 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
202 free(dinfo, M_MACGPIO);
205 device_set_ivars(cdev, dinfo);
208 return (bus_generic_attach(dev));
213 macgpio_print_child(device_t dev, device_t child)
215 struct macgpio_devinfo *dinfo;
218 dinfo = device_get_ivars(child);
220 retval += bus_print_child_header(dev, child);
222 if (dinfo->gpio_num >= GPIO_BASE)
223 printf(" gpio %d", dinfo->gpio_num - GPIO_BASE);
224 else if (dinfo->gpio_num >= GPIO_EXTINT_BASE)
225 printf(" extint-gpio %d", dinfo->gpio_num - GPIO_EXTINT_BASE);
226 else if (dinfo->gpio_num >= 0)
227 printf(" addr 0x%02x", dinfo->gpio_num); /* should not happen */
229 resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ,
231 retval += bus_print_child_footer(dev, child);
238 macgpio_probe_nomatch(device_t dev, device_t child)
240 struct macgpio_devinfo *dinfo;
244 dinfo = device_get_ivars(child);
246 if ((type = ofw_bus_get_type(child)) == NULL)
248 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
249 if (dinfo->gpio_num >= 0)
250 printf(" gpio %d",dinfo->gpio_num);
251 resource_list_print_type(&dinfo->mdi_resources, "irq",
253 printf(" (no driver attached)\n");
258 static struct resource *
259 macgpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
260 u_long start, u_long end, u_long count, u_int flags)
262 struct macgpio_softc *sc;
263 struct macgpio_devinfo *dinfo;
265 sc = device_get_softc(bus);
266 dinfo = device_get_ivars(child);
268 if (type != SYS_RES_IRQ)
271 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type,
272 rid, start, end, count, flags));
276 macgpio_activate_resource(device_t bus, device_t child, int type, int rid,
277 struct resource *res)
279 struct macgpio_softc *sc;
280 struct macgpio_devinfo *dinfo;
283 sc = device_get_softc(bus);
284 dinfo = device_get_ivars(child);
286 if (type != SYS_RES_IRQ)
289 if (dinfo->gpio_num >= 0) {
290 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
292 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
295 return (bus_activate_resource(bus, type, rid, res));
300 macgpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
301 struct resource *res)
303 struct macgpio_softc *sc;
304 struct macgpio_devinfo *dinfo;
307 sc = device_get_softc(bus);
308 dinfo = device_get_ivars(child);
310 if (type != SYS_RES_IRQ)
313 if (dinfo->gpio_num >= 0) {
314 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
316 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
319 return (bus_deactivate_resource(bus, type, rid, res));
323 macgpio_read(device_t dev)
325 struct macgpio_softc *sc;
326 struct macgpio_devinfo *dinfo;
328 sc = device_get_softc(device_get_parent(dev));
329 dinfo = device_get_ivars(dev);
331 if (dinfo->gpio_num < 0)
334 return (bus_read_1(sc->sc_gpios,dinfo->gpio_num));
338 macgpio_write(device_t dev, uint8_t val)
340 struct macgpio_softc *sc;
341 struct macgpio_devinfo *dinfo;
343 sc = device_get_softc(device_get_parent(dev));
344 dinfo = device_get_ivars(dev);
346 if (dinfo->gpio_num < 0)
349 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
352 static const struct ofw_bus_devinfo *
353 macgpio_get_devinfo(device_t dev, device_t child)
355 struct macgpio_devinfo *dinfo;
357 dinfo = device_get_ivars(child);
358 return (&dinfo->mdi_obdinfo);