1 /* 2 * Copyright 2002 by Peter Grehan. 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 * 3. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 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 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 /* 31 * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/bus.h> 39 #include <machine/bus.h> 40 #include <sys/rman.h> 41 42 #include <machine/vmparam.h> 43 #include <vm/vm.h> 44 #include <vm/pmap.h> 45 #include <machine/pmap.h> 46 47 #include <machine/resource.h> 48 49 #include <dev/ofw/openfirm.h> 50 51 #include <powerpc/powermac/maciovar.h> 52 53 #include <dev/pci/pcivar.h> 54 #include <dev/pci/pcireg.h> 55 56 static MALLOC_DEFINE(M_MACIO, "macio", "macio device information"); 57 58 static int macio_probe(device_t); 59 static int macio_attach(device_t); 60 static int macio_print_child(device_t dev, device_t child); 61 static void macio_probe_nomatch(device_t, device_t); 62 static int macio_read_ivar(device_t, device_t, int, uintptr_t *); 63 static int macio_write_ivar(device_t, device_t, int, uintptr_t); 64 static struct resource *macio_alloc_resource(device_t, device_t, int, int *, 65 u_long, u_long, u_long, u_int); 66 static int macio_activate_resource(device_t, device_t, int, int, 67 struct resource *); 68 static int macio_deactivate_resource(device_t, device_t, int, int, 69 struct resource *); 70 static int macio_release_resource(device_t, device_t, int, int, 71 struct resource *); 72 static struct resource_list *macio_get_resource_list (device_t, device_t); 73 74 /* 75 * Bus interface definition 76 */ 77 static device_method_t macio_methods[] = { 78 /* Device interface */ 79 DEVMETHOD(device_probe, macio_probe), 80 DEVMETHOD(device_attach, macio_attach), 81 DEVMETHOD(device_detach, bus_generic_detach), 82 DEVMETHOD(device_shutdown, bus_generic_shutdown), 83 DEVMETHOD(device_suspend, bus_generic_suspend), 84 DEVMETHOD(device_resume, bus_generic_resume), 85 86 /* Bus interface */ 87 DEVMETHOD(bus_print_child, macio_print_child), 88 DEVMETHOD(bus_probe_nomatch, macio_probe_nomatch), 89 DEVMETHOD(bus_read_ivar, macio_read_ivar), 90 DEVMETHOD(bus_write_ivar, macio_write_ivar), 91 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 92 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 93 94 DEVMETHOD(bus_alloc_resource, macio_alloc_resource), 95 DEVMETHOD(bus_release_resource, macio_release_resource), 96 DEVMETHOD(bus_activate_resource, macio_activate_resource), 97 DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource), 98 DEVMETHOD(bus_get_resource_list, macio_get_resource_list), 99 100 { 0, 0 } 101 }; 102 103 static driver_t macio_pci_driver = { 104 "macio", 105 macio_methods, 106 sizeof(struct macio_softc) 107 }; 108 109 devclass_t macio_devclass; 110 111 DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0); 112 113 /* 114 * PCI ID search table 115 */ 116 static struct macio_pci_dev { 117 u_int32_t mpd_devid; 118 char *mpd_desc; 119 } macio_pci_devlist[] = { 120 { 0x0017106b, "Paddington I/O Controller" }, 121 { 0x0022106b, "KeyLargo I/O Controller" }, 122 { 0x0025106b, "Pangea I/O Controller" }, 123 { 0x003e106b, "Intrepid I/O Controller" }, 124 { 0, NULL } 125 }; 126 127 /* 128 * Devices to exclude from the probe 129 * XXX some of these may be required in the future... 130 */ 131 #define MACIO_QUIRK_IGNORE 0x00000001 132 #define MACIO_QUIRK_CHILD_HAS_INTR 0x00000002 133 134 struct macio_quirk_entry { 135 const char *mq_name; 136 int mq_quirks; 137 }; 138 139 static struct macio_quirk_entry macio_quirks[] = { 140 { "interrupt-controller", MACIO_QUIRK_IGNORE }, 141 { "escc-legacy", MACIO_QUIRK_IGNORE }, 142 { "timer", MACIO_QUIRK_IGNORE }, 143 { "escc", MACIO_QUIRK_CHILD_HAS_INTR }, 144 { NULL, 0 } 145 }; 146 147 static int 148 macio_get_quirks(const char *name) 149 { 150 struct macio_quirk_entry *mqe; 151 152 for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++) 153 if (strcmp(name, mqe->mq_name) == 0) 154 return (mqe->mq_quirks); 155 return (0); 156 } 157 158 159 /* 160 * Add an interrupt to the dev's resource list if present 161 */ 162 static void 163 macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo) 164 { 165 int intr; 166 167 if (dinfo->mdi_ninterrupts >= 5) { 168 printf("macio: device has more than 5 interrupts\n"); 169 return; 170 } 171 172 if (OF_getprop(devnode, "interrupts", &intr, sizeof(intr)) == -1) { 173 if (OF_getprop(devnode, "AAPL,interrupts", &intr, 174 sizeof(intr)) == -1) 175 return; 176 } 177 178 if (intr == -1) 179 return; 180 181 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ, 182 dinfo->mdi_ninterrupts, intr, intr, 1); 183 184 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr; 185 dinfo->mdi_ninterrupts++; 186 } 187 188 189 static void 190 macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo) 191 { 192 struct macio_reg *reg; 193 int i, nreg; 194 195 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®); 196 if (nreg == -1) 197 return; 198 199 dinfo->mdi_nregs = nreg; 200 dinfo->mdi_regs = reg; 201 202 for (i = 0; i < nreg; i++) { 203 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i, 204 reg[i].mr_base, reg[i].mr_base + reg[i].mr_size, 205 reg[i].mr_size); 206 } 207 } 208 209 /* 210 * PCI probe 211 */ 212 static int 213 macio_probe(device_t dev) 214 { 215 int i; 216 u_int32_t devid; 217 218 devid = pci_get_devid(dev); 219 for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) { 220 if (devid == macio_pci_devlist[i].mpd_devid) { 221 device_set_desc(dev, macio_pci_devlist[i].mpd_desc); 222 return (0); 223 } 224 } 225 226 return (ENXIO); 227 } 228 229 /* 230 * PCI attach: scan OpenFirmware child nodes, and attach these as children 231 * of the macio bus 232 */ 233 static int 234 macio_attach(device_t dev) 235 { 236 struct macio_softc *sc; 237 struct macio_devinfo *dinfo; 238 phandle_t root; 239 phandle_t child; 240 phandle_t subchild; 241 device_t cdev; 242 u_int reg[3]; 243 char *name, *type; 244 int quirks; 245 246 sc = device_get_softc(dev); 247 root = sc->sc_node = OF_finddevice("mac-io"); 248 249 /* 250 * Locate the device node and it's base address 251 */ 252 if (OF_getprop(root, "assigned-addresses", 253 reg, sizeof(reg)) < sizeof(reg)) { 254 return (ENXIO); 255 } 256 257 sc->sc_base = reg[2]; 258 sc->sc_size = MACIO_REG_SIZE; 259 260 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 261 sc->sc_mem_rman.rm_descr = "MacIO Device Memory"; 262 if (rman_init(&sc->sc_mem_rman) != 0) { 263 device_printf(dev, 264 "failed to init mem range resources\n"); 265 return (ENXIO); 266 } 267 rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size); 268 269 /* 270 * Iterate through the sub-devices 271 */ 272 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 273 OF_getprop_alloc(child, "name", 1, (void **)&name); 274 OF_getprop_alloc(child, "device_type", 1, (void **)&type); 275 276 quirks = macio_get_quirks(name); 277 if ((quirks & MACIO_QUIRK_IGNORE) != 0) { 278 free(name, M_OFWPROP); 279 free(type, M_OFWPROP); 280 continue; 281 } 282 283 cdev = device_add_child(dev, NULL, -1); 284 if (cdev != NULL) { 285 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK); 286 memset(dinfo, 0, sizeof(*dinfo)); 287 resource_list_init(&dinfo->mdi_resources); 288 dinfo->mdi_node = child; 289 dinfo->mdi_name = name; 290 dinfo->mdi_device_type = type; 291 dinfo->mdi_ninterrupts = 0; 292 macio_add_intr(child, dinfo); 293 macio_add_reg(child, dinfo); 294 295 296 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0) { 297 for (subchild = OF_child(child); subchild != 0; 298 subchild = OF_peer(subchild)) { 299 macio_add_intr(subchild, dinfo); 300 } 301 } 302 303 device_set_ivars(cdev, dinfo); 304 } else { 305 free(name, M_OFWPROP); 306 free(type, M_OFWPROP); 307 } 308 } 309 310 return (bus_generic_attach(dev)); 311 } 312 313 314 static int 315 macio_print_child(device_t dev, device_t child) 316 { 317 struct macio_devinfo *dinfo; 318 struct resource_list *rl; 319 int retval = 0; 320 321 dinfo = device_get_ivars(child); 322 rl = &dinfo->mdi_resources; 323 324 retval += bus_print_child_header(dev, child); 325 326 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 327 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 328 329 retval += bus_print_child_footer(dev, child); 330 331 return (retval); 332 } 333 334 335 static void 336 macio_probe_nomatch(device_t dev, device_t child) 337 { 338 struct macio_devinfo *dinfo; 339 struct resource_list *rl; 340 341 if (bootverbose) { 342 dinfo = device_get_ivars(child); 343 rl = &dinfo->mdi_resources; 344 345 device_printf(dev, "<%s, %s>", macio_get_devtype(child), 346 macio_get_name(child)); 347 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 348 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 349 printf(" (no driver attached)\n"); 350 } 351 } 352 353 354 static int 355 macio_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 356 { 357 struct macio_devinfo *dinfo; 358 359 if ((dinfo = device_get_ivars(child)) == 0) 360 return (ENOENT); 361 362 switch (which) { 363 case MACIO_IVAR_NODE: 364 *result = dinfo->mdi_node; 365 break; 366 case MACIO_IVAR_NAME: 367 *result = (uintptr_t)dinfo->mdi_name; 368 break; 369 case MACIO_IVAR_DEVTYPE: 370 *result = (uintptr_t)dinfo->mdi_device_type; 371 break; 372 case MACIO_IVAR_NREGS: 373 *result = dinfo->mdi_nregs; 374 break; 375 case MACIO_IVAR_REGS: 376 *result = (uintptr_t)dinfo->mdi_regs; 377 break; 378 default: 379 return (ENOENT); 380 } 381 382 return (0); 383 } 384 385 386 static int 387 macio_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 388 { 389 return (EINVAL); 390 } 391 392 393 static struct resource * 394 macio_alloc_resource(device_t bus, device_t child, int type, int *rid, 395 u_long start, u_long end, u_long count, u_int flags) 396 { 397 struct macio_softc *sc; 398 int needactivate; 399 struct resource *rv; 400 struct rman *rm; 401 bus_space_tag_t tagval; 402 u_long adjstart, adjend, adjcount; 403 struct macio_devinfo *dinfo; 404 struct resource_list_entry *rle; 405 406 sc = device_get_softc(bus); 407 dinfo = device_get_ivars(child); 408 409 needactivate = flags & RF_ACTIVE; 410 flags &= ~RF_ACTIVE; 411 412 switch (type) { 413 case SYS_RES_MEMORY: 414 case SYS_RES_IOPORT: 415 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY, 416 *rid); 417 if (rle == NULL) { 418 device_printf(bus, "no rle for %s memory %d\n", 419 device_get_nameunit(child), *rid); 420 return (NULL); 421 } 422 423 if (start < rle->start) 424 adjstart = rle->start; 425 else if (start > rle->end) 426 adjstart = rle->end; 427 else 428 adjstart = start; 429 430 if (end < rle->start) 431 adjend = rle->start; 432 else if (end > rle->end) 433 adjend = rle->end; 434 else 435 adjend = end; 436 437 adjcount = adjend - adjstart; 438 439 rm = &sc->sc_mem_rman; 440 441 tagval = PPC_BUS_SPACE_MEM; 442 if (flags & PPC_BUS_SPARSE4) 443 tagval |= 4; 444 445 break; 446 447 case SYS_RES_IRQ: 448 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ, 449 *rid); 450 if (rle == NULL) { 451 if (dinfo->mdi_ninterrupts >= 5) { 452 device_printf(bus, 453 "%s has more than 5 interrupts\n", 454 device_get_nameunit(child)); 455 return (NULL); 456 } 457 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ, 458 dinfo->mdi_ninterrupts, start, start, 1); 459 460 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start; 461 dinfo->mdi_ninterrupts++; 462 } 463 464 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, 465 type, rid, start, end, count, flags)); 466 break; 467 468 default: 469 device_printf(bus, "unknown resource request from %s\n", 470 device_get_nameunit(child)); 471 return (NULL); 472 } 473 474 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags, 475 child); 476 if (rv == NULL) { 477 device_printf(bus, 478 "failed to reserve resource %#lx - %#lx (%#lx) for %s\n", 479 adjstart, adjend, adjcount, device_get_nameunit(child)); 480 return (NULL); 481 } 482 483 rman_set_bustag(rv, tagval); 484 rman_set_bushandle(rv, rman_get_start(rv)); 485 486 if (needactivate) { 487 if (bus_activate_resource(child, type, *rid, rv) != 0) { 488 device_printf(bus, 489 "failed to activate resource for %s\n", 490 device_get_nameunit(child)); 491 rman_release_resource(rv); 492 return (NULL); 493 } 494 } 495 496 return (rv); 497 } 498 499 500 static int 501 macio_release_resource(device_t bus, device_t child, int type, int rid, 502 struct resource *res) 503 { 504 if (rman_get_flags(res) & RF_ACTIVE) { 505 int error = bus_deactivate_resource(child, type, rid, res); 506 if (error) 507 return error; 508 } 509 510 return (rman_release_resource(res)); 511 } 512 513 514 static int 515 macio_activate_resource(device_t bus, device_t child, int type, int rid, 516 struct resource *res) 517 { 518 struct macio_softc *sc; 519 void *p; 520 521 sc = device_get_softc(bus); 522 523 if (type == SYS_RES_IRQ) 524 return (bus_activate_resource(bus, type, rid, res)); 525 526 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 527 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base, 528 (vm_size_t)rman_get_size(res)); 529 if (p == NULL) 530 return (ENOMEM); 531 rman_set_virtual(res, p); 532 rman_set_bushandle(res, (u_long)p); 533 } 534 535 return (rman_activate_resource(res)); 536 } 537 538 539 static int 540 macio_deactivate_resource(device_t bus, device_t child, int type, int rid, 541 struct resource *res) 542 { 543 /* 544 * If this is a memory resource, unmap it. 545 */ 546 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 547 u_int32_t psize; 548 549 psize = rman_get_size(res); 550 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 551 } 552 553 return (rman_deactivate_resource(res)); 554 } 555 556 557 static struct resource_list * 558 macio_get_resource_list (device_t dev, device_t child) 559 { 560 struct macio_devinfo *dinfo = device_get_ivars(child); 561 struct resource_list *rl = &dinfo->mdi_resources; 562 563 if (!rl) 564 return (NULL); 565 566 return (rl); 567 } 568