[SPARC64]: Handle PCI bridges without 'ranges' property.
[sfrench/cifs-2.6.git] / arch / sparc64 / kernel / pci.c
index d85e1ed7c3e438d3425ab96d103c3f427ca4a38a..81f4a5ea05f7ab948e307daa1853f53d1b554fbd 100644 (file)
@@ -306,6 +306,20 @@ static void __init pci_controller_probe(void)
        pci_controller_scan(pci_controller_init);
 }
 
+static int ofpci_verbose;
+
+static int __init ofpci_debug(char *str)
+{
+       int val = 0;
+
+       get_option(&str, &val);
+       if (val)
+               ofpci_verbose = 1;
+       return 1;
+}
+
+__setup("ofpci_debug=", ofpci_debug);
+
 static unsigned long pci_parse_of_flags(u32 addr0)
 {
        unsigned long flags = 0;
@@ -337,7 +351,9 @@ static void pci_parse_of_addrs(struct of_device *op,
        addrs = of_get_property(node, "assigned-addresses", &proplen);
        if (!addrs)
                return;
-       printk("    parse addresses (%d bytes) @ %p\n", proplen, addrs);
+       if (ofpci_verbose)
+               printk("    parse addresses (%d bytes) @ %p\n",
+                      proplen, addrs);
        op_res = &op->resource[0];
        for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
                struct resource *res;
@@ -348,8 +364,9 @@ static void pci_parse_of_addrs(struct of_device *op,
                if (!flags)
                        continue;
                i = addrs[0] & 0xff;
-               printk("  start: %lx, end: %lx, i: %x\n",
-                      op_res->start, op_res->end, i);
+               if (ofpci_verbose)
+                       printk("  start: %lx, end: %lx, i: %x\n",
+                              op_res->start, op_res->end, i);
 
                if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
                        res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
@@ -377,7 +394,7 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
        const char *type;
        u32 class;
 
-       dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
+       dev = alloc_pci_dev();
        if (!dev)
                return NULL;
 
@@ -393,8 +410,9 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
        if (type == NULL)
                type = "";
 
-       printk("    create device, devfn: %x, type: %s hostcontroller(%d)\n",
-              devfn, type, host_controller);
+       if (ofpci_verbose)
+               printk("    create device, devfn: %x, type: %s\n",
+                      devfn, type);
 
        dev->bus = bus;
        dev->sysdata = node;
@@ -434,8 +452,9 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
                sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
                        dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
        }
-       printk("    class: 0x%x device name: %s\n",
-              dev->class, pci_name(dev));
+       if (ofpci_verbose)
+               printk("    class: 0x%x device name: %s\n",
+                      dev->class, pci_name(dev));
 
        /* I have seen IDE devices which will not respond to
         * the bmdma simplex check reads if bus mastering is
@@ -469,7 +488,8 @@ struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
        }
        pci_parse_of_addrs(sd->op, node, dev);
 
-       printk("    adding to system ...\n");
+       if (ofpci_verbose)
+               printk("    adding to system ...\n");
 
        pci_device_add(dev, bus);
 
@@ -495,13 +515,96 @@ static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
        *last_p = last;
 }
 
-static void __init pci_resource_adjust(struct resource *res,
-                                      struct resource *root)
+static void pci_resource_adjust(struct resource *res,
+                               struct resource *root)
 {
        res->start += root->start;
        res->end += root->start;
 }
 
+/* For PCI bus devices which lack a 'ranges' property we interrogate
+ * the config space values to set the resources, just like the generic
+ * Linux PCI probing code does.
+ */
+static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
+                                         struct pci_bus *bus,
+                                         struct pci_pbm_info *pbm)
+{
+       struct resource *res;
+       u8 io_base_lo, io_limit_lo;
+       u16 mem_base_lo, mem_limit_lo;
+       unsigned long base, limit;
+
+       pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
+       pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
+       base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
+       limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
+
+       if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
+               u16 io_base_hi, io_limit_hi;
+
+               pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
+               pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
+               base |= (io_base_hi << 16);
+               limit |= (io_limit_hi << 16);
+       }
+
+       res = bus->resource[0];
+       if (base <= limit) {
+               res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
+               if (!res->start)
+                       res->start = base;
+               if (!res->end)
+                       res->end = limit + 0xfff;
+               pci_resource_adjust(res, &pbm->io_space);
+       }
+
+       pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
+       pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
+       base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
+       limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
+
+       res = bus->resource[1];
+       if (base <= limit) {
+               res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
+                             IORESOURCE_MEM);
+               res->start = base;
+               res->end = limit + 0xfffff;
+               pci_resource_adjust(res, &pbm->mem_space);
+       }
+
+       pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
+       pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
+       base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
+       limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
+
+       if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
+               u32 mem_base_hi, mem_limit_hi;
+
+               pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
+               pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
+
+               /*
+                * Some bridges set the base > limit by default, and some
+                * (broken) BIOSes do not initialize them.  If we find
+                * this, just assume they are not being used.
+                */
+               if (mem_base_hi <= mem_limit_hi) {
+                       base |= ((long) mem_base_hi) << 32;
+                       limit |= ((long) mem_limit_hi) << 32;
+               }
+       }
+
+       res = bus->resource[2];
+       if (base <= limit) {
+               res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
+                             IORESOURCE_MEM | IORESOURCE_PREFETCH);
+               res->start = base;
+               res->end = limit + 0xfffff;
+               pci_resource_adjust(res, &pbm->mem_space);
+       }
+}
+
 /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  * a proper 'ranges' property.
  */
