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