blob: 7fa369cfccebfd627c3c153f72d8159f8d7d8918 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Bus & driver management routines for devices within
3 * a MacIO ASIC. Interface to new driver model mostly
4 * stolen from the PCI version.
5 *
6 * TODO:
7 *
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
10 * sub-devices.
11 */
12
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>
23#include <asm/prom.h>
24#include <asm/pci-bridge.h>
25
26#undef DEBUG
27
28#define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)
29
30static struct macio_chip *macio_on_hold;
31
32static int macio_bus_match(struct device *dev, struct device_driver *drv)
33{
34 struct macio_dev * macio_dev = to_macio_device(dev);
35 struct macio_driver * macio_drv = to_macio_driver(drv);
Jeff Mahoney5e655772005-07-06 15:44:41 -040036 const struct of_device_id * matches = macio_drv->match_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 if (!matches)
39 return 0;
40
41 return of_match_device(matches, &macio_dev->ofdev) != NULL;
42}
43
44struct macio_dev *macio_dev_get(struct macio_dev *dev)
45{
46 struct device *tmp;
47
48 if (!dev)
49 return NULL;
50 tmp = get_device(&dev->ofdev.dev);
51 if (tmp)
52 return to_macio_device(tmp);
53 else
54 return NULL;
55}
56
57void macio_dev_put(struct macio_dev *dev)
58{
59 if (dev)
60 put_device(&dev->ofdev.dev);
61}
62
63
64static int macio_device_probe(struct device *dev)
65{
66 int error = -ENODEV;
67 struct macio_driver *drv;
68 struct macio_dev *macio_dev;
Jeff Mahoney5e655772005-07-06 15:44:41 -040069 const struct of_device_id *match;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 drv = to_macio_driver(dev->driver);
72 macio_dev = to_macio_device(dev);
73
74 if (!drv->probe)
75 return error;
76
77 macio_dev_get(macio_dev);
78
79 match = of_match_device(drv->match_table, &macio_dev->ofdev);
80 if (match)
81 error = drv->probe(macio_dev, match);
82 if (error)
83 macio_dev_put(macio_dev);
84
85 return error;
86}
87
88static int macio_device_remove(struct device *dev)
89{
90 struct macio_dev * macio_dev = to_macio_device(dev);
91 struct macio_driver * drv = to_macio_driver(dev->driver);
92
93 if (dev->driver && drv->remove)
94 drv->remove(macio_dev);
95 macio_dev_put(macio_dev);
96
97 return 0;
98}
99
100static void macio_device_shutdown(struct device *dev)
101{
102 struct macio_dev * macio_dev = to_macio_device(dev);
103 struct macio_driver * drv = to_macio_driver(dev->driver);
104
105 if (dev->driver && drv->shutdown)
106 drv->shutdown(macio_dev);
107}
108
Pavel Machekf4513902005-04-16 15:25:32 -0700109static int macio_device_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct macio_dev * macio_dev = to_macio_device(dev);
112 struct macio_driver * drv = to_macio_driver(dev->driver);
113
114 if (dev->driver && drv->suspend)
115 return drv->suspend(macio_dev, state);
116 return 0;
117}
118
119static int macio_device_resume(struct device * dev)
120{
121 struct macio_dev * macio_dev = to_macio_device(dev);
122 struct macio_driver * drv = to_macio_driver(dev->driver);
123
124 if (dev->driver && drv->resume)
125 return drv->resume(macio_dev);
126 return 0;
127}
128
Jeff Mahoneyb5bf5b62005-07-06 15:26:27 -0400129extern struct device_attribute macio_dev_attrs[];
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131struct bus_type macio_bus_type = {
132 .name = "macio",
133 .match = macio_bus_match,
134 .suspend = macio_device_suspend,
135 .resume = macio_device_resume,
Jeff Mahoneyb5bf5b62005-07-06 15:26:27 -0400136 .dev_attrs = macio_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139static int __init macio_bus_driver_init(void)
140{
141 return bus_register(&macio_bus_type);
142}
143
144postcore_initcall(macio_bus_driver_init);
145
146
147/**
148 * macio_release_dev - free a macio device structure when all users of it are finished.
149 * @dev: device that's been disconnected
150 *
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.
154 */
155static void macio_release_dev(struct device *dev)
156{
157 struct macio_dev *mdev;
158
159 mdev = to_macio_device(dev);
160 kfree(mdev);
161}
162
163/**
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
168 *
169 * If this routine returns non-null, then the resource is completely
170 * skipped.
171 */
172static int macio_resource_quirks(struct device_node *np, struct resource *res, int index)
173{
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;
179 }
180 /* Airport has bogus resource 2 */
181 if (index >= 2 && !strcmp(np->name, "radio"))
182 return 1;
183 /* DBDMAs may have bogus sizes */
184 if ((res->start & 0x0001f000) == 0x00008000) {
185 np->addrs[index].size = 0x100;
186 res->end = res->start + 0xff;
187 }
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"))
191 return 1;
192 /* ESCC has bogus resources >= 3 */
193 if (index >= 3 && !(strcmp(np->name, "ch-a") && strcmp(np->name, "ch-b")))
194 return 1;
195 /* Media bay has too many resources, keep only first one */
196 if (index > 0 && !strcmp(np->name, "media-bay"))
197 return 1;
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;
204 }
205 if (index == 1 && np->addrs[1].size > 0x100) {
206 np->addrs[1].size = 0x100;
207 res->end = res->start + 0xff;
208 }
209 }
210 }
211 return 0;
212}
213
214
215/**
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
220 *
221 * When media-bay is changed to hotswap drivers, this function will
222 * be exposed to the bay driver some way...
223 */
224static 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)
227{
228 struct macio_dev *dev;
229 int i, j;
230 u32 *reg;
231
232 if (np == NULL)
233 return NULL;
234
235 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
236 if (!dev)
237 return NULL;
238 memset(dev, 0, sizeof(*dev));
239
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;
248
249#ifdef DEBUG
250 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
251 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
252#endif
253
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,
257#ifdef CONFIG_PCI
258 pci_resource_start(chip->lbus.pdev, 0),
259#else
260 0, /* NuBus may want to do something better here */
261#endif
262 MAX_NODE_NAME_SIZE, np->name);
263 } else {
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);
267 }
268
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
273 */
274 for (i = j = 0; i < np->n_intrs; i++) {
275 struct resource *res = &dev->interrupt[j];
276
277 if (j >= MACIO_DEV_COUNT_IRQS)
278 break;
279 res->start = np->intrs[i].line;
280 res->flags = IORESOURCE_IO;
281 if (np->intrs[j].sense)
282 res->flags |= IORESOURCE_IRQ_LOWLEVEL;
283 else
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));
288 else
289 j++;
290 }
291 dev->n_interrupts = j;
292 for (i = j = 0; i < np->n_addrs; i++) {
293 struct resource *res = &dev->resource[j];
294
295 if (j >= MACIO_DEV_COUNT_RESOURCES)
296 break;
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));
303 else {
304 j++;
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
308 */
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);
312 }
313 }
314 dev->n_resources = j;
315
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);
319 kfree(dev);
320 return NULL;
321 }
322
323 return dev;
324}
325
326static int macio_skip_device(struct device_node *np)
327{
328 if (strncmp(np->name, "battery", 7) == 0)
329 return 1;
330 if (strncmp(np->name, "escc-legacy", 11) == 0)
331 return 1;
332 return 0;
333}
334
335/**
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
338 *
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.
342 *
343 * For now, childs of media-bay are added now as well. This will
344 * change rsn though.
345 */
346static void macio_pci_add_devices(struct macio_chip *chip)
347{
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;
352
353 /* Add a node for the macio bus itself */
354#ifdef CONFIG_PCI
355 if (chip->lbus.pdev) {
356 parent = &chip->lbus.pdev->dev;
357 root_res = &chip->lbus.pdev->resource[0];
358 }
359#endif
360 pnode = of_node_get(chip->of_node);
361 if (pnode == NULL)
362 return;
363
364 /* Add macio itself to hierarchy */
365 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
366 if (rdev == NULL)
367 return;
368 root_res = &rdev->resource[0];
369
370 /* First scan 1st level */
371 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
372 if (!macio_skip_device(np)) {
373 of_node_get(np);
374 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, root_res);
375 if (mdev == NULL)
376 of_node_put(np);
377 else if (strncmp(np->name, "media-bay", 9) == 0)
378 mbdev = mdev;
379 else if (strncmp(np->name, "escc", 4) == 0)
380 sdev = mdev;
381 }
382 }
383
384 /* Add media bay devices if any */
385 if (mbdev)
386 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
387 if (!macio_skip_device(np)) {
388 of_node_get(np);
389 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev,
390 root_res) == NULL)
391 of_node_put(np);
392 }
393 /* Add serial ports if any */
394 if (sdev) {
395 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
396 if (!macio_skip_device(np)) {
397 of_node_get(np);
398 if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL,
399 root_res) == NULL)
400 of_node_put(np);
401 }
402 }
403}
404
405
406/**
407 * macio_register_driver - Registers a new MacIO device driver
408 * @drv: pointer to the driver definition structure
409 */
410int macio_register_driver(struct macio_driver *drv)
411{
412 int count = 0;
413
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;
420
421 /* register with core */
422 count = driver_register(&drv->driver);
423 return count ? count : 1;
424}
425
426/**
427 * macio_unregister_driver - Unregisters a new MacIO device driver
428 * @drv: pointer to the driver definition structure
429 */
430void macio_unregister_driver(struct macio_driver *drv)
431{
432 driver_unregister(&drv->driver);
433}
434
435/**
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
440 *
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
444 * successfully.
445 *
446 * Returns 0 on success, or %EBUSY on error. A warning
447 * message is also printed on failure.
448 */
449int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name)
450{
451 if (macio_resource_len(dev, resource_no) == 0)
452 return 0;
453
454 if (!request_mem_region(macio_resource_start(dev, resource_no),
455 macio_resource_len(dev, resource_no),
456 name))
457 goto err_out;
458
459 return 0;
460
461err_out:
462 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
463 " for device %s\n",
464 resource_no,
465 macio_resource_len(dev, resource_no),
466 macio_resource_start(dev, resource_no),
467 dev->ofdev.dev.bus_id);
468 return -EBUSY;
469}
470
471/**
472 * macio_release_resource - Release an MMIO resource
473 * @dev: pointer to the device holding the resource
474 * @resource_no: resource number to release
475 */
476void macio_release_resource(struct macio_dev *dev, int resource_no)
477{
478 if (macio_resource_len(dev, resource_no) == 0)
479 return;
480 release_mem_region(macio_resource_start(dev, resource_no),
481 macio_resource_len(dev, resource_no));
482}
483
484/**
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.
488 *
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.
492 *
493 * Returns 0 on success, or %EBUSY on error. A warning
494 * message is also printed on failure.
495 */
496int macio_request_resources(struct macio_dev *dev, const char *name)
497{
498 int i;
499
500 for (i = 0; i < dev->n_resources; i++)
501 if (macio_request_resource(dev, i, name))
502 goto err_out;
503 return 0;
504
505err_out:
506 while(--i >= 0)
507 macio_release_resource(dev, i);
508
509 return -EBUSY;
510}
511
512/**
513 * macio_release_resources - Release reserved memory resources
514 * @dev: MacIO device whose resources were previously reserved
515 */
516
517void macio_release_resources(struct macio_dev *dev)
518{
519 int i;
520
521 for (i = 0; i < dev->n_resources; i++)
522 macio_release_resource(dev, i);
523}
524
525
526#ifdef CONFIG_PCI
527
528static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
529{
530 struct device_node* np;
531 struct macio_chip* chip;
532
533 if (ent->vendor != PCI_VENDOR_ID_APPLE)
534 return -ENODEV;
535
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.
540 */
541 np = pci_device_to_OF_node(pdev);
542 if (np == NULL)
543 return -ENODEV;
544
545 /* This assumption is wrong, fix that here for now until I fix the arch */
546 of_node_get(np);
547
548 /* We also assume that pmac_feature will have done a get() on nodes stored
549 * in the macio chips array
550 */
551 chip = macio_find(np, macio_unknown);
552 of_node_put(np);
553 if (chip == NULL)
554 return -ENODEV;
555
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);
562 }
563
564 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
565 chip->name);
566
567 /*
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
572 */
573 if (chip->type == macio_gatwick || chip->type == macio_ohareII)
574 if (macio_chips[0].lbus.pdev == NULL) {
575 macio_on_hold = chip;
576 return 0;
577 }
578
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;
583 }
584
585 return 0;
586}
587
588static void __devexit macio_pci_remove(struct pci_dev* pdev)
589{
590 panic("removing of macio-asic not supported !\n");
591}
592
593/*
594 * MacIO is matched against any Apple ID, it's probe() function
595 * will then decide wether it applies or not
596 */
597static 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,
602
603 }, { /* end: all zeroes */ }
604};
605MODULE_DEVICE_TABLE (pci, pci_ids);
606
607/* pci driver glue; this is a "new style" PCI driver module */
608static struct pci_driver macio_pci_driver = {
609 .name = (char *) "macio",
610 .id_table = pci_ids,
611
612 .probe = macio_pci_probe,
613 .remove = macio_pci_remove,
614};
615
616#endif /* CONFIG_PCI */
617
618static int __init macio_module_init (void)
619{
620#ifdef CONFIG_PCI
621 int rc;
622
623 rc = pci_register_driver(&macio_pci_driver);
624 if (rc)
625 return rc;
626#endif /* CONFIG_PCI */
627 return 0;
628}
629
630module_init(macio_module_init);
631
632EXPORT_SYMBOL(macio_register_driver);
633EXPORT_SYMBOL(macio_unregister_driver);
634EXPORT_SYMBOL(macio_dev_get);
635EXPORT_SYMBOL(macio_dev_put);
636EXPORT_SYMBOL(macio_request_resource);
637EXPORT_SYMBOL(macio_release_resource);
638EXPORT_SYMBOL(macio_request_resources);
639EXPORT_SYMBOL(macio_release_resources);