@@ -547,7 +650,8 @@ static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
        unsigned int flags;
        u64 size;
 
-       printk("of_scan_pci_bridge(%s)\n", node->full_name);
+       if (ofpci_verbose)
+               printk("of_scan_pci_bridge(%s)\n", node->full_name);
 
        /* parse bus-range property */
        busrange = of_get_property(node, "bus-range", &len);
@@ -560,13 +664,8 @@ static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
        simba = 0;
        if (ranges == NULL) {
                const char *model = of_get_property(node, "model", NULL);
-               if (model && !strcmp(model, "SUNW,simba")) {
+               if (model && !strcmp(model, "SUNW,simba"))
                        simba = 1;
-               } else {
-                       printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
-                              node->full_name);
-                       return;
-               }
        }
 
        bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
@@ -590,7 +689,10 @@ static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
        }
        if (simba) {
                apb_fake_ranges(dev, bus, pbm);
-               goto simba_cont;
+               goto after_ranges;
+       } else if (ranges == NULL) {
+               pci_cfg_fake_ranges(dev, bus, pbm);
+               goto after_ranges;
        }
        i = 1;
        for (; len >= 32; len -= 32, ranges += 8) {
@@ -629,10 +731,11 @@ static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
                 */
                pci_resource_adjust(res, root);
        }
-simba_cont:
+after_ranges:
        sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
                bus->number);
-       printk("    bus name: %s\n", bus->name);
+       if (ofpci_verbose)
+               printk("    bus name: %s\n", bus->name);
 
        pci_of_scan_bus(pbm, node, bus);
 }
@@ -646,12 +749,14 @@ static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
        int reglen, devfn;
        struct pci_dev *dev;
 
-       printk("PCI: scan_bus[%s] bus no %d\n",
-              node->full_name, bus->number);
+       if (ofpci_verbose)
+               printk("PCI: scan_bus[%s] bus no %d\n",
+                      node->full_name, bus->number);
 
        child = NULL;
        while ((child = of_get_next_child(node, child)) != NULL) {
-               printk("  * %s\n", child->full_name);
+               if (ofpci_verbose)
+                       printk("  * %s\n", child->full_name);
                reg = of_get_property(child, "reg", &reglen);
                if (reg == NULL || reglen < 20)
                        continue;
@@ -661,7 +766,9 @@ static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
                dev = of_create_pci_dev(pbm, child, bus, devfn, 0);
                if (!dev)
                        continue;
-               printk("PCI: dev header type: %x\n", dev->hdr_type);
+               if (ofpci_verbose)
+                       printk("PCI: dev header type: %x\n",
+                              dev->hdr_type);
 
                if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
                    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)