aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/macintosh/macio_asic.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/macintosh/macio_asic.c')
-rw-r--r--drivers/macintosh/macio_asic.c636
1 files changed, 636 insertions, 0 deletions
diff --git a/drivers/macintosh/macio_asic.c b/drivers/macintosh/macio_asic.c
new file mode 100644
index 000000000000..3a609ecd251b
--- /dev/null
+++ b/drivers/macintosh/macio_asic.c
@@ -0,0 +1,636 @@
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
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);
36 const struct of_match * 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
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;
69 const struct of_match *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
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
109static int macio_device_suspend(struct device *dev, u32 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
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
129struct bus_type macio_bus_type = {
130 .name = "macio",
131 .match = macio_bus_match,
132 .suspend = macio_device_suspend,
133 .resume = macio_device_resume,
134};
135
136static int __init macio_bus_driver_init(void)
137{
138 return bus_register(&macio_bus_type);
139}
140
141postcore_initcall(macio_bus_driver_init);
142
143
144/**
145 * macio_release_dev - free a macio device structure when all users of it are finished.
146 * @dev: device that's been disconnected
147 *
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.
151 */
152static void macio_release_dev(struct device *dev)
153{
154 struct macio_dev *mdev;
155
156 mdev = to_macio_device(dev);
157 kfree(mdev);
158}
159
160/**
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
165 *
166 * If this routine returns non-null, then the resource is completely
167 * skipped.
168 */
169static int macio_resource_quirks(struct device_node *np, struct resource *res, int index)
170{
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;
176 }
177 /* Airport has bogus resource 2 */
178 if (index >= 2 && !strcmp(np->name, "radio"))
179 return 1;
180 /* DBDMAs may have bogus sizes */
181 if ((res->start & 0x0001f000) == 0x00008000) {
182 np->addrs[index].size = 0x100;
183 res->end = res->start + 0xff;
184 }
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"))
188 return 1;
189 /* ESCC has bogus resources >= 3 */
190 if (index >= 3 && !(strcmp(np->name, "ch-a") && strcmp(np->name, "ch-b")))
191 return 1;
192 /* Media bay has too many resources, keep only first one */
193 if (index > 0 && !strcmp(np->name, "media-bay"))
194 return 1;
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;
201 }
202 if (index == 1 && np->addrs[1].size > 0x100) {
203 np->addrs[1].size = 0x100;
204 res->end = res->start + 0xff;
205 }
206 }
207 }
208 return 0;
209}
210
211
212/**
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
217 *
218 * When media-bay is changed to hotswap drivers, this function will
219 * be exposed to the bay driver some way...
220 */
221static 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)
224{
225 struct macio_dev *dev;
226 int i, j;
227 u32 *reg;
228
229 if (np == NULL)
230 return NULL;
231
232 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
233 if (!dev)
234 return NULL;
235 memset(dev, 0, sizeof(*dev));
236
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;
245
246#ifdef DEBUG
247 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
248 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
249#endif
250
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,
254#ifdef CONFIG_PCI
255 pci_resource_start(chip->lbus.pdev, 0),
256#else
257 0, /* NuBus may want to do something better here */
258#endif
259 MAX_NODE_NAME_SIZE, np->name);
260 } else {
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);
264 }
265
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
270 */
271 for (i = j = 0; i < np->n_intrs; i++) {
272 struct resource *res = &dev->interrupt[j];
273
274 if (j >= MACIO_DEV_COUNT_IRQS)
275 break;
276 res->start = np->intrs[i].line;
277 res->flags = IORESOURCE_IO;
278 if (np->intrs[j].sense)
279 res->flags |= IORESOURCE_IRQ_LOWLEVEL;
280 else
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));
285 else
286 j++;
287 }
288 dev->n_interrupts = j;
289 for (i = j = 0; i < np->n_addrs; i++) {
290 struct resource *res = &dev->resource[j];
291
292 if (j >= MACIO_DEV_COUNT_RESOURCES)
293 break;
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));
300 else {
301 j++;
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
305 */
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);
309 }
310 }
311 dev->n_resources = j;
312
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);
316 kfree(dev);
317 return NULL;
318 }
319
320 return dev;
321}
322
323static int macio_skip_device(struct device_node *np)
324{
325 if (strncmp(np->name, "battery", 7) == 0)
326 return 1;
327 if (strncmp(np->name, "escc-legacy", 11) == 0)
328 return 1;
329 return 0;
330}
331
332/**
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
335 *
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.
339 *
340 * For now, childs of media-bay are added now as well. This will
341 * change rsn though.
342 */
343static void macio_pci_add_devices(struct macio_chip *chip)
344{
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;
349
350 /* Add a node for the macio bus itself */
351#ifdef CONFIG_PCI
352 if (chip->lbus.pdev) {
353 parent = &chip->lbus.pdev->dev;
354 root_res = &chip->lbus.pdev->resource[0];
355 }
356#endif
357 pnode = of_node_get(chip->of_node);
358 if (pnode == NULL)
359 return;
360
361 /* Add macio itself to hierarchy */
362 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
363 if (rdev == NULL)
364 return;
365 root_res = &rdev->resource[0];
366
367 /* First scan 1st level */
368 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
369 if (!macio_skip_device(np)) {
370 of_node_get(np);
371 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL, root_res);
372 if (mdev == NULL)
373 of_node_put(np);
374 else if (strncmp(np->name, "media-bay", 9) == 0)
375 mbdev = mdev;
376 else if (strncmp(np->name, "escc", 4) == 0)
377 sdev = mdev;
378 }
379 }
380
381 /* Add media bay devices if any */
382 if (mbdev)
383 for (np = NULL; (np = of_get_next_child(mbdev->ofdev.node, np)) != NULL;)
384 if (!macio_skip_device(np)) {
385 of_node_get(np);
386 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np, mbdev,
387 root_res) == NULL)
388 of_node_put(np);
389 }
390 /* Add serial ports if any */
391 if (sdev) {
392 for (np = NULL; (np = of_get_next_child(sdev->ofdev.node, np)) != NULL;)
393 if (!macio_skip_device(np)) {
394 of_node_get(np);
395 if (macio_add_one_device(chip, &sdev->ofdev.dev, np, NULL,
396 root_res) == NULL)
397 of_node_put(np);
398 }
399 }
400}
401
402
403/**
404 * macio_register_driver - Registers a new MacIO device driver
405 * @drv: pointer to the driver definition structure
406 */
407int macio_register_driver(struct macio_driver *drv)
408{
409 int count = 0;
410
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;
417
418 /* register with core */
419 count = driver_register(&drv->driver);
420 return count ? count : 1;
421}
422
423/**
424 * macio_unregister_driver - Unregisters a new MacIO device driver
425 * @drv: pointer to the driver definition structure
426 */
427void macio_unregister_driver(struct macio_driver *drv)
428{
429 driver_unregister(&drv->driver);
430}
431
432/**
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
437 *
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
441 * successfully.
442 *
443 * Returns 0 on success, or %EBUSY on error. A warning
444 * message is also printed on failure.
445 */
446int macio_request_resource(struct macio_dev *dev, int resource_no, const char *name)
447{
448 if (macio_resource_len(dev, resource_no) == 0)
449 return 0;
450
451 if (!request_mem_region(macio_resource_start(dev, resource_no),
452 macio_resource_len(dev, resource_no),
453 name))
454 goto err_out;
455
456 return 0;
457
458err_out:
459 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
460 " for device %s\n",
461 resource_no,
462 macio_resource_len(dev, resource_no),
463 macio_resource_start(dev, resource_no),
464 dev->ofdev.dev.bus_id);
465 return -EBUSY;
466}
467
468/**
469 * macio_release_resource - Release an MMIO resource
470 * @dev: pointer to the device holding the resource
471 * @resource_no: resource number to release
472 */
473void macio_release_resource(struct macio_dev *dev, int resource_no)
474{
475 if (macio_resource_len(dev, resource_no) == 0)
476 return;
477 release_mem_region(macio_resource_start(dev, resource_no),
478 macio_resource_len(dev, resource_no));
479}
480
481/**
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.
485 *
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.
489 *
490 * Returns 0 on success, or %EBUSY on error. A warning
491 * message is also printed on failure.
492 */
493int macio_request_resources(struct macio_dev *dev, const char *name)
494{
495 int i;
496
497 for (i = 0; i < dev->n_resources; i++)
498 if (macio_request_resource(dev, i, name))
499 goto err_out;
500 return 0;
501
502err_out:
503 while(--i >= 0)
504 macio_release_resource(dev, i);
505
506 return -EBUSY;
507}
508
509/**
510 * macio_release_resources - Release reserved memory resources
511 * @dev: MacIO device whose resources were previously reserved
512 */
513
514void macio_release_resources(struct macio_dev *dev)
515{
516 int i;
517
518 for (i = 0; i < dev->n_resources; i++)
519 macio_release_resource(dev, i);
520}
521
522
523#ifdef CONFIG_PCI
524
525static int __devinit macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
526{
527 struct device_node* np;
528 struct macio_chip* chip;
529
530 if (ent->vendor != PCI_VENDOR_ID_APPLE)
531 return -ENODEV;
532
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.
537 */
538 np = pci_device_to_OF_node(pdev);
539 if (np == NULL)
540 return -ENODEV;
541
542 /* This assumption is wrong, fix that here for now until I fix the arch */
543 of_node_get(np);
544
545 /* We also assume that pmac_feature will have done a get() on nodes stored
546 * in the macio chips array
547 */
548 chip = macio_find(np, macio_unknown);
549 of_node_put(np);
550 if (chip == NULL)
551 return -ENODEV;
552
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);
559 }
560
561 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
562 chip->name);
563
564 /*
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
569 */
570 if (chip->type == macio_gatwick || chip->type == macio_ohareII)
571 if (macio_chips[0].lbus.pdev == NULL) {
572 macio_on_hold = chip;
573 return 0;
574 }
575
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;
580 }
581
582 return 0;
583}
584
585static void __devexit macio_pci_remove(struct pci_dev* pdev)
586{
587 panic("removing of macio-asic not supported !\n");
588}
589
590/*
591 * MacIO is matched against any Apple ID, it's probe() function
592 * will then decide wether it applies or not
593 */
594static 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,
599
600 }, { /* end: all zeroes */ }
601};
602MODULE_DEVICE_TABLE (pci, pci_ids);
603
604/* pci driver glue; this is a "new style" PCI driver module */
605static struct pci_driver macio_pci_driver = {
606 .name = (char *) "macio",
607 .id_table = pci_ids,
608
609 .probe = macio_pci_probe,
610 .remove = macio_pci_remove,
611};
612
613#endif /* CONFIG_PCI */
614
615static int __init macio_module_init (void)
616{
617#ifdef CONFIG_PCI
618 int rc;
619
620 rc = pci_register_driver(&macio_pci_driver);
621 if (rc)
622 return rc;
623#endif /* CONFIG_PCI */
624 return 0;
625}
626
627module_init(macio_module_init);
628
629EXPORT_SYMBOL(macio_register_driver);
630EXPORT_SYMBOL(macio_unregister_driver);
631EXPORT_SYMBOL(macio_dev_get);
632EXPORT_SYMBOL(macio_dev_put);
633EXPORT_SYMBOL(macio_request_resource);
634EXPORT_SYMBOL(macio_release_resource);
635EXPORT_SYMBOL(macio_request_resources);
636EXPORT_SYMBOL(macio_release_resources);