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