2 * Bus & driver management routines for devices within
3 * a MacIO ASIC. Interface to new driver model mostly
4 * stolen from the PCI version.
6 * Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org)
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.
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
20 #include <linux/config.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/pci.h>
24 #include <linux/pci_ids.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
29 #include <asm/machdep.h>
30 #include <asm/macio.h>
31 #include <asm/pmac_feature.h>
33 #include <asm/pci-bridge.h>
37 #define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
39 static struct macio_chip
*macio_on_hold
;
41 static int macio_bus_match(struct device
*dev
, struct device_driver
*drv
)
43 struct macio_dev
* macio_dev
= to_macio_device(dev
);
44 struct macio_driver
* macio_drv
= to_macio_driver(drv
);
45 const struct of_device_id
* matches
= macio_drv
->match_table
;
50 return of_match_device(matches
, &macio_dev
->ofdev
) != NULL
;
53 struct macio_dev
*macio_dev_get(struct macio_dev
*dev
)
59 tmp
= get_device(&dev
->ofdev
.dev
);
61 return to_macio_device(tmp
);
66 void macio_dev_put(struct macio_dev
*dev
)
69 put_device(&dev
->ofdev
.dev
);
73 static int macio_device_probe(struct device
*dev
)
76 struct macio_driver
*drv
;
77 struct macio_dev
*macio_dev
;
78 const struct of_device_id
*match
;
80 drv
= to_macio_driver(dev
->driver
);
81 macio_dev
= to_macio_device(dev
);
86 macio_dev_get(macio_dev
);
88 match
= of_match_device(drv
->match_table
, &macio_dev
->ofdev
);
90 error
= drv
->probe(macio_dev
, match
);
92 macio_dev_put(macio_dev
);
97 static int macio_device_remove(struct device
*dev
)
99 struct macio_dev
* macio_dev
= to_macio_device(dev
);
100 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
102 if (dev
->driver
&& drv
->remove
)
103 drv
->remove(macio_dev
);
104 macio_dev_put(macio_dev
);
109 static void macio_device_shutdown(struct device
*dev
)
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
->shutdown
)
115 drv
->shutdown(macio_dev
);
118 static int macio_device_suspend(struct device
*dev
, pm_message_t state
)
120 struct macio_dev
* macio_dev
= to_macio_device(dev
);
121 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
123 if (dev
->driver
&& drv
->suspend
)
124 return drv
->suspend(macio_dev
, state
);
128 static int macio_device_resume(struct device
* dev
)
130 struct macio_dev
* macio_dev
= to_macio_device(dev
);
131 struct macio_driver
* drv
= to_macio_driver(dev
->driver
);
133 if (dev
->driver
&& drv
->resume
)
134 return drv
->resume(macio_dev
);
138 static int macio_uevent(struct device
*dev
, char **envp
, int num_envp
,
139 char *buffer
, int buffer_size
)
141 struct macio_dev
* macio_dev
;
142 struct of_device
* of
;
143 char *scratch
, *compat
;
151 macio_dev
= to_macio_device(dev
);
155 of
= &macio_dev
->ofdev
;
158 /* stuff we want to pass to /sbin/hotplug */
160 length
+= scnprintf (scratch
, buffer_size
- length
, "OF_NAME=%s",
162 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
168 length
+= scnprintf (scratch
, buffer_size
- length
, "OF_TYPE=%s",
170 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
175 /* Since the compatible field can contain pretty much anything
176 * it's not really legal to split it out with commas. We split it
177 * up using a number of environment variables instead. */
179 compat
= (char *) get_property(of
->node
, "compatible", &cplen
);
180 while (compat
&& cplen
> 0) {
183 length
+= scnprintf (scratch
, buffer_size
- length
,
184 "OF_COMPATIBLE_%d=%s", seen
, compat
);
185 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
189 l
= strlen (compat
) + 1;
196 length
+= scnprintf (scratch
, buffer_size
- length
,
197 "OF_COMPATIBLE_N=%d", seen
);
198 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
208 extern struct device_attribute macio_dev_attrs
[];
210 struct bus_type macio_bus_type
= {
212 .match
= macio_bus_match
,
213 .uevent
= macio_uevent
,
214 .suspend
= macio_device_suspend
,
215 .resume
= macio_device_resume
,
216 .dev_attrs
= macio_dev_attrs
,
219 static int __init
macio_bus_driver_init(void)
221 return bus_register(&macio_bus_type
);
224 postcore_initcall(macio_bus_driver_init
);
228 * macio_release_dev - free a macio device structure when all users of it are
230 * @dev: device that's been disconnected
232 * Will be called only by the device core when all users of this macio device
233 * are done. This currently means never as we don't hot remove any macio
234 * device yet, though that will happen with mediabay based devices in a later
237 static void macio_release_dev(struct device
*dev
)
239 struct macio_dev
*mdev
;
241 mdev
= to_macio_device(dev
);
246 * macio_resource_quirks - tweak or skip some resources for a device
247 * @np: pointer to the device node
248 * @res: resulting resource
249 * @index: index of resource in node
251 * If this routine returns non-null, then the resource is completely
254 static int macio_resource_quirks(struct device_node
*np
, struct resource
*res
,
257 if (res
->flags
& IORESOURCE_MEM
) {
258 /* Grand Central has too large resource 0 on some machines */
259 if (index
== 0 && !strcmp(np
->name
, "gc"))
260 res
->end
= res
->start
+ 0x1ffff;
262 /* Airport has bogus resource 2 */
263 if (index
>= 2 && !strcmp(np
->name
, "radio"))
267 /* DBDMAs may have bogus sizes */
268 if ((res
->start
& 0x0001f000) == 0x00008000)
269 res
->end
= res
->start
+ 0xff;
270 #endif /* CONFIG_PPC64 */
272 /* ESCC parent eats child resources. We could have added a
273 * level of hierarchy, but I don't really feel the need
276 if (!strcmp(np
->name
, "escc"))
279 /* ESCC has bogus resources >= 3 */
280 if (index
>= 3 && !(strcmp(np
->name
, "ch-a") &&
281 strcmp(np
->name
, "ch-b")))
284 /* Media bay has too many resources, keep only first one */
285 if (index
> 0 && !strcmp(np
->name
, "media-bay"))
288 /* Some older IDE resources have bogus sizes */
289 if (!(strcmp(np
->name
, "IDE") && strcmp(np
->name
, "ATA") &&
290 strcmp(np
->type
, "ide") && strcmp(np
->type
, "ata"))) {
291 if (index
== 0 && (res
->end
- res
->start
) > 0xfff)
292 res
->end
= res
->start
+ 0xfff;
293 if (index
== 1 && (res
->end
- res
->start
) > 0xff)
294 res
->end
= res
->start
+ 0xff;
301 static void macio_setup_interrupts(struct macio_dev
*dev
)
303 struct device_node
*np
= dev
->ofdev
.node
;
306 /* For now, we use pre-parsed entries in the device-tree for
307 * interrupt routing and addresses, but we should change that
308 * to dynamically parsed entries and so get rid of most of the
309 * clutter in struct device_node
311 for (i
= j
= 0; i
< np
->n_intrs
; i
++) {
312 struct resource
*res
= &dev
->interrupt
[j
];
314 if (j
>= MACIO_DEV_COUNT_IRQS
)
316 res
->start
= np
->intrs
[i
].line
;
317 res
->flags
= IORESOURCE_IO
;
318 if (np
->intrs
[j
].sense
)
319 res
->flags
|= IORESOURCE_IRQ_LOWLEVEL
;
321 res
->flags
|= IORESOURCE_IRQ_HIGHEDGE
;
322 res
->name
= dev
->ofdev
.dev
.bus_id
;
323 if (macio_resource_quirks(np
, res
, i
))
324 memset(res
, 0, sizeof(struct resource
));
328 dev
->n_interrupts
= j
;
331 static void macio_setup_resources(struct macio_dev
*dev
,
332 struct resource
*parent_res
)
334 struct device_node
*np
= dev
->ofdev
.node
;
338 for (index
= 0; of_address_to_resource(np
, index
, &r
) == 0; index
++) {
339 struct resource
*res
= &dev
->resource
[index
];
340 if (index
>= MACIO_DEV_COUNT_RESOURCES
)
343 res
->name
= dev
->ofdev
.dev
.bus_id
;
345 if (macio_resource_quirks(np
, res
, index
)) {
346 memset(res
, 0, sizeof(struct resource
));
349 /* Currently, we consider failure as harmless, this may
350 * change in the future, once I've found all the device
351 * tree bugs in older machines & worked around them
353 if (insert_resource(parent_res
, res
)) {
354 printk(KERN_WARNING
"Can't request resource "
355 "%d for MacIO device %s\n",
356 index
, dev
->ofdev
.dev
.bus_id
);
359 dev
->n_resources
= index
;
363 * macio_add_one_device - Add one device from OF node to the device tree
364 * @chip: pointer to the macio_chip holding the device
365 * @np: pointer to the device node in the OF tree
366 * @in_bay: set to 1 if device is part of a media-bay
368 * When media-bay is changed to hotswap drivers, this function will
369 * be exposed to the bay driver some way...
371 static struct macio_dev
* macio_add_one_device(struct macio_chip
*chip
,
372 struct device
*parent
,
373 struct device_node
*np
,
374 struct macio_dev
*in_bay
,
375 struct resource
*parent_res
)
377 struct macio_dev
*dev
;
383 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
386 memset(dev
, 0, sizeof(*dev
));
388 dev
->bus
= &chip
->lbus
;
389 dev
->media_bay
= in_bay
;
390 dev
->ofdev
.node
= np
;
391 dev
->ofdev
.dma_mask
= 0xffffffffUL
;
392 dev
->ofdev
.dev
.dma_mask
= &dev
->ofdev
.dma_mask
;
393 dev
->ofdev
.dev
.parent
= parent
;
394 dev
->ofdev
.dev
.bus
= &macio_bus_type
;
395 dev
->ofdev
.dev
.release
= macio_release_dev
;
398 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
399 dev
, &dev
->ofdev
, &dev
->ofdev
.dev
, &dev
->ofdev
.dev
.kobj
);
402 /* MacIO itself has a different reg, we use it's PCI base */
403 if (np
== chip
->of_node
) {
404 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08lx:%.*s",
407 pci_resource_start(chip
->lbus
.pdev
, 0),
409 0, /* NuBus may want to do something better here */
411 MAX_NODE_NAME_SIZE
, np
->name
);
413 reg
= (u32
*)get_property(np
, "reg", NULL
);
414 sprintf(dev
->ofdev
.dev
.bus_id
, "%1d.%08x:%.*s",
416 reg
? *reg
: 0, MAX_NODE_NAME_SIZE
, np
->name
);
419 /* Setup interrupts & resources */
420 macio_setup_interrupts(dev
);
421 macio_setup_resources(dev
, parent_res
);
423 /* Register with core */
424 if (of_device_register(&dev
->ofdev
) != 0) {
425 printk(KERN_DEBUG
"macio: device registration error for %s!\n",
426 dev
->ofdev
.dev
.bus_id
);
434 static int macio_skip_device(struct device_node
*np
)
436 if (strncmp(np
->name
, "battery", 7) == 0)
438 if (strncmp(np
->name
, "escc-legacy", 11) == 0)
444 * macio_pci_add_devices - Adds sub-devices of mac-io to the device tree
445 * @chip: pointer to the macio_chip holding the devices
447 * This function will do the job of extracting devices from the
448 * Open Firmware device tree, build macio_dev structures and add
449 * them to the Linux device tree.
451 * For now, childs of media-bay are added now as well. This will
454 static void macio_pci_add_devices(struct macio_chip
*chip
)
456 struct device_node
*np
, *pnode
;
457 struct macio_dev
*rdev
, *mdev
, *mbdev
= NULL
, *sdev
= NULL
;
458 struct device
*parent
= NULL
;
459 struct resource
*root_res
= &iomem_resource
;
461 /* Add a node for the macio bus itself */
463 if (chip
->lbus
.pdev
) {
464 parent
= &chip
->lbus
.pdev
->dev
;
465 root_res
= &chip
->lbus
.pdev
->resource
[0];
468 pnode
= of_node_get(chip
->of_node
);
472 /* Add macio itself to hierarchy */
473 rdev
= macio_add_one_device(chip
, parent
, pnode
, NULL
, root_res
);
476 root_res
= &rdev
->resource
[0];
478 /* First scan 1st level */
479 for (np
= NULL
; (np
= of_get_next_child(pnode
, np
)) != NULL
;) {
480 if (macio_skip_device(np
))
483 mdev
= macio_add_one_device(chip
, &rdev
->ofdev
.dev
, np
, NULL
,
487 else if (strncmp(np
->name
, "media-bay", 9) == 0)
489 else if (strncmp(np
->name
, "escc", 4) == 0)
493 /* Add media bay devices if any */
495 for (np
= NULL
; (np
= of_get_next_child(mbdev
->ofdev
.node
, np
))
497 if (macio_skip_device(np
))
500 if (macio_add_one_device(chip
, &mbdev
->ofdev
.dev
, np
,
501 mbdev
, root_res
) == NULL
)
505 /* Add serial ports if any */
507 for (np
= NULL
; (np
= of_get_next_child(sdev
->ofdev
.node
, np
))
509 if (macio_skip_device(np
))
512 if (macio_add_one_device(chip
, &sdev
->ofdev
.dev
, np
,
513 NULL
, root_res
) == NULL
)
521 * macio_register_driver - Registers a new MacIO device driver
522 * @drv: pointer to the driver definition structure
524 int macio_register_driver(struct macio_driver
*drv
)
528 /* initialize common driver fields */
529 drv
->driver
.name
= drv
->name
;
530 drv
->driver
.bus
= &macio_bus_type
;
531 drv
->driver
.probe
= macio_device_probe
;
532 drv
->driver
.remove
= macio_device_remove
;
533 drv
->driver
.shutdown
= macio_device_shutdown
;
535 /* register with core */
536 count
= driver_register(&drv
->driver
);
537 return count
? count
: 1;
541 * macio_unregister_driver - Unregisters a new MacIO device driver
542 * @drv: pointer to the driver definition structure
544 void macio_unregister_driver(struct macio_driver
*drv
)
546 driver_unregister(&drv
->driver
);
550 * macio_request_resource - Request an MMIO resource
551 * @dev: pointer to the device holding the resource
552 * @resource_no: resource number to request
553 * @name: resource name
555 * Mark memory region number @resource_no associated with MacIO
556 * device @dev as being reserved by owner @name. Do not access
557 * any address inside the memory regions unless this call returns
560 * Returns 0 on success, or %EBUSY on error. A warning
561 * message is also printed on failure.
563 int macio_request_resource(struct macio_dev
*dev
, int resource_no
,
566 if (macio_resource_len(dev
, resource_no
) == 0)
569 if (!request_mem_region(macio_resource_start(dev
, resource_no
),
570 macio_resource_len(dev
, resource_no
),
577 printk (KERN_WARNING
"MacIO: Unable to reserve resource #%d:%lx@%lx"
580 macio_resource_len(dev
, resource_no
),
581 macio_resource_start(dev
, resource_no
),
582 dev
->ofdev
.dev
.bus_id
);
587 * macio_release_resource - Release an MMIO resource
588 * @dev: pointer to the device holding the resource
589 * @resource_no: resource number to release
591 void macio_release_resource(struct macio_dev
*dev
, int resource_no
)
593 if (macio_resource_len(dev
, resource_no
) == 0)
595 release_mem_region(macio_resource_start(dev
, resource_no
),
596 macio_resource_len(dev
, resource_no
));
600 * macio_request_resources - Reserve all memory resources
601 * @dev: MacIO device whose resources are to be reserved
602 * @name: Name to be associated with resource.
604 * Mark all memory regions associated with MacIO device @dev as
605 * being reserved by owner @name. Do not access any address inside
606 * the memory regions unless this call returns successfully.
608 * Returns 0 on success, or %EBUSY on error. A warning
609 * message is also printed on failure.
611 int macio_request_resources(struct macio_dev
*dev
, const char *name
)
615 for (i
= 0; i
< dev
->n_resources
; i
++)
616 if (macio_request_resource(dev
, i
, name
))
622 macio_release_resource(dev
, i
);
628 * macio_release_resources - Release reserved memory resources
629 * @dev: MacIO device whose resources were previously reserved
632 void macio_release_resources(struct macio_dev
*dev
)
636 for (i
= 0; i
< dev
->n_resources
; i
++)
637 macio_release_resource(dev
, i
);
643 static int __devinit
macio_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
645 struct device_node
* np
;
646 struct macio_chip
* chip
;
648 if (ent
->vendor
!= PCI_VENDOR_ID_APPLE
)
651 /* Note regarding refcounting: We assume pci_device_to_OF_node() is
652 * ported to new OF APIs and returns a node with refcount incremented.
654 np
= pci_device_to_OF_node(pdev
);
658 /* The above assumption is wrong !!!
659 * fix that here for now until I fix the arch code
663 /* We also assume that pmac_feature will have done a get() on nodes
664 * stored in the macio chips array
666 chip
= macio_find(np
, macio_unknown
);
671 /* XXX Need locking ??? */
672 if (chip
->lbus
.pdev
== NULL
) {
673 chip
->lbus
.pdev
= pdev
;
674 chip
->lbus
.chip
= chip
;
675 pci_set_drvdata(pdev
, &chip
->lbus
);
676 pci_set_master(pdev
);
679 printk(KERN_INFO
"MacIO PCI driver attached to %s chipset\n",
683 * HACK ALERT: The WallStreet PowerBook and some OHare based machines
684 * have 2 macio ASICs. I must probe the "main" one first or IDE
685 * ordering will be incorrect. So I put on "hold" the second one since
686 * it seem to appear first on PCI
688 if (chip
->type
== macio_gatwick
|| chip
->type
== macio_ohareII
)
689 if (macio_chips
[0].lbus
.pdev
== NULL
) {
690 macio_on_hold
= chip
;
694 macio_pci_add_devices(chip
);
695 if (macio_on_hold
&& macio_chips
[0].lbus
.pdev
!= NULL
) {
696 macio_pci_add_devices(macio_on_hold
);
697 macio_on_hold
= NULL
;
703 static void __devexit
macio_pci_remove(struct pci_dev
* pdev
)
705 panic("removing of macio-asic not supported !\n");
709 * MacIO is matched against any Apple ID, it's probe() function
710 * will then decide wether it applies or not
712 static const struct pci_device_id __devinitdata pci_ids
[] = { {
713 .vendor
= PCI_VENDOR_ID_APPLE
,
714 .device
= PCI_ANY_ID
,
715 .subvendor
= PCI_ANY_ID
,
716 .subdevice
= PCI_ANY_ID
,
718 }, { /* end: all zeroes */ }
720 MODULE_DEVICE_TABLE (pci
, pci_ids
);
722 /* pci driver glue; this is a "new style" PCI driver module */
723 static struct pci_driver macio_pci_driver
= {
724 .name
= (char *) "macio",
727 .probe
= macio_pci_probe
,
728 .remove
= macio_pci_remove
,
731 #endif /* CONFIG_PCI */
733 static int __init
macio_module_init (void)
738 rc
= pci_register_driver(&macio_pci_driver
);
741 #endif /* CONFIG_PCI */
745 module_init(macio_module_init
);
747 EXPORT_SYMBOL(macio_register_driver
);
748 EXPORT_SYMBOL(macio_unregister_driver
);
749 EXPORT_SYMBOL(macio_dev_get
);
750 EXPORT_SYMBOL(macio_dev_put
);
751 EXPORT_SYMBOL(macio_request_resource
);
752 EXPORT_SYMBOL(macio_release_resource
);
753 EXPORT_SYMBOL(macio_request_resources
);
754 EXPORT_SYMBOL(macio_release_resources
);