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 <sys/rman.h> 39 40 #include <vm/vm.h> 41 #include <vm/pmap.h> 42 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> 48 49 #include <dev/ofw/ofw_bus.h> 50 #include <dev/ofw/ofw_bus_subr.h> 51 #include <dev/ofw/openfirm.h> 52 53 #include <powerpc/powermac/macgpiovar.h> 54 55 /* 56 * Macgpio softc 57 */ 58 struct macgpio_softc { 59 phandle_t sc_node; 60 struct resource *sc_gpios; 61 int sc_gpios_rid; 62 }; 63 64 static MALLOC_DEFINE(M_MACGPIO, "macgpio", "macgpio device information"); 65 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, 73 struct resource *); 74 static int macgpio_deactivate_resource(device_t, device_t, int, int, 75 struct resource *); 76 static ofw_bus_get_devinfo_t macgpio_get_devinfo; 77 78 /* 79 * Bus interface definition 80 */ 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), 89 90 /* Bus interface */ 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), 95 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), 100 101 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 102 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), 110 111 { 0, 0 } 112 }; 113 114 static driver_t macgpio_pci_driver = { 115 "macgpio", 116 macgpio_methods, 117 sizeof(struct macgpio_softc) 118 }; 119 120 devclass_t macgpio_devclass; 121 122 DRIVER_MODULE(macgpio, macio, macgpio_pci_driver, macgpio_devclass, 0, 0); 123 124 struct macgpio_devinfo { 125 struct ofw_bus_devinfo mdi_obdinfo; 126 struct resource_list mdi_resources; 127 128 int gpio_num; 129 }; 130 131 static int 132 macgpio_probe(device_t dev) 133 { 134 const char *name; 135 136 name = ofw_bus_get_name(dev); 137 if (name && strcmp(name, "gpio") == 0) { 138 device_set_desc(dev, "MacIO GPIO Controller"); 139 return (0); 140 } 141 142 return (ENXIO); 143 } 144 145 /* 146 * Scan Open Firmware child nodes, and attach these as children 147 * of the macgpio bus 148 */ 149 static int 150 macgpio_attach(device_t dev) 151 { 152 struct macgpio_softc *sc; 153 struct macgpio_devinfo *dinfo; 154 phandle_t root, child, iparent; 155 device_t cdev; 156 uint32_t irq; 157 158 sc = device_get_softc(dev); 159 root = sc->sc_node = ofw_bus_get_node(dev); 160 161 sc->sc_gpios = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 162 &sc->sc_gpios_rid, RF_ACTIVE); 163 164 /* 165 * Iterate through the sub-devices 166 */ 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) != 170 0) { 171 free(dinfo, M_MACGPIO); 172 continue; 173 } 174 175 if (OF_getprop(child,"reg",&dinfo->gpio_num, 176 sizeof(dinfo->gpio_num)) != sizeof(dinfo->gpio_num)) { 177 /* 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. 182 */ 183 184 dinfo->gpio_num = -1; 185 } 186 187 resource_list_init(&dinfo->mdi_resources); 188 189 if (OF_getprop(child, "interrupts", &irq, sizeof(irq)) == 190 sizeof(irq)) { 191 OF_searchprop(child, "interrupt-parent", &iparent, 192 sizeof(iparent)); 193 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ, 194 0, MAP_IRQ(iparent, irq), MAP_IRQ(iparent, irq), 195 1); 196 } 197 198 /* Fix messed-up offsets */ 199 if (dinfo->gpio_num > 0x50) 200 dinfo->gpio_num -= 0x50; 201 202 cdev = device_add_child(dev, NULL, -1); 203 if (cdev == NULL) { 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); 208 continue; 209 } 210 device_set_ivars(cdev, dinfo); 211 } 212 213 return (bus_generic_attach(dev)); 214 } 215 216 217 static int 218 macgpio_print_child(device_t dev, device_t child) 219 { 220 struct macgpio_devinfo *dinfo; 221 int retval = 0; 222 223 dinfo = device_get_ivars(child); 224 225 retval += bus_print_child_header(dev, child); 226 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 */ 233 234 resource_list_print_type(&dinfo->mdi_resources, "irq", SYS_RES_IRQ, 235 "%ld"); 236 retval += bus_print_child_footer(dev, child); 237 238 return (retval); 239 } 240 241 242 static void 243 macgpio_probe_nomatch(device_t dev, device_t child) 244 { 245 struct macgpio_devinfo *dinfo; 246 const char *type; 247 248 if (bootverbose) { 249 dinfo = device_get_ivars(child); 250 251 if ((type = ofw_bus_get_type(child)) == NULL) 252 type = "(unknown)"; 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", 257 SYS_RES_IRQ, "%ld"); 258 printf(" (no driver attached)\n"); 259 } 260 } 261 262 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) 266 { 267 struct macgpio_devinfo *dinfo; 268 269 dinfo = device_get_ivars(child); 270 271 if (type != SYS_RES_IRQ) 272 return (NULL); 273 274 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, type, 275 rid, start, end, count, flags)); 276 } 277 278 static int 279 macgpio_activate_resource(device_t bus, device_t child, int type, int rid, 280 struct resource *res) 281 { 282 struct macgpio_softc *sc; 283 struct macgpio_devinfo *dinfo; 284 u_char val; 285 286 sc = device_get_softc(bus); 287 dinfo = device_get_ivars(child); 288 289 if (type != SYS_RES_IRQ) 290 return ENXIO; 291 292 if (dinfo->gpio_num >= 0) { 293 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num); 294 val |= 0x80; 295 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val); 296 } 297 298 return (bus_activate_resource(bus, type, rid, res)); 299 } 300 301 302 static int 303 macgpio_deactivate_resource(device_t bus, device_t child, int type, int rid, 304 struct resource *res) 305 { 306 struct macgpio_softc *sc; 307 struct macgpio_devinfo *dinfo; 308 u_char val; 309 310 sc = device_get_softc(bus); 311 dinfo = device_get_ivars(child); 312 313 if (type != SYS_RES_IRQ) 314 return ENXIO; 315 316 if (dinfo->gpio_num >= 0) { 317 val = bus_read_1(sc->sc_gpios,dinfo->gpio_num); 318 val &= ~0x80; 319 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val); 320 } 321 322 return (bus_deactivate_resource(bus, type, rid, res)); 323 } 324 325 uint8_t 326 macgpio_read(device_t dev) 327 { 328 struct macgpio_softc *sc; 329 struct macgpio_devinfo *dinfo; 330 331 sc = device_get_softc(device_get_parent(dev)); 332 dinfo = device_get_ivars(dev); 333 334 if (dinfo->gpio_num < 0) 335 return (0); 336 337 return (bus_read_1(sc->sc_gpios,dinfo->gpio_num)); 338 } 339 340 void 341 macgpio_write(device_t dev, uint8_t val) 342 { 343 struct macgpio_softc *sc; 344 struct macgpio_devinfo *dinfo; 345 346 sc = device_get_softc(device_get_parent(dev)); 347 dinfo = device_get_ivars(dev); 348 349 if (dinfo->gpio_num < 0) 350 return; 351 352 bus_write_1(sc->sc_gpios,dinfo->gpio_num,val); 353 } 354 355 static const struct ofw_bus_devinfo * 356 macgpio_get_devinfo(device_t dev, device_t child) 357 { 358 struct macgpio_devinfo *dinfo; 359 360 dinfo = device_get_ivars(child); 361 return (&dinfo->mdi_obdinfo); 362 } 363 364