1788044fe7865232773c184e63fa8db9f18ed62e
[coreboot.git] / src / devices / pci_device.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * It was originally based on the Linux kernel (drivers/pci/pci.c).
5  *
6  * Modifications are:
7  * Copyright (C) 2003-2004 Linux Networx
8  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
9  * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
10  * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
11  * Copyright (C) 2005-2006 Tyan
12  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
13  * Copyright (C) 2005-2009 coresystems GmbH
14  * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
15  */
16
17 /*
18  * PCI Bus Services, see include/linux/pci.h for further explanation.
19  *
20  * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
21  * David Mosberger-Tang
22  *
23  * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
24  */
25
26 #include <console/console.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <bitops.h>
30 #include <string.h>
31 #include <arch/io.h>
32 #include <device/device.h>
33 #include <device/pci.h>
34 #include <device/pci_ids.h>
35 #include <part/hard_reset.h>
36 #include <part/fallback_boot.h>
37 #include <delay.h>
38 #if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
39 #include <device/hypertransport.h>
40 #endif
41 #if CONFIG_PCIX_PLUGIN_SUPPORT == 1
42 #include <device/pcix.h>
43 #endif
44 #if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
45 #include <device/pciexp.h>
46 #endif
47 #if CONFIG_AGP_PLUGIN_SUPPORT == 1
48 #include <device/agp.h>
49 #endif
50 #if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
51 #include <device/cardbus.h>
52 #endif
53 #define CONFIG_PC80_SYSTEM 1
54 #if CONFIG_PC80_SYSTEM == 1
55 #include <pc80/i8259.h>
56 #endif
57
58 u8 pci_moving_config8(struct device *dev, unsigned int reg)
59 {
60         u8 value, ones, zeroes;
61         value = pci_read_config8(dev, reg);
62
63         pci_write_config8(dev, reg, 0xff);
64         ones = pci_read_config8(dev, reg);
65
66         pci_write_config8(dev, reg, 0x00);
67         zeroes = pci_read_config8(dev, reg);
68
69         pci_write_config8(dev, reg, value);
70
71         return ones ^ zeroes;
72 }
73
74 u16 pci_moving_config16(struct device * dev, unsigned int reg)
75 {
76         u16 value, ones, zeroes;
77         value = pci_read_config16(dev, reg);
78
79         pci_write_config16(dev, reg, 0xffff);
80         ones = pci_read_config16(dev, reg);
81
82         pci_write_config16(dev, reg, 0x0000);
83         zeroes = pci_read_config16(dev, reg);
84
85         pci_write_config16(dev, reg, value);
86
87         return ones ^ zeroes;
88 }
89
90 u32 pci_moving_config32(struct device * dev, unsigned int reg)
91 {
92         u32 value, ones, zeroes;
93         value = pci_read_config32(dev, reg);
94
95         pci_write_config32(dev, reg, 0xffffffff);
96         ones = pci_read_config32(dev, reg);
97
98         pci_write_config32(dev, reg, 0x00000000);
99         zeroes = pci_read_config32(dev, reg);
100
101         pci_write_config32(dev, reg, value);
102
103         return ones ^ zeroes;
104 }
105
106 /**
107  * Given a device, a capability type, and a last position, return the next
108  * matching capability. Always start at the head of the list.
109  *
110  * @param dev Pointer to the device structure.
111  * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
112  * @param last Location of the PCI capability register to start from.
113  */
114 unsigned pci_find_next_capability(struct device *dev, unsigned cap,
115                                   unsigned last)
116 {
117         unsigned pos = 0;
118         unsigned status;
119         unsigned reps = 48;
120
121         status = pci_read_config16(dev, PCI_STATUS);
122         if (!(status & PCI_STATUS_CAP_LIST)) {
123                 return 0;
124         }
125         switch (dev->hdr_type & 0x7f) {
126         case PCI_HEADER_TYPE_NORMAL:
127         case PCI_HEADER_TYPE_BRIDGE:
128                 pos = PCI_CAPABILITY_LIST;
129                 break;
130         case PCI_HEADER_TYPE_CARDBUS:
131                 pos = PCI_CB_CAPABILITY_LIST;
132                 break;
133         default:
134                 return 0;
135         }
136         pos = pci_read_config8(dev, pos);
137         while (reps-- && (pos >= 0x40)) {       /* Loop through the linked list. */
138                 int this_cap;
139                 pos &= ~3;
140                 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
141                 printk_spew("Capability: type 0x%02x @ 0x%02x\n", this_cap,
142                             pos);
143                 if (this_cap == 0xff) {
144                         break;
145                 }
146                 if (!last && (this_cap == cap)) {
147                         return pos;
148                 }
149                 if (last == pos) {
150                         last = 0;
151                 }
152                 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
153         }
154         return 0;
155 }
156
157 /**
158  * Given a device, and a capability type, return the next matching
159  * capability. Always start at the head of the list.
160  *
161  * @param dev Pointer to the device structure.
162  * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
163  */
164 unsigned pci_find_capability(device_t dev, unsigned cap)
165 {
166         return pci_find_next_capability(dev, cap, 0);
167 }
168
169 /**
170  * Given a device and register, read the size of the BAR for that register.
171  *
172  * @param dev Pointer to the device structure.
173  * @param index Address of the PCI configuration register.
174  */
175 struct resource *pci_get_resource(struct device *dev, unsigned long index)
176 {
177         struct resource *resource;
178         unsigned long value, attr;
179         resource_t moving, limit;
180
181         /* Initialize the resources to nothing. */
182         resource = new_resource(dev, index);
183
184         /* Get the initial value. */
185         value = pci_read_config32(dev, index);
186
187         /* See which bits move. */
188         moving = pci_moving_config32(dev, index);
189
190         /* Initialize attr to the bits that do not move. */
191         attr = value & ~moving;
192
193         /* If it is a 64bit resource look at the high half as well. */
194         if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
195             ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
196              PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
197                 /* Find the high bits that move. */
198                 moving |=
199                     ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
200         }
201         /* Find the resource constraints.
202          * Start by finding the bits that move. From there:
203          * - Size is the least significant bit of the bits that move.
204          * - Limit is all of the bits that move plus all of the lower bits.
205          * See PCI Spec 6.2.5.1.
206          */
207         limit = 0;
208         if (moving) {
209                 resource->size = 1;
210                 resource->align = resource->gran = 0;
211                 while (!(moving & resource->size)) {
212                         resource->size <<= 1;
213                         resource->align += 1;
214                         resource->gran += 1;
215                 }
216                 resource->limit = limit = moving | (resource->size - 1);
217         }
218
219         /* Some broken hardware has read-only registers that do not
220          * really size correctly.
221          * Example: the Acer M7229 has BARs 1-4 normally read-only.
222          * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
223          * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
224          * violation of the spec.
225          * We catch this case and ignore it by observing which bits move,
226          * This also catches the common case unimplemented registers
227          * that always read back as 0.
228          */
229         if (moving == 0) {
230                 if (value != 0) {
231                         printk_debug
232                             ("%s register %02lx(%08lx), read-only ignoring it\n",
233                              dev_path(dev), index, value);
234                 }
235                 resource->flags = 0;
236         } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
237                 /* An I/O mapped base address. */
238                 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
239                 resource->flags |= IORESOURCE_IO;
240                 /* I don't want to deal with 32bit I/O resources. */
241                 resource->limit = 0xffff;
242         } else {
243                 /* A Memory mapped base address. */
244                 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
245                 resource->flags |= IORESOURCE_MEM;
246                 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
247                         resource->flags |= IORESOURCE_PREFETCH;
248                 }
249                 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
250                 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
251                         /* 32bit limit. */
252                         resource->limit = 0xffffffffUL;
253                 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
254                         /* 1MB limit. */
255                         resource->limit = 0x000fffffUL;
256                 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
257                         /* 64bit limit. */
258                         resource->limit = 0xffffffffffffffffULL;
259                         resource->flags |= IORESOURCE_PCI64;
260                 } else {
261                         /* Invalid value. */
262                         printk_err("Broken BAR with value %lx\n", attr);
263                         printk_err(" on dev %s at index %02lx\n",
264                                dev_path(dev), index);
265                         resource->flags = 0;
266                 }
267         }
268         /* Don't let the limit exceed which bits can move. */
269         if (resource->limit > limit) {
270                 resource->limit = limit;
271         }
272
273         return resource;
274 }
275
276 /**
277  * Given a device and an index, read the size of the BAR for that register.
278  *
279  * @param dev Pointer to the device structure.
280  * @param index Address of the PCI configuration register.
281  */
282 static void pci_get_rom_resource(struct device *dev, unsigned long index)
283 {
284         struct resource *resource;
285         unsigned long value;
286         resource_t moving;
287
288         if ((dev->on_mainboard) && (dev->rom_address == 0)) {
289                 /* Skip it if rom_address is not set in the MB Config.lb. */
290                 return;
291         }
292
293         /* Initialize the resources to nothing. */
294         resource = new_resource(dev, index);
295
296         /* Get the initial value. */
297         value = pci_read_config32(dev, index);
298
299         /* See which bits move. */
300         moving = pci_moving_config32(dev, index);
301
302         /* Clear the Enable bit. */
303         moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
304
305         /* Find the resource constraints.
306          * Start by finding the bits that move. From there:
307          * - Size is the least significant bit of the bits that move.
308          * - Limit is all of the bits that move plus all of the lower bits.
309          * See PCI Spec 6.2.5.1.
310          */
311         if (moving) {
312                 resource->size = 1;
313                 resource->align = resource->gran = 0;
314                 while (!(moving & resource->size)) {
315                         resource->size <<= 1;
316                         resource->align += 1;
317                         resource->gran += 1;
318                 }
319                 resource->limit = moving | (resource->size - 1);
320                 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
321         } else {
322                 if (value != 0) {
323                         printk_debug
324                             ("%s register %02lx(%08lx), read-only ignoring it\n",
325                              dev_path(dev), index, value);
326                 }
327                 resource->flags = 0;
328         }
329
330         /* For on board device with embedded ROM image, the ROM image is at
331          * fixed address specified in the Config.lb, the dev->rom_address is
332          * inited by driver_pci_onboard_ops::enable_dev() */
333         if ((dev->on_mainboard) && (dev->rom_address != 0)) {
334                 resource->base = dev->rom_address;
335                 /* The resource allocator needs the size to be non-zero. */
336                 resource->size = 0x100;
337                 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
338                     IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
339         }
340
341         compact_resources(dev);
342 }
343
344 /**
345  * Read the base address registers for a given device.
346  *
347  * @param dev Pointer to the dev structure.
348  * @param howmany How many registers to read (6 for device, 2 for bridge).
349  */
350 static void pci_read_bases(struct device *dev, unsigned int howmany)
351 {
352         unsigned long index;
353
354         for (index = PCI_BASE_ADDRESS_0;
355              (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
356                 struct resource *resource;
357                 resource = pci_get_resource(dev, index);
358                 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
359         }
360
361         compact_resources(dev);
362 }
363
364 static void pci_record_bridge_resource(struct device *dev, resource_t moving,
365                                        unsigned index, unsigned long type)
366 {
367         /* Initialize the constraints on the current bus. */
368         struct resource *resource;
369         resource = NULL;
370         if (moving) {
371                 unsigned long gran;
372                 resource_t step;
373                 resource = new_resource(dev, index);
374                 resource->size = 0;
375                 gran = 0;
376                 step = 1;
377                 while ((moving & step) == 0) {
378                         gran += 1;
379                         step <<= 1;
380                 }
381                 resource->gran = gran;
382                 resource->align = gran;
383                 resource->limit = moving | (step - 1);
384                 resource->flags = type | IORESOURCE_PCI_BRIDGE |
385                                   IORESOURCE_BRIDGE;
386         }
387         return;
388 }
389
390 static void pci_bridge_read_bases(struct device *dev)
391 {
392         resource_t moving_base, moving_limit, moving;
393
394         /* See if the bridge I/O resources are implemented. */
395         moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
396         moving_base |=
397             ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
398
399         moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
400         moving_limit |=
401             ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
402
403         moving = moving_base & moving_limit;
404
405         /* Initialize the I/O space constraints on the current bus. */
406         pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
407
408         /* See if the bridge prefmem resources are implemented. */
409         moving_base =
410             ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
411         moving_base |=
412             ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) <<
413             32;
414
415         moving_limit =
416             ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) <<
417             16;
418         moving_limit |=
419             ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) <<
420             32;
421
422         moving = moving_base & moving_limit;
423         /* Initialize the prefetchable memory constraints on the current bus. */
424         pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
425                                    IORESOURCE_MEM | IORESOURCE_PREFETCH);
426
427         /* See if the bridge mem resources are implemented. */
428         moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
429         moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
430
431         moving = moving_base & moving_limit;
432
433         /* Initialize the memory resources on the current bus. */
434         pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
435                                    IORESOURCE_MEM);
436
437         compact_resources(dev);
438 }
439
440 void pci_dev_read_resources(struct device *dev)
441 {
442         pci_read_bases(dev, 6);
443         pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
444 }
445
446 void pci_bus_read_resources(struct device *dev)
447 {
448         pci_bridge_read_bases(dev);
449         pci_read_bases(dev, 2);
450         pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
451 }
452
453 void pci_domain_read_resources(struct device *dev)
454 {
455         struct resource *res;
456
457         /* Initialize the system-wide I/O space constraints. */
458         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
459         res->limit = 0xffffUL;
460         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
461                      IORESOURCE_ASSIGNED;
462
463         /* Initialize the system-wide memory resources constraints. */
464         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
465         res->limit = 0xffffffffULL;
466         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
467                      IORESOURCE_ASSIGNED;
468 }
469
470 static void pci_set_resource(struct device *dev, struct resource *resource)
471 {
472         resource_t base, end;
473
474         /* Make certain the resource has actually been assigned a value. */
475         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
476                 printk_err("ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
477                            dev_path(dev), resource->index,
478                            resource_type(resource), resource->size);
479                 return;
480         }
481
482         /* If I have already stored this resource don't worry about it. */
483         if (resource->flags & IORESOURCE_STORED) {
484                 return;
485         }
486
487         /* If the resource is subtractive don't worry about it. */
488         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
489                 return;
490         }
491
492         /* Only handle PCI memory and I/O resources for now. */
493         if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
494                 return;
495
496         /* Enable the resources in the command register. */
497         if (resource->size) {
498                 if (resource->flags & IORESOURCE_MEM) {
499                         dev->command |= PCI_COMMAND_MEMORY;
500                 }
501                 if (resource->flags & IORESOURCE_IO) {
502                         dev->command |= PCI_COMMAND_IO;
503                 }
504                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
505                         dev->command |= PCI_COMMAND_MASTER;
506                 }
507         }
508         /* Get the base address. */
509         base = resource->base;
510
511         /* Get the end. */
512         end = resource_end(resource);
513
514         /* Now store the resource. */
515         resource->flags |= IORESOURCE_STORED;
516
517         /* PCI Bridges have no enable bit.  They are disabled if the base of
518          * the range is greater than the limit.  If the size is zero, disable
519          * by setting the base = limit and end = limit - 2^gran.
520          */
521         if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
522                 base = resource->limit;
523                 end = resource->limit - (1 << resource->gran);
524                 resource->base = base;
525         }
526
527         if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
528                 unsigned long base_lo, base_hi;
529                 /* Some chipsets allow us to set/clear the I/O bit
530                  * (e.g. VIA 82c686a). So set it to be safe.
531                  */
532                 base_lo = base & 0xffffffff;
533                 base_hi = (base >> 32) & 0xffffffff;
534                 if (resource->flags & IORESOURCE_IO) {
535                         base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
536                 }
537                 pci_write_config32(dev, resource->index, base_lo);
538                 if (resource->flags & IORESOURCE_PCI64) {
539                         pci_write_config32(dev, resource->index + 4, base_hi);
540                 }
541         } else if (resource->index == PCI_IO_BASE) {
542                 /* Set the I/O ranges. */
543                 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
544                 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
545                 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
546                 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
547         } else if (resource->index == PCI_MEMORY_BASE) {
548                 /* Set the memory range. */
549                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
550                 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
551         } else if (resource->index == PCI_PREF_MEMORY_BASE) {
552                 /* Set the prefetchable memory range. */
553                 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
554                 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
555                 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
556                 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
557         } else {
558                 /* Don't let me think I stored the resource. */
559                 resource->flags &= ~IORESOURCE_STORED;
560                 printk_err("ERROR: invalid resource->index %lx\n",
561                            resource->index);
562         }
563         report_resource_stored(dev, resource, "");
564         return;
565 }
566
567 void pci_dev_set_resources(struct device *dev)
568 {
569         struct resource *resource, *last;
570         unsigned link;
571         u8 line;
572
573         last = &dev->resource[dev->resources];
574
575         for (resource = &dev->resource[0]; resource < last; resource++) {
576                 pci_set_resource(dev, resource);
577         }
578         for (link = 0; link < dev->links; link++) {
579                 struct bus *bus;
580                 bus = &dev->link[link];
581                 if (bus->children) {
582                         assign_resources(bus);
583                 }
584         }
585
586         /* Set a default latency timer. */
587         pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
588
589         /* Set a default secondary latency timer. */
590         if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
591                 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
592         }
593
594         /* Zero the IRQ settings. */
595         line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
596         if (line) {
597                 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
598         }
599         /* Set the cache line size, so far 64 bytes is good for everyone. */
600         pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
601 }
602
603 void pci_dev_enable_resources(struct device *dev)
604 {
605         const struct pci_operations *ops;
606         u16 command;
607
608         /* Set the subsystem vendor and device id for mainboard devices. */
609         ops = ops_pci(dev);
610         if (dev->on_mainboard && ops && ops->set_subsystem) {
611                 printk_debug("%s subsystem <- %02x/%02x\n",
612                              dev_path(dev),
613                              CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
614                              CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
615                 ops->set_subsystem(dev,
616                                    CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
617                                    CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
618         }
619         command = pci_read_config16(dev, PCI_COMMAND);
620         command |= dev->command;
621         /* v3 has
622          * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR);  // Error check.
623          */
624         printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
625         pci_write_config16(dev, PCI_COMMAND, command);
626 }
627
628 void pci_bus_enable_resources(struct device *dev)
629 {
630         u16 ctrl;
631
632         /* Enable I/O in command register if there is VGA card
633          * connected with (even it does not claim I/O resource).
634          */
635         if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
636                 dev->command |= PCI_COMMAND_IO;
637         ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
638         ctrl |= dev->link[0].bridge_ctrl;
639         ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR);  /* Error check. */
640         printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
641         pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
642
643         pci_dev_enable_resources(dev);
644         enable_childrens_resources(dev);
645 }
646
647 void pci_bus_reset(struct bus *bus)
648 {
649         unsigned ctl;
650         ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
651         ctl |= PCI_BRIDGE_CTL_BUS_RESET;
652         pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
653         mdelay(10);
654         ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
655         pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
656         delay(1);
657 }
658
659 void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
660 {
661         pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
662                            ((device & 0xffff) << 16) | (vendor & 0xffff));
663 }
664
665 /** default handler: only runs the relevant pci bios. */
666 void pci_dev_init(struct device *dev)
667 {
668 #if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
669         void run_bios(struct device *dev, unsigned long addr);
670         struct rom_header *rom, *ram;
671
672         if (CONFIG_PCI_ROM_RUN != 1 && /* Only execute VGA ROMs. */
673             ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
674                 return;
675
676         if (CONFIG_VGA_ROM_RUN != 1 && /* Only execute non-VGA ROMs. */
677             ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
678                 return;
679
680         rom = pci_rom_probe(dev);
681         if (rom == NULL)
682                 return;
683
684         ram = pci_rom_load(dev, rom);
685         if (ram == NULL)
686                 return;
687
688         run_bios(dev, (unsigned long)ram);
689
690 #if CONFIG_CONSOLE_VGA == 1
691         if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA)
692                 vga_console_init();
693 #endif /* CONFIG_CONSOLE_VGA */
694 #endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
695 }
696
697 /** Default device operation for PCI devices */
698 static struct pci_operations pci_dev_ops_pci = {
699         .set_subsystem = pci_dev_set_subsystem,
700 };
701
702 struct device_operations default_pci_ops_dev = {
703         .read_resources = pci_dev_read_resources,
704         .set_resources = pci_dev_set_resources,
705         .enable_resources = pci_dev_enable_resources,
706         .init = pci_dev_init,
707         .scan_bus = 0,
708         .enable = 0,
709         .ops_pci = &pci_dev_ops_pci,
710 };
711
712 /** Default device operations for PCI bridges */
713 static struct pci_operations pci_bus_ops_pci = {
714         .set_subsystem = 0,
715 };
716
717 struct device_operations default_pci_ops_bus = {
718         .read_resources = pci_bus_read_resources,
719         .set_resources = pci_dev_set_resources,
720         .enable_resources = pci_bus_enable_resources,
721         .init = 0,
722         .scan_bus = pci_scan_bridge,
723         .enable = 0,
724         .reset_bus = pci_bus_reset,
725         .ops_pci = &pci_bus_ops_pci,
726 };
727
728 /**
729  * @brief Detect the type of downstream bridge
730  *
731  * This function is a heuristic to detect which type of bus is downstream
732  * of a PCI-to-PCI bridge. This functions by looking for various capability
733  * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
734  * Hypertransport all seem to have appropriate capabilities.
735  *
736  * When only a PCI-Express capability is found the type
737  * is examined to see which type of bridge we have.
738  *
739  * @param dev Pointer to the device structure of the bridge.
740  * @return Appropriate bridge operations.
741  */
742 static struct device_operations *get_pci_bridge_ops(device_t dev)
743 {
744         unsigned pos;
745
746 #if CONFIG_PCIX_PLUGIN_SUPPORT == 1
747         pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
748         if (pos) {
749                 printk_debug("%s subordinate bus PCI-X\n", dev_path(dev));
750                 return &default_pcix_ops_bus;
751         }
752 #endif
753 #if CONFIG_AGP_PLUGIN_SUPPORT == 1
754         /* How do I detect an PCI to AGP bridge? */
755 #endif
756 #if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
757         pos = 0;
758         while ((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
759                 unsigned flags;
760                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
761                 if ((flags >> 13) == 1) {
762                         /* Host or Secondary Interface */
763                         printk_debug("%s subordinate bus Hypertransport\n",
764                                      dev_path(dev));
765                         return &default_ht_ops_bus;
766                 }
767         }
768 #endif
769 #if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
770         pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
771         if (pos) {
772                 unsigned flags;
773                 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
774                 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
775                 case PCI_EXP_TYPE_ROOT_PORT:
776                 case PCI_EXP_TYPE_UPSTREAM:
777                 case PCI_EXP_TYPE_DOWNSTREAM:
778                         printk_debug("%s subordinate bus PCI Express\n",
779                                      dev_path(dev));
780                         return &default_pciexp_ops_bus;
781                 case PCI_EXP_TYPE_PCI_BRIDGE:
782                         printk_debug("%s subordinate PCI\n", dev_path(dev));
783                         return &default_pci_ops_bus;
784                 default:
785                         break;
786                 }
787         }
788 #endif
789         return &default_pci_ops_bus;
790 }
791
792 /**
793  * Set up PCI device operation.  Check if it already has a driver.  If not, use
794  * find_device_operations, or set to a default based on type.
795  *
796  * @param dev Pointer to the device whose pci_ops you want to set.
797  * @see pci_drivers
798  */
799 static void set_pci_ops(struct device *dev)
800 {
801         struct pci_driver *driver;
802         if (dev->ops) {
803                 return;
804         }
805
806         /* Look through the list of setup drivers and find one for
807          * this PCI device.
808          */
809         for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
810                 if ((driver->vendor == dev->vendor) &&
811                     (driver->device == dev->device)) {
812                         dev->ops = (struct device_operations *)driver->ops;
813                         printk_spew("%s [%04x/%04x] %sops\n",
814                                     dev_path(dev),
815                                     driver->vendor, driver->device,
816                                     (driver->ops->scan_bus ? "bus " : ""));
817                         return;
818                 }
819         }
820
821         /* If I don't have a specific driver use the default operations */
822         switch (dev->hdr_type & 0x7f) { /* header type */
823         case PCI_HEADER_TYPE_NORMAL:    /* standard header */
824                 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
825                         goto bad;
826                 dev->ops = &default_pci_ops_dev;
827                 break;
828         case PCI_HEADER_TYPE_BRIDGE:
829                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
830                         goto bad;
831                 dev->ops = get_pci_bridge_ops(dev);
832                 break;
833 #if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
834         case PCI_HEADER_TYPE_CARDBUS:
835                 dev->ops = &default_cardbus_ops_bus;
836                 break;
837 #endif
838         default:
839               bad:
840                 if (dev->enabled) {
841                         printk_err("%s [%04x/%04x/%06x] has unknown header "
842                                    "type %02x, ignoring.\n",
843                                    dev_path(dev),
844                                    dev->vendor, dev->device,
845                                    dev->class >> 8, dev->hdr_type);
846                 }
847         }
848         return;
849 }
850
851 /**
852  * @brief See if we have already allocated a device structure for a given devfn.
853  *
854  * Given a linked list of PCI device structures and a devfn number, find the
855  * device structure correspond to the devfn, if present. This function also
856  * removes the device structure from the linked list.
857  *
858  * @param list The device structure list.
859  * @param devfn A device/function number.
860  *
861  * @return Pointer to the device structure found or NULL if we have not
862  *         allocated a device for this devfn yet.
863  */
864 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
865 {
866         struct device *dev;
867         dev = 0;
868         for (; *list; list = &(*list)->sibling) {
869                 if ((*list)->path.type != DEVICE_PATH_PCI) {
870                         printk_err("child %s not a pci device\n",
871                                    dev_path(*list));
872                         continue;
873                 }
874                 if ((*list)->path.pci.devfn == devfn) {
875                         /* Unlink from the list. */
876                         dev = *list;
877                         *list = (*list)->sibling;
878                         dev->sibling = NULL;
879                         break;
880                 }
881         }
882
883         /* Just like alloc_dev() add the device to the list of devices on the
884          * bus. When the list of devices was formed we removed all of the
885          * parents children, and now we are interleaving static and dynamic
886          * devices in order on the bus.
887          */
888         if (dev) {
889                 struct device *child;
890                 /* Find the last child of our parent. */
891                 for (child = dev->bus->children; child && child->sibling;) {
892                         child = child->sibling;
893                 }
894                 /* Place the device on the list of children of its parent. */
895                 if (child) {
896                         child->sibling = dev;
897                 } else {
898                         dev->bus->children = dev;
899                 }
900         }
901
902         return dev;
903 }
904
905 /**
906  * @brief Scan a PCI bus.
907  *
908  * Determine the existence of a given PCI device. Allocate a new struct device
909  * if dev==NULL was passed in and the device exists in hardware.
910  *
911  * @param bus pointer to the bus structure
912  * @param devfn to look at
913  *
914  * @return The device structure for hte device (if found)
915  *         or the NULL if no device is found.
916  */
917 device_t pci_probe_dev(device_t dev, struct bus * bus, unsigned devfn)
918 {
919         u32 id, class;
920         u8 hdr_type;
921
922         /* Detect if a device is present. */
923         if (!dev) {
924                 struct device dummy;
925                 dummy.bus = bus;
926                 dummy.path.type = DEVICE_PATH_PCI;
927                 dummy.path.pci.devfn = devfn;
928                 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
929                 /* Have we found something?
930                  * Some broken boards return 0 if a slot is empty.
931                  */
932                 if ((id == 0xffffffff) || (id == 0x00000000) ||
933                     (id == 0x0000ffff) || (id == 0xffff0000)) {
934                         printk_spew("%s, bad id 0x%x\n", dev_path(&dummy), id);
935                         return NULL;
936                 }
937                 dev = alloc_dev(bus, &dummy.path);
938         } else {
939                 /* Enable/disable the device. Once we have found the device-
940                  * specific operations this operations we will disable the
941                  * device with those as well.
942                  *
943                  * This is geared toward devices that have subfunctions
944                  * that do not show up by default.
945                  *
946                  * If a device is a stuff option on the motherboard
947                  * it may be absent and enable_dev() must cope.
948                  */
949                 /* Run the magic enable sequence for the device. */
950                 if (dev->chip_ops && dev->chip_ops->enable_dev) {
951                         dev->chip_ops->enable_dev(dev);
952                 }
953                 /* Now read the vendor and device ID. */
954                 id = pci_read_config32(dev, PCI_VENDOR_ID);
955
956                 /* If the device does not have a PCI ID disable it. Possibly
957                  * this is because we have already disabled the device. But
958                  * this also handles optional devices that may not always
959                  * show up.
960                  */
961                 /* If the chain is fully enumerated quit */
962                 if ((id == 0xffffffff) || (id == 0x00000000) ||
963                     (id == 0x0000ffff) || (id == 0xffff0000)) {
964                         if (dev->enabled) {
965                                 printk_info("Disabling static device: %s\n",
966                                             dev_path(dev));
967                                 dev->enabled = 0;
968                         }
969                         return dev;
970                 }
971         }
972         /* Read the rest of the PCI configuration information. */
973         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
974         class = pci_read_config32(dev, PCI_CLASS_REVISION);
975
976         /* Store the interesting information in the device structure. */
977         dev->vendor = id & 0xffff;
978         dev->device = (id >> 16) & 0xffff;
979         dev->hdr_type = hdr_type;
980
981         /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
982         dev->class = class >> 8;
983
984         /* Architectural/System devices always need to be bus masters. */
985         if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
986                 dev->command |= PCI_COMMAND_MASTER;
987         }
988         /* Look at the vendor and device ID, or at least the header type and
989          * class and figure out which set of configuration methods to use.
990          * Unless we already have some PCI ops.
991          */
992         set_pci_ops(dev);
993
994         /* Now run the magic enable/disable sequence for the device. */
995         if (dev->ops && dev->ops->enable) {
996                 dev->ops->enable(dev);
997         }
998
999         /* Display the device. */
1000         printk_debug("%s [%04x/%04x] %s%s\n",
1001                      dev_path(dev),
1002                      dev->vendor, dev->device,
1003                      dev->enabled ? "enabled" : "disabled",
1004                      dev->ops ? "" : " No operations");
1005
1006         return dev;
1007 }
1008
1009 /**
1010  * @brief Scan a PCI bus.
1011  *
1012  * Determine the existence of devices and bridges on a PCI bus. If there are
1013  * bridges on the bus, recursively scan the buses behind the bridges.
1014  *
1015  * This function is the default scan_bus() method for the root device
1016  * 'dev_root'.
1017  *
1018  * @param bus pointer to the bus structure
1019  * @param min_devfn minimum devfn to look at in the scan usually 0x00
1020  * @param max_devfn maximum devfn to look at in the scan usually 0xff
1021  * @param max current bus number
1022  *
1023  * @return The maximum bus number found, after scanning all subordinate busses
1024  */
1025 unsigned int pci_scan_bus(struct bus *bus,
1026                           unsigned min_devfn, unsigned max_devfn,
1027                           unsigned int max)
1028 {
1029         unsigned int devfn;
1030         struct device *old_devices;
1031         struct device *child;
1032
1033 #if CONFIG_PCI_BUS_SEGN_BITS
1034         printk_debug("PCI: pci_scan_bus for bus %04x:%02x\n",
1035                      bus->secondary >> 8, bus->secondary & 0xff);
1036 #else
1037         printk_debug("PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1038 #endif
1039
1040         old_devices = bus->children;
1041         bus->children = NULL;
1042
1043         post_code(0x24);
1044         /* Probe all devices/functions on this bus with some optimization for
1045          * non-existence and single function devices.
1046          */
1047         for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
1048                 struct device *dev;
1049
1050                 /* First thing setup the device structure */
1051                 dev = pci_scan_get_dev(&old_devices, devfn);
1052
1053                 /* See if a device is present and setup the device structure. */
1054                 dev = pci_probe_dev(dev, bus, devfn);
1055
1056                 /* If this is not a multi function device, or the device is
1057                  * not present don't waste time probing another function.
1058                  * Skip to next device.
1059                  */
1060                 if ((PCI_FUNC(devfn) == 0x00) &&
1061                     (!dev
1062                      || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
1063                         devfn += 0x07;
1064                 }
1065         }
1066         post_code(0x25);
1067
1068         /* Warn if any leftover static devices are are found.
1069          * There's probably a problem in the Config.lb.
1070          */
1071         if (old_devices) {
1072                 device_t left;
1073                 printk_warning("PCI: Left over static devices:\n");
1074                 for (left = old_devices; left; left = left->sibling) {
1075                         printk_warning("%s\n", dev_path(left));
1076                 }
1077                 printk_warning("PCI: Check your mainboard Config.lb.\n");
1078         }
1079
1080         /* For all children that implement scan_bus() (i.e. bridges)
1081          * scan the bus behind that child.
1082          */
1083         for (child = bus->children; child; child = child->sibling) {
1084                 max = scan_bus(child, max);
1085         }
1086
1087         /* We've scanned the bus and so we know all about what's on the other
1088          * side of any bridges that may be on this bus plus any devices.
1089          * Return how far we've got finding sub-buses.
1090          */
1091         printk_debug("PCI: pci_scan_bus returning with max=%03x\n", max);
1092         post_code(0x55);
1093         return max;
1094 }
1095
1096 /**
1097  * @brief Scan a PCI bridge and the buses behind the bridge.
1098  *
1099  * Determine the existence of buses behind the bridge. Set up the bridge
1100  * according to the result of the scan.
1101  *
1102  * This function is the default scan_bus() method for PCI bridge devices.
1103  *
1104  * @param dev Pointer to the bridge device.
1105  * @param max The highest bus number assigned up to now.
1106  * @return The maximum bus number found, after scanning all subordinate buses.
1107  */
1108 unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
1109                                 unsigned int (*do_scan_bus) (struct bus * bus,
1110                                                              unsigned min_devfn,
1111                                                              unsigned max_devfn,
1112                                                              unsigned int max))
1113 {
1114         struct bus *bus;
1115         u32 buses;
1116         u16 cr;
1117
1118         printk_spew("%s for %s\n", __func__, dev_path(dev));
1119
1120         bus = &dev->link[0];
1121         bus->dev = dev;
1122         dev->links = 1;
1123
1124         /* Set up the primary, secondary and subordinate bus numbers. We have
1125          * no idea how many buses are behind this bridge yet, so we set the
1126          * subordinate bus number to 0xff for the moment.
1127          */
1128         bus->secondary = ++max;
1129         bus->subordinate = 0xff;
1130
1131         /* Clear all status bits and turn off memory, I/O and master enables. */
1132         cr = pci_read_config16(dev, PCI_COMMAND);
1133         pci_write_config16(dev, PCI_COMMAND, 0x0000);
1134         pci_write_config16(dev, PCI_STATUS, 0xffff);
1135
1136         /* Read the existing primary/secondary/subordinate bus
1137          * number configuration.
1138          */
1139         buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
1140
1141         /* Configure the bus numbers for this bridge: the configuration
1142          * transactions will not be propagated by the bridge if it is not
1143          * correctly configured.
1144          */
1145         buses &= 0xff000000;
1146         buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1147                   ((unsigned int)(bus->secondary) << 8) |
1148                   ((unsigned int)(bus->subordinate) << 16));
1149         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1150
1151         /* Now we can scan all subordinate buses
1152          * i.e. the bus behind the bridge.
1153          */
1154         max = do_scan_bus(bus, 0x00, 0xff, max);
1155
1156         /* We know the number of buses behind this bridge. Set the subordinate
1157          * bus number to its real value.
1158          */
1159         bus->subordinate = max;
1160         buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
1161         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1162         pci_write_config16(dev, PCI_COMMAND, cr);
1163
1164         printk_spew("%s returns max %d\n", __func__, max);
1165         return max;
1166 }
1167
1168 /**
1169  * @brief Scan a PCI bridge and the buses behind the bridge.
1170  *
1171  * Determine the existence of buses behind the bridge. Set up the bridge
1172  * according to the result of the scan.
1173  *
1174  * This function is the default scan_bus() method for PCI bridge devices.
1175  *
1176  * @param dev Pointer to the bridge device.
1177  * @param max The highest bus number assigned up to now.
1178  * @return The maximum bus number found, after scanning all subordinate buses.
1179  */
1180 unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1181 {
1182         return do_pci_scan_bridge(dev, max, pci_scan_bus);
1183 }
1184
1185 /**
1186  * @brief Scan a PCI domain.
1187  *
1188  * This function is the default scan_bus() method for PCI domains.
1189  *
1190  * @param dev pointer to the domain
1191  * @param max the highest bus number assgined up to now
1192  *
1193  * @return The maximum bus number found, after scanning all subordinate busses
1194  */
1195 unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1196 {
1197         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
1198         return max;
1199 }
1200
1201 #if CONFIG_PC80_SYSTEM == 1
1202 /**
1203  * 
1204  * @brief Assign IRQ numbers
1205  *
1206  * This function assigns IRQs for all functions contained within the indicated
1207  * device address.  If the device does not exist or does not require interrupts
1208  * then this function has no effect.
1209  *
1210  * This function should be called for each PCI slot in your system.
1211  *
1212  * @param bus
1213  * @param slot
1214  * @param pIntAtoD is an array of IRQ #s that are assigned to PINTA through
1215  *        PINTD of this slot.  The particular irq #s that are passed in 
1216  *        depend on the routing inside your southbridge and on your 
1217  *        motherboard.
1218  */
1219 void pci_assign_irqs(unsigned bus, unsigned slot,
1220         const unsigned char pIntAtoD[4])
1221 {
1222         unsigned int funct;
1223         device_t pdev;
1224         u8 line;
1225         u8 irq;
1226
1227         /* Each slot may contain up to eight functions */
1228         for (funct = 0; funct < 8; funct++) {
1229                 pdev = dev_find_slot(bus, (slot << 3) + funct);
1230
1231                 if (!pdev)
1232                         continue;
1233
1234                 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1235
1236                 // PCI spec says all values except 1..4 are reserved.
1237                 if ((line < 1) || (line > 4))
1238                         continue;
1239
1240                 irq = pIntAtoD[line - 1];
1241
1242                 printk_debug("Assigning IRQ %d to %d:%x.%d\n",
1243                         irq, bus, slot, funct);
1244
1245                 pci_write_config8(pdev, PCI_INTERRUPT_LINE, 
1246                         pIntAtoD[line - 1]);
1247
1248 #ifdef PARANOID_IRQ_ASSIGNMENTS
1249                 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1250                 printk_debug("  Readback = %d\n", irq);
1251 #endif
1252
1253                 // Change to level triggered
1254                 i8259_configure_irq_trigger(pIntAtoD[line - 1], IRQ_LEVEL_TRIGGERED);
1255         }
1256 }
1257 #endif
1258