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 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +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 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 162 | static void pci_bridge_read_bases(struct device *dev) |
| 163 | { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 164 | struct resource *resource; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 165 | |
| 166 | /* FIXME handle bridges without some of the optional resources */ |
| 167 | |
| 168 | /* Initialize the io space constraints on the current bus */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 169 | resource = get_resource(dev, PCI_IO_BASE); |
| 170 | resource->size = 0; |
| 171 | resource->align = log2(PCI_IO_BRIDGE_ALIGN); |
| 172 | resource->gran = log2(PCI_IO_BRIDGE_ALIGN); |
| 173 | resource->limit = 0xffffUL; |
| 174 | resource->flags |= IORESOURCE_IO | IORESOURCE_PCI_BRIDGE; |
| 175 | compute_allocate_resource(&dev->link[0], resource, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 176 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 177 | |
| 178 | /* Initiliaze the prefetchable memory constraints on the current bus */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 179 | resource = get_resource(dev, PCI_PREF_MEMORY_BASE); |
| 180 | resource->size = 0; |
| 181 | resource->align = log2(PCI_MEM_BRIDGE_ALIGN); |
| 182 | resource->gran = log2(PCI_MEM_BRIDGE_ALIGN); |
| 183 | resource->limit = 0xffffffffUL; |
| 184 | resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE; |
| 185 | resource->index = PCI_PREF_MEMORY_BASE; |
| 186 | compute_allocate_resource(&dev->link[0], resource, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 187 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 188 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 189 | |
| 190 | /* Initialize the memory resources on the current bus */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 191 | resource = get_resource(dev, PCI_MEMORY_BASE); |
| 192 | resource->size = 0; |
| 193 | resource->align = log2(PCI_MEM_BRIDGE_ALIGN); |
| 194 | resource->gran = log2(PCI_MEM_BRIDGE_ALIGN); |
| 195 | resource->limit = 0xffffffffUL; |
| 196 | resource->flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE; |
| 197 | compute_allocate_resource(&dev->link[0], resource, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 198 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 199 | IORESOURCE_MEM); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 200 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 201 | compact_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 204 | void pci_dev_read_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 205 | { |
| 206 | uint32_t addr; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 207 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 208 | pci_read_bases(dev, 6); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 209 | |
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; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 217 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 218 | pci_bridge_read_bases(dev); |
| 219 | pci_read_bases(dev, 2); |
| 220 | |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 221 | addr = pci_read_config32(dev, PCI_ROM_ADDRESS1); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 222 | dev->rom_address = (addr == 0xffffffff)? 0 : addr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 225 | /** |
| 226 | * @brief round a number up to an alignment. |
| 227 | * @param val the starting value |
| 228 | * @param roundup Alignment as a power of two |
| 229 | * @returns rounded up number |
| 230 | */ |
| 231 | static unsigned long round(unsigned long val, unsigned long roundup) |
| 232 | { |
| 233 | /* ROUNDUP MUST BE A POWER OF TWO. */ |
| 234 | unsigned long inverse; |
| 235 | inverse = ~(roundup - 1); |
| 236 | val += (roundup - 1); |
| 237 | val &= inverse; |
| 238 | return val; |
| 239 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 240 | |
| 241 | static void pci_set_resource(struct device *dev, struct resource *resource) |
| 242 | { |
| 243 | unsigned long base, limit; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 244 | unsigned char buf[10]; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 245 | unsigned long gran; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 246 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 247 | /* Make certain the resource has actually been set */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 248 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 249 | if (!(resource->flags & IORESOURCE_ASSIGNED)) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 250 | printk_err("ERROR: %s %02x not allocated\n", |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 251 | dev_path(dev), resource->index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 255 | /* If I have already stored this resource don't worry about it */ |
| 256 | if (resource->flags & IORESOURCE_STORED) { |
| 257 | return; |
| 258 | } |
| 259 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 260 | /* Only handle PCI memory and IO resources for now */ |
| 261 | if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO))) |
| 262 | return; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 263 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 264 | if (resource->flags & IORESOURCE_MEM) { |
| 265 | dev->command |= PCI_COMMAND_MEMORY; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 266 | } |
| 267 | if (resource->flags & IORESOURCE_IO) { |
| 268 | dev->command |= PCI_COMMAND_IO; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 269 | } |
| 270 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
| 271 | dev->command |= PCI_COMMAND_MASTER; |
| 272 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 273 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 274 | /* Get the base address */ |
| 275 | base = resource->base; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 276 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 277 | /* Get the resource granularity */ |
| 278 | gran = 1UL << resource->gran; |
| 279 | |
| 280 | /* For a non bridge resource granularity and alignment are the same. |
| 281 | * For a bridge resource align is the largest needed alignment below |
| 282 | * the bridge. While the granularity is simply how many low bits of the |
| 283 | * address cannot be set. |
| 284 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 285 | |
| 286 | /* Get the limit (rounded up) */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 287 | limit = base + round(resource->size, gran) - 1UL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 288 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 289 | /* Now store the resource */ |
| 290 | resource->flags |= IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 291 | if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 292 | /* some chipsets allow us to set/clear the IO bit. |
| 293 | * (e.g. VIA 82c686a.) So set it to be safe) */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 294 | limit = base + resource->size -1; |
| 295 | if (resource->flags & IORESOURCE_IO) { |
| 296 | base |= PCI_BASE_ADDRESS_SPACE_IO; |
| 297 | } |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 298 | pci_write_config32(dev, resource->index, base & 0xffffffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 299 | if (resource->flags & IORESOURCE_PCI64) { |
| 300 | /* FIXME handle real 64bit base addresses */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 301 | pci_write_config32(dev, resource->index + 4, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 302 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 303 | } else if (resource->index == PCI_IO_BASE) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 304 | /* set the IO ranges |
| 305 | * WARNING: we don't really do 32-bit addressing for IO yet! |
| 306 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 307 | compute_allocate_resource(&dev->link[0], resource, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 308 | IORESOURCE_IO, IORESOURCE_IO); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 309 | pci_write_config8(dev, PCI_IO_BASE, base >> 8); |
| 310 | pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8); |
Eric Biederman | 5fb929e | 2003-07-17 02:15:46 +0000 | [diff] [blame] | 311 | pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0); |
| 312 | pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 313 | } else if (resource->index == PCI_MEMORY_BASE) { |
| 314 | /* set the memory range */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 315 | compute_allocate_resource(&dev->link[0], resource, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 316 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 317 | IORESOURCE_MEM); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 318 | pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16); |
| 319 | pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 320 | } else if (resource->index == PCI_PREF_MEMORY_BASE) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 321 | /* set the prefetchable memory range |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 322 | * WARNING: we don't really do 64-bit addressing for |
| 323 | * prefetchable memory yet! */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 324 | compute_allocate_resource(&dev->link[0], resource, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 325 | IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 326 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 327 | pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16); |
| 328 | pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16); |
Eric Biederman | 5fb929e | 2003-07-17 02:15:46 +0000 | [diff] [blame] | 329 | pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0); |
| 330 | pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 331 | } else { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 332 | /* Don't let me think I stored the resource */ |
| 333 | resource->flags &= ~IORESOURCE_STORED; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 334 | printk_err("ERROR: invalid resource->index %x\n", |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 335 | resource->index); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 336 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 337 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 338 | buf[0] = '\0'; |
| 339 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 340 | sprintf(buf, "bus %d ", dev->link[0].secondary); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 341 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 342 | printk_debug("%s %02x <- [0x%08lx - 0x%08lx] %s%s\n", |
| 343 | dev_path(dev), resource->index, resource->base, |
| 344 | limit, buf, |
| 345 | (resource->flags & IORESOURCE_IO)? "io": |
| 346 | (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 347 | return; |
| 348 | } |
| 349 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 350 | void pci_dev_set_resources(struct device *dev) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 351 | { |
| 352 | struct resource *resource, *last; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 353 | unsigned link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 354 | uint8_t line; |
| 355 | |
| 356 | last = &dev->resource[dev->resources]; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 357 | for (resource = &dev->resource[0]; resource < last; resource++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 358 | pci_set_resource(dev, resource); |
| 359 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 360 | |
| 361 | for (link = 0; link < dev->links; link++) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 362 | struct bus *bus; |
| 363 | bus = &dev->link[link]; |
| 364 | if (bus->children) { |
| 365 | assign_resources(bus); |
| 366 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* set a default latency timer */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 370 | pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 371 | |
| 372 | /* set a default secondary latency timer */ |
| 373 | if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 374 | pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* zero the irq settings */ |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 378 | line = pci_read_config8(dev, PCI_INTERRUPT_PIN); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 379 | if (line) { |
Eric Biederman | 7a5416a | 2003-06-12 19:23:51 +0000 | [diff] [blame] | 380 | pci_write_config8(dev, PCI_INTERRUPT_LINE, 0); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 381 | } |
| 382 | /* 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] | 383 | pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 386 | void pci_dev_enable_resources(struct device *dev) |
| 387 | { |
| 388 | uint16_t command; |
| 389 | command = pci_read_config16(dev, PCI_COMMAND); |
| 390 | command |= dev->command; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 391 | command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 392 | printk_debug("%s cmd <- %02x\n", dev_path(dev), command); |
| 393 | pci_write_config16(dev, PCI_COMMAND, command); |
| 394 | |
| 395 | enable_childrens_resources(dev); |
| 396 | } |
| 397 | |
| 398 | void pci_bus_enable_resources(struct device *dev) |
| 399 | { |
| 400 | uint16_t ctrl; |
| 401 | ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL); |
| 402 | ctrl |= dev->link[0].bridge_ctrl; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 403 | ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 404 | printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl); |
| 405 | pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl); |
| 406 | |
| 407 | pci_dev_enable_resources(dev); |
| 408 | } |
| 409 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 410 | /** Default device operation for PCI devices */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 411 | struct device_operations default_pci_ops_dev = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 412 | .read_resources = pci_dev_read_resources, |
| 413 | .set_resources = pci_dev_set_resources, |
| 414 | .enable_resources = pci_dev_enable_resources, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 415 | .init = 0, |
| 416 | .scan_bus = 0, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 417 | }; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 418 | |
| 419 | /** Default device operations for PCI bridges */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 420 | struct device_operations default_pci_ops_bus = { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 421 | .read_resources = pci_bus_read_resources, |
| 422 | .set_resources = pci_dev_set_resources, |
| 423 | .enable_resources = pci_bus_enable_resources, |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 424 | .init = 0, |
| 425 | .scan_bus = pci_scan_bridge, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 426 | }; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * @brief Set up PCI device operation |
| 430 | * |
| 431 | * |
| 432 | * @param dev |
| 433 | * |
| 434 | * @see pci_drivers |
| 435 | */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 436 | static void set_pci_ops(struct device *dev) |
| 437 | { |
| 438 | struct pci_driver *driver; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 439 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 440 | if (dev->ops) { |
| 441 | return; |
| 442 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 443 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 444 | /* Look through the list of setup drivers and find one for |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 445 | * this pci device */ |
| 446 | for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 447 | if ((driver->vendor == dev->vendor) && |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 448 | (driver->device == dev->device)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 449 | dev->ops = driver->ops; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 450 | |
| 451 | printk_debug("%s [%04x/%04x] %sops\n", dev_path(dev), |
| 452 | driver->vendor, driver->device, |
| 453 | (driver->ops->scan_bus?"bus ":"")); |
| 454 | |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 455 | return; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 458 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 459 | /* If I don't have a specific driver use the default operations */ |
| 460 | switch(dev->hdr_type & 0x7f) { /* header type */ |
| 461 | case PCI_HEADER_TYPE_NORMAL: /* standard header */ |
| 462 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) |
| 463 | goto bad; |
| 464 | dev->ops = &default_pci_ops_dev; |
| 465 | break; |
| 466 | case PCI_HEADER_TYPE_BRIDGE: |
| 467 | if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI) |
| 468 | goto bad; |
| 469 | dev->ops = &default_pci_ops_bus; |
| 470 | break; |
| 471 | default: |
| 472 | bad: |
Li-Ta Lo | 69c5a90 | 2004-04-29 20:08:54 +0000 | [diff] [blame] | 473 | if (dev->enabled) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 474 | printk_err("%s [%04x/%04x/%06x] has unknown header " |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 475 | "type %02x, ignoring.\n", |
| 476 | dev_path(dev), |
| 477 | dev->vendor, dev->device, |
| 478 | dev->class >> 8, dev->hdr_type); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 479 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 480 | } |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | /** |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 485 | * @brief Find a specific device structure on a list of device structures |
| 486 | * |
| 487 | * Given a linked list of PCI device structures and a devfn number, find the |
| 488 | * device structure correspond to the devfn. |
| 489 | * |
| 490 | * @param list the device structure list |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 491 | * @param devfn a device/function number |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 492 | * |
| 493 | * @return pointer to the device structure found |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 494 | */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 495 | static struct device *pci_scan_get_dev(struct device **list, |
| 496 | unsigned int devfn) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 497 | { |
| 498 | struct device *dev = 0; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 499 | |
| 500 | for (; *list; list = &(*list)->sibling) { |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 501 | if ((*list)->path.type != DEVICE_PATH_PCI) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 502 | printk_err("child %s not a pci device\n", |
| 503 | dev_path(*list)); |
Eric Biederman | ad1b35a | 2003-10-14 02:36:51 +0000 | [diff] [blame] | 504 | continue; |
| 505 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 506 | if ((*list)->path.u.pci.devfn == devfn) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 507 | /* Unlink from the list */ |
| 508 | dev = *list; |
| 509 | *list = (*list)->sibling; |
| 510 | dev->sibling = 0; |
| 511 | break; |
| 512 | } |
| 513 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 514 | |
| 515 | /* FIXME: why are we doing this ? Isn't there some order between the |
| 516 | * structures before ? */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 517 | if (dev) { |
| 518 | device_t child; |
| 519 | /* Find the last child of our parent */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 520 | for (child = dev->bus->children; child && child->sibling; ) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 521 | child = child->sibling; |
| 522 | } |
| 523 | /* Place the device on the list of children of it's parent. */ |
| 524 | if (child) { |
| 525 | child->sibling = dev; |
| 526 | } else { |
| 527 | dev->bus->children = dev; |
| 528 | } |
| 529 | } |
| 530 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 531 | return dev; |
| 532 | } |
| 533 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 534 | /** |
| 535 | * @brief Scan a PCI bus |
| 536 | * |
| 537 | * Determine the existence of devices and bridges on a PCI bus. If there are |
| 538 | * bridges on the bus, recursively scan the buses behind the bridges. |
| 539 | * |
| 540 | * This function is the default scan_bus() method for the root device |
| 541 | * 'dev_root'. |
| 542 | * |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 543 | * @param bus pointer to the bus structure |
| 544 | * @param min_devfn minimum devfn to look at in the scan usually 0x00 |
| 545 | * @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] | 546 | * @param max current bus number |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 547 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 548 | * @return The maximum bus number found, after scanning all subordinate busses |
| 549 | */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 550 | unsigned int pci_scan_bus(struct bus *bus, unsigned min_devfn, |
| 551 | unsigned max_devfn, unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 552 | { |
| 553 | unsigned int devfn; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 554 | device_t dev; |
| 555 | device_t old_devices; |
| 556 | device_t child; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 557 | |
| 558 | printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary); |
| 559 | |
| 560 | old_devices = bus->children; |
| 561 | bus->children = 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 562 | |
| 563 | post_code(0x24); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 564 | |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame^] | 565 | /* probe all devices/functions on this bus with some optimization for |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 566 | * non-existence and single funcion devices */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 567 | for (devfn = min_devfn; devfn <= max_devfn; devfn++) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 568 | uint32_t id, class; |
Eric Biederman | 30e143a | 2003-09-01 23:45:32 +0000 | [diff] [blame] | 569 | uint8_t hdr_type; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 570 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 571 | /* device structures for PCI devices associated with static |
| 572 | * devices are already created during the static device |
| 573 | * enumeration, find out if it is the case for this devfn */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 574 | dev = pci_scan_get_dev(&old_devices, devfn); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 575 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 576 | if (!dev) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 577 | /* it's not associated with a static device, detect if |
| 578 | * this device is present */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 579 | struct device dummy; |
| 580 | dummy.bus = bus; |
| 581 | dummy.path.type = DEVICE_PATH_PCI; |
| 582 | dummy.path.u.pci.devfn = devfn; |
| 583 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
| 584 | /* some broken boards return 0 if a slot is empty: */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 585 | if ((id == 0xffffffff) || (id == 0x00000000) || |
| 586 | (id == 0x0000ffff) || (id == 0xffff0000)) { |
| 587 | printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", |
| 588 | devfn, id); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 589 | if (PCI_FUNC(devfn) == 0x00) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 590 | /* if this is a function 0 device and |
| 591 | * it is not present, skip to next |
| 592 | * device */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 593 | devfn += 0x07; |
| 594 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 595 | /* this function in a multi function device is |
| 596 | * not present, skip to next function */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 597 | continue; |
| 598 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 599 | dev = alloc_dev(bus, &dummy.path); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 600 | } else { |
| 601 | /* Run the magic enable/disable sequence for the |
| 602 | * device */ |
| 603 | /* FIXME: What happen if this PCI device listed as |
| 604 | * static device but does not exist ? This calls |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame^] | 605 | * some arbitray code without any justification |
| 606 | * Also, it calls the enable function regardlessly |
| 607 | * the value of dev->enabled */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 608 | if (dev->chip && dev->chip->control && |
| 609 | dev->chip->control->enable_dev) { |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame^] | 610 | int enabled = dev->enabled; |
Li-Ta Lo | 69c5a90 | 2004-04-29 20:08:54 +0000 | [diff] [blame] | 611 | dev->enabled = 1; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 612 | dev->chip->control->enable_dev(dev); |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame^] | 613 | dev->enabled = enabled; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 614 | } |
| 615 | /* Now read the vendor and device id */ |
| 616 | id = pci_read_config32(dev, PCI_VENDOR_ID); |
| 617 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 618 | /* Read the rest of the pci configuration information */ |
| 619 | hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE); |
| 620 | class = pci_read_config32(dev, PCI_CLASS_REVISION); |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame^] | 621 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 622 | /* Store the interesting information in the device structure */ |
| 623 | dev->vendor = id & 0xffff; |
| 624 | dev->device = (id >> 16) & 0xffff; |
| 625 | dev->hdr_type = hdr_type; |
| 626 | /* class code, the upper 3 bytes of PCI_CLASS_REVISION */ |
| 627 | dev->class = class >> 8; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 628 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 629 | /* Look at the vendor and device id, or at least the |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 630 | * header type and class and figure out which set of |
| 631 | * configuration methods to use. Unless we already |
| 632 | * have some pci ops. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 633 | */ |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 634 | set_pci_ops(dev); |
| 635 | /* Error if we don't have some pci operations for it */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 636 | if (!dev->ops) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 637 | printk_err("%s No device operations\n", |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 638 | dev_path(dev)); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 639 | continue; |
| 640 | } |
| 641 | |
| 642 | /* Now run the magic enable/disable sequence for the device */ |
| 643 | if (dev->ops && dev->ops->enable) { |
| 644 | dev->ops->enable(dev); |
Li-Ta Lo | 9782f75 | 2004-05-05 21:15:42 +0000 | [diff] [blame^] | 645 | } else if (dev->chip && dev->chip->control && |
| 646 | dev->chip->control->enable_dev) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 647 | dev->chip->control->enable_dev(dev); |
| 648 | } |
Eric Biederman | 4086d16 | 2003-07-17 03:26:03 +0000 | [diff] [blame] | 649 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 650 | printk_debug("%s [%04x/%04x] %s\n", |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 651 | dev_path(dev), |
| 652 | dev->vendor, dev->device, |
Li-Ta Lo | 69c5a90 | 2004-04-29 20:08:54 +0000 | [diff] [blame] | 653 | dev->enabled?"enabled": "disabled"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 654 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 655 | if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) { |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 656 | /* if this is not a multi function device, don't |
| 657 | * waste time probe another function. |
| 658 | * Skip to next device. */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 659 | devfn += 0x07; |
| 660 | } |
| 661 | } |
| 662 | post_code(0x25); |
| 663 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 664 | /* if the child provides scan_bus(), for example a bridge, scan the |
| 665 | * bus behind that child */ |
| 666 | for (child = bus->children; child; child = child->sibling) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 667 | if (!child->ops->scan_bus) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 668 | continue; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 669 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 670 | max = child->ops->scan_bus(child, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 671 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 672 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 673 | /* |
| 674 | * We've scanned the bus and so we know all about what's on |
| 675 | * the other side of any bridges that may be on this bus plus |
| 676 | * any devices. |
| 677 | * |
| 678 | * Return how far we've got finding sub-buses. |
| 679 | */ |
| 680 | printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max); |
| 681 | post_code(0x55); |
| 682 | return max; |
| 683 | } |
| 684 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 685 | /** |
| 686 | * @brief Scan a PCI bridge and the buses behind the bridge. |
| 687 | * |
| 688 | * Determine the existence of buses behind the bridge. Set up the bridge |
| 689 | * according to the result of the scan. |
| 690 | * |
| 691 | * This function is the default scan_bus() method for PCI bridge devices. |
| 692 | * |
| 693 | * @param dev pointer to the bridge device |
| 694 | * @param max the highest bus number assgined up to now |
| 695 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 696 | * @return The maximum bus number found, after scanning all subordinate busses |
| 697 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 698 | unsigned int pci_scan_bridge(struct device *dev, unsigned int max) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 699 | { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 700 | struct bus *bus; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 701 | uint32_t buses; |
| 702 | uint16_t cr; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 703 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 704 | bus = &dev->link[0]; |
| 705 | dev->links = 1; |
| 706 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 707 | /* Set up the primary, secondary and subordinate bus numbers. We have |
| 708 | * no idea how many buses are behind this bridge yet, so we set the |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 709 | * subordinate bus number to 0xff for the moment. */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 710 | bus->secondary = ++max; |
| 711 | bus->subordinate = 0xff; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 712 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 713 | /* 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] | 714 | cr = pci_read_config16(dev, PCI_COMMAND); |
| 715 | pci_write_config16(dev, PCI_COMMAND, 0x0000); |
| 716 | pci_write_config16(dev, PCI_STATUS, 0xffff); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 717 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 718 | /* Read the existing primary/secondary/subordinate bus |
| 719 | * number configuration. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 720 | buses = pci_read_config32(dev, PCI_PRIMARY_BUS); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 721 | |
| 722 | /* Configure the bus numbers for this bridge: the configuration |
| 723 | * transactions will not be propagated by the bridge if it is not |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 724 | * correctly configured */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 725 | buses &= 0xff000000; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 726 | buses |= (((unsigned int) (dev->bus->secondary) << 0) | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 727 | ((unsigned int) (bus->secondary) << 8) | |
| 728 | ((unsigned int) (bus->subordinate) << 16)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 729 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 730 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 731 | /* Now we can scan all subordinate buses i.e. the buses behind the |
| 732 | * bridge */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 733 | max = pci_scan_bus(bus, 0x00, 0xff, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 734 | |
| 735 | /* We know the number of buses behind this bridge. Set the subordinate |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 736 | * bus number to its real value */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 737 | bus->subordinate = max; |
| 738 | buses = (buses & 0xff00ffff) | |
| 739 | ((unsigned int) (bus->subordinate) << 16); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 740 | pci_write_config32(dev, PCI_PRIMARY_BUS, buses); |
| 741 | pci_write_config16(dev, PCI_COMMAND, cr); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 742 | |
Ronald G. Minnich | 99dcf23 | 2003-09-30 02:16:47 +0000 | [diff] [blame] | 743 | printk_spew("%s returns max %d\n", __FUNCTION__, max); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 744 | return max; |
| 745 | } |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 746 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 747 | /* |
| 748 | Tell the EISA int controller this int must be level triggered |
| 749 | THIS IS A KLUDGE -- sorry, this needs to get cleaned up. |
| 750 | */ |
| 751 | static void pci_level_irq(unsigned char intNum) |
| 752 | { |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 753 | unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 754 | |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 755 | printk_spew("%s: current ints are 0x%x\n", __FUNCTION__, intBits); |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 756 | intBits |= (1 << intNum); |
| 757 | |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 758 | printk_spew("%s: try to set ints 0x%x\n", __FUNCTION__, intBits); |
| 759 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 760 | // Write new values |
| 761 | outb((unsigned char) intBits, 0x4d0); |
| 762 | outb((unsigned char) (intBits >> 8), 0x4d1); |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 763 | |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 764 | /* this seems like an error but is not ... */ |
| 765 | #if 0 |
Ronald G. Minnich | cb3f498 | 2003-10-02 18:16:07 +0000 | [diff] [blame] | 766 | if (inb(0x4d0) != (intBits & 0xf)) { |
| 767 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
| 768 | __FUNCTION__, intBits &0xf, inb(0x4d0)); |
| 769 | } |
| 770 | if (inb(0x4d1) != ((intBits >> 8) & 0xf)) { |
| 771 | printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n", |
| 772 | __FUNCTION__, (intBits>>8) &0xf, inb(0x4d1)); |
| 773 | } |
Ronald G. Minnich | b56ef07 | 2003-10-15 20:05:11 +0000 | [diff] [blame] | 774 | #endif |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Ronald G. Minnich | 6dd6c685 | 2003-10-02 00:08:42 +0000 | [diff] [blame] | 777 | /* |
| 778 | This function assigns IRQs for all functions contained within |
| 779 | the indicated device address. If the device does not exist or does |
| 780 | not require interrupts then this function has no effect. |
| 781 | |
| 782 | This function should be called for each PCI slot in your system. |
| 783 | |
| 784 | pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of |
| 785 | this slot. |
| 786 | The particular irq #s that are passed in depend on the routing inside |
| 787 | your southbridge and on your motherboard. |
| 788 | |
| 789 | -kevinh@ispiri.com |
| 790 | */ |
| 791 | void pci_assign_irqs(unsigned bus, unsigned slot, |
| 792 | const unsigned char pIntAtoD[4]) |
| 793 | { |
| 794 | unsigned functNum; |
| 795 | device_t pdev; |
| 796 | unsigned char line; |
| 797 | unsigned char irq; |
| 798 | unsigned char readback; |
| 799 | |
| 800 | /* Each slot may contain up to eight functions */ |
| 801 | for (functNum = 0; functNum < 8; functNum++) { |
| 802 | pdev = dev_find_slot(bus, (slot << 3) + functNum); |
| 803 | |
| 804 | if (pdev) { |
| 805 | line = pci_read_config8(pdev, PCI_INTERRUPT_PIN); |
| 806 | |
| 807 | // PCI spec says all other values are reserved |
| 808 | if ((line >= 1) && (line <= 4)) { |
| 809 | irq = pIntAtoD[line - 1]; |
| 810 | |
| 811 | printk_debug("Assigning IRQ %d to %d:%x.%d\n", \ |
| 812 | irq, bus, slot, functNum); |
| 813 | |
| 814 | pci_write_config8(pdev, PCI_INTERRUPT_LINE,\ |
| 815 | pIntAtoD[line - 1]); |
| 816 | |
| 817 | readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE); |
| 818 | printk_debug(" Readback = %d\n", readback); |
| 819 | |
| 820 | // Change to level triggered |
| 821 | pci_level_irq(pIntAtoD[line - 1]); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |