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 614a015c37SJohn Rigby if ((class == PCI_CLASS_PROCESSOR_POWERPC || 624a015c37SJohn Rigby class == PCI_CLASS_BRIDGE_OTHER) && 632052d6d2SKumar Gala (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) && 642052d6d2SKumar Gala (dev->bus->parent == NULL)) { 652052d6d2SKumar Gala for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 662052d6d2SKumar Gala dev->resource[i].start = 0; 672052d6d2SKumar Gala dev->resource[i].end = 0; 682052d6d2SKumar Gala dev->resource[i].flags = 0; 692052d6d2SKumar Gala } 702052d6d2SKumar Gala } 712052d6d2SKumar Gala } 722052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); 732052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); 742052d6d2SKumar Gala 752052d6d2SKumar Gala static void 76e05b3b4aSPaul Mackerras fixup_broken_pcnet32(struct pci_dev* dev) 77e05b3b4aSPaul Mackerras { 78e05b3b4aSPaul Mackerras if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) { 79e05b3b4aSPaul Mackerras dev->vendor = PCI_VENDOR_ID_AMD; 80e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD); 81e05b3b4aSPaul Mackerras } 82e05b3b4aSPaul Mackerras } 83e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32); 84e05b3b4aSPaul Mackerras 85e05b3b4aSPaul Mackerras static void 86e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev) 87e05b3b4aSPaul Mackerras { 88e05b3b4aSPaul Mackerras /* Hide the PCI64 BARs from the kernel as their content doesn't 89e05b3b4aSPaul Mackerras * fit well in the resource management 90e05b3b4aSPaul Mackerras */ 91e05b3b4aSPaul Mackerras dev->resource[0].start = dev->resource[0].end = 0; 92e05b3b4aSPaul Mackerras dev->resource[0].flags = 0; 93e05b3b4aSPaul Mackerras dev->resource[1].start = dev->resource[1].end = 0; 94e05b3b4aSPaul Mackerras dev->resource[1].flags = 0; 95e05b3b4aSPaul Mackerras } 96e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64); 97e05b3b4aSPaul Mackerras 98e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF 99e05b3b4aSPaul Mackerras /* 100e05b3b4aSPaul Mackerras * Functions below are used on OpenFirmware machines. 101e05b3b4aSPaul Mackerras */ 102e05b3b4aSPaul Mackerras static void 103e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus) 104e05b3b4aSPaul Mackerras { 105a7f67bdfSJeremy Kerr const int *bus_range; 106e05b3b4aSPaul Mackerras int len; 107e05b3b4aSPaul Mackerras 108e05b3b4aSPaul Mackerras if (pci_bus >= pci_bus_count) 109e05b3b4aSPaul Mackerras return; 110e2eb6392SStephen Rothwell bus_range = of_get_property(node, "bus-range", &len); 111e05b3b4aSPaul Mackerras if (bus_range == NULL || len < 2 * sizeof(int)) { 112e05b3b4aSPaul Mackerras printk(KERN_WARNING "Can't get bus-range for %s, " 113e05b3b4aSPaul Mackerras "assuming it starts at 0\n", node->full_name); 114e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = 0; 115e05b3b4aSPaul Mackerras } else 116e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = bus_range[0]; 117e05b3b4aSPaul Mackerras 11866524b22SStephen Rothwell for_each_child_of_node(node, node) { 119e05b3b4aSPaul Mackerras struct pci_dev* dev; 120a7f67bdfSJeremy Kerr const unsigned int *class_code, *reg; 121e05b3b4aSPaul Mackerras 122e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 123e05b3b4aSPaul Mackerras if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 124e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) 125e05b3b4aSPaul Mackerras continue; 126e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 127e05b3b4aSPaul Mackerras if (!reg) 128e05b3b4aSPaul Mackerras continue; 129ab462768SAlan Cox dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff)); 130ab462768SAlan Cox if (!dev || !dev->subordinate) { 131ab462768SAlan Cox pci_dev_put(dev); 132e05b3b4aSPaul Mackerras continue; 133ab462768SAlan Cox } 134e05b3b4aSPaul Mackerras make_one_node_map(node, dev->subordinate->number); 135ab462768SAlan Cox pci_dev_put(dev); 136e05b3b4aSPaul Mackerras } 137e05b3b4aSPaul Mackerras } 138e05b3b4aSPaul Mackerras 139e05b3b4aSPaul Mackerras void 140e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void) 141e05b3b4aSPaul Mackerras { 142e05b3b4aSPaul Mackerras int i; 143a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 144a7f67bdfSJeremy Kerr struct property *map_prop; 1458c8dc322SStephen Rothwell struct device_node *dn; 146e05b3b4aSPaul Mackerras 1475cbded58SRobert P. J. Day pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL); 148e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) { 149e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't allocate OF bus map !\n"); 150e05b3b4aSPaul Mackerras return; 151e05b3b4aSPaul Mackerras } 152e05b3b4aSPaul Mackerras 153e05b3b4aSPaul Mackerras /* We fill the bus map with invalid values, that helps 154e05b3b4aSPaul Mackerras * debugging. 155e05b3b4aSPaul Mackerras */ 156e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) 157e05b3b4aSPaul Mackerras pci_to_OF_bus_map[i] = 0xff; 158e05b3b4aSPaul Mackerras 159e05b3b4aSPaul Mackerras /* For each hose, we begin searching bridges */ 160a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 16144ef3390SStephen Rothwell struct device_node* node = hose->dn; 16244ef3390SStephen Rothwell 163e05b3b4aSPaul Mackerras if (!node) 164e05b3b4aSPaul Mackerras continue; 165e05b3b4aSPaul Mackerras make_one_node_map(node, hose->first_busno); 166e05b3b4aSPaul Mackerras } 1678c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 1688c8dc322SStephen Rothwell map_prop = of_find_property(dn, "pci-OF-bus-map", NULL); 169a7f67bdfSJeremy Kerr if (map_prop) { 170a7f67bdfSJeremy Kerr BUG_ON(pci_bus_count > map_prop->length); 171a7f67bdfSJeremy Kerr memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count); 172a7f67bdfSJeremy Kerr } 1738c8dc322SStephen Rothwell of_node_put(dn); 174e05b3b4aSPaul Mackerras #ifdef DEBUG 175e05b3b4aSPaul Mackerras printk("PCI->OF bus map:\n"); 176e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) { 177e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[i] == 0xff) 178e05b3b4aSPaul Mackerras continue; 179e05b3b4aSPaul Mackerras printk("%d -> %d\n", i, pci_to_OF_bus_map[i]); 180e05b3b4aSPaul Mackerras } 181e05b3b4aSPaul Mackerras #endif 182e05b3b4aSPaul Mackerras } 183e05b3b4aSPaul Mackerras 184e05b3b4aSPaul Mackerras typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data); 185e05b3b4aSPaul Mackerras 186e05b3b4aSPaul Mackerras static struct device_node* 18766524b22SStephen Rothwell scan_OF_pci_childs(struct device_node *parent, pci_OF_scan_iterator filter, void* data) 188e05b3b4aSPaul Mackerras { 18966524b22SStephen Rothwell struct device_node *node; 190e05b3b4aSPaul Mackerras struct device_node* sub_node; 191e05b3b4aSPaul Mackerras 19266524b22SStephen Rothwell for_each_child_of_node(parent, node) { 193a7f67bdfSJeremy Kerr const unsigned int *class_code; 194e05b3b4aSPaul Mackerras 19566524b22SStephen Rothwell if (filter(node, data)) { 19666524b22SStephen Rothwell of_node_put(node); 197e05b3b4aSPaul Mackerras return node; 19866524b22SStephen Rothwell } 199e05b3b4aSPaul Mackerras 200e05b3b4aSPaul Mackerras /* For PCI<->PCI bridges or CardBus bridges, we go down 201e05b3b4aSPaul Mackerras * Note: some OFs create a parent node "multifunc-device" as 202e05b3b4aSPaul Mackerras * a fake root for all functions of a multi-function device, 203e05b3b4aSPaul Mackerras * we go down them as well. 204e05b3b4aSPaul Mackerras */ 205e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 206e05b3b4aSPaul Mackerras if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 207e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) && 208e05b3b4aSPaul Mackerras strcmp(node->name, "multifunc-device")) 209e05b3b4aSPaul Mackerras continue; 21066524b22SStephen Rothwell sub_node = scan_OF_pci_childs(node, filter, data); 21166524b22SStephen Rothwell if (sub_node) { 21266524b22SStephen Rothwell of_node_put(node); 213e05b3b4aSPaul Mackerras return sub_node; 214e05b3b4aSPaul Mackerras } 21566524b22SStephen Rothwell } 216e05b3b4aSPaul Mackerras return NULL; 217e05b3b4aSPaul Mackerras } 218e05b3b4aSPaul Mackerras 219dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_dev(struct device_node *parent, 220dae4828dSBenjamin Herrenschmidt unsigned int devfn) 221e05b3b4aSPaul Mackerras { 22285e99b9fSStephen Rothwell struct device_node *np; 223dae4828dSBenjamin Herrenschmidt const u32 *reg; 224dae4828dSBenjamin Herrenschmidt unsigned int psize; 225e05b3b4aSPaul Mackerras 22685e99b9fSStephen Rothwell for_each_child_of_node(parent, np) { 227e2eb6392SStephen Rothwell reg = of_get_property(np, "reg", &psize); 228dae4828dSBenjamin Herrenschmidt if (reg == NULL || psize < 4) 229dae4828dSBenjamin Herrenschmidt continue; 230dae4828dSBenjamin Herrenschmidt if (((reg[0] >> 8) & 0xff) == devfn) 231dae4828dSBenjamin Herrenschmidt return np; 232dae4828dSBenjamin Herrenschmidt } 233dae4828dSBenjamin Herrenschmidt return NULL; 234e05b3b4aSPaul Mackerras } 235e05b3b4aSPaul Mackerras 236e05b3b4aSPaul Mackerras 237dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus) 238dae4828dSBenjamin Herrenschmidt { 239dae4828dSBenjamin Herrenschmidt struct device_node *parent, *np; 240dae4828dSBenjamin Herrenschmidt 241dae4828dSBenjamin Herrenschmidt /* Are we a root bus ? */ 242dae4828dSBenjamin Herrenschmidt if (bus->self == NULL || bus->parent == NULL) { 2430b1d40c4SKumar Gala struct pci_controller *hose = pci_bus_to_host(bus); 244dae4828dSBenjamin Herrenschmidt if (hose == NULL) 245dae4828dSBenjamin Herrenschmidt return NULL; 24644ef3390SStephen Rothwell return of_node_get(hose->dn); 247dae4828dSBenjamin Herrenschmidt } 248dae4828dSBenjamin Herrenschmidt 249dae4828dSBenjamin Herrenschmidt /* not a root bus, we need to get our parent */ 250dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus->parent); 251dae4828dSBenjamin Herrenschmidt if (parent == NULL) 252dae4828dSBenjamin Herrenschmidt return NULL; 253dae4828dSBenjamin Herrenschmidt 254dae4828dSBenjamin Herrenschmidt /* now iterate for children for a match */ 255dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, bus->self->devfn); 256dae4828dSBenjamin Herrenschmidt of_node_put(parent); 257dae4828dSBenjamin Herrenschmidt 258dae4828dSBenjamin Herrenschmidt return np; 259e05b3b4aSPaul Mackerras } 260e05b3b4aSPaul Mackerras 261e05b3b4aSPaul Mackerras /* 262e05b3b4aSPaul Mackerras * Scans the OF tree for a device node matching a PCI device 263e05b3b4aSPaul Mackerras */ 264e05b3b4aSPaul Mackerras struct device_node * 265e05b3b4aSPaul Mackerras pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) 266e05b3b4aSPaul Mackerras { 267dae4828dSBenjamin Herrenschmidt struct device_node *parent, *np; 268e05b3b4aSPaul Mackerras 269e05b3b4aSPaul Mackerras if (!have_of) 270e05b3b4aSPaul Mackerras return NULL; 271e05b3b4aSPaul Mackerras 272dae4828dSBenjamin Herrenschmidt DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); 273dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus); 274dae4828dSBenjamin Herrenschmidt if (parent == NULL) 275e05b3b4aSPaul Mackerras return NULL; 276dae4828dSBenjamin Herrenschmidt DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>"); 277dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, devfn); 278dae4828dSBenjamin Herrenschmidt of_node_put(parent); 279dae4828dSBenjamin Herrenschmidt DBG(" result is %s\n", np ? np->full_name : "<NULL>"); 280e05b3b4aSPaul Mackerras 281dae4828dSBenjamin Herrenschmidt /* XXX most callers don't release the returned node 282dae4828dSBenjamin Herrenschmidt * mostly because ppc64 doesn't increase the refcount, 283dae4828dSBenjamin Herrenschmidt * we need to fix that. 284e05b3b4aSPaul Mackerras */ 285dae4828dSBenjamin Herrenschmidt return np; 286e05b3b4aSPaul Mackerras } 287e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node); 288e05b3b4aSPaul Mackerras 289e05b3b4aSPaul Mackerras struct device_node* 290e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev) 291e05b3b4aSPaul Mackerras { 292e05b3b4aSPaul Mackerras return pci_busdev_to_OF_node(dev->bus, dev->devfn); 293e05b3b4aSPaul Mackerras } 294e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node); 295e05b3b4aSPaul Mackerras 296e05b3b4aSPaul Mackerras static int 297e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data) 298e05b3b4aSPaul Mackerras { 299e05b3b4aSPaul Mackerras return ((void *)node == data); 300e05b3b4aSPaul Mackerras } 301e05b3b4aSPaul Mackerras 302e05b3b4aSPaul Mackerras /* 303e05b3b4aSPaul Mackerras * Returns the PCI device matching a given OF node 304e05b3b4aSPaul Mackerras */ 305e05b3b4aSPaul Mackerras int 306e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) 307e05b3b4aSPaul Mackerras { 308a7f67bdfSJeremy Kerr const unsigned int *reg; 309e05b3b4aSPaul Mackerras struct pci_controller* hose; 310e05b3b4aSPaul Mackerras struct pci_dev* dev = NULL; 311e05b3b4aSPaul Mackerras 312e05b3b4aSPaul Mackerras if (!have_of) 313e05b3b4aSPaul Mackerras return -ENODEV; 314e05b3b4aSPaul Mackerras /* Make sure it's really a PCI device */ 315e05b3b4aSPaul Mackerras hose = pci_find_hose_for_OF_device(node); 31644ef3390SStephen Rothwell if (!hose || !hose->dn) 317e05b3b4aSPaul Mackerras return -ENODEV; 31866524b22SStephen Rothwell if (!scan_OF_pci_childs(hose->dn, 319e05b3b4aSPaul Mackerras find_OF_pci_device_filter, (void *)node)) 320e05b3b4aSPaul Mackerras return -ENODEV; 321e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 322e05b3b4aSPaul Mackerras if (!reg) 323e05b3b4aSPaul Mackerras return -ENODEV; 324e05b3b4aSPaul Mackerras *bus = (reg[0] >> 16) & 0xff; 325e05b3b4aSPaul Mackerras *devfn = ((reg[0] >> 8) & 0xff); 326e05b3b4aSPaul Mackerras 327e05b3b4aSPaul Mackerras /* Ok, here we need some tweak. If we have already renumbered 328e05b3b4aSPaul Mackerras * all busses, we can't rely on the OF bus number any more. 329e05b3b4aSPaul Mackerras * the pci_to_OF_bus_map is not enough as several PCI busses 330e05b3b4aSPaul Mackerras * may match the same OF bus number. 331e05b3b4aSPaul Mackerras */ 332e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) 333e05b3b4aSPaul Mackerras return 0; 334e05b3b4aSPaul Mackerras 335e05b3b4aSPaul Mackerras for_each_pci_dev(dev) 336e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[dev->bus->number] == *bus && 337e05b3b4aSPaul Mackerras dev->devfn == *devfn) { 338e05b3b4aSPaul Mackerras *bus = dev->bus->number; 339e05b3b4aSPaul Mackerras pci_dev_put(dev); 340e05b3b4aSPaul Mackerras return 0; 341e05b3b4aSPaul Mackerras } 342e05b3b4aSPaul Mackerras 343e05b3b4aSPaul Mackerras return -ENODEV; 344e05b3b4aSPaul Mackerras } 345e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node); 346e05b3b4aSPaul Mackerras 347e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the 348e05b3b4aSPaul Mackerras * /proc device tree 349e05b3b4aSPaul Mackerras */ 350e05b3b4aSPaul Mackerras void __init 351e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void) 352e05b3b4aSPaul Mackerras { 353e05b3b4aSPaul Mackerras struct property* of_prop; 3548c8dc322SStephen Rothwell struct device_node *dn; 355e05b3b4aSPaul Mackerras 356e05b3b4aSPaul Mackerras of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); 3578c8dc322SStephen Rothwell if (!of_prop) 3588c8dc322SStephen Rothwell return; 3598c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 3608c8dc322SStephen Rothwell if (dn) { 361e05b3b4aSPaul Mackerras memset(of_prop, -1, sizeof(struct property) + 256); 362e05b3b4aSPaul Mackerras of_prop->name = "pci-OF-bus-map"; 363e05b3b4aSPaul Mackerras of_prop->length = 256; 3641a38147eSStephen Rothwell of_prop->value = &of_prop[1]; 3658c8dc322SStephen Rothwell prom_add_property(dn, of_prop); 3668c8dc322SStephen Rothwell of_node_put(dn); 367e05b3b4aSPaul Mackerras } 368e05b3b4aSPaul Mackerras } 369e05b3b4aSPaul Mackerras 370e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */ 371e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void) 372e05b3b4aSPaul Mackerras { 373e05b3b4aSPaul Mackerras } 374e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */ 375e05b3b4aSPaul Mackerras 3763fd94c6bSBenjamin Herrenschmidt static int __init pcibios_init(void) 377e05b3b4aSPaul Mackerras { 378a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 379e05b3b4aSPaul Mackerras struct pci_bus *bus; 380a4c9e328SKumar Gala int next_busno = 0; 381e05b3b4aSPaul Mackerras 382e05b3b4aSPaul Mackerras printk(KERN_INFO "PCI: Probing PCI hardware\n"); 383e05b3b4aSPaul Mackerras 384fc3fb71cSBenjamin Herrenschmidt if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS) 385fc3fb71cSBenjamin Herrenschmidt pci_assign_all_buses = 1; 386fc3fb71cSBenjamin Herrenschmidt 387e05b3b4aSPaul Mackerras /* Scan all of the recorded PCI controllers. */ 388a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 389e05b3b4aSPaul Mackerras if (pci_assign_all_buses) 390e05b3b4aSPaul Mackerras hose->first_busno = next_busno; 391e05b3b4aSPaul Mackerras hose->last_busno = 0xff; 392803d4573SBenjamin Herrenschmidt bus = pci_scan_bus_parented(hose->parent, hose->first_busno, 393803d4573SBenjamin Herrenschmidt hose->ops, hose); 394b1b166b7SBenjamin Herrenschmidt if (bus) { 395803d4573SBenjamin Herrenschmidt pci_bus_add_devices(bus); 396e05b3b4aSPaul Mackerras hose->last_busno = bus->subordinate; 397b1b166b7SBenjamin Herrenschmidt } 398e05b3b4aSPaul Mackerras if (pci_assign_all_buses || next_busno <= hose->last_busno) 399e05b3b4aSPaul Mackerras next_busno = hose->last_busno + pcibios_assign_bus_offset; 400e05b3b4aSPaul Mackerras } 401e05b3b4aSPaul Mackerras pci_bus_count = next_busno; 402e05b3b4aSPaul Mackerras 403e05b3b4aSPaul Mackerras /* OpenFirmware based machines need a map of OF bus 404e05b3b4aSPaul Mackerras * numbers vs. kernel bus numbers since we may have to 405e05b3b4aSPaul Mackerras * remap them. 406e05b3b4aSPaul Mackerras */ 407e05b3b4aSPaul Mackerras if (pci_assign_all_buses && have_of) 408e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(); 409e05b3b4aSPaul Mackerras 4103fd94c6bSBenjamin Herrenschmidt /* Call common code to handle resource allocation */ 4113fd94c6bSBenjamin Herrenschmidt pcibios_resource_survey(); 412e05b3b4aSPaul Mackerras 413e05b3b4aSPaul Mackerras /* Call machine dependent post-init code */ 414e05b3b4aSPaul Mackerras if (ppc_md.pcibios_after_init) 415e05b3b4aSPaul Mackerras ppc_md.pcibios_after_init(); 416e05b3b4aSPaul Mackerras 417e05b3b4aSPaul Mackerras return 0; 418e05b3b4aSPaul Mackerras } 419e05b3b4aSPaul Mackerras 420e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init); 421e05b3b4aSPaul Mackerras 422bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_do_bus_setup(struct pci_bus *bus) 423e05b3b4aSPaul Mackerras { 424e05b3b4aSPaul Mackerras struct pci_controller *hose = (struct pci_controller *) bus->sysdata; 425e05b3b4aSPaul Mackerras unsigned long io_offset; 426e05b3b4aSPaul Mackerras struct resource *res; 427e05b3b4aSPaul Mackerras int i; 4284fc665b8SBecky Bruce struct pci_dev *dev; 429e05b3b4aSPaul Mackerras 430bf5e2ba2SBenjamin Herrenschmidt /* Hookup PHB resources */ 431e05b3b4aSPaul Mackerras io_offset = (unsigned long)hose->io_base_virt - isa_io_base; 432e05b3b4aSPaul Mackerras if (bus->parent == NULL) { 433e05b3b4aSPaul Mackerras /* This is a host bridge - fill in its resources */ 434e05b3b4aSPaul Mackerras hose->bus = bus; 435e05b3b4aSPaul Mackerras 436e05b3b4aSPaul Mackerras bus->resource[0] = res = &hose->io_resource; 437e05b3b4aSPaul Mackerras if (!res->flags) { 438e05b3b4aSPaul Mackerras if (io_offset) 439e05b3b4aSPaul Mackerras printk(KERN_ERR "I/O resource not set for host" 4405516b540SKumar Gala " bridge %d\n", hose->global_number); 441e05b3b4aSPaul Mackerras res->start = 0; 442e05b3b4aSPaul Mackerras res->end = IO_SPACE_LIMIT; 443e05b3b4aSPaul Mackerras res->flags = IORESOURCE_IO; 444e05b3b4aSPaul Mackerras } 44505d3957eSBenjamin Herrenschmidt res->start = (res->start + io_offset) & 0xffffffffu; 44605d3957eSBenjamin Herrenschmidt res->end = (res->end + io_offset) & 0xffffffffu; 447e05b3b4aSPaul Mackerras 448e05b3b4aSPaul Mackerras for (i = 0; i < 3; ++i) { 449e05b3b4aSPaul Mackerras res = &hose->mem_resources[i]; 450e05b3b4aSPaul Mackerras if (!res->flags) { 451e05b3b4aSPaul Mackerras if (i > 0) 452e05b3b4aSPaul Mackerras continue; 453e05b3b4aSPaul Mackerras printk(KERN_ERR "Memory resource not set for " 4545516b540SKumar Gala "host bridge %d\n", hose->global_number); 455e05b3b4aSPaul Mackerras res->start = hose->pci_mem_offset; 456e05b3b4aSPaul Mackerras res->end = ~0U; 457e05b3b4aSPaul Mackerras res->flags = IORESOURCE_MEM; 458e05b3b4aSPaul Mackerras } 459e05b3b4aSPaul Mackerras bus->resource[i+1] = res; 460e05b3b4aSPaul Mackerras } 461f90bb153SBenjamin Herrenschmidt } 4624fc665b8SBecky Bruce 4634fc665b8SBecky Bruce if (ppc_md.pci_dma_bus_setup) 4644fc665b8SBecky Bruce ppc_md.pci_dma_bus_setup(bus); 4654fc665b8SBecky Bruce 4664fc665b8SBecky Bruce list_for_each_entry(dev, &bus->devices, bus_list) 4674fc665b8SBecky Bruce pcibios_setup_new_device(dev); 468e05b3b4aSPaul Mackerras } 469e05b3b4aSPaul Mackerras 470e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */ 471e05b3b4aSPaul Mackerras void __init 472e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq) 473e05b3b4aSPaul Mackerras { 474e05b3b4aSPaul Mackerras pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); 475e05b3b4aSPaul Mackerras /* XXX FIXME - update OF device tree node interrupt property */ 476e05b3b4aSPaul Mackerras } 477e05b3b4aSPaul Mackerras 4780b1d40c4SKumar Gala static struct pci_controller* 479e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus) 480e05b3b4aSPaul Mackerras { 481a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 482e05b3b4aSPaul Mackerras 483a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) 484e05b3b4aSPaul Mackerras if (bus >= hose->first_busno && bus <= hose->last_busno) 485e05b3b4aSPaul Mackerras return hose; 486e05b3b4aSPaul Mackerras return NULL; 487e05b3b4aSPaul Mackerras } 488e05b3b4aSPaul Mackerras 489e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical 490e05b3b4aSPaul Mackerras * memory. Do this on a per-card basis so that we choose the right 491e05b3b4aSPaul Mackerras * root bridge. 492e05b3b4aSPaul Mackerras * Note that the returned IO or memory base is a physical address 493e05b3b4aSPaul Mackerras */ 494e05b3b4aSPaul Mackerras 495e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) 496e05b3b4aSPaul Mackerras { 497e05b3b4aSPaul Mackerras struct pci_controller* hose; 498e05b3b4aSPaul Mackerras long result = -EOPNOTSUPP; 499e05b3b4aSPaul Mackerras 500e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(bus); 501e05b3b4aSPaul Mackerras if (!hose) 502e05b3b4aSPaul Mackerras return -ENODEV; 503e05b3b4aSPaul Mackerras 504e05b3b4aSPaul Mackerras switch (which) { 505e05b3b4aSPaul Mackerras case IOBASE_BRIDGE_NUMBER: 506e05b3b4aSPaul Mackerras return (long)hose->first_busno; 507e05b3b4aSPaul Mackerras case IOBASE_MEMORY: 508e05b3b4aSPaul Mackerras return (long)hose->pci_mem_offset; 509e05b3b4aSPaul Mackerras case IOBASE_IO: 510e05b3b4aSPaul Mackerras return (long)hose->io_base_phys; 511e05b3b4aSPaul Mackerras case IOBASE_ISA_IO: 512e05b3b4aSPaul Mackerras return (long)isa_io_base; 513e05b3b4aSPaul Mackerras case IOBASE_ISA_MEM: 514e05b3b4aSPaul Mackerras return (long)isa_mem_base; 515e05b3b4aSPaul Mackerras } 516e05b3b4aSPaul Mackerras 517e05b3b4aSPaul Mackerras return result; 518e05b3b4aSPaul Mackerras } 519e05b3b4aSPaul Mackerras 520e05b3b4aSPaul Mackerras unsigned long pci_address_to_pio(phys_addr_t address) 521e05b3b4aSPaul Mackerras { 522a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 523e05b3b4aSPaul Mackerras 524a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 525e05b3b4aSPaul Mackerras unsigned int size = hose->io_resource.end - 526e05b3b4aSPaul Mackerras hose->io_resource.start + 1; 527e05b3b4aSPaul Mackerras if (address >= hose->io_base_phys && 528e05b3b4aSPaul Mackerras address < (hose->io_base_phys + size)) { 529e05b3b4aSPaul Mackerras unsigned long base = 530e05b3b4aSPaul Mackerras (unsigned long)hose->io_base_virt - _IO_BASE; 531e05b3b4aSPaul Mackerras return base + (address - hose->io_base_phys); 532e05b3b4aSPaul Mackerras } 533e05b3b4aSPaul Mackerras } 534e05b3b4aSPaul Mackerras return (unsigned int)-1; 535e05b3b4aSPaul Mackerras } 536e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_address_to_pio); 537e05b3b4aSPaul Mackerras 538e05b3b4aSPaul Mackerras /* 539e05b3b4aSPaul Mackerras * Null PCI config access functions, for the case when we can't 540e05b3b4aSPaul Mackerras * find a hose. 541e05b3b4aSPaul Mackerras */ 542e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type) \ 543e05b3b4aSPaul Mackerras static int \ 544e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \ 545e05b3b4aSPaul Mackerras { \ 546e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; \ 547e05b3b4aSPaul Mackerras } 548e05b3b4aSPaul Mackerras 549e05b3b4aSPaul Mackerras static int 550e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset, 551e05b3b4aSPaul Mackerras int len, u32 *val) 552e05b3b4aSPaul Mackerras { 553e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 554e05b3b4aSPaul Mackerras } 555e05b3b4aSPaul Mackerras 556e05b3b4aSPaul Mackerras static int 557e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset, 558e05b3b4aSPaul Mackerras int len, u32 val) 559e05b3b4aSPaul Mackerras { 560e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 561e05b3b4aSPaul Mackerras } 562e05b3b4aSPaul Mackerras 563e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops = 564e05b3b4aSPaul Mackerras { 5656127d1c0SNathan Lynch .read = null_read_config, 5666127d1c0SNathan Lynch .write = null_write_config, 567e05b3b4aSPaul Mackerras }; 568e05b3b4aSPaul Mackerras 569e05b3b4aSPaul Mackerras /* 570e05b3b4aSPaul Mackerras * These functions are used early on before PCI scanning is done 571e05b3b4aSPaul Mackerras * and all of the pci_dev and pci_bus structures have been created. 572e05b3b4aSPaul Mackerras */ 573e05b3b4aSPaul Mackerras static struct pci_bus * 574e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr) 575e05b3b4aSPaul Mackerras { 576e05b3b4aSPaul Mackerras static struct pci_bus bus; 577e05b3b4aSPaul Mackerras 578e05b3b4aSPaul Mackerras if (hose == 0) { 579e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(busnr); 580e05b3b4aSPaul Mackerras if (hose == 0) 581e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr); 582e05b3b4aSPaul Mackerras } 583e05b3b4aSPaul Mackerras bus.number = busnr; 584e05b3b4aSPaul Mackerras bus.sysdata = hose; 585e05b3b4aSPaul Mackerras bus.ops = hose? hose->ops: &null_pci_ops; 586e05b3b4aSPaul Mackerras return &bus; 587e05b3b4aSPaul Mackerras } 588e05b3b4aSPaul Mackerras 589e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type) \ 590e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus, \ 591e05b3b4aSPaul Mackerras int devfn, int offset, type value) \ 592e05b3b4aSPaul Mackerras { \ 593e05b3b4aSPaul Mackerras return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \ 594e05b3b4aSPaul Mackerras devfn, offset, value); \ 595e05b3b4aSPaul Mackerras } 596e05b3b4aSPaul Mackerras 597e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *) 598e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *) 599e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *) 600e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8) 601e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16) 602e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32) 60338805e5fSKumar Gala 60438805e5fSKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap); 60538805e5fSKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn, 60638805e5fSKumar Gala int cap) 60738805e5fSKumar Gala { 60838805e5fSKumar Gala return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap); 60938805e5fSKumar Gala } 610