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