Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Benjamin Herrenschmuidt (benh@kernel.crashing.org), |
| 3 | * IBM Corp. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | */ |
| 10 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 11 | #undef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/pci.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/bootmem.h> |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 19 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include <asm/sections.h> |
| 22 | #include <asm/io.h> |
| 23 | #include <asm/prom.h> |
| 24 | #include <asm/pci-bridge.h> |
| 25 | #include <asm/machdep.h> |
| 26 | #include <asm/iommu.h> |
Stephen Rothwell | d387899 | 2005-09-28 02:50:25 +1000 | [diff] [blame] | 27 | #include <asm/ppc-pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Paul Mackerras | 0cb7b2a | 2005-10-29 22:07:56 +1000 | [diff] [blame] | 29 | #include "maple.h" |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #ifdef DEBUG |
| 32 | #define DBG(x...) printk(x) |
| 33 | #else |
| 34 | #define DBG(x...) |
| 35 | #endif |
| 36 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 37 | static struct pci_controller *u3_agp, *u3_ht, *u4_pcie; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | static int __init fixup_one_level_bus_range(struct device_node *node, int higher) |
| 40 | { |
| 41 | for (; node != 0;node = node->sibling) { |
Jeremy Kerr | eeb2b72 | 2006-07-12 15:40:17 +1000 | [diff] [blame] | 42 | const int *bus_range; |
| 43 | const unsigned int *class_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | int len; |
| 45 | |
| 46 | /* For PCI<->PCI bridges or CardBus bridges, we go down */ |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 47 | class_code = of_get_property(node, "class-code", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI && |
| 49 | (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) |
| 50 | continue; |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 51 | bus_range = of_get_property(node, "bus-range", &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (bus_range != NULL && len > 2 * sizeof(int)) { |
| 53 | if (bus_range[1] > higher) |
| 54 | higher = bus_range[1]; |
| 55 | } |
| 56 | higher = fixup_one_level_bus_range(node->child, higher); |
| 57 | } |
| 58 | return higher; |
| 59 | } |
| 60 | |
| 61 | /* This routine fixes the "bus-range" property of all bridges in the |
| 62 | * system since they tend to have their "last" member wrong on macs |
| 63 | * |
| 64 | * Note that the bus numbers manipulated here are OF bus numbers, they |
| 65 | * are not Linux bus numbers. |
| 66 | */ |
| 67 | static void __init fixup_bus_range(struct device_node *bridge) |
| 68 | { |
Jeremy Kerr | eeb2b72 | 2006-07-12 15:40:17 +1000 | [diff] [blame] | 69 | int *bus_range; |
| 70 | struct property *prop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | int len; |
| 72 | |
| 73 | /* Lookup the "bus-range" property for the hose */ |
Jeremy Kerr | eeb2b72 | 2006-07-12 15:40:17 +1000 | [diff] [blame] | 74 | prop = of_find_property(bridge, "bus-range", &len); |
| 75 | if (prop == NULL || prop->value == NULL || len < 2 * sizeof(int)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | printk(KERN_WARNING "Can't get bus-range for %s\n", |
| 77 | bridge->full_name); |
| 78 | return; |
| 79 | } |
Stephen Rothwell | 1a38147 | 2007-04-03 10:58:52 +1000 | [diff] [blame] | 80 | bus_range = prop->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | bus_range[1] = fixup_one_level_bus_range(bridge->child, bus_range[1]); |
| 82 | } |
| 83 | |
| 84 | |
Nathan Lynch | cc9881c | 2006-09-21 14:31:13 -0500 | [diff] [blame] | 85 | static unsigned long u3_agp_cfa0(u8 devfn, u8 off) |
| 86 | { |
| 87 | return (1 << (unsigned long)PCI_SLOT(devfn)) | |
| 88 | ((unsigned long)PCI_FUNC(devfn) << 8) | |
| 89 | ((unsigned long)off & 0xFCUL); |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Nathan Lynch | cc9881c | 2006-09-21 14:31:13 -0500 | [diff] [blame] | 92 | static unsigned long u3_agp_cfa1(u8 bus, u8 devfn, u8 off) |
| 93 | { |
| 94 | return ((unsigned long)bus << 16) | |
| 95 | ((unsigned long)devfn << 8) | |
| 96 | ((unsigned long)off & 0xFCUL) | |
| 97 | 1UL; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 100 | static volatile void __iomem *u3_agp_cfg_access(struct pci_controller* hose, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | u8 bus, u8 dev_fn, u8 offset) |
| 102 | { |
| 103 | unsigned int caddr; |
| 104 | |
| 105 | if (bus == hose->first_busno) { |
| 106 | if (dev_fn < (11 << 3)) |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 107 | return NULL; |
Nathan Lynch | cc9881c | 2006-09-21 14:31:13 -0500 | [diff] [blame] | 108 | caddr = u3_agp_cfa0(dev_fn, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } else |
Nathan Lynch | cc9881c | 2006-09-21 14:31:13 -0500 | [diff] [blame] | 110 | caddr = u3_agp_cfa1(bus, dev_fn, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | /* Uninorth will return garbage if we don't read back the value ! */ |
| 113 | do { |
| 114 | out_le32(hose->cfg_addr, caddr); |
| 115 | } while (in_le32(hose->cfg_addr) != caddr); |
| 116 | |
| 117 | offset &= 0x07; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 118 | return hose->cfg_data + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int u3_agp_read_config(struct pci_bus *bus, unsigned int devfn, |
| 122 | int offset, int len, u32 *val) |
| 123 | { |
| 124 | struct pci_controller *hose; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 125 | volatile void __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | hose = pci_bus_to_host(bus); |
| 128 | if (hose == NULL) |
| 129 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 130 | |
| 131 | addr = u3_agp_cfg_access(hose, bus->number, devfn, offset); |
| 132 | if (!addr) |
| 133 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 134 | /* |
| 135 | * Note: the caller has already checked that offset is |
| 136 | * suitably aligned and that len is 1, 2 or 4. |
| 137 | */ |
| 138 | switch (len) { |
| 139 | case 1: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 140 | *val = in_8(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | break; |
| 142 | case 2: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 143 | *val = in_le16(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | break; |
| 145 | default: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 146 | *val = in_le32(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | return PCIBIOS_SUCCESSFUL; |
| 150 | } |
| 151 | |
| 152 | static int u3_agp_write_config(struct pci_bus *bus, unsigned int devfn, |
| 153 | int offset, int len, u32 val) |
| 154 | { |
| 155 | struct pci_controller *hose; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 156 | volatile void __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | hose = pci_bus_to_host(bus); |
| 159 | if (hose == NULL) |
| 160 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 161 | |
| 162 | addr = u3_agp_cfg_access(hose, bus->number, devfn, offset); |
| 163 | if (!addr) |
| 164 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 165 | /* |
| 166 | * Note: the caller has already checked that offset is |
| 167 | * suitably aligned and that len is 1, 2 or 4. |
| 168 | */ |
| 169 | switch (len) { |
| 170 | case 1: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 171 | out_8(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | break; |
| 173 | case 2: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 174 | out_le16(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | break; |
| 176 | default: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 177 | out_le32(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | return PCIBIOS_SUCCESSFUL; |
| 181 | } |
| 182 | |
| 183 | static struct pci_ops u3_agp_pci_ops = |
| 184 | { |
Nathan Lynch | 2e67d40 | 2007-08-10 05:18:39 +1000 | [diff] [blame] | 185 | .read = u3_agp_read_config, |
| 186 | .write = u3_agp_write_config, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Nathan Lynch | cc9881c | 2006-09-21 14:31:13 -0500 | [diff] [blame] | 189 | static unsigned long u3_ht_cfa0(u8 devfn, u8 off) |
| 190 | { |
| 191 | return (devfn << 8) | off; |
| 192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Nathan Lynch | cc9881c | 2006-09-21 14:31:13 -0500 | [diff] [blame] | 194 | static unsigned long u3_ht_cfa1(u8 bus, u8 devfn, u8 off) |
| 195 | { |
| 196 | return u3_ht_cfa0(devfn, off) + (bus << 16) + 0x01000000UL; |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 199 | static volatile void __iomem *u3_ht_cfg_access(struct pci_controller* hose, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | u8 bus, u8 devfn, u8 offset) |
| 201 | { |
| 202 | if (bus == hose->first_busno) { |
| 203 | if (PCI_SLOT(devfn) == 0) |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 204 | return NULL; |
| 205 | return hose->cfg_data + u3_ht_cfa0(devfn, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } else |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 207 | return hose->cfg_data + u3_ht_cfa1(bus, devfn, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Dmitry Eremin-Solenikov | f49a0c9 | 2011-06-29 04:17:40 +0000 | [diff] [blame] | 210 | static int u3_ht_root_read_config(struct pci_controller *hose, u8 offset, |
| 211 | int len, u32 *val) |
| 212 | { |
| 213 | volatile void __iomem *addr; |
| 214 | |
| 215 | addr = hose->cfg_addr; |
| 216 | addr += ((offset & ~3) << 2) + (4 - len - (offset & 3)); |
| 217 | |
| 218 | switch (len) { |
| 219 | case 1: |
| 220 | *val = in_8(addr); |
| 221 | break; |
| 222 | case 2: |
| 223 | *val = in_be16(addr); |
| 224 | break; |
| 225 | default: |
| 226 | *val = in_be32(addr); |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | return PCIBIOS_SUCCESSFUL; |
| 231 | } |
| 232 | |
| 233 | static int u3_ht_root_write_config(struct pci_controller *hose, u8 offset, |
| 234 | int len, u32 val) |
| 235 | { |
| 236 | volatile void __iomem *addr; |
| 237 | |
| 238 | addr = hose->cfg_addr + ((offset & ~3) << 2) + (4 - len - (offset & 3)); |
| 239 | |
| 240 | if (offset >= PCI_BASE_ADDRESS_0 && offset < PCI_CAPABILITY_LIST) |
| 241 | return PCIBIOS_SUCCESSFUL; |
| 242 | |
| 243 | switch (len) { |
| 244 | case 1: |
| 245 | out_8(addr, val); |
| 246 | break; |
| 247 | case 2: |
| 248 | out_be16(addr, val); |
| 249 | break; |
| 250 | default: |
| 251 | out_be32(addr, val); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | return PCIBIOS_SUCCESSFUL; |
| 256 | } |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn, |
| 259 | int offset, int len, u32 *val) |
| 260 | { |
| 261 | struct pci_controller *hose; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 262 | volatile void __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
| 264 | hose = pci_bus_to_host(bus); |
| 265 | if (hose == NULL) |
| 266 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 267 | |
Dmitry Eremin-Solenikov | f49a0c9 | 2011-06-29 04:17:40 +0000 | [diff] [blame] | 268 | if (bus->number == hose->first_busno && devfn == PCI_DEVFN(0, 0)) |
| 269 | return u3_ht_root_read_config(hose, offset, len, val); |
| 270 | |
Nathan Lynch | d608df5 | 2006-09-21 14:25:34 -0500 | [diff] [blame] | 271 | if (offset > 0xff) |
| 272 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | addr = u3_ht_cfg_access(hose, bus->number, devfn, offset); |
| 275 | if (!addr) |
| 276 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 277 | |
| 278 | /* |
| 279 | * Note: the caller has already checked that offset is |
| 280 | * suitably aligned and that len is 1, 2 or 4. |
| 281 | */ |
| 282 | switch (len) { |
| 283 | case 1: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 284 | *val = in_8(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | break; |
| 286 | case 2: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 287 | *val = in_le16(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | break; |
| 289 | default: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 290 | *val = in_le32(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | return PCIBIOS_SUCCESSFUL; |
| 294 | } |
| 295 | |
| 296 | static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn, |
| 297 | int offset, int len, u32 val) |
| 298 | { |
| 299 | struct pci_controller *hose; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 300 | volatile void __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | hose = pci_bus_to_host(bus); |
| 303 | if (hose == NULL) |
| 304 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 305 | |
Dmitry Eremin-Solenikov | f49a0c9 | 2011-06-29 04:17:40 +0000 | [diff] [blame] | 306 | if (bus->number == hose->first_busno && devfn == PCI_DEVFN(0, 0)) |
| 307 | return u3_ht_root_write_config(hose, offset, len, val); |
| 308 | |
Nathan Lynch | d608df5 | 2006-09-21 14:25:34 -0500 | [diff] [blame] | 309 | if (offset > 0xff) |
| 310 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | addr = u3_ht_cfg_access(hose, bus->number, devfn, offset); |
| 313 | if (!addr) |
| 314 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 315 | /* |
| 316 | * Note: the caller has already checked that offset is |
| 317 | * suitably aligned and that len is 1, 2 or 4. |
| 318 | */ |
| 319 | switch (len) { |
| 320 | case 1: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 321 | out_8(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | break; |
| 323 | case 2: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 324 | out_le16(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | break; |
| 326 | default: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 327 | out_le32(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | return PCIBIOS_SUCCESSFUL; |
| 331 | } |
| 332 | |
| 333 | static struct pci_ops u3_ht_pci_ops = |
| 334 | { |
Nathan Lynch | 2e67d40 | 2007-08-10 05:18:39 +1000 | [diff] [blame] | 335 | .read = u3_ht_read_config, |
| 336 | .write = u3_ht_write_config, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 339 | static unsigned int u4_pcie_cfa0(unsigned int devfn, unsigned int off) |
| 340 | { |
| 341 | return (1 << PCI_SLOT(devfn)) | |
| 342 | (PCI_FUNC(devfn) << 8) | |
| 343 | ((off >> 8) << 28) | |
| 344 | (off & 0xfcu); |
| 345 | } |
| 346 | |
| 347 | static unsigned int u4_pcie_cfa1(unsigned int bus, unsigned int devfn, |
| 348 | unsigned int off) |
| 349 | { |
| 350 | return (bus << 16) | |
| 351 | (devfn << 8) | |
| 352 | ((off >> 8) << 28) | |
| 353 | (off & 0xfcu) | 1u; |
| 354 | } |
| 355 | |
| 356 | static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose, |
| 357 | u8 bus, u8 dev_fn, int offset) |
| 358 | { |
| 359 | unsigned int caddr; |
| 360 | |
| 361 | if (bus == hose->first_busno) |
| 362 | caddr = u4_pcie_cfa0(dev_fn, offset); |
| 363 | else |
| 364 | caddr = u4_pcie_cfa1(bus, dev_fn, offset); |
| 365 | |
| 366 | /* Uninorth will return garbage if we don't read back the value ! */ |
| 367 | do { |
| 368 | out_le32(hose->cfg_addr, caddr); |
| 369 | } while (in_le32(hose->cfg_addr) != caddr); |
| 370 | |
| 371 | offset &= 0x03; |
| 372 | return hose->cfg_data + offset; |
| 373 | } |
| 374 | |
| 375 | static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn, |
| 376 | int offset, int len, u32 *val) |
| 377 | { |
| 378 | struct pci_controller *hose; |
| 379 | volatile void __iomem *addr; |
| 380 | |
| 381 | hose = pci_bus_to_host(bus); |
| 382 | if (hose == NULL) |
| 383 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 384 | if (offset >= 0x1000) |
| 385 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 386 | addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset); |
| 387 | if (!addr) |
| 388 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 389 | /* |
| 390 | * Note: the caller has already checked that offset is |
| 391 | * suitably aligned and that len is 1, 2 or 4. |
| 392 | */ |
| 393 | switch (len) { |
| 394 | case 1: |
| 395 | *val = in_8(addr); |
| 396 | break; |
| 397 | case 2: |
| 398 | *val = in_le16(addr); |
| 399 | break; |
| 400 | default: |
| 401 | *val = in_le32(addr); |
| 402 | break; |
| 403 | } |
| 404 | return PCIBIOS_SUCCESSFUL; |
| 405 | } |
| 406 | static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn, |
| 407 | int offset, int len, u32 val) |
| 408 | { |
| 409 | struct pci_controller *hose; |
| 410 | volatile void __iomem *addr; |
| 411 | |
| 412 | hose = pci_bus_to_host(bus); |
| 413 | if (hose == NULL) |
| 414 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 415 | if (offset >= 0x1000) |
| 416 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 417 | addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset); |
| 418 | if (!addr) |
| 419 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 420 | /* |
| 421 | * Note: the caller has already checked that offset is |
| 422 | * suitably aligned and that len is 1, 2 or 4. |
| 423 | */ |
| 424 | switch (len) { |
| 425 | case 1: |
| 426 | out_8(addr, val); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 427 | break; |
| 428 | case 2: |
| 429 | out_le16(addr, val); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | out_le32(addr, val); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | return PCIBIOS_SUCCESSFUL; |
| 436 | } |
| 437 | |
| 438 | static struct pci_ops u4_pcie_pci_ops = |
| 439 | { |
Nathan Lynch | 2e67d40 | 2007-08-10 05:18:39 +1000 | [diff] [blame] | 440 | .read = u4_pcie_read_config, |
| 441 | .write = u4_pcie_write_config, |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 442 | }; |
| 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | static void __init setup_u3_agp(struct pci_controller* hose) |
| 445 | { |
| 446 | /* On G5, we move AGP up to high bus number so we don't need |
| 447 | * to reassign bus numbers for HT. If we ever have P2P bridges |
Paul Mackerras | 399fe2b | 2005-10-20 20:57:05 +1000 | [diff] [blame] | 448 | * on AGP, we'll have to move pci_assign_all_buses to the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | * pci_controller structure so we enable it for AGP and not for |
| 450 | * HT childs. |
| 451 | * We hard code the address because of the different size of |
| 452 | * the reg address cell, we shall fix that by killing struct |
| 453 | * reg_property and using some accessor functions instead |
| 454 | */ |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 455 | hose->first_busno = 0xf0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | hose->last_busno = 0xff; |
| 457 | hose->ops = &u3_agp_pci_ops; |
| 458 | hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000); |
| 459 | hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000); |
| 460 | |
| 461 | u3_agp = hose; |
| 462 | } |
| 463 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 464 | static void __init setup_u4_pcie(struct pci_controller* hose) |
| 465 | { |
| 466 | /* We currently only implement the "non-atomic" config space, to |
| 467 | * be optimised later. |
| 468 | */ |
| 469 | hose->ops = &u4_pcie_pci_ops; |
| 470 | hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000); |
| 471 | hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000); |
| 472 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 473 | u4_pcie = hose; |
| 474 | } |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | static void __init setup_u3_ht(struct pci_controller* hose) |
| 477 | { |
| 478 | hose->ops = &u3_ht_pci_ops; |
| 479 | |
| 480 | /* We hard code the address because of the different size of |
| 481 | * the reg address cell, we shall fix that by killing struct |
| 482 | * reg_property and using some accessor functions instead |
| 483 | */ |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 484 | hose->cfg_data = ioremap(0xf2000000, 0x02000000); |
Dmitry Eremin-Solenikov | f49a0c9 | 2011-06-29 04:17:40 +0000 | [diff] [blame] | 485 | hose->cfg_addr = ioremap(0xf8070000, 0x1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | hose->first_busno = 0; |
| 488 | hose->last_busno = 0xef; |
| 489 | |
| 490 | u3_ht = hose; |
| 491 | } |
| 492 | |
Arnd Bergmann | 09b55f7 | 2007-06-18 01:06:54 +0200 | [diff] [blame] | 493 | static int __init maple_add_bridge(struct device_node *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
| 495 | int len; |
| 496 | struct pci_controller *hose; |
| 497 | char* disp_name; |
Jeremy Kerr | eeb2b72 | 2006-07-12 15:40:17 +1000 | [diff] [blame] | 498 | const int *bus_range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | int primary = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | DBG("Adding PCI host bridge %s\n", dev->full_name); |
| 502 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 503 | bus_range = of_get_property(dev, "bus-range", &len); |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 504 | if (bus_range == NULL || len < 2 * sizeof(int)) { |
| 505 | printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n", |
| 506 | dev->full_name); |
| 507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Benjamin Herrenschmidt | b5166cc | 2005-11-15 16:05:33 +1100 | [diff] [blame] | 509 | hose = pcibios_alloc_controller(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | if (hose == NULL) |
| 511 | return -ENOMEM; |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 512 | hose->first_busno = bus_range ? bus_range[0] : 0; |
| 513 | hose->last_busno = bus_range ? bus_range[1] : 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | disp_name = NULL; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 516 | if (of_device_is_compatible(dev, "u3-agp")) { |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 517 | setup_u3_agp(hose); |
| 518 | disp_name = "U3-AGP"; |
| 519 | primary = 0; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 520 | } else if (of_device_is_compatible(dev, "u3-ht")) { |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 521 | setup_u3_ht(hose); |
| 522 | disp_name = "U3-HT"; |
| 523 | primary = 1; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 524 | } else if (of_device_is_compatible(dev, "u4-pcie")) { |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 525 | setup_u4_pcie(hose); |
| 526 | disp_name = "U4-PCIE"; |
| 527 | primary = 0; |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 528 | } |
| 529 | printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n", |
| 530 | disp_name, hose->first_busno, hose->last_busno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 532 | /* Interpret the "ranges" property */ |
| 533 | /* This also maps the I/O region and sets isa_io/mem_base */ |
Paul Mackerras | f7abbc1 | 2005-10-22 15:03:21 +1000 | [diff] [blame] | 534 | pci_process_bridge_OF_ranges(hose, dev, primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 536 | /* Fixup "bus-range" OF property */ |
| 537 | fixup_bus_range(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Benjamin Herrenschmidt | 17cd87c | 2007-07-26 14:07:14 +1000 | [diff] [blame] | 539 | /* Check for legacy IOs */ |
| 540 | isa_bridge_find_early(hose); |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 546 | void maple_pci_irq_fixup(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 548 | DBG(" -> maple_pci_irq_fixup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 550 | /* Fixup IRQ for PCIe host */ |
| 551 | if (u4_pcie != NULL && dev->bus->number == 0 && |
| 552 | pci_bus_to_host(dev->bus) == u4_pcie) { |
| 553 | printk(KERN_DEBUG "Fixup U4 PCIe IRQ\n"); |
| 554 | dev->irq = irq_create_mapping(NULL, 1); |
| 555 | if (dev->irq != NO_IRQ) |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 556 | irq_set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 559 | /* Hide AMD8111 IDE interrupt when in legacy mode so |
| 560 | * the driver calls pci_get_legacy_ide_irq() |
| 561 | */ |
| 562 | if (dev->vendor == PCI_VENDOR_ID_AMD && |
| 563 | dev->device == PCI_DEVICE_ID_AMD_8111_IDE && |
| 564 | (dev->class & 5) != 5) { |
| 565 | dev->irq = NO_IRQ; |
| 566 | } |
| 567 | |
| 568 | DBG(" <- maple_pci_irq_fixup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | void __init maple_pci_init(void) |
| 572 | { |
| 573 | struct device_node *np, *root; |
| 574 | struct device_node *ht = NULL; |
| 575 | |
| 576 | /* Probe root PCI hosts, that is on U3 the AGP host and the |
| 577 | * HyperTransport host. That one is actually "kept" around |
| 578 | * and actually added last as it's resource management relies |
| 579 | * on the AGP resources to have been setup first |
| 580 | */ |
| 581 | root = of_find_node_by_path("/"); |
| 582 | if (root == NULL) { |
| 583 | printk(KERN_CRIT "maple_find_bridges: can't find root of device tree\n"); |
| 584 | return; |
| 585 | } |
| 586 | for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) { |
Nathan Lynch | f1f0033 | 2007-01-03 12:56:28 -0600 | [diff] [blame] | 587 | if (!np->type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | continue; |
Nathan Lynch | f1f0033 | 2007-01-03 12:56:28 -0600 | [diff] [blame] | 589 | if (strcmp(np->type, "pci") && strcmp(np->type, "ht")) |
| 590 | continue; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 591 | if ((of_device_is_compatible(np, "u4-pcie") || |
| 592 | of_device_is_compatible(np, "u3-agp")) && |
Arnd Bergmann | 09b55f7 | 2007-06-18 01:06:54 +0200 | [diff] [blame] | 593 | maple_add_bridge(np) == 0) |
Nathan Lynch | f1f0033 | 2007-01-03 12:56:28 -0600 | [diff] [blame] | 594 | of_node_get(np); |
| 595 | |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 596 | if (of_device_is_compatible(np, "u3-ht")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | of_node_get(np); |
| 598 | ht = np; |
| 599 | } |
| 600 | } |
| 601 | of_node_put(root); |
| 602 | |
| 603 | /* Now setup the HyperTransport host if we found any |
| 604 | */ |
Arnd Bergmann | 09b55f7 | 2007-06-18 01:06:54 +0200 | [diff] [blame] | 605 | if (ht && maple_add_bridge(ht) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | of_node_put(ht); |
| 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | /* Setup the linkage between OF nodes and PHBs */ |
| 609 | pci_devs_phb_init(); |
| 610 | |
| 611 | /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We |
| 612 | * assume there is no P2P bridge on the AGP bus, which should be a |
| 613 | * safe assumptions hopefully. |
| 614 | */ |
| 615 | if (u3_agp) { |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 616 | struct device_node *np = u3_agp->dn; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 617 | PCI_DN(np)->busno = 0xf0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | for (np = np->child; np; np = np->sibling) |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 619 | PCI_DN(np)->busno = 0xf0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Segher Boessenkool | 4558f41 | 2006-02-17 11:30:30 +0100 | [diff] [blame] | 622 | /* Tell pci.c to not change any resource allocations. */ |
Bjorn Helgaas | 673c975 | 2012-02-23 20:18:58 -0700 | [diff] [blame] | 623 | pci_add_flags(PCI_PROBE_ONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | int maple_pci_get_legacy_ide_irq(struct pci_dev *pdev, int channel) |
| 627 | { |
| 628 | struct device_node *np; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 629 | unsigned int defirq = channel ? 15 : 14; |
| 630 | unsigned int irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | if (pdev->vendor != PCI_VENDOR_ID_AMD || |
| 633 | pdev->device != PCI_DEVICE_ID_AMD_8111_IDE) |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 634 | return defirq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | np = pci_device_to_OF_node(pdev); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 637 | if (np == NULL) { |
| 638 | printk("Failed to locate OF node for IDE %s\n", |
| 639 | pci_name(pdev)); |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 640 | return defirq; |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 641 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 642 | irq = irq_of_parse_and_map(np, channel & 0x1); |
| 643 | if (irq == NO_IRQ) { |
| 644 | printk("Failed to map onboard IDE interrupt for channel %d\n", |
| 645 | channel); |
| 646 | return defirq; |
| 647 | } |
| 648 | return irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
Michael Ellerman | 6eb0ac0 | 2009-05-21 19:10:23 +0000 | [diff] [blame] | 650 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 651 | static void quirk_ipr_msi(struct pci_dev *dev) |
Michael Ellerman | 6eb0ac0 | 2009-05-21 19:10:23 +0000 | [diff] [blame] | 652 | { |
| 653 | /* Something prevents MSIs from the IPR from working on Bimini, |
| 654 | * and the driver has no smarts to recover. So disable MSI |
| 655 | * on it for now. */ |
| 656 | |
| 657 | if (machine_is(maple)) { |
| 658 | dev->no_msi = 1; |
| 659 | dev_info(&dev->dev, "Quirk disabled MSI\n"); |
| 660 | } |
| 661 | } |
| 662 | DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_OBSIDIAN, |
| 663 | quirk_ipr_msi); |