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 | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 21 | #include <device/chip.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 22 | #include <part/hard_reset.h> |
Eric Biederman | 30e143a | 2003-09-01 23:45:32 +0000 | [diff] [blame] | 23 | #include <part/fallback_boot.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 24 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | /** Given a device and register, read the size of the BAR for that register. |
| 26 | * @param dev Pointer to the device structure |
| 27 | * @param resource Pointer to the resource structure |
| 28 | * @param index Address of the pci configuration register |
| 29 | */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 30 | static struct resource *pci_get_resource(struct device *dev, unsigned long index) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 31 | { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 32 | struct resource *resource; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | uint32_t addr, size, base; |
| 34 | unsigned long type; |
| 35 | |
| 36 | /* Initialize the resources to nothing */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 37 | resource = get_resource(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 38 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 39 | addr = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 40 | |
| 41 | /* FIXME: more consideration for 64-bit PCI devices, |
| 42 | * we currently detect their size but otherwise |
| 43 | * treat them as 32-bit resources |
| 44 | */ |
| 45 | /* get the size */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 46 | pci_write_config32(dev, index, ~0); |
| 47 | size = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 48 | |
| 49 | /* get the minimum value the bar can be set to */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 50 | pci_write_config32(dev, index, 0); |
| 51 | base = pci_read_config32(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 52 | |
| 53 | /* restore addr */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 54 | pci_write_config32(dev, index, addr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * some broken hardware has read-only registers that do not |
| 58 | * really size correctly. You can tell this if addr == size |
| 59 | * Example: the acer m7229 has BARs 1-4 normally read-only. |
| 60 | * so BAR1 at offset 0x10 reads 0x1f1. If you size that register |
| 61 | * by writing 0xffffffff to it, it will read back as 0x1f1 -- a |
| 62 | * violation of the spec. |
| 63 | * We catch this case and ignore it by settting size and type to 0. |
| 64 | * This incidentally catches the common case where registers |
| 65 | * read back as 0 for both address and size. |
| 66 | */ |
| 67 | if ((addr == size) && (addr == base)) { |
| 68 | if (size != 0) { |
| 69 | printk_debug( |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 70 | "%s register %02x(%08x), read-only ignoring it\n", |
| 71 | dev_path(dev), |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 72 | index, addr); |
| 73 | } |
| 74 | resource->flags = 0; |
| 75 | } |
| 76 | /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */ |
| 77 | else if (size & PCI_BASE_ADDRESS_SPACE_IO) { |
| 78 | type = size & (~PCI_BASE_ADDRESS_IO_MASK); |
| 79 | /* BUG! Top 16 bits can be zero (or not) |
| 80 | * So set them to 0xffff so they go away ... |
| 81 | */ |
| 82 | resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1; |
| 83 | resource->align = log2(resource->size); |
| 84 | resource->gran = resource->align; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 85 | resource->flags |= IORESOURCE_IO; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 86 | resource->limit = 0xffff; |
| 87 | } |
| 88 | else { |
| 89 | /* A Memory mapped base address */ |
| 90 | type = size & (~PCI_BASE_ADDRESS_MEM_MASK); |
| 91 | resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1; |
| 92 | resource->align = log2(resource->size); |
| 93 | resource->gran = resource->align; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 94 | resource->flags |= IORESOURCE_MEM; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 95 | if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) { |
| 96 | resource->flags |= IORESOURCE_PREFETCH; |
| 97 | } |
| 98 | type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK; |
| 99 | if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) { |
| 100 | /* 32bit limit */ |
| 101 | resource->limit = 0xffffffffUL; |
| 102 | } |
| 103 | else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) { |
| 104 | /* 1MB limit */ |
| 105 | resource->limit = 0x000fffffUL; |
| 106 | } |
| 107 | else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) { |
| 108 | unsigned long index_hi; |
| 109 | /* 64bit limit |
| 110 | * For now just treat this as a 32bit limit |
| 111 | */ |
| 112 | index_hi = index + 4; |
| 113 | resource->limit = 0xffffffffUL; |
| 114 | resource->flags |= IORESOURCE_PCI64; |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 115 | addr = pci_read_config32( dev, index_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 116 | /* get the extended size */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 117 | pci_write_config32(dev, index_hi, 0xffffffffUL); |
| 118 | size = pci_read_config32( dev, index_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 119 | |
| 120 | /* get the minimum value the bar can be set to */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 121 | pci_write_config32(dev, index_hi, 0); |
| 122 | base = pci_read_config32(dev, index_hi); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 123 | |
| 124 | /* restore addr */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 125 | pci_write_config32(dev, index_hi, addr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 126 | |
| 127 | if ((size == 0xffffffff) && (base == 0)) { |
| 128 | /* Clear the top half of the bar */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 129 | pci_write_config32(dev, index_hi, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 130 | } |
| 131 | else { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 132 | printk_err("%s Unable to handle 64-bit address\n", |
| 133 | dev_path(dev)); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 134 | resource->flags = IORESOURCE_PCI64; |
| 135 | } |
| 136 | } |
| 137 | else { |
| 138 | /* Invalid value */ |
| 139 | resource->flags = 0; |
| 140 | } |
| 141 | } |
| 142 | /* dev->size holds the flags... */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 143 | return resource; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** Read the base address registers for a given device. |
| 147 | * @param dev Pointer to the dev structure |
| 148 | * @param howmany How many registers to read (6 for device, 2 for bridge) |
| 149 | */ |
| 150 | static void pci_read_bases(struct device *dev, unsigned int howmany) |
| 151 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 152 | unsigned long index; |
| 153 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 154 | 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] | 155 | struct resource *resource; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 156 | resource = pci_get_resource(dev, index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 157 | index += (resource->flags & IORESOURCE_PCI64)?8:4; |
| 158 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 159 | compact_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
| 163 | static void pci_bridge_read_bases(struct device *dev) |
| 164 | { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 165 | struct resource *resource; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 166 | |
| 167 | /* FIXME handle bridges without some of the optional resources */ |
| 168 | |
| 169 | /* Initialize the io space constraints on the current bus */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 170 | resource = get_resource(dev, PCI_IO_BASE); |
| 171 | resource->size = 0; |
| 172 | resource->align = log2(PCI_IO_BRIDGE_ALIGN); |
| 173 | resource->gran = log2(PCI_IO_BRIDGE_ALIGN); |
| 174 | resource->limit = 0xffffUL; |
| 175 | resource->flags |= IORESOURCE_IO | IORESOURCE_PCI_BRIDGE; |
| 176 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 177 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 178 | |
| 179 | /* Initiliaze the prefetchable memory constraints on the current bus */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 180 | resource = get_resource(dev, PCI_PREF_MEMORY_BASE); |
| 181 | resource->size = 0; |
| 182 | resource->align = log2(PCI_MEM_BRIDGE_ALIGN); |
| 183 | resource->gran = log2(PCI_MEM_BRIDGE_ALIGN); |
| 184 | resource->limit = 0xffffffffUL; |
| 185 | resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE; |
| 186 | resource->index = PCI_PREF_MEMORY_BASE; |
| 187 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 188 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 189 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 190 | |
| 191 | /* Initialize the memory resources on the current bus */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 192 | resource = get_resource(dev, PCI_MEMORY_BASE); |
| 193 | resource->size = 0; |
| 194 | resource->align = log2(PCI_MEM_BRIDGE_ALIGN); |
| 195 | resource->gran = log2(PCI_MEM_BRIDGE_ALIGN); |
| 196 | resource->limit = 0xffffffffUL; |
| 197 | resource->flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE; |
| 198 | compute_allocate_resource(&dev->link[0], resource, |
| 199 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 200 | IORESOURCE_MEM); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 201 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 202 | compact_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 206 | void pci_dev_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 207 | { |
| 208 | uint32_t addr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 209 | pci_read_bases(dev, 6); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 210 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 211 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
| 212 | } |
| 213 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 214 | void pci_bus_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 215 | { |
| 216 | uint32_t addr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 217 | pci_bridge_read_bases(dev); |
| 218 | pci_read_bases(dev, 2); |
| 219 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 220 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 221 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
| 222 | |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static void pci_set_resource(struct device *dev, struct resource *resource) |
| 227 | { |
| 228 | unsigned long base, limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 229 | unsigned char buf[10]; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 230 | unsigned long gran; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 231 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 232 | /* Make certain the resource has actually been set */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 233 | if (!(resource->flags & IORESOURCE_ASSIGNED)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 234 | #if 1 |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 235 | printk_err("ERROR: %s %02x not allocated\n", |
| 236 | dev_path(dev), resource->index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 237 | #endif |
| 238 | return; |
| 239 | } |
| 240 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 241 | /* If I have already stored this resource don't worry about it */ |
| 242 | if (resource->flags & IORESOURCE_STORED) { |
| 243 | return; |
| 244 | } |
| 245 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 246 | /* Only handle PCI memory and IO resources for now */ |
| 247 | if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO))) |
| 248 | return; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 249 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 250 | if (resource->flags & IORESOURCE_MEM) { |
| 251 | dev->command |= PCI_COMMAND_MEMORY; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 252 | } |
| 253 | if (resource->flags & IORESOURCE_IO) { |
| 254 | dev->command |= PCI_COMMAND_IO; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 255 | } |
| 256 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
| 257 | dev->command |= PCI_COMMAND_MASTER; |
| 258 | } |
| 259 | /* Get the base address */ |
| 260 | base = resource->base; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 261 | /* Get the resource granularity */ |
| 262 | gran = 1UL << resource->gran; |
| 263 | |
| 264 | /* For a non bridge resource granularity and alignment are the same. |
| 265 | * For a bridge resource align is the largest needed alignment below |
| 266 | * the bridge. While the granularity is simply how many low bits of the |
| 267 | * address cannot be set. |
| 268 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 269 | |
| 270 | /* Get the limit (rounded up) */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 271 | limit = base + ((resource->size + gran - 1UL) & ~(gran - 1UL)) -1UL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 272 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 273 | /* Now store the resource */ |
| 274 | resource->flags |= IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 275 | if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) { |
| 276 | /* |
| 277 | * some chipsets allow us to set/clear the IO bit. |
| 278 | * (e.g. VIA 82c686a.) So set it to be safe) |
| 279 | */ |
| 280 | limit = base + resource->size -1; |
| 281 | if (resource->flags & IORESOURCE_IO) { |
| 282 | base |= PCI_BASE_ADDRESS_SPACE_IO; |
| 283 | } |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 284 | pci_write_config32(dev, resource->index, base & 0xffffffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 285 | if (resource->flags & IORESOURCE_PCI64) { |
| 286 | /* FIXME handle real 64bit base addresses */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 287 | pci_write_config32(dev, resource->index + 4, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | else if (resource->index == PCI_IO_BASE) { |
| 291 | /* set the IO ranges |
| 292 | * WARNING: we don't really do 32-bit addressing for IO yet! |
| 293 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 294 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 295 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 296 | pci_write_config8(dev, PCI_IO_BASE, base >> 8); |
| 297 | pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8); |
Eric Biederman | 5fb929e | 2003-07-17 02:15:46 +0000 | [diff] [blame] | 298 | pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0); |
| 299 | pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 300 | } |
| 301 | else if (resource->index == PCI_MEMORY_BASE) { |
| 302 | /* set the memory range |
| 303 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 304 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 305 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 306 | IORESOURCE_MEM); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 307 | pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16); |
| 308 | pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 309 | } |
| 310 | else if (resource->index == PCI_PREF_MEMORY_BASE) { |
| 311 | /* set the prefetchable memory range |
| 312 | * WARNING: we don't really do 64-bit addressing for prefetchable memory yet! |
| 313 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 314 | compute_allocate_resource(&dev->link[0], resource, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 315 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 316 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 317 | pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16); |
| 318 | pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16); |
Eric Biederman | 5fb929e | 2003-07-17 02:15:46 +0000 | [diff] [blame] | 319 | pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0); |
| 320 | pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 321 | } |
| 322 | else { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 323 | /* Don't let me think I stored the resource */ |
| 324 | resource->flags &= ~IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 325 | printk_err("ERROR: invalid resource->index %x\n", |
| 326 | resource->index); |
| 327 | } |
| 328 | buf[0] = '\0'; |
| 329 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 330 | sprintf(buf, "bus %d ", dev->link[0].secondary); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | printk_debug( |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 334 | "%s %02x <- [0x%08lx - 0x%08lx] %s%s\n", |
| 335 | dev_path(dev), |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 336 | resource->index, |
| 337 | resource->base, limit, |
| 338 | buf, |
| 339 | (resource->flags & IORESOURCE_IO)? "io": |
| 340 | (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem"); |
| 341 | return; |
| 342 | } |
| 343 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 344 | void pci_dev_set_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 345 | { |
| 346 | struct resource *resource, *last; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 347 | unsigned link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 348 | uint8_t line; |
| 349 | |
| 350 | last = &dev->resource[dev->resources]; |
| 351 | |
| 352 | for(resource = &dev->resource[0]; resource < last; resource++) { |
| 353 | pci_set_resource(dev, resource); |
| 354 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 355 | for(link = 0; link < dev->links; link++) { |
| 356 | struct bus *bus; |
| 357 | bus = &dev->link[link]; |
| 358 | if (bus->children) { |
| 359 | assign_resources(bus); |
| 360 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* set a default latency timer */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 364 | pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 365 | |
| 366 | /* set a default secondary latency timer */ |
| 367 | if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 368 | pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* zero the irq settings */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 372 | line = pci_read_config8(dev, PCI_INTERRUPT_PIN); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 373 | if (line) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 374 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 375 | } |
| 376 | /* 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] | 377 | pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 380 | void pci_dev_enable_resources(struct device *dev) |
| 381 | { |
| 382 | uint16_t command; |
| 383 | command = pci_read_config16(dev, PCI_COMMAND); |
| 384 | command |= dev->command; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 385 | command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 386 | printk_debug("%s cmd <- %02x\n", dev_path(dev), command); |
| 387 | pci_write_config16(dev, PCI_COMMAND, command); |
| 388 | |
| 389 | enable_childrens_resources(dev); |
| 390 | } |
| 391 | |
| 392 | void pci_bus_enable_resources(struct device *dev) |
| 393 | { |
| 394 | uint16_t ctrl; |
| 395 | ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL); |
| 396 | ctrl |= dev->link[0].bridge_ctrl; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 397 | ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 398 | printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl); |
| 399 | pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl); |
| 400 | |
| 401 | pci_dev_enable_resources(dev); |
| 402 | } |
| 403 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 404 | struct device_operations default_pci_ops_dev = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 405 | .read_resources = pci_dev_read_resources, |
| 406 | .set_resources = pci_dev_set_resources, |
| 407 | .enable_resources = pci_dev_enable_resources, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 408 | .init = 0, |
| 409 | .scan_bus = 0, |
| 410 | }; |
| 411 | struct device_operations default_pci_ops_bus = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 412 | .read_resources = pci_bus_read_resources, |
| 413 | .set_resources = pci_dev_set_resources, |
| 414 | .enable_resources = pci_bus_enable_resources, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 415 | .init = 0, |
| 416 | .scan_bus = pci_scan_bridge, |
| 417 | }; |
| 418 | static void set_pci_ops(struct device *dev) |
| 419 | { |
| 420 | struct pci_driver *driver; |
| 421 | if (dev->ops) { |
| 422 | return; |
| 423 | } |
| 424 | /* Look through the list of setup drivers and find one for |
| 425 | * this pci device |
| 426 | */ |
| 427 | for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) { |
| 428 | if ((driver->vendor == dev->vendor) && |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 429 | (driver->device == dev->device)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 430 | dev->ops = driver->ops; |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 431 | #if 1 |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 432 | printk_debug("%s [%04x/%04x] %sops\n", |
| 433 | dev_path(dev), |
| 434 | driver->vendor, driver->device, |
| 435 | (driver->ops->scan_bus?"bus ":"") |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 436 | ); |
| 437 | #endif |
| 438 | return; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | /* If I don't have a specific driver use the default operations */ |
| 442 | switch(dev->hdr_type & 0x7f) { /* header type */ |
| 443 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ |
| 444 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) |
| 445 | goto bad; |
| 446 | dev->ops = &default_pci_ops_dev; |
| 447 | break; |
| 448 | case PCI_HEADER_TYPE_BRIDGE: |
| 449 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 450 | goto bad; |
| 451 | dev->ops = &default_pci_ops_bus; |
| 452 | break; |
| 453 | default: |
| 454 | bad: |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 455 | if (dev->enable) { |
| 456 | printk_err("%s [%04x/%04x/%06x] has unknown header " |
| 457 | "type %02x, ignoring.\n", |
| 458 | dev_path(dev), |
| 459 | dev->vendor, dev->device, |
| 460 | dev->class >> 8, dev->hdr_type); |
| 461 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 462 | } |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Given a bus and a devfn number, find the device structure |
| 468 | * @param bus The bus structure |
| 469 | * @param devfn a device/function number |
| 470 | * @return pointer to the device structure |
| 471 | */ |
| 472 | static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn) |
| 473 | { |
| 474 | struct device *dev = 0; |
| 475 | for(; *list; list = &(*list)->sibling) { |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 476 | if ((*list)->path.type != DEVICE_PATH_PCI) { |
| 477 | printk_err("child %s not a pci device\n", dev_path(*list)); |
| 478 | continue; |
| 479 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 480 | if ((*list)->path.u.pci.devfn == devfn) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 481 | /* Unlink from the list */ |
| 482 | dev = *list; |
| 483 | *list = (*list)->sibling; |
| 484 | dev->sibling = 0; |
| 485 | break; |
| 486 | } |
| 487 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 488 | if (dev) { |
| 489 | device_t child; |
| 490 | /* Find the last child of our parent */ |
| 491 | for(child = dev->bus->children; child && child->sibling; ) { |
| 492 | child = child->sibling; |
| 493 | } |
| 494 | /* Place the device on the list of children of it's parent. */ |
| 495 | if (child) { |
| 496 | child->sibling = dev; |
| 497 | } else { |
| 498 | dev->bus->children = dev; |
| 499 | } |
| 500 | } |
| 501 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 502 | return dev; |
| 503 | } |
| 504 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 505 | /** Scan the pci bus devices and bridges. |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 506 | * @param bus pointer to the bus structure |
| 507 | * @param min_devfn minimum devfn to look at in the scan usually 0x00 |
| 508 | * @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] | 509 | * @param max current bus number |
| 510 | * @return The maximum bus number found, after scanning all subordinate busses |
| 511 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 512 | unsigned int pci_scan_bus(struct bus *bus, |
| 513 | unsigned min_devfn, unsigned max_devfn, |
| 514 | unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 515 | { |
| 516 | unsigned int devfn; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 517 | device_t dev; |
| 518 | device_t old_devices; |
| 519 | device_t child; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 520 | |
| 521 | printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary); |
| 522 | |
| 523 | old_devices = bus->children; |
| 524 | bus->children = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 525 | |
| 526 | post_code(0x24); |
| 527 | |
| 528 | |
| 529 | /* probe all devices on this bus with some optimization for non-existance and |
| 530 | single funcion devices */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 531 | for (devfn = min_devfn; devfn <= max_devfn; devfn++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 532 | uint32_t id, class; |
Eric Biederman | 30e143a | 2003-09-01 23:45:32 +0000 | [diff] [blame] | 533 | uint8_t hdr_type; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 534 | |
| 535 | /* First thing setup the device structure */ |
| 536 | dev = pci_scan_get_dev(&old_devices, devfn); |
| 537 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 538 | /* Detect if a device is present */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 539 | if (!dev) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 540 | struct device dummy; |
| 541 | dummy.bus = bus; |
| 542 | dummy.path.type = DEVICE_PATH_PCI; |
| 543 | dummy.path.u.pci.devfn = devfn; |
| 544 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
| 545 | /* some broken boards return 0 if a slot is empty: */ |
| 546 | if ( (id == 0xffffffff) || (id == 0x00000000) || |
| 547 | (id == 0x0000ffff) || (id == 0xffff0000)) |
| 548 | { |
| 549 | printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id); |
| 550 | if (PCI_FUNC(devfn) == 0x00) { |
| 551 | /* if this is a function 0 device and it is not present, |
| 552 | skip to next device */ |
| 553 | devfn += 0x07; |
| 554 | } |
| 555 | /* multi function device, skip to next function */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 556 | continue; |
| 557 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 558 | dev = alloc_dev(bus, &dummy.path); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 559 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 560 | else { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 561 | /* Run the magic enable sequence for the device */ |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 562 | if (dev->chip && dev->chip->control && dev->chip->control->enable_dev) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 563 | int enable = dev->enable; |
| 564 | dev->enable = 1; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 565 | dev->chip->control->enable_dev(dev); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 566 | dev->enable = enable; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 567 | } |
| 568 | /* Now read the vendor and device id */ |
| 569 | id = pci_read_config32(dev, PCI_VENDOR_ID); |
| 570 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 571 | /* Read the rest of the pci configuration information */ |
| 572 | hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 573 | class = pci_read_config32(dev, PCI_CLASS_REVISION); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 574 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 575 | /* Store the interesting information in the device structure */ |
| 576 | dev->vendor = id & 0xffff; |
| 577 | dev->device = (id >> 16) & 0xffff; |
| 578 | dev->hdr_type = hdr_type; |
| 579 | /* class code, the upper 3 bytes of PCI_CLASS_REVISION */ |
| 580 | dev->class = class >> 8; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 581 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 582 | /* Look at the vendor and device id, or at least the |
| 583 | * header type and class and figure out which set of configuration |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 584 | * methods to use. Unless we already have some pci ops. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 585 | */ |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 586 | set_pci_ops(dev); |
| 587 | /* Error if we don't have some pci operations for it */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 588 | if (!dev->ops) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 589 | printk_err("%s No device operations\n", |
| 590 | dev_path(dev)); |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | /* Now run the magic enable/disable sequence for the device */ |
| 595 | if (dev->ops && dev->ops->enable) { |
| 596 | dev->ops->enable(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 597 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame^] | 598 | else if (dev->chip && dev->chip->control && dev->chip->control->enable_dev) { |
| 599 | dev->chip->control->enable_dev(dev); |
| 600 | } |
Eric Biederman | 4086d16 | 2003-07-17 03:26:03 +0000 | [diff] [blame] | 601 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 602 | printk_debug("%s [%04x/%04x] %s\n", |
| 603 | dev_path(dev), |
Eric Biederman | 4086d16 | 2003-07-17 03:26:03 +0000 | [diff] [blame] | 604 | dev->vendor, dev->device, |
| 605 | dev->enable?"enabled": "disabled"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 606 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 607 | if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) { |
| 608 | /* if this is not a multi function device, don't waste time probe |
| 609 | another function. Skip to next device. */ |
| 610 | devfn += 0x07; |
| 611 | } |
| 612 | } |
| 613 | post_code(0x25); |
| 614 | |
| 615 | for(child = bus->children; child; child = child->sibling) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 616 | if (!child->ops->scan_bus) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 617 | continue; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 618 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 619 | max = child->ops->scan_bus(child, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 620 | } |
| 621 | /* |
| 622 | * We've scanned the bus and so we know all about what's on |
| 623 | * the other side of any bridges that may be on this bus plus |
| 624 | * any devices. |
| 625 | * |
| 626 | * Return how far we've got finding sub-buses. |
| 627 | */ |
| 628 | printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max); |
| 629 | post_code(0x55); |
| 630 | return max; |
| 631 | } |
| 632 | |
| 633 | /** Scan the bus, first for bridges and next for devices. |
| 634 | * @param pci_bus pointer to the bus structure |
| 635 | * @return The maximum bus number found, after scanning all subordinate busses |
| 636 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 637 | unsigned int pci_scan_bridge(struct device *dev, unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 638 | { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 639 | struct bus *bus; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 640 | uint32_t buses; |
| 641 | uint16_t cr; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 642 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 643 | bus = &dev->link[0]; |
| 644 | dev->links = 1; |
| 645 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 646 | /* Set up the primary, secondary and subordinate bus numbers. We have |
| 647 | * no idea how many buses are behind this bridge yet, so we set the |
| 648 | * subordinate bus number to 0xff for the moment |
| 649 | */ |
| 650 | bus->secondary = ++max; |
| 651 | bus->subordinate = 0xff; |
| 652 | |
| 653 | /* 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] | 654 | cr = pci_read_config16(dev, PCI_COMMAND); |
| 655 | pci_write_config16(dev, PCI_COMMAND, 0x0000); |
| 656 | pci_write_config16(dev, PCI_STATUS, 0xffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 657 | |
| 658 | /* |
| 659 | * Read the existing primary/secondary/subordinate bus |
| 660 | * number configuration. |
| 661 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 662 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 663 | |
| 664 | /* Configure the bus numbers for this bridge: the configuration |
| 665 | * transactions will not be propagated by the bridge if it is not |
| 666 | * correctly configured |
| 667 | */ |
| 668 | buses &= 0xff000000; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 669 | buses |= (((unsigned int) (dev->bus->secondary) << 0) | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 670 | ((unsigned int) (bus->secondary) << 8) | |
| 671 | ((unsigned int) (bus->subordinate) << 16)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 672 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 673 | |
| 674 | /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 675 | max = pci_scan_bus(bus, 0x00, 0xff, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 676 | |
| 677 | /* We know the number of buses behind this bridge. Set the subordinate |
| 678 | * bus number to its real value |
| 679 | */ |
| 680 | bus->subordinate = max; |
| 681 | buses = (buses & 0xff00ffff) | |
| 682 | ((unsigned int) (bus->subordinate) << 16); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 683 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
| 684 | pci_write_config16(dev, PCI_COMMAND, cr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 685 | |
Ronald G. Minnich | 99dcf23 | 2003-09-30 02:16:47 +0000 | [diff] [blame] | 686 | printk_spew("%s returns max %d\n", __FUNCTION__, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 687 | return max; |
| 688 | } |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 689 | /* |
| 690 | Tell the EISA int controller this int must be level triggered |
| 691 | THIS IS A KLUDGE -- sorry, this needs to get cleaned up. |
| 692 | */ |
| 693 | static void pci_level_irq(unsigned char intNum) |
| 694 | { |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 695 | unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 696 | |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 697 | printk_spew("%s: current ints are 0x%x\n", __FUNCTION__, intBits); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 698 | intBits |= (1 << intNum); |
| 699 | |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 700 | printk_spew("%s: try to set ints 0x%x\n", __FUNCTION__, intBits); |
| 701 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 702 | // Write new values |
| 703 | outb((unsigned char) intBits, 0x4d0); |
| 704 | outb((unsigned char) (intBits >> 8), 0x4d1); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 705 | |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 706 | /* this seems like an error but is not ... */ |
| 707 | #if 0 |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 708 | if (inb(0x4d0) != (intBits & 0xf)) { |
| 709 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
| 710 | __FUNCTION__, intBits &0xf, inb(0x4d0)); |
| 711 | } |
| 712 | if (inb(0x4d1) != ((intBits >> 8) & 0xf)) { |
| 713 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
| 714 | __FUNCTION__, (intBits>>8) &0xf, inb(0x4d1)); |
| 715 | } |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 716 | #endif |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 719 | /* |
| 720 | This function assigns IRQs for all functions contained within |
| 721 | the indicated device address. If the device does not exist or does |
| 722 | not require interrupts then this function has no effect. |
| 723 | |
| 724 | This function should be called for each PCI slot in your system. |
| 725 | |
| 726 | pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of |
| 727 | this slot. |
| 728 | The particular irq #s that are passed in depend on the routing inside |
| 729 | your southbridge and on your motherboard. |
| 730 | |
| 731 | -kevinh@ispiri.com |
| 732 | */ |
| 733 | void pci_assign_irqs(unsigned bus, unsigned slot, |
| 734 | const unsigned char pIntAtoD[4]) |
| 735 | { |
| 736 | unsigned functNum; |
| 737 | device_t pdev; |
| 738 | unsigned char line; |
| 739 | unsigned char irq; |
| 740 | unsigned char readback; |
| 741 | |
| 742 | /* Each slot may contain up to eight functions */ |
| 743 | for (functNum = 0; functNum < 8; functNum++) { |
| 744 | pdev = dev_find_slot(bus, (slot << 3) + functNum); |
| 745 | |
| 746 | if (pdev) { |
| 747 | line = pci_read_config8(pdev, PCI_INTERRUPT_PIN); |
| 748 | |
| 749 | // PCI spec says all other values are reserved |
| 750 | if ((line >= 1) && (line <= 4)) { |
| 751 | irq = pIntAtoD[line - 1]; |
| 752 | |
| 753 | printk_debug("Assigning IRQ %d to %d:%x.%d\n", \ |
| 754 | irq, bus, slot, functNum); |
| 755 | |
| 756 | pci_write_config8(pdev, PCI_INTERRUPT_LINE,\ |
| 757 | pIntAtoD[line - 1]); |
| 758 | |
| 759 | readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE); |
| 760 | printk_debug(" Readback = %d\n", readback); |
| 761 | |
| 762 | // Change to level triggered |
| 763 | pci_level_irq(pIntAtoD[line - 1]); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |