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