1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/pci.h>
15#include <linux/delay.h>
16#include <linux/string.h>
17#include <linux/init.h>
18#include <linux/irq.h>
19#include <linux/of_pci.h>
20
21#include <asm/sections.h>
22#include <asm/io.h>
23#include <asm/prom.h>
24#include <asm/pci-bridge.h>
25#include <asm/machdep.h>
26#include <asm/pmac_feature.h>
27#include <asm/grackle.h>
28#include <asm/ppc-pci.h>
29
30#include "pmac.h"
31
32#undef DEBUG
33
34#ifdef DEBUG
35#define DBG(x...) printk(x)
36#else
37#define DBG(x...)
38#endif
39
40
41
42static int has_uninorth;
43#ifdef CONFIG_PPC64
44static struct pci_controller *u3_agp;
45#else
46static int has_second_ohare;
47#endif
48
49extern int pcibios_assign_bus_offset;
50
51struct device_node *k2_skiplist[2];
52
53
54
55
56#define BANDIT_DEVID_2 8
57#define BANDIT_REVID 3
58
59#define BANDIT_DEVNUM 11
60#define BANDIT_MAGIC 0x50
61#define BANDIT_COHERENT 0x40
62
63static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
64{
65 for (; node != 0;node = node->sibling) {
66 const int * bus_range;
67 const unsigned int *class_code;
68 int len;
69
70
71 class_code = of_get_property(node, "class-code", NULL);
72 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
73 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
74 continue;
75 bus_range = of_get_property(node, "bus-range", &len);
76 if (bus_range != NULL && len > 2 * sizeof(int)) {
77 if (bus_range[1] > higher)
78 higher = bus_range[1];
79 }
80 higher = fixup_one_level_bus_range(node->child, higher);
81 }
82 return higher;
83}
84
85
86
87
88
89
90
91static void __init fixup_bus_range(struct device_node *bridge)
92{
93 int *bus_range, len;
94 struct property *prop;
95
96
97 prop = of_find_property(bridge, "bus-range", &len);
98 if (prop == NULL || prop->length < 2 * sizeof(int))
99 return;
100
101 bus_range = prop->value;
102 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
103}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127#define MACRISC_CFA0(devfn, off) \
128 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
129 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
130 | (((unsigned int)(off)) & 0xFCUL))
131
132#define MACRISC_CFA1(bus, devfn, off) \
133 ((((unsigned int)(bus)) << 16) \
134 |(((unsigned int)(devfn)) << 8) \
135 |(((unsigned int)(off)) & 0xFCUL) \
136 |1UL)
137
138static void __iomem *macrisc_cfg_map_bus(struct pci_bus *bus,
139 unsigned int dev_fn,
140 int offset)
141{
142 unsigned int caddr;
143 struct pci_controller *hose;
144
145 hose = pci_bus_to_host(bus);
146 if (hose == NULL)
147 return NULL;
148
149 if (bus->number == hose->first_busno) {
150 if (dev_fn < (11 << 3))
151 return NULL;
152 caddr = MACRISC_CFA0(dev_fn, offset);
153 } else
154 caddr = MACRISC_CFA1(bus->number, dev_fn, offset);
155
156
157 do {
158 out_le32(hose->cfg_addr, caddr);
159 } while (in_le32(hose->cfg_addr) != caddr);
160
161 offset &= has_uninorth ? 0x07 : 0x03;
162 return hose->cfg_data + offset;
163}
164
165static struct pci_ops macrisc_pci_ops =
166{
167 .map_bus = macrisc_cfg_map_bus,
168 .read = pci_generic_config_read,
169 .write = pci_generic_config_write,
170};
171
172#ifdef CONFIG_PPC32
173
174
175
176static void __iomem *chaos_map_bus(struct pci_bus *bus, unsigned int devfn,
177 int offset)
178{
179 struct device_node *np;
180 const u32 *vendor, *device;
181
182 if (offset >= 0x100)
183 return NULL;
184 np = of_pci_find_child_device(bus->dev.of_node, devfn);
185 if (np == NULL)
186 return NULL;
187
188 vendor = of_get_property(np, "vendor-id", NULL);
189 device = of_get_property(np, "device-id", NULL);
190 if (vendor == NULL || device == NULL)
191 return NULL;
192
193 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
194 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
195 return NULL;
196
197 return macrisc_cfg_map_bus(bus, devfn, offset);
198}
199
200static struct pci_ops chaos_pci_ops =
201{
202 .map_bus = chaos_map_bus,
203 .read = pci_generic_config_read,
204 .write = pci_generic_config_write,
205};
206
207static void __init setup_chaos(struct pci_controller *hose,
208 struct resource *addr)
209{
210
211 hose->ops = &chaos_pci_ops;
212 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
213 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
214}
215#endif
216
217#ifdef CONFIG_PPC64
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232static int u3_ht_skip_device(struct pci_controller *hose,
233 struct pci_bus *bus, unsigned int devfn)
234{
235 struct device_node *busdn, *dn;
236 int i;
237
238
239
240
241
242
243 if (bus->self)
244 busdn = pci_device_to_OF_node(bus->self);
245 else if (devfn == 0)
246 return 0;
247 else
248 busdn = hose->dn;
249 for (dn = busdn->child; dn; dn = dn->sibling)
250 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
251 break;
252 if (dn == NULL)
253 return -1;
254
255
256
257
258
259 for (i=0; i<2; i++)
260 if (k2_skiplist[i] == dn)
261 return 1;
262
263 return 0;
264}
265
266#define U3_HT_CFA0(devfn, off) \
267 ((((unsigned int)devfn) << 8) | offset)
268#define U3_HT_CFA1(bus, devfn, off) \
269 (U3_HT_CFA0(devfn, off) \
270 + (((unsigned int)bus) << 16) \
271 + 0x01000000UL)
272
273static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
274 u8 devfn, u8 offset, int *swap)
275{
276 *swap = 1;
277 if (bus == hose->first_busno) {
278 if (devfn != 0)
279 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
280 *swap = 0;
281 return ((void __iomem *)hose->cfg_addr) + (offset << 2);
282 } else
283 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
284}
285
286static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
287 int offset, int len, u32 *val)
288{
289 struct pci_controller *hose;
290 void __iomem *addr;
291 int swap;
292
293 hose = pci_bus_to_host(bus);
294 if (hose == NULL)
295 return PCIBIOS_DEVICE_NOT_FOUND;
296 if (offset >= 0x100)
297 return PCIBIOS_BAD_REGISTER_NUMBER;
298 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
299 if (!addr)
300 return PCIBIOS_DEVICE_NOT_FOUND;
301
302 switch (u3_ht_skip_device(hose, bus, devfn)) {
303 case 0:
304 break;
305 case 1:
306 switch (len) {
307 case 1:
308 *val = 0xff; break;
309 case 2:
310 *val = 0xffff; break;
311 default:
312 *val = 0xfffffffful; break;
313 }
314 return PCIBIOS_SUCCESSFUL;
315 default:
316 return PCIBIOS_DEVICE_NOT_FOUND;
317 }
318
319
320
321
322
323 switch (len) {
324 case 1:
325 *val = in_8(addr);
326 break;
327 case 2:
328 *val = swap ? in_le16(addr) : in_be16(addr);
329 break;
330 default:
331 *val = swap ? in_le32(addr) : in_be32(addr);
332 break;
333 }
334 return PCIBIOS_SUCCESSFUL;
335}
336
337static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
338 int offset, int len, u32 val)
339{
340 struct pci_controller *hose;
341 void __iomem *addr;
342 int swap;
343
344 hose = pci_bus_to_host(bus);
345 if (hose == NULL)
346 return PCIBIOS_DEVICE_NOT_FOUND;
347 if (offset >= 0x100)
348 return PCIBIOS_BAD_REGISTER_NUMBER;
349 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
350 if (!addr)
351 return PCIBIOS_DEVICE_NOT_FOUND;
352
353 switch (u3_ht_skip_device(hose, bus, devfn)) {
354 case 0:
355 break;
356 case 1:
357 return PCIBIOS_SUCCESSFUL;
358 default:
359 return PCIBIOS_DEVICE_NOT_FOUND;
360 }
361
362
363
364
365
366 switch (len) {
367 case 1:
368 out_8(addr, val);
369 break;
370 case 2:
371 swap ? out_le16(addr, val) : out_be16(addr, val);
372 break;
373 default:
374 swap ? out_le32(addr, val) : out_be32(addr, val);
375 break;
376 }
377 return PCIBIOS_SUCCESSFUL;
378}
379
380static struct pci_ops u3_ht_pci_ops =
381{
382 .read = u3_ht_read_config,
383 .write = u3_ht_write_config,
384};
385
386#define U4_PCIE_CFA0(devfn, off) \
387 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
388 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
389 | ((((unsigned int)(off)) >> 8) << 28) \
390 | (((unsigned int)(off)) & 0xfcU))
391
392#define U4_PCIE_CFA1(bus, devfn, off) \
393 ((((unsigned int)(bus)) << 16) \
394 |(((unsigned int)(devfn)) << 8) \
395 | ((((unsigned int)(off)) >> 8) << 28) \
396 |(((unsigned int)(off)) & 0xfcU) \
397 |1UL)
398
399static void __iomem *u4_pcie_cfg_map_bus(struct pci_bus *bus,
400 unsigned int dev_fn,
401 int offset)
402{
403 struct pci_controller *hose;
404 unsigned int caddr;
405
406 if (offset >= 0x1000)
407 return NULL;
408
409 hose = pci_bus_to_host(bus);
410 if (!hose)
411 return NULL;
412
413 if (bus->number == hose->first_busno) {
414 caddr = U4_PCIE_CFA0(dev_fn, offset);
415 } else
416 caddr = U4_PCIE_CFA1(bus->number, dev_fn, offset);
417
418
419 do {
420 out_le32(hose->cfg_addr, caddr);
421 } while (in_le32(hose->cfg_addr) != caddr);
422
423 offset &= 0x03;
424 return hose->cfg_data + offset;
425}
426
427static struct pci_ops u4_pcie_pci_ops =
428{
429 .map_bus = u4_pcie_cfg_map_bus,
430 .read = pci_generic_config_read,
431 .write = pci_generic_config_write,
432};
433
434static void pmac_pci_fixup_u4_of_node(struct pci_dev *dev)
435{
436
437
438
439
440
441
442
443 if (dev->dev.of_node == NULL)
444 dev->dev.of_node = pcibios_get_phb_of_node(dev->bus);
445}
446DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, 0x5b, pmac_pci_fixup_u4_of_node);
447
448#endif
449
450#ifdef CONFIG_PPC32
451
452
453
454
455static void __init init_bandit(struct pci_controller *bp)
456{
457 unsigned int vendev, magic;
458 int rev;
459
460
461 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
462 udelay(2);
463 vendev = in_le32(bp->cfg_data);
464 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
465 PCI_VENDOR_ID_APPLE) {
466
467 out_le32(bp->cfg_addr,
468 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
469 udelay(2);
470 rev = in_8(bp->cfg_data);
471 if (rev != BANDIT_REVID)
472 printk(KERN_WARNING
473 "Unknown revision %d for bandit\n", rev);
474 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
475 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
476 return;
477 }
478
479
480 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
481 udelay(2);
482 magic = in_le32(bp->cfg_data);
483 if ((magic & BANDIT_COHERENT) != 0)
484 return;
485 magic |= BANDIT_COHERENT;
486 udelay(2);
487 out_le32(bp->cfg_data, magic);
488 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
489}
490
491
492
493
494static void __init init_p2pbridge(void)
495{
496 struct device_node *p2pbridge;
497 struct pci_controller* hose;
498 u8 bus, devfn;
499 u16 val;
500
501
502
503 p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
504 if (p2pbridge == NULL
505 || p2pbridge->parent == NULL
506 || strcmp(p2pbridge->parent->name, "pci") != 0)
507 goto done;
508 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
509 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
510 goto done;
511 }
512
513
514
515 hose = pci_find_hose_for_OF_device(p2pbridge);
516 if (!hose) {
517 DBG("Can't find hose for PCI<->PCI bridge\n");
518 goto done;
519 }
520 if (early_read_config_word(hose, bus, devfn,
521 PCI_BRIDGE_CONTROL, &val) < 0) {
522 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
523 " control\n");
524 goto done;
525 }
526 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
527 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
528done:
529 of_node_put(p2pbridge);
530}
531
532static void __init init_second_ohare(void)
533{
534 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
535 unsigned char bus, devfn;
536 unsigned short cmd;
537
538 if (np == NULL)
539 return;
540
541
542
543
544 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
545 struct pci_controller* hose =
546 pci_find_hose_for_OF_device(np);
547 if (!hose) {
548 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
549 of_node_put(np);
550 return;
551 }
552 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
553 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
554 cmd &= ~PCI_COMMAND_IO;
555 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
556 }
557 has_second_ohare = 1;
558 of_node_put(np);
559}
560
561
562
563
564
565
566
567static void __init fixup_nec_usb2(void)
568{
569 struct device_node *nec;
570
571 for_each_node_by_name(nec, "usb") {
572 struct pci_controller *hose;
573 u32 data;
574 const u32 *prop;
575 u8 bus, devfn;
576
577 prop = of_get_property(nec, "vendor-id", NULL);
578 if (prop == NULL)
579 continue;
580 if (0x1033 != *prop)
581 continue;
582 prop = of_get_property(nec, "device-id", NULL);
583 if (prop == NULL)
584 continue;
585 if (0x0035 != *prop)
586 continue;
587 prop = of_get_property(nec, "reg", NULL);
588 if (prop == NULL)
589 continue;
590 devfn = (prop[0] >> 8) & 0xff;
591 bus = (prop[0] >> 16) & 0xff;
592 if (PCI_FUNC(devfn) != 0)
593 continue;
594 hose = pci_find_hose_for_OF_device(nec);
595 if (!hose)
596 continue;
597 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
598 if (data & 1UL) {
599 printk("Found NEC PD720100A USB2 chip with disabled"
600 " EHCI, fixing up...\n");
601 data &= ~1UL;
602 early_write_config_dword(hose, bus, devfn, 0xe4, data);
603 }
604 }
605}
606
607static void __init setup_bandit(struct pci_controller *hose,
608 struct resource *addr)
609{
610 hose->ops = ¯isc_pci_ops;
611 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
612 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
613 init_bandit(hose);
614}
615
616static int __init setup_uninorth(struct pci_controller *hose,
617 struct resource *addr)
618{
619 pci_add_flags(PCI_REASSIGN_ALL_BUS);
620 has_uninorth = 1;
621 hose->ops = ¯isc_pci_ops;
622 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
623 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
624
625 return addr->start == 0xf2000000;
626}
627#endif
628
629#ifdef CONFIG_PPC64
630static void __init setup_u3_agp(struct pci_controller* hose)
631{
632
633
634
635
636
637
638
639
640
641 hose->first_busno = 0xf0;
642 hose->last_busno = 0xff;
643 has_uninorth = 1;
644 hose->ops = ¯isc_pci_ops;
645 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
646 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
647 u3_agp = hose;
648}
649
650static void __init setup_u4_pcie(struct pci_controller* hose)
651{
652
653
654
655 hose->ops = &u4_pcie_pci_ops;
656 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
657 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
658
659
660
661
662
663
664
665 hose->first_busno = 0x00;
666 hose->last_busno = 0xff;
667}
668
669static void __init parse_region_decode(struct pci_controller *hose,
670 u32 decode)
671{
672 unsigned long base, end, next = -1;
673 int i, cur = -1;
674
675
676
677
678 for (i = 0; i < 31; i++) {
679 if ((decode & (0x80000000 >> i)) == 0)
680 continue;
681 if (i < 16) {
682 base = 0xf0000000 | (((u32)i) << 24);
683 end = base + 0x00ffffff;
684 } else {
685 base = ((u32)i-16) << 28;
686 end = base + 0x0fffffff;
687 }
688 if (base != next) {
689 if (++cur >= 3) {
690 printk(KERN_WARNING "PCI: Too many ranges !\n");
691 break;
692 }
693 hose->mem_resources[cur].flags = IORESOURCE_MEM;
694 hose->mem_resources[cur].name = hose->dn->full_name;
695 hose->mem_resources[cur].start = base;
696 hose->mem_resources[cur].end = end;
697 hose->mem_offset[cur] = 0;
698 DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);
699 } else {
700 DBG(" : -0x%08lx\n", end);
701 hose->mem_resources[cur].end = end;
702 }
703 next = end + 1;
704 }
705}
706
707static void __init setup_u3_ht(struct pci_controller* hose)
708{
709 struct device_node *np = hose->dn;
710 struct resource cfg_res, self_res;
711 u32 decode;
712
713 hose->ops = &u3_ht_pci_ops;
714
715
716
717 if (of_address_to_resource(np, 0, &cfg_res) ||
718 of_address_to_resource(np, 1, &self_res)) {
719 printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
720 return;
721 }
722
723
724
725
726 hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
727 hose->cfg_addr = ioremap(self_res.start, resource_size(&self_res));
728
729
730
731
732
733
734 hose->io_base_phys = 0xf4000000;
735 hose->pci_io_size = 0x00400000;
736 hose->io_resource.name = np->full_name;
737 hose->io_resource.start = 0;
738 hose->io_resource.end = 0x003fffff;
739 hose->io_resource.flags = IORESOURCE_IO;
740 hose->first_busno = 0;
741 hose->last_busno = 0xef;
742
743
744 decode = in_be32(hose->cfg_addr + 0x80);
745
746 DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765 decode &= 0x003fffff;
766
767
768 parse_region_decode(hose, decode);
769}
770#endif
771
772
773
774
775
776
777static int __init pmac_add_bridge(struct device_node *dev)
778{
779 int len;
780 struct pci_controller *hose;
781 struct resource rsrc;
782 char *disp_name;
783 const int *bus_range;
784 int primary = 1, has_address = 0;
785
786 DBG("Adding PCI host bridge %pOF\n", dev);
787
788
789 has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
790
791
792 bus_range = of_get_property(dev, "bus-range", &len);
793 if (bus_range == NULL || len < 2 * sizeof(int)) {
794 printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
795 " bus 0\n", dev);
796 }
797
798 hose = pcibios_alloc_controller(dev);
799 if (!hose)
800 return -ENOMEM;
801 hose->first_busno = bus_range ? bus_range[0] : 0;
802 hose->last_busno = bus_range ? bus_range[1] : 0xff;
803 hose->controller_ops = pmac_pci_controller_ops;
804
805 disp_name = NULL;
806
807
808#ifdef CONFIG_PPC64
809 if (of_device_is_compatible(dev, "u3-agp")) {
810 setup_u3_agp(hose);
811 disp_name = "U3-AGP";
812 primary = 0;
813 } else if (of_device_is_compatible(dev, "u3-ht")) {
814 setup_u3_ht(hose);
815 disp_name = "U3-HT";
816 primary = 1;
817 } else if (of_device_is_compatible(dev, "u4-pcie")) {
818 setup_u4_pcie(hose);
819 disp_name = "U4-PCIE";
820 primary = 0;
821 }
822 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
823 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
824#endif
825
826
827#ifdef CONFIG_PPC32
828 if (of_device_is_compatible(dev, "uni-north")) {
829 primary = setup_uninorth(hose, &rsrc);
830 disp_name = "UniNorth";
831 } else if (strcmp(dev->name, "pci") == 0) {
832
833 setup_grackle(hose);
834 disp_name = "Grackle (MPC106)";
835 } else if (strcmp(dev->name, "bandit") == 0) {
836 setup_bandit(hose, &rsrc);
837 disp_name = "Bandit";
838 } else if (strcmp(dev->name, "chaos") == 0) {
839 setup_chaos(hose, &rsrc);
840 disp_name = "Chaos";
841 primary = 0;
842 }
843 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
844 "Firmware bus number: %d->%d\n",
845 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
846 hose->last_busno);
847#endif
848
849 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
850 hose, hose->cfg_addr, hose->cfg_data);
851
852
853
854 pci_process_bridge_OF_ranges(hose, dev, primary);
855
856
857 fixup_bus_range(dev);
858
859 return 0;
860}
861
862void pmac_pci_irq_fixup(struct pci_dev *dev)
863{
864#ifdef CONFIG_PPC32
865
866
867
868
869
870
871
872 if (has_second_ohare &&
873 dev->vendor == PCI_VENDOR_ID_DEC &&
874 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
875 dev->irq = irq_create_mapping(NULL, 60);
876 irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
877 }
878#endif
879}
880
881#ifdef CONFIG_PPC64
882static int pmac_pci_root_bridge_prepare(struct pci_host_bridge *bridge)
883{
884 struct pci_controller *hose = pci_bus_to_host(bridge->bus);
885 struct device_node *np, *child;
886
887 if (hose != u3_agp)
888 return 0;
889
890
891
892
893
894
895 np = hose->dn;
896 PCI_DN(np)->busno = 0xf0;
897 for_each_child_of_node(np, child)
898 PCI_DN(child)->busno = 0xf0;
899
900 return 0;
901}
902#endif
903
904void __init pmac_pci_init(void)
905{
906 struct device_node *np, *root;
907 struct device_node *ht = NULL;
908
909 pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
910
911 root = of_find_node_by_path("/");
912 if (root == NULL) {
913 printk(KERN_CRIT "pmac_pci_init: can't find root "
914 "of device tree\n");
915 return;
916 }
917 for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) {
918 if (np->name == NULL)
919 continue;
920 if (strcmp(np->name, "bandit") == 0
921 || strcmp(np->name, "chaos") == 0
922 || strcmp(np->name, "pci") == 0) {
923 if (pmac_add_bridge(np) == 0)
924 of_node_get(np);
925 }
926 if (strcmp(np->name, "ht") == 0) {
927 of_node_get(np);
928 ht = np;
929 }
930 }
931 of_node_put(root);
932
933#ifdef CONFIG_PPC64
934
935
936
937 if (ht && pmac_add_bridge(ht) != 0)
938 of_node_put(ht);
939
940 ppc_md.pcibios_root_bridge_prepare = pmac_pci_root_bridge_prepare;
941
942
943#else
944 init_p2pbridge();
945 init_second_ohare();
946 fixup_nec_usb2();
947
948
949
950
951
952 if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
953 pcibios_assign_bus_offset = 0x10;
954#endif
955}
956
957#ifdef CONFIG_PPC32
958static bool pmac_pci_enable_device_hook(struct pci_dev *dev)
959{
960 struct device_node* node;
961 int updatecfg = 0;
962 int uninorth_child;
963
964 node = pci_device_to_OF_node(dev);
965
966
967
968
969 if (dev->vendor == PCI_VENDOR_ID_APPLE
970 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
971 && !node) {
972 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
973 pci_name(dev));
974 return false;
975 }
976
977 if (!node)
978 return true;
979
980 uninorth_child = node->parent &&
981 of_device_is_compatible(node->parent, "uni-north");
982
983
984
985
986 if (uninorth_child && !strcmp(node->name, "firewire") &&
987 (of_device_is_compatible(node, "pci106b,18") ||
988 of_device_is_compatible(node, "pci106b,30") ||
989 of_device_is_compatible(node, "pci11c1,5811"))) {
990 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
991 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
992 updatecfg = 1;
993 }
994 if (uninorth_child && !strcmp(node->name, "ethernet") &&
995 of_device_is_compatible(node, "gmac")) {
996 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
997 updatecfg = 1;
998 }
999
1000
1001
1002
1003
1004
1005
1006 if (updatecfg) {
1007 u16 cmd;
1008
1009 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1010 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1011 | PCI_COMMAND_INVALIDATE;
1012 pci_write_config_word(dev, PCI_COMMAND, cmd);
1013 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
1014
1015 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1016 L1_CACHE_BYTES >> 2);
1017 }
1018
1019 return true;
1020}
1021
1022void pmac_pci_fixup_ohci(struct pci_dev *dev)
1023{
1024 struct device_node *node = pci_device_to_OF_node(dev);
1025
1026
1027
1028
1029 if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
1030 dev->resource[0].flags = 0;
1031}
1032DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
1033
1034
1035
1036
1037void __init pmac_pcibios_after_init(void)
1038{
1039 struct device_node* nd;
1040
1041 for_each_node_by_name(nd, "firewire") {
1042 if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
1043 of_device_is_compatible(nd, "pci106b,30") ||
1044 of_device_is_compatible(nd, "pci11c1,5811"))
1045 && of_device_is_compatible(nd->parent, "uni-north")) {
1046 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1047 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1048 }
1049 }
1050 for_each_node_by_name(nd, "ethernet") {
1051 if (nd->parent && of_device_is_compatible(nd, "gmac")
1052 && of_device_is_compatible(nd->parent, "uni-north"))
1053 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
1054 }
1055}
1056
1057void pmac_pci_fixup_cardbus(struct pci_dev* dev)
1058{
1059 if (!machine_is(powermac))
1060 return;
1061
1062
1063
1064
1065 if (dev->vendor != PCI_VENDOR_ID_TI)
1066 return;
1067 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1068 dev->device == PCI_DEVICE_ID_TI_1131) {
1069 u8 val;
1070
1071 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1072 pci_write_config_byte(dev, 0x91, val | 0x30);
1073
1074 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1075 pci_write_config_byte(dev, 0x92, val & ~0x06);
1076 }
1077 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1078 dev->device == PCI_DEVICE_ID_TI_1211 ||
1079 dev->device == PCI_DEVICE_ID_TI_1410 ||
1080 dev->device == PCI_DEVICE_ID_TI_1510) {
1081 u8 val;
1082
1083
1084 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1085 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1086
1087 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1088 pci_write_config_byte(dev, 0x92, val & ~0x06);
1089 }
1090}
1091
1092DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1093
1094void pmac_pci_fixup_pciata(struct pci_dev* dev)
1095{
1096 u8 progif = 0;
1097
1098
1099
1100
1101
1102 if (!machine_is(powermac))
1103 return;
1104
1105
1106 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1107 switch(dev->device) {
1108 case PCI_DEVICE_ID_PROMISE_20246:
1109 case PCI_DEVICE_ID_PROMISE_20262:
1110 case PCI_DEVICE_ID_PROMISE_20263:
1111 case PCI_DEVICE_ID_PROMISE_20265:
1112 case PCI_DEVICE_ID_PROMISE_20267:
1113 case PCI_DEVICE_ID_PROMISE_20268:
1114 case PCI_DEVICE_ID_PROMISE_20269:
1115 case PCI_DEVICE_ID_PROMISE_20270:
1116 case PCI_DEVICE_ID_PROMISE_20271:
1117 case PCI_DEVICE_ID_PROMISE_20275:
1118 case PCI_DEVICE_ID_PROMISE_20276:
1119 case PCI_DEVICE_ID_PROMISE_20277:
1120 goto good;
1121 }
1122
1123 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1124 return;
1125 good:
1126 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1127 if ((progif & 5) != 5) {
1128 printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
1129 pci_name(dev));
1130 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1131 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1132 (progif & 5) != 5)
1133 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
1134 else {
1135
1136 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
1137 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
1138 pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
1139 pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
1140 }
1141 }
1142}
1143DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
1144#endif
1145
1146
1147
1148
1149
1150static void fixup_k2_sata(struct pci_dev* dev)
1151{
1152 int i;
1153 u16 cmd;
1154
1155 if (PCI_FUNC(dev->devfn) > 0) {
1156 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1157 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1158 pci_write_config_word(dev, PCI_COMMAND, cmd);
1159 for (i = 0; i < 6; i++) {
1160 dev->resource[i].start = dev->resource[i].end = 0;
1161 dev->resource[i].flags = 0;
1162 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1163 0);
1164 }
1165 } else {
1166 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1167 cmd &= ~PCI_COMMAND_IO;
1168 pci_write_config_word(dev, PCI_COMMAND, cmd);
1169 for (i = 0; i < 5; i++) {
1170 dev->resource[i].start = dev->resource[i].end = 0;
1171 dev->resource[i].flags = 0;
1172 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1173 0);
1174 }
1175 }
1176}
1177DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197static void fixup_u4_pcie(struct pci_dev* dev)
1198{
1199 struct pci_controller *host = pci_bus_to_host(dev->bus);
1200 struct resource *region = NULL;
1201 u32 reg;
1202 int i;
1203
1204
1205 if (!machine_is(powermac))
1206 return;
1207
1208
1209 for (i = 0; i < 3; i++) {
1210 struct resource *r = &host->mem_resources[i];
1211 if (!(r->flags & IORESOURCE_MEM))
1212 continue;
1213
1214
1215
1216 if (r->start >= 0xf0000000 && r->start < 0xf3000000)
1217 continue;
1218 if (!region || resource_size(r) > resource_size(region))
1219 region = r;
1220 }
1221
1222 if (region == 0)
1223 return;
1224
1225
1226 printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);
1227
1228
1229
1230
1231
1232 reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);
1233 pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);
1234 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);
1235 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
1236 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);
1237}
1238DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);
1239
1240#ifdef CONFIG_PPC64
1241static int pmac_pci_probe_mode(struct pci_bus *bus)
1242{
1243 struct device_node *node = pci_bus_to_OF_node(bus);
1244
1245
1246
1247
1248
1249 if (bus->self == NULL && (of_device_is_compatible(node, "u3-agp") ||
1250 of_device_is_compatible(node, "u4-pcie") ||
1251 of_device_is_compatible(node, "u3-ht")))
1252 return PCI_PROBE_NORMAL;
1253 return PCI_PROBE_DEVTREE;
1254}
1255#endif
1256
1257struct pci_controller_ops pmac_pci_controller_ops = {
1258#ifdef CONFIG_PPC64
1259 .probe_mode = pmac_pci_probe_mode,
1260#endif
1261#ifdef CONFIG_PPC32
1262 .enable_device_hook = pmac_pci_enable_device_hook,
1263#endif
1264};
1265
1266