Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * PCI Bus Services, see include/linux/pci.h for further explanation. |
| 3 | * |
| 4 | * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter, |
| 5 | * David Mosberger-Tang |
| 6 | * |
| 7 | * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz> |
| 8 | * |
| 9 | * Copyright 2003 -- Eric Biederman <ebiederman@lnxi.com> |
| 10 | */ |
| 11 | |
| 12 | #include <console/console.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <stdint.h> |
| 15 | #include <bitops.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 16 | #include <string.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 17 | #include <device/device.h> |
| 18 | #include <device/pci.h> |
| 19 | #include <device/pci_ids.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 20 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 21 | /** Given a device and register, read the size of the BAR for that register. |
| 22 | * @param dev Pointer to the device structure |
| 23 | * @param resource Pointer to the resource structure |
| 24 | * @param index Address of the pci configuration register |
| 25 | */ |
| 26 | static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index) |
| 27 | { |
| 28 | uint32_t addr, size, base; |
| 29 | unsigned long type; |
| 30 | |
| 31 | /* Initialize the resources to nothing */ |
| 32 | resource->base = 0; |
| 33 | resource->size = 0; |
| 34 | resource->align = 0; |
| 35 | resource->gran = 0; |
| 36 | resource->limit = 0; |
| 37 | resource->flags = 0; |
| 38 | resource->index = index; |
| 39 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 40 | addr = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 41 | if (addr == 0xffffffffUL) |
| 42 | return; |
| 43 | |
| 44 | /* FIXME: more consideration for 64-bit PCI devices, |
| 45 | * we currently detect their size but otherwise |
| 46 | * treat them as 32-bit resources |
| 47 | */ |
| 48 | /* get the size */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 49 | pci_write_config32(dev, index, ~0); |
| 50 | size = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | |
| 52 | /* get the minimum value the bar can be set to */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 53 | pci_write_config32(dev, index, 0); |
| 54 | base = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 55 | |
| 56 | /* restore addr */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 57 | pci_write_config32(dev, index, addr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * some broken hardware has read-only registers that do not |
| 61 | * really size correctly. You can tell this if addr == size |
| 62 | * Example: the acer m7229 has BARs 1-4 normally read-only. |
| 63 | * so BAR1 at offset 0x10 reads 0x1f1. If you size that register |
| 64 | * by writing 0xffffffff to it, it will read back as 0x1f1 -- a |
| 65 | * violation of the spec. |
| 66 | * We catch this case and ignore it by settting size and type to 0. |
| 67 | * This incidentally catches the common case where registers |
| 68 | * read back as 0 for both address and size. |
| 69 | */ |
| 70 | if ((addr == size) && (addr == base)) { |
| 71 | if (size != 0) { |
| 72 | printk_debug( |
| 73 | "PCI: %02x:%02x.%01x register %02x(%08x), read-only ignoring it\n", |
| 74 | dev->bus->secondary, |
| 75 | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
| 76 | index, addr); |
| 77 | } |
| 78 | resource->flags = 0; |
| 79 | } |
| 80 | /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */ |
| 81 | else if (size & PCI_BASE_ADDRESS_SPACE_IO) { |
| 82 | type = size & (~PCI_BASE_ADDRESS_IO_MASK); |
| 83 | /* BUG! Top 16 bits can be zero (or not) |
| 84 | * So set them to 0xffff so they go away ... |
| 85 | */ |
| 86 | resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1; |
| 87 | resource->align = log2(resource->size); |
| 88 | resource->gran = resource->align; |
| 89 | resource->flags = IORESOURCE_IO; |
| 90 | resource->limit = 0xffff; |
| 91 | } |
| 92 | else { |
| 93 | /* A Memory mapped base address */ |
| 94 | type = size & (~PCI_BASE_ADDRESS_MEM_MASK); |
| 95 | resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1; |
| 96 | resource->align = log2(resource->size); |
| 97 | resource->gran = resource->align; |
| 98 | resource->flags = IORESOURCE_MEM; |
| 99 | if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) { |
| 100 | resource->flags |= IORESOURCE_PREFETCH; |
| 101 | } |
| 102 | type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK; |
| 103 | if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) { |
| 104 | /* 32bit limit */ |
| 105 | resource->limit = 0xffffffffUL; |
| 106 | } |
| 107 | else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) { |
| 108 | /* 1MB limit */ |
| 109 | resource->limit = 0x000fffffUL; |
| 110 | } |
| 111 | else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 112 | unsigned long index_hi; |
| 113 | /* 64bit limit |
| 114 | * For now just treat this as a 32bit limit |
| 115 | */ |
| 116 | index_hi = index + 4; |
| 117 | resource->limit = 0xffffffffUL; |
| 118 | resource->flags |= IORESOURCE_PCI64; |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 119 | addr = pci_read_config32( dev, index_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 120 | /* get the extended size */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 121 | pci_write_config32(dev, index_hi, 0xffffffffUL); |
| 122 | size = pci_read_config32( dev, index_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 123 | |
| 124 | /* get the minimum value the bar can be set to */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 125 | pci_write_config32(dev, index_hi, 0); |
| 126 | base = pci_read_config32(dev, index_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 127 | |
| 128 | /* restore addr */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 129 | pci_write_config32(dev, index_hi, addr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 130 | |
| 131 | if ((size == 0xffffffff) && (base == 0)) { |
| 132 | /* Clear the top half of the bar */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 133 | pci_write_config32(dev, index_hi, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 134 | } |
| 135 | else { |
| 136 | printk_err("PCI: %02x:%02x.%01x Unable to handle 64-bit address\n", |
| 137 | dev->bus->secondary, |
| 138 | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn)); |
| 139 | resource->flags = IORESOURCE_PCI64; |
| 140 | } |
| 141 | } |
| 142 | else { |
| 143 | /* Invalid value */ |
| 144 | resource->flags = 0; |
| 145 | } |
| 146 | } |
| 147 | /* dev->size holds the flags... */ |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /** Read the base address registers for a given device. |
| 152 | * @param dev Pointer to the dev structure |
| 153 | * @param howmany How many registers to read (6 for device, 2 for bridge) |
| 154 | */ |
| 155 | static void pci_read_bases(struct device *dev, unsigned int howmany) |
| 156 | { |
| 157 | unsigned int reg; |
| 158 | unsigned long index; |
| 159 | |
| 160 | reg = dev->resources; |
| 161 | for(index = PCI_BASE_ADDRESS_0; |
| 162 | (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) { |
| 163 | struct resource *resource; |
| 164 | resource = &dev->resource[reg]; |
| 165 | pci_get_resource(dev, resource, index); |
| 166 | reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0; |
| 167 | index += (resource->flags & IORESOURCE_PCI64)?8:4; |
| 168 | } |
| 169 | dev->resources = reg; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static void pci_bridge_read_bases(struct device *dev) |
| 174 | { |
| 175 | unsigned int reg = dev->resources; |
| 176 | |
| 177 | /* FIXME handle bridges without some of the optional resources */ |
| 178 | |
| 179 | /* Initialize the io space constraints on the current bus */ |
| 180 | dev->resource[reg].base = 0; |
| 181 | dev->resource[reg].size = 0; |
| 182 | dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN); |
| 183 | dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN); |
| 184 | dev->resource[reg].limit = 0xffffUL; |
| 185 | dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE; |
| 186 | dev->resource[reg].index = PCI_IO_BASE; |
| 187 | compute_allocate_resource(dev, &dev->resource[reg], |
| 188 | IORESOURCE_IO, IORESOURCE_IO); |
| 189 | reg++; |
| 190 | |
| 191 | /* Initiliaze the prefetchable memory constraints on the current bus */ |
| 192 | dev->resource[reg].base = 0; |
| 193 | dev->resource[reg].size = 0; |
| 194 | dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN); |
| 195 | dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN); |
| 196 | dev->resource[reg].limit = 0xffffffffUL; |
| 197 | dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE; |
| 198 | dev->resource[reg].index = PCI_PREF_MEMORY_BASE; |
| 199 | compute_allocate_resource(dev, &dev->resource[reg], |
| 200 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 201 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
| 202 | reg++; |
| 203 | |
| 204 | /* Initialize the memory resources on the current bus */ |
| 205 | dev->resource[reg].base = 0; |
| 206 | dev->resource[reg].size = 0; |
| 207 | dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN); |
| 208 | dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN); |
| 209 | dev->resource[reg].limit = 0xffffffffUL; |
| 210 | dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE; |
| 211 | dev->resource[reg].index = PCI_MEMORY_BASE; |
| 212 | compute_allocate_resource(dev, &dev->resource[reg], |
| 213 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 214 | IORESOURCE_MEM); |
| 215 | reg++; |
| 216 | |
| 217 | dev->resources = reg; |
| 218 | } |
| 219 | |
| 220 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 221 | void pci_dev_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 222 | { |
| 223 | uint32_t addr; |
| 224 | dev->resources = 0; |
| 225 | memset(&dev->resource[0], 0, sizeof(dev->resource)); |
| 226 | pci_read_bases(dev, 6); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 227 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 228 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
| 229 | } |
| 230 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 231 | void pci_bus_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 232 | { |
| 233 | uint32_t addr; |
| 234 | dev->resources = 0; |
| 235 | memset(&dev->resource[0], 0, sizeof(dev->resource)); |
| 236 | pci_bridge_read_bases(dev); |
| 237 | pci_read_bases(dev, 2); |
| 238 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 239 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 240 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
| 241 | |
| 242 | } |
| 243 | |
| 244 | |
| 245 | static void pci_set_resource(struct device *dev, struct resource *resource) |
| 246 | { |
| 247 | unsigned long base, limit; |
| 248 | unsigned long bridge_align = PCI_MEM_BRIDGE_ALIGN; |
| 249 | unsigned char buf[10]; |
| 250 | |
| 251 | /* Make certain the resource has actually been set */ |
| 252 | if (!(resource->flags & IORESOURCE_SET)) { |
| 253 | #if 1 |
| 254 | printk_err("ERROR: %02x:%02x.%01x %02x not allocated\n", |
| 255 | dev->bus->secondary, |
| 256 | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
| 257 | resource->index); |
| 258 | #endif |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | /* Only handle PCI memory and IO resources for now */ |
| 263 | if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO))) |
| 264 | return; |
| 265 | |
| 266 | if (resource->flags & IORESOURCE_MEM) { |
| 267 | dev->command |= PCI_COMMAND_MEMORY; |
| 268 | bridge_align = PCI_MEM_BRIDGE_ALIGN; |
| 269 | } |
| 270 | if (resource->flags & IORESOURCE_IO) { |
| 271 | dev->command |= PCI_COMMAND_IO; |
| 272 | bridge_align = PCI_IO_BRIDGE_ALIGN; |
| 273 | } |
| 274 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
| 275 | dev->command |= PCI_COMMAND_MASTER; |
| 276 | } |
| 277 | /* Get the base address */ |
| 278 | base = resource->base; |
| 279 | |
| 280 | /* Get the limit (rounded up) */ |
| 281 | limit = base + ((resource->size + bridge_align - 1UL) & ~(bridge_align -1)) -1UL; |
| 282 | |
| 283 | if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) { |
| 284 | /* |
| 285 | * some chipsets allow us to set/clear the IO bit. |
| 286 | * (e.g. VIA 82c686a.) So set it to be safe) |
| 287 | */ |
| 288 | limit = base + resource->size -1; |
| 289 | if (resource->flags & IORESOURCE_IO) { |
| 290 | base |= PCI_BASE_ADDRESS_SPACE_IO; |
| 291 | } |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 292 | pci_write_config32(dev, resource->index, base & 0xffffffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 293 | if (resource->flags & IORESOURCE_PCI64) { |
| 294 | /* FIXME handle real 64bit base addresses */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 295 | pci_write_config32(dev, resource->index + 4, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | else if (resource->index == PCI_IO_BASE) { |
| 299 | /* set the IO ranges |
| 300 | * WARNING: we don't really do 32-bit addressing for IO yet! |
| 301 | */ |
| 302 | compute_allocate_resource(dev, resource, |
| 303 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 304 | pci_write_config8(dev, PCI_IO_BASE, base >> 8); |
| 305 | pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 306 | } |
| 307 | else if (resource->index == PCI_MEMORY_BASE) { |
| 308 | /* set the memory range |
| 309 | */ |
| 310 | compute_allocate_resource(dev, resource, |
| 311 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 312 | IORESOURCE_MEM); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 313 | pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16); |
| 314 | pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 315 | } |
| 316 | else if (resource->index == PCI_PREF_MEMORY_BASE) { |
| 317 | /* set the prefetchable memory range |
| 318 | * WARNING: we don't really do 64-bit addressing for prefetchable memory yet! |
| 319 | */ |
| 320 | compute_allocate_resource(dev, resource, |
| 321 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 322 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 323 | pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16); |
| 324 | pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 325 | } |
| 326 | else { |
| 327 | printk_err("ERROR: invalid resource->index %x\n", |
| 328 | resource->index); |
| 329 | } |
| 330 | buf[0] = '\0'; |
| 331 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
| 332 | sprintf(buf, "bus %d ", dev->secondary); |
| 333 | } |
| 334 | |
| 335 | printk_debug( |
| 336 | "PCI: %02x:%02x.%01x %02x <- [0x%08lx - 0x%08lx] %s%s\n", |
| 337 | dev->bus->secondary, |
| 338 | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
| 339 | resource->index, |
| 340 | resource->base, limit, |
| 341 | buf, |
| 342 | (resource->flags & IORESOURCE_IO)? "io": |
| 343 | (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem"); |
| 344 | return; |
| 345 | } |
| 346 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 347 | void pci_dev_set_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 348 | { |
| 349 | struct resource *resource, *last; |
| 350 | uint8_t line; |
| 351 | |
| 352 | last = &dev->resource[dev->resources]; |
| 353 | |
| 354 | for(resource = &dev->resource[0]; resource < last; resource++) { |
| 355 | pci_set_resource(dev, resource); |
| 356 | } |
| 357 | if (dev->children) { |
| 358 | assign_resources(dev); |
| 359 | } |
| 360 | |
| 361 | /* set a default latency timer */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 362 | pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 363 | |
| 364 | /* set a default secondary latency timer */ |
| 365 | if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 366 | pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* zero the irq settings */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 370 | line = pci_read_config8(dev, PCI_INTERRUPT_PIN); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 371 | if (line) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 372 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 373 | } |
| 374 | /* set the cache line size, so far 64 bytes is good for everyone */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 375 | pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | struct device_operations default_pci_ops_dev = { |
| 379 | .read_resources = pci_dev_read_resources, |
| 380 | .set_resources = pci_dev_set_resources, |
| 381 | .init = 0, |
| 382 | .scan_bus = 0, |
| 383 | }; |
| 384 | struct device_operations default_pci_ops_bus = { |
| 385 | .read_resources = pci_bus_read_resources, |
| 386 | .set_resources = pci_dev_set_resources, |
| 387 | .init = 0, |
| 388 | .scan_bus = pci_scan_bridge, |
| 389 | }; |
| 390 | static void set_pci_ops(struct device *dev) |
| 391 | { |
| 392 | struct pci_driver *driver; |
| 393 | if (dev->ops) { |
| 394 | return; |
| 395 | } |
| 396 | /* Look through the list of setup drivers and find one for |
| 397 | * this pci device |
| 398 | */ |
| 399 | for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) { |
| 400 | if ((driver->vendor == dev->vendor) && |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 401 | (driver->device == dev->device)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 402 | dev->ops = driver->ops; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 403 | #if 1 |
| 404 | printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] ops\n", |
| 405 | dev->bus->secondary, |
| 406 | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
| 407 | driver->vendor, driver->device |
| 408 | ); |
| 409 | #endif |
| 410 | return; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | /* If I don't have a specific driver use the default operations */ |
| 414 | switch(dev->hdr_type & 0x7f) { /* header type */ |
| 415 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ |
| 416 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) |
| 417 | goto bad; |
| 418 | dev->ops = &default_pci_ops_dev; |
| 419 | break; |
| 420 | case PCI_HEADER_TYPE_BRIDGE: |
| 421 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 422 | goto bad; |
| 423 | dev->ops = &default_pci_ops_bus; |
| 424 | break; |
| 425 | default: |
| 426 | bad: |
| 427 | printk_err("PCI: %02x:%02x.%01x [%04x/%04x/%06x] has unknown header " |
| 428 | "type %02x, ignoring.\n", |
| 429 | dev->bus->secondary, |
| 430 | PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
| 431 | dev->vendor, dev->device, |
| 432 | dev->class >> 8, dev->hdr_type); |
| 433 | } |
| 434 | return; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Given a bus and a devfn number, find the device structure |
| 439 | * @param bus The bus structure |
| 440 | * @param devfn a device/function number |
| 441 | * @return pointer to the device structure |
| 442 | */ |
| 443 | static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn) |
| 444 | { |
| 445 | struct device *dev = 0; |
| 446 | for(; *list; list = &(*list)->sibling) { |
| 447 | if ((*list)->devfn == devfn) { |
| 448 | /* Unlink from the list */ |
| 449 | dev = *list; |
| 450 | *list = (*list)->sibling; |
| 451 | dev->sibling = 0; |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | return dev; |
| 456 | } |
| 457 | |
| 458 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 459 | #define HYPERTRANSPORT_SUPPORT 1 |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 460 | /** Scan the pci bus devices and bridges. |
| 461 | * @param pci_bus pointer to the bus structure |
| 462 | * @param max current bus number |
| 463 | * @return The maximum bus number found, after scanning all subordinate busses |
| 464 | */ |
| 465 | unsigned int pci_scan_bus(struct device *bus, unsigned int max) |
| 466 | { |
| 467 | unsigned int devfn; |
| 468 | struct device *dev, **bus_last; |
| 469 | struct device *old_devices; |
| 470 | struct device *child; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 471 | #if HYPERTRANSPORT_SUPPORT |
| 472 | unsigned next_unitid, last_unitid; |
| 473 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 474 | |
| 475 | printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary); |
| 476 | |
| 477 | old_devices = bus->children; |
| 478 | bus->children = 0; |
| 479 | bus_last = &bus->children; |
| 480 | |
| 481 | post_code(0x24); |
| 482 | |
| 483 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 484 | #if HYPERTRANSPORT_SUPPORT |
| 485 | /* If present assign unitid to a hypertransport chain */ |
| 486 | next_unitid = 1; |
| 487 | do { |
| 488 | struct device dummy; |
| 489 | uint32_t id; |
| 490 | uint8_t hdr_type, pos; |
| 491 | last_unitid = next_unitid; |
| 492 | |
| 493 | dummy.bus = bus; |
| 494 | dummy.devfn = 0; |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 495 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 496 | if (id == 0xffffffff || id == 0x00000000 || |
| 497 | id == 0x0000ffff || id == 0xffff0000) { |
| 498 | break; |
| 499 | } |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 500 | hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 501 | pos = 0; |
| 502 | switch(hdr_type & 0x7f) { |
| 503 | case PCI_HEADER_TYPE_NORMAL: |
| 504 | case PCI_HEADER_TYPE_BRIDGE: |
| 505 | pos = PCI_CAPABILITY_LIST; |
| 506 | break; |
| 507 | } |
| 508 | if (pos > PCI_CAP_LIST_NEXT) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 509 | pos = pci_read_config8(&dummy, pos); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 510 | } |
| 511 | while(pos != 0) { |
| 512 | uint8_t cap; |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 513 | cap = pci_read_config8(&dummy, pos + PCI_CAP_LIST_ID); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 514 | printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos); |
| 515 | if (cap == PCI_CAP_ID_HT) { |
| 516 | uint16_t flags; |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 517 | flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 518 | printk_debug("flags: 0x%04x\n", (unsigned)flags); |
| 519 | if ((flags >> 13) == 0) { |
| 520 | unsigned count; |
| 521 | flags &= ~0x1f; |
| 522 | flags |= next_unitid & 0x1f; |
| 523 | count = (flags >> 5) & 0x1f; |
| 524 | printk_debug("unitid: 0x%02x, count: 0x%02x\n", |
| 525 | next_unitid, count); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 526 | pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 527 | next_unitid += count; |
| 528 | break; |
| 529 | } |
| 530 | } |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 531 | pos = pci_read_config8(&dummy, pos + PCI_CAP_LIST_NEXT); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 532 | } |
| 533 | } while((last_unitid != next_unitid) && (next_unitid <= 0x1f)); |
| 534 | #endif /* HYPERTRANSPORT_SUPPORT */ |
| 535 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 536 | /* probe all devices on this bus with some optimization for non-existance and |
| 537 | single funcion devices */ |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 538 | for (devfn = 0; devfn <= 0xff; devfn++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 539 | struct device dummy; |
| 540 | uint32_t id, class; |
| 541 | uint8_t cmd, tmp, hdr_type; |
| 542 | |
| 543 | /* First thing setup the device structure */ |
| 544 | dev = pci_scan_get_dev(&old_devices, devfn); |
| 545 | |
| 546 | dummy.bus = bus; |
| 547 | dummy.devfn = devfn; |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 548 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 549 | /* some broken boards return 0 if a slot is empty: */ |
| 550 | if (!dev && |
| 551 | (id == 0xffffffff || id == 0x00000000 || |
| 552 | id == 0x0000ffff || id == 0xffff0000)) { |
| 553 | printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id); |
| 554 | if (PCI_FUNC(devfn) == 0x00) { |
| 555 | /* if this is a function 0 device and it is not present, |
| 556 | skip to next device */ |
| 557 | devfn += 0x07; |
| 558 | } |
| 559 | /* multi function device, skip to next function */ |
| 560 | continue; |
| 561 | } |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 562 | hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE); |
| 563 | class = pci_read_config32(&dummy, PCI_CLASS_REVISION); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 564 | |
| 565 | if (!dev) { |
| 566 | if ((dev = malloc(sizeof(*dev))) == 0) { |
| 567 | printk_err("PCI: out of memory.\n"); |
| 568 | continue; |
| 569 | } |
| 570 | memset(dev, 0, sizeof(*dev)); |
| 571 | } |
| 572 | |
| 573 | dev->bus = bus; |
| 574 | dev->devfn = devfn; |
| 575 | dev->vendor = id & 0xffff; |
| 576 | dev->device = (id >> 16) & 0xffff; |
| 577 | dev->hdr_type = hdr_type; |
| 578 | /* class code, the upper 3 bytes of PCI_CLASS_REVISION */ |
| 579 | dev->class = class >> 8; |
| 580 | |
| 581 | /* non-destructively determine if device can be a master: */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 582 | cmd = pci_read_config8(dev, PCI_COMMAND); |
| 583 | pci_write_config8(dev, PCI_COMMAND, cmd | PCI_COMMAND_MASTER); |
| 584 | tmp = pci_read_config8(dev, PCI_COMMAND); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 585 | |
| 586 | dev->master = ((tmp & PCI_COMMAND_MASTER) != 0); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 587 | pci_write_config8(dev, PCI_COMMAND, cmd); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 588 | |
| 589 | /* Look at the vendor and device id, or at least the |
| 590 | * header type and class and figure out which set of configuration |
| 591 | * methods to use. |
| 592 | */ |
| 593 | set_pci_ops(dev); |
| 594 | /* Kill the device if we don't have some pci operations for it */ |
| 595 | if (!dev->ops) { |
| 596 | free(dev); |
| 597 | continue; |
| 598 | } |
| 599 | printk_debug("PCI: %02x:%02x.%01x [%04x/%04x]\n", |
| 600 | bus->secondary, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), |
| 601 | dev->vendor, dev->device); |
| 602 | |
| 603 | /* Put it into the global device chain. */ |
| 604 | append_device(dev); |
| 605 | |
| 606 | /* Now insert it into the list of devices held by the parent bus. */ |
| 607 | *bus_last = dev; |
| 608 | bus_last = &dev->sibling; |
| 609 | |
| 610 | if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) { |
| 611 | /* if this is not a multi function device, don't waste time probe |
| 612 | another function. Skip to next device. */ |
| 613 | devfn += 0x07; |
| 614 | } |
| 615 | } |
| 616 | post_code(0x25); |
| 617 | |
| 618 | for(child = bus->children; child; child = child->sibling) { |
| 619 | if (!child->ops->scan_bus) |
| 620 | continue; |
| 621 | max = child->ops->scan_bus(child, max); |
| 622 | |
| 623 | } |
| 624 | /* |
| 625 | * We've scanned the bus and so we know all about what's on |
| 626 | * the other side of any bridges that may be on this bus plus |
| 627 | * any devices. |
| 628 | * |
| 629 | * Return how far we've got finding sub-buses. |
| 630 | */ |
| 631 | printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max); |
| 632 | post_code(0x55); |
| 633 | return max; |
| 634 | } |
| 635 | |
| 636 | /** Scan the bus, first for bridges and next for devices. |
| 637 | * @param pci_bus pointer to the bus structure |
| 638 | * @return The maximum bus number found, after scanning all subordinate busses |
| 639 | */ |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 640 | unsigned int pci_scan_bridge(struct device *bus, unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 641 | { |
| 642 | uint32_t buses; |
| 643 | uint16_t cr; |
| 644 | /* Set up the primary, secondary and subordinate bus numbers. We have |
| 645 | * no idea how many buses are behind this bridge yet, so we set the |
| 646 | * subordinate bus number to 0xff for the moment |
| 647 | */ |
| 648 | bus->secondary = ++max; |
| 649 | bus->subordinate = 0xff; |
| 650 | |
| 651 | /* Clear all status bits and turn off memory, I/O and master enables. */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 652 | cr = pci_read_config16(bus, PCI_COMMAND); |
| 653 | pci_write_config16(bus, PCI_COMMAND, 0x0000); |
| 654 | pci_write_config16(bus, PCI_STATUS, 0xffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Read the existing primary/secondary/subordinate bus |
| 658 | * number configuration. |
| 659 | */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 660 | buses = pci_read_config32(bus, PCI_PRIMARY_BUS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 661 | |
| 662 | /* Configure the bus numbers for this bridge: the configuration |
| 663 | * transactions will not be propagated by the bridge if it is not |
| 664 | * correctly configured |
| 665 | */ |
| 666 | buses &= 0xff000000; |
| 667 | buses |= (((unsigned int) (bus->bus->secondary) << 0) | |
| 668 | ((unsigned int) (bus->secondary) << 8) | |
| 669 | ((unsigned int) (bus->subordinate) << 16)); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 670 | pci_write_config32(bus, PCI_PRIMARY_BUS, buses); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 671 | |
| 672 | /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */ |
| 673 | max = pci_scan_bus(bus, max); |
| 674 | |
| 675 | /* We know the number of buses behind this bridge. Set the subordinate |
| 676 | * bus number to its real value |
| 677 | */ |
| 678 | bus->subordinate = max; |
| 679 | buses = (buses & 0xff00ffff) | |
| 680 | ((unsigned int) (bus->subordinate) << 16); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame^] | 681 | pci_write_config32(bus, PCI_PRIMARY_BUS, buses); |
| 682 | pci_write_config16(bus, PCI_COMMAND, cr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 683 | |
| 684 | return max; |
| 685 | } |
| 686 | |
| 687 | |
| 688 | static void pci_root_read_resources(struct device *bus) |
| 689 | { |
| 690 | int res = 0; |
| 691 | /* Initialize the system wide io space constraints */ |
| 692 | bus->resource[res].base = 0x400; |
| 693 | bus->resource[res].size = 0; |
| 694 | bus->resource[res].align = 0; |
| 695 | bus->resource[res].gran = 0; |
| 696 | bus->resource[res].limit = 0xffffUL; |
| 697 | bus->resource[res].flags = IORESOURCE_IO; |
| 698 | bus->resource[res].index = PCI_IO_BASE; |
| 699 | compute_allocate_resource(bus, &bus->resource[res], |
| 700 | IORESOURCE_IO, IORESOURCE_IO); |
| 701 | res++; |
| 702 | |
| 703 | /* Initialize the system wide memory resources constraints */ |
| 704 | bus->resource[res].base = 0; |
| 705 | bus->resource[res].size = 0; |
| 706 | bus->resource[res].align = 0; |
| 707 | bus->resource[res].gran = 0; |
| 708 | bus->resource[res].limit = 0xffffffffUL; |
| 709 | bus->resource[res].flags = IORESOURCE_MEM; |
| 710 | bus->resource[res].index = PCI_MEMORY_BASE; |
| 711 | compute_allocate_resource(bus, &bus->resource[res], |
| 712 | IORESOURCE_MEM, IORESOURCE_MEM); |
| 713 | res++; |
| 714 | |
| 715 | bus->resources = res; |
| 716 | } |
| 717 | static void pci_root_set_resources(struct device *bus) |
| 718 | { |
| 719 | compute_allocate_resource(bus, |
| 720 | &bus->resource[0], IORESOURCE_IO, IORESOURCE_IO); |
| 721 | compute_allocate_resource(bus, |
| 722 | &bus->resource[1], IORESOURCE_MEM, IORESOURCE_MEM); |
| 723 | assign_resources(bus); |
| 724 | } |
| 725 | |
| 726 | struct device_operations default_pci_ops_root = { |
| 727 | .read_resources = pci_root_read_resources, |
| 728 | .set_resources = pci_root_set_resources, |
| 729 | .init = 0, |
| 730 | .scan_bus = pci_scan_bus, |
| 731 | }; |
| 732 | |