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> 16e05b3b4aSPaul Mackerras 17e05b3b4aSPaul Mackerras #include <asm/processor.h> 18e05b3b4aSPaul Mackerras #include <asm/io.h> 19e05b3b4aSPaul Mackerras #include <asm/prom.h> 20e05b3b4aSPaul Mackerras #include <asm/sections.h> 21e05b3b4aSPaul Mackerras #include <asm/pci-bridge.h> 22e05b3b4aSPaul Mackerras #include <asm/byteorder.h> 23e05b3b4aSPaul Mackerras #include <asm/uaccess.h> 24e05b3b4aSPaul Mackerras #include <asm/machdep.h> 25e05b3b4aSPaul Mackerras 26e05b3b4aSPaul Mackerras #undef DEBUG 27e05b3b4aSPaul Mackerras 28e05b3b4aSPaul Mackerras #ifdef DEBUG 29e05b3b4aSPaul Mackerras #define DBG(x...) printk(x) 30e05b3b4aSPaul Mackerras #else 31e05b3b4aSPaul Mackerras #define DBG(x...) 32e05b3b4aSPaul Mackerras #endif 33e05b3b4aSPaul Mackerras 34e05b3b4aSPaul Mackerras unsigned long isa_io_base = 0; 35e05b3b4aSPaul Mackerras unsigned long pci_dram_offset = 0; 36e05b3b4aSPaul Mackerras int pcibios_assign_bus_offset = 1; 37e05b3b4aSPaul Mackerras 38e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void); 39e05b3b4aSPaul Mackerras 40e05b3b4aSPaul Mackerras static void fixup_broken_pcnet32(struct pci_dev* dev); 41e05b3b4aSPaul Mackerras static void fixup_cpc710_pci64(struct pci_dev* dev); 42e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF 43e05b3b4aSPaul Mackerras static u8* pci_to_OF_bus_map; 44e05b3b4aSPaul Mackerras #endif 45e05b3b4aSPaul Mackerras 46e05b3b4aSPaul Mackerras /* By default, we don't re-assign bus numbers. We do this only on 47e05b3b4aSPaul Mackerras * some pmacs 48e05b3b4aSPaul Mackerras */ 49fc3fb71cSBenjamin Herrenschmidt static int pci_assign_all_buses; 50e05b3b4aSPaul Mackerras 51a4c9e328SKumar Gala LIST_HEAD(hose_list); 52e05b3b4aSPaul Mackerras 53e05b3b4aSPaul Mackerras static int pci_bus_count; 54e05b3b4aSPaul Mackerras 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 602052d6d2SKumar Gala if ((class == PCI_CLASS_PROCESSOR_POWERPC) && 612052d6d2SKumar Gala (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) && 622052d6d2SKumar Gala (dev->bus->parent == NULL)) { 632052d6d2SKumar Gala for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 642052d6d2SKumar Gala dev->resource[i].start = 0; 652052d6d2SKumar Gala dev->resource[i].end = 0; 662052d6d2SKumar Gala dev->resource[i].flags = 0; 672052d6d2SKumar Gala } 682052d6d2SKumar Gala } 692052d6d2SKumar Gala } 702052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MOTOROLA, PCI_ANY_ID, fixup_hide_host_resource_fsl); 712052d6d2SKumar Gala DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_FREESCALE, PCI_ANY_ID, fixup_hide_host_resource_fsl); 722052d6d2SKumar Gala 732052d6d2SKumar Gala static void 74e05b3b4aSPaul Mackerras fixup_broken_pcnet32(struct pci_dev* dev) 75e05b3b4aSPaul Mackerras { 76e05b3b4aSPaul Mackerras if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) { 77e05b3b4aSPaul Mackerras dev->vendor = PCI_VENDOR_ID_AMD; 78e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD); 79e05b3b4aSPaul Mackerras } 80e05b3b4aSPaul Mackerras } 81e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32); 82e05b3b4aSPaul Mackerras 83e05b3b4aSPaul Mackerras static void 84e05b3b4aSPaul Mackerras fixup_cpc710_pci64(struct pci_dev* dev) 85e05b3b4aSPaul Mackerras { 86e05b3b4aSPaul Mackerras /* Hide the PCI64 BARs from the kernel as their content doesn't 87e05b3b4aSPaul Mackerras * fit well in the resource management 88e05b3b4aSPaul Mackerras */ 89e05b3b4aSPaul Mackerras dev->resource[0].start = dev->resource[0].end = 0; 90e05b3b4aSPaul Mackerras dev->resource[0].flags = 0; 91e05b3b4aSPaul Mackerras dev->resource[1].start = dev->resource[1].end = 0; 92e05b3b4aSPaul Mackerras dev->resource[1].flags = 0; 93e05b3b4aSPaul Mackerras } 94e05b3b4aSPaul Mackerras DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64); 95e05b3b4aSPaul Mackerras 96e05b3b4aSPaul Mackerras 97f64fddbeSKumar Gala void __init 98f64fddbeSKumar Gala update_bridge_resource(struct pci_dev *dev, struct resource *res) 99e05b3b4aSPaul Mackerras { 100e05b3b4aSPaul Mackerras u8 io_base_lo, io_limit_lo; 101e05b3b4aSPaul Mackerras u16 mem_base, mem_limit; 102e05b3b4aSPaul Mackerras u16 cmd; 10305d3957eSBenjamin Herrenschmidt resource_size_t start, end, off; 104e05b3b4aSPaul Mackerras struct pci_controller *hose = dev->sysdata; 105e05b3b4aSPaul Mackerras 106e05b3b4aSPaul Mackerras if (!hose) { 107e05b3b4aSPaul Mackerras printk("update_bridge_base: no hose?\n"); 108e05b3b4aSPaul Mackerras return; 109e05b3b4aSPaul Mackerras } 110e05b3b4aSPaul Mackerras pci_read_config_word(dev, PCI_COMMAND, &cmd); 111e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_COMMAND, 112e05b3b4aSPaul Mackerras cmd & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)); 113e05b3b4aSPaul Mackerras if (res->flags & IORESOURCE_IO) { 114e05b3b4aSPaul Mackerras off = (unsigned long) hose->io_base_virt - isa_io_base; 115e05b3b4aSPaul Mackerras start = res->start - off; 116e05b3b4aSPaul Mackerras end = res->end - off; 117e05b3b4aSPaul Mackerras io_base_lo = (start >> 8) & PCI_IO_RANGE_MASK; 118e05b3b4aSPaul Mackerras io_limit_lo = (end >> 8) & PCI_IO_RANGE_MASK; 11960b2a46cSRandy Vinson if (end > 0xffff) 12060b2a46cSRandy Vinson io_base_lo |= PCI_IO_RANGE_TYPE_32; 12160b2a46cSRandy Vinson else 12260b2a46cSRandy Vinson io_base_lo |= PCI_IO_RANGE_TYPE_16; 123e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_IO_BASE_UPPER16, 124e05b3b4aSPaul Mackerras start >> 16); 125e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16, 126e05b3b4aSPaul Mackerras end >> 16); 127e05b3b4aSPaul Mackerras pci_write_config_byte(dev, PCI_IO_BASE, io_base_lo); 128e05b3b4aSPaul Mackerras pci_write_config_byte(dev, PCI_IO_LIMIT, io_limit_lo); 129e05b3b4aSPaul Mackerras 130e05b3b4aSPaul Mackerras } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) 131e05b3b4aSPaul Mackerras == IORESOURCE_MEM) { 132e05b3b4aSPaul Mackerras off = hose->pci_mem_offset; 133e05b3b4aSPaul Mackerras mem_base = ((res->start - off) >> 16) & PCI_MEMORY_RANGE_MASK; 134e05b3b4aSPaul Mackerras mem_limit = ((res->end - off) >> 16) & PCI_MEMORY_RANGE_MASK; 135e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_MEMORY_BASE, mem_base); 136e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_MEMORY_LIMIT, mem_limit); 137e05b3b4aSPaul Mackerras 138e05b3b4aSPaul Mackerras } else if ((res->flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) 139e05b3b4aSPaul Mackerras == (IORESOURCE_MEM | IORESOURCE_PREFETCH)) { 140e05b3b4aSPaul Mackerras off = hose->pci_mem_offset; 141e05b3b4aSPaul Mackerras mem_base = ((res->start - off) >> 16) & PCI_PREF_RANGE_MASK; 142e05b3b4aSPaul Mackerras mem_limit = ((res->end - off) >> 16) & PCI_PREF_RANGE_MASK; 143e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_PREF_MEMORY_BASE, mem_base); 144e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, mem_limit); 145e05b3b4aSPaul Mackerras 146e05b3b4aSPaul Mackerras } else { 147f64fddbeSKumar Gala DBG(KERN_ERR "PCI: ugh, bridge %s res has flags=%lx\n", 148f64fddbeSKumar Gala pci_name(dev), res->flags); 149e05b3b4aSPaul Mackerras } 150e05b3b4aSPaul Mackerras pci_write_config_word(dev, PCI_COMMAND, cmd); 151e05b3b4aSPaul Mackerras } 152e05b3b4aSPaul Mackerras 153e05b3b4aSPaul Mackerras 154e05b3b4aSPaul Mackerras #ifdef CONFIG_PPC_OF 155e05b3b4aSPaul Mackerras /* 156e05b3b4aSPaul Mackerras * Functions below are used on OpenFirmware machines. 157e05b3b4aSPaul Mackerras */ 158e05b3b4aSPaul Mackerras static void 159e05b3b4aSPaul Mackerras make_one_node_map(struct device_node* node, u8 pci_bus) 160e05b3b4aSPaul Mackerras { 161a7f67bdfSJeremy Kerr const int *bus_range; 162e05b3b4aSPaul Mackerras int len; 163e05b3b4aSPaul Mackerras 164e05b3b4aSPaul Mackerras if (pci_bus >= pci_bus_count) 165e05b3b4aSPaul Mackerras return; 166e2eb6392SStephen Rothwell bus_range = of_get_property(node, "bus-range", &len); 167e05b3b4aSPaul Mackerras if (bus_range == NULL || len < 2 * sizeof(int)) { 168e05b3b4aSPaul Mackerras printk(KERN_WARNING "Can't get bus-range for %s, " 169e05b3b4aSPaul Mackerras "assuming it starts at 0\n", node->full_name); 170e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = 0; 171e05b3b4aSPaul Mackerras } else 172e05b3b4aSPaul Mackerras pci_to_OF_bus_map[pci_bus] = bus_range[0]; 173e05b3b4aSPaul Mackerras 174e05b3b4aSPaul Mackerras for (node=node->child; node != 0;node = node->sibling) { 175e05b3b4aSPaul Mackerras struct pci_dev* dev; 176a7f67bdfSJeremy Kerr const unsigned int *class_code, *reg; 177e05b3b4aSPaul Mackerras 178e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 179e05b3b4aSPaul Mackerras if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 180e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) 181e05b3b4aSPaul Mackerras continue; 182e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 183e05b3b4aSPaul Mackerras if (!reg) 184e05b3b4aSPaul Mackerras continue; 185ab462768SAlan Cox dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff)); 186ab462768SAlan Cox if (!dev || !dev->subordinate) { 187ab462768SAlan Cox pci_dev_put(dev); 188e05b3b4aSPaul Mackerras continue; 189ab462768SAlan Cox } 190e05b3b4aSPaul Mackerras make_one_node_map(node, dev->subordinate->number); 191ab462768SAlan Cox pci_dev_put(dev); 192e05b3b4aSPaul Mackerras } 193e05b3b4aSPaul Mackerras } 194e05b3b4aSPaul Mackerras 195e05b3b4aSPaul Mackerras void 196e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(void) 197e05b3b4aSPaul Mackerras { 198e05b3b4aSPaul Mackerras int i; 199a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 200a7f67bdfSJeremy Kerr struct property *map_prop; 2018c8dc322SStephen Rothwell struct device_node *dn; 202e05b3b4aSPaul Mackerras 2035cbded58SRobert P. J. Day pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL); 204e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) { 205e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't allocate OF bus map !\n"); 206e05b3b4aSPaul Mackerras return; 207e05b3b4aSPaul Mackerras } 208e05b3b4aSPaul Mackerras 209e05b3b4aSPaul Mackerras /* We fill the bus map with invalid values, that helps 210e05b3b4aSPaul Mackerras * debugging. 211e05b3b4aSPaul Mackerras */ 212e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) 213e05b3b4aSPaul Mackerras pci_to_OF_bus_map[i] = 0xff; 214e05b3b4aSPaul Mackerras 215e05b3b4aSPaul Mackerras /* For each hose, we begin searching bridges */ 216a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 21744ef3390SStephen Rothwell struct device_node* node = hose->dn; 21844ef3390SStephen Rothwell 219e05b3b4aSPaul Mackerras if (!node) 220e05b3b4aSPaul Mackerras continue; 221e05b3b4aSPaul Mackerras make_one_node_map(node, hose->first_busno); 222e05b3b4aSPaul Mackerras } 2238c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 2248c8dc322SStephen Rothwell map_prop = of_find_property(dn, "pci-OF-bus-map", NULL); 225a7f67bdfSJeremy Kerr if (map_prop) { 226a7f67bdfSJeremy Kerr BUG_ON(pci_bus_count > map_prop->length); 227a7f67bdfSJeremy Kerr memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count); 228a7f67bdfSJeremy Kerr } 2298c8dc322SStephen Rothwell of_node_put(dn); 230e05b3b4aSPaul Mackerras #ifdef DEBUG 231e05b3b4aSPaul Mackerras printk("PCI->OF bus map:\n"); 232e05b3b4aSPaul Mackerras for (i=0; i<pci_bus_count; i++) { 233e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[i] == 0xff) 234e05b3b4aSPaul Mackerras continue; 235e05b3b4aSPaul Mackerras printk("%d -> %d\n", i, pci_to_OF_bus_map[i]); 236e05b3b4aSPaul Mackerras } 237e05b3b4aSPaul Mackerras #endif 238e05b3b4aSPaul Mackerras } 239e05b3b4aSPaul Mackerras 240e05b3b4aSPaul Mackerras typedef int (*pci_OF_scan_iterator)(struct device_node* node, void* data); 241e05b3b4aSPaul Mackerras 242e05b3b4aSPaul Mackerras static struct device_node* 243e05b3b4aSPaul Mackerras scan_OF_pci_childs(struct device_node* node, pci_OF_scan_iterator filter, void* data) 244e05b3b4aSPaul Mackerras { 245e05b3b4aSPaul Mackerras struct device_node* sub_node; 246e05b3b4aSPaul Mackerras 247e05b3b4aSPaul Mackerras for (; node != 0;node = node->sibling) { 248a7f67bdfSJeremy Kerr const unsigned int *class_code; 249e05b3b4aSPaul Mackerras 250e05b3b4aSPaul Mackerras if (filter(node, data)) 251e05b3b4aSPaul Mackerras return node; 252e05b3b4aSPaul Mackerras 253e05b3b4aSPaul Mackerras /* For PCI<->PCI bridges or CardBus bridges, we go down 254e05b3b4aSPaul Mackerras * Note: some OFs create a parent node "multifunc-device" as 255e05b3b4aSPaul Mackerras * a fake root for all functions of a multi-function device, 256e05b3b4aSPaul Mackerras * we go down them as well. 257e05b3b4aSPaul Mackerras */ 258e2eb6392SStephen Rothwell class_code = of_get_property(node, "class-code", NULL); 259e05b3b4aSPaul Mackerras if ((!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && 260e05b3b4aSPaul Mackerras (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) && 261e05b3b4aSPaul Mackerras strcmp(node->name, "multifunc-device")) 262e05b3b4aSPaul Mackerras continue; 263e05b3b4aSPaul Mackerras sub_node = scan_OF_pci_childs(node->child, filter, data); 264e05b3b4aSPaul Mackerras if (sub_node) 265e05b3b4aSPaul Mackerras return sub_node; 266e05b3b4aSPaul Mackerras } 267e05b3b4aSPaul Mackerras return NULL; 268e05b3b4aSPaul Mackerras } 269e05b3b4aSPaul Mackerras 270dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_dev(struct device_node *parent, 271dae4828dSBenjamin Herrenschmidt unsigned int devfn) 272e05b3b4aSPaul Mackerras { 273dae4828dSBenjamin Herrenschmidt struct device_node *np = NULL; 274dae4828dSBenjamin Herrenschmidt const u32 *reg; 275dae4828dSBenjamin Herrenschmidt unsigned int psize; 276e05b3b4aSPaul Mackerras 277dae4828dSBenjamin Herrenschmidt while ((np = of_get_next_child(parent, np)) != NULL) { 278e2eb6392SStephen Rothwell reg = of_get_property(np, "reg", &psize); 279dae4828dSBenjamin Herrenschmidt if (reg == NULL || psize < 4) 280dae4828dSBenjamin Herrenschmidt continue; 281dae4828dSBenjamin Herrenschmidt if (((reg[0] >> 8) & 0xff) == devfn) 282dae4828dSBenjamin Herrenschmidt return np; 283dae4828dSBenjamin Herrenschmidt } 284dae4828dSBenjamin Herrenschmidt return NULL; 285e05b3b4aSPaul Mackerras } 286e05b3b4aSPaul Mackerras 287e05b3b4aSPaul Mackerras 288dae4828dSBenjamin Herrenschmidt static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus) 289dae4828dSBenjamin Herrenschmidt { 290dae4828dSBenjamin Herrenschmidt struct device_node *parent, *np; 291dae4828dSBenjamin Herrenschmidt 292dae4828dSBenjamin Herrenschmidt /* Are we a root bus ? */ 293dae4828dSBenjamin Herrenschmidt if (bus->self == NULL || bus->parent == NULL) { 2940b1d40c4SKumar Gala struct pci_controller *hose = pci_bus_to_host(bus); 295dae4828dSBenjamin Herrenschmidt if (hose == NULL) 296dae4828dSBenjamin Herrenschmidt return NULL; 29744ef3390SStephen Rothwell return of_node_get(hose->dn); 298dae4828dSBenjamin Herrenschmidt } 299dae4828dSBenjamin Herrenschmidt 300dae4828dSBenjamin Herrenschmidt /* not a root bus, we need to get our parent */ 301dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus->parent); 302dae4828dSBenjamin Herrenschmidt if (parent == NULL) 303dae4828dSBenjamin Herrenschmidt return NULL; 304dae4828dSBenjamin Herrenschmidt 305dae4828dSBenjamin Herrenschmidt /* now iterate for children for a match */ 306dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, bus->self->devfn); 307dae4828dSBenjamin Herrenschmidt of_node_put(parent); 308dae4828dSBenjamin Herrenschmidt 309dae4828dSBenjamin Herrenschmidt return np; 310e05b3b4aSPaul Mackerras } 311e05b3b4aSPaul Mackerras 312e05b3b4aSPaul Mackerras /* 313e05b3b4aSPaul Mackerras * Scans the OF tree for a device node matching a PCI device 314e05b3b4aSPaul Mackerras */ 315e05b3b4aSPaul Mackerras struct device_node * 316e05b3b4aSPaul Mackerras pci_busdev_to_OF_node(struct pci_bus *bus, int devfn) 317e05b3b4aSPaul Mackerras { 318dae4828dSBenjamin Herrenschmidt struct device_node *parent, *np; 319e05b3b4aSPaul Mackerras 320e05b3b4aSPaul Mackerras if (!have_of) 321e05b3b4aSPaul Mackerras return NULL; 322e05b3b4aSPaul Mackerras 323dae4828dSBenjamin Herrenschmidt DBG("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn); 324dae4828dSBenjamin Herrenschmidt parent = scan_OF_for_pci_bus(bus); 325dae4828dSBenjamin Herrenschmidt if (parent == NULL) 326e05b3b4aSPaul Mackerras return NULL; 327dae4828dSBenjamin Herrenschmidt DBG(" parent is %s\n", parent ? parent->full_name : "<NULL>"); 328dae4828dSBenjamin Herrenschmidt np = scan_OF_for_pci_dev(parent, devfn); 329dae4828dSBenjamin Herrenschmidt of_node_put(parent); 330dae4828dSBenjamin Herrenschmidt DBG(" result is %s\n", np ? np->full_name : "<NULL>"); 331e05b3b4aSPaul Mackerras 332dae4828dSBenjamin Herrenschmidt /* XXX most callers don't release the returned node 333dae4828dSBenjamin Herrenschmidt * mostly because ppc64 doesn't increase the refcount, 334dae4828dSBenjamin Herrenschmidt * we need to fix that. 335e05b3b4aSPaul Mackerras */ 336dae4828dSBenjamin Herrenschmidt return np; 337e05b3b4aSPaul Mackerras } 338e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_busdev_to_OF_node); 339e05b3b4aSPaul Mackerras 340e05b3b4aSPaul Mackerras struct device_node* 341e05b3b4aSPaul Mackerras pci_device_to_OF_node(struct pci_dev *dev) 342e05b3b4aSPaul Mackerras { 343e05b3b4aSPaul Mackerras return pci_busdev_to_OF_node(dev->bus, dev->devfn); 344e05b3b4aSPaul Mackerras } 345e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_to_OF_node); 346e05b3b4aSPaul Mackerras 347e05b3b4aSPaul Mackerras static int 348e05b3b4aSPaul Mackerras find_OF_pci_device_filter(struct device_node* node, void* data) 349e05b3b4aSPaul Mackerras { 350e05b3b4aSPaul Mackerras return ((void *)node == data); 351e05b3b4aSPaul Mackerras } 352e05b3b4aSPaul Mackerras 353e05b3b4aSPaul Mackerras /* 354e05b3b4aSPaul Mackerras * Returns the PCI device matching a given OF node 355e05b3b4aSPaul Mackerras */ 356e05b3b4aSPaul Mackerras int 357e05b3b4aSPaul Mackerras pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn) 358e05b3b4aSPaul Mackerras { 359a7f67bdfSJeremy Kerr const unsigned int *reg; 360e05b3b4aSPaul Mackerras struct pci_controller* hose; 361e05b3b4aSPaul Mackerras struct pci_dev* dev = NULL; 362e05b3b4aSPaul Mackerras 363e05b3b4aSPaul Mackerras if (!have_of) 364e05b3b4aSPaul Mackerras return -ENODEV; 365e05b3b4aSPaul Mackerras /* Make sure it's really a PCI device */ 366e05b3b4aSPaul Mackerras hose = pci_find_hose_for_OF_device(node); 36744ef3390SStephen Rothwell if (!hose || !hose->dn) 368e05b3b4aSPaul Mackerras return -ENODEV; 36944ef3390SStephen Rothwell if (!scan_OF_pci_childs(hose->dn->child, 370e05b3b4aSPaul Mackerras find_OF_pci_device_filter, (void *)node)) 371e05b3b4aSPaul Mackerras return -ENODEV; 372e2eb6392SStephen Rothwell reg = of_get_property(node, "reg", NULL); 373e05b3b4aSPaul Mackerras if (!reg) 374e05b3b4aSPaul Mackerras return -ENODEV; 375e05b3b4aSPaul Mackerras *bus = (reg[0] >> 16) & 0xff; 376e05b3b4aSPaul Mackerras *devfn = ((reg[0] >> 8) & 0xff); 377e05b3b4aSPaul Mackerras 378e05b3b4aSPaul Mackerras /* Ok, here we need some tweak. If we have already renumbered 379e05b3b4aSPaul Mackerras * all busses, we can't rely on the OF bus number any more. 380e05b3b4aSPaul Mackerras * the pci_to_OF_bus_map is not enough as several PCI busses 381e05b3b4aSPaul Mackerras * may match the same OF bus number. 382e05b3b4aSPaul Mackerras */ 383e05b3b4aSPaul Mackerras if (!pci_to_OF_bus_map) 384e05b3b4aSPaul Mackerras return 0; 385e05b3b4aSPaul Mackerras 386e05b3b4aSPaul Mackerras for_each_pci_dev(dev) 387e05b3b4aSPaul Mackerras if (pci_to_OF_bus_map[dev->bus->number] == *bus && 388e05b3b4aSPaul Mackerras dev->devfn == *devfn) { 389e05b3b4aSPaul Mackerras *bus = dev->bus->number; 390e05b3b4aSPaul Mackerras pci_dev_put(dev); 391e05b3b4aSPaul Mackerras return 0; 392e05b3b4aSPaul Mackerras } 393e05b3b4aSPaul Mackerras 394e05b3b4aSPaul Mackerras return -ENODEV; 395e05b3b4aSPaul Mackerras } 396e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_device_from_OF_node); 397e05b3b4aSPaul Mackerras 398e05b3b4aSPaul Mackerras /* We create the "pci-OF-bus-map" property now so it appears in the 399e05b3b4aSPaul Mackerras * /proc device tree 400e05b3b4aSPaul Mackerras */ 401e05b3b4aSPaul Mackerras void __init 402e05b3b4aSPaul Mackerras pci_create_OF_bus_map(void) 403e05b3b4aSPaul Mackerras { 404e05b3b4aSPaul Mackerras struct property* of_prop; 4058c8dc322SStephen Rothwell struct device_node *dn; 406e05b3b4aSPaul Mackerras 407e05b3b4aSPaul Mackerras of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256); 4088c8dc322SStephen Rothwell if (!of_prop) 4098c8dc322SStephen Rothwell return; 4108c8dc322SStephen Rothwell dn = of_find_node_by_path("/"); 4118c8dc322SStephen Rothwell if (dn) { 412e05b3b4aSPaul Mackerras memset(of_prop, -1, sizeof(struct property) + 256); 413e05b3b4aSPaul Mackerras of_prop->name = "pci-OF-bus-map"; 414e05b3b4aSPaul Mackerras of_prop->length = 256; 4151a38147eSStephen Rothwell of_prop->value = &of_prop[1]; 4168c8dc322SStephen Rothwell prom_add_property(dn, of_prop); 4178c8dc322SStephen Rothwell of_node_put(dn); 418e05b3b4aSPaul Mackerras } 419e05b3b4aSPaul Mackerras } 420e05b3b4aSPaul Mackerras 421e05b3b4aSPaul Mackerras #else /* CONFIG_PPC_OF */ 422e05b3b4aSPaul Mackerras void pcibios_make_OF_bus_map(void) 423e05b3b4aSPaul Mackerras { 424e05b3b4aSPaul Mackerras } 425e05b3b4aSPaul Mackerras #endif /* CONFIG_PPC_OF */ 426e05b3b4aSPaul Mackerras 4273fd94c6bSBenjamin Herrenschmidt static int __init pcibios_init(void) 428e05b3b4aSPaul Mackerras { 429a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 430e05b3b4aSPaul Mackerras struct pci_bus *bus; 431a4c9e328SKumar Gala int next_busno = 0; 432e05b3b4aSPaul Mackerras 433e05b3b4aSPaul Mackerras printk(KERN_INFO "PCI: Probing PCI hardware\n"); 434e05b3b4aSPaul Mackerras 435fc3fb71cSBenjamin Herrenschmidt if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS) 436fc3fb71cSBenjamin Herrenschmidt pci_assign_all_buses = 1; 437fc3fb71cSBenjamin Herrenschmidt 438e05b3b4aSPaul Mackerras /* Scan all of the recorded PCI controllers. */ 439a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 440e05b3b4aSPaul Mackerras if (pci_assign_all_buses) 441e05b3b4aSPaul Mackerras hose->first_busno = next_busno; 442e05b3b4aSPaul Mackerras hose->last_busno = 0xff; 443803d4573SBenjamin Herrenschmidt bus = pci_scan_bus_parented(hose->parent, hose->first_busno, 444803d4573SBenjamin Herrenschmidt hose->ops, hose); 445b1b166b7SBenjamin Herrenschmidt if (bus) { 446803d4573SBenjamin Herrenschmidt pci_bus_add_devices(bus); 447e05b3b4aSPaul Mackerras hose->last_busno = bus->subordinate; 448b1b166b7SBenjamin Herrenschmidt } 449e05b3b4aSPaul Mackerras if (pci_assign_all_buses || next_busno <= hose->last_busno) 450e05b3b4aSPaul Mackerras next_busno = hose->last_busno + pcibios_assign_bus_offset; 451e05b3b4aSPaul Mackerras } 452e05b3b4aSPaul Mackerras pci_bus_count = next_busno; 453e05b3b4aSPaul Mackerras 454e05b3b4aSPaul Mackerras /* OpenFirmware based machines need a map of OF bus 455e05b3b4aSPaul Mackerras * numbers vs. kernel bus numbers since we may have to 456e05b3b4aSPaul Mackerras * remap them. 457e05b3b4aSPaul Mackerras */ 458e05b3b4aSPaul Mackerras if (pci_assign_all_buses && have_of) 459e05b3b4aSPaul Mackerras pcibios_make_OF_bus_map(); 460e05b3b4aSPaul Mackerras 4613fd94c6bSBenjamin Herrenschmidt /* Call common code to handle resource allocation */ 4623fd94c6bSBenjamin Herrenschmidt pcibios_resource_survey(); 463e05b3b4aSPaul Mackerras 464e05b3b4aSPaul Mackerras /* Call machine dependent post-init code */ 465e05b3b4aSPaul Mackerras if (ppc_md.pcibios_after_init) 466e05b3b4aSPaul Mackerras ppc_md.pcibios_after_init(); 467e05b3b4aSPaul Mackerras 468e05b3b4aSPaul Mackerras return 0; 469e05b3b4aSPaul Mackerras } 470e05b3b4aSPaul Mackerras 471e05b3b4aSPaul Mackerras subsys_initcall(pcibios_init); 472e05b3b4aSPaul Mackerras 473bf5e2ba2SBenjamin Herrenschmidt void __devinit pcibios_do_bus_setup(struct pci_bus *bus) 474e05b3b4aSPaul Mackerras { 475e05b3b4aSPaul Mackerras struct pci_controller *hose = (struct pci_controller *) bus->sysdata; 476e05b3b4aSPaul Mackerras unsigned long io_offset; 477e05b3b4aSPaul Mackerras struct resource *res; 478e05b3b4aSPaul Mackerras int i; 479e05b3b4aSPaul Mackerras 480bf5e2ba2SBenjamin Herrenschmidt /* Hookup PHB resources */ 481e05b3b4aSPaul Mackerras io_offset = (unsigned long)hose->io_base_virt - isa_io_base; 482e05b3b4aSPaul Mackerras if (bus->parent == NULL) { 483e05b3b4aSPaul Mackerras /* This is a host bridge - fill in its resources */ 484e05b3b4aSPaul Mackerras hose->bus = bus; 485e05b3b4aSPaul Mackerras 486e05b3b4aSPaul Mackerras bus->resource[0] = res = &hose->io_resource; 487e05b3b4aSPaul Mackerras if (!res->flags) { 488e05b3b4aSPaul Mackerras if (io_offset) 489e05b3b4aSPaul Mackerras printk(KERN_ERR "I/O resource not set for host" 4905516b540SKumar Gala " bridge %d\n", hose->global_number); 491e05b3b4aSPaul Mackerras res->start = 0; 492e05b3b4aSPaul Mackerras res->end = IO_SPACE_LIMIT; 493e05b3b4aSPaul Mackerras res->flags = IORESOURCE_IO; 494e05b3b4aSPaul Mackerras } 49505d3957eSBenjamin Herrenschmidt res->start = (res->start + io_offset) & 0xffffffffu; 49605d3957eSBenjamin Herrenschmidt res->end = (res->end + io_offset) & 0xffffffffu; 497e05b3b4aSPaul Mackerras 498e05b3b4aSPaul Mackerras for (i = 0; i < 3; ++i) { 499e05b3b4aSPaul Mackerras res = &hose->mem_resources[i]; 500e05b3b4aSPaul Mackerras if (!res->flags) { 501e05b3b4aSPaul Mackerras if (i > 0) 502e05b3b4aSPaul Mackerras continue; 503e05b3b4aSPaul Mackerras printk(KERN_ERR "Memory resource not set for " 5045516b540SKumar Gala "host bridge %d\n", hose->global_number); 505e05b3b4aSPaul Mackerras res->start = hose->pci_mem_offset; 506e05b3b4aSPaul Mackerras res->end = ~0U; 507e05b3b4aSPaul Mackerras res->flags = IORESOURCE_MEM; 508e05b3b4aSPaul Mackerras } 509e05b3b4aSPaul Mackerras bus->resource[i+1] = res; 510e05b3b4aSPaul Mackerras } 511f90bb153SBenjamin Herrenschmidt } 512e05b3b4aSPaul Mackerras } 513e05b3b4aSPaul Mackerras 514e05b3b4aSPaul Mackerras /* the next one is stolen from the alpha port... */ 515e05b3b4aSPaul Mackerras void __init 516e05b3b4aSPaul Mackerras pcibios_update_irq(struct pci_dev *dev, int irq) 517e05b3b4aSPaul Mackerras { 518e05b3b4aSPaul Mackerras pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq); 519e05b3b4aSPaul Mackerras /* XXX FIXME - update OF device tree node interrupt property */ 520e05b3b4aSPaul Mackerras } 521e05b3b4aSPaul Mackerras 5220b1d40c4SKumar Gala static struct pci_controller* 523e05b3b4aSPaul Mackerras pci_bus_to_hose(int bus) 524e05b3b4aSPaul Mackerras { 525a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 526e05b3b4aSPaul Mackerras 527a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) 528e05b3b4aSPaul Mackerras if (bus >= hose->first_busno && bus <= hose->last_busno) 529e05b3b4aSPaul Mackerras return hose; 530e05b3b4aSPaul Mackerras return NULL; 531e05b3b4aSPaul Mackerras } 532e05b3b4aSPaul Mackerras 533e05b3b4aSPaul Mackerras /* Provide information on locations of various I/O regions in physical 534e05b3b4aSPaul Mackerras * memory. Do this on a per-card basis so that we choose the right 535e05b3b4aSPaul Mackerras * root bridge. 536e05b3b4aSPaul Mackerras * Note that the returned IO or memory base is a physical address 537e05b3b4aSPaul Mackerras */ 538e05b3b4aSPaul Mackerras 539e05b3b4aSPaul Mackerras long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn) 540e05b3b4aSPaul Mackerras { 541e05b3b4aSPaul Mackerras struct pci_controller* hose; 542e05b3b4aSPaul Mackerras long result = -EOPNOTSUPP; 543e05b3b4aSPaul Mackerras 544e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(bus); 545e05b3b4aSPaul Mackerras if (!hose) 546e05b3b4aSPaul Mackerras return -ENODEV; 547e05b3b4aSPaul Mackerras 548e05b3b4aSPaul Mackerras switch (which) { 549e05b3b4aSPaul Mackerras case IOBASE_BRIDGE_NUMBER: 550e05b3b4aSPaul Mackerras return (long)hose->first_busno; 551e05b3b4aSPaul Mackerras case IOBASE_MEMORY: 552e05b3b4aSPaul Mackerras return (long)hose->pci_mem_offset; 553e05b3b4aSPaul Mackerras case IOBASE_IO: 554e05b3b4aSPaul Mackerras return (long)hose->io_base_phys; 555e05b3b4aSPaul Mackerras case IOBASE_ISA_IO: 556e05b3b4aSPaul Mackerras return (long)isa_io_base; 557e05b3b4aSPaul Mackerras case IOBASE_ISA_MEM: 558e05b3b4aSPaul Mackerras return (long)isa_mem_base; 559e05b3b4aSPaul Mackerras } 560e05b3b4aSPaul Mackerras 561e05b3b4aSPaul Mackerras return result; 562e05b3b4aSPaul Mackerras } 563e05b3b4aSPaul Mackerras 564e05b3b4aSPaul Mackerras unsigned long pci_address_to_pio(phys_addr_t address) 565e05b3b4aSPaul Mackerras { 566a4c9e328SKumar Gala struct pci_controller *hose, *tmp; 567e05b3b4aSPaul Mackerras 568a4c9e328SKumar Gala list_for_each_entry_safe(hose, tmp, &hose_list, list_node) { 569e05b3b4aSPaul Mackerras unsigned int size = hose->io_resource.end - 570e05b3b4aSPaul Mackerras hose->io_resource.start + 1; 571e05b3b4aSPaul Mackerras if (address >= hose->io_base_phys && 572e05b3b4aSPaul Mackerras address < (hose->io_base_phys + size)) { 573e05b3b4aSPaul Mackerras unsigned long base = 574e05b3b4aSPaul Mackerras (unsigned long)hose->io_base_virt - _IO_BASE; 575e05b3b4aSPaul Mackerras return base + (address - hose->io_base_phys); 576e05b3b4aSPaul Mackerras } 577e05b3b4aSPaul Mackerras } 578e05b3b4aSPaul Mackerras return (unsigned int)-1; 579e05b3b4aSPaul Mackerras } 580e05b3b4aSPaul Mackerras EXPORT_SYMBOL(pci_address_to_pio); 581e05b3b4aSPaul Mackerras 582e05b3b4aSPaul Mackerras /* 583e05b3b4aSPaul Mackerras * Null PCI config access functions, for the case when we can't 584e05b3b4aSPaul Mackerras * find a hose. 585e05b3b4aSPaul Mackerras */ 586e05b3b4aSPaul Mackerras #define NULL_PCI_OP(rw, size, type) \ 587e05b3b4aSPaul Mackerras static int \ 588e05b3b4aSPaul Mackerras null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \ 589e05b3b4aSPaul Mackerras { \ 590e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; \ 591e05b3b4aSPaul Mackerras } 592e05b3b4aSPaul Mackerras 593e05b3b4aSPaul Mackerras static int 594e05b3b4aSPaul Mackerras null_read_config(struct pci_bus *bus, unsigned int devfn, int offset, 595e05b3b4aSPaul Mackerras int len, u32 *val) 596e05b3b4aSPaul Mackerras { 597e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 598e05b3b4aSPaul Mackerras } 599e05b3b4aSPaul Mackerras 600e05b3b4aSPaul Mackerras static int 601e05b3b4aSPaul Mackerras null_write_config(struct pci_bus *bus, unsigned int devfn, int offset, 602e05b3b4aSPaul Mackerras int len, u32 val) 603e05b3b4aSPaul Mackerras { 604e05b3b4aSPaul Mackerras return PCIBIOS_DEVICE_NOT_FOUND; 605e05b3b4aSPaul Mackerras } 606e05b3b4aSPaul Mackerras 607e05b3b4aSPaul Mackerras static struct pci_ops null_pci_ops = 608e05b3b4aSPaul Mackerras { 6096127d1c0SNathan Lynch .read = null_read_config, 6106127d1c0SNathan Lynch .write = null_write_config, 611e05b3b4aSPaul Mackerras }; 612e05b3b4aSPaul Mackerras 613e05b3b4aSPaul Mackerras /* 614e05b3b4aSPaul Mackerras * These functions are used early on before PCI scanning is done 615e05b3b4aSPaul Mackerras * and all of the pci_dev and pci_bus structures have been created. 616e05b3b4aSPaul Mackerras */ 617e05b3b4aSPaul Mackerras static struct pci_bus * 618e05b3b4aSPaul Mackerras fake_pci_bus(struct pci_controller *hose, int busnr) 619e05b3b4aSPaul Mackerras { 620e05b3b4aSPaul Mackerras static struct pci_bus bus; 621e05b3b4aSPaul Mackerras 622e05b3b4aSPaul Mackerras if (hose == 0) { 623e05b3b4aSPaul Mackerras hose = pci_bus_to_hose(busnr); 624e05b3b4aSPaul Mackerras if (hose == 0) 625e05b3b4aSPaul Mackerras printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr); 626e05b3b4aSPaul Mackerras } 627e05b3b4aSPaul Mackerras bus.number = busnr; 628e05b3b4aSPaul Mackerras bus.sysdata = hose; 629e05b3b4aSPaul Mackerras bus.ops = hose? hose->ops: &null_pci_ops; 630e05b3b4aSPaul Mackerras return &bus; 631e05b3b4aSPaul Mackerras } 632e05b3b4aSPaul Mackerras 633e05b3b4aSPaul Mackerras #define EARLY_PCI_OP(rw, size, type) \ 634e05b3b4aSPaul Mackerras int early_##rw##_config_##size(struct pci_controller *hose, int bus, \ 635e05b3b4aSPaul Mackerras int devfn, int offset, type value) \ 636e05b3b4aSPaul Mackerras { \ 637e05b3b4aSPaul Mackerras return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \ 638e05b3b4aSPaul Mackerras devfn, offset, value); \ 639e05b3b4aSPaul Mackerras } 640e05b3b4aSPaul Mackerras 641e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, byte, u8 *) 642e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, word, u16 *) 643e05b3b4aSPaul Mackerras EARLY_PCI_OP(read, dword, u32 *) 644e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, byte, u8) 645e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, word, u16) 646e05b3b4aSPaul Mackerras EARLY_PCI_OP(write, dword, u32) 64738805e5fSKumar Gala 64838805e5fSKumar Gala extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap); 64938805e5fSKumar Gala int early_find_capability(struct pci_controller *hose, int bus, int devfn, 65038805e5fSKumar Gala int cap) 65138805e5fSKumar Gala { 65238805e5fSKumar Gala return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap); 65338805e5fSKumar Gala } 654