2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2008 by Nathan Whitehorn. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
31 * Driver for MacIO GPIO controller
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>
45 #include <machine/bus.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/resource.h>
48 #include <machine/vmparam.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/ofw/openfirm.h>
54 #include <powerpc/powermac/macgpiovar.h>
59 struct macgpio_softc {
61 struct resource *sc_gpios;
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];
68 static MALLOC_DEFINE(M_MACGPIO, "macgpio", "macgpio device information");
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,
78 static int macgpio_deactivate_resource(device_t, device_t, int, int,
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);
85 * Bus interface definition
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),
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),
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),
107 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
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),
120 static driver_t macgpio_pci_driver = {
123 sizeof(struct macgpio_softc)
126 devclass_t macgpio_devclass;
128 EARLY_DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, macgpio_devclass, 0, 0,
131 struct macgpio_devinfo {
132 struct ofw_bus_devinfo mdi_obdinfo;
133 struct resource_list mdi_resources;
139 macgpio_probe(device_t dev)
143 name = ofw_bus_get_name(dev);
144 if (name && strcmp(name, "gpio") == 0) {
145 device_set_desc(dev, "MacIO GPIO Controller");
153 * Scan Open Firmware child nodes, and attach these as children
157 macgpio_attach(device_t dev)
159 struct macgpio_softc *sc;
160 struct macgpio_devinfo *dinfo;
161 phandle_t root, child, iparent;
165 sc = device_get_softc(dev);
166 root = sc->sc_node = ofw_bus_get_node(dev);
168 sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
169 &sc->sc_gpios_rid, RF_ACTIVE);
172 * Iterate through the sub-devices
174 for (child = OF_child(root); child != 0; child = OF_peer(child)) {
175 dinfo = malloc(sizeof(*dinfo), M_MACGPIO, M_WAITOK | M_ZERO);
176 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
178 free(dinfo, M_MACGPIO);
182 if (OF_getencprop(child, "reg", &dinfo->gpio_num,
183 sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) {
185 * Some early GPIO controllers don't provide GPIO
186 * numbers for GPIOs designed only to provide
187 * interrupt resources. We should still allow these
188 * to attach, but with caution.
191 dinfo->gpio_num = -1;
194 resource_list_init(&dinfo->mdi_resources);
196 if (OF_getencprop(child, "interrupts", irq, sizeof(irq)) ==
198 OF_searchencprop(child, "interrupt-parent", &iparent,
200 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
201 0, MAP_IRQ(iparent, irq[0]),
202 MAP_IRQ(iparent, irq[0]), 1);
205 /* Fix messed-up offsets */
206 if (dinfo->gpio_num > 0x50)
207 dinfo->gpio_num -= 0x50;
209 cdev = device_add_child(dev, NULL, -1);
211 device_printf(dev, "<%s>: device_add_child failed\n",
212 dinfo->mdi_obdinfo.obd_name);
213 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
214 free(dinfo, M_MACGPIO);
217 device_set_ivars(cdev, dinfo);
220 return (bus_generic_attach(dev));
225 macgpio_print_child(device_t dev, device_t child)
227 struct macgpio_devinfo *dinfo;
230 dinfo = device_get_ivars(child);
232 retval += bus_print_child_header(dev, child);
234 if (dinfo->gpio_num >= GPIO_BASE)
235 printf(" gpio %d", dinfo->gpio_num - GPIO_BASE);
236 else if (dinfo->gpio_num >= GPIO_EXTINT_BASE)
237 printf(" extint-gpio %d", dinfo->gpio_num - GPIO_EXTINT_BASE);
238 else if (dinfo->gpio_num >= 0)
239 printf(" addr 0x%02x", dinfo->gpio_num); /* should not happen */
241 resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ,
243 retval += bus_print_child_footer(dev, child);
250 macgpio_probe_nomatch(device_t dev, device_t child)
252 struct macgpio_devinfo *dinfo;
256 dinfo = device_get_ivars(child);
258 if ((type = ofw_bus_get_type(child)) == NULL)
260 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
261 if (dinfo->gpio_num >= 0)
262 printf(" gpio %d",dinfo->gpio_num);
263 resource_list_print_type(&dinfo->mdi_resources, "irq",
265 printf(" (no driver attached)\n");
270 static struct resource *
271 macgpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
272 rman_res_t start, rman_res_t end, rman_res_t count,
275 struct macgpio_devinfo *dinfo;
277 dinfo = device_get_ivars(child);
279 if (type != SYS_RES_IRQ)
282 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type,
283 rid, start, end, count, flags));
287 macgpio_activate_resource(device_t bus, device_t child, int type, int rid,
288 struct resource *res)
290 struct macgpio_softc *sc;
291 struct macgpio_devinfo *dinfo;
294 sc = device_get_softc(bus);
295 dinfo = device_get_ivars(child);
297 if (type != SYS_RES_IRQ)
300 if (dinfo->gpio_num >= 0) {
301 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
303 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
306 return (bus_activate_resource(bus, type, rid, res));
311 macgpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
312 struct resource *res)
314 struct macgpio_softc *sc;
315 struct macgpio_devinfo *dinfo;
318 sc = device_get_softc(bus);
319 dinfo = device_get_ivars(child);
321 if (type != SYS_RES_IRQ)
324 if (dinfo->gpio_num >= 0) {
325 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num);
327 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
330 return (bus_deactivate_resource(bus, type, rid, res));
334 macgpio_read(device_t dev)
336 struct macgpio_softc *sc;
337 struct macgpio_devinfo *dinfo;
339 sc = device_get_softc(device_get_parent(dev));
340 dinfo = device_get_ivars(dev);
342 if (dinfo->gpio_num < 0)
345 return (bus_read_1(sc->sc_gpios,dinfo->gpio_num));
349 macgpio_write(device_t dev, uint8_t val)
351 struct macgpio_softc *sc;
352 struct macgpio_devinfo *dinfo;
354 sc = device_get_softc(device_get_parent(dev));
355 dinfo = device_get_ivars(dev);
357 if (dinfo->gpio_num < 0)
360 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val);
363 static const struct ofw_bus_devinfo *
364 macgpio_get_devinfo(device_t dev, device_t child)
366 struct macgpio_devinfo *dinfo;
368 dinfo = device_get_ivars(child);
369 return (&dinfo->mdi_obdinfo);
373 macgpio_suspend(device_t dev)
375 struct macgpio_softc *sc;
378 sc = device_get_softc(dev);
379 sc->sc_saved_gpio_levels[0] = bus_read_4(sc->sc_gpios, GPIO_LEVELS_0);
380 sc->sc_saved_gpio_levels[1] = bus_read_4(sc->sc_gpios, GPIO_LEVELS_1);
382 for (i = 0; i < GPIO_COUNT; i++)
383 sc->sc_saved_gpios[i] = bus_read_1(sc->sc_gpios, GPIO_BASE + i);
384 for (i = 0; i < GPIO_EXTINT_COUNT; i++)
385 sc->sc_saved_extint_gpios[i] = bus_read_1(sc->sc_gpios, GPIO_EXTINT_BASE + i);
391 macgpio_resume(device_t dev)
393 struct macgpio_softc *sc;
396 sc = device_get_softc(dev);
397 bus_write_4(sc->sc_gpios, GPIO_LEVELS_0, sc->sc_saved_gpio_levels[0]);
398 bus_write_4(sc->sc_gpios, GPIO_LEVELS_1, sc->sc_saved_gpio_levels[1]);
400 for (i = 0; i < GPIO_COUNT; i++)
401 bus_write_1(sc->sc_gpios, GPIO_BASE + i, sc->sc_saved_gpios[i]);
402 for (i = 0; i < GPIO_EXTINT_COUNT; i++)
403 bus_write_1(sc->sc_gpios, GPIO_EXTINT_BASE + i, sc->sc_saved_extint_gpios[i]);