Pull ngam-maule-steiner into release branch
[pandora-kernel.git] / drivers / macintosh / macio_asic.c
1 /*
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
30 static struct macio_chip      *macio_on_hold;
31
32 static 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);
36         const struct of_device_id * matches = macio_drv->match_table;
37
38         if (!matches) 
39                 return 0;
40
41         return of_match_device(matches, &macio_dev->ofdev) != NULL;
42 }
43
44 struct 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
57 void macio_dev_put(struct macio_dev *dev)
58 {
59         if (dev)
60                 put_device(&dev->ofdev.dev);
61 }
62
63
64 static int macio_device_probe(struct device *dev)
65 {
66         int error = -ENODEV;
67         struct macio_driver *drv;
68         struct macio_dev *macio_dev;
69         const struct of_device_id *match;
70
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
88 static 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
100 static 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
109 static int macio_device_suspend(struct device *dev, pm_message_t state)
110 {
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
119 static 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
129 static int macio_hotplug (struct device *dev, char **envp, int num_envp,
130                           char *buffer, int buffer_size)
131 {
132         struct macio_dev * macio_dev;
133         struct of_device * of;
134         char *scratch, *compat;
135         int i = 0;
136         int length = 0;
137         int cplen, seen = 0;
138
139         if (!dev)
140                 return -ENODEV;
141
142         macio_dev = to_macio_device(dev);
143         if (!macio_dev)
144                 return -ENODEV;
145
146         of = &macio_dev->ofdev;
147         scratch = buffer;
148
149         /* stuff we want to pass to /sbin/hotplug */
150         envp[i++] = scratch;
151         length += scnprintf (scratch, buffer_size - length, "OF_NAME=%s",
152                              of->node->name);
153         if ((buffer_size - length <= 0) || (i >= num_envp))
154                 return -ENOMEM;
155         ++length;
156         scratch += length;
157
158         envp[i++] = scratch;
159         length += scnprintf (scratch, buffer_size - length, "OF_TYPE=%s",
160                              of->node->type);
161         if ((buffer_size - length <= 0) || (i >= num_envp))
162                 return -ENOMEM;
163         ++length;
164         scratch += length;
165
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. */
169
170         compat = (char *) get_property(of->node, "compatible", &cplen);
171         while (compat && cplen > 0) {
172                 int l;
173                 envp[i++] = scratch;
174                 length += scnprintf (scratch, buffer_size - length,
175                                      "OF_COMPATIBLE_%d=%s", seen, compat);
176                 if ((buffer_size - length <= 0) || (i >= num_envp))
177                         return -ENOMEM;
178                 length++;
179                 scratch += length;
180                 l = strlen (compat) + 1;
181                 compat += l;
182                 cplen -= l;
183                 seen++;
184         }
185
186         envp[i++] = scratch;
187         length += scnprintf (scratch, buffer_size - length,
188                              "OF_COMPATIBLE_N=%d", seen);
189         if ((buffer_size - length <= 0) || (i >= num_envp))
190                 return -ENOMEM;
191         ++length;
192         scratch += length;
193
194         envp[i] = NULL;
195
196         return 0;
197 }
198
199 extern struct device_attribute macio_dev_attrs[];
200
201 struct bus_type macio_bus_type = {
202        .name    = "macio",
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,
208 };
209
210 static int __init macio_bus_driver_init(void)
211 {
212         return bus_register(&macio_bus_type);
213 }
214
215 postcore_initcall(macio_bus_driver_init);
216
217
218 /**
219  * macio_release_dev - free a macio device structure when all users of it are finished.
220  * @dev: device that's been disconnected
221  *
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.
225  */
226 static void macio_release_dev(struct device *dev)
227 {
228         struct macio_dev *mdev;
229
230         mdev = to_macio_device(dev);
231         kfree(mdev);
232 }
233
234 /**
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
239  *
240  * If this routine returns non-null, then the resource is completely
241  * skipped.
242  */
243 static int macio_resource_quirks(struct device_node *np, struct resource *res, int index)
244 {
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;
250                 }
251                 /* Airport has bogus resource 2 */
252                 if (index >= 2 && !strcmp(np->name, "radio"))
253                         return 1;
254                 /* DBDMAs may have bogus sizes */
255                 if ((res->start & 0x0001f000) == 0x00008000) {
256                         np->addrs[index].size = 0x100;
257                         res->end = res->start + 0xff;
258                 }
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"))
262                         return 1;
263                 /* ESCC has bogus resources >= 3 */
264                 if (index >= 3 && !(strcmp(np->name, "ch-a") && strcmp(np->name, "ch-b")))
265                         return 1;
266                 /* Media bay has too many resources, keep only first one */
267                 if (index > 0 && !strcmp(np->name, "media-bay"))
268                         return 1;
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;
275                         }
276                         if (index == 1 && np->addrs[1].size > 0x100) {
277                                 np->addrs[1].size = 0x100;
278                                 res->end = res->start + 0xff;
279                         }
280                 }
281         }
282         return 0;
283 }
284
285
286 /**
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
291  *
292  * When media-bay is changed to hotswap drivers, this function will
293  * be exposed to the bay driver some way...
294  */
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)
298 {
299         struct macio_dev *dev;
300         int i, j;
301         u32 *reg;
302         
303         if (np == NULL)
304                 return NULL;
305
306         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
307         if (!dev)
308                 return NULL;
309         memset(dev, 0, sizeof(*dev));
310
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;
319
320 #ifdef DEBUG
321         printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
322                dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
323 #endif
324
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,
328 #ifdef CONFIG_PCI
329                         pci_resource_start(chip->lbus.pdev, 0),
330 #else
331                         0, /* NuBus may want to do something better here */
332 #endif
333                         MAX_NODE_NAME_SIZE, np->name);
334         } else {
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);
338         }
339
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
344          */
345         for (i = j = 0; i < np->n_intrs; i++) {
346                 struct resource *res = &dev->interrupt[j];
347
348                 if (j >= MACIO_DEV_COUNT_IRQS)
349                         break;
350                 res->start = np->intrs[i].line;
351                 res->flags = IORESOURCE_IO;
352                 if (np->intrs[j].sense)
353                         res->flags |= IORESOURCE_IRQ_LOWLEVEL;
354                 else
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));
359                 else
360                         j++;
361         }
362         dev->n_interrupts = j;
363         for (i = j = 0; i < np->n_addrs; i++) {
364                 struct resource *res = &dev->resource[j];
365                 
366                 if (j >= MACIO_DEV_COUNT_RESOURCES)
367                         break;
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));
374                 else {
375                         j++;
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
379                          */
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);
383                 }
384         }
385         dev->n_resources = j;
386
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);
390                 kfree(dev);
391                 return NULL;
392         }
393
394         return dev;
395 }
396
397 static int macio_skip_device(struct device_node *np)
398 {
399         if (strncmp(np->name, "battery", 7) == 0)
400                 return 1;
401         if (strncmp(np->name, "escc-legacy", 11) == 0)
402                 return 1;
403         return 0;
404 }
405
406 /**
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
409  * 
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.
413  * 
414  * For now, childs of media-bay are added now as well. This will
415  * change rsn though.
416  */
417 static void macio_pci_add_devices(struct macio_chip *chip)
418 {
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;
423         
424         /* Add a node for the macio bus itself */
425 #ifdef CONFIG_PCI
426         if (chip->lbus.pdev) {
427                 parent = &chip->lbus.pdev->dev;
428                 root_res = &chip->lbus.pdev->resource[0];
429         }
430 #endif
431         pnode = of_node_get(chip->of_node);
432         if (pnode == NULL)
433                 return;
434
435         /* Add macio itself to hierarchy */
436         rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
437         if (rdev == NULL)
438                 return;
439         root_res = &rdev->resource[0];
440
441         /* First scan 1st level */
442         for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
443                 if (!macio_skip_device(np)) {
444                         of_node_get(np);
445                         mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, root_res);
446                         if (mdev == NULL)
447                                 of_node_put(np);
448                         else if (strncmp(np->name, "media-bay", 9) == 0)
449                                 mbdev = mdev;
450                         else if (strncmp(np->name, "escc", 4) == 0)
451                                 sdev = mdev;
452                 }
453         }
454
455         /* Add media bay devices if any */
456         if (mbdev)
457                 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
458                         if (!macio_skip_device(np)) {
459                                 of_node_get(np);
460                                 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev,
461                                                          root_res) == NULL)
462                                         of_node_put(np);
463                         }
464         /* Add serial ports if any */
465         if (sdev) {
466                 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
467                         if (!macio_skip_device(np)) {
468                                 of_node_get(np);
469                                 if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL,
470                                                          root_res) == NULL)
471                                         of_node_put(np);
472                         }
473         }
474 }
475
476
477 /**
478  * macio_register_driver - Registers a new MacIO device driver
479  * @drv: pointer to the driver definition structure
480  */
481 int macio_register_driver(struct macio_driver *drv)
482 {
483         int count = 0;
484
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;
491
492         /* register with core */
493         count = driver_register(&drv->driver);
494         return count ? count : 1;
495 }
496
497 /**
498  * macio_unregister_driver - Unregisters a new MacIO device driver
499  * @drv: pointer to the driver definition structure
500  */
501 void macio_unregister_driver(struct macio_driver *drv)
502 {
503         driver_unregister(&drv->driver);
504 }
505
506 /**
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
511  *
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
515  *      successfully.
516  *
517  *      Returns 0 on success, or %EBUSY on error.  A warning
518  *      message is also printed on failure.
519  */
520 int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name)
521 {
522         if (macio_resource_len(dev, resource_no) == 0)
523                 return 0;
524                 
525         if (!request_mem_region(macio_resource_start(dev, resource_no),
526                                 macio_resource_len(dev, resource_no),
527                                 name))
528                 goto err_out;
529         
530         return 0;
531
532 err_out:
533         printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
534                 " for device %s\n",
535                 resource_no,
536                 macio_resource_len(dev, resource_no),
537                 macio_resource_start(dev, resource_no),
538                 dev->ofdev.dev.bus_id);
539         return -EBUSY;
540 }
541
542 /**
543  * macio_release_resource - Release an MMIO resource
544  * @dev: pointer to the device holding the resource
545  * @resource_no: resource number to release
546  */
547 void macio_release_resource(struct macio_dev *dev, int resource_no)
548 {
549         if (macio_resource_len(dev, resource_no) == 0)
550                 return;
551         release_mem_region(macio_resource_start(dev, resource_no),
552                            macio_resource_len(dev, resource_no));
553 }
554
555 /**
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.
559  *
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.
563  *
564  *      Returns 0 on success, or %EBUSY on error.  A warning
565  *      message is also printed on failure.
566  */
567 int macio_request_resources(struct macio_dev *dev, const char *name)
568 {
569         int i;
570         
571         for (i = 0; i < dev->n_resources; i++)
572                 if (macio_request_resource(dev, i, name))
573                         goto err_out;
574         return 0;
575
576 err_out:
577         while(--i >= 0)
578                 macio_release_resource(dev, i);
579                 
580         return -EBUSY;
581 }
582
583 /**
584  *      macio_release_resources - Release reserved memory resources
585  *      @dev: MacIO device whose resources were previously reserved
586  */
587
588 void macio_release_resources(struct macio_dev *dev)
589 {
590         int i;
591         
592         for (i = 0; i < dev->n_resources; i++)
593                 macio_release_resource(dev, i);
594 }
595
596
597 #ifdef CONFIG_PCI
598
599 static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
600 {
601         struct device_node* np;
602         struct macio_chip* chip;
603         
604         if (ent->vendor != PCI_VENDOR_ID_APPLE)
605                 return -ENODEV;
606
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.
611          */
612         np = pci_device_to_OF_node(pdev);
613         if (np == NULL)
614                 return -ENODEV;
615
616         /* This assumption is wrong, fix that here for now until I fix the arch */
617         of_node_get(np);
618
619         /* We also assume that pmac_feature will have done a get() on nodes stored
620          * in the macio chips array
621          */
622         chip = macio_find(np, macio_unknown);
623         of_node_put(np);
624         if (chip == NULL)
625                 return -ENODEV;
626
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);
633         }
634
635         printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
636                 chip->name);
637
638         /*
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
643          */
644         if (chip->type == macio_gatwick || chip->type == macio_ohareII)
645                 if (macio_chips[0].lbus.pdev == NULL) {
646                         macio_on_hold = chip;
647                         return 0;
648                 }
649
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;
654         }
655
656         return 0;
657 }
658
659 static void __devexit macio_pci_remove(struct pci_dev* pdev)
660 {
661         panic("removing of macio-asic not supported !\n");
662 }
663
664 /*
665  * MacIO is matched against any Apple ID, it's probe() function
666  * will then decide wether it applies or not
667  */
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,
673
674         }, { /* end: all zeroes */ }
675 };
676 MODULE_DEVICE_TABLE (pci, pci_ids);
677
678 /* pci driver glue; this is a "new style" PCI driver module */
679 static struct pci_driver macio_pci_driver = {
680         .name           = (char *) "macio",
681         .id_table       = pci_ids,
682
683         .probe          = macio_pci_probe,
684         .remove         = macio_pci_remove,
685 };
686
687 #endif /* CONFIG_PCI */
688
689 static int __init macio_module_init (void) 
690 {
691 #ifdef CONFIG_PCI
692         int rc;
693
694         rc = pci_register_driver(&macio_pci_driver);
695         if (rc)
696                 return rc;
697 #endif /* CONFIG_PCI */
698         return 0;
699 }
700
701 module_init(macio_module_init);
702
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);