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 /* 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), 107 108 { 0, 0 } 109 }; 110 111 static driver_t macgpio_pci_driver = { 112 "macgpio", 113 macgpio_methods, 114 sizeof(struct macgpio_softc) 115 }; 116 117 devclass_t macgpio_devclass; 118 119 DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, macgpio_devclass, 0, 0); 120 121 struct macgpio_devinfo { 122 struct ofw_bus_devinfo mdi_obdinfo; 123 struct resource_list mdi_resources; 124 125 int gpio_num; 126 }; 127 128 static int 129 macgpio_probe(device_t dev) 130 { 131 const char *name; 132 133 name = ofw_bus_get_name(dev); 134 if (name && strcmp(name, "gpio") == 0) { 135 device_set_desc(dev, "MacIO GPIO Controller"); 136 return (0); 137 } 138 139 return (ENXIO); 140 } 141 142 /* 143 * Scan Open Firmware child nodes, and attach these as children 144 * of the macgpio bus 145 */ 146 static int 147 macgpio_attach(device_t dev) 148 { 149 struct macgpio_softc *sc; 150 struct macgpio_devinfo *dinfo; 151 phandle_t root; 152 phandle_t child; 153 device_t cdev; 154 uint32_t irq; 155 156 sc = device_get_softc(dev); 157 root = sc->sc_node = ofw_bus_get_node(dev); 158 159 sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 160 &sc->sc_gpios_rid, RF_ACTIVE); 161 162 /* 163 * Iterate through the sub-devices 164 */ 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) != 168 0) { 169 free(dinfo, M_MACGPIO); 170 continue; 171 } 172 173 if (OF_getprop(child,"reg",&dinfo->gpio_num, 174 sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) { 175 free(dinfo, M_MACGPIO); 176 continue; 177 } 178 179 resource_list_init(&dinfo->mdi_resources); 180 181 if (OF_getprop(child,"interrupts",&irq, sizeof(irq)) == 182 sizeof(irq)) { 183 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ, 184 0, irq, irq, 1); 185 } 186 187 /* Fix messed-up offsets */ 188 if (dinfo->gpio_num > 0x50) 189 dinfo->gpio_num -= 0x50; 190 191 cdev = device_add_child(dev, NULL, -1); 192 if (cdev == NULL) { 193 device_printf(dev, "<%s>: device_add_child failed\n", 194 dinfo->mdi_obdinfo.obd_name); 195 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo); 196 free(dinfo, M_MACGPIO); 197 continue; 198 } 199 device_set_ivars(cdev, dinfo); 200 } 201 202 return (bus_generic_attach(dev)); 203 } 204 205 206 static int 207 macgpio_print_child(device_t dev, device_t child) 208 { 209 struct macgpio_devinfo *dinfo; 210 int retval = 0; 211 212 dinfo = device_get_ivars(child); 213 214 retval += bus_print_child_header(dev, child); 215 216 if (dinfo->gpio_num >= GPIO_BASE) 217 printf(" gpio %d", dinfo->gpio_num - GPIO_BASE); 218 else if (dinfo->gpio_num >= GPIO_EXTINT_BASE) 219 printf(" extint-gpio %d", dinfo->gpio_num - GPIO_EXTINT_BASE); 220 else 221 printf(" addr 0x%02x", dinfo->gpio_num); /* should not happen */ 222 223 resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ, 224 "%ld"); 225 retval += bus_print_child_footer(dev, child); 226 227 return (retval); 228 } 229 230 231 static void 232 macgpio_probe_nomatch(device_t dev, device_t child) 233 { 234 struct macgpio_devinfo *dinfo; 235 const char *type; 236 237 if (bootverbose) { 238 dinfo = device_get_ivars(child); 239 240 if ((type = ofw_bus_get_type(child)) == NULL) 241 type = "(unknown)"; 242 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child)); 243 printf(" gpio %d",dinfo->gpio_num); 244 resource_list_print_type(&dinfo->mdi_resources, "irq", 245 SYS_RES_IRQ, "%ld"); 246 printf(" (no driver attached)\n"); 247 } 248 } 249 250 251 static struct resource * 252 macgpio_alloc_resource(device_t bus, device_t child, int type, int *rid, 253 u_long start, u_long end, u_long count, u_int flags) 254 { 255 struct macgpio_softc *sc; 256 struct macgpio_devinfo *dinfo; 257 258 sc = device_get_softc(bus); 259 dinfo = device_get_ivars(child); 260 261 if (type != SYS_RES_IRQ) 262 return (NULL); 263 264 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type, 265 rid, start, end, count, flags)); 266 } 267 268 static int 269 macgpio_activate_resource(device_t bus, device_t child, int type, int rid, 270 struct resource *res) 271 { 272 struct macgpio_softc *sc; 273 struct macgpio_devinfo *dinfo; 274 u_char val; 275 276 sc = device_get_softc(bus); 277 dinfo = device_get_ivars(child); 278 279 if (type != SYS_RES_IRQ) 280 return ENXIO; 281 282 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num); 283 val |= 0x80; 284 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val); 285 286 return (bus_activate_resource(bus, type, rid, res)); 287 } 288 289 290 static int 291 macgpio_deactivate_resource(device_t bus, device_t child, int type, int rid, 292 struct resource *res) 293 { 294 struct macgpio_softc *sc; 295 struct macgpio_devinfo *dinfo; 296 u_char val; 297 298 sc = device_get_softc(bus); 299 dinfo = device_get_ivars(child); 300 301 if (type != SYS_RES_IRQ) 302 return ENXIO; 303 304 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num); 305 val &= ~0x80; 306 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val); 307 308 return (bus_deactivate_resource(bus, type, rid, res)); 309 } 310 311 uint8_t 312 macgpio_read(device_t dev) 313 { 314 struct macgpio_softc *sc; 315 struct macgpio_devinfo *dinfo; 316 317 sc = device_get_softc(device_get_parent(dev)); 318 dinfo = device_get_ivars(dev); 319 320 return (bus_read_1(sc->sc_gpios,dinfo->gpio_num)); 321 } 322 323 void 324 macgpio_write(device_t dev, uint8_t val) 325 { 326 struct macgpio_softc *sc; 327 struct macgpio_devinfo *dinfo; 328 329 sc = device_get_softc(device_get_parent(dev)); 330 dinfo = device_get_ivars(dev); 331 332 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val); 333 } 334 335 static const struct ofw_bus_devinfo * 336 macgpio_get_devinfo(device_t dev, device_t child) 337 { 338 struct macgpio_devinfo *dinfo; 339 340 dinfo = device_get_ivars(child); 341 return (&dinfo->mdi_obdinfo); 342 } 343 344