1 /* 2 * Bus & driver management routines for devices within 3 * a MacIO ASIC. Interface to new driver model mostly 4 * stolen from the PCI version. 5 * 6 * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org) 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 11 * 2 of the License, or (at your option) any later version. 12 * 13 * TODO: 14 * 15 * - Don't probe below media bay by default, but instead provide 16 * some hooks for media bay to dynamically add/remove it's own 17 * sub-devices. 18 */ 19 20 #include <linux/string.h> 21 #include <linux/kernel.h> 22 #include <linux/pci.h> 23 #include <linux/pci_ids.h> 24 #include <linux/init.h> 25 #include <linux/module.h> 26 #include <linux/slab.h> 27 28 #include <asm/machdep.h> 29 #include <asm/macio.h> 30 #include <asm/pmac_feature.h> 31 #include <asm/prom.h> 32 #include <asm/pci-bridge.h> 33 34 #undef DEBUG 35 36 #define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12) 37 38 static struct macio_chip *macio_on_hold; 39 40 static int macio_bus_match(struct device *dev, struct device_driver *drv) 41 { 42 struct macio_dev * macio_dev = to_macio_device(dev); 43 struct macio_driver * macio_drv = to_macio_driver(drv); 44 const struct of_device_id * matches = macio_drv->match_table; 45 46 if (!matches) 47 return 0; 48 49 return of_match_device(matches, &macio_dev->ofdev) != NULL; 50 } 51 52 struct macio_dev *macio_dev_get(struct macio_dev *dev) 53 { 54 struct device *tmp; 55 56 if (!dev) 57 return NULL; 58 tmp = get_device(&dev->ofdev.dev); 59 if (tmp) 60 return to_macio_device(tmp); 61 else 62 return NULL; 63 } 64 65 void macio_dev_put(struct macio_dev *dev) 66 { 67 if (dev) 68 put_device(&dev->ofdev.dev); 69 } 70 71 72 static int macio_device_probe(struct device *dev) 73 { 74 int error = -ENODEV; 75 struct macio_driver *drv; 76 struct macio_dev *macio_dev; 77 const struct of_device_id *match; 78 79 drv = to_macio_driver(dev->driver); 80 macio_dev = to_macio_device(dev); 81 82 if (!drv->probe) 83 return error; 84 85 macio_dev_get(macio_dev); 86 87 match = of_match_device(drv->match_table, &macio_dev->ofdev); 88 if (match) 89 error = drv->probe(macio_dev, match); 90 if (error) 91 macio_dev_put(macio_dev); 92 93 return error; 94 } 95 96 static int macio_device_remove(struct device *dev) 97 { 98 struct macio_dev * macio_dev = to_macio_device(dev); 99 struct macio_driver * drv = to_macio_driver(dev->driver); 100 101 if (dev->driver && drv->remove) 102 drv->remove(macio_dev); 103 macio_dev_put(macio_dev); 104 105 return 0; 106 } 107 108 static void macio_device_shutdown(struct device *dev) 109 { 110 struct macio_dev * macio_dev = to_macio_device(dev); 111 struct macio_driver * drv = to_macio_driver(dev->driver); 112 113 if (dev->driver && drv->shutdown) 114 drv->shutdown(macio_dev); 115 } 116 117 static int macio_device_suspend(struct device *dev, pm_message_t state) 118 { 119 struct macio_dev * macio_dev = to_macio_device(dev); 120 struct macio_driver * drv = to_macio_driver(dev->driver); 121 122 if (dev->driver && drv->suspend) 123 return drv->suspend(macio_dev, state); 124 return 0; 125 } 126 127 static int macio_device_resume(struct device * dev) 128 { 129 struct macio_dev * macio_dev = to_macio_device(dev); 130 struct macio_driver * drv = to_macio_driver(dev->driver); 131 132 if (dev->driver && drv->resume) 133 return drv->resume(macio_dev); 134 return 0; 135 } 136 137 extern struct device_attribute macio_dev_attrs[]; 138 139 struct bus_type macio_bus_type = { 140 .name = "macio", 141 .match = macio_bus_match, 142 .uevent = of_device_uevent, 143 .probe = macio_device_probe, 144 .remove = macio_device_remove, 145 .shutdown = macio_device_shutdown, 146 .suspend = macio_device_suspend, 147 .resume = macio_device_resume, 148 .dev_attrs = macio_dev_attrs, 149 }; 150 151 static int __init macio_bus_driver_init(void) 152 { 153 return bus_register(&macio_bus_type); 154 } 155 156 postcore_initcall(macio_bus_driver_init); 157 158 159 /** 160 * macio_release_dev - free a macio device structure when all users of it are 161 * finished. 162 * @dev: device that's been disconnected 163 * 164 * Will be called only by the device core when all users of this macio device 165 * are done. This currently means never as we don't hot remove any macio 166 * device yet, though that will happen with mediabay based devices in a later 167 * implementation. 168 */ 169 static void macio_release_dev(struct device *dev) 170 { 171 struct macio_dev *mdev; 172 173 mdev = to_macio_device(dev); 174 kfree(mdev); 175 } 176 177 /** 178 * macio_resource_quirks - tweak or skip some resources for a device 179 * @np: pointer to the device node 180 * @res: resulting resource 181 * @index: index of resource in node 182 * 183 * If this routine returns non-null, then the resource is completely 184 * skipped. 185 */ 186 static int macio_resource_quirks(struct device_node *np, struct resource *res, 187 int index) 188 { 189 /* Only quirks for memory resources for now */ 190 if ((res->flags & IORESOURCE_MEM) == 0) 191 return 0; 192 193 /* Grand Central has too large resource 0 on some machines */ 194 if (index == 0 && !strcmp(np->name, "gc")) 195 res->end = res->start + 0x1ffff; 196 197 /* Airport has bogus resource 2 */ 198 if (index >= 2 && !strcmp(np->name, "radio")) 199 return 1; 200 201 #ifndef CONFIG_PPC64 202 /* DBDMAs may have bogus sizes */ 203 if ((res->start & 0x0001f000) == 0x00008000) 204 res->end = res->start + 0xff; 205 #endif /* CONFIG_PPC64 */ 206 207 /* ESCC parent eats child resources. We could have added a 208 * level of hierarchy, but I don't really feel the need 209 * for it 210 */ 211 if (!strcmp(np->name, "escc")) 212 return 1; 213 214 /* ESCC has bogus resources >= 3 */ 215 if (index >= 3 && !(strcmp(np->name, "ch-a") && 216 strcmp(np->name, "ch-b"))) 217 return 1; 218 219 /* Media bay has too many resources, keep only first one */ 220 if (index > 0 && !strcmp(np->name, "media-bay")) 221 return 1; 222 223 /* Some older IDE resources have bogus sizes */ 224 if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") && 225 strcmp(np->type, "ide") && strcmp(np->type, "ata"))) { 226 if (index == 0 && (res->end - res->start) > 0xfff) 227 res->end = res->start + 0xfff; 228 if (index == 1 && (res->end - res->start) > 0xff) 229 res->end = res->start + 0xff; 230 } 231 return 0; 232 } 233 234 static void macio_create_fixup_irq(struct macio_dev *dev, int index, 235 unsigned int line) 236 { 237 unsigned int irq; 238 239 irq = irq_create_mapping(NULL, line); 240 if (irq != NO_IRQ) { 241 dev->interrupt[index].start = irq; 242 dev->interrupt[index].flags = IORESOURCE_IRQ; 243 dev->interrupt[index].name = dev->ofdev.dev.bus_id; 244 } 245 if (dev->n_interrupts <= index) 246 dev->n_interrupts = index + 1; 247 } 248 249 static void macio_add_missing_resources(struct macio_dev *dev) 250 { 251 struct device_node *np = dev->ofdev.node; 252 unsigned int irq_base; 253 254 /* Gatwick has some missing interrupts on child nodes */ 255 if (dev->bus->chip->type != macio_gatwick) 256 return; 257 258 /* irq_base is always 64 on gatwick. I have no cleaner way to get 259 * that value from here at this point 260 */ 261 irq_base = 64; 262 263 /* Fix SCC */ 264 if (strcmp(np->name, "ch-a") == 0) { 265 macio_create_fixup_irq(dev, 0, 15 + irq_base); 266 macio_create_fixup_irq(dev, 1, 4 + irq_base); 267 macio_create_fixup_irq(dev, 2, 5 + irq_base); 268 printk(KERN_INFO "macio: fixed SCC irqs on gatwick\n"); 269 } 270 271 /* Fix media-bay */ 272 if (strcmp(np->name, "media-bay") == 0) { 273 macio_create_fixup_irq(dev, 0, 29 + irq_base); 274 printk(KERN_INFO "macio: fixed media-bay irq on gatwick\n"); 275 } 276 277 /* Fix left media bay childs */ 278 if (dev->media_bay != NULL && strcmp(np->name, "floppy") == 0) { 279 macio_create_fixup_irq(dev, 0, 19 + irq_base); 280 macio_create_fixup_irq(dev, 1, 1 + irq_base); 281 printk(KERN_INFO "macio: fixed left floppy irqs\n"); 282 } 283 if (dev->media_bay != NULL && strcasecmp(np->name, "ata4") == 0) { 284 macio_create_fixup_irq(dev, 0, 14 + irq_base); 285 macio_create_fixup_irq(dev, 0, 3 + irq_base); 286 printk(KERN_INFO "macio: fixed left ide irqs\n"); 287 } 288 } 289 290 static void macio_setup_interrupts(struct macio_dev *dev) 291 { 292 struct device_node *np = dev->ofdev.node; 293 unsigned int irq; 294 int i = 0, j = 0; 295 296 for (;;) { 297 struct resource *res = &dev->interrupt[j]; 298 299 if (j >= MACIO_DEV_COUNT_IRQS) 300 break; 301 irq = irq_of_parse_and_map(np, i++); 302 if (irq == NO_IRQ) 303 break; 304 res->start = irq; 305 res->flags = IORESOURCE_IRQ; 306 res->name = dev->ofdev.dev.bus_id; 307 if (macio_resource_quirks(np, res, i - 1)) { 308 memset(res, 0, sizeof(struct resource)); 309 continue; 310 } else 311 j++; 312 } 313 dev->n_interrupts = j; 314 } 315 316 static void macio_setup_resources(struct macio_dev *dev, 317 struct resource *parent_res) 318 { 319 struct device_node *np = dev->ofdev.node; 320 struct resource r; 321 int index; 322 323 for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) { 324 struct resource *res = &dev->resource[index]; 325 if (index >= MACIO_DEV_COUNT_RESOURCES) 326 break; 327 *res = r; 328 res->name = dev->ofdev.dev.bus_id; 329 330 if (macio_resource_quirks(np, res, index)) { 331 memset(res, 0, sizeof(struct resource)); 332 continue; 333 } 334 /* Currently, we consider failure as harmless, this may 335 * change in the future, once I've found all the device 336 * tree bugs in older machines & worked around them 337 */ 338 if (insert_resource(parent_res, res)) { 339 printk(KERN_WARNING "Can't request resource " 340 "%d for MacIO device %s\n", 341 index, dev->ofdev.dev.bus_id); 342 } 343 } 344 dev->n_resources = index; 345 } 346 347 /** 348 * macio_add_one_device - Add one device from OF node to the device tree 349 * @chip: pointer to the macio_chip holding the device 350 * @np: pointer to the device node in the OF tree 351 * @in_bay: set to 1 if device is part of a media-bay 352 * 353 * When media-bay is changed to hotswap drivers, this function will 354 * be exposed to the bay driver some way... 355 */ 356 static struct macio_dev * macio_add_one_device(struct macio_chip *chip, 357 struct device *parent, 358 struct device_node *np, 359 struct macio_dev *in_bay, 360 struct resource *parent_res) 361 { 362 struct macio_dev *dev; 363 const u32 *reg; 364 365 if (np == NULL) 366 return NULL; 367 368 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 369 if (!dev) 370 return NULL; 371 372 dev->bus = &chip->lbus; 373 dev->media_bay = in_bay; 374 dev->ofdev.node = np; 375 dev->ofdev.dma_mask = 0xffffffffUL; 376 dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask; 377 dev->ofdev.dev.parent = parent; 378 dev->ofdev.dev.bus = &macio_bus_type; 379 dev->ofdev.dev.release = macio_release_dev; 380 381 #ifdef DEBUG 382 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n", 383 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj); 384 #endif 385 386 /* MacIO itself has a different reg, we use it's PCI base */ 387 if (np == chip->of_node) { 388 sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", 389 chip->lbus.index, 390 #ifdef CONFIG_PCI 391 (unsigned int)pci_resource_start(chip->lbus.pdev, 0), 392 #else 393 0, /* NuBus may want to do something better here */ 394 #endif 395 MAX_NODE_NAME_SIZE, np->name); 396 } else { 397 reg = of_get_property(np, "reg", NULL); 398 sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", 399 chip->lbus.index, 400 reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name); 401 } 402 403 /* Setup interrupts & resources */ 404 macio_setup_interrupts(dev); 405 macio_setup_resources(dev, parent_res); 406 macio_add_missing_resources(dev); 407 408 /* Register with core */ 409 if (of_device_register(&dev->ofdev) != 0) { 410 printk(KERN_DEBUG"macio: device registration error for %s!\n", 411 dev->ofdev.dev.bus_id); 412 kfree(dev); 413 return NULL; 414 } 415 416 return dev; 417 } 418 419 static int macio_skip_device(struct device_node *np) 420 { 421 if (strncmp(np->name, "battery", 7) == 0) 422 return 1; 423 if (strncmp(np->name, "escc-legacy", 11) == 0) 424 return 1; 425 return 0; 426 } 427 428 /** 429 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree 430 * @chip: pointer to the macio_chip holding the devices 431 * 432 * This function will do the job of extracting devices from the 433 * Open Firmware device tree, build macio_dev structures and add 434 * them to the Linux device tree. 435 * 436 * For now, childs of media-bay are added now as well. This will 437 * change rsn though. 438 */ 439 static void macio_pci_add_devices(struct macio_chip *chip) 440 { 441 struct device_node *np, *pnode; 442 struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL; 443 struct device *parent = NULL; 444 struct resource *root_res = &iomem_resource; 445 446 /* Add a node for the macio bus itself */ 447 #ifdef CONFIG_PCI 448 if (chip->lbus.pdev) { 449 parent = &chip->lbus.pdev->dev; 450 root_res = &chip->lbus.pdev->resource[0]; 451 } 452 #endif 453 pnode = of_node_get(chip->of_node); 454 if (pnode == NULL) 455 return; 456 457 /* Add macio itself to hierarchy */ 458 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res); 459 if (rdev == NULL) 460 return; 461 root_res = &rdev->resource[0]; 462 463 /* First scan 1st level */ 464 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) { 465 if (macio_skip_device(np)) 466 continue; 467 of_node_get(np); 468 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, 469 root_res); 470 if (mdev == NULL) 471 of_node_put(np); 472 else if (strncmp(np->name, "media-bay", 9) == 0) 473 mbdev = mdev; 474 else if (strncmp(np->name, "escc", 4) == 0) 475 sdev = mdev; 476 } 477 478 /* Add media bay devices if any */ 479 if (mbdev) 480 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) 481 != NULL;) { 482 if (macio_skip_device(np)) 483 continue; 484 of_node_get(np); 485 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, 486 mbdev, root_res) == NULL) 487 of_node_put(np); 488 } 489 490 /* Add serial ports if any */ 491 if (sdev) { 492 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) 493 != NULL;) { 494 if (macio_skip_device(np)) 495 continue; 496 of_node_get(np); 497 if (macio_add_one_device(chip, &sdev->ofdev.dev, np, 498 NULL, root_res) == NULL) 499 of_node_put(np); 500 } 501 } 502 } 503 504 505 /** 506 * macio_register_driver - Registers a new MacIO device driver 507 * @drv: pointer to the driver definition structure 508 */ 509 int macio_register_driver(struct macio_driver *drv) 510 { 511 /* initialize common driver fields */ 512 drv->driver.name = drv->name; 513 drv->driver.bus = &macio_bus_type; 514 515 /* register with core */ 516 return driver_register(&drv->driver); 517 } 518 519 /** 520 * macio_unregister_driver - Unregisters a new MacIO device driver 521 * @drv: pointer to the driver definition structure 522 */ 523 void macio_unregister_driver(struct macio_driver *drv) 524 { 525 driver_unregister(&drv->driver); 526 } 527 528 /** 529 * macio_request_resource - Request an MMIO resource 530 * @dev: pointer to the device holding the resource 531 * @resource_no: resource number to request 532 * @name: resource name 533 * 534 * Mark memory region number @resource_no associated with MacIO 535 * device @dev as being reserved by owner @name. Do not access 536 * any address inside the memory regions unless this call returns 537 * successfully. 538 * 539 * Returns 0 on success, or %EBUSY on error. A warning 540 * message is also printed on failure. 541 */ 542 int macio_request_resource(struct macio_dev *dev, int resource_no, 543 const char *name) 544 { 545 if (macio_resource_len(dev, resource_no) == 0) 546 return 0; 547 548 if (!request_mem_region(macio_resource_start(dev, resource_no), 549 macio_resource_len(dev, resource_no), 550 name)) 551 goto err_out; 552 553 return 0; 554 555 err_out: 556 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx" 557 " for device %s\n", 558 resource_no, 559 macio_resource_len(dev, resource_no), 560 macio_resource_start(dev, resource_no), 561 dev->ofdev.dev.bus_id); 562 return -EBUSY; 563 } 564 565 /** 566 * macio_release_resource - Release an MMIO resource 567 * @dev: pointer to the device holding the resource 568 * @resource_no: resource number to release 569 */ 570 void macio_release_resource(struct macio_dev *dev, int resource_no) 571 { 572 if (macio_resource_len(dev, resource_no) == 0) 573 return; 574 release_mem_region(macio_resource_start(dev, resource_no), 575 macio_resource_len(dev, resource_no)); 576 } 577 578 /** 579 * macio_request_resources - Reserve all memory resources 580 * @dev: MacIO device whose resources are to be reserved 581 * @name: Name to be associated with resource. 582 * 583 * Mark all memory regions associated with MacIO device @dev as 584 * being reserved by owner @name. Do not access any address inside 585 * the memory regions unless this call returns successfully. 586 * 587 * Returns 0 on success, or %EBUSY on error. A warning 588 * message is also printed on failure. 589 */ 590 int macio_request_resources(struct macio_dev *dev, const char *name) 591 { 592 int i; 593 594 for (i = 0; i < dev->n_resources; i++) 595 if (macio_request_resource(dev, i, name)) 596 goto err_out; 597 return 0; 598 599 err_out: 600 while(--i >= 0) 601 macio_release_resource(dev, i); 602 603 return -EBUSY; 604 } 605 606 /** 607 * macio_release_resources - Release reserved memory resources 608 * @dev: MacIO device whose resources were previously reserved 609 */ 610 611 void macio_release_resources(struct macio_dev *dev) 612 { 613 int i; 614 615 for (i = 0; i < dev->n_resources; i++) 616 macio_release_resource(dev, i); 617 } 618 619 620 #ifdef CONFIG_PCI 621 622 static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 623 { 624 struct device_node* np; 625 struct macio_chip* chip; 626 627 if (ent->vendor != PCI_VENDOR_ID_APPLE) 628 return -ENODEV; 629 630 /* Note regarding refcounting: We assume pci_device_to_OF_node() is 631 * ported to new OF APIs and returns a node with refcount incremented. 632 */ 633 np = pci_device_to_OF_node(pdev); 634 if (np == NULL) 635 return -ENODEV; 636 637 /* The above assumption is wrong !!! 638 * fix that here for now until I fix the arch code 639 */ 640 of_node_get(np); 641 642 /* We also assume that pmac_feature will have done a get() on nodes 643 * stored in the macio chips array 644 */ 645 chip = macio_find(np, macio_unknown); 646 of_node_put(np); 647 if (chip == NULL) 648 return -ENODEV; 649 650 /* XXX Need locking ??? */ 651 if (chip->lbus.pdev == NULL) { 652 chip->lbus.pdev = pdev; 653 chip->lbus.chip = chip; 654 pci_set_drvdata(pdev, &chip->lbus); 655 pci_set_master(pdev); 656 } 657 658 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n", 659 chip->name); 660 661 /* 662 * HACK ALERT: The WallStreet PowerBook and some OHare based machines 663 * have 2 macio ASICs. I must probe the "main" one first or IDE 664 * ordering will be incorrect. So I put on "hold" the second one since 665 * it seem to appear first on PCI 666 */ 667 if (chip->type == macio_gatwick || chip->type == macio_ohareII) 668 if (macio_chips[0].lbus.pdev == NULL) { 669 macio_on_hold = chip; 670 return 0; 671 } 672 673 macio_pci_add_devices(chip); 674 if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) { 675 macio_pci_add_devices(macio_on_hold); 676 macio_on_hold = NULL; 677 } 678 679 return 0; 680 } 681 682 static void __devexit macio_pci_remove(struct pci_dev* pdev) 683 { 684 panic("removing of macio-asic not supported !\n"); 685 } 686 687 /* 688 * MacIO is matched against any Apple ID, it's probe() function 689 * will then decide wether it applies or not 690 */ 691 static const struct pci_device_id __devinitdata pci_ids [] = { { 692 .vendor = PCI_VENDOR_ID_APPLE, 693 .device = PCI_ANY_ID, 694 .subvendor = PCI_ANY_ID, 695 .subdevice = PCI_ANY_ID, 696 697 }, { /* end: all zeroes */ } 698 }; 699 MODULE_DEVICE_TABLE (pci, pci_ids); 700 701 /* pci driver glue; this is a "new style" PCI driver module */ 702 static struct pci_driver macio_pci_driver = { 703 .name = (char *) "macio", 704 .id_table = pci_ids, 705 706 .probe = macio_pci_probe, 707 .remove = macio_pci_remove, 708 }; 709 710 #endif /* CONFIG_PCI */ 711 712 static int __init macio_module_init (void) 713 { 714 #ifdef CONFIG_PCI 715 int rc; 716 717 rc = pci_register_driver(&macio_pci_driver); 718 if (rc) 719 return rc; 720 #endif /* CONFIG_PCI */ 721 return 0; 722 } 723 724 module_init(macio_module_init); 725 726 EXPORT_SYMBOL(macio_register_driver); 727 EXPORT_SYMBOL(macio_unregister_driver); 728 EXPORT_SYMBOL(macio_dev_get); 729 EXPORT_SYMBOL(macio_dev_put); 730 EXPORT_SYMBOL(macio_request_resource); 731 EXPORT_SYMBOL(macio_release_resource); 732 EXPORT_SYMBOL(macio_request_resources); 733 EXPORT_SYMBOL(macio_release_resources); 734