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 <linux/slab.h>
22 #include <asm/machdep.h>
23 #include <asm/macio.h>
24 #include <asm/pmac_feature.h>
26 #include <asm/pci-bridge.h>
30 #define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
32 static struct macio_chip
*macio_on_hold
;
34 static int macio_bus_match(struct device
*dev
, struct device_driver
*drv
)
36 struct macio_dev
* macio_dev
= to_macio_device(dev
);
37 struct macio_driver
* macio_drv
= to_macio_driver(drv
);
38 const struct of_device_id
* matches
= macio_drv
->match_table
;
43 return of_match_device(matches
, &macio_dev
->ofdev
) != NULL
;
46 struct macio_dev
*macio_dev_get(struct macio_dev
*dev
)
52 tmp
= get_device(&dev
->ofdev
.dev
);
54 return to_macio_device(tmp
);
59 void macio_dev_put(struct macio_dev
*dev
)
62 put_device(&dev
->ofdev
.dev
);
66 static int macio_device_probe(struct device
*dev
)
69 struct macio_driver
*drv
;
70 struct macio_dev
*macio_dev
;
71 const struct of_device_id
*match
;
73 drv
= to_macio_driver(dev
->driver
);
74 macio_dev
= to_macio_device(dev
);
79 macio_dev_get(macio_dev
);
81 match
= of_match_device(drv
->match_table
, &macio_dev
->ofdev
);
83 error
= drv
->probe(macio_dev
, match
);
85 macio_dev_put(macio_dev
);
90 static int macio_device_remove(struct device
*dev
)
92 struct macio_dev
* macio_dev
= to_macio_device(dev
);
93 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
95 if (dev
->driver
&& drv
->remove
)
96 drv
->remove(macio_dev
);
97 macio_dev_put(macio_dev
);
102 static void macio_device_shutdown(struct device
*dev
)
104 struct macio_dev
* macio_dev
= to_macio_device(dev
);
105 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
107 if (dev
->driver
&& drv
->shutdown
)
108 drv
->shutdown(macio_dev
);
111 static int macio_device_suspend(struct device
*dev
, pm_message_t state
)
113 struct macio_dev
* macio_dev
= to_macio_device(dev
);
114 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
116 if (dev
->driver
&& drv
->suspend
)
117 return drv
->suspend(macio_dev
, state
);
121 static int macio_device_resume(struct device
* dev
)
123 struct macio_dev
* macio_dev
= to_macio_device(dev
);
124 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
126 if (dev
->driver
&& drv
->resume
)
127 return drv
->resume(macio_dev
);
131 static int macio_hotplug (struct device
*dev
, char **envp
, int num_envp
,
132 char *buffer
, int buffer_size
)
134 struct macio_dev
* macio_dev
;
135 struct of_device
* of
;
136 char *scratch
, *compat
;
144 macio_dev
= to_macio_device(dev
);
148 of
= &macio_dev
->ofdev
;
151 /* stuff we want to pass to /sbin/hotplug */
153 length
+= scnprintf (scratch
, buffer_size
- length
, "OF_NAME=%s",
155 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
161 length
+= scnprintf (scratch
, buffer_size
- length
, "OF_TYPE=%s",
163 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
168 /* Since the compatible field can contain pretty much anything
169 * it's not really legal to split it out with commas. We split it
170 * up using a number of environment variables instead. */
172 compat
= (char *) get_property(of
->node
, "compatible", &cplen
);
173 while (compat
&& cplen
> 0) {
176 length
+= scnprintf (scratch
, buffer_size
- length
,
177 "OF_COMPATIBLE_%d=%s", seen
, compat
);
178 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
182 l
= strlen (compat
) + 1;
189 length
+= scnprintf (scratch
, buffer_size
- length
,
190 "OF_COMPATIBLE_N=%d", seen
);
191 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
201 extern struct device_attribute macio_dev_attrs
[];
203 struct bus_type macio_bus_type
= {
205 .match
= macio_bus_match
,
206 .hotplug
= macio_hotplug
,
207 .suspend
= macio_device_suspend
,
208 .resume
= macio_device_resume
,
209 .dev_attrs
= macio_dev_attrs
,
212 static int __init
macio_bus_driver_init(void)
214 return bus_register(&macio_bus_type
);
217 postcore_initcall(macio_bus_driver_init
);
221 * macio_release_dev - free a macio device structure when all users of it are finished.
222 * @dev: device that's been disconnected
224 * Will be called only by the device core when all users of this macio device are
225 * done. This currently means never as we don't hot remove any macio device yet,
226 * though that will happen with mediabay based devices in a later implementation.
228 static void macio_release_dev(struct device
*dev
)
230 struct macio_dev
*mdev
;
232 mdev
= to_macio_device(dev
);
237 * macio_resource_quirks - tweak or skip some resources for a device
238 * @np: pointer to the device node
239 * @res: resulting resource
240 * @index: index of resource in node
242 * If this routine returns non-null, then the resource is completely
245 static int macio_resource_quirks(struct device_node
*np
, struct resource
*res
, int index
)
247 if (res
->flags
& IORESOURCE_MEM
) {
248 /* Grand Central has too large resource 0 on some machines */
249 if (index
== 0 && !strcmp(np
->name
, "gc")) {
250 np
->addrs
[0].size
= 0x20000;
251 res
->end
= res
->start
+ 0x1ffff;
253 /* Airport has bogus resource 2 */
254 if (index
>= 2 && !strcmp(np
->name
, "radio"))
256 /* DBDMAs may have bogus sizes */
257 if ((res
->start
& 0x0001f000) == 0x00008000) {
258 np
->addrs
[index
].size
= 0x100;
259 res
->end
= res
->start
+ 0xff;
261 /* ESCC parent eats child resources. We could have added a level of hierarchy,
262 * but I don't really feel the need for it */
263 if (!strcmp(np
->name
, "escc"))
265 /* ESCC has bogus resources >= 3 */
266 if (index
>= 3 && !(strcmp(np
->name
, "ch-a") && strcmp(np
->name
, "ch-b")))
268 /* Media bay has too many resources, keep only first one */
269 if (index
> 0 && !strcmp(np
->name
, "media-bay"))
271 /* Some older IDE resources have bogus sizes */
272 if (!(strcmp(np
->name
, "IDE") && strcmp(np
->name
, "ATA") &&
273 strcmp(np
->type
, "ide") && strcmp(np
->type
, "ata"))) {
274 if (index
== 0 && np
->addrs
[0].size
> 0x1000) {
275 np
->addrs
[0].size
= 0x1000;
276 res
->end
= res
->start
+ 0xfff;
278 if (index
== 1 && np
->addrs
[1].size
> 0x100) {
279 np
->addrs
[1].size
= 0x100;
280 res
->end
= res
->start
+ 0xff;
289 * macio_add_one_device - Add one device from OF node to the device tree
290 * @chip: pointer to the macio_chip holding the device
291 * @np: pointer to the device node in the OF tree
292 * @in_bay: set to 1 if device is part of a media-bay
294 * When media-bay is changed to hotswap drivers, this function will
295 * be exposed to the bay driver some way...
297 static struct macio_dev
* macio_add_one_device(struct macio_chip
*chip
, struct device
*parent
,
298 struct device_node
*np
, struct macio_dev
*in_bay
,
299 struct resource
*parent_res
)
301 struct macio_dev
*dev
;
308 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
311 memset(dev
, 0, sizeof(*dev
));
313 dev
->bus
= &chip
->lbus
;
314 dev
->media_bay
= in_bay
;
315 dev
->ofdev
.node
= np
;
316 dev
->ofdev
.dma_mask
= 0xffffffffUL
;
317 dev
->ofdev
.dev
.dma_mask
= &dev
->ofdev
.dma_mask
;
318 dev
->ofdev
.dev
.parent
= parent
;
319 dev
->ofdev
.dev
.bus
= &macio_bus_type
;
320 dev
->ofdev
.dev
.release
= macio_release_dev
;
323 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
324 dev
, &dev
->ofdev
, &dev
->ofdev
.dev
, &dev
->ofdev
.dev
.kobj
);
327 /* MacIO itself has a different reg, we use it's PCI base */
328 if (np
== chip
->of_node
) {
329 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08lx:%.*s", chip
->lbus
.index
,
331 pci_resource_start(chip
->lbus
.pdev
, 0),
333 0, /* NuBus may want to do something better here */
335 MAX_NODE_NAME_SIZE
, np
->name
);
337 reg
= (u32
*)get_property(np
, "reg", NULL
);
338 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08x:%.*s", chip
->lbus
.index
,
339 reg
? *reg
: 0, MAX_NODE_NAME_SIZE
, np
->name
);
342 /* For now, we use pre-parsed entries in the device-tree for
343 * interrupt routing and addresses, but we should change that
344 * to dynamically parsed entries and so get rid of most of the
345 * clutter in struct device_node
347 for (i
= j
= 0; i
< np
->n_intrs
; i
++) {
348 struct resource
*res
= &dev
->interrupt
[j
];
350 if (j
>= MACIO_DEV_COUNT_IRQS
)
352 res
->start
= np
->intrs
[i
].line
;
353 res
->flags
= IORESOURCE_IO
;
354 if (np
->intrs
[j
].sense
)
355 res
->flags
|= IORESOURCE_IRQ_LOWLEVEL
;
357 res
->flags
|= IORESOURCE_IRQ_HIGHEDGE
;
358 res
->name
= dev
->ofdev
.dev
.bus_id
;
359 if (macio_resource_quirks(np
, res
, i
))
360 memset(res
, 0, sizeof(struct resource
));
364 dev
->n_interrupts
= j
;
365 for (i
= j
= 0; i
< np
->n_addrs
; i
++) {
366 struct resource
*res
= &dev
->resource
[j
];
368 if (j
>= MACIO_DEV_COUNT_RESOURCES
)
370 res
->start
= np
->addrs
[i
].address
;
371 res
->end
= np
->addrs
[i
].address
+ np
->addrs
[i
].size
- 1;
372 res
->flags
= IORESOURCE_MEM
;
373 res
->name
= dev
->ofdev
.dev
.bus_id
;
374 if (macio_resource_quirks(np
, res
, i
))
375 memset(res
, 0, sizeof(struct resource
));
378 /* Currently, we consider failure as harmless, this may
379 * change in the future, once I've found all the device
380 * tree bugs in older machines & worked around them
382 if (insert_resource(parent_res
, res
))
383 printk(KERN_WARNING
"Can't request resource %d for MacIO"
384 " device %s\n", i
, dev
->ofdev
.dev
.bus_id
);
387 dev
->n_resources
= j
;
389 if (of_device_register(&dev
->ofdev
) != 0) {
390 printk(KERN_DEBUG
"macio: device registration error for %s!\n",
391 dev
->ofdev
.dev
.bus_id
);
399 static int macio_skip_device(struct device_node
*np
)
401 if (strncmp(np
->name
, "battery", 7) == 0)
403 if (strncmp(np
->name
, "escc-legacy", 11) == 0)
409 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
410 * @chip: pointer to the macio_chip holding the devices
412 * This function will do the job of extracting devices from the
413 * Open Firmware device tree, build macio_dev structures and add
414 * them to the Linux device tree.
416 * For now, childs of media-bay are added now as well. This will
419 static void macio_pci_add_devices(struct macio_chip
*chip
)
421 struct device_node
*np
, *pnode
;
422 struct macio_dev
*rdev
, *mdev
, *mbdev
= NULL
, *sdev
= NULL
;
423 struct device
*parent
= NULL
;
424 struct resource
*root_res
= &iomem_resource
;
426 /* Add a node for the macio bus itself */
428 if (chip
->lbus
.pdev
) {
429 parent
= &chip
->lbus
.pdev
->dev
;
430 root_res
= &chip
->lbus
.pdev
->resource
[0];
433 pnode
= of_node_get(chip
->of_node
);
437 /* Add macio itself to hierarchy */
438 rdev
= macio_add_one_device(chip
, parent
, pnode
, NULL
, root_res
);
441 root_res
= &rdev
->resource
[0];
443 /* First scan 1st level */
444 for (np
= NULL
; (np
= of_get_next_child(pnode
, np
)) != NULL
;) {
445 if (!macio_skip_device(np
)) {
447 mdev
= macio_add_one_device(chip
, &rdev
->ofdev
.dev
, np
, NULL
, root_res
);
450 else if (strncmp(np
->name
, "media-bay", 9) == 0)
452 else if (strncmp(np
->name
, "escc", 4) == 0)
457 /* Add media bay devices if any */
459 for (np
= NULL
; (np
= of_get_next_child(mbdev
->ofdev
.node
, np
)) != NULL
;)
460 if (!macio_skip_device(np
)) {
462 if (macio_add_one_device(chip
, &mbdev
->ofdev
.dev
, np
, mbdev
,
466 /* Add serial ports if any */
468 for (np
= NULL
; (np
= of_get_next_child(sdev
->ofdev
.node
, np
)) != NULL
;)
469 if (!macio_skip_device(np
)) {
471 if (macio_add_one_device(chip
, &sdev
->ofdev
.dev
, np
, NULL
,
480 * macio_register_driver - Registers a new MacIO device driver
481 * @drv: pointer to the driver definition structure
483 int macio_register_driver(struct macio_driver
*drv
)
487 /* initialize common driver fields */
488 drv
->driver
.name
= drv
->name
;
489 drv
->driver
.bus
= &macio_bus_type
;
490 drv
->driver
.probe
= macio_device_probe
;
491 drv
->driver
.remove
= macio_device_remove
;
492 drv
->driver
.shutdown
= macio_device_shutdown
;
494 /* register with core */
495 count
= driver_register(&drv
->driver
);
496 return count
? count
: 1;
500 * macio_unregister_driver - Unregisters a new MacIO device driver
501 * @drv: pointer to the driver definition structure
503 void macio_unregister_driver(struct macio_driver
*drv
)
505 driver_unregister(&drv
->driver
);
509 * macio_request_resource - Request an MMIO resource
510 * @dev: pointer to the device holding the resource
511 * @resource_no: resource number to request
512 * @name: resource name
514 * Mark memory region number @resource_no associated with MacIO
515 * device @dev as being reserved by owner @name. Do not access
516 * any address inside the memory regions unless this call returns
519 * Returns 0 on success, or %EBUSY on error. A warning
520 * message is also printed on failure.
522 int macio_request_resource(struct macio_dev
*dev
, int resource_no
, const char *name
)
524 if (macio_resource_len(dev
, resource_no
) == 0)
527 if (!request_mem_region(macio_resource_start(dev
, resource_no
),
528 macio_resource_len(dev
, resource_no
),
535 printk (KERN_WARNING
"MacIO: Unable to reserve resource #%d:%lx@%lx"
538 macio_resource_len(dev
, resource_no
),
539 macio_resource_start(dev
, resource_no
),
540 dev
->ofdev
.dev
.bus_id
);
545 * macio_release_resource - Release an MMIO resource
546 * @dev: pointer to the device holding the resource
547 * @resource_no: resource number to release
549 void macio_release_resource(struct macio_dev
*dev
, int resource_no
)
551 if (macio_resource_len(dev
, resource_no
) == 0)
553 release_mem_region(macio_resource_start(dev
, resource_no
),
554 macio_resource_len(dev
, resource_no
));
558 * macio_request_resources - Reserve all memory resources
559 * @dev: MacIO device whose resources are to be reserved
560 * @name: Name to be associated with resource.
562 * Mark all memory regions associated with MacIO device @dev as
563 * being reserved by owner @name. Do not access any address inside
564 * the memory regions unless this call returns successfully.
566 * Returns 0 on success, or %EBUSY on error. A warning
567 * message is also printed on failure.
569 int macio_request_resources(struct macio_dev
*dev
, const char *name
)
573 for (i
= 0; i
< dev
->n_resources
; i
++)
574 if (macio_request_resource(dev
, i
, name
))
580 macio_release_resource(dev
, i
);
586 * macio_release_resources - Release reserved memory resources
587 * @dev: MacIO device whose resources were previously reserved
590 void macio_release_resources(struct macio_dev
*dev
)
594 for (i
= 0; i
< dev
->n_resources
; i
++)
595 macio_release_resource(dev
, i
);
601 static int __devinit
macio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
603 struct device_node
* np
;
604 struct macio_chip
* chip
;
606 if (ent
->vendor
!= PCI_VENDOR_ID_APPLE
)
609 /* Note regarding refcounting: We assume pci_device_to_OF_node() is ported
610 * to new OF APIs and returns a node with refcount incremented. This isn't
611 * the case today, but on the other hand ppc32 doesn't do refcounting. This
612 * will have to be fixed when going to ppc64. --BenH.
614 np
= pci_device_to_OF_node(pdev
);
618 /* This assumption is wrong, fix that here for now until I fix the arch */
621 /* We also assume that pmac_feature will have done a get() on nodes stored
622 * in the macio chips array
624 chip
= macio_find(np
, macio_unknown
);
629 /* XXX Need locking ??? */
630 if (chip
->lbus
.pdev
== NULL
) {
631 chip
->lbus
.pdev
= pdev
;
632 chip
->lbus
.chip
= chip
;
633 pci_set_drvdata(pdev
, &chip
->lbus
);
634 pci_set_master(pdev
);
637 printk(KERN_INFO
"MacIO PCI driver attached to %s chipset\n",
641 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
642 * have 2 macio ASICs. I must probe the "main" one first or IDE ordering
643 * will be incorrect. So I put on "hold" the second one since it seem to
644 * appear first on PCI
646 if (chip
->type
== macio_gatwick
|| chip
->type
== macio_ohareII
)
647 if (macio_chips
[0].lbus
.pdev
== NULL
) {
648 macio_on_hold
= chip
;
652 macio_pci_add_devices(chip
);
653 if (macio_on_hold
&& macio_chips
[0].lbus
.pdev
!= NULL
) {
654 macio_pci_add_devices(macio_on_hold
);
655 macio_on_hold
= NULL
;
661 static void __devexit
macio_pci_remove(struct pci_dev
* pdev
)
663 panic("removing of macio-asic not supported !\n");
667 * MacIO is matched against any Apple ID, it's probe() function
668 * will then decide wether it applies or not
670 static const struct pci_device_id __devinitdata pci_ids
[] = { {
671 .vendor
= PCI_VENDOR_ID_APPLE
,
672 .device
= PCI_ANY_ID
,
673 .subvendor
= PCI_ANY_ID
,
674 .subdevice
= PCI_ANY_ID
,
676 }, { /* end: all zeroes */ }
678 MODULE_DEVICE_TABLE (pci
, pci_ids
);
680 /* pci driver glue; this is a "new style" PCI driver module */
681 static struct pci_driver macio_pci_driver
= {
682 .name
= (char *) "macio",
685 .probe
= macio_pci_probe
,
686 .remove
= macio_pci_remove
,
689 #endif /* CONFIG_PCI */
691 static int __init
macio_module_init (void)
696 rc
= pci_register_driver(&macio_pci_driver
);
699 #endif /* CONFIG_PCI */
703 module_init(macio_module_init
);
705 EXPORT_SYMBOL(macio_register_driver
);
706 EXPORT_SYMBOL(macio_unregister_driver
);
707 EXPORT_SYMBOL(macio_dev_get
);
708 EXPORT_SYMBOL(macio_dev_put
);
709 EXPORT_SYMBOL(macio_request_resource
);
710 EXPORT_SYMBOL(macio_release_resource
);
711 EXPORT_SYMBOL(macio_request_resources
);
712 EXPORT_SYMBOL(macio_release_resources
);