1e05b3b4aSPaul Mackerras /* 2e05b3b4aSPaul Mackerras * Common pmac/prep/chrp pci routines. -- Cort 3e05b3b4aSPaul Mackerras */ 4e05b3b4aSPaul Mackerras 5e05b3b4aSPaul Mackerras #include <linux/kernel.h> 6e05b3b4aSPaul Mackerras #include <linux/pci.h> 7e05b3b4aSPaul Mackerras #include <linux/delay.h> 8e05b3b4aSPaul Mackerras #include <linux/string.h> 9e05b3b4aSPaul Mackerras #include <linux/init.h> 10e05b3b4aSPaul Mackerras #include <linux/capability.h> 11e05b3b4aSPaul Mackerras #include <linux/sched.h> 12e05b3b4aSPaul Mackerras #include <linux/errno.h> 13e05b3b4aSPaul Mackerras #include <linux/bootmem.h> 146e99e458SBenjamin Herrenschmidt #include <linux/irq.h> 15f90bb153SBenjamin Herrenschmidt #include <linux/list.h> 1666524b22SStephen Rothwell #include <linux/of.h> 17e05b3b4aSPaul Mackerras 18e05b3b4aSPaul Mackerras #include <asm/processor.h> 19e05b3b4aSPaul Mackerras #include <asm/io.h> 20e05b3b4aSPaul Mackerras #include <asm/prom.h> 21e05b3b4aSPaul Mackerras #include <asm/sections.h> 22e05b3b4aSPaul Mackerras #include <asm/pci-bridge.h> 23c3bd517dSMilton Miller #include <asm/ppc-pci.h> 24e05b3b4aSPaul Mackerras #include <asm/byteorder.h> 25e05b3b4aSPaul Mackerras #include <asm/uaccess.h> 26e05b3b4aSPaul Mackerras #include <asm/machdep.h> 27e05b3b4aSPaul Mackerras 28e05b3b4aSPaul Mackerras #undef DEBUG 29e05b3b4aSPaul Mackerras 30e05b3b4aSPaul Mackerras unsigned long isa_io_base = 0; 31e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0; 32e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1; 33e05b3b4aSPaul Mackerras 34e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void); 35e05b3b4aSPaul Mackerras 36e05b3b4aSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev); 37e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev); 38e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF 39e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map; 40e05b3b4aSPaul Mackerras #endif 41e05b3b4aSPaul Mackerras 42e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on 43e05b3b4aSPaul Mackerras * some pmacs 44e05b3b4aSPaul Mackerras */ 45fc3fb71cSBenjamin Herrenschmidt static int pci_assign_all_buses; 46e05b3b4aSPaul Mackerras 47e05b3b4aSPaul Mackerras static int pci_bus_count; 48e05b3b4aSPaul Mackerras 497b6b574cSBenjamin Herrenschmidt /* This will remain NULL for now, until isa-bridge.c is made common 507b6b574cSBenjamin Herrenschmidt * to both 32-bit and 64-bit. 517b6b574cSBenjamin Herrenschmidt */ 527b6b574cSBenjamin Herrenschmidt struct pci_dev *isa_bridge_pcidev; 537b6b574cSBenjamin Herrenschmidt EXPORT_SYMBOL_GPL(isa_bridge_pcidev); 547b6b574cSBenjamin Herrenschmidt 55e05b3b4aSPaul Mackerras static void 562052d6d2SKumar Gala fixup_hide_host_resource_fsl(struct pci_dev *dev) 572052d6d2SKumar Gala { 582052d6d2SKumar Gala int i, class = dev->class >> 8; 592052d6d2SKumar Gala 604a015c37SJohn Rigby if ((class == PCI_CLASS_PROCESSOR_POWERPC || 614a015c37SJohn Rigby class == PCI_CLASS_BRIDGE_OTHER) && 622052d6d2SKumar Gala (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) && 632052d6d2SKumar Gala (dev->bus->parent == NULL)) { 642052d6d2SKumar Gala for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 652052d6d2SKumar Gala dev->resource[i].start = 0; 662052d6d2SKumar Gala dev->resource[i].end = 0; 672052d6d2SKumar Gala dev->resource[i].flags = 0; 682052d6d2SKumar Gala } 692052d6d2SKumar Gala } 702052d6d2SKumar Gala } 712052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); 722052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); 732052d6d2SKumar Gala 742052d6d2SKumar Gala static void 75e05b3b4aSPaul Mackerras fixup_broken_pcnet32(struct pci_dev* dev) 76e05b3b4aSPaul Mackerras { 77e05b3b4aSPaul Mackerras if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) { 78e05b3b4aSPaul Mackerras dev->vendor = PCI_VENDOR_ID_AMD; 79e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD); 80e05b3b4aSPaul Mackerras } 81e05b3b4aSPaul Mackerras } 82e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32); 83e05b3b4aSPaul Mackerras 84e05b3b4aSPaul Mackerras static void 85e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev) 86e05b3b4aSPaul Mackerras { 87e05b3b4aSPaul Mackerras /* Hide the PCI64 BARs from the kernel as their content doesn't 88e05b3b4aSPaul Mackerras * fit well in the resource management 89e05b3b4aSPaul Mackerras */ 90e05b3b4aSPaul Mackerras dev->resource[0].start = dev->resource[0].end = 0; 91e05b3b4aSPaul Mackerras dev->resource[0].flags = 0; 92e05b3b4aSPaul Mackerras dev->resource[1].start = dev->resource[1].end = 0; 93e05b3b4aSPaul Mackerras dev->resource[1].flags = 0; 94e05b3b4aSPaul Mackerras } 95e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64); 96e05b3b4aSPaul Mackerras 97e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF 98e05b3b4aSPaul Mackerras /* 99e05b3b4aSPaul Mackerras * Functions below are used on OpenFirmware machines. 100e05b3b4aSPaul Mackerras */ 101e05b3b4aSPaul Mackerras static void 102e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus) 103e05b3b4aSPaul Mackerras { 104a7f67bdfSJeremy Kerr const int *bus_range; 105e05b3b4aSPaul Mackerras int len; 106e05b3b4aSPaul Mackerras 107e05b3b4aSPaul Mackerras if (pci_bus >= pci_bus_count) 108e05b3b4aSPaul Mackerras return; 109e2eb6392SStephen Rothwell bus_range = of_get_property(node, "bus-range", &len); 110e05b3b4aSPaul Mackerras if (bus_range == NULL || len < 2 * sizeof(int)) { 111e05b3b4aSPaul Mackerras printk(KERN_WARNING "Can't get bus-range for %s, " 112e05b3b4aSPaul Mackerras "assuming it starts at 0\n", node->full_name); 113e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = 0; 114e05b3b4aSPaul Mackerras } else 115e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = bus_range[0]; 116e05b3b4aSPaul Mackerras 11766524b22SStephen Rothwell for_each_child_of_node(node, node) { 118e05b3b4aSPaul Mackerras struct pci_dev* dev; 119a7f67bdfSJeremy Kerr const unsigned int *class_code, *reg; 120e05b3b4aSPaul Mackerras 121e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 122e05b3b4aSPaul Mackerras if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 123e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) 124e05b3b4aSPaul Mackerras continue; 125e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 126e05b3b4aSPaul Mackerras if (!reg) 127e05b3b4aSPaul Mackerras continue; 128ab462768SAlan Cox dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff)); 129ab462768SAlan Cox if (!dev || !dev->subordinate) { 130ab462768SAlan Cox pci_dev_put(dev); 131e05b3b4aSPaul Mackerras continue; 132ab462768SAlan Cox } 133e05b3b4aSPaul Mackerras make_one_node_map(node, dev->subordinate->number); 134ab462768SAlan Cox pci_dev_put(dev); 135e05b3b4aSPaul Mackerras } 136e05b3b4aSPaul Mackerras } 137e05b3b4aSPaul Mackerras 138e05b3b4aSPaul Mackerras void 139e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void) 140e05b3b4aSPaul Mackerras { 141e05b3b4aSPaul Mackerras int i; 142a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 143a7f67bdfSJeremy Kerr struct property *map_prop; 1448c8dc322SStephen Rothwell struct device_node *dn; 145e05b3b4aSPaul Mackerras 1465cbded58SRobert P. J. Day pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL); 147e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) { 148e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't allocate OF bus map !\n"); 149e05b3b4aSPaul Mackerras return; 150e05b3b4aSPaul Mackerras } 151e05b3b4aSPaul Mackerras 152e05b3b4aSPaul Mackerras /* We fill the bus map with invalid values, that helps 153e05b3b4aSPaul Mackerras * debugging. 154e05b3b4aSPaul Mackerras */ 155e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) 156e05b3b4aSPaul Mackerras pci_to_OF_bus_map[i] = 0xff; 157e05b3b4aSPaul Mackerras 158e05b3b4aSPaul Mackerras /* For each hose, we begin searching bridges */ 159a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 16044ef3390SStephen Rothwell struct device_node* node = hose->dn; 16144ef3390SStephen Rothwell 162e05b3b4aSPaul Mackerras if (!node) 163e05b3b4aSPaul Mackerras continue; 164e05b3b4aSPaul Mackerras make_one_node_map(node, hose->first_busno); 165e05b3b4aSPaul Mackerras } 1668c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 1678c8dc322SStephen Rothwell map_prop = of_find_property(dn, "pci-OF-bus-map", NULL); 168a7f67bdfSJeremy Kerr if (map_prop) { 169a7f67bdfSJeremy Kerr BUG_ON(pci_bus_count > map_prop->length); 170a7f67bdfSJeremy Kerr memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count); 171a7f67bdfSJeremy Kerr } 1728c8dc322SStephen Rothwell of_node_put(dn); 173e05b3b4aSPaul Mackerras #ifdef DEBUG 174e05b3b4aSPaul Mackerras printk("PCI->OF bus map:\n"); 175e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) { 176e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[i] == 0xff) 177e05b3b4aSPaul Mackerras continue; 178e05b3b4aSPaul Mackerras printk("%d -> %d\n", i, pci_to_OF_bus_map[i]); 179e05b3b4aSPaul Mackerras } 180e05b3b4aSPaul Mackerras #endif 181e05b3b4aSPaul Mackerras } 182e05b3b4aSPaul Mackerras 183e05b3b4aSPaul Mackerras typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data); 184e05b3b4aSPaul Mackerras 185e05b3b4aSPaul Mackerras static struct device_node* 18666524b22SStephen Rothwell scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data) 187e05b3b4aSPaul Mackerras { 18866524b22SStephen Rothwell struct device_node *node; 189e05b3b4aSPaul Mackerras struct device_node* sub_node; 190e05b3b4aSPaul Mackerras 19166524b22SStephen Rothwell for_each_child_of_node(parent, node) { 192a7f67bdfSJeremy Kerr const unsigned int *class_code; 193e05b3b4aSPaul Mackerras 19466524b22SStephen Rothwell if (filter(node, data)) { 19566524b22SStephen Rothwell of_node_put(node); 196e05b3b4aSPaul Mackerras return node; 19766524b22SStephen Rothwell } 198e05b3b4aSPaul Mackerras 199e05b3b4aSPaul Mackerras /* For PCI<->PCI bridges or CardBus bridges, we go down 200e05b3b4aSPaul Mackerras * Note: some OFs create a parent node "multifunc-device" as 201e05b3b4aSPaul Mackerras * a fake root for all functions of a multi-function device, 202e05b3b4aSPaul Mackerras * we go down them as well. 203e05b3b4aSPaul Mackerras */ 204e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 205e05b3b4aSPaul Mackerras if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 206e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) && 207e05b3b4aSPaul Mackerras strcmp(node->name, "multifunc-device")) 208e05b3b4aSPaul Mackerras continue; 20966524b22SStephen Rothwell sub_node = scan_OF_pci_childs(node, filter, data); 21066524b22SStephen Rothwell if (sub_node) { 21166524b22SStephen Rothwell of_node_put(node); 212e05b3b4aSPaul Mackerras return sub_node; 213e05b3b4aSPaul Mackerras } 21466524b22SStephen Rothwell } 215e05b3b4aSPaul Mackerras return NULL; 216e05b3b4aSPaul Mackerras } 217e05b3b4aSPaul Mackerras 218dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_dev(struct device_node *parent, 219dae4828dSBenjamin Herrenschmidt unsigned int devfn) 220e05b3b4aSPaul Mackerras { 22185e99b9fSStephen Rothwell struct device_node *np; 222dae4828dSBenjamin Herrenschmidt const u32 *reg; 223dae4828dSBenjamin Herrenschmidt unsigned int psize; 224e05b3b4aSPaul Mackerras 22585e99b9fSStephen Rothwell for_each_child_of_node(parent, np) { 226e2eb6392SStephen Rothwell reg = of_get_property(np, "reg", &psize); 227dae4828dSBenjamin Herrenschmidt if (reg == NULL || psize < 4) 228dae4828dSBenjamin Herrenschmidt continue; 229dae4828dSBenjamin Herrenschmidt if (((reg[0] >> 8) & 0xff) == devfn) 230dae4828dSBenjamin Herrenschmidt return np; 231dae4828dSBenjamin Herrenschmidt } 232dae4828dSBenjamin Herrenschmidt return NULL; 233e05b3b4aSPaul Mackerras } 234e05b3b4aSPaul Mackerras 235e05b3b4aSPaul Mackerras 236dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus) 237dae4828dSBenjamin Herrenschmidt { 238dae4828dSBenjamin Herrenschmidt struct device_node *parent, *np; 239dae4828dSBenjamin Herrenschmidt 240dae4828dSBenjamin Herrenschmidt /* Are we a root bus ? */ 241dae4828dSBenjamin Herrenschmidt if (bus->self == NULL || bus->parent == NULL) { 2420b1d40c4SKumar Gala struct pci_controller *hose = pci_bus_to_host(bus); 243dae4828dSBenjamin Herrenschmidt if (hose == NULL) 244dae4828dSBenjamin Herrenschmidt return NULL; 24544ef3390SStephen Rothwell return of_node_get(hose->dn); 246dae4828dSBenjamin Herrenschmidt } 247dae4828dSBenjamin Herrenschmidt 248dae4828dSBenjamin Herrenschmidt /* not a root bus, we need to get our parent */ 249dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus->parent); 250dae4828dSBenjamin Herrenschmidt if (parent == NULL) 251dae4828dSBenjamin Herrenschmidt return NULL; 252dae4828dSBenjamin Herrenschmidt 253dae4828dSBenjamin Herrenschmidt /* now iterate for children for a match */ 254dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, bus->self->devfn); 255dae4828dSBenjamin Herrenschmidt of_node_put(parent); 256dae4828dSBenjamin Herrenschmidt 257dae4828dSBenjamin Herrenschmidt return np; 258e05b3b4aSPaul Mackerras } 259e05b3b4aSPaul Mackerras 260e05b3b4aSPaul Mackerras /* 261e05b3b4aSPaul Mackerras * Scans the OF tree for a device node matching a PCI device 262e05b3b4aSPaul Mackerras */ 263e05b3b4aSPaul Mackerras struct device_node * 264e05b3b4aSPaul Mackerras pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) 265e05b3b4aSPaul Mackerras { 266dae4828dSBenjamin Herrenschmidt struct device_node *parent, *np; 267e05b3b4aSPaul Mackerras 268b0494bc8SBenjamin Herrenschmidt pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); 269dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus); 270dae4828dSBenjamin Herrenschmidt if (parent == NULL) 271e05b3b4aSPaul Mackerras return NULL; 272b0494bc8SBenjamin Herrenschmidt pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>"); 273dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, devfn); 274dae4828dSBenjamin Herrenschmidt of_node_put(parent); 275b0494bc8SBenjamin Herrenschmidt pr_debug(" result is %s\n", np ? np->full_name : "<NULL>"); 276e05b3b4aSPaul Mackerras 277dae4828dSBenjamin Herrenschmidt /* XXX most callers don't release the returned node 278dae4828dSBenjamin Herrenschmidt * mostly because ppc64 doesn't increase the refcount, 279dae4828dSBenjamin Herrenschmidt * we need to fix that. 280e05b3b4aSPaul Mackerras */ 281dae4828dSBenjamin Herrenschmidt return np; 282e05b3b4aSPaul Mackerras } 283e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node); 284e05b3b4aSPaul Mackerras 285e05b3b4aSPaul Mackerras struct device_node* 286e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev) 287e05b3b4aSPaul Mackerras { 288e05b3b4aSPaul Mackerras return pci_busdev_to_OF_node(dev->bus, dev->devfn); 289e05b3b4aSPaul Mackerras } 290e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node); 291e05b3b4aSPaul Mackerras 292e05b3b4aSPaul Mackerras static int 293e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data) 294e05b3b4aSPaul Mackerras { 295e05b3b4aSPaul Mackerras return ((void *)node == data); 296e05b3b4aSPaul Mackerras } 297e05b3b4aSPaul Mackerras 298e05b3b4aSPaul Mackerras /* 299e05b3b4aSPaul Mackerras * Returns the PCI device matching a given OF node 300e05b3b4aSPaul Mackerras */ 301e05b3b4aSPaul Mackerras int 302e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) 303e05b3b4aSPaul Mackerras { 304a7f67bdfSJeremy Kerr const unsigned int *reg; 305e05b3b4aSPaul Mackerras struct pci_controller* hose; 306e05b3b4aSPaul Mackerras struct pci_dev* dev = NULL; 307e05b3b4aSPaul Mackerras 308e05b3b4aSPaul Mackerras /* Make sure it's really a PCI device */ 309e05b3b4aSPaul Mackerras hose = pci_find_hose_for_OF_device(node); 31044ef3390SStephen Rothwell if (!hose || !hose->dn) 311e05b3b4aSPaul Mackerras return -ENODEV; 31266524b22SStephen Rothwell if (!scan_OF_pci_childs(hose->dn, 313e05b3b4aSPaul Mackerras find_OF_pci_device_filter, (void *)node)) 314e05b3b4aSPaul Mackerras return -ENODEV; 315e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 316e05b3b4aSPaul Mackerras if (!reg) 317e05b3b4aSPaul Mackerras return -ENODEV; 318e05b3b4aSPaul Mackerras *bus = (reg[0] >> 16) & 0xff; 319e05b3b4aSPaul Mackerras *devfn = ((reg[0] >> 8) & 0xff); 320e05b3b4aSPaul Mackerras 321e05b3b4aSPaul Mackerras /* Ok, here we need some tweak. If we have already renumbered 322e05b3b4aSPaul Mackerras * all busses, we can't rely on the OF bus number any more. 323e05b3b4aSPaul Mackerras * the pci_to_OF_bus_map is not enough as several PCI busses 324e05b3b4aSPaul Mackerras * may match the same OF bus number. 325e05b3b4aSPaul Mackerras */ 326e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) 327e05b3b4aSPaul Mackerras return 0; 328e05b3b4aSPaul Mackerras 329e05b3b4aSPaul Mackerras for_each_pci_dev(dev) 330e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[dev->bus->number] == *bus && 331e05b3b4aSPaul Mackerras dev->devfn == *devfn) { 332e05b3b4aSPaul Mackerras *bus = dev->bus->number; 333e05b3b4aSPaul Mackerras pci_dev_put(dev); 334e05b3b4aSPaul Mackerras return 0; 335e05b3b4aSPaul Mackerras } 336e05b3b4aSPaul Mackerras 337e05b3b4aSPaul Mackerras return -ENODEV; 338e05b3b4aSPaul Mackerras } 339e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node); 340e05b3b4aSPaul Mackerras 341e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the 342e05b3b4aSPaul Mackerras * /proc device tree 343e05b3b4aSPaul Mackerras */ 344e05b3b4aSPaul Mackerras void __init 345e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void) 346e05b3b4aSPaul Mackerras { 347e05b3b4aSPaul Mackerras struct property* of_prop; 3488c8dc322SStephen Rothwell struct device_node *dn; 349e05b3b4aSPaul Mackerras 350e05b3b4aSPaul Mackerras of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); 3518c8dc322SStephen Rothwell if (!of_prop) 3528c8dc322SStephen Rothwell return; 3538c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 3548c8dc322SStephen Rothwell if (dn) { 355e05b3b4aSPaul Mackerras memset(of_prop, -1, sizeof(struct property) + 256); 356e05b3b4aSPaul Mackerras of_prop->name = "pci-OF-bus-map"; 357e05b3b4aSPaul Mackerras of_prop->length = 256; 3581a38147eSStephen Rothwell of_prop->value = &of_prop[1]; 3598c8dc322SStephen Rothwell prom_add_property(dn, of_prop); 3608c8dc322SStephen Rothwell of_node_put(dn); 361e05b3b4aSPaul Mackerras } 362e05b3b4aSPaul Mackerras } 363e05b3b4aSPaul Mackerras 364e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */ 365e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void) 366e05b3b4aSPaul Mackerras { 367e05b3b4aSPaul Mackerras } 368e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */ 369e05b3b4aSPaul Mackerras 37053280323SBenjamin Herrenschmidt static void __devinit pcibios_scan_phb(struct pci_controller *hose) 37153280323SBenjamin Herrenschmidt { 37253280323SBenjamin Herrenschmidt struct pci_bus *bus; 37353280323SBenjamin Herrenschmidt struct device_node *node = hose->dn; 37453280323SBenjamin Herrenschmidt unsigned long io_offset; 37553280323SBenjamin Herrenschmidt struct resource *res = &hose->io_resource; 37653280323SBenjamin Herrenschmidt 37753280323SBenjamin Herrenschmidt pr_debug("PCI: Scanning PHB %s\n", 37853280323SBenjamin Herrenschmidt node ? node->full_name : "<NO NAME>"); 37953280323SBenjamin Herrenschmidt 38053280323SBenjamin Herrenschmidt /* Create an empty bus for the toplevel */ 38153280323SBenjamin Herrenschmidt bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose); 38253280323SBenjamin Herrenschmidt if (bus == NULL) { 38353280323SBenjamin Herrenschmidt printk(KERN_ERR "Failed to create bus for PCI domain %04x\n", 38453280323SBenjamin Herrenschmidt hose->global_number); 38553280323SBenjamin Herrenschmidt return; 38653280323SBenjamin Herrenschmidt } 38753280323SBenjamin Herrenschmidt bus->secondary = hose->first_busno; 38853280323SBenjamin Herrenschmidt hose->bus = bus; 38953280323SBenjamin Herrenschmidt 39053280323SBenjamin Herrenschmidt /* Fixup IO space offset */ 39153280323SBenjamin Herrenschmidt io_offset = (unsigned long)hose->io_base_virt - isa_io_base; 39253280323SBenjamin Herrenschmidt res->start = (res->start + io_offset) & 0xffffffffu; 39353280323SBenjamin Herrenschmidt res->end = (res->end + io_offset) & 0xffffffffu; 39453280323SBenjamin Herrenschmidt 39553280323SBenjamin Herrenschmidt /* Wire up PHB bus resources */ 39653280323SBenjamin Herrenschmidt pcibios_setup_phb_resources(hose); 39753280323SBenjamin Herrenschmidt 39853280323SBenjamin Herrenschmidt /* Scan children */ 39953280323SBenjamin Herrenschmidt hose->last_busno = bus->subordinate = pci_scan_child_bus(bus); 40053280323SBenjamin Herrenschmidt } 40153280323SBenjamin Herrenschmidt 4023fd94c6bSBenjamin Herrenschmidt static int __init pcibios_init(void) 403e05b3b4aSPaul Mackerras { 404a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 405a4c9e328SKumar Gala int next_busno = 0; 406e05b3b4aSPaul Mackerras 407e05b3b4aSPaul Mackerras printk(KERN_INFO "PCI: Probing PCI hardware\n"); 408e05b3b4aSPaul Mackerras 409fc3fb71cSBenjamin Herrenschmidt if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS) 410fc3fb71cSBenjamin Herrenschmidt pci_assign_all_buses = 1; 411fc3fb71cSBenjamin Herrenschmidt 412e05b3b4aSPaul Mackerras /* Scan all of the recorded PCI controllers. */ 413a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 414e05b3b4aSPaul Mackerras if (pci_assign_all_buses) 415e05b3b4aSPaul Mackerras hose->first_busno = next_busno; 416e05b3b4aSPaul Mackerras hose->last_busno = 0xff; 41753280323SBenjamin Herrenschmidt pcibios_scan_phb(hose); 41853280323SBenjamin Herrenschmidt pci_bus_add_devices(hose->bus); 419e05b3b4aSPaul Mackerras if (pci_assign_all_buses || next_busno <= hose->last_busno) 420e05b3b4aSPaul Mackerras next_busno = hose->last_busno + pcibios_assign_bus_offset; 421e05b3b4aSPaul Mackerras } 422e05b3b4aSPaul Mackerras pci_bus_count = next_busno; 423e05b3b4aSPaul Mackerras 424e05b3b4aSPaul Mackerras /* OpenFirmware based machines need a map of OF bus 425e05b3b4aSPaul Mackerras * numbers vs. kernel bus numbers since we may have to 426e05b3b4aSPaul Mackerras * remap them. 427e05b3b4aSPaul Mackerras */ 4286b82b3e4SAnton Vorontsov if (pci_assign_all_buses) 429e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(); 430e05b3b4aSPaul Mackerras 4313fd94c6bSBenjamin Herrenschmidt /* Call common code to handle resource allocation */ 4323fd94c6bSBenjamin Herrenschmidt pcibios_resource_survey(); 433e05b3b4aSPaul Mackerras 434e05b3b4aSPaul Mackerras /* Call machine dependent post-init code */ 435e05b3b4aSPaul Mackerras if (ppc_md.pcibios_after_init) 436e05b3b4aSPaul Mackerras ppc_md.pcibios_after_init(); 437e05b3b4aSPaul Mackerras 438e05b3b4aSPaul Mackerras return 0; 439e05b3b4aSPaul Mackerras } 440e05b3b4aSPaul Mackerras 441e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init); 442e05b3b4aSPaul Mackerras 443e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */ 444e05b3b4aSPaul Mackerras void __init 445e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq) 446e05b3b4aSPaul Mackerras { 447e05b3b4aSPaul Mackerras pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); 448e05b3b4aSPaul Mackerras /* XXX FIXME - update OF device tree node interrupt property */ 449e05b3b4aSPaul Mackerras } 450e05b3b4aSPaul Mackerras 4510b1d40c4SKumar Gala static struct pci_controller* 452e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus) 453e05b3b4aSPaul Mackerras { 454a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 455e05b3b4aSPaul Mackerras 456a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) 457e05b3b4aSPaul Mackerras if (bus >= hose->first_busno && bus <= hose->last_busno) 458e05b3b4aSPaul Mackerras return hose; 459e05b3b4aSPaul Mackerras return NULL; 460e05b3b4aSPaul Mackerras } 461e05b3b4aSPaul Mackerras 462e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical 463e05b3b4aSPaul Mackerras * memory. Do this on a per-card basis so that we choose the right 464e05b3b4aSPaul Mackerras * root bridge. 465e05b3b4aSPaul Mackerras * Note that the returned IO or memory base is a physical address 466e05b3b4aSPaul Mackerras */ 467e05b3b4aSPaul Mackerras 468e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) 469e05b3b4aSPaul Mackerras { 470e05b3b4aSPaul Mackerras struct pci_controller* hose; 471e05b3b4aSPaul Mackerras long result = -EOPNOTSUPP; 472e05b3b4aSPaul Mackerras 473e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(bus); 474e05b3b4aSPaul Mackerras if (!hose) 475e05b3b4aSPaul Mackerras return -ENODEV; 476e05b3b4aSPaul Mackerras 477e05b3b4aSPaul Mackerras switch (which) { 478e05b3b4aSPaul Mackerras case IOBASE_BRIDGE_NUMBER: 479e05b3b4aSPaul Mackerras return (long)hose->first_busno; 480e05b3b4aSPaul Mackerras case IOBASE_MEMORY: 481e05b3b4aSPaul Mackerras return (long)hose->pci_mem_offset; 482e05b3b4aSPaul Mackerras case IOBASE_IO: 483e05b3b4aSPaul Mackerras return (long)hose->io_base_phys; 484e05b3b4aSPaul Mackerras case IOBASE_ISA_IO: 485e05b3b4aSPaul Mackerras return (long)isa_io_base; 486e05b3b4aSPaul Mackerras case IOBASE_ISA_MEM: 487e05b3b4aSPaul Mackerras return (long)isa_mem_base; 488e05b3b4aSPaul Mackerras } 489e05b3b4aSPaul Mackerras 490e05b3b4aSPaul Mackerras return result; 491e05b3b4aSPaul Mackerras } 492e05b3b4aSPaul Mackerras 493e05b3b4aSPaul Mackerras /* 494e05b3b4aSPaul Mackerras * Null PCI config access functions, for the case when we can't 495e05b3b4aSPaul Mackerras * find a hose. 496e05b3b4aSPaul Mackerras */ 497e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type) \ 498e05b3b4aSPaul Mackerras static int \ 499e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \ 500e05b3b4aSPaul Mackerras { \ 501e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; \ 502e05b3b4aSPaul Mackerras } 503e05b3b4aSPaul Mackerras 504e05b3b4aSPaul Mackerras static int 505e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset, 506e05b3b4aSPaul Mackerras int len, u32 *val) 507e05b3b4aSPaul Mackerras { 508e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 509e05b3b4aSPaul Mackerras } 510e05b3b4aSPaul Mackerras 511e05b3b4aSPaul Mackerras static int 512e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset, 513e05b3b4aSPaul Mackerras int len, u32 val) 514e05b3b4aSPaul Mackerras { 515e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 516e05b3b4aSPaul Mackerras } 517e05b3b4aSPaul Mackerras 518e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops = 519e05b3b4aSPaul Mackerras { 5206127d1c0SNathan Lynch .read = null_read_config, 5216127d1c0SNathan Lynch .write = null_write_config, 522e05b3b4aSPaul Mackerras }; 523e05b3b4aSPaul Mackerras 524e05b3b4aSPaul Mackerras /* 525e05b3b4aSPaul Mackerras * These functions are used early on before PCI scanning is done 526e05b3b4aSPaul Mackerras * and all of the pci_dev and pci_bus structures have been created. 527e05b3b4aSPaul Mackerras */ 528e05b3b4aSPaul Mackerras static struct pci_bus * 529e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr) 530e05b3b4aSPaul Mackerras { 531e05b3b4aSPaul Mackerras static struct pci_bus bus; 532e05b3b4aSPaul Mackerras 533e05b3b4aSPaul Mackerras if (hose == 0) { 534e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(busnr); 535e05b3b4aSPaul Mackerras if (hose == 0) 536e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr); 537e05b3b4aSPaul Mackerras } 538e05b3b4aSPaul Mackerras bus.number = busnr; 539e05b3b4aSPaul Mackerras bus.sysdata = hose; 540e05b3b4aSPaul Mackerras bus.ops = hose? hose->ops: &null_pci_ops; 541e05b3b4aSPaul Mackerras return &bus; 542e05b3b4aSPaul Mackerras } 543e05b3b4aSPaul Mackerras 544e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type) \ 545e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus, \ 546e05b3b4aSPaul Mackerras int devfn, int offset, type value) \ 547e05b3b4aSPaul Mackerras { \ 548e05b3b4aSPaul Mackerras return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \ 549e05b3b4aSPaul Mackerras devfn, offset, value); \ 550e05b3b4aSPaul Mackerras } 551e05b3b4aSPaul Mackerras 552e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *) 553e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *) 554e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *) 555e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8) 556e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16) 557e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32) 55838805e5fSKumar Gala 55938805e5fSKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap); 56038805e5fSKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn, 56138805e5fSKumar Gala int cap) 56238805e5fSKumar Gala { 56338805e5fSKumar Gala return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap); 56438805e5fSKumar Gala } 565