0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/pci.h>
0011 #include <linux/delay.h>
0012 #include <linux/string.h>
0013 #include <linux/init.h>
0014 #include <linux/irq.h>
0015 #include <linux/of_address.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/of_pci.h>
0018
0019 #include <asm/sections.h>
0020 #include <asm/io.h>
0021 #include <asm/pci-bridge.h>
0022 #include <asm/machdep.h>
0023 #include <asm/pmac_feature.h>
0024 #include <asm/grackle.h>
0025 #include <asm/ppc-pci.h>
0026
0027 #include "pmac.h"
0028
0029 #undef DEBUG
0030
0031 #ifdef DEBUG
0032 #define DBG(x...) printk(x)
0033 #else
0034 #define DBG(x...)
0035 #endif
0036
0037
0038
0039 static int has_uninorth;
0040 #ifdef CONFIG_PPC64
0041 static struct pci_controller *u3_agp;
0042 #else
0043 static int has_second_ohare;
0044 #endif
0045
0046 extern int pcibios_assign_bus_offset;
0047
0048 struct device_node *k2_skiplist[2];
0049
0050
0051
0052
0053 #define BANDIT_DEVID_2 8
0054 #define BANDIT_REVID 3
0055
0056 #define BANDIT_DEVNUM 11
0057 #define BANDIT_MAGIC 0x50
0058 #define BANDIT_COHERENT 0x40
0059
0060 static int __init fixup_one_level_bus_range(struct device_node *node, int higher)
0061 {
0062 for (; node; node = node->sibling) {
0063 const int * bus_range;
0064 const unsigned int *class_code;
0065 int len;
0066
0067
0068 class_code = of_get_property(node, "class-code", NULL);
0069 if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
0070 (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
0071 continue;
0072 bus_range = of_get_property(node, "bus-range", &len);
0073 if (bus_range != NULL && len > 2 * sizeof(int)) {
0074 if (bus_range[1] > higher)
0075 higher = bus_range[1];
0076 }
0077 higher = fixup_one_level_bus_range(node->child, higher);
0078 }
0079 return higher;
0080 }
0081
0082
0083
0084
0085
0086
0087
0088 static void __init fixup_bus_range(struct device_node *bridge)
0089 {
0090 int *bus_range, len;
0091 struct property *prop;
0092
0093
0094 prop = of_find_property(bridge, "bus-range", &len);
0095 if (prop == NULL || prop->length < 2 * sizeof(int))
0096 return;
0097
0098 bus_range = prop->value;
0099 bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]);
0100 }
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 #define MACRISC_CFA0(devfn, off) \
0125 ((1 << (unsigned int)PCI_SLOT(dev_fn)) \
0126 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
0127 | (((unsigned int)(off)) & 0xFCUL))
0128
0129 #define MACRISC_CFA1(bus, devfn, off) \
0130 ((((unsigned int)(bus)) << 16) \
0131 |(((unsigned int)(devfn)) << 8) \
0132 |(((unsigned int)(off)) & 0xFCUL) \
0133 |1UL)
0134
0135 static void __iomem *macrisc_cfg_map_bus(struct pci_bus *bus,
0136 unsigned int dev_fn,
0137 int offset)
0138 {
0139 unsigned int caddr;
0140 struct pci_controller *hose;
0141
0142 hose = pci_bus_to_host(bus);
0143 if (hose == NULL)
0144 return NULL;
0145
0146 if (bus->number == hose->first_busno) {
0147 if (dev_fn < (11 << 3))
0148 return NULL;
0149 caddr = MACRISC_CFA0(dev_fn, offset);
0150 } else
0151 caddr = MACRISC_CFA1(bus->number, dev_fn, offset);
0152
0153
0154 do {
0155 out_le32(hose->cfg_addr, caddr);
0156 } while (in_le32(hose->cfg_addr) != caddr);
0157
0158 offset &= has_uninorth ? 0x07 : 0x03;
0159 return hose->cfg_data + offset;
0160 }
0161
0162 static struct pci_ops macrisc_pci_ops =
0163 {
0164 .map_bus = macrisc_cfg_map_bus,
0165 .read = pci_generic_config_read,
0166 .write = pci_generic_config_write,
0167 };
0168
0169 #ifdef CONFIG_PPC32
0170
0171
0172
0173 static void __iomem *chaos_map_bus(struct pci_bus *bus, unsigned int devfn,
0174 int offset)
0175 {
0176 struct device_node *np;
0177 const u32 *vendor, *device;
0178
0179 if (offset >= 0x100)
0180 return NULL;
0181 np = of_pci_find_child_device(bus->dev.of_node, devfn);
0182 if (np == NULL)
0183 return NULL;
0184
0185 vendor = of_get_property(np, "vendor-id", NULL);
0186 device = of_get_property(np, "device-id", NULL);
0187 if (vendor == NULL || device == NULL)
0188 return NULL;
0189
0190 if ((*vendor == 0x106b) && (*device == 3) && (offset >= 0x10)
0191 && (offset != 0x14) && (offset != 0x18) && (offset <= 0x24))
0192 return NULL;
0193
0194 return macrisc_cfg_map_bus(bus, devfn, offset);
0195 }
0196
0197 static struct pci_ops chaos_pci_ops =
0198 {
0199 .map_bus = chaos_map_bus,
0200 .read = pci_generic_config_read,
0201 .write = pci_generic_config_write,
0202 };
0203
0204 static void __init setup_chaos(struct pci_controller *hose,
0205 struct resource *addr)
0206 {
0207
0208 hose->ops = &chaos_pci_ops;
0209 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
0210 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
0211 }
0212 #endif
0213
0214 #ifdef CONFIG_PPC64
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 static int u3_ht_skip_device(struct pci_controller *hose,
0230 struct pci_bus *bus, unsigned int devfn)
0231 {
0232 struct device_node *busdn, *dn;
0233 int i;
0234
0235
0236
0237
0238
0239
0240 if (bus->self)
0241 busdn = pci_device_to_OF_node(bus->self);
0242 else if (devfn == 0)
0243 return 0;
0244 else
0245 busdn = hose->dn;
0246 for (dn = busdn->child; dn; dn = dn->sibling)
0247 if (PCI_DN(dn) && PCI_DN(dn)->devfn == devfn)
0248 break;
0249 if (dn == NULL)
0250 return -1;
0251
0252
0253
0254
0255
0256 for (i=0; i<2; i++)
0257 if (k2_skiplist[i] == dn)
0258 return 1;
0259
0260 return 0;
0261 }
0262
0263 #define U3_HT_CFA0(devfn, off) \
0264 ((((unsigned int)devfn) << 8) | offset)
0265 #define U3_HT_CFA1(bus, devfn, off) \
0266 (U3_HT_CFA0(devfn, off) \
0267 + (((unsigned int)bus) << 16) \
0268 + 0x01000000UL)
0269
0270 static void __iomem *u3_ht_cfg_access(struct pci_controller *hose, u8 bus,
0271 u8 devfn, u8 offset, int *swap)
0272 {
0273 *swap = 1;
0274 if (bus == hose->first_busno) {
0275 if (devfn != 0)
0276 return hose->cfg_data + U3_HT_CFA0(devfn, offset);
0277 *swap = 0;
0278 return ((void __iomem *)hose->cfg_addr) + (offset << 2);
0279 } else
0280 return hose->cfg_data + U3_HT_CFA1(bus, devfn, offset);
0281 }
0282
0283 static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn,
0284 int offset, int len, u32 *val)
0285 {
0286 struct pci_controller *hose;
0287 void __iomem *addr;
0288 int swap;
0289
0290 hose = pci_bus_to_host(bus);
0291 if (hose == NULL)
0292 return PCIBIOS_DEVICE_NOT_FOUND;
0293 if (offset >= 0x100)
0294 return PCIBIOS_BAD_REGISTER_NUMBER;
0295 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
0296 if (!addr)
0297 return PCIBIOS_DEVICE_NOT_FOUND;
0298
0299 switch (u3_ht_skip_device(hose, bus, devfn)) {
0300 case 0:
0301 break;
0302 case 1:
0303 switch (len) {
0304 case 1:
0305 *val = 0xff; break;
0306 case 2:
0307 *val = 0xffff; break;
0308 default:
0309 *val = 0xfffffffful; break;
0310 }
0311 return PCIBIOS_SUCCESSFUL;
0312 default:
0313 return PCIBIOS_DEVICE_NOT_FOUND;
0314 }
0315
0316
0317
0318
0319
0320 switch (len) {
0321 case 1:
0322 *val = in_8(addr);
0323 break;
0324 case 2:
0325 *val = swap ? in_le16(addr) : in_be16(addr);
0326 break;
0327 default:
0328 *val = swap ? in_le32(addr) : in_be32(addr);
0329 break;
0330 }
0331 return PCIBIOS_SUCCESSFUL;
0332 }
0333
0334 static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn,
0335 int offset, int len, u32 val)
0336 {
0337 struct pci_controller *hose;
0338 void __iomem *addr;
0339 int swap;
0340
0341 hose = pci_bus_to_host(bus);
0342 if (hose == NULL)
0343 return PCIBIOS_DEVICE_NOT_FOUND;
0344 if (offset >= 0x100)
0345 return PCIBIOS_BAD_REGISTER_NUMBER;
0346 addr = u3_ht_cfg_access(hose, bus->number, devfn, offset, &swap);
0347 if (!addr)
0348 return PCIBIOS_DEVICE_NOT_FOUND;
0349
0350 switch (u3_ht_skip_device(hose, bus, devfn)) {
0351 case 0:
0352 break;
0353 case 1:
0354 return PCIBIOS_SUCCESSFUL;
0355 default:
0356 return PCIBIOS_DEVICE_NOT_FOUND;
0357 }
0358
0359
0360
0361
0362
0363 switch (len) {
0364 case 1:
0365 out_8(addr, val);
0366 break;
0367 case 2:
0368 swap ? out_le16(addr, val) : out_be16(addr, val);
0369 break;
0370 default:
0371 swap ? out_le32(addr, val) : out_be32(addr, val);
0372 break;
0373 }
0374 return PCIBIOS_SUCCESSFUL;
0375 }
0376
0377 static struct pci_ops u3_ht_pci_ops =
0378 {
0379 .read = u3_ht_read_config,
0380 .write = u3_ht_write_config,
0381 };
0382
0383 #define U4_PCIE_CFA0(devfn, off) \
0384 ((1 << ((unsigned int)PCI_SLOT(dev_fn))) \
0385 | (((unsigned int)PCI_FUNC(dev_fn)) << 8) \
0386 | ((((unsigned int)(off)) >> 8) << 28) \
0387 | (((unsigned int)(off)) & 0xfcU))
0388
0389 #define U4_PCIE_CFA1(bus, devfn, off) \
0390 ((((unsigned int)(bus)) << 16) \
0391 |(((unsigned int)(devfn)) << 8) \
0392 | ((((unsigned int)(off)) >> 8) << 28) \
0393 |(((unsigned int)(off)) & 0xfcU) \
0394 |1UL)
0395
0396 static void __iomem *u4_pcie_cfg_map_bus(struct pci_bus *bus,
0397 unsigned int dev_fn,
0398 int offset)
0399 {
0400 struct pci_controller *hose;
0401 unsigned int caddr;
0402
0403 if (offset >= 0x1000)
0404 return NULL;
0405
0406 hose = pci_bus_to_host(bus);
0407 if (!hose)
0408 return NULL;
0409
0410 if (bus->number == hose->first_busno) {
0411 caddr = U4_PCIE_CFA0(dev_fn, offset);
0412 } else
0413 caddr = U4_PCIE_CFA1(bus->number, dev_fn, offset);
0414
0415
0416 do {
0417 out_le32(hose->cfg_addr, caddr);
0418 } while (in_le32(hose->cfg_addr) != caddr);
0419
0420 offset &= 0x03;
0421 return hose->cfg_data + offset;
0422 }
0423
0424 static struct pci_ops u4_pcie_pci_ops =
0425 {
0426 .map_bus = u4_pcie_cfg_map_bus,
0427 .read = pci_generic_config_read,
0428 .write = pci_generic_config_write,
0429 };
0430
0431 static void pmac_pci_fixup_u4_of_node(struct pci_dev *dev)
0432 {
0433
0434
0435
0436
0437
0438
0439
0440 if (dev->dev.of_node == NULL)
0441 dev->dev.of_node = pcibios_get_phb_of_node(dev->bus);
0442 }
0443 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, 0x5b, pmac_pci_fixup_u4_of_node);
0444
0445 #endif
0446
0447 #ifdef CONFIG_PPC32
0448
0449
0450
0451
0452 static void __init init_bandit(struct pci_controller *bp)
0453 {
0454 unsigned int vendev, magic;
0455 int rev;
0456
0457
0458 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + PCI_VENDOR_ID);
0459 udelay(2);
0460 vendev = in_le32(bp->cfg_data);
0461 if (vendev == (PCI_DEVICE_ID_APPLE_BANDIT << 16) +
0462 PCI_VENDOR_ID_APPLE) {
0463
0464 out_le32(bp->cfg_addr,
0465 (1UL << BANDIT_DEVNUM) + PCI_REVISION_ID);
0466 udelay(2);
0467 rev = in_8(bp->cfg_data);
0468 if (rev != BANDIT_REVID)
0469 printk(KERN_WARNING
0470 "Unknown revision %d for bandit\n", rev);
0471 } else if (vendev != (BANDIT_DEVID_2 << 16) + PCI_VENDOR_ID_APPLE) {
0472 printk(KERN_WARNING "bandit isn't? (%x)\n", vendev);
0473 return;
0474 }
0475
0476
0477 out_le32(bp->cfg_addr, (1UL << BANDIT_DEVNUM) + BANDIT_MAGIC);
0478 udelay(2);
0479 magic = in_le32(bp->cfg_data);
0480 if ((magic & BANDIT_COHERENT) != 0)
0481 return;
0482 magic |= BANDIT_COHERENT;
0483 udelay(2);
0484 out_le32(bp->cfg_data, magic);
0485 printk(KERN_INFO "Cache coherency enabled for bandit/PSX\n");
0486 }
0487
0488
0489
0490
0491 static void __init init_p2pbridge(void)
0492 {
0493 struct device_node *p2pbridge;
0494 struct pci_controller* hose;
0495 u8 bus, devfn;
0496 u16 val;
0497
0498
0499
0500 p2pbridge = of_find_node_by_name(NULL, "pci-bridge");
0501 if (p2pbridge == NULL || !of_node_name_eq(p2pbridge->parent, "pci"))
0502 goto done;
0503 if (pci_device_from_OF_node(p2pbridge, &bus, &devfn) < 0) {
0504 DBG("Can't find PCI infos for PCI<->PCI bridge\n");
0505 goto done;
0506 }
0507
0508
0509
0510 hose = pci_find_hose_for_OF_device(p2pbridge);
0511 if (!hose) {
0512 DBG("Can't find hose for PCI<->PCI bridge\n");
0513 goto done;
0514 }
0515 if (early_read_config_word(hose, bus, devfn,
0516 PCI_BRIDGE_CONTROL, &val) < 0) {
0517 printk(KERN_ERR "init_p2pbridge: couldn't read bridge"
0518 " control\n");
0519 goto done;
0520 }
0521 val &= ~PCI_BRIDGE_CTL_MASTER_ABORT;
0522 early_write_config_word(hose, bus, devfn, PCI_BRIDGE_CONTROL, val);
0523 done:
0524 of_node_put(p2pbridge);
0525 }
0526
0527 static void __init init_second_ohare(void)
0528 {
0529 struct device_node *np = of_find_node_by_name(NULL, "pci106b,7");
0530 unsigned char bus, devfn;
0531 unsigned short cmd;
0532
0533 if (np == NULL)
0534 return;
0535
0536
0537
0538
0539 if (pci_device_from_OF_node(np, &bus, &devfn) == 0) {
0540 struct pci_controller* hose =
0541 pci_find_hose_for_OF_device(np);
0542 if (!hose) {
0543 printk(KERN_ERR "Can't find PCI hose for OHare2 !\n");
0544 of_node_put(np);
0545 return;
0546 }
0547 early_read_config_word(hose, bus, devfn, PCI_COMMAND, &cmd);
0548 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
0549 cmd &= ~PCI_COMMAND_IO;
0550 early_write_config_word(hose, bus, devfn, PCI_COMMAND, cmd);
0551 }
0552 has_second_ohare = 1;
0553 of_node_put(np);
0554 }
0555
0556
0557
0558
0559
0560
0561
0562 static void __init fixup_nec_usb2(void)
0563 {
0564 struct device_node *nec;
0565
0566 for_each_node_by_name(nec, "usb") {
0567 struct pci_controller *hose;
0568 u32 data;
0569 const u32 *prop;
0570 u8 bus, devfn;
0571
0572 prop = of_get_property(nec, "vendor-id", NULL);
0573 if (prop == NULL)
0574 continue;
0575 if (0x1033 != *prop)
0576 continue;
0577 prop = of_get_property(nec, "device-id", NULL);
0578 if (prop == NULL)
0579 continue;
0580 if (0x0035 != *prop)
0581 continue;
0582 prop = of_get_property(nec, "reg", NULL);
0583 if (prop == NULL)
0584 continue;
0585 devfn = (prop[0] >> 8) & 0xff;
0586 bus = (prop[0] >> 16) & 0xff;
0587 if (PCI_FUNC(devfn) != 0)
0588 continue;
0589 hose = pci_find_hose_for_OF_device(nec);
0590 if (!hose)
0591 continue;
0592 early_read_config_dword(hose, bus, devfn, 0xe4, &data);
0593 if (data & 1UL) {
0594 printk("Found NEC PD720100A USB2 chip with disabled"
0595 " EHCI, fixing up...\n");
0596 data &= ~1UL;
0597 early_write_config_dword(hose, bus, devfn, 0xe4, data);
0598 }
0599 }
0600 }
0601
0602 static void __init setup_bandit(struct pci_controller *hose,
0603 struct resource *addr)
0604 {
0605 hose->ops = ¯isc_pci_ops;
0606 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
0607 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
0608 init_bandit(hose);
0609 }
0610
0611 static int __init setup_uninorth(struct pci_controller *hose,
0612 struct resource *addr)
0613 {
0614 pci_add_flags(PCI_REASSIGN_ALL_BUS);
0615 has_uninorth = 1;
0616 hose->ops = ¯isc_pci_ops;
0617 hose->cfg_addr = ioremap(addr->start + 0x800000, 0x1000);
0618 hose->cfg_data = ioremap(addr->start + 0xc00000, 0x1000);
0619
0620 return addr->start == 0xf2000000;
0621 }
0622 #endif
0623
0624 #ifdef CONFIG_PPC64
0625 static void __init setup_u3_agp(struct pci_controller* hose)
0626 {
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636 hose->first_busno = 0xf0;
0637 hose->last_busno = 0xff;
0638 has_uninorth = 1;
0639 hose->ops = ¯isc_pci_ops;
0640 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
0641 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
0642 u3_agp = hose;
0643 }
0644
0645 static void __init setup_u4_pcie(struct pci_controller* hose)
0646 {
0647
0648
0649
0650 hose->ops = &u4_pcie_pci_ops;
0651 hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000);
0652 hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000);
0653
0654
0655
0656
0657
0658
0659
0660 hose->first_busno = 0x00;
0661 hose->last_busno = 0xff;
0662 }
0663
0664 static void __init parse_region_decode(struct pci_controller *hose,
0665 u32 decode)
0666 {
0667 unsigned long base, end, next = -1;
0668 int i, cur = -1;
0669
0670
0671
0672
0673 for (i = 0; i < 31; i++) {
0674 if ((decode & (0x80000000 >> i)) == 0)
0675 continue;
0676 if (i < 16) {
0677 base = 0xf0000000 | (((u32)i) << 24);
0678 end = base + 0x00ffffff;
0679 } else {
0680 base = ((u32)i-16) << 28;
0681 end = base + 0x0fffffff;
0682 }
0683 if (base != next) {
0684 if (++cur >= 3) {
0685 printk(KERN_WARNING "PCI: Too many ranges !\n");
0686 break;
0687 }
0688 hose->mem_resources[cur].flags = IORESOURCE_MEM;
0689 hose->mem_resources[cur].name = hose->dn->full_name;
0690 hose->mem_resources[cur].start = base;
0691 hose->mem_resources[cur].end = end;
0692 hose->mem_offset[cur] = 0;
0693 DBG(" %d: 0x%08lx-0x%08lx\n", cur, base, end);
0694 } else {
0695 DBG(" : -0x%08lx\n", end);
0696 hose->mem_resources[cur].end = end;
0697 }
0698 next = end + 1;
0699 }
0700 }
0701
0702 static void __init setup_u3_ht(struct pci_controller* hose)
0703 {
0704 struct device_node *np = hose->dn;
0705 struct resource cfg_res, self_res;
0706 u32 decode;
0707
0708 hose->ops = &u3_ht_pci_ops;
0709
0710
0711
0712 if (of_address_to_resource(np, 0, &cfg_res) ||
0713 of_address_to_resource(np, 1, &self_res)) {
0714 printk(KERN_ERR "PCI: Failed to get U3/U4 HT resources !\n");
0715 return;
0716 }
0717
0718
0719
0720
0721 hose->cfg_data = ioremap(cfg_res.start, 0x02000000);
0722 hose->cfg_addr = ioremap(self_res.start, resource_size(&self_res));
0723
0724
0725
0726
0727
0728
0729 hose->io_base_phys = 0xf4000000;
0730 hose->pci_io_size = 0x00400000;
0731 hose->io_resource.name = np->full_name;
0732 hose->io_resource.start = 0;
0733 hose->io_resource.end = 0x003fffff;
0734 hose->io_resource.flags = IORESOURCE_IO;
0735 hose->first_busno = 0;
0736 hose->last_busno = 0xef;
0737
0738
0739 decode = in_be32(hose->cfg_addr + 0x80);
0740
0741 DBG("PCI: Apple HT bridge decode register: 0x%08x\n", decode);
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760 decode &= 0x003fffff;
0761
0762
0763 parse_region_decode(hose, decode);
0764 }
0765 #endif
0766
0767
0768
0769
0770
0771
0772 static int __init pmac_add_bridge(struct device_node *dev)
0773 {
0774 int len;
0775 struct pci_controller *hose;
0776 struct resource rsrc;
0777 char *disp_name;
0778 const int *bus_range;
0779 int primary = 1;
0780
0781 DBG("Adding PCI host bridge %pOF\n", dev);
0782
0783
0784 of_address_to_resource(dev, 0, &rsrc);
0785
0786
0787 bus_range = of_get_property(dev, "bus-range", &len);
0788 if (bus_range == NULL || len < 2 * sizeof(int)) {
0789 printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
0790 " bus 0\n", dev);
0791 }
0792
0793 hose = pcibios_alloc_controller(dev);
0794 if (!hose)
0795 return -ENOMEM;
0796 hose->first_busno = bus_range ? bus_range[0] : 0;
0797 hose->last_busno = bus_range ? bus_range[1] : 0xff;
0798 hose->controller_ops = pmac_pci_controller_ops;
0799
0800 disp_name = NULL;
0801
0802
0803 #ifdef CONFIG_PPC64
0804 if (of_device_is_compatible(dev, "u3-agp")) {
0805 setup_u3_agp(hose);
0806 disp_name = "U3-AGP";
0807 primary = 0;
0808 } else if (of_device_is_compatible(dev, "u3-ht")) {
0809 setup_u3_ht(hose);
0810 disp_name = "U3-HT";
0811 primary = 1;
0812 } else if (of_device_is_compatible(dev, "u4-pcie")) {
0813 setup_u4_pcie(hose);
0814 disp_name = "U4-PCIE";
0815 primary = 0;
0816 }
0817 printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number:"
0818 " %d->%d\n", disp_name, hose->first_busno, hose->last_busno);
0819 #endif
0820
0821
0822 #ifdef CONFIG_PPC32
0823 if (of_device_is_compatible(dev, "uni-north")) {
0824 primary = setup_uninorth(hose, &rsrc);
0825 disp_name = "UniNorth";
0826 } else if (of_node_name_eq(dev, "pci")) {
0827
0828 setup_grackle(hose);
0829 disp_name = "Grackle (MPC106)";
0830 } else if (of_node_name_eq(dev, "bandit")) {
0831 setup_bandit(hose, &rsrc);
0832 disp_name = "Bandit";
0833 } else if (of_node_name_eq(dev, "chaos")) {
0834 setup_chaos(hose, &rsrc);
0835 disp_name = "Chaos";
0836 primary = 0;
0837 }
0838 printk(KERN_INFO "Found %s PCI host bridge at 0x%016llx. "
0839 "Firmware bus number: %d->%d\n",
0840 disp_name, (unsigned long long)rsrc.start, hose->first_busno,
0841 hose->last_busno);
0842 #endif
0843
0844 DBG(" ->Hose at 0x%p, cfg_addr=0x%p,cfg_data=0x%p\n",
0845 hose, hose->cfg_addr, hose->cfg_data);
0846
0847
0848
0849 pci_process_bridge_OF_ranges(hose, dev, primary);
0850
0851
0852 fixup_bus_range(dev);
0853
0854
0855 if (IS_ENABLED(CONFIG_PPC64))
0856 pci_devs_phb_init_dynamic(hose);
0857
0858 return 0;
0859 }
0860
0861 void pmac_pci_irq_fixup(struct pci_dev *dev)
0862 {
0863 #ifdef CONFIG_PPC32
0864
0865
0866
0867
0868
0869
0870
0871 if (has_second_ohare &&
0872 dev->vendor == PCI_VENDOR_ID_DEC &&
0873 dev->device == PCI_DEVICE_ID_DEC_TULIP_PLUS) {
0874 dev->irq = irq_create_mapping(NULL, 60);
0875 irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW);
0876 }
0877 #endif
0878 }
0879
0880 #ifdef CONFIG_PPC64
0881 static int pmac_pci_root_bridge_prepare(struct pci_host_bridge *bridge)
0882 {
0883 struct pci_controller *hose = pci_bus_to_host(bridge->bus);
0884 struct device_node *np, *child;
0885
0886 if (hose != u3_agp)
0887 return 0;
0888
0889
0890
0891
0892
0893
0894 np = hose->dn;
0895 PCI_DN(np)->busno = 0xf0;
0896 for_each_child_of_node(np, child)
0897 PCI_DN(child)->busno = 0xf0;
0898
0899 return 0;
0900 }
0901 #endif
0902
0903 void __init pmac_pci_init(void)
0904 {
0905 struct device_node *np, *root;
0906 struct device_node *ht __maybe_unused = NULL;
0907
0908 pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
0909
0910 root = of_find_node_by_path("/");
0911 if (root == NULL) {
0912 printk(KERN_CRIT "pmac_pci_init: can't find root "
0913 "of device tree\n");
0914 return;
0915 }
0916 for_each_child_of_node(root, np) {
0917 if (of_node_name_eq(np, "bandit")
0918 || of_node_name_eq(np, "chaos")
0919 || of_node_name_eq(np, "pci")) {
0920 if (pmac_add_bridge(np) == 0)
0921 of_node_get(np);
0922 }
0923 if (of_node_name_eq(np, "ht")) {
0924 of_node_get(np);
0925 ht = np;
0926 }
0927 }
0928 of_node_put(root);
0929
0930 #ifdef CONFIG_PPC64
0931
0932
0933
0934 if (ht && pmac_add_bridge(ht) != 0)
0935 of_node_put(ht);
0936
0937 ppc_md.pcibios_root_bridge_prepare = pmac_pci_root_bridge_prepare;
0938
0939
0940 #else
0941 init_p2pbridge();
0942 init_second_ohare();
0943 fixup_nec_usb2();
0944
0945
0946
0947
0948
0949 if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
0950 pcibios_assign_bus_offset = 0x10;
0951 #endif
0952 }
0953
0954 #ifdef CONFIG_PPC32
0955 static bool pmac_pci_enable_device_hook(struct pci_dev *dev)
0956 {
0957 struct device_node* node;
0958 int updatecfg = 0;
0959 int uninorth_child;
0960
0961 node = pci_device_to_OF_node(dev);
0962
0963
0964
0965
0966 if (dev->vendor == PCI_VENDOR_ID_APPLE
0967 && dev->class == PCI_CLASS_SERIAL_USB_OHCI
0968 && !node) {
0969 printk(KERN_INFO "Apple USB OHCI %s disabled by firmware\n",
0970 pci_name(dev));
0971 return false;
0972 }
0973
0974 if (!node)
0975 return true;
0976
0977 uninorth_child = node->parent &&
0978 of_device_is_compatible(node->parent, "uni-north");
0979
0980
0981
0982
0983 if (uninorth_child && of_node_name_eq(node, "firewire") &&
0984 (of_device_is_compatible(node, "pci106b,18") ||
0985 of_device_is_compatible(node, "pci106b,30") ||
0986 of_device_is_compatible(node, "pci11c1,5811"))) {
0987 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, node, 0, 1);
0988 pmac_call_feature(PMAC_FTR_1394_ENABLE, node, 0, 1);
0989 updatecfg = 1;
0990 }
0991 if (uninorth_child && of_node_name_eq(node, "ethernet") &&
0992 of_device_is_compatible(node, "gmac")) {
0993 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, node, 0, 1);
0994 updatecfg = 1;
0995 }
0996
0997
0998
0999
1000
1001
1002
1003 if (updatecfg) {
1004 u16 cmd;
1005
1006 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1007 cmd |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER
1008 | PCI_COMMAND_INVALIDATE;
1009 pci_write_config_word(dev, PCI_COMMAND, cmd);
1010 pci_write_config_byte(dev, PCI_LATENCY_TIMER, 16);
1011
1012 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1013 L1_CACHE_BYTES >> 2);
1014 }
1015
1016 return true;
1017 }
1018
1019 static void pmac_pci_fixup_ohci(struct pci_dev *dev)
1020 {
1021 struct device_node *node = pci_device_to_OF_node(dev);
1022
1023
1024
1025
1026 if (dev->class == PCI_CLASS_SERIAL_USB_OHCI && !node)
1027 dev->resource[0].flags = 0;
1028 }
1029 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_ANY_ID, pmac_pci_fixup_ohci);
1030
1031
1032
1033
1034 void __init pmac_pcibios_after_init(void)
1035 {
1036 struct device_node* nd;
1037
1038 for_each_node_by_name(nd, "firewire") {
1039 if (nd->parent && (of_device_is_compatible(nd, "pci106b,18") ||
1040 of_device_is_compatible(nd, "pci106b,30") ||
1041 of_device_is_compatible(nd, "pci11c1,5811"))
1042 && of_device_is_compatible(nd->parent, "uni-north")) {
1043 pmac_call_feature(PMAC_FTR_1394_ENABLE, nd, 0, 0);
1044 pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, nd, 0, 0);
1045 }
1046 }
1047 for_each_node_by_name(nd, "ethernet") {
1048 if (nd->parent && of_device_is_compatible(nd, "gmac")
1049 && of_device_is_compatible(nd->parent, "uni-north"))
1050 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, nd, 0, 0);
1051 }
1052 }
1053
1054 static void pmac_pci_fixup_cardbus(struct pci_dev *dev)
1055 {
1056 if (!machine_is(powermac))
1057 return;
1058
1059
1060
1061
1062 if (dev->vendor != PCI_VENDOR_ID_TI)
1063 return;
1064 if (dev->device == PCI_DEVICE_ID_TI_1130 ||
1065 dev->device == PCI_DEVICE_ID_TI_1131) {
1066 u8 val;
1067
1068 if (pci_read_config_byte(dev, 0x91, &val) == 0)
1069 pci_write_config_byte(dev, 0x91, val | 0x30);
1070
1071 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1072 pci_write_config_byte(dev, 0x92, val & ~0x06);
1073 }
1074 if (dev->device == PCI_DEVICE_ID_TI_1210 ||
1075 dev->device == PCI_DEVICE_ID_TI_1211 ||
1076 dev->device == PCI_DEVICE_ID_TI_1410 ||
1077 dev->device == PCI_DEVICE_ID_TI_1510) {
1078 u8 val;
1079
1080
1081 if (pci_read_config_byte(dev, 0x8c, &val) == 0)
1082 pci_write_config_byte(dev, 0x8c, (val & ~0x0f) | 2);
1083
1084 if (pci_read_config_byte(dev, 0x92, &val) == 0)
1085 pci_write_config_byte(dev, 0x92, val & ~0x06);
1086 }
1087 }
1088
1089 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_TI, PCI_ANY_ID, pmac_pci_fixup_cardbus);
1090
1091 static void pmac_pci_fixup_pciata(struct pci_dev *dev)
1092 {
1093 u8 progif = 0;
1094
1095
1096
1097
1098
1099 if (!machine_is(powermac))
1100 return;
1101
1102
1103 if (dev->vendor == PCI_VENDOR_ID_PROMISE)
1104 switch(dev->device) {
1105 case PCI_DEVICE_ID_PROMISE_20246:
1106 case PCI_DEVICE_ID_PROMISE_20262:
1107 case PCI_DEVICE_ID_PROMISE_20263:
1108 case PCI_DEVICE_ID_PROMISE_20265:
1109 case PCI_DEVICE_ID_PROMISE_20267:
1110 case PCI_DEVICE_ID_PROMISE_20268:
1111 case PCI_DEVICE_ID_PROMISE_20269:
1112 case PCI_DEVICE_ID_PROMISE_20270:
1113 case PCI_DEVICE_ID_PROMISE_20271:
1114 case PCI_DEVICE_ID_PROMISE_20275:
1115 case PCI_DEVICE_ID_PROMISE_20276:
1116 case PCI_DEVICE_ID_PROMISE_20277:
1117 goto good;
1118 }
1119
1120 if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1121 return;
1122 good:
1123 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
1124 if ((progif & 5) != 5) {
1125 printk(KERN_INFO "PCI: %s Forcing PCI IDE into native mode\n",
1126 pci_name(dev));
1127 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
1128 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
1129 (progif & 5) != 5)
1130 printk(KERN_ERR "Rewrite of PROGIF failed !\n");
1131 else {
1132
1133 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
1134 pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
1135 pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, 0);
1136 pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, 0);
1137 }
1138 }
1139 }
1140 DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pmac_pci_fixup_pciata);
1141 #endif
1142
1143
1144
1145
1146
1147 static void fixup_k2_sata(struct pci_dev* dev)
1148 {
1149 int i;
1150 u16 cmd;
1151
1152 if (PCI_FUNC(dev->devfn) > 0) {
1153 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1154 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
1155 pci_write_config_word(dev, PCI_COMMAND, cmd);
1156 for (i = 0; i < 6; i++) {
1157 dev->resource[i].start = dev->resource[i].end = 0;
1158 dev->resource[i].flags = 0;
1159 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1160 0);
1161 }
1162 } else {
1163 pci_read_config_word(dev, PCI_COMMAND, &cmd);
1164 cmd &= ~PCI_COMMAND_IO;
1165 pci_write_config_word(dev, PCI_COMMAND, cmd);
1166 for (i = 0; i < 5; i++) {
1167 dev->resource[i].start = dev->resource[i].end = 0;
1168 dev->resource[i].flags = 0;
1169 pci_write_config_dword(dev, PCI_BASE_ADDRESS_0 + 4 * i,
1170 0);
1171 }
1172 }
1173 }
1174 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_SERVERWORKS, 0x0240, fixup_k2_sata);
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194 static void fixup_u4_pcie(struct pci_dev* dev)
1195 {
1196 struct pci_controller *host = pci_bus_to_host(dev->bus);
1197 struct resource *region = NULL;
1198 u32 reg;
1199 int i;
1200
1201
1202 if (!machine_is(powermac))
1203 return;
1204
1205
1206 for (i = 0; i < 3; i++) {
1207 struct resource *r = &host->mem_resources[i];
1208 if (!(r->flags & IORESOURCE_MEM))
1209 continue;
1210
1211
1212
1213 if (r->start >= 0xf0000000 && r->start < 0xf3000000)
1214 continue;
1215 if (!region || resource_size(r) > resource_size(region))
1216 region = r;
1217 }
1218
1219 if (!region)
1220 return;
1221
1222
1223 printk(KERN_INFO "PCI: Fixup U4 PCIe bridge range: %pR\n", region);
1224
1225
1226
1227
1228
1229 reg = ((region->start >> 16) & 0xfff0) | (region->end & 0xfff00000);
1230 pci_write_config_dword(dev, PCI_MEMORY_BASE, reg);
1231 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0);
1232 pci_write_config_dword(dev, PCI_PREF_LIMIT_UPPER32, 0);
1233 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0);
1234 }
1235 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_U4_PCIE, fixup_u4_pcie);
1236
1237 #ifdef CONFIG_PPC64
1238 static int pmac_pci_probe_mode(struct pci_bus *bus)
1239 {
1240 struct device_node *node = pci_bus_to_OF_node(bus);
1241
1242
1243
1244
1245
1246 if (bus->self == NULL && (of_device_is_compatible(node, "u3-agp") ||
1247 of_device_is_compatible(node, "u4-pcie") ||
1248 of_device_is_compatible(node, "u3-ht")))
1249 return PCI_PROBE_NORMAL;
1250 return PCI_PROBE_DEVTREE;
1251 }
1252 #endif
1253
1254 struct pci_controller_ops pmac_pci_controller_ops = {
1255 #ifdef CONFIG_PPC64
1256 .probe_mode = pmac_pci_probe_mode,
1257 #endif
1258 #ifdef CONFIG_PPC32
1259 .enable_device_hook = pmac_pci_enable_device_hook,
1260 #endif
1261 };