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> |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 17 | #include <arch/io.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 18 | #include <device/device.h> |
| 19 | #include <device/pci.h> |
| 20 | #include <device/pci_ids.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 21 | #include <part/hard_reset.h> |
Eric Biederman | 30e143a | 2003-09-01 23:45:32 +0000 | [diff] [blame] | 22 | #include <part/fallback_boot.h> |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 23 | #include <delay.h> |
| 24 | |
| 25 | static uint8_t pci_moving_config8(struct device *dev, unsigned reg) |
| 26 | { |
| 27 | uint8_t value, ones, zeroes; |
| 28 | value = pci_read_config8(dev, reg); |
| 29 | |
| 30 | pci_write_config8(dev, reg, 0xff); |
| 31 | ones = pci_read_config8(dev, reg); |
| 32 | |
| 33 | pci_write_config8(dev, reg, 0x00); |
| 34 | zeroes = pci_read_config8(dev, reg); |
| 35 | |
| 36 | pci_write_config8(dev, reg, value); |
| 37 | |
| 38 | return ones ^ zeroes; |
| 39 | } |
| 40 | static uint16_t pci_moving_config16(struct device *dev, unsigned reg) |
| 41 | { |
| 42 | uint16_t value, ones, zeroes; |
| 43 | value = pci_read_config16(dev, reg); |
| 44 | |
| 45 | pci_write_config16(dev, reg, 0xffff); |
| 46 | ones = pci_read_config16(dev, reg); |
| 47 | |
| 48 | pci_write_config16(dev, reg, 0x0000); |
| 49 | zeroes = pci_read_config16(dev, reg); |
| 50 | |
| 51 | pci_write_config16(dev, reg, value); |
| 52 | |
| 53 | return ones ^ zeroes; |
| 54 | } |
| 55 | static uint32_t pci_moving_config32(struct device *dev, unsigned reg) |
| 56 | { |
| 57 | uint32_t value, ones, zeroes; |
| 58 | value = pci_read_config32(dev, reg); |
| 59 | |
| 60 | pci_write_config32(dev, reg, 0xffffffff); |
| 61 | ones = pci_read_config32(dev, reg); |
| 62 | |
| 63 | pci_write_config32(dev, reg, 0x00000000); |
| 64 | zeroes = pci_read_config32(dev, reg); |
| 65 | |
| 66 | pci_write_config32(dev, reg, value); |
| 67 | |
| 68 | return ones ^ zeroes; |
| 69 | } |
| 70 | |
| 71 | unsigned pci_find_capability(device_t dev, unsigned cap) |
| 72 | { |
| 73 | unsigned pos; |
| 74 | pos = 0; |
| 75 | switch(dev->hdr_type & 0x7f) { |
| 76 | case PCI_HEADER_TYPE_NORMAL: |
| 77 | case PCI_HEADER_TYPE_BRIDGE: |
| 78 | pos = PCI_CAPABILITY_LIST; |
| 79 | break; |
| 80 | } |
| 81 | if (pos > PCI_CAP_LIST_NEXT) { |
| 82 | pos = pci_read_config8(dev, pos); |
| 83 | } |
| 84 | while(pos != 0) { /* loop through the linked list */ |
| 85 | int this_cap; |
| 86 | this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID); |
| 87 | if (this_cap == cap) { |
| 88 | return pos; |
| 89 | } |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 94 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 95 | /** Given a device and register, read the size of the BAR for that register. |
| 96 | * @param dev Pointer to the device structure |
| 97 | * @param resource Pointer to the resource structure |
| 98 | * @param index Address of the pci configuration register |
| 99 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 100 | struct resource *pci_get_resource(struct device *dev, unsigned long index) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 101 | { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 102 | struct resource *resource; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 103 | unsigned long value, attr; |
| 104 | resource_t moving, limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 105 | |
| 106 | /* Initialize the resources to nothing */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 107 | resource = new_resource(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 108 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 109 | /* Get the initial value */ |
| 110 | value = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 111 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 112 | /* See which bits move */ |
| 113 | moving = pci_moving_config32(dev, index); |
| 114 | |
| 115 | /* Initialize attr to the bits that do not move */ |
| 116 | attr = value & ~moving; |
| 117 | |
| 118 | /* If it is a 64bit resource look at the high half as well */ |
| 119 | if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) && |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 120 | ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64)) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 121 | { |
| 122 | /* Find the high bits that move */ |
| 123 | moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32; |
| 124 | } |
| 125 | /* Find the resource constraints. |
| 126 | * |
| 127 | * Start by finding the bits that move. From there: |
| 128 | * - Size is the least significant bit of the bits that move. |
| 129 | * - Limit is all of the bits that move plus all of the lower bits. |
| 130 | * See PCI Spec 6.2.5.1 ... |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 131 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 132 | limit = 0; |
| 133 | if (moving) { |
| 134 | resource->size = 1; |
| 135 | resource->align = resource->gran = 0; |
| 136 | while(!(moving & resource->size)) { |
| 137 | resource->size <<= 1; |
| 138 | resource->align += 1; |
| 139 | resource->gran += 1; |
| 140 | } |
| 141 | resource->limit = limit = moving | (resource->size - 1); |
| 142 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 143 | /* |
| 144 | * some broken hardware has read-only registers that do not |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 145 | * really size correctly. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 146 | * Example: the acer m7229 has BARs 1-4 normally read-only. |
| 147 | * so BAR1 at offset 0x10 reads 0x1f1. If you size that register |
| 148 | * by writing 0xffffffff to it, it will read back as 0x1f1 -- a |
| 149 | * violation of the spec. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 150 | * We catch this case and ignore it by observing which bits move, |
| 151 | * This also catches the common case unimplemented registers |
| 152 | * that always read back as 0. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 153 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 154 | if (moving == 0) { |
| 155 | if (value != 0) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 156 | printk_debug( |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 157 | "%s register %02x(%08x), read-only ignoring it\n", |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 158 | dev_path(dev), index, value); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 159 | } |
| 160 | resource->flags = 0; |
| 161 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 162 | else if (attr & PCI_BASE_ADDRESS_SPACE_IO) { |
| 163 | /* An I/O mapped base address */ |
| 164 | attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 165 | resource->flags |= IORESOURCE_IO; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 166 | /* I don't want to deal with 32bit I/O resources */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 167 | resource->limit = 0xffff; |
| 168 | } |
| 169 | else { |
| 170 | /* A Memory mapped base address */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 171 | attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 172 | resource->flags |= IORESOURCE_MEM; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 173 | if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 174 | resource->flags |= IORESOURCE_PREFETCH; |
| 175 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 176 | attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK; |
| 177 | if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 178 | /* 32bit limit */ |
| 179 | resource->limit = 0xffffffffUL; |
| 180 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 181 | else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 182 | /* 1MB limit */ |
| 183 | resource->limit = 0x000fffffUL; |
| 184 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 185 | else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) { |
| 186 | /* 64bit limit */ |
| 187 | resource->limit = 0xffffffffffffffffULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 188 | resource->flags |= IORESOURCE_PCI64; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 189 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 190 | else { |
| 191 | /* Invalid value */ |
| 192 | resource->flags = 0; |
| 193 | } |
| 194 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 195 | /* Don't let the limit exceed which bits can move */ |
| 196 | if (resource->limit > limit) { |
| 197 | resource->limit = limit; |
| 198 | } |
| 199 | #if 0 |
| 200 | if (resource->flags) { |
| 201 | printk_debug("%s %02x ->", |
| 202 | dev_path(dev), resource->index); |
| 203 | printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n", |
| 204 | value, zeroes, ones, attr); |
| 205 | printk_debug( |
| 206 | "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s%s\n ", |
| 207 | dev_path(dev), |
| 208 | resource->index, |
| 209 | resource->size, resource->limit, |
| 210 | (resource->flags == 0) ? "unused": |
| 211 | (resource->flags & IORESOURCE_IO)? "io": |
| 212 | (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem", |
| 213 | (resource->flags & IORESOURCE_PCI64)?"64":""); |
| 214 | } |
| 215 | #endif |
| 216 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 217 | return resource; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** Read the base address registers for a given device. |
| 221 | * @param dev Pointer to the dev structure |
| 222 | * @param howmany How many registers to read (6 for device, 2 for bridge) |
| 223 | */ |
| 224 | static void pci_read_bases(struct device *dev, unsigned int howmany) |
| 225 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 226 | unsigned long index; |
| 227 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 228 | for(index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 229 | struct resource *resource; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 230 | resource = pci_get_resource(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 231 | index += (resource->flags & IORESOURCE_PCI64)?8:4; |
| 232 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 233 | compact_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 236 | static void pci_set_resource(struct device *dev, struct resource *resource); |
| 237 | |
| 238 | static void pci_record_bridge_resource( |
| 239 | struct device *dev, resource_t moving, |
| 240 | unsigned index, unsigned long mask, unsigned long type) |
| 241 | { |
| 242 | /* Initiliaze the constraints on the current bus */ |
| 243 | struct resource *resource; |
| 244 | resource = 0; |
| 245 | if (moving) { |
| 246 | unsigned long gran; |
| 247 | resource_t step; |
| 248 | resource = new_resource(dev, index); |
| 249 | resource->size = 0; |
| 250 | gran = 0; |
| 251 | step = 1; |
| 252 | while((moving & step) == 0) { |
| 253 | gran += 1; |
| 254 | step <<= 1; |
| 255 | } |
| 256 | resource->gran = gran; |
| 257 | resource->align = gran; |
| 258 | resource->limit = moving | (step - 1); |
| 259 | resource->flags = type | IORESOURCE_PCI_BRIDGE; |
| 260 | compute_allocate_resource(&dev->link[0], resource, mask, type); |
| 261 | /* If there is nothing behind the resource, |
| 262 | * clear it and forget it. |
| 263 | */ |
| 264 | if (resource->size == 0) { |
| 265 | resource->base = moving; |
| 266 | resource->flags |= IORESOURCE_ASSIGNED; |
| 267 | resource->flags &= ~IORESOURCE_STORED; |
| 268 | pci_set_resource(dev, resource); |
| 269 | resource->flags = 0; |
| 270 | } |
| 271 | } |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 276 | static void pci_bridge_read_bases(struct device *dev) |
| 277 | { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 278 | resource_t moving_base, moving_limit, moving; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 279 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 280 | |
| 281 | /* See if the bridge I/O resources are implemented */ |
| 282 | moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8; |
| 283 | moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16; |
| 284 | |
| 285 | moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8; |
| 286 | moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16; |
| 287 | |
| 288 | moving = moving_base & moving_limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 289 | |
| 290 | /* Initialize the io space constraints on the current bus */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 291 | pci_record_bridge_resource( |
| 292 | dev, moving, PCI_IO_BASE, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 293 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 294 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 295 | |
| 296 | /* See if the bridge prefmem resources are implemented */ |
| 297 | moving_base = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16; |
| 298 | moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32; |
| 299 | |
| 300 | moving_limit = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16; |
| 301 | moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32; |
| 302 | |
| 303 | moving = moving_base & moving_limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 304 | /* Initiliaze the prefetchable memory constraints on the current bus */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 305 | pci_record_bridge_resource( |
| 306 | dev, moving, PCI_PREF_MEMORY_BASE, |
| 307 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 308 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 309 | |
| 310 | |
| 311 | /* See if the bridge mem resources are implemented */ |
| 312 | moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16; |
| 313 | moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16; |
| 314 | |
| 315 | moving = moving_base & moving_limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 316 | |
| 317 | /* Initialize the memory resources on the current bus */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 318 | pci_record_bridge_resource( |
| 319 | dev, moving, PCI_MEMORY_BASE, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 320 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 321 | IORESOURCE_MEM); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 322 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 323 | compact_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 326 | void pci_dev_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 327 | { |
| 328 | uint32_t addr; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 329 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 330 | pci_read_bases(dev, 6); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 331 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 332 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 333 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
| 334 | } |
| 335 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 336 | void pci_bus_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 337 | { |
| 338 | uint32_t addr; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 339 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 340 | pci_bridge_read_bases(dev); |
| 341 | pci_read_bases(dev, 2); |
| 342 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 343 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 344 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 347 | static void pci_set_resource(struct device *dev, struct resource *resource) |
| 348 | { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 349 | resource_t base, end; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 350 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 351 | /* Make certain the resource has actually been set */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 352 | if (!(resource->flags & IORESOURCE_ASSIGNED)) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 353 | printk_err("ERROR: %s %02x not allocated\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 354 | dev_path(dev), resource->index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 355 | return; |
| 356 | } |
| 357 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 358 | /* If I have already stored this resource don't worry about it */ |
| 359 | if (resource->flags & IORESOURCE_STORED) { |
| 360 | return; |
| 361 | } |
| 362 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 363 | /* If the resources is substractive don't worry about it */ |
| 364 | if (resource->flags & IORESOURCE_SUBTRACTIVE) { |
| 365 | return; |
| 366 | } |
| 367 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 368 | /* Only handle PCI memory and IO resources for now */ |
| 369 | if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO))) |
| 370 | return; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 371 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 372 | /* Enable the resources in the command register */ |
| 373 | if (resource->size) { |
| 374 | if (resource->flags & IORESOURCE_MEM) { |
| 375 | dev->command |= PCI_COMMAND_MEMORY; |
| 376 | } |
| 377 | if (resource->flags & IORESOURCE_IO) { |
| 378 | dev->command |= PCI_COMMAND_IO; |
| 379 | } |
| 380 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
| 381 | dev->command |= PCI_COMMAND_MASTER; |
| 382 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 383 | } |
| 384 | /* Get the base address */ |
| 385 | base = resource->base; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 386 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 387 | /* Get the end */ |
| 388 | end = resource_end(resource); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 389 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 390 | /* Now store the resource */ |
| 391 | resource->flags |= IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 392 | if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 393 | unsigned long base_lo, base_hi; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 394 | /* |
| 395 | * some chipsets allow us to set/clear the IO bit. |
| 396 | * (e.g. VIA 82c686a.) So set it to be safe) |
| 397 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 398 | base_lo = base & 0xffffffff; |
| 399 | base_hi = (base >> 32) & 0xffffffff; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 400 | if (resource->flags & IORESOURCE_IO) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 401 | base_lo |= PCI_BASE_ADDRESS_SPACE_IO; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 402 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 403 | pci_write_config32(dev, resource->index, base_lo); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 404 | if (resource->flags & IORESOURCE_PCI64) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 405 | pci_write_config32(dev, resource->index + 4, base_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 406 | } |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 407 | } |
| 408 | else if (resource->index == PCI_IO_BASE) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 409 | /* set the IO ranges */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 410 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 411 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 412 | pci_write_config8(dev, PCI_IO_BASE, base >> 8); |
| 413 | pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16); |
| 414 | pci_write_config8(dev, PCI_IO_LIMIT, end >> 8); |
| 415 | pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 416 | } |
| 417 | else if (resource->index == PCI_MEMORY_BASE) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 418 | /* set the memory range */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 419 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 420 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 421 | IORESOURCE_MEM); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 422 | pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 423 | pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 424 | } |
| 425 | else if (resource->index == PCI_PREF_MEMORY_BASE) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 426 | /* set the prefetchable memory range */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 427 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 428 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 429 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 430 | pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16); |
| 431 | pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32); |
| 432 | pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16); |
| 433 | pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 434 | } |
| 435 | else { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 436 | /* Don't let me think I stored the resource */ |
| 437 | resource->flags &= ~IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 438 | printk_err("ERROR: invalid resource->index %x\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 439 | resource->index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 440 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 441 | report_resource_stored(dev, resource, ""); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 445 | void pci_dev_set_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 446 | { |
| 447 | struct resource *resource, *last; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 448 | unsigned link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 449 | uint8_t line; |
| 450 | |
| 451 | last = &dev->resource[dev->resources]; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 452 | |
| 453 | for(resource = &dev->resource[0]; resource < last; resource++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 454 | pci_set_resource(dev, resource); |
| 455 | } |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 456 | for(link = 0; link < dev->links; link++) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 457 | struct bus *bus; |
| 458 | bus = &dev->link[link]; |
| 459 | if (bus->children) { |
| 460 | assign_resources(bus); |
| 461 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* set a default latency timer */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 465 | pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 466 | |
| 467 | /* set a default secondary latency timer */ |
| 468 | if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 469 | pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* zero the irq settings */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 473 | line = pci_read_config8(dev, PCI_INTERRUPT_PIN); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 474 | if (line) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 475 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 476 | } |
| 477 | /* 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] | 478 | pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 481 | void pci_dev_enable_resources(struct device *dev) |
| 482 | { |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 483 | const struct pci_operations *ops; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 484 | uint16_t command; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 485 | |
| 486 | /* Set the subsystem vendor and device id for mainboard devices */ |
| 487 | ops = ops_pci(dev); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 488 | if (dev->on_mainboard && ops && ops->set_subsystem) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 489 | printk_debug("%s subsystem <- %02x/%02x\n", |
| 490 | dev_path(dev), |
| 491 | MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID, |
| 492 | MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID); |
| 493 | ops->set_subsystem(dev, |
| 494 | MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID, |
| 495 | MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID); |
| 496 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 497 | command = pci_read_config16(dev, PCI_COMMAND); |
| 498 | command |= dev->command; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 499 | command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 500 | printk_debug("%s cmd <- %02x\n", dev_path(dev), command); |
| 501 | pci_write_config16(dev, PCI_COMMAND, command); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void pci_bus_enable_resources(struct device *dev) |
| 505 | { |
| 506 | uint16_t ctrl; |
| 507 | ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL); |
| 508 | ctrl |= dev->link[0].bridge_ctrl; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 509 | ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 510 | printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl); |
| 511 | pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl); |
| 512 | |
| 513 | pci_dev_enable_resources(dev); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 514 | |
| 515 | enable_childrens_resources(dev); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 518 | |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 519 | void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 520 | { |
| 521 | pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, |
| 522 | ((device & 0xffff) << 16) | (vendor & 0xffff)); |
| 523 | } |
| 524 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 525 | /** Default device operation for PCI devices */ |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 526 | static struct pci_operations pci_dev_ops_pci = { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 527 | .set_subsystem = pci_dev_set_subsystem, |
| 528 | }; |
| 529 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 530 | struct device_operations default_pci_ops_dev = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 531 | .read_resources = pci_dev_read_resources, |
| 532 | .set_resources = pci_dev_set_resources, |
| 533 | .enable_resources = pci_dev_enable_resources, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 534 | .init = 0, |
| 535 | .scan_bus = 0, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 536 | .enable = 0, |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 537 | .ops_pci = &pci_dev_ops_pci, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 538 | }; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 539 | |
| 540 | /** Default device operations for PCI bridges */ |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 541 | static struct pci_operations pci_bus_ops_pci = { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 542 | .set_subsystem = 0, |
| 543 | }; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 544 | struct device_operations default_pci_ops_bus = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 545 | .read_resources = pci_bus_read_resources, |
| 546 | .set_resources = pci_dev_set_resources, |
| 547 | .enable_resources = pci_bus_enable_resources, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 548 | .init = 0, |
| 549 | .scan_bus = pci_scan_bridge, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 550 | .enable = 0, |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 551 | .ops_pci = &pci_bus_ops_pci, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 552 | }; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 553 | |
| 554 | /** |
| 555 | * @brief Set up PCI device operation |
| 556 | * |
| 557 | * |
| 558 | * @param dev |
| 559 | * |
| 560 | * @see pci_drivers |
| 561 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 562 | static void set_pci_ops(struct device *dev) |
| 563 | { |
| 564 | struct pci_driver *driver; |
| 565 | if (dev->ops) { |
| 566 | return; |
| 567 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 568 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 569 | /* Look through the list of setup drivers and find one for |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 570 | * this pci device |
| 571 | */ |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 572 | for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 573 | if ((driver->vendor == dev->vendor) && |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 574 | (driver->device == dev->device)) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 575 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 576 | dev->ops = driver->ops; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 577 | printk_debug("%s [%04x/%04x] %sops\n", |
| 578 | dev_path(dev), |
| 579 | driver->vendor, driver->device, |
| 580 | (driver->ops->scan_bus?"bus ":"")); |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 581 | return; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 584 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 585 | /* If I don't have a specific driver use the default operations */ |
| 586 | switch(dev->hdr_type & 0x7f) { /* header type */ |
| 587 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ |
| 588 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) |
| 589 | goto bad; |
| 590 | dev->ops = &default_pci_ops_dev; |
| 591 | break; |
| 592 | case PCI_HEADER_TYPE_BRIDGE: |
| 593 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 594 | goto bad; |
| 595 | dev->ops = &default_pci_ops_bus; |
| 596 | break; |
| 597 | default: |
| 598 | bad: |
Li-Ta Lo | 69c5a90 | 2004-04-29 20:08:54 +0000 | [diff] [blame] | 599 | if (dev->enabled) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 600 | printk_err("%s [%04x/%04x/%06x] has unknown header " |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 601 | "type %02x, ignoring.\n", |
| 602 | dev_path(dev), |
| 603 | dev->vendor, dev->device, |
| 604 | dev->class >> 8, dev->hdr_type); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 605 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 606 | } |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | /** |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 611 | * @brief See if we have already allocated a device structure for a given devfn. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 612 | * |
| 613 | * Given a linked list of PCI device structures and a devfn number, find the |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 614 | * device structure correspond to the devfn, if present. This function also |
| 615 | * removes the device structure from the linked list. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 616 | * |
| 617 | * @param list the device structure list |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 618 | * @param devfn a device/function number |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 619 | * |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 620 | * @return pointer to the device structure found or null of we have not |
| 621 | * allocated a device for this devfn yet. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 622 | */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 623 | static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 624 | { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 625 | struct device *dev; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 626 | dev = 0; |
| 627 | for(; *list; list = &(*list)->sibling) { |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 628 | if ((*list)->path.type != DEVICE_PATH_PCI) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 629 | printk_err("child %s not a pci device\n", |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 630 | dev_path(*list)); |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 631 | continue; |
| 632 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 633 | if ((*list)->path.u.pci.devfn == devfn) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 634 | /* Unlink from the list */ |
| 635 | dev = *list; |
| 636 | *list = (*list)->sibling; |
| 637 | dev->sibling = 0; |
| 638 | break; |
| 639 | } |
| 640 | } |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 641 | /* Just like alloc_dev add the device to the list of device on the bus. |
| 642 | * When the list of devices was formed we removed all of the parents |
| 643 | * children, and now we are interleaving static and dynamic devices in |
| 644 | * order on the bus. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 645 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 646 | if (dev) { |
| 647 | device_t child; |
| 648 | /* Find the last child of our parent */ |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 649 | for (child = dev->bus->children; child && child->sibling; ) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 650 | child = child->sibling; |
| 651 | } |
| 652 | /* Place the device on the list of children of it's parent. */ |
| 653 | if (child) { |
| 654 | child->sibling = dev; |
| 655 | } else { |
| 656 | dev->bus->children = dev; |
| 657 | } |
| 658 | } |
| 659 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 660 | return dev; |
| 661 | } |
| 662 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 663 | /** |
| 664 | * @brief Scan a PCI bus. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 665 | * |
| 666 | * Determine the existence of devices and bridges on a PCI bus. If there are |
| 667 | * bridges on the bus, recursively scan the buses behind the bridges. |
| 668 | * |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 669 | * @param bus pointer to the bus structure |
| 670 | * @param min_devfn minimum devfn to look at in the scan usually 0x00 |
| 671 | * @param max_devfn maximum devfn to look at in the scan usually 0xff |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 672 | * @param max current bus number |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 673 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 674 | * @return The maximum bus number found, after scanning all subordinate busses |
| 675 | */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 676 | unsigned int pci_scan_bus(struct bus *bus, |
| 677 | unsigned min_devfn, unsigned max_devfn, |
| 678 | unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 679 | { |
| 680 | unsigned int devfn; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 681 | device_t dev; |
| 682 | device_t old_devices; |
| 683 | device_t child; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 684 | |
| 685 | printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary); |
| 686 | |
| 687 | old_devices = bus->children; |
| 688 | bus->children = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 689 | |
| 690 | post_code(0x24); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 691 | |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame] | 692 | /* probe all devices/functions on this bus with some optimization for |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 693 | * non-existence and single funcion devices |
| 694 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 695 | for (devfn = min_devfn; devfn <= max_devfn; devfn++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 696 | uint32_t id, class; |
Eric Biederman | 30e143a | 2003-09-01 23:45:32 +0000 | [diff] [blame] | 697 | uint8_t hdr_type; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 698 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 699 | /* First thing setup the device structure */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 700 | dev = pci_scan_get_dev(&old_devices, devfn); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 701 | |
| 702 | /* Detect if a device is present */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 703 | if (!dev) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 704 | struct device dummy; |
| 705 | dummy.bus = bus; |
| 706 | dummy.path.type = DEVICE_PATH_PCI; |
| 707 | dummy.path.u.pci.devfn = devfn; |
| 708 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
| 709 | /* some broken boards return 0 if a slot is empty: */ |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 710 | if ((id == 0xffffffff) || (id == 0x00000000) || |
| 711 | (id == 0x0000ffff) || (id == 0xffff0000)) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 712 | { |
| 713 | printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 714 | if (PCI_FUNC(devfn) == 0x00) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 715 | /* if this is a function 0 device and |
| 716 | * it is not present, |
| 717 | * skip to next device |
| 718 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 719 | devfn += 0x07; |
| 720 | } |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 721 | /* This function in a multi function device is |
| 722 | * not present, skip to the next function. |
| 723 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 724 | continue; |
| 725 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 726 | dev = alloc_dev(bus, &dummy.path); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 727 | } |
| 728 | else { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 729 | /* Enable/disable the device. Once we have |
| 730 | * found the device specific operations this |
| 731 | * operations we will disable the device with |
| 732 | * those as well. |
| 733 | * |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 734 | * This is geared toward devices that have subfunctions |
| 735 | * that do not show up by default. |
| 736 | * |
| 737 | * If a device is a stuff option on the motherboard |
| 738 | * it may be absent and enable_dev must cope. |
| 739 | * |
| 740 | */ |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 741 | if (dev->chip_ops && dev->chip_ops->enable_dev) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 742 | { |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 743 | dev->chip_ops->enable_dev(dev); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 744 | } |
| 745 | /* Now read the vendor and device id */ |
| 746 | id = pci_read_config32(dev, PCI_VENDOR_ID); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 747 | |
| 748 | /* If the device does not have a pci id disable it. |
| 749 | * Possibly this is because we have already disabled |
| 750 | * the device. But this also handles optional devices |
| 751 | * that may not always show up. |
| 752 | */ |
| 753 | if (id == 0xffffffff || id == 0x00000000 || |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 754 | id == 0x0000ffff || id == 0xffff0000) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 755 | { |
| 756 | if (dev->enabled) { |
| 757 | printk_info("Disabling static device: %s\n", |
| 758 | dev_path(dev)); |
| 759 | dev->enabled = 0; |
| 760 | } |
| 761 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 762 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 763 | /* Read the rest of the pci configuration information */ |
| 764 | hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 765 | class = pci_read_config32(dev, PCI_CLASS_REVISION); |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame] | 766 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 767 | /* Store the interesting information in the device structure */ |
| 768 | dev->vendor = id & 0xffff; |
| 769 | dev->device = (id >> 16) & 0xffff; |
| 770 | dev->hdr_type = hdr_type; |
| 771 | /* class code, the upper 3 bytes of PCI_CLASS_REVISION */ |
| 772 | dev->class = class >> 8; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 773 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 774 | /* Architectural/System devices always need to |
| 775 | * be bus masters. |
| 776 | */ |
| 777 | if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) { |
| 778 | dev->command |= PCI_COMMAND_MASTER; |
| 779 | } |
| 780 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 781 | /* Look at the vendor and device id, or at least the |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 782 | * header type and class and figure out which set of |
| 783 | * configuration methods to use. Unless we already |
| 784 | * have some pci ops. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 785 | */ |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 786 | set_pci_ops(dev); |
| 787 | /* Error if we don't have some pci operations for it */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 788 | if (!dev->ops) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 789 | printk_err("%s No device operations\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 790 | dev_path(dev)); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 791 | continue; |
| 792 | } |
| 793 | |
| 794 | /* Now run the magic enable/disable sequence for the device */ |
| 795 | if (dev->ops && dev->ops->enable) { |
| 796 | dev->ops->enable(dev); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 797 | } |
Eric Biederman | 4086d16 | 2003-07-17 03:26:03 +0000 | [diff] [blame] | 798 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 799 | printk_debug("%s [%04x/%04x] %s\n", |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 800 | dev_path(dev), |
| 801 | dev->vendor, dev->device, |
| 802 | dev->enabled?"enabled": "disabled"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 803 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 804 | if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 805 | /* if this is not a multi function device, |
| 806 | * don't waste time probing another function. |
| 807 | * Skip to next device. |
| 808 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 809 | devfn += 0x07; |
| 810 | } |
| 811 | } |
| 812 | post_code(0x25); |
| 813 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 814 | /* For all children that implement scan_bus (i.e. bridges) |
| 815 | * scan the bus behind that child. |
| 816 | */ |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 817 | for (child = bus->children; child; child = child->sibling) { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 818 | if (!child->enabled || |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 819 | !child->ops || |
| 820 | !child->ops->scan_bus) |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 821 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 822 | continue; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 823 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 824 | max = child->ops->scan_bus(child, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 825 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 826 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 827 | /* |
| 828 | * We've scanned the bus and so we know all about what's on |
| 829 | * the other side of any bridges that may be on this bus plus |
| 830 | * any devices. |
| 831 | * |
| 832 | * Return how far we've got finding sub-buses. |
| 833 | */ |
| 834 | printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max); |
| 835 | post_code(0x55); |
| 836 | return max; |
| 837 | } |
| 838 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 839 | /** |
| 840 | * @brief Scan a PCI bridge and the buses behind the bridge. |
| 841 | * |
| 842 | * Determine the existence of buses behind the bridge. Set up the bridge |
| 843 | * according to the result of the scan. |
| 844 | * |
| 845 | * This function is the default scan_bus() method for PCI bridge devices. |
| 846 | * |
| 847 | * @param dev pointer to the bridge device |
| 848 | * @param max the highest bus number assgined up to now |
| 849 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 850 | * @return The maximum bus number found, after scanning all subordinate busses |
| 851 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 852 | unsigned int pci_scan_bridge(struct device *dev, unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 853 | { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 854 | struct bus *bus; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 855 | uint32_t buses; |
| 856 | uint16_t cr; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 857 | |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 858 | printk_spew("%s for %s\n", __func__, dev_path(dev)); |
| 859 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 860 | bus = &dev->link[0]; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 861 | bus->dev = dev; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 862 | dev->links = 1; |
| 863 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 864 | /* Set up the primary, secondary and subordinate bus numbers. We have |
| 865 | * no idea how many buses are behind this bridge yet, so we set the |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 866 | * subordinate bus number to 0xff for the moment. |
| 867 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 868 | bus->secondary = ++max; |
| 869 | bus->subordinate = 0xff; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 870 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 871 | /* Clear all status bits and turn off memory, I/O and master enables. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 872 | cr = pci_read_config16(dev, PCI_COMMAND); |
| 873 | pci_write_config16(dev, PCI_COMMAND, 0x0000); |
| 874 | pci_write_config16(dev, PCI_STATUS, 0xffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 875 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 876 | /* |
| 877 | * Read the existing primary/secondary/subordinate bus |
| 878 | * number configuration. |
| 879 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 880 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 881 | |
| 882 | /* Configure the bus numbers for this bridge: the configuration |
| 883 | * transactions will not be propagated by the bridge if it is not |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 884 | * correctly configured. |
| 885 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 886 | buses &= 0xff000000; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 887 | buses |= (((unsigned int) (dev->bus->secondary) << 0) | |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 888 | ((unsigned int) (bus->secondary) << 8) | |
| 889 | ((unsigned int) (bus->subordinate) << 16)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 890 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 891 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 892 | /* Now we can scan all subordinate buses |
| 893 | * i.e. the bus behind the bridge. |
| 894 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 895 | max = pci_scan_bus(bus, 0x00, 0xff, max); |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 896 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 897 | /* We know the number of buses behind this bridge. Set the subordinate |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 898 | * bus number to its real value. |
| 899 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 900 | bus->subordinate = max; |
| 901 | buses = (buses & 0xff00ffff) | |
| 902 | ((unsigned int) (bus->subordinate) << 16); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 903 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
| 904 | pci_write_config16(dev, PCI_COMMAND, cr); |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame^] | 905 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 906 | printk_spew("%s returns max %d\n", __func__, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 907 | return max; |
| 908 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 909 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 910 | /* |
| 911 | Tell the EISA int controller this int must be level triggered |
| 912 | THIS IS A KLUDGE -- sorry, this needs to get cleaned up. |
| 913 | */ |
| 914 | static void pci_level_irq(unsigned char intNum) |
| 915 | { |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 916 | unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 917 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 918 | printk_spew("%s: current ints are 0x%x\n", __func__, intBits); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 919 | intBits |= (1 << intNum); |
| 920 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 921 | printk_spew("%s: try to set ints 0x%x\n", __func__, intBits); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 922 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 923 | // Write new values |
| 924 | outb((unsigned char) intBits, 0x4d0); |
| 925 | outb((unsigned char) (intBits >> 8), 0x4d1); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 926 | |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 927 | /* this seems like an error but is not ... */ |
Ronald G. Minnich | 02fa3b2 | 2004-10-06 17:33:54 +0000 | [diff] [blame] | 928 | #if 1 |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 929 | if (inb(0x4d0) != (intBits & 0xf)) { |
| 930 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 931 | __func__, intBits &0xf, inb(0x4d0)); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 932 | } |
| 933 | if (inb(0x4d1) != ((intBits >> 8) & 0xf)) { |
| 934 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 935 | __func__, (intBits>>8) &0xf, inb(0x4d1)); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 936 | } |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 937 | #endif |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 940 | /* |
| 941 | This function assigns IRQs for all functions contained within |
| 942 | the indicated device address. If the device does not exist or does |
| 943 | not require interrupts then this function has no effect. |
| 944 | |
| 945 | This function should be called for each PCI slot in your system. |
| 946 | |
| 947 | pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of |
| 948 | this slot. |
| 949 | The particular irq #s that are passed in depend on the routing inside |
| 950 | your southbridge and on your motherboard. |
| 951 | |
| 952 | -kevinh@ispiri.com |
| 953 | */ |
| 954 | void pci_assign_irqs(unsigned bus, unsigned slot, |
| 955 | const unsigned char pIntAtoD[4]) |
| 956 | { |
| 957 | unsigned functNum; |
| 958 | device_t pdev; |
| 959 | unsigned char line; |
| 960 | unsigned char irq; |
| 961 | unsigned char readback; |
| 962 | |
| 963 | /* Each slot may contain up to eight functions */ |
| 964 | for (functNum = 0; functNum < 8; functNum++) { |
| 965 | pdev = dev_find_slot(bus, (slot << 3) + functNum); |
| 966 | |
| 967 | if (pdev) { |
| 968 | line = pci_read_config8(pdev, PCI_INTERRUPT_PIN); |
| 969 | |
| 970 | // PCI spec says all other values are reserved |
| 971 | if ((line >= 1) && (line <= 4)) { |
| 972 | irq = pIntAtoD[line - 1]; |
| 973 | |
| 974 | printk_debug("Assigning IRQ %d to %d:%x.%d\n", \ |
| 975 | irq, bus, slot, functNum); |
| 976 | |
| 977 | pci_write_config8(pdev, PCI_INTERRUPT_LINE,\ |
| 978 | pIntAtoD[line - 1]); |
| 979 | |
| 980 | readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE); |
| 981 | printk_debug(" Readback = %d\n", readback); |
| 982 | |
| 983 | // Change to level triggered |
| 984 | pci_level_irq(pIntAtoD[line - 1]); |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | } |