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