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_match
* 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_match
*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 struct bus_type macio_bus_type
= {
131 .match
= macio_bus_match
,
132 .suspend
= macio_device_suspend
,
133 .resume
= macio_device_resume
,
136 static int __init
macio_bus_driver_init(void)
138 return bus_register(&macio_bus_type
);
141 postcore_initcall(macio_bus_driver_init
);
145 * macio_release_dev - free a macio device structure when all users of it are finished.
146 * @dev: device that's been disconnected
148 * Will be called only by the device core when all users of this macio device are
149 * done. This currently means never as we don't hot remove any macio device yet,
150 * though that will happen with mediabay based devices in a later implementation.
152 static void macio_release_dev(struct device
*dev
)
154 struct macio_dev
*mdev
;
156 mdev
= to_macio_device(dev
);
161 * macio_resource_quirks - tweak or skip some resources for a device
162 * @np: pointer to the device node
163 * @res: resulting resource
164 * @index: index of resource in node
166 * If this routine returns non-null, then the resource is completely
169 static int macio_resource_quirks(struct device_node
*np
, struct resource
*res
, int index
)
171 if (res
->flags
& IORESOURCE_MEM
) {
172 /* Grand Central has too large resource 0 on some machines */
173 if (index
== 0 && !strcmp(np
->name
, "gc")) {
174 np
->addrs
[0].size
= 0x20000;
175 res
->end
= res
->start
+ 0x1ffff;
177 /* Airport has bogus resource 2 */
178 if (index
>= 2 && !strcmp(np
->name
, "radio"))
180 /* DBDMAs may have bogus sizes */
181 if ((res
->start
& 0x0001f000) == 0x00008000) {
182 np
->addrs
[index
].size
= 0x100;
183 res
->end
= res
->start
+ 0xff;
185 /* ESCC parent eats child resources. We could have added a level of hierarchy,
186 * but I don't really feel the need for it */
187 if (!strcmp(np
->name
, "escc"))
189 /* ESCC has bogus resources >= 3 */
190 if (index
>= 3 && !(strcmp(np
->name
, "ch-a") && strcmp(np
->name
, "ch-b")))
192 /* Media bay has too many resources, keep only first one */
193 if (index
> 0 && !strcmp(np
->name
, "media-bay"))
195 /* Some older IDE resources have bogus sizes */
196 if (!(strcmp(np
->name
, "IDE") && strcmp(np
->name
, "ATA") &&
197 strcmp(np
->type
, "ide") && strcmp(np
->type
, "ata"))) {
198 if (index
== 0 && np
->addrs
[0].size
> 0x1000) {
199 np
->addrs
[0].size
= 0x1000;
200 res
->end
= res
->start
+ 0xfff;
202 if (index
== 1 && np
->addrs
[1].size
> 0x100) {
203 np
->addrs
[1].size
= 0x100;
204 res
->end
= res
->start
+ 0xff;
213 * macio_add_one_device - Add one device from OF node to the device tree
214 * @chip: pointer to the macio_chip holding the device
215 * @np: pointer to the device node in the OF tree
216 * @in_bay: set to 1 if device is part of a media-bay
218 * When media-bay is changed to hotswap drivers, this function will
219 * be exposed to the bay driver some way...
221 static struct macio_dev
* macio_add_one_device(struct macio_chip
*chip
, struct device
*parent
,
222 struct device_node
*np
, struct macio_dev
*in_bay
,
223 struct resource
*parent_res
)
225 struct macio_dev
*dev
;
232 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
235 memset(dev
, 0, sizeof(*dev
));
237 dev
->bus
= &chip
->lbus
;
238 dev
->media_bay
= in_bay
;
239 dev
->ofdev
.node
= np
;
240 dev
->ofdev
.dma_mask
= 0xffffffffUL
;
241 dev
->ofdev
.dev
.dma_mask
= &dev
->ofdev
.dma_mask
;
242 dev
->ofdev
.dev
.parent
= parent
;
243 dev
->ofdev
.dev
.bus
= &macio_bus_type
;
244 dev
->ofdev
.dev
.release
= macio_release_dev
;
247 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
248 dev
, &dev
->ofdev
, &dev
->ofdev
.dev
, &dev
->ofdev
.dev
.kobj
);
251 /* MacIO itself has a different reg, we use it's PCI base */
252 if (np
== chip
->of_node
) {
253 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08lx:%.*s", chip
->lbus
.index
,
255 pci_resource_start(chip
->lbus
.pdev
, 0),
257 0, /* NuBus may want to do something better here */
259 MAX_NODE_NAME_SIZE
, np
->name
);
261 reg
= (u32
*)get_property(np
, "reg", NULL
);
262 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08x:%.*s", chip
->lbus
.index
,
263 reg
? *reg
: 0, MAX_NODE_NAME_SIZE
, np
->name
);
266 /* For now, we use pre-parsed entries in the device-tree for
267 * interrupt routing and addresses, but we should change that
268 * to dynamically parsed entries and so get rid of most of the
269 * clutter in struct device_node
271 for (i
= j
= 0; i
< np
->n_intrs
; i
++) {
272 struct resource
*res
= &dev
->interrupt
[j
];
274 if (j
>= MACIO_DEV_COUNT_IRQS
)
276 res
->start
= np
->intrs
[i
].line
;
277 res
->flags
= IORESOURCE_IO
;
278 if (np
->intrs
[j
].sense
)
279 res
->flags
|= IORESOURCE_IRQ_LOWLEVEL
;
281 res
->flags
|= IORESOURCE_IRQ_HIGHEDGE
;
282 res
->name
= dev
->ofdev
.dev
.bus_id
;
283 if (macio_resource_quirks(np
, res
, i
))
284 memset(res
, 0, sizeof(struct resource
));
288 dev
->n_interrupts
= j
;
289 for (i
= j
= 0; i
< np
->n_addrs
; i
++) {
290 struct resource
*res
= &dev
->resource
[j
];
292 if (j
>= MACIO_DEV_COUNT_RESOURCES
)
294 res
->start
= np
->addrs
[i
].address
;
295 res
->end
= np
->addrs
[i
].address
+ np
->addrs
[i
].size
- 1;
296 res
->flags
= IORESOURCE_MEM
;
297 res
->name
= dev
->ofdev
.dev
.bus_id
;
298 if (macio_resource_quirks(np
, res
, i
))
299 memset(res
, 0, sizeof(struct resource
));
302 /* Currently, we consider failure as harmless, this may
303 * change in the future, once I've found all the device
304 * tree bugs in older machines & worked around them
306 if (insert_resource(parent_res
, res
))
307 printk(KERN_WARNING
"Can't request resource %d for MacIO"
308 " device %s\n", i
, dev
->ofdev
.dev
.bus_id
);
311 dev
->n_resources
= j
;
313 if (of_device_register(&dev
->ofdev
) != 0) {
314 printk(KERN_DEBUG
"macio: device registration error for %s!\n",
315 dev
->ofdev
.dev
.bus_id
);
323 static int macio_skip_device(struct device_node
*np
)
325 if (strncmp(np
->name
, "battery", 7) == 0)
327 if (strncmp(np
->name
, "escc-legacy", 11) == 0)
333 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
334 * @chip: pointer to the macio_chip holding the devices
336 * This function will do the job of extracting devices from the
337 * Open Firmware device tree, build macio_dev structures and add
338 * them to the Linux device tree.
340 * For now, childs of media-bay are added now as well. This will
343 static void macio_pci_add_devices(struct macio_chip
*chip
)
345 struct device_node
*np
, *pnode
;
346 struct macio_dev
*rdev
, *mdev
, *mbdev
= NULL
, *sdev
= NULL
;
347 struct device
*parent
= NULL
;
348 struct resource
*root_res
= &iomem_resource
;
350 /* Add a node for the macio bus itself */
352 if (chip
->lbus
.pdev
) {
353 parent
= &chip
->lbus
.pdev
->dev
;
354 root_res
= &chip
->lbus
.pdev
->resource
[0];
357 pnode
= of_node_get(chip
->of_node
);
361 /* Add macio itself to hierarchy */
362 rdev
= macio_add_one_device(chip
, parent
, pnode
, NULL
, root_res
);
365 root_res
= &rdev
->resource
[0];
367 /* First scan 1st level */
368 for (np
= NULL
; (np
= of_get_next_child(pnode
, np
)) != NULL
;) {
369 if (!macio_skip_device(np
)) {
371 mdev
= macio_add_one_device(chip
, &rdev
->ofdev
.dev
, np
, NULL
, root_res
);
374 else if (strncmp(np
->name
, "media-bay", 9) == 0)
376 else if (strncmp(np
->name
, "escc", 4) == 0)
381 /* Add media bay devices if any */
383 for (np
= NULL
; (np
= of_get_next_child(mbdev
->ofdev
.node
, np
)) != NULL
;)
384 if (!macio_skip_device(np
)) {
386 if (macio_add_one_device(chip
, &mbdev
->ofdev
.dev
, np
, mbdev
,
390 /* Add serial ports if any */
392 for (np
= NULL
; (np
= of_get_next_child(sdev
->ofdev
.node
, np
)) != NULL
;)
393 if (!macio_skip_device(np
)) {
395 if (macio_add_one_device(chip
, &sdev
->ofdev
.dev
, np
, NULL
,
404 * macio_register_driver - Registers a new MacIO device driver
405 * @drv: pointer to the driver definition structure
407 int macio_register_driver(struct macio_driver
*drv
)
411 /* initialize common driver fields */
412 drv
->driver
.name
= drv
->name
;
413 drv
->driver
.bus
= &macio_bus_type
;
414 drv
->driver
.probe
= macio_device_probe
;
415 drv
->driver
.remove
= macio_device_remove
;
416 drv
->driver
.shutdown
= macio_device_shutdown
;
418 /* register with core */
419 count
= driver_register(&drv
->driver
);
420 return count
? count
: 1;
424 * macio_unregister_driver - Unregisters a new MacIO device driver
425 * @drv: pointer to the driver definition structure
427 void macio_unregister_driver(struct macio_driver
*drv
)
429 driver_unregister(&drv
->driver
);
433 * macio_request_resource - Request an MMIO resource
434 * @dev: pointer to the device holding the resource
435 * @resource_no: resource number to request
436 * @name: resource name
438 * Mark memory region number @resource_no associated with MacIO
439 * device @dev as being reserved by owner @name. Do not access
440 * any address inside the memory regions unless this call returns
443 * Returns 0 on success, or %EBUSY on error. A warning
444 * message is also printed on failure.
446 int macio_request_resource(struct macio_dev
*dev
, int resource_no
, const char *name
)
448 if (macio_resource_len(dev
, resource_no
) == 0)
451 if (!request_mem_region(macio_resource_start(dev
, resource_no
),
452 macio_resource_len(dev
, resource_no
),
459 printk (KERN_WARNING
"MacIO: Unable to reserve resource #%d:%lx@%lx"
462 macio_resource_len(dev
, resource_no
),
463 macio_resource_start(dev
, resource_no
),
464 dev
->ofdev
.dev
.bus_id
);
469 * macio_release_resource - Release an MMIO resource
470 * @dev: pointer to the device holding the resource
471 * @resource_no: resource number to release
473 void macio_release_resource(struct macio_dev
*dev
, int resource_no
)
475 if (macio_resource_len(dev
, resource_no
) == 0)
477 release_mem_region(macio_resource_start(dev
, resource_no
),
478 macio_resource_len(dev
, resource_no
));
482 * macio_request_resources - Reserve all memory resources
483 * @dev: MacIO device whose resources are to be reserved
484 * @name: Name to be associated with resource.
486 * Mark all memory regions associated with MacIO device @dev as
487 * being reserved by owner @name. Do not access any address inside
488 * the memory regions unless this call returns successfully.
490 * Returns 0 on success, or %EBUSY on error. A warning
491 * message is also printed on failure.
493 int macio_request_resources(struct macio_dev
*dev
, const char *name
)
497 for (i
= 0; i
< dev
->n_resources
; i
++)
498 if (macio_request_resource(dev
, i
, name
))
504 macio_release_resource(dev
, i
);
510 * macio_release_resources - Release reserved memory resources
511 * @dev: MacIO device whose resources were previously reserved
514 void macio_release_resources(struct macio_dev
*dev
)
518 for (i
= 0; i
< dev
->n_resources
; i
++)
519 macio_release_resource(dev
, i
);
525 static int __devinit
macio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
527 struct device_node
* np
;
528 struct macio_chip
* chip
;
530 if (ent
->vendor
!= PCI_VENDOR_ID_APPLE
)
533 /* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
534 * to new OF APIs and returns a node with refcount incremented. This isn't
535 * the case today, but on the other hand ppc32 doesn't do refcounting. This
536 * will have to be fixed when going to ppc64. --BenH.
538 np
= pci_device_to_OF_node(pdev
);
542 /* This assumption is wrong, fix that here for now until I fix the arch */
545 /* We also assume that pmac_feature will have done a get() on nodes stored
546 * in the macio chips array
548 chip
= macio_find(np
, macio_unknown
);
553 /* XXX Need locking ??? */
554 if (chip
->lbus
.pdev
== NULL
) {
555 chip
->lbus
.pdev
= pdev
;
556 chip
->lbus
.chip
= chip
;
557 pci_set_drvdata(pdev
, &chip
->lbus
);
558 pci_set_master(pdev
);
561 printk(KERN_INFO
"MacIO PCI driver attached to %s chipset\n",
565 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
566 * have 2 macio ASICs. I must probe the "main" one first or IDE ordering
567 * will be incorrect. So I put on "hold" the second one since it seem to
568 * appear first on PCI
570 if (chip
->type
== macio_gatwick
|| chip
->type
== macio_ohareII
)
571 if (macio_chips
[0].lbus
.pdev
== NULL
) {
572 macio_on_hold
= chip
;
576 macio_pci_add_devices(chip
);
577 if (macio_on_hold
&& macio_chips
[0].lbus
.pdev
!= NULL
) {
578 macio_pci_add_devices(macio_on_hold
);
579 macio_on_hold
= NULL
;
585 static void __devexit
macio_pci_remove(struct pci_dev
* pdev
)
587 panic("removing of macio-asic not supported !\n");
591 * MacIO is matched against any Apple ID, it's probe() function
592 * will then decide wether it applies or not
594 static const struct pci_device_id __devinitdata pci_ids
[] = { {
595 .vendor
= PCI_VENDOR_ID_APPLE
,
596 .device
= PCI_ANY_ID
,
597 .subvendor
= PCI_ANY_ID
,
598 .subdevice
= PCI_ANY_ID
,
600 }, { /* end: all zeroes */ }
602 MODULE_DEVICE_TABLE (pci
, pci_ids
);
604 /* pci driver glue; this is a "new style" PCI driver module */
605 static struct pci_driver macio_pci_driver
= {
606 .name
= (char *) "macio",
609 .probe
= macio_pci_probe
,
610 .remove
= macio_pci_remove
,
613 #endif /* CONFIG_PCI */
615 static int __init
macio_module_init (void)
620 rc
= pci_register_driver(&macio_pci_driver
);
623 #endif /* CONFIG_PCI */
627 module_init(macio_module_init
);
629 EXPORT_SYMBOL(macio_register_driver
);
630 EXPORT_SYMBOL(macio_unregister_driver
);
631 EXPORT_SYMBOL(macio_dev_get
);
632 EXPORT_SYMBOL(macio_dev_put
);
633 EXPORT_SYMBOL(macio_request_resource
);
634 EXPORT_SYMBOL(macio_release_resource
);
635 EXPORT_SYMBOL(macio_request_resources
);
636 EXPORT_SYMBOL(macio_release_resources
);