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 | |
| 210 | static int u3_ht_read_config(struct pci_bus *bus, unsigned int devfn, |
| 211 | int offset, int len, u32 *val) |
| 212 | { |
| 213 | struct pci_controller *hose; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 214 | volatile void __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | hose = pci_bus_to_host(bus); |
| 217 | if (hose == NULL) |
| 218 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 219 | |
Nathan Lynch | d608df5 | 2006-09-21 14:25:34 -0500 | [diff] [blame] | 220 | if (offset > 0xff) |
| 221 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | addr = u3_ht_cfg_access(hose, bus->number, devfn, offset); |
| 224 | if (!addr) |
| 225 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 226 | |
| 227 | /* |
| 228 | * Note: the caller has already checked that offset is |
| 229 | * suitably aligned and that len is 1, 2 or 4. |
| 230 | */ |
| 231 | switch (len) { |
| 232 | case 1: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 233 | *val = in_8(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | break; |
| 235 | case 2: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 236 | *val = in_le16(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | break; |
| 238 | default: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 239 | *val = in_le32(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | return PCIBIOS_SUCCESSFUL; |
| 243 | } |
| 244 | |
| 245 | static int u3_ht_write_config(struct pci_bus *bus, unsigned int devfn, |
| 246 | int offset, int len, u32 val) |
| 247 | { |
| 248 | struct pci_controller *hose; |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 249 | volatile void __iomem *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | hose = pci_bus_to_host(bus); |
| 252 | if (hose == NULL) |
| 253 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 254 | |
Nathan Lynch | d608df5 | 2006-09-21 14:25:34 -0500 | [diff] [blame] | 255 | if (offset > 0xff) |
| 256 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | addr = u3_ht_cfg_access(hose, bus->number, devfn, offset); |
| 259 | if (!addr) |
| 260 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 261 | /* |
| 262 | * Note: the caller has already checked that offset is |
| 263 | * suitably aligned and that len is 1, 2 or 4. |
| 264 | */ |
| 265 | switch (len) { |
| 266 | case 1: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 267 | out_8(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | break; |
| 269 | case 2: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 270 | out_le16(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | break; |
| 272 | default: |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 273 | out_le32(addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | return PCIBIOS_SUCCESSFUL; |
| 277 | } |
| 278 | |
| 279 | static struct pci_ops u3_ht_pci_ops = |
| 280 | { |
Nathan Lynch | 2e67d40 | 2007-08-10 05:18:39 +1000 | [diff] [blame^] | 281 | .read = u3_ht_read_config, |
| 282 | .write = u3_ht_write_config, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 285 | static unsigned int u4_pcie_cfa0(unsigned int devfn, unsigned int off) |
| 286 | { |
| 287 | return (1 << PCI_SLOT(devfn)) | |
| 288 | (PCI_FUNC(devfn) << 8) | |
| 289 | ((off >> 8) << 28) | |
| 290 | (off & 0xfcu); |
| 291 | } |
| 292 | |
| 293 | static unsigned int u4_pcie_cfa1(unsigned int bus, unsigned int devfn, |
| 294 | unsigned int off) |
| 295 | { |
| 296 | return (bus << 16) | |
| 297 | (devfn << 8) | |
| 298 | ((off >> 8) << 28) | |
| 299 | (off & 0xfcu) | 1u; |
| 300 | } |
| 301 | |
| 302 | static volatile void __iomem *u4_pcie_cfg_access(struct pci_controller* hose, |
| 303 | u8 bus, u8 dev_fn, int offset) |
| 304 | { |
| 305 | unsigned int caddr; |
| 306 | |
| 307 | if (bus == hose->first_busno) |
| 308 | caddr = u4_pcie_cfa0(dev_fn, offset); |
| 309 | else |
| 310 | caddr = u4_pcie_cfa1(bus, dev_fn, offset); |
| 311 | |
| 312 | /* Uninorth will return garbage if we don't read back the value ! */ |
| 313 | do { |
| 314 | out_le32(hose->cfg_addr, caddr); |
| 315 | } while (in_le32(hose->cfg_addr) != caddr); |
| 316 | |
| 317 | offset &= 0x03; |
| 318 | return hose->cfg_data + offset; |
| 319 | } |
| 320 | |
| 321 | static int u4_pcie_read_config(struct pci_bus *bus, unsigned int devfn, |
| 322 | int offset, int len, u32 *val) |
| 323 | { |
| 324 | struct pci_controller *hose; |
| 325 | volatile void __iomem *addr; |
| 326 | |
| 327 | hose = pci_bus_to_host(bus); |
| 328 | if (hose == NULL) |
| 329 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 330 | if (offset >= 0x1000) |
| 331 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 332 | addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset); |
| 333 | if (!addr) |
| 334 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 335 | /* |
| 336 | * Note: the caller has already checked that offset is |
| 337 | * suitably aligned and that len is 1, 2 or 4. |
| 338 | */ |
| 339 | switch (len) { |
| 340 | case 1: |
| 341 | *val = in_8(addr); |
| 342 | break; |
| 343 | case 2: |
| 344 | *val = in_le16(addr); |
| 345 | break; |
| 346 | default: |
| 347 | *val = in_le32(addr); |
| 348 | break; |
| 349 | } |
| 350 | return PCIBIOS_SUCCESSFUL; |
| 351 | } |
| 352 | static int u4_pcie_write_config(struct pci_bus *bus, unsigned int devfn, |
| 353 | int offset, int len, u32 val) |
| 354 | { |
| 355 | struct pci_controller *hose; |
| 356 | volatile void __iomem *addr; |
| 357 | |
| 358 | hose = pci_bus_to_host(bus); |
| 359 | if (hose == NULL) |
| 360 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 361 | if (offset >= 0x1000) |
| 362 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 363 | addr = u4_pcie_cfg_access(hose, bus->number, devfn, offset); |
| 364 | if (!addr) |
| 365 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 366 | /* |
| 367 | * Note: the caller has already checked that offset is |
| 368 | * suitably aligned and that len is 1, 2 or 4. |
| 369 | */ |
| 370 | switch (len) { |
| 371 | case 1: |
| 372 | out_8(addr, val); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 373 | break; |
| 374 | case 2: |
| 375 | out_le16(addr, val); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 376 | break; |
| 377 | default: |
| 378 | out_le32(addr, val); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | return PCIBIOS_SUCCESSFUL; |
| 382 | } |
| 383 | |
| 384 | static struct pci_ops u4_pcie_pci_ops = |
| 385 | { |
Nathan Lynch | 2e67d40 | 2007-08-10 05:18:39 +1000 | [diff] [blame^] | 386 | .read = u4_pcie_read_config, |
| 387 | .write = u4_pcie_write_config, |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 388 | }; |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | static void __init setup_u3_agp(struct pci_controller* hose) |
| 391 | { |
| 392 | /* On G5, we move AGP up to high bus number so we don't need |
| 393 | * to reassign bus numbers for HT. If we ever have P2P bridges |
Paul Mackerras | 399fe2b | 2005-10-20 20:57:05 +1000 | [diff] [blame] | 394 | * 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] | 395 | * pci_controller structure so we enable it for AGP and not for |
| 396 | * HT childs. |
| 397 | * We hard code the address because of the different size of |
| 398 | * the reg address cell, we shall fix that by killing struct |
| 399 | * reg_property and using some accessor functions instead |
| 400 | */ |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 401 | hose->first_busno = 0xf0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | hose->last_busno = 0xff; |
| 403 | hose->ops = &u3_agp_pci_ops; |
| 404 | hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000); |
| 405 | hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000); |
| 406 | |
| 407 | u3_agp = hose; |
| 408 | } |
| 409 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 410 | static void __init setup_u4_pcie(struct pci_controller* hose) |
| 411 | { |
| 412 | /* We currently only implement the "non-atomic" config space, to |
| 413 | * be optimised later. |
| 414 | */ |
| 415 | hose->ops = &u4_pcie_pci_ops; |
| 416 | hose->cfg_addr = ioremap(0xf0000000 + 0x800000, 0x1000); |
| 417 | hose->cfg_data = ioremap(0xf0000000 + 0xc00000, 0x1000); |
| 418 | |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 419 | u4_pcie = hose; |
| 420 | } |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | static void __init setup_u3_ht(struct pci_controller* hose) |
| 423 | { |
| 424 | hose->ops = &u3_ht_pci_ops; |
| 425 | |
| 426 | /* We hard code the address because of the different size of |
| 427 | * the reg address cell, we shall fix that by killing struct |
| 428 | * reg_property and using some accessor functions instead |
| 429 | */ |
Al Viro | 8c42ec2 | 2006-09-23 01:37:41 +0100 | [diff] [blame] | 430 | hose->cfg_data = ioremap(0xf2000000, 0x02000000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | hose->first_busno = 0; |
| 433 | hose->last_busno = 0xef; |
| 434 | |
| 435 | u3_ht = hose; |
| 436 | } |
| 437 | |
Arnd Bergmann | 09b55f7 | 2007-06-18 01:06:54 +0200 | [diff] [blame] | 438 | static int __init maple_add_bridge(struct device_node *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | int len; |
| 441 | struct pci_controller *hose; |
| 442 | char* disp_name; |
Jeremy Kerr | eeb2b72 | 2006-07-12 15:40:17 +1000 | [diff] [blame] | 443 | const int *bus_range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | int primary = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | DBG("Adding PCI host bridge %s\n", dev->full_name); |
| 447 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 448 | bus_range = of_get_property(dev, "bus-range", &len); |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 449 | if (bus_range == NULL || len < 2 * sizeof(int)) { |
| 450 | printk(KERN_WARNING "Can't get bus-range for %s, assume bus 0\n", |
| 451 | dev->full_name); |
| 452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
Benjamin Herrenschmidt | b5166cc | 2005-11-15 16:05:33 +1100 | [diff] [blame] | 454 | hose = pcibios_alloc_controller(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | if (hose == NULL) |
| 456 | return -ENOMEM; |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 457 | hose->first_busno = bus_range ? bus_range[0] : 0; |
| 458 | hose->last_busno = bus_range ? bus_range[1] : 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | disp_name = NULL; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 461 | if (of_device_is_compatible(dev, "u3-agp")) { |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 462 | setup_u3_agp(hose); |
| 463 | disp_name = "U3-AGP"; |
| 464 | primary = 0; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 465 | } else if (of_device_is_compatible(dev, "u3-ht")) { |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 466 | setup_u3_ht(hose); |
| 467 | disp_name = "U3-HT"; |
| 468 | primary = 1; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 469 | } else if (of_device_is_compatible(dev, "u4-pcie")) { |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 470 | setup_u4_pcie(hose); |
| 471 | disp_name = "U4-PCIE"; |
| 472 | primary = 0; |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 473 | } |
| 474 | printk(KERN_INFO "Found %s PCI host bridge. Firmware bus number: %d->%d\n", |
| 475 | disp_name, hose->first_busno, hose->last_busno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 477 | /* Interpret the "ranges" property */ |
| 478 | /* 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] | 479 | pci_process_bridge_OF_ranges(hose, dev, primary); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Anton Blanchard | 3238e9c | 2005-09-12 13:14:26 +1000 | [diff] [blame] | 481 | /* Fixup "bus-range" OF property */ |
| 482 | fixup_bus_range(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Benjamin Herrenschmidt | 17cd87c | 2007-07-26 14:07:14 +1000 | [diff] [blame] | 484 | /* Check for legacy IOs */ |
| 485 | isa_bridge_find_early(hose); |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 491 | void __devinit maple_pci_irq_fixup(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 493 | DBG(" -> maple_pci_irq_fixup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 495 | /* Fixup IRQ for PCIe host */ |
| 496 | if (u4_pcie != NULL && dev->bus->number == 0 && |
| 497 | pci_bus_to_host(dev->bus) == u4_pcie) { |
| 498 | printk(KERN_DEBUG "Fixup U4 PCIe IRQ\n"); |
| 499 | dev->irq = irq_create_mapping(NULL, 1); |
| 500 | if (dev->irq != NO_IRQ) |
| 501 | set_irq_type(dev->irq, IRQ_TYPE_LEVEL_LOW); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 502 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Benjamin Herrenschmidt | f90bb15 | 2006-11-11 17:24:51 +1100 | [diff] [blame] | 504 | /* Hide AMD8111 IDE interrupt when in legacy mode so |
| 505 | * the driver calls pci_get_legacy_ide_irq() |
| 506 | */ |
| 507 | if (dev->vendor == PCI_VENDOR_ID_AMD && |
| 508 | dev->device == PCI_DEVICE_ID_AMD_8111_IDE && |
| 509 | (dev->class & 5) != 5) { |
| 510 | dev->irq = NO_IRQ; |
| 511 | } |
| 512 | |
| 513 | DBG(" <- maple_pci_irq_fixup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | void __init maple_pci_init(void) |
| 517 | { |
| 518 | struct device_node *np, *root; |
| 519 | struct device_node *ht = NULL; |
| 520 | |
| 521 | /* Probe root PCI hosts, that is on U3 the AGP host and the |
| 522 | * HyperTransport host. That one is actually "kept" around |
| 523 | * and actually added last as it's resource management relies |
| 524 | * on the AGP resources to have been setup first |
| 525 | */ |
| 526 | root = of_find_node_by_path("/"); |
| 527 | if (root == NULL) { |
| 528 | printk(KERN_CRIT "maple_find_bridges: can't find root of device tree\n"); |
| 529 | return; |
| 530 | } |
| 531 | for (np = NULL; (np = of_get_next_child(root, np)) != NULL;) { |
Nathan Lynch | f1f0033 | 2007-01-03 12:56:28 -0600 | [diff] [blame] | 532 | if (!np->type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | continue; |
Nathan Lynch | f1f0033 | 2007-01-03 12:56:28 -0600 | [diff] [blame] | 534 | if (strcmp(np->type, "pci") && strcmp(np->type, "ht")) |
| 535 | continue; |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 536 | if ((of_device_is_compatible(np, "u4-pcie") || |
| 537 | of_device_is_compatible(np, "u3-agp")) && |
Arnd Bergmann | 09b55f7 | 2007-06-18 01:06:54 +0200 | [diff] [blame] | 538 | maple_add_bridge(np) == 0) |
Nathan Lynch | f1f0033 | 2007-01-03 12:56:28 -0600 | [diff] [blame] | 539 | of_node_get(np); |
| 540 | |
Stephen Rothwell | 55b61fe | 2007-05-03 17:26:52 +1000 | [diff] [blame] | 541 | if (of_device_is_compatible(np, "u3-ht")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | of_node_get(np); |
| 543 | ht = np; |
| 544 | } |
| 545 | } |
| 546 | of_node_put(root); |
| 547 | |
| 548 | /* Now setup the HyperTransport host if we found any |
| 549 | */ |
Arnd Bergmann | 09b55f7 | 2007-06-18 01:06:54 +0200 | [diff] [blame] | 550 | if (ht && maple_add_bridge(ht) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | of_node_put(ht); |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | /* Setup the linkage between OF nodes and PHBs */ |
| 554 | pci_devs_phb_init(); |
| 555 | |
| 556 | /* Fixup the PCI<->OF mapping for U3 AGP due to bus renumbering. We |
| 557 | * assume there is no P2P bridge on the AGP bus, which should be a |
| 558 | * safe assumptions hopefully. |
| 559 | */ |
| 560 | if (u3_agp) { |
| 561 | struct device_node *np = u3_agp->arch_data; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 562 | PCI_DN(np)->busno = 0xf0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | for (np = np->child; np; np = np->sibling) |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 564 | PCI_DN(np)->busno = 0xf0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Segher Boessenkool | 4558f41 | 2006-02-17 11:30:30 +0100 | [diff] [blame] | 567 | /* Tell pci.c to not change any resource allocations. */ |
| 568 | pci_probe_only = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | int maple_pci_get_legacy_ide_irq(struct pci_dev *pdev, int channel) |
| 572 | { |
| 573 | struct device_node *np; |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 574 | unsigned int defirq = channel ? 15 : 14; |
| 575 | unsigned int irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | if (pdev->vendor != PCI_VENDOR_ID_AMD || |
| 578 | pdev->device != PCI_DEVICE_ID_AMD_8111_IDE) |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 579 | return defirq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | np = pci_device_to_OF_node(pdev); |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 582 | if (np == NULL) { |
| 583 | printk("Failed to locate OF node for IDE %s\n", |
| 584 | pci_name(pdev)); |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 585 | return defirq; |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 586 | } |
Benjamin Herrenschmidt | 0ebfff1 | 2006-07-03 21:36:01 +1000 | [diff] [blame] | 587 | irq = irq_of_parse_and_map(np, channel & 0x1); |
| 588 | if (irq == NO_IRQ) { |
| 589 | printk("Failed to map onboard IDE interrupt for channel %d\n", |
| 590 | channel); |
| 591 | return defirq; |
| 592 | } |
| 593 | return irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* XXX: To remove once all firmwares are ok */ |
| 597 | static void fixup_maple_ide(struct pci_dev* dev) |
| 598 | { |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 599 | if (!machine_is(maple)) |
| 600 | return; |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | #if 0 /* Enable this to enable IDE port 0 */ |
| 603 | { |
| 604 | u8 v; |
| 605 | |
| 606 | pci_read_config_byte(dev, 0x40, &v); |
| 607 | v |= 2; |
| 608 | pci_write_config_byte(dev, 0x40, v); |
| 609 | } |
| 610 | #endif |
| 611 | #if 0 /* fix bus master base */ |
| 612 | pci_write_config_dword(dev, 0x20, 0xcc01); |
| 613 | printk("old ide resource: %lx -> %lx \n", |
| 614 | dev->resource[4].start, dev->resource[4].end); |
| 615 | dev->resource[4].start = 0xcc00; |
| 616 | dev->resource[4].end = 0xcc10; |
| 617 | #endif |
Benjamin Herrenschmidt | c10af8c | 2006-10-09 13:25:15 +1000 | [diff] [blame] | 618 | #if 0 /* Enable this to fixup IDE sense/polarity of irqs in IO-APICs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
| 620 | struct pci_dev *apicdev; |
| 621 | u32 v; |
| 622 | |
| 623 | apicdev = pci_get_slot (dev->bus, PCI_DEVFN(5,0)); |
| 624 | if (apicdev == NULL) |
| 625 | printk("IDE Fixup IRQ: Can't find IO-APIC !\n"); |
| 626 | else { |
| 627 | pci_write_config_byte(apicdev, 0xf2, 0x10 + 2*14); |
| 628 | pci_read_config_dword(apicdev, 0xf4, &v); |
| 629 | v &= ~0x00000022; |
| 630 | pci_write_config_dword(apicdev, 0xf4, v); |
| 631 | pci_write_config_byte(apicdev, 0xf2, 0x10 + 2*15); |
| 632 | pci_read_config_dword(apicdev, 0xf4, &v); |
| 633 | v &= ~0x00000022; |
| 634 | pci_write_config_dword(apicdev, 0xf4, v); |
| 635 | pci_dev_put(apicdev); |
| 636 | } |
| 637 | } |
| 638 | #endif |
| 639 | } |
| 640 | DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8111_IDE, |
| 641 | fixup_maple_ide); |