2 * Bus & driver management routines for devices within
3 * a MacIO ASIC. Interface to new driver model mostly
4 * stolen from the PCI version.
8 * - Don't probe below media bay by default, but instead provide
9 * some hooks for media bay to dynamically add/remove it's own
13 #include <linux/config.h>
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/pci_ids.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <asm/machdep.h>
21 #include <asm/macio.h>
22 #include <asm/pmac_feature.h>
24 #include <asm/pci-bridge.h>
28 #define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
30 static struct macio_chip *macio_on_hold;
32 static int macio_bus_match(struct device *dev, struct device_driver *drv)
34 struct macio_dev * macio_dev = to_macio_device(dev);
35 struct macio_driver * macio_drv = to_macio_driver(drv);
36 const struct of_device_id * matches = macio_drv->match_table;
41 return of_match_device(matches, &macio_dev->ofdev) != NULL;
44 struct macio_dev *macio_dev_get(struct macio_dev *dev)
50 tmp = get_device(&dev->ofdev.dev);
52 return to_macio_device(tmp);
57 void macio_dev_put(struct macio_dev *dev)
60 put_device(&dev->ofdev.dev);
64 static int macio_device_probe(struct device *dev)
67 struct macio_driver *drv;
68 struct macio_dev *macio_dev;
69 const struct of_device_id *match;
71 drv = to_macio_driver(dev->driver);
72 macio_dev = to_macio_device(dev);
77 macio_dev_get(macio_dev);
79 match = of_match_device(drv->match_table, &macio_dev->ofdev);
81 error = drv->probe(macio_dev, match);
83 macio_dev_put(macio_dev);
88 static int macio_device_remove(struct device *dev)
90 struct macio_dev * macio_dev = to_macio_device(dev);
91 struct macio_driver * drv = to_macio_driver(dev->driver);
93 if (dev->driver && drv->remove)
94 drv->remove(macio_dev);
95 macio_dev_put(macio_dev);
100 static void macio_device_shutdown(struct device *dev)
102 struct macio_dev * macio_dev = to_macio_device(dev);
103 struct macio_driver * drv = to_macio_driver(dev->driver);
105 if (dev->driver && drv->shutdown)
106 drv->shutdown(macio_dev);
109 static int macio_device_suspend(struct device *dev, pm_message_t state)
111 struct macio_dev * macio_dev = to_macio_device(dev);
112 struct macio_driver * drv = to_macio_driver(dev->driver);
114 if (dev->driver && drv->suspend)
115 return drv->suspend(macio_dev, state);
119 static int macio_device_resume(struct device * dev)
121 struct macio_dev * macio_dev = to_macio_device(dev);
122 struct macio_driver * drv = to_macio_driver(dev->driver);
124 if (dev->driver && drv->resume)
125 return drv->resume(macio_dev);
129 extern struct device_attribute macio_dev_attrs[];
131 struct bus_type macio_bus_type = {
133 .match = macio_bus_match,
134 .suspend = macio_device_suspend,
135 .resume = macio_device_resume,
136 .dev_attrs = macio_dev_attrs,
139 static int __init macio_bus_driver_init(void)
141 return bus_register(&macio_bus_type);
144 postcore_initcall(macio_bus_driver_init);
148 * macio_release_dev - free a macio device structure when all users of it are finished.
149 * @dev: device that's been disconnected
151 * Will be called only by the device core when all users of this macio device are
152 * done. This currently means never as we don't hot remove any macio device yet,
153 * though that will happen with mediabay based devices in a later implementation.
155 static void macio_release_dev(struct device *dev)
157 struct macio_dev *mdev;
159 mdev = to_macio_device(dev);
164 * macio_resource_quirks - tweak or skip some resources for a device
165 * @np: pointer to the device node
166 * @res: resulting resource
167 * @index: index of resource in node
169 * If this routine returns non-null, then the resource is completely
172 static int macio_resource_quirks(struct device_node *np, struct resource *res, int index)
174 if (res->flags & IORESOURCE_MEM) {
175 /* Grand Central has too large resource 0 on some machines */
176 if (index == 0 && !strcmp(np->name, "gc")) {
177 np->addrs[0].size = 0x20000;
178 res->end = res->start + 0x1ffff;
180 /* Airport has bogus resource 2 */
181 if (index >= 2 && !strcmp(np->name, "radio"))
183 /* DBDMAs may have bogus sizes */
184 if ((res->start & 0x0001f000) == 0x00008000) {
185 np->addrs[index].size = 0x100;
186 res->end = res->start + 0xff;
188 /* ESCC parent eats child resources. We could have added a level of hierarchy,
189 * but I don't really feel the need for it */
190 if (!strcmp(np->name, "escc"))
192 /* ESCC has bogus resources >= 3 */
193 if (index >= 3 && !(strcmp(np->name, "ch-a") && strcmp(np->name, "ch-b")))
195 /* Media bay has too many resources, keep only first one */
196 if (index > 0 && !strcmp(np->name, "media-bay"))
198 /* Some older IDE resources have bogus sizes */
199 if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&
200 strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {
201 if (index == 0 && np->addrs[0].size > 0x1000) {
202 np->addrs[0].size = 0x1000;
203 res->end = res->start + 0xfff;
205 if (index == 1 && np->addrs[1].size > 0x100) {
206 np->addrs[1].size = 0x100;
207 res->end = res->start + 0xff;
216 * macio_add_one_device - Add one device from OF node to the device tree
217 * @chip: pointer to the macio_chip holding the device
218 * @np: pointer to the device node in the OF tree
219 * @in_bay: set to 1 if device is part of a media-bay
221 * When media-bay is changed to hotswap drivers, this function will
222 * be exposed to the bay driver some way...
224 static struct macio_dev * macio_add_one_device(struct macio_chip *chip, struct device *parent,
225 struct device_node *np, struct macio_dev *in_bay,
226 struct resource *parent_res)
228 struct macio_dev *dev;
235 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
238 memset(dev, 0, sizeof(*dev));
240 dev->bus = &chip->lbus;
241 dev->media_bay = in_bay;
242 dev->ofdev.node = np;
243 dev->ofdev.dma_mask = 0xffffffffUL;
244 dev->ofdev.dev.dma_mask = &dev->ofdev.dma_mask;
245 dev->ofdev.dev.parent = parent;
246 dev->ofdev.dev.bus = &macio_bus_type;
247 dev->ofdev.dev.release = macio_release_dev;
250 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
251 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
254 /* MacIO itself has a different reg, we use it's PCI base */
255 if (np == chip->of_node) {
256 sprintf(dev->ofdev.dev.bus_id, "%1d.%08lx:%.*s", chip->lbus.index,
258 pci_resource_start(chip->lbus.pdev, 0),
260 0, /* NuBus may want to do something better here */
262 MAX_NODE_NAME_SIZE, np->name);
264 reg = (u32 *)get_property(np, "reg", NULL);
265 sprintf(dev->ofdev.dev.bus_id, "%1d.%08x:%.*s", chip->lbus.index,
266 reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name);
269 /* For now, we use pre-parsed entries in the device-tree for
270 * interrupt routing and addresses, but we should change that
271 * to dynamically parsed entries and so get rid of most of the
272 * clutter in struct device_node
274 for (i = j = 0; i < np->n_intrs; i++) {
275 struct resource *res = &dev->interrupt[j];
277 if (j >= MACIO_DEV_COUNT_IRQS)
279 res->start = np->intrs[i].line;
280 res->flags = IORESOURCE_IO;
281 if (np->intrs[j].sense)
282 res->flags |= IORESOURCE_IRQ_LOWLEVEL;
284 res->flags |= IORESOURCE_IRQ_HIGHEDGE;
285 res->name = dev->ofdev.dev.bus_id;
286 if (macio_resource_quirks(np, res, i))
287 memset(res, 0, sizeof(struct resource));
291 dev->n_interrupts = j;
292 for (i = j = 0; i < np->n_addrs; i++) {
293 struct resource *res = &dev->resource[j];
295 if (j >= MACIO_DEV_COUNT_RESOURCES)
297 res->start = np->addrs[i].address;
298 res->end = np->addrs[i].address + np->addrs[i].size - 1;
299 res->flags = IORESOURCE_MEM;
300 res->name = dev->ofdev.dev.bus_id;
301 if (macio_resource_quirks(np, res, i))
302 memset(res, 0, sizeof(struct resource));
305 /* Currently, we consider failure as harmless, this may
306 * change in the future, once I've found all the device
307 * tree bugs in older machines & worked around them
309 if (insert_resource(parent_res, res))
310 printk(KERN_WARNING "Can't request resource %d for MacIO"
311 " device %s\n", i, dev->ofdev.dev.bus_id);
314 dev->n_resources = j;
316 if (of_device_register(&dev->ofdev) != 0) {
317 printk(KERN_DEBUG"macio: device registration error for %s!\n",
318 dev->ofdev.dev.bus_id);
326 static int macio_skip_device(struct device_node *np)
328 if (strncmp(np->name, "battery", 7) == 0)
330 if (strncmp(np->name, "escc-legacy", 11) == 0)
336 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
337 * @chip: pointer to the macio_chip holding the devices
339 * This function will do the job of extracting devices from the
340 * Open Firmware device tree, build macio_dev structures and add
341 * them to the Linux device tree.
343 * For now, childs of media-bay are added now as well. This will
346 static void macio_pci_add_devices(struct macio_chip *chip)
348 struct device_node *np, *pnode;
349 struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
350 struct device *parent = NULL;
351 struct resource *root_res = &iomem_resource;
353 /* Add a node for the macio bus itself */
355 if (chip->lbus.pdev) {
356 parent = &chip->lbus.pdev->dev;
357 root_res = &chip->lbus.pdev->resource[0];
360 pnode = of_node_get(chip->of_node);
364 /* Add macio itself to hierarchy */
365 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
368 root_res = &rdev->resource[0];
370 /* First scan 1st level */
371 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
372 if (!macio_skip_device(np)) {
374 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, root_res);
377 else if (strncmp(np->name, "media-bay", 9) == 0)
379 else if (strncmp(np->name, "escc", 4) == 0)
384 /* Add media bay devices if any */
386 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
387 if (!macio_skip_device(np)) {
389 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev,
393 /* Add serial ports if any */
395 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
396 if (!macio_skip_device(np)) {
398 if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL,
407 * macio_register_driver - Registers a new MacIO device driver
408 * @drv: pointer to the driver definition structure
410 int macio_register_driver(struct macio_driver *drv)
414 /* initialize common driver fields */
415 drv->driver.name = drv->name;
416 drv->driver.bus = &macio_bus_type;
417 drv->driver.probe = macio_device_probe;
418 drv->driver.remove = macio_device_remove;
419 drv->driver.shutdown = macio_device_shutdown;
421 /* register with core */
422 count = driver_register(&drv->driver);
423 return count ? count : 1;
427 * macio_unregister_driver - Unregisters a new MacIO device driver
428 * @drv: pointer to the driver definition structure
430 void macio_unregister_driver(struct macio_driver *drv)
432 driver_unregister(&drv->driver);
436 * macio_request_resource - Request an MMIO resource
437 * @dev: pointer to the device holding the resource
438 * @resource_no: resource number to request
439 * @name: resource name
441 * Mark memory region number @resource_no associated with MacIO
442 * device @dev as being reserved by owner @name. Do not access
443 * any address inside the memory regions unless this call returns
446 * Returns 0 on success, or %EBUSY on error. A warning
447 * message is also printed on failure.
449 int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name)
451 if (macio_resource_len(dev, resource_no) == 0)
454 if (!request_mem_region(macio_resource_start(dev, resource_no),
455 macio_resource_len(dev, resource_no),
462 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
465 macio_resource_len(dev, resource_no),
466 macio_resource_start(dev, resource_no),
467 dev->ofdev.dev.bus_id);
472 * macio_release_resource - Release an MMIO resource
473 * @dev: pointer to the device holding the resource
474 * @resource_no: resource number to release
476 void macio_release_resource(struct macio_dev *dev, int resource_no)
478 if (macio_resource_len(dev, resource_no) == 0)
480 release_mem_region(macio_resource_start(dev, resource_no),
481 macio_resource_len(dev, resource_no));
485 * macio_request_resources - Reserve all memory resources
486 * @dev: MacIO device whose resources are to be reserved
487 * @name: Name to be associated with resource.
489 * Mark all memory regions associated with MacIO device @dev as
490 * being reserved by owner @name. Do not access any address inside
491 * the memory regions unless this call returns successfully.
493 * Returns 0 on success, or %EBUSY on error. A warning
494 * message is also printed on failure.
496 int macio_request_resources(struct macio_dev *dev, const char *name)
500 for (i = 0; i < dev->n_resources; i++)
501 if (macio_request_resource(dev, i, name))
507 macio_release_resource(dev, i);
513 * macio_release_resources - Release reserved memory resources
514 * @dev: MacIO device whose resources were previously reserved
517 void macio_release_resources(struct macio_dev *dev)
521 for (i = 0; i < dev->n_resources; i++)
522 macio_release_resource(dev, i);
528 static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
530 struct device_node* np;
531 struct macio_chip* chip;
533 if (ent->vendor != PCI_VENDOR_ID_APPLE)
536 /* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
537 * to new OF APIs and returns a node with refcount incremented. This isn't
538 * the case today, but on the other hand ppc32 doesn't do refcounting. This
539 * will have to be fixed when going to ppc64. --BenH.
541 np = pci_device_to_OF_node(pdev);
545 /* This assumption is wrong, fix that here for now until I fix the arch */
548 /* We also assume that pmac_feature will have done a get() on nodes stored
549 * in the macio chips array
551 chip = macio_find(np, macio_unknown);
556 /* XXX Need locking ??? */
557 if (chip->lbus.pdev == NULL) {
558 chip->lbus.pdev = pdev;
559 chip->lbus.chip = chip;
560 pci_set_drvdata(pdev, &chip->lbus);
561 pci_set_master(pdev);
564 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
568 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
569 * have 2 macio ASICs. I must probe the "main" one first or IDE ordering
570 * will be incorrect. So I put on "hold" the second one since it seem to
571 * appear first on PCI
573 if (chip->type == macio_gatwick || chip->type == macio_ohareII)
574 if (macio_chips[0].lbus.pdev == NULL) {
575 macio_on_hold = chip;
579 macio_pci_add_devices(chip);
580 if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
581 macio_pci_add_devices(macio_on_hold);
582 macio_on_hold = NULL;
588 static void __devexit macio_pci_remove(struct pci_dev* pdev)
590 panic("removing of macio-asic not supported !\n");
594 * MacIO is matched against any Apple ID, it's probe() function
595 * will then decide wether it applies or not
597 static const struct pci_device_id __devinitdata pci_ids [] = { {
598 .vendor = PCI_VENDOR_ID_APPLE,
599 .device = PCI_ANY_ID,
600 .subvendor = PCI_ANY_ID,
601 .subdevice = PCI_ANY_ID,
603 }, { /* end: all zeroes */ }
605 MODULE_DEVICE_TABLE (pci, pci_ids);
607 /* pci driver glue; this is a "new style" PCI driver module */
608 static struct pci_driver macio_pci_driver = {
609 .name = (char *) "macio",
612 .probe = macio_pci_probe,
613 .remove = macio_pci_remove,
616 #endif /* CONFIG_PCI */
618 static int __init macio_module_init (void)
623 rc = pci_register_driver(&macio_pci_driver);
626 #endif /* CONFIG_PCI */
630 module_init(macio_module_init);
632 EXPORT_SYMBOL(macio_register_driver);
633 EXPORT_SYMBOL(macio_unregister_driver);
634 EXPORT_SYMBOL(macio_dev_get);
635 EXPORT_SYMBOL(macio_dev_put);
636 EXPORT_SYMBOL(macio_request_resource);
637 EXPORT_SYMBOL(macio_release_resource);
638 EXPORT_SYMBOL(macio_request_resources);
639 EXPORT_SYMBOL(macio_release_resources);