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 static int macio_hotplug (struct device
*dev
, char **envp
, int num_envp
,
130 char *buffer
, int buffer_size
)
132 struct macio_dev
* macio_dev
;
133 struct of_device
* of
;
134 char *scratch
, *compat
;
142 macio_dev
= to_macio_device(dev
);
146 of
= &macio_dev
->ofdev
;
149 /* stuff we want to pass to /sbin/hotplug */
151 length
+= scnprintf (scratch
, buffer_size
- length
, "OF_NAME=%s",
153 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
159 length
+= scnprintf (scratch
, buffer_size
- length
, "OF_TYPE=%s",
161 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
166 /* Since the compatible field can contain pretty much anything
167 * it's not really legal to split it out with commas. We split it
168 * up using a number of environment variables instead. */
170 compat
= (char *) get_property(of
->node
, "compatible", &cplen
);
171 while (compat
&& cplen
> 0) {
174 length
+= scnprintf (scratch
, buffer_size
- length
,
175 "OF_COMPATIBLE_%d=%s", seen
, compat
);
176 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
180 l
= strlen (compat
) + 1;
187 length
+= scnprintf (scratch
, buffer_size
- length
,
188 "OF_COMPATIBLE_N=%d", seen
);
189 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
199 extern struct device_attribute macio_dev_attrs
[];
201 struct bus_type macio_bus_type
= {
203 .match
= macio_bus_match
,
204 .hotplug
= macio_hotplug
,
205 .suspend
= macio_device_suspend
,
206 .resume
= macio_device_resume
,
207 .dev_attrs
= macio_dev_attrs
,
210 static int __init
macio_bus_driver_init(void)
212 return bus_register(&macio_bus_type
);
215 postcore_initcall(macio_bus_driver_init
);
219 * macio_release_dev - free a macio device structure when all users of it are finished.
220 * @dev: device that's been disconnected
222 * Will be called only by the device core when all users of this macio device are
223 * done. This currently means never as we don't hot remove any macio device yet,
224 * though that will happen with mediabay based devices in a later implementation.
226 static void macio_release_dev(struct device
*dev
)
228 struct macio_dev
*mdev
;
230 mdev
= to_macio_device(dev
);
235 * macio_resource_quirks - tweak or skip some resources for a device
236 * @np: pointer to the device node
237 * @res: resulting resource
238 * @index: index of resource in node
240 * If this routine returns non-null, then the resource is completely
243 static int macio_resource_quirks(struct device_node
*np
, struct resource
*res
, int index
)
245 if (res
->flags
& IORESOURCE_MEM
) {
246 /* Grand Central has too large resource 0 on some machines */
247 if (index
== 0 && !strcmp(np
->name
, "gc")) {
248 np
->addrs
[0].size
= 0x20000;
249 res
->end
= res
->start
+ 0x1ffff;
251 /* Airport has bogus resource 2 */
252 if (index
>= 2 && !strcmp(np
->name
, "radio"))
254 /* DBDMAs may have bogus sizes */
255 if ((res
->start
& 0x0001f000) == 0x00008000) {
256 np
->addrs
[index
].size
= 0x100;
257 res
->end
= res
->start
+ 0xff;
259 /* ESCC parent eats child resources. We could have added a level of hierarchy,
260 * but I don't really feel the need for it */
261 if (!strcmp(np
->name
, "escc"))
263 /* ESCC has bogus resources >= 3 */
264 if (index
>= 3 && !(strcmp(np
->name
, "ch-a") && strcmp(np
->name
, "ch-b")))
266 /* Media bay has too many resources, keep only first one */
267 if (index
> 0 && !strcmp(np
->name
, "media-bay"))
269 /* Some older IDE resources have bogus sizes */
270 if (!(strcmp(np
->name
, "IDE") && strcmp(np
->name
, "ATA") &&
271 strcmp(np
->type
, "ide") && strcmp(np
->type
, "ata"))) {
272 if (index
== 0 && np
->addrs
[0].size
> 0x1000) {
273 np
->addrs
[0].size
= 0x1000;
274 res
->end
= res
->start
+ 0xfff;
276 if (index
== 1 && np
->addrs
[1].size
> 0x100) {
277 np
->addrs
[1].size
= 0x100;
278 res
->end
= res
->start
+ 0xff;
287 * macio_add_one_device - Add one device from OF node to the device tree
288 * @chip: pointer to the macio_chip holding the device
289 * @np: pointer to the device node in the OF tree
290 * @in_bay: set to 1 if device is part of a media-bay
292 * When media-bay is changed to hotswap drivers, this function will
293 * be exposed to the bay driver some way...
295 static struct macio_dev
* macio_add_one_device(struct macio_chip
*chip
, struct device
*parent
,
296 struct device_node
*np
, struct macio_dev
*in_bay
,
297 struct resource
*parent_res
)
299 struct macio_dev
*dev
;
306 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
309 memset(dev
, 0, sizeof(*dev
));
311 dev
->bus
= &chip
->lbus
;
312 dev
->media_bay
= in_bay
;
313 dev
->ofdev
.node
= np
;
314 dev
->ofdev
.dma_mask
= 0xffffffffUL
;
315 dev
->ofdev
.dev
.dma_mask
= &dev
->ofdev
.dma_mask
;
316 dev
->ofdev
.dev
.parent
= parent
;
317 dev
->ofdev
.dev
.bus
= &macio_bus_type
;
318 dev
->ofdev
.dev
.release
= macio_release_dev
;
321 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
322 dev
, &dev
->ofdev
, &dev
->ofdev
.dev
, &dev
->ofdev
.dev
.kobj
);
325 /* MacIO itself has a different reg, we use it's PCI base */
326 if (np
== chip
->of_node
) {
327 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08lx:%.*s", chip
->lbus
.index
,
329 pci_resource_start(chip
->lbus
.pdev
, 0),
331 0, /* NuBus may want to do something better here */
333 MAX_NODE_NAME_SIZE
, np
->name
);
335 reg
= (u32
*)get_property(np
, "reg", NULL
);
336 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08x:%.*s", chip
->lbus
.index
,
337 reg
? *reg
: 0, MAX_NODE_NAME_SIZE
, np
->name
);
340 /* For now, we use pre-parsed entries in the device-tree for
341 * interrupt routing and addresses, but we should change that
342 * to dynamically parsed entries and so get rid of most of the
343 * clutter in struct device_node
345 for (i
= j
= 0; i
< np
->n_intrs
; i
++) {
346 struct resource
*res
= &dev
->interrupt
[j
];
348 if (j
>= MACIO_DEV_COUNT_IRQS
)
350 res
->start
= np
->intrs
[i
].line
;
351 res
->flags
= IORESOURCE_IO
;
352 if (np
->intrs
[j
].sense
)
353 res
->flags
|= IORESOURCE_IRQ_LOWLEVEL
;
355 res
->flags
|= IORESOURCE_IRQ_HIGHEDGE
;
356 res
->name
= dev
->ofdev
.dev
.bus_id
;
357 if (macio_resource_quirks(np
, res
, i
))
358 memset(res
, 0, sizeof(struct resource
));
362 dev
->n_interrupts
= j
;
363 for (i
= j
= 0; i
< np
->n_addrs
; i
++) {
364 struct resource
*res
= &dev
->resource
[j
];
366 if (j
>= MACIO_DEV_COUNT_RESOURCES
)
368 res
->start
= np
->addrs
[i
].address
;
369 res
->end
= np
->addrs
[i
].address
+ np
->addrs
[i
].size
- 1;
370 res
->flags
= IORESOURCE_MEM
;
371 res
->name
= dev
->ofdev
.dev
.bus_id
;
372 if (macio_resource_quirks(np
, res
, i
))
373 memset(res
, 0, sizeof(struct resource
));
376 /* Currently, we consider failure as harmless, this may
377 * change in the future, once I've found all the device
378 * tree bugs in older machines & worked around them
380 if (insert_resource(parent_res
, res
))
381 printk(KERN_WARNING
"Can't request resource %d for MacIO"
382 " device %s\n", i
, dev
->ofdev
.dev
.bus_id
);
385 dev
->n_resources
= j
;
387 if (of_device_register(&dev
->ofdev
) != 0) {
388 printk(KERN_DEBUG
"macio: device registration error for %s!\n",
389 dev
->ofdev
.dev
.bus_id
);
397 static int macio_skip_device(struct device_node
*np
)
399 if (strncmp(np
->name
, "battery", 7) == 0)
401 if (strncmp(np
->name
, "escc-legacy", 11) == 0)
407 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
408 * @chip: pointer to the macio_chip holding the devices
410 * This function will do the job of extracting devices from the
411 * Open Firmware device tree, build macio_dev structures and add
412 * them to the Linux device tree.
414 * For now, childs of media-bay are added now as well. This will
417 static void macio_pci_add_devices(struct macio_chip
*chip
)
419 struct device_node
*np
, *pnode
;
420 struct macio_dev
*rdev
, *mdev
, *mbdev
= NULL
, *sdev
= NULL
;
421 struct device
*parent
= NULL
;
422 struct resource
*root_res
= &iomem_resource
;
424 /* Add a node for the macio bus itself */
426 if (chip
->lbus
.pdev
) {
427 parent
= &chip
->lbus
.pdev
->dev
;
428 root_res
= &chip
->lbus
.pdev
->resource
[0];
431 pnode
= of_node_get(chip
->of_node
);
435 /* Add macio itself to hierarchy */
436 rdev
= macio_add_one_device(chip
, parent
, pnode
, NULL
, root_res
);
439 root_res
= &rdev
->resource
[0];
441 /* First scan 1st level */
442 for (np
= NULL
; (np
= of_get_next_child(pnode
, np
)) != NULL
;) {
443 if (!macio_skip_device(np
)) {
445 mdev
= macio_add_one_device(chip
, &rdev
->ofdev
.dev
, np
, NULL
, root_res
);
448 else if (strncmp(np
->name
, "media-bay", 9) == 0)
450 else if (strncmp(np
->name
, "escc", 4) == 0)
455 /* Add media bay devices if any */
457 for (np
= NULL
; (np
= of_get_next_child(mbdev
->ofdev
.node
, np
)) != NULL
;)
458 if (!macio_skip_device(np
)) {
460 if (macio_add_one_device(chip
, &mbdev
->ofdev
.dev
, np
, mbdev
,
464 /* Add serial ports if any */
466 for (np
= NULL
; (np
= of_get_next_child(sdev
->ofdev
.node
, np
)) != NULL
;)
467 if (!macio_skip_device(np
)) {
469 if (macio_add_one_device(chip
, &sdev
->ofdev
.dev
, np
, NULL
,
478 * macio_register_driver - Registers a new MacIO device driver
479 * @drv: pointer to the driver definition structure
481 int macio_register_driver(struct macio_driver
*drv
)
485 /* initialize common driver fields */
486 drv
->driver
.name
= drv
->name
;
487 drv
->driver
.bus
= &macio_bus_type
;
488 drv
->driver
.probe
= macio_device_probe
;
489 drv
->driver
.remove
= macio_device_remove
;
490 drv
->driver
.shutdown
= macio_device_shutdown
;
492 /* register with core */
493 count
= driver_register(&drv
->driver
);
494 return count
? count
: 1;
498 * macio_unregister_driver - Unregisters a new MacIO device driver
499 * @drv: pointer to the driver definition structure
501 void macio_unregister_driver(struct macio_driver
*drv
)
503 driver_unregister(&drv
->driver
);
507 * macio_request_resource - Request an MMIO resource
508 * @dev: pointer to the device holding the resource
509 * @resource_no: resource number to request
510 * @name: resource name
512 * Mark memory region number @resource_no associated with MacIO
513 * device @dev as being reserved by owner @name. Do not access
514 * any address inside the memory regions unless this call returns
517 * Returns 0 on success, or %EBUSY on error. A warning
518 * message is also printed on failure.
520 int macio_request_resource(struct macio_dev
*dev
, int resource_no
, const char *name
)
522 if (macio_resource_len(dev
, resource_no
) == 0)
525 if (!request_mem_region(macio_resource_start(dev
, resource_no
),
526 macio_resource_len(dev
, resource_no
),
533 printk (KERN_WARNING
"MacIO: Unable to reserve resource #%d:%lx@%lx"
536 macio_resource_len(dev
, resource_no
),
537 macio_resource_start(dev
, resource_no
),
538 dev
->ofdev
.dev
.bus_id
);
543 * macio_release_resource - Release an MMIO resource
544 * @dev: pointer to the device holding the resource
545 * @resource_no: resource number to release
547 void macio_release_resource(struct macio_dev
*dev
, int resource_no
)
549 if (macio_resource_len(dev
, resource_no
) == 0)
551 release_mem_region(macio_resource_start(dev
, resource_no
),
552 macio_resource_len(dev
, resource_no
));
556 * macio_request_resources - Reserve all memory resources
557 * @dev: MacIO device whose resources are to be reserved
558 * @name: Name to be associated with resource.
560 * Mark all memory regions associated with MacIO device @dev as
561 * being reserved by owner @name. Do not access any address inside
562 * the memory regions unless this call returns successfully.
564 * Returns 0 on success, or %EBUSY on error. A warning
565 * message is also printed on failure.
567 int macio_request_resources(struct macio_dev
*dev
, const char *name
)
571 for (i
= 0; i
< dev
->n_resources
; i
++)
572 if (macio_request_resource(dev
, i
, name
))
578 macio_release_resource(dev
, i
);
584 * macio_release_resources - Release reserved memory resources
585 * @dev: MacIO device whose resources were previously reserved
588 void macio_release_resources(struct macio_dev
*dev
)
592 for (i
= 0; i
< dev
->n_resources
; i
++)
593 macio_release_resource(dev
, i
);
599 static int __devinit
macio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
601 struct device_node
* np
;
602 struct macio_chip
* chip
;
604 if (ent
->vendor
!= PCI_VENDOR_ID_APPLE
)
607 /* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
608 * to new OF APIs and returns a node with refcount incremented. This isn't
609 * the case today, but on the other hand ppc32 doesn't do refcounting. This
610 * will have to be fixed when going to ppc64. --BenH.
612 np
= pci_device_to_OF_node(pdev
);
616 /* This assumption is wrong, fix that here for now until I fix the arch */
619 /* We also assume that pmac_feature will have done a get() on nodes stored
620 * in the macio chips array
622 chip
= macio_find(np
, macio_unknown
);
627 /* XXX Need locking ??? */
628 if (chip
->lbus
.pdev
== NULL
) {
629 chip
->lbus
.pdev
= pdev
;
630 chip
->lbus
.chip
= chip
;
631 pci_set_drvdata(pdev
, &chip
->lbus
);
632 pci_set_master(pdev
);
635 printk(KERN_INFO
"MacIO PCI driver attached to %s chipset\n",
639 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
640 * have 2 macio ASICs. I must probe the "main" one first or IDE ordering
641 * will be incorrect. So I put on "hold" the second one since it seem to
642 * appear first on PCI
644 if (chip
->type
== macio_gatwick
|| chip
->type
== macio_ohareII
)
645 if (macio_chips
[0].lbus
.pdev
== NULL
) {
646 macio_on_hold
= chip
;
650 macio_pci_add_devices(chip
);
651 if (macio_on_hold
&& macio_chips
[0].lbus
.pdev
!= NULL
) {
652 macio_pci_add_devices(macio_on_hold
);
653 macio_on_hold
= NULL
;
659 static void __devexit
macio_pci_remove(struct pci_dev
* pdev
)
661 panic("removing of macio-asic not supported !\n");
665 * MacIO is matched against any Apple ID, it's probe() function
666 * will then decide wether it applies or not
668 static const struct pci_device_id __devinitdata pci_ids
[] = { {
669 .vendor
= PCI_VENDOR_ID_APPLE
,
670 .device
= PCI_ANY_ID
,
671 .subvendor
= PCI_ANY_ID
,
672 .subdevice
= PCI_ANY_ID
,
674 }, { /* end: all zeroes */ }
676 MODULE_DEVICE_TABLE (pci
, pci_ids
);
678 /* pci driver glue; this is a "new style" PCI driver module */
679 static struct pci_driver macio_pci_driver
= {
680 .name
= (char *) "macio",
683 .probe
= macio_pci_probe
,
684 .remove
= macio_pci_remove
,
687 #endif /* CONFIG_PCI */
689 static int __init
macio_module_init (void)
694 rc
= pci_register_driver(&macio_pci_driver
);
697 #endif /* CONFIG_PCI */
701 module_init(macio_module_init
);
703 EXPORT_SYMBOL(macio_register_driver
);
704 EXPORT_SYMBOL(macio_unregister_driver
);
705 EXPORT_SYMBOL(macio_dev_get
);
706 EXPORT_SYMBOL(macio_dev_put
);
707 EXPORT_SYMBOL(macio_request_resource
);
708 EXPORT_SYMBOL(macio_release_resource
);
709 EXPORT_SYMBOL(macio_request_resources
);
710 EXPORT_SYMBOL(macio_release_resources
);