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> 23e05b3b4aSPaul Mackerras #include <asm/byteorder.h> 24e05b3b4aSPaul Mackerras #include <asm/uaccess.h> 25e05b3b4aSPaul Mackerras #include <asm/machdep.h> 26e05b3b4aSPaul Mackerras 27e05b3b4aSPaul Mackerras #undef DEBUG 28e05b3b4aSPaul Mackerras 29e05b3b4aSPaul Mackerras #ifdef DEBUG 30e05b3b4aSPaul Mackerras #define DBG(x...) printk(x) 31e05b3b4aSPaul Mackerras #else 32e05b3b4aSPaul Mackerras #define DBG(x...) 33e05b3b4aSPaul Mackerras #endif 34e05b3b4aSPaul Mackerras 35e05b3b4aSPaul Mackerras unsigned long isa_io_base = 0; 36e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0; 37e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1; 38e05b3b4aSPaul Mackerras 39e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void); 40e05b3b4aSPaul Mackerras 41e05b3b4aSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev); 42e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev); 43e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF 44e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map; 45e05b3b4aSPaul Mackerras #endif 46e05b3b4aSPaul Mackerras 47e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on 48e05b3b4aSPaul Mackerras * some pmacs 49e05b3b4aSPaul Mackerras */ 50fc3fb71cSBenjamin Herrenschmidt static int pci_assign_all_buses; 51e05b3b4aSPaul Mackerras 52a4c9e328SKumar Gala LIST_HEAD(hose_list); 53e05b3b4aSPaul Mackerras 54e05b3b4aSPaul Mackerras static int pci_bus_count; 55e05b3b4aSPaul Mackerras 56e05b3b4aSPaul Mackerras static void 572052d6d2SKumar Gala fixup_hide_host_resource_fsl(struct pci_dev* dev) 582052d6d2SKumar Gala { 592052d6d2SKumar Gala int i, class = dev->class >> 8; 602052d6d2SKumar Gala 612052d6d2SKumar Gala if ((class == PCI_CLASS_PROCESSOR_POWERPC) && 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 268e05b3b4aSPaul Mackerras if (!have_of) 269e05b3b4aSPaul Mackerras return NULL; 270e05b3b4aSPaul Mackerras 271dae4828dSBenjamin Herrenschmidt DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); 272dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus); 273dae4828dSBenjamin Herrenschmidt if (parent == NULL) 274e05b3b4aSPaul Mackerras return NULL; 275dae4828dSBenjamin Herrenschmidt DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>"); 276dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, devfn); 277dae4828dSBenjamin Herrenschmidt of_node_put(parent); 278dae4828dSBenjamin Herrenschmidt DBG(" result is %s\n", np ? np->full_name : "<NULL>"); 279e05b3b4aSPaul Mackerras 280dae4828dSBenjamin Herrenschmidt /* XXX most callers don't release the returned node 281dae4828dSBenjamin Herrenschmidt * mostly because ppc64 doesn't increase the refcount, 282dae4828dSBenjamin Herrenschmidt * we need to fix that. 283e05b3b4aSPaul Mackerras */ 284dae4828dSBenjamin Herrenschmidt return np; 285e05b3b4aSPaul Mackerras } 286e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node); 287e05b3b4aSPaul Mackerras 288e05b3b4aSPaul Mackerras struct device_node* 289e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev) 290e05b3b4aSPaul Mackerras { 291e05b3b4aSPaul Mackerras return pci_busdev_to_OF_node(dev->bus, dev->devfn); 292e05b3b4aSPaul Mackerras } 293e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node); 294e05b3b4aSPaul Mackerras 295e05b3b4aSPaul Mackerras static int 296e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data) 297e05b3b4aSPaul Mackerras { 298e05b3b4aSPaul Mackerras return ((void *)node == data); 299e05b3b4aSPaul Mackerras } 300e05b3b4aSPaul Mackerras 301e05b3b4aSPaul Mackerras /* 302e05b3b4aSPaul Mackerras * Returns the PCI device matching a given OF node 303e05b3b4aSPaul Mackerras */ 304e05b3b4aSPaul Mackerras int 305e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) 306e05b3b4aSPaul Mackerras { 307a7f67bdfSJeremy Kerr const unsigned int *reg; 308e05b3b4aSPaul Mackerras struct pci_controller* hose; 309e05b3b4aSPaul Mackerras struct pci_dev* dev = NULL; 310e05b3b4aSPaul Mackerras 311e05b3b4aSPaul Mackerras if (!have_of) 312e05b3b4aSPaul Mackerras return -ENODEV; 313e05b3b4aSPaul Mackerras /* Make sure it's really a PCI device */ 314e05b3b4aSPaul Mackerras hose = pci_find_hose_for_OF_device(node); 31544ef3390SStephen Rothwell if (!hose || !hose->dn) 316e05b3b4aSPaul Mackerras return -ENODEV; 31766524b22SStephen Rothwell if (!scan_OF_pci_childs(hose->dn, 318e05b3b4aSPaul Mackerras find_OF_pci_device_filter, (void *)node)) 319e05b3b4aSPaul Mackerras return -ENODEV; 320e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 321e05b3b4aSPaul Mackerras if (!reg) 322e05b3b4aSPaul Mackerras return -ENODEV; 323e05b3b4aSPaul Mackerras *bus = (reg[0] >> 16) & 0xff; 324e05b3b4aSPaul Mackerras *devfn = ((reg[0] >> 8) & 0xff); 325e05b3b4aSPaul Mackerras 326e05b3b4aSPaul Mackerras /* Ok, here we need some tweak. If we have already renumbered 327e05b3b4aSPaul Mackerras * all busses, we can't rely on the OF bus number any more. 328e05b3b4aSPaul Mackerras * the pci_to_OF_bus_map is not enough as several PCI busses 329e05b3b4aSPaul Mackerras * may match the same OF bus number. 330e05b3b4aSPaul Mackerras */ 331e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) 332e05b3b4aSPaul Mackerras return 0; 333e05b3b4aSPaul Mackerras 334e05b3b4aSPaul Mackerras for_each_pci_dev(dev) 335e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[dev->bus->number] == *bus && 336e05b3b4aSPaul Mackerras dev->devfn == *devfn) { 337e05b3b4aSPaul Mackerras *bus = dev->bus->number; 338e05b3b4aSPaul Mackerras pci_dev_put(dev); 339e05b3b4aSPaul Mackerras return 0; 340e05b3b4aSPaul Mackerras } 341e05b3b4aSPaul Mackerras 342e05b3b4aSPaul Mackerras return -ENODEV; 343e05b3b4aSPaul Mackerras } 344e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node); 345e05b3b4aSPaul Mackerras 346e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the 347e05b3b4aSPaul Mackerras * /proc device tree 348e05b3b4aSPaul Mackerras */ 349e05b3b4aSPaul Mackerras void __init 350e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void) 351e05b3b4aSPaul Mackerras { 352e05b3b4aSPaul Mackerras struct property* of_prop; 3538c8dc322SStephen Rothwell struct device_node *dn; 354e05b3b4aSPaul Mackerras 355e05b3b4aSPaul Mackerras of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); 3568c8dc322SStephen Rothwell if (!of_prop) 3578c8dc322SStephen Rothwell return; 3588c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 3598c8dc322SStephen Rothwell if (dn) { 360e05b3b4aSPaul Mackerras memset(of_prop, -1, sizeof(struct property) + 256); 361e05b3b4aSPaul Mackerras of_prop->name = "pci-OF-bus-map"; 362e05b3b4aSPaul Mackerras of_prop->length = 256; 3631a38147eSStephen Rothwell of_prop->value = &of_prop[1]; 3648c8dc322SStephen Rothwell prom_add_property(dn, of_prop); 3658c8dc322SStephen Rothwell of_node_put(dn); 366e05b3b4aSPaul Mackerras } 367e05b3b4aSPaul Mackerras } 368e05b3b4aSPaul Mackerras 369e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */ 370e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void) 371e05b3b4aSPaul Mackerras { 372e05b3b4aSPaul Mackerras } 373e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */ 374e05b3b4aSPaul Mackerras 3753fd94c6bSBenjamin Herrenschmidt static int __init pcibios_init(void) 376e05b3b4aSPaul Mackerras { 377a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 378e05b3b4aSPaul Mackerras struct pci_bus *bus; 379a4c9e328SKumar Gala int next_busno = 0; 380e05b3b4aSPaul Mackerras 381e05b3b4aSPaul Mackerras printk(KERN_INFO "PCI: Probing PCI hardware\n"); 382e05b3b4aSPaul Mackerras 383fc3fb71cSBenjamin Herrenschmidt if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS) 384fc3fb71cSBenjamin Herrenschmidt pci_assign_all_buses = 1; 385fc3fb71cSBenjamin Herrenschmidt 386e05b3b4aSPaul Mackerras /* Scan all of the recorded PCI controllers. */ 387a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 388e05b3b4aSPaul Mackerras if (pci_assign_all_buses) 389e05b3b4aSPaul Mackerras hose->first_busno = next_busno; 390e05b3b4aSPaul Mackerras hose->last_busno = 0xff; 391803d4573SBenjamin Herrenschmidt bus = pci_scan_bus_parented(hose->parent, hose->first_busno, 392803d4573SBenjamin Herrenschmidt hose->ops, hose); 393b1b166b7SBenjamin Herrenschmidt if (bus) { 394803d4573SBenjamin Herrenschmidt pci_bus_add_devices(bus); 395e05b3b4aSPaul Mackerras hose->last_busno = bus->subordinate; 396b1b166b7SBenjamin Herrenschmidt } 397e05b3b4aSPaul Mackerras if (pci_assign_all_buses || next_busno <= hose->last_busno) 398e05b3b4aSPaul Mackerras next_busno = hose->last_busno + pcibios_assign_bus_offset; 399e05b3b4aSPaul Mackerras } 400e05b3b4aSPaul Mackerras pci_bus_count = next_busno; 401e05b3b4aSPaul Mackerras 402e05b3b4aSPaul Mackerras /* OpenFirmware based machines need a map of OF bus 403e05b3b4aSPaul Mackerras * numbers vs. kernel bus numbers since we may have to 404e05b3b4aSPaul Mackerras * remap them. 405e05b3b4aSPaul Mackerras */ 406e05b3b4aSPaul Mackerras if (pci_assign_all_buses && have_of) 407e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(); 408e05b3b4aSPaul Mackerras 4093fd94c6bSBenjamin Herrenschmidt /* Call common code to handle resource allocation */ 4103fd94c6bSBenjamin Herrenschmidt pcibios_resource_survey(); 411e05b3b4aSPaul Mackerras 412e05b3b4aSPaul Mackerras /* Call machine dependent post-init code */ 413e05b3b4aSPaul Mackerras if (ppc_md.pcibios_after_init) 414e05b3b4aSPaul Mackerras ppc_md.pcibios_after_init(); 415e05b3b4aSPaul Mackerras 416e05b3b4aSPaul Mackerras return 0; 417e05b3b4aSPaul Mackerras } 418e05b3b4aSPaul Mackerras 419e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init); 420e05b3b4aSPaul Mackerras 421bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_do_bus_setup(struct pci_bus *bus) 422e05b3b4aSPaul Mackerras { 423e05b3b4aSPaul Mackerras struct pci_controller *hose = (struct pci_controller *) bus->sysdata; 424e05b3b4aSPaul Mackerras unsigned long io_offset; 425e05b3b4aSPaul Mackerras struct resource *res; 426e05b3b4aSPaul Mackerras int i; 4274fc665b8SBecky Bruce struct pci_dev *dev; 428e05b3b4aSPaul Mackerras 429bf5e2ba2SBenjamin Herrenschmidt /* Hookup PHB resources */ 430e05b3b4aSPaul Mackerras io_offset = (unsigned long)hose->io_base_virt - isa_io_base; 431e05b3b4aSPaul Mackerras if (bus->parent == NULL) { 432e05b3b4aSPaul Mackerras /* This is a host bridge - fill in its resources */ 433e05b3b4aSPaul Mackerras hose->bus = bus; 434e05b3b4aSPaul Mackerras 435e05b3b4aSPaul Mackerras bus->resource[0] = res = &hose->io_resource; 436e05b3b4aSPaul Mackerras if (!res->flags) { 437e05b3b4aSPaul Mackerras if (io_offset) 438e05b3b4aSPaul Mackerras printk(KERN_ERR "I/O resource not set for host" 4395516b540SKumar Gala " bridge %d\n", hose->global_number); 440e05b3b4aSPaul Mackerras res->start = 0; 441e05b3b4aSPaul Mackerras res->end = IO_SPACE_LIMIT; 442e05b3b4aSPaul Mackerras res->flags = IORESOURCE_IO; 443e05b3b4aSPaul Mackerras } 44405d3957eSBenjamin Herrenschmidt res->start = (res->start + io_offset) & 0xffffffffu; 44505d3957eSBenjamin Herrenschmidt res->end = (res->end + io_offset) & 0xffffffffu; 446e05b3b4aSPaul Mackerras 447e05b3b4aSPaul Mackerras for (i = 0; i < 3; ++i) { 448e05b3b4aSPaul Mackerras res = &hose->mem_resources[i]; 449e05b3b4aSPaul Mackerras if (!res->flags) { 450e05b3b4aSPaul Mackerras if (i > 0) 451e05b3b4aSPaul Mackerras continue; 452e05b3b4aSPaul Mackerras printk(KERN_ERR "Memory resource not set for " 4535516b540SKumar Gala "host bridge %d\n", hose->global_number); 454e05b3b4aSPaul Mackerras res->start = hose->pci_mem_offset; 455e05b3b4aSPaul Mackerras res->end = ~0U; 456e05b3b4aSPaul Mackerras res->flags = IORESOURCE_MEM; 457e05b3b4aSPaul Mackerras } 458e05b3b4aSPaul Mackerras bus->resource[i+1] = res; 459e05b3b4aSPaul Mackerras } 460f90bb153SBenjamin Herrenschmidt } 4614fc665b8SBecky Bruce 4624fc665b8SBecky Bruce if (ppc_md.pci_dma_bus_setup) 4634fc665b8SBecky Bruce ppc_md.pci_dma_bus_setup(bus); 4644fc665b8SBecky Bruce 4654fc665b8SBecky Bruce list_for_each_entry(dev, &bus->devices, bus_list) 4664fc665b8SBecky Bruce pcibios_setup_new_device(dev); 467e05b3b4aSPaul Mackerras } 468e05b3b4aSPaul Mackerras 469e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */ 470e05b3b4aSPaul Mackerras void __init 471e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq) 472e05b3b4aSPaul Mackerras { 473e05b3b4aSPaul Mackerras pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); 474e05b3b4aSPaul Mackerras /* XXX FIXME - update OF device tree node interrupt property */ 475e05b3b4aSPaul Mackerras } 476e05b3b4aSPaul Mackerras 4770b1d40c4SKumar Gala static struct pci_controller* 478e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus) 479e05b3b4aSPaul Mackerras { 480a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 481e05b3b4aSPaul Mackerras 482a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) 483e05b3b4aSPaul Mackerras if (bus >= hose->first_busno && bus <= hose->last_busno) 484e05b3b4aSPaul Mackerras return hose; 485e05b3b4aSPaul Mackerras return NULL; 486e05b3b4aSPaul Mackerras } 487e05b3b4aSPaul Mackerras 488e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical 489e05b3b4aSPaul Mackerras * memory. Do this on a per-card basis so that we choose the right 490e05b3b4aSPaul Mackerras * root bridge. 491e05b3b4aSPaul Mackerras * Note that the returned IO or memory base is a physical address 492e05b3b4aSPaul Mackerras */ 493e05b3b4aSPaul Mackerras 494e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) 495e05b3b4aSPaul Mackerras { 496e05b3b4aSPaul Mackerras struct pci_controller* hose; 497e05b3b4aSPaul Mackerras long result = -EOPNOTSUPP; 498e05b3b4aSPaul Mackerras 499e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(bus); 500e05b3b4aSPaul Mackerras if (!hose) 501e05b3b4aSPaul Mackerras return -ENODEV; 502e05b3b4aSPaul Mackerras 503e05b3b4aSPaul Mackerras switch (which) { 504e05b3b4aSPaul Mackerras case IOBASE_BRIDGE_NUMBER: 505e05b3b4aSPaul Mackerras return (long)hose->first_busno; 506e05b3b4aSPaul Mackerras case IOBASE_MEMORY: 507e05b3b4aSPaul Mackerras return (long)hose->pci_mem_offset; 508e05b3b4aSPaul Mackerras case IOBASE_IO: 509e05b3b4aSPaul Mackerras return (long)hose->io_base_phys; 510e05b3b4aSPaul Mackerras case IOBASE_ISA_IO: 511e05b3b4aSPaul Mackerras return (long)isa_io_base; 512e05b3b4aSPaul Mackerras case IOBASE_ISA_MEM: 513e05b3b4aSPaul Mackerras return (long)isa_mem_base; 514e05b3b4aSPaul Mackerras } 515e05b3b4aSPaul Mackerras 516e05b3b4aSPaul Mackerras return result; 517e05b3b4aSPaul Mackerras } 518e05b3b4aSPaul Mackerras 519e05b3b4aSPaul Mackerras unsigned long pci_address_to_pio(phys_addr_t address) 520e05b3b4aSPaul Mackerras { 521a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 522e05b3b4aSPaul Mackerras 523a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 524e05b3b4aSPaul Mackerras unsigned int size = hose->io_resource.end - 525e05b3b4aSPaul Mackerras hose->io_resource.start + 1; 526e05b3b4aSPaul Mackerras if (address >= hose->io_base_phys && 527e05b3b4aSPaul Mackerras address < (hose->io_base_phys + size)) { 528e05b3b4aSPaul Mackerras unsigned long base = 529e05b3b4aSPaul Mackerras (unsigned long)hose->io_base_virt - _IO_BASE; 530e05b3b4aSPaul Mackerras return base + (address - hose->io_base_phys); 531e05b3b4aSPaul Mackerras } 532e05b3b4aSPaul Mackerras } 533e05b3b4aSPaul Mackerras return (unsigned int)-1; 534e05b3b4aSPaul Mackerras } 535e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_address_to_pio); 536e05b3b4aSPaul Mackerras 537e05b3b4aSPaul Mackerras /* 538e05b3b4aSPaul Mackerras * Null PCI config access functions, for the case when we can't 539e05b3b4aSPaul Mackerras * find a hose. 540e05b3b4aSPaul Mackerras */ 541e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type) \ 542e05b3b4aSPaul Mackerras static int \ 543e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \ 544e05b3b4aSPaul Mackerras { \ 545e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; \ 546e05b3b4aSPaul Mackerras } 547e05b3b4aSPaul Mackerras 548e05b3b4aSPaul Mackerras static int 549e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset, 550e05b3b4aSPaul Mackerras int len, u32 *val) 551e05b3b4aSPaul Mackerras { 552e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 553e05b3b4aSPaul Mackerras } 554e05b3b4aSPaul Mackerras 555e05b3b4aSPaul Mackerras static int 556e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset, 557e05b3b4aSPaul Mackerras int len, u32 val) 558e05b3b4aSPaul Mackerras { 559e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 560e05b3b4aSPaul Mackerras } 561e05b3b4aSPaul Mackerras 562e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops = 563e05b3b4aSPaul Mackerras { 5646127d1c0SNathan Lynch .read = null_read_config, 5656127d1c0SNathan Lynch .write = null_write_config, 566e05b3b4aSPaul Mackerras }; 567e05b3b4aSPaul Mackerras 568e05b3b4aSPaul Mackerras /* 569e05b3b4aSPaul Mackerras * These functions are used early on before PCI scanning is done 570e05b3b4aSPaul Mackerras * and all of the pci_dev and pci_bus structures have been created. 571e05b3b4aSPaul Mackerras */ 572e05b3b4aSPaul Mackerras static struct pci_bus * 573e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr) 574e05b3b4aSPaul Mackerras { 575e05b3b4aSPaul Mackerras static struct pci_bus bus; 576e05b3b4aSPaul Mackerras 577e05b3b4aSPaul Mackerras if (hose == 0) { 578e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(busnr); 579e05b3b4aSPaul Mackerras if (hose == 0) 580e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr); 581e05b3b4aSPaul Mackerras } 582e05b3b4aSPaul Mackerras bus.number = busnr; 583e05b3b4aSPaul Mackerras bus.sysdata = hose; 584e05b3b4aSPaul Mackerras bus.ops = hose? hose->ops: &null_pci_ops; 585e05b3b4aSPaul Mackerras return &bus; 586e05b3b4aSPaul Mackerras } 587e05b3b4aSPaul Mackerras 588e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type) \ 589e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus, \ 590e05b3b4aSPaul Mackerras int devfn, int offset, type value) \ 591e05b3b4aSPaul Mackerras { \ 592e05b3b4aSPaul Mackerras return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \ 593e05b3b4aSPaul Mackerras devfn, offset, value); \ 594e05b3b4aSPaul Mackerras } 595e05b3b4aSPaul Mackerras 596e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *) 597e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *) 598e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *) 599e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8) 600e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16) 601e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32) 60238805e5fSKumar Gala 60338805e5fSKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap); 60438805e5fSKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn, 60538805e5fSKumar Gala int cap) 60638805e5fSKumar Gala { 60738805e5fSKumar Gala return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap); 60838805e5fSKumar Gala } 609