blob: a4789d22f44b0f6a19413dddd976ecd4a3b07cfa [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
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 Biederman8ca8d762003-04-22 19:02:15 +000016#include <string.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000017#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000020#include <part/hard_reset.h>
Eric Biederman30e143a2003-09-01 23:45:32 +000021#include <part/fallback_boot.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000022
Eric Biederman8ca8d762003-04-22 19:02:15 +000023/** Given a device and register, read the size of the BAR for that register.
24 * @param dev Pointer to the device structure
25 * @param resource Pointer to the resource structure
26 * @param index Address of the pci configuration register
27 */
28static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index)
29{
30 uint32_t addr, size, base;
31 unsigned long type;
32
33 /* Initialize the resources to nothing */
34 resource->base = 0;
35 resource->size = 0;
36 resource->align = 0;
37 resource->gran = 0;
38 resource->limit = 0;
39 resource->flags = 0;
40 resource->index = index;
41
Eric Biederman7a5416a2003-06-12 19:23:51 +000042 addr = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000043
44 /* FIXME: more consideration for 64-bit PCI devices,
45 * we currently detect their size but otherwise
46 * treat them as 32-bit resources
47 */
48 /* get the size */
Eric Biederman7a5416a2003-06-12 19:23:51 +000049 pci_write_config32(dev, index, ~0);
50 size = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000051
52 /* get the minimum value the bar can be set to */
Eric Biederman7a5416a2003-06-12 19:23:51 +000053 pci_write_config32(dev, index, 0);
54 base = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000055
56 /* restore addr */
Eric Biederman7a5416a2003-06-12 19:23:51 +000057 pci_write_config32(dev, index, addr);
Eric Biederman8ca8d762003-04-22 19:02:15 +000058
59 /*
60 * some broken hardware has read-only registers that do not
61 * really size correctly. You can tell this if addr == size
62 * Example: the acer m7229 has BARs 1-4 normally read-only.
63 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
64 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
65 * violation of the spec.
66 * We catch this case and ignore it by settting size and type to 0.
67 * This incidentally catches the common case where registers
68 * read back as 0 for both address and size.
69 */
70 if ((addr == size) && (addr == base)) {
71 if (size != 0) {
72 printk_debug(
Eric Biedermane9a271e32003-09-02 03:36:25 +000073 "%s register %02x(%08x), read-only ignoring it\n",
74 dev_path(dev),
Eric Biederman8ca8d762003-04-22 19:02:15 +000075 index, addr);
76 }
77 resource->flags = 0;
78 }
79 /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */
80 else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
81 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
82 /* BUG! Top 16 bits can be zero (or not)
83 * So set them to 0xffff so they go away ...
84 */
85 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
86 resource->align = log2(resource->size);
87 resource->gran = resource->align;
88 resource->flags = IORESOURCE_IO;
89 resource->limit = 0xffff;
90 }
91 else {
92 /* A Memory mapped base address */
93 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
94 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
95 resource->align = log2(resource->size);
96 resource->gran = resource->align;
97 resource->flags = IORESOURCE_MEM;
98 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
99 resource->flags |= IORESOURCE_PREFETCH;
100 }
101 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
102 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
103 /* 32bit limit */
104 resource->limit = 0xffffffffUL;
105 }
106 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
107 /* 1MB limit */
108 resource->limit = 0x000fffffUL;
109 }
110 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
111 unsigned long index_hi;
112 /* 64bit limit
113 * For now just treat this as a 32bit limit
114 */
115 index_hi = index + 4;
116 resource->limit = 0xffffffffUL;
117 resource->flags |= IORESOURCE_PCI64;
Eric Biederman7a5416a2003-06-12 19:23:51 +0000118 addr = pci_read_config32( dev, index_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 /* get the extended size */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000120 pci_write_config32(dev, index_hi, 0xffffffffUL);
121 size = pci_read_config32( dev, index_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122
123 /* get the minimum value the bar can be set to */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000124 pci_write_config32(dev, index_hi, 0);
125 base = pci_read_config32(dev, index_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000126
127 /* restore addr */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000128 pci_write_config32(dev, index_hi, addr);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000129
130 if ((size == 0xffffffff) && (base == 0)) {
131 /* Clear the top half of the bar */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000132 pci_write_config32(dev, index_hi, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000133 }
134 else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000135 printk_err("%s Unable to handle 64-bit address\n",
136 dev_path(dev));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137 resource->flags = IORESOURCE_PCI64;
138 }
139 }
140 else {
141 /* Invalid value */
142 resource->flags = 0;
143 }
144 }
145 /* dev->size holds the flags... */
146 return;
147}
148
149/** Read the base address registers for a given device.
150 * @param dev Pointer to the dev structure
151 * @param howmany How many registers to read (6 for device, 2 for bridge)
152 */
153static void pci_read_bases(struct device *dev, unsigned int howmany)
154{
155 unsigned int reg;
156 unsigned long index;
157
158 reg = dev->resources;
159 for(index = PCI_BASE_ADDRESS_0;
160 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
161 struct resource *resource;
162 resource = &dev->resource[reg];
163 pci_get_resource(dev, resource, index);
164 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
165 index += (resource->flags & IORESOURCE_PCI64)?8:4;
166 }
167 dev->resources = reg;
168}
169
170
171static void pci_bridge_read_bases(struct device *dev)
172{
173 unsigned int reg = dev->resources;
174
175 /* FIXME handle bridges without some of the optional resources */
176
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000177 printk_spew("%s: path %s\n", __FUNCTION__, dev_path(dev));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178 /* Initialize the io space constraints on the current bus */
179 dev->resource[reg].base = 0;
180 dev->resource[reg].size = 0;
181 dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
182 dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN);
183 dev->resource[reg].limit = 0xffffUL;
184 dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
185 dev->resource[reg].index = PCI_IO_BASE;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000186 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187 IORESOURCE_IO, IORESOURCE_IO);
188 reg++;
189
190 /* Initiliaze the prefetchable memory constraints on the current bus */
191 dev->resource[reg].base = 0;
192 dev->resource[reg].size = 0;
193 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
194 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
195 dev->resource[reg].limit = 0xffffffffUL;
196 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
197 dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000198 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199 IORESOURCE_MEM | IORESOURCE_PREFETCH,
200 IORESOURCE_MEM | IORESOURCE_PREFETCH);
201 reg++;
202
203 /* Initialize the memory resources on the current bus */
204 dev->resource[reg].base = 0;
205 dev->resource[reg].size = 0;
206 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
207 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
208 dev->resource[reg].limit = 0xffffffffUL;
209 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
210 dev->resource[reg].index = PCI_MEMORY_BASE;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000211 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
Eric Biederman8ca8d762003-04-22 19:02:15 +0000212 IORESOURCE_MEM | IORESOURCE_PREFETCH,
213 IORESOURCE_MEM);
214 reg++;
215
216 dev->resources = reg;
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000217 printk_spew("DONE %s: path %s\n", __FUNCTION__, dev_path(dev));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000218}
219
220
Eric Biederman5899fd82003-04-24 06:25:08 +0000221void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222{
223 uint32_t addr;
224 dev->resources = 0;
225 memset(&dev->resource[0], 0, sizeof(dev->resource));
226 pci_read_bases(dev, 6);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000227 addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000228 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
229}
230
Eric Biederman5899fd82003-04-24 06:25:08 +0000231void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000232{
233 uint32_t addr;
234 dev->resources = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000235 memset(&dev->resource, 0, sizeof(dev->resource));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000236 pci_bridge_read_bases(dev);
237 pci_read_bases(dev, 2);
238
Eric Biederman7a5416a2003-06-12 19:23:51 +0000239 addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000240 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
241
242}
243
244
245static void pci_set_resource(struct device *dev, struct resource *resource)
246{
247 unsigned long base, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000248 unsigned char buf[10];
Eric Biedermane9a271e32003-09-02 03:36:25 +0000249 unsigned long align;
250
Eric Biederman8ca8d762003-04-22 19:02:15 +0000251 /* Make certain the resource has actually been set */
252 if (!(resource->flags & IORESOURCE_SET)) {
253#if 1
Eric Biedermane9a271e32003-09-02 03:36:25 +0000254 printk_err("ERROR: %s %02x not allocated\n",
255 dev_path(dev), resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000256#endif
257 return;
258 }
259
260 /* Only handle PCI memory and IO resources for now */
261 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
262 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000263
Eric Biederman8ca8d762003-04-22 19:02:15 +0000264 if (resource->flags & IORESOURCE_MEM) {
265 dev->command |= PCI_COMMAND_MEMORY;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000266 }
267 if (resource->flags & IORESOURCE_IO) {
268 dev->command |= PCI_COMMAND_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000269 }
270 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
271 dev->command |= PCI_COMMAND_MASTER;
272 }
273 /* Get the base address */
274 base = resource->base;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000275 /* Get the resource alignment */
276 align = 1UL << resource->align;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000277
278 /* Get the limit (rounded up) */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000279 limit = base + ((resource->size + align - 1UL) & ~(align - 1UL)) -1UL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000280
281 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
282 /*
283 * some chipsets allow us to set/clear the IO bit.
284 * (e.g. VIA 82c686a.) So set it to be safe)
285 */
286 limit = base + resource->size -1;
287 if (resource->flags & IORESOURCE_IO) {
288 base |= PCI_BASE_ADDRESS_SPACE_IO;
289 }
Eric Biederman7a5416a2003-06-12 19:23:51 +0000290 pci_write_config32(dev, resource->index, base & 0xffffffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000291 if (resource->flags & IORESOURCE_PCI64) {
292 /* FIXME handle real 64bit base addresses */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000293 pci_write_config32(dev, resource->index + 4, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000294 }
295 }
296 else if (resource->index == PCI_IO_BASE) {
297 /* set the IO ranges
298 * WARNING: we don't really do 32-bit addressing for IO yet!
299 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000300 compute_allocate_resource(&dev->link[0], resource,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000301 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000302 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
303 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
Eric Biederman5fb929e2003-07-17 02:15:46 +0000304 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
305 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000306 }
307 else if (resource->index == PCI_MEMORY_BASE) {
308 /* set the memory range
309 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000310 compute_allocate_resource(&dev->link[0], resource,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000311 IORESOURCE_MEM | IORESOURCE_PREFETCH,
312 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000313 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
314 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000315 }
316 else if (resource->index == PCI_PREF_MEMORY_BASE) {
317 /* set the prefetchable memory range
318 * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
319 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000320 compute_allocate_resource(&dev->link[0], resource,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000321 IORESOURCE_MEM | IORESOURCE_PREFETCH,
322 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000323 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
324 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
Eric Biederman5fb929e2003-07-17 02:15:46 +0000325 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
326 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000327 }
328 else {
329 printk_err("ERROR: invalid resource->index %x\n",
330 resource->index);
331 }
332 buf[0] = '\0';
333 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000334 sprintf(buf, "bus %d ", dev->link[0].secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000335 }
336
337 printk_debug(
Eric Biedermane9a271e32003-09-02 03:36:25 +0000338 "%s %02x <- [0x%08lx - 0x%08lx] %s%s\n",
339 dev_path(dev),
Eric Biederman8ca8d762003-04-22 19:02:15 +0000340 resource->index,
341 resource->base, limit,
342 buf,
343 (resource->flags & IORESOURCE_IO)? "io":
344 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
345 return;
346}
347
Eric Biederman5899fd82003-04-24 06:25:08 +0000348void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000349{
350 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000351 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000352 uint8_t line;
353
354 last = &dev->resource[dev->resources];
355
356 for(resource = &dev->resource[0]; resource < last; resource++) {
357 pci_set_resource(dev, resource);
358 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000359 for(link = 0; link < dev->links; link++) {
360 struct bus *bus;
361 bus = &dev->link[link];
362 if (bus->children) {
363 assign_resources(bus);
364 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000365 }
366
367 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000368 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000369
370 /* set a default secondary latency timer */
371 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000372 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000373 }
374
375 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000376 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000377 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000378 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000379 }
380 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000381 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000382}
383
Eric Biedermane9a271e32003-09-02 03:36:25 +0000384void pci_dev_enable_resources(struct device *dev)
385{
386 uint16_t command;
387 command = pci_read_config16(dev, PCI_COMMAND);
388 command |= dev->command;
389 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
390 pci_write_config16(dev, PCI_COMMAND, command);
391
392 enable_childrens_resources(dev);
393}
394
395void pci_bus_enable_resources(struct device *dev)
396{
397 uint16_t ctrl;
398 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
399 ctrl |= dev->link[0].bridge_ctrl;
400 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
401 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
402
403 pci_dev_enable_resources(dev);
404}
405
Eric Biederman8ca8d762003-04-22 19:02:15 +0000406struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000407 .read_resources = pci_dev_read_resources,
408 .set_resources = pci_dev_set_resources,
409 .enable_resources = pci_dev_enable_resources,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000410 .init = 0,
411 .scan_bus = 0,
412};
413struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000414 .read_resources = pci_bus_read_resources,
415 .set_resources = pci_dev_set_resources,
416 .enable_resources = pci_bus_enable_resources,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417 .init = 0,
418 .scan_bus = pci_scan_bridge,
419};
420static void set_pci_ops(struct device *dev)
421{
422 struct pci_driver *driver;
423 if (dev->ops) {
424 return;
425 }
426 /* Look through the list of setup drivers and find one for
427 * this pci device
428 */
429 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
430 if ((driver->vendor == dev->vendor) &&
Eric Biederman5899fd82003-04-24 06:25:08 +0000431 (driver->device == dev->device)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432 dev->ops = driver->ops;
Eric Biederman5899fd82003-04-24 06:25:08 +0000433#if 1
Eric Biedermane9a271e32003-09-02 03:36:25 +0000434 printk_debug("%s [%04x/%04x] %sops\n",
435 dev_path(dev),
436 driver->vendor, driver->device,
437 (driver->ops->scan_bus?"bus ":"")
Eric Biederman5899fd82003-04-24 06:25:08 +0000438 );
439#endif
440 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000441 }
442 }
443 /* If I don't have a specific driver use the default operations */
444 switch(dev->hdr_type & 0x7f) { /* header type */
445 case PCI_HEADER_TYPE_NORMAL: /* standard header */
446 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
447 goto bad;
448 dev->ops = &default_pci_ops_dev;
449 break;
450 case PCI_HEADER_TYPE_BRIDGE:
451 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
452 goto bad;
453 dev->ops = &default_pci_ops_bus;
454 break;
455 default:
456 bad:
Eric Biedermane9a271e32003-09-02 03:36:25 +0000457 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biederman8ca8d762003-04-22 19:02:15 +0000458 "type %02x, ignoring.\n",
Eric Biedermane9a271e32003-09-02 03:36:25 +0000459 dev_path(dev),
Eric Biederman8ca8d762003-04-22 19:02:15 +0000460 dev->vendor, dev->device,
461 dev->class >> 8, dev->hdr_type);
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 */
472static 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 Biedermane9a271e32003-09-02 03:36:25 +0000476 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000477 /* Unlink from the list */
478 dev = *list;
479 *list = (*list)->sibling;
480 dev->sibling = 0;
481 break;
482 }
483 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000484 if (dev) {
485 device_t child;
486 /* Find the last child of our parent */
487 for(child = dev->bus->children; child && child->sibling; ) {
488 child = child->sibling;
489 }
490 /* Place the device on the list of children of it's parent. */
491 if (child) {
492 child->sibling = dev;
493 } else {
494 dev->bus->children = dev;
495 }
496 }
497
Eric Biederman8ca8d762003-04-22 19:02:15 +0000498 return dev;
499}
500
Eric Biederman8ca8d762003-04-22 19:02:15 +0000501/** Scan the pci bus devices and bridges.
Eric Biedermane9a271e32003-09-02 03:36:25 +0000502 * @param bus pointer to the bus structure
503 * @param min_devfn minimum devfn to look at in the scan usually 0x00
504 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +0000505 * @param max current bus number
506 * @return The maximum bus number found, after scanning all subordinate busses
507 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000508unsigned int pci_scan_bus(struct bus *bus,
509 unsigned min_devfn, unsigned max_devfn,
510 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000511{
512 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000513 device_t dev;
514 device_t old_devices;
515 device_t child;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000516
517 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
518
519 old_devices = bus->children;
520 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000521
522 post_code(0x24);
523
524
525 /* probe all devices on this bus with some optimization for non-existance and
526 single funcion devices */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000527 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000528 uint32_t id, class;
Eric Biederman30e143a2003-09-01 23:45:32 +0000529 uint8_t hdr_type;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000530
531 /* First thing setup the device structure */
532 dev = pci_scan_get_dev(&old_devices, devfn);
533
Eric Biedermane9a271e32003-09-02 03:36:25 +0000534 /* Detect if a device is present */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000535 if (!dev) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000536 struct device dummy;
537 dummy.bus = bus;
538 dummy.path.type = DEVICE_PATH_PCI;
539 dummy.path.u.pci.devfn = devfn;
540 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
541 /* some broken boards return 0 if a slot is empty: */
542 if ( (id == 0xffffffff) || (id == 0x00000000) ||
543 (id == 0x0000ffff) || (id == 0xffff0000))
544 {
545 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
546 if (PCI_FUNC(devfn) == 0x00) {
547 /* if this is a function 0 device and it is not present,
548 skip to next device */
549 devfn += 0x07;
550 }
551 /* multi function device, skip to next function */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000552 continue;
553 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000554 dev = alloc_dev(bus, &dummy.path);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000555 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000556 else {
557 /* Run the magic enable/disable sequence for the device */
558 if (dev->ops && dev->ops->enable) {
559 dev->ops->enable(dev);
560 }
561 /* Now read the vendor and device id */
562 id = pci_read_config32(dev, PCI_VENDOR_ID);
563 }
564
565 /* Read the rest of the pci configuration information */
566 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
567 class = pci_read_config32(dev, PCI_CLASS_REVISION);
568
569 /* Store the interesting information in the device structure */
570 dev->vendor = id & 0xffff;
571 dev->device = (id >> 16) & 0xffff;
572 dev->hdr_type = hdr_type;
573 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
574 dev->class = class >> 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000575
Eric Biederman8ca8d762003-04-22 19:02:15 +0000576 /* Look at the vendor and device id, or at least the
577 * header type and class and figure out which set of configuration
578 * methods to use.
579 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000580 if (!dev->ops) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000581 set_pci_ops(dev);
582 /* Error if we don't have some pci operations for it */
583 if (!dev->ops) {
584 printk_err("%s No device operations\n",
585 dev_path(dev));
586 continue;
587 }
588 /* Now run the magic enable/disable sequence for the device */
589 if (dev->ops && dev->ops->enable) {
590 dev->ops->enable(dev);
591 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000592 }
Eric Biederman4086d162003-07-17 03:26:03 +0000593
Eric Biedermane9a271e32003-09-02 03:36:25 +0000594 printk_debug("%s [%04x/%04x] %s\n",
595 dev_path(dev),
Eric Biederman4086d162003-07-17 03:26:03 +0000596 dev->vendor, dev->device,
597 dev->enable?"enabled": "disabled");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000598
Eric Biederman8ca8d762003-04-22 19:02:15 +0000599 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
600 /* if this is not a multi function device, don't waste time probe
601 another function. Skip to next device. */
602 devfn += 0x07;
603 }
604 }
605 post_code(0x25);
606
607 for(child = bus->children; child; child = child->sibling) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000608 if (!child->ops->scan_bus) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000609 continue;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000610 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000611 max = child->ops->scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000612 }
613 /*
614 * We've scanned the bus and so we know all about what's on
615 * the other side of any bridges that may be on this bus plus
616 * any devices.
617 *
618 * Return how far we've got finding sub-buses.
619 */
620 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
621 post_code(0x55);
622 return max;
623}
624
625/** Scan the bus, first for bridges and next for devices.
626 * @param pci_bus pointer to the bus structure
627 * @return The maximum bus number found, after scanning all subordinate busses
628 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000629unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000630{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000631 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000632 uint32_t buses;
633 uint16_t cr;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000634
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000635 printk_spew("%s: dev %p, max %d\n", __FUNCTION__, dev, max);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000636 bus = &dev->link[0];
637 dev->links = 1;
638
Eric Biederman8ca8d762003-04-22 19:02:15 +0000639 /* Set up the primary, secondary and subordinate bus numbers. We have
640 * no idea how many buses are behind this bridge yet, so we set the
641 * subordinate bus number to 0xff for the moment
642 */
643 bus->secondary = ++max;
644 bus->subordinate = 0xff;
645
646 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000647 cr = pci_read_config16(dev, PCI_COMMAND);
648 pci_write_config16(dev, PCI_COMMAND, 0x0000);
649 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000650
651 /*
652 * Read the existing primary/secondary/subordinate bus
653 * number configuration.
654 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000655 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000656
657 /* Configure the bus numbers for this bridge: the configuration
658 * transactions will not be propagated by the bridge if it is not
659 * correctly configured
660 */
661 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000662 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Eric Biederman8ca8d762003-04-22 19:02:15 +0000663 ((unsigned int) (bus->secondary) << 8) |
664 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000665 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000666
667 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000668 max = pci_scan_bus(bus, 0x00, 0xff, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000669
670 /* We know the number of buses behind this bridge. Set the subordinate
671 * bus number to its real value
672 */
673 bus->subordinate = max;
674 buses = (buses & 0xff00ffff) |
675 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000676 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
677 pci_write_config16(dev, PCI_COMMAND, cr);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000678
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000679 printk_spew("%s returns max %d\n", __FUNCTION__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000680 return max;
681}