1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/string.h>
21#include <linux/kernel.h>
22#include <linux/pci.h>
23#include <linux/pci_ids.h>
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/slab.h>
27
28#include <asm/machdep.h>
29#include <asm/macio.h>
30#include <asm/pmac_feature.h>
31#include <asm/prom.h>
32
33#undef DEBUG
34
35#define MAX_NODE_NAME_SIZE (20 - 12)
36
37static struct macio_chip *macio_on_hold;
38
39static int macio_bus_match(struct device *dev, struct device_driver *drv)
40{
41 const struct of_device_id * matches = drv->of_match_table;
42
43 if (!matches)
44 return 0;
45
46 return of_match_device(matches, dev) != NULL;
47}
48
49struct macio_dev *macio_dev_get(struct macio_dev *dev)
50{
51 struct device *tmp;
52
53 if (!dev)
54 return NULL;
55 tmp = get_device(&dev->ofdev.dev);
56 if (tmp)
57 return to_macio_device(tmp);
58 else
59 return NULL;
60}
61
62void macio_dev_put(struct macio_dev *dev)
63{
64 if (dev)
65 put_device(&dev->ofdev.dev);
66}
67
68
69static int macio_device_probe(struct device *dev)
70{
71 int error = -ENODEV;
72 struct macio_driver *drv;
73 struct macio_dev *macio_dev;
74 const struct of_device_id *match;
75
76 drv = to_macio_driver(dev->driver);
77 macio_dev = to_macio_device(dev);
78
79 if (!drv->probe)
80 return error;
81
82 macio_dev_get(macio_dev);
83
84 match = of_match_device(drv->driver.of_match_table, dev);
85 if (match)
86 error = drv->probe(macio_dev, match);
87 if (error)
88 macio_dev_put(macio_dev);
89
90 return error;
91}
92
93static int macio_device_remove(struct device *dev)
94{
95 struct macio_dev * macio_dev = to_macio_device(dev);
96 struct macio_driver * drv = to_macio_driver(dev->driver);
97
98 if (dev->driver && drv->remove)
99 drv->remove(macio_dev);
100 macio_dev_put(macio_dev);
101
102 return 0;
103}
104
105static void macio_device_shutdown(struct device *dev)
106{
107 struct macio_dev * macio_dev = to_macio_device(dev);
108 struct macio_driver * drv = to_macio_driver(dev->driver);
109
110 if (dev->driver && drv->shutdown)
111 drv->shutdown(macio_dev);
112}
113
114static int macio_device_suspend(struct device *dev, pm_message_t state)
115{
116 struct macio_dev * macio_dev = to_macio_device(dev);
117 struct macio_driver * drv = to_macio_driver(dev->driver);
118
119 if (dev->driver && drv->suspend)
120 return drv->suspend(macio_dev, state);
121 return 0;
122}
123
124static int macio_device_resume(struct device * dev)
125{
126 struct macio_dev * macio_dev = to_macio_device(dev);
127 struct macio_driver * drv = to_macio_driver(dev->driver);
128
129 if (dev->driver && drv->resume)
130 return drv->resume(macio_dev);
131 return 0;
132}
133
134extern struct device_attribute macio_dev_attrs[];
135
136struct bus_type macio_bus_type = {
137 .name = "macio",
138 .match = macio_bus_match,
139 .uevent = of_device_uevent_modalias,
140 .probe = macio_device_probe,
141 .remove = macio_device_remove,
142 .shutdown = macio_device_shutdown,
143 .suspend = macio_device_suspend,
144 .resume = macio_device_resume,
145 .dev_attrs = macio_dev_attrs,
146};
147
148static int __init macio_bus_driver_init(void)
149{
150 return bus_register(&macio_bus_type);
151}
152
153postcore_initcall(macio_bus_driver_init);
154
155
156
157
158
159
160
161
162
163
164
165
166static void macio_release_dev(struct device *dev)
167{
168 struct macio_dev *mdev;
169
170 mdev = to_macio_device(dev);
171 kfree(mdev);
172}
173
174
175
176
177
178
179
180
181
182
183static int macio_resource_quirks(struct device_node *np, struct resource *res,
184 int index)
185{
186
187 if ((res->flags & IORESOURCE_MEM) == 0)
188 return 0;
189
190
191 if (index == 0 && !strcmp(np->name, "gc"))
192 res->end = res->start + 0x1ffff;
193
194
195 if (index >= 2 && !strcmp(np->name, "radio"))
196 return 1;
197
198#ifndef CONFIG_PPC64
199
200 if ((res->start & 0x0001f000) == 0x00008000)
201 res->end = res->start + 0xff;
202#endif
203
204
205
206
207
208 if (!strcmp(np->name, "escc"))
209 return 1;
210
211
212 if (index >= 3 && !(strcmp(np->name, "ch-a") &&
213 strcmp(np->name, "ch-b")))
214 return 1;
215
216
217 if (index > 0 && !strcmp(np->name, "media-bay"))
218 return 1;
219
220
221 if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&
222 strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {
223 if (index == 0 && (res->end - res->start) > 0xfff)
224 res->end = res->start + 0xfff;
225 if (index == 1 && (res->end - res->start) > 0xff)
226 res->end = res->start + 0xff;
227 }
228 return 0;
229}
230
231static void macio_create_fixup_irq(struct macio_dev *dev, int index,
232 unsigned int line)
233{
234 unsigned int irq;
235
236 irq = irq_create_mapping(NULL, line);
237 if (irq != NO_IRQ) {
238 dev->interrupt[index].start = irq;
239 dev->interrupt[index].flags = IORESOURCE_IRQ;
240 dev->interrupt[index].name = dev_name(&dev->ofdev.dev);
241 }
242 if (dev->n_interrupts <= index)
243 dev->n_interrupts = index + 1;
244}
245
246static void macio_add_missing_resources(struct macio_dev *dev)
247{
248 struct device_node *np = dev->ofdev.dev.of_node;
249 unsigned int irq_base;
250
251
252 if (dev->bus->chip->type != macio_gatwick)
253 return;
254
255
256
257
258 irq_base = 64;
259
260
261 if (strcmp(np->name, "ch-a") == 0) {
262 macio_create_fixup_irq(dev, 0, 15 + irq_base);
263 macio_create_fixup_irq(dev, 1, 4 + irq_base);
264 macio_create_fixup_irq(dev, 2, 5 + irq_base);
265 printk(KERN_INFO "macio: fixed SCC irqs on gatwick\n");
266 }
267
268
269 if (strcmp(np->name, "media-bay") == 0) {
270 macio_create_fixup_irq(dev, 0, 29 + irq_base);
271 printk(KERN_INFO "macio: fixed media-bay irq on gatwick\n");
272 }
273
274
275 if (dev->media_bay != NULL && strcmp(np->name, "floppy") == 0) {
276 macio_create_fixup_irq(dev, 0, 19 + irq_base);
277 macio_create_fixup_irq(dev, 1, 1 + irq_base);
278 printk(KERN_INFO "macio: fixed left floppy irqs\n");
279 }
280 if (dev->media_bay != NULL && strcasecmp(np->name, "ata4") == 0) {
281 macio_create_fixup_irq(dev, 0, 14 + irq_base);
282 macio_create_fixup_irq(dev, 0, 3 + irq_base);
283 printk(KERN_INFO "macio: fixed left ide irqs\n");
284 }
285}
286
287static void macio_setup_interrupts(struct macio_dev *dev)
288{
289 struct device_node *np = dev->ofdev.dev.of_node;
290 unsigned int irq;
291 int i = 0, j = 0;
292
293 for (;;) {
294 struct resource *res;
295
296 if (j >= MACIO_DEV_COUNT_IRQS)
297 break;
298 res = &dev->interrupt[j];
299 irq = irq_of_parse_and_map(np, i++);
300 if (irq == NO_IRQ)
301 break;
302 res->start = irq;
303 res->flags = IORESOURCE_IRQ;
304 res->name = dev_name(&dev->ofdev.dev);
305 if (macio_resource_quirks(np, res, i - 1)) {
306 memset(res, 0, sizeof(struct resource));
307 continue;
308 } else
309 j++;
310 }
311 dev->n_interrupts = j;
312}
313
314static void macio_setup_resources(struct macio_dev *dev,
315 struct resource *parent_res)
316{
317 struct device_node *np = dev->ofdev.dev.of_node;
318 struct resource r;
319 int index;
320
321 for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) {
322 struct resource *res;
323 if (index >= MACIO_DEV_COUNT_RESOURCES)
324 break;
325 res = &dev->resource[index];
326 *res = r;
327 res->name = dev_name(&dev->ofdev.dev);
328
329 if (macio_resource_quirks(np, res, index)) {
330 memset(res, 0, sizeof(struct resource));
331 continue;
332 }
333
334
335
336
337 if (insert_resource(parent_res, res)) {
338 printk(KERN_WARNING "Can't request resource "
339 "%d for MacIO device %s\n",
340 index, dev_name(&dev->ofdev.dev));
341 }
342 }
343 dev->n_resources = index;
344}
345
346
347
348
349
350
351
352
353
354
355static struct macio_dev * macio_add_one_device(struct macio_chip *chip,
356 struct device *parent,
357 struct device_node *np,
358 struct macio_dev *in_bay,
359 struct resource *parent_res)
360{
361 struct macio_dev *dev;
362 const u32 *reg;
363
364 if (np == NULL)
365 return NULL;
366
367 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
368 if (!dev)
369 return NULL;
370
371 dev->bus = &chip->lbus;
372 dev->media_bay = in_bay;
373 dev->ofdev.dev.of_node = np;
374 dev->ofdev.archdata.dma_mask = 0xffffffffUL;
375 dev->ofdev.dev.dma_mask = &dev->ofdev.archdata.dma_mask;
376 dev->ofdev.dev.parent = parent;
377 dev->ofdev.dev.bus = &macio_bus_type;
378 dev->ofdev.dev.release = macio_release_dev;
379 dev->ofdev.dev.dma_parms = &dev->dma_parms;
380
381
382 dma_set_max_seg_size(&dev->ofdev.dev, 65536);
383 dma_set_seg_boundary(&dev->ofdev.dev, 0xffffffff);
384
385#ifdef CONFIG_PCI
386
387
388
389
390
391
392 dev->ofdev.dev.archdata = chip->lbus.pdev->dev.archdata;
393#endif
394
395#ifdef DEBUG
396 printk("preparing mdev @%p, ofdev @%p, dev @%p, kobj @%p\n",
397 dev, &dev->ofdev, &dev->ofdev.dev, &dev->ofdev.dev.kobj);
398#endif
399
400
401 if (np == chip->of_node) {
402 dev_set_name(&dev->ofdev.dev, "%1d.%08x:%.*s",
403 chip->lbus.index,
404#ifdef CONFIG_PCI
405 (unsigned int)pci_resource_start(chip->lbus.pdev, 0),
406#else
407 0,
408#endif
409 MAX_NODE_NAME_SIZE, np->name);
410 } else {
411 reg = of_get_property(np, "reg", NULL);
412 dev_set_name(&dev->ofdev.dev, "%1d.%08x:%.*s",
413 chip->lbus.index,
414 reg ? *reg : 0, MAX_NODE_NAME_SIZE, np->name);
415 }
416
417
418 macio_setup_interrupts(dev);
419 macio_setup_resources(dev, parent_res);
420 macio_add_missing_resources(dev);
421
422
423 if (of_device_register(&dev->ofdev) != 0) {
424 printk(KERN_DEBUG"macio: device registration error for %s!\n",
425 dev_name(&dev->ofdev.dev));
426 kfree(dev);
427 return NULL;
428 }
429
430 return dev;
431}
432
433static int macio_skip_device(struct device_node *np)
434{
435 if (strncmp(np->name, "battery", 7) == 0)
436 return 1;
437 if (strncmp(np->name, "escc-legacy", 11) == 0)
438 return 1;
439 return 0;
440}
441
442
443
444
445
446
447
448
449
450
451
452
453static void macio_pci_add_devices(struct macio_chip *chip)
454{
455 struct device_node *np, *pnode;
456 struct macio_dev *rdev, *mdev, *mbdev = NULL, *sdev = NULL;
457 struct device *parent = NULL;
458 struct resource *root_res = &iomem_resource;
459
460
461#ifdef CONFIG_PCI
462 if (chip->lbus.pdev) {
463 parent = &chip->lbus.pdev->dev;
464 root_res = &chip->lbus.pdev->resource[0];
465 }
466#endif
467 pnode = of_node_get(chip->of_node);
468 if (pnode == NULL)
469 return;
470
471
472 rdev = macio_add_one_device(chip, parent, pnode, NULL, root_res);
473 if (rdev == NULL)
474 return;
475 root_res = &rdev->resource[0];
476
477
478 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
479 if (macio_skip_device(np))
480 continue;
481 of_node_get(np);
482 mdev = macio_add_one_device(chip, &rdev->ofdev.dev, np, NULL,
483 root_res);
484 if (mdev == NULL)
485 of_node_put(np);
486 else if (strncmp(np->name, "media-bay", 9) == 0)
487 mbdev = mdev;
488 else if (strncmp(np->name, "escc", 4) == 0)
489 sdev = mdev;
490 }
491
492
493 if (mbdev) {
494 pnode = mbdev->ofdev.dev.of_node;
495 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
496 if (macio_skip_device(np))
497 continue;
498 of_node_get(np);
499 if (macio_add_one_device(chip, &mbdev->ofdev.dev, np,
500 mbdev, root_res) == NULL)
501 of_node_put(np);
502 }
503 }
504
505
506 if (sdev) {
507 pnode = sdev->ofdev.dev.of_node;
508 for (np = NULL; (np = of_get_next_child(pnode, np)) != NULL;) {
509 if (macio_skip_device(np))
510 continue;
511 of_node_get(np);
512 if (macio_add_one_device(chip, &sdev->ofdev.dev, np,
513 NULL, root_res) == NULL)
514 of_node_put(np);
515 }
516 }
517}
518
519
520
521
522
523
524int macio_register_driver(struct macio_driver *drv)
525{
526
527 drv->driver.bus = &macio_bus_type;
528
529
530 return driver_register(&drv->driver);
531}
532
533
534
535
536
537void macio_unregister_driver(struct macio_driver *drv)
538{
539 driver_unregister(&drv->driver);
540}
541
542
543struct macio_devres {
544 u32 res_mask;
545};
546
547static void maciom_release(struct device *gendev, void *res)
548{
549 struct macio_dev *dev = to_macio_device(gendev);
550 struct macio_devres *dr = res;
551 int i, max;
552
553 max = min(dev->n_resources, 32);
554 for (i = 0; i < max; i++) {
555 if (dr->res_mask & (1 << i))
556 macio_release_resource(dev, i);
557 }
558}
559
560int macio_enable_devres(struct macio_dev *dev)
561{
562 struct macio_devres *dr;
563
564 dr = devres_find(&dev->ofdev.dev, maciom_release, NULL, NULL);
565 if (!dr) {
566 dr = devres_alloc(maciom_release, sizeof(*dr), GFP_KERNEL);
567 if (!dr)
568 return -ENOMEM;
569 }
570 return devres_get(&dev->ofdev.dev, dr, NULL, NULL) != NULL;
571}
572
573static struct macio_devres * find_macio_dr(struct macio_dev *dev)
574{
575 return devres_find(&dev->ofdev.dev, maciom_release, NULL, NULL);
576}
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592int macio_request_resource(struct macio_dev *dev, int resource_no,
593 const char *name)
594{
595 struct macio_devres *dr = find_macio_dr(dev);
596
597 if (macio_resource_len(dev, resource_no) == 0)
598 return 0;
599
600 if (!request_mem_region(macio_resource_start(dev, resource_no),
601 macio_resource_len(dev, resource_no),
602 name))
603 goto err_out;
604
605 if (dr && resource_no < 32)
606 dr->res_mask |= 1 << resource_no;
607
608 return 0;
609
610err_out:
611 printk (KERN_WARNING "MacIO: Unable to reserve resource #%d:%lx@%lx"
612 " for device %s\n",
613 resource_no,
614 macio_resource_len(dev, resource_no),
615 macio_resource_start(dev, resource_no),
616 dev_name(&dev->ofdev.dev));
617 return -EBUSY;
618}
619
620
621
622
623
624
625void macio_release_resource(struct macio_dev *dev, int resource_no)
626{
627 struct macio_devres *dr = find_macio_dr(dev);
628
629 if (macio_resource_len(dev, resource_no) == 0)
630 return;
631 release_mem_region(macio_resource_start(dev, resource_no),
632 macio_resource_len(dev, resource_no));
633 if (dr && resource_no < 32)
634 dr->res_mask &= ~(1 << resource_no);
635}
636
637
638
639
640
641
642
643
644
645
646
647
648
649int macio_request_resources(struct macio_dev *dev, const char *name)
650{
651 int i;
652
653 for (i = 0; i < dev->n_resources; i++)
654 if (macio_request_resource(dev, i, name))
655 goto err_out;
656 return 0;
657
658err_out:
659 while(--i >= 0)
660 macio_release_resource(dev, i);
661
662 return -EBUSY;
663}
664
665
666
667
668
669
670void macio_release_resources(struct macio_dev *dev)
671{
672 int i;
673
674 for (i = 0; i < dev->n_resources; i++)
675 macio_release_resource(dev, i);
676}
677
678
679#ifdef CONFIG_PCI
680
681static int macio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
682{
683 struct device_node* np;
684 struct macio_chip* chip;
685
686 if (ent->vendor != PCI_VENDOR_ID_APPLE)
687 return -ENODEV;
688
689
690
691
692 np = pci_device_to_OF_node(pdev);
693 if (np == NULL)
694 return -ENODEV;
695
696
697
698
699 of_node_get(np);
700
701
702
703
704 chip = macio_find(np, macio_unknown);
705 of_node_put(np);
706 if (chip == NULL)
707 return -ENODEV;
708
709
710 if (chip->lbus.pdev == NULL) {
711 chip->lbus.pdev = pdev;
712 chip->lbus.chip = chip;
713 pci_set_drvdata(pdev, &chip->lbus);
714 pci_set_master(pdev);
715 }
716
717 printk(KERN_INFO "MacIO PCI driver attached to %s chipset\n",
718 chip->name);
719
720
721
722
723
724
725
726 if (chip->type == macio_gatwick || chip->type == macio_ohareII)
727 if (macio_chips[0].lbus.pdev == NULL) {
728 macio_on_hold = chip;
729 return 0;
730 }
731
732 macio_pci_add_devices(chip);
733 if (macio_on_hold && macio_chips[0].lbus.pdev != NULL) {
734 macio_pci_add_devices(macio_on_hold);
735 macio_on_hold = NULL;
736 }
737
738 return 0;
739}
740
741static void macio_pci_remove(struct pci_dev* pdev)
742{
743 panic("removing of macio-asic not supported !\n");
744}
745
746
747
748
749
750static const struct pci_device_id pci_ids[] = { {
751 .vendor = PCI_VENDOR_ID_APPLE,
752 .device = PCI_ANY_ID,
753 .subvendor = PCI_ANY_ID,
754 .subdevice = PCI_ANY_ID,
755
756 }, { }
757};
758MODULE_DEVICE_TABLE (pci, pci_ids);
759
760
761static struct pci_driver macio_pci_driver = {
762 .name = (char *) "macio",
763 .id_table = pci_ids,
764
765 .probe = macio_pci_probe,
766 .remove = macio_pci_remove,
767};
768
769#endif
770
771static int __init macio_module_init (void)
772{
773#ifdef CONFIG_PCI
774 int rc;
775
776 rc = pci_register_driver(&macio_pci_driver);
777 if (rc)
778 return rc;
779#endif
780 return 0;
781}
782
783module_init(macio_module_init);
784
785EXPORT_SYMBOL(macio_register_driver);
786EXPORT_SYMBOL(macio_unregister_driver);
787EXPORT_SYMBOL(macio_dev_get);
788EXPORT_SYMBOL(macio_dev_put);
789EXPORT_SYMBOL(macio_request_resource);
790EXPORT_SYMBOL(macio_release_resource);
791EXPORT_SYMBOL(macio_request_resources);
792EXPORT_SYMBOL(macio_release_resources);
793EXPORT_SYMBOL(macio_enable_devres);
794
795