Rewrite interrupt handling in coreboot to be more comprehensible and
[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                 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
336                     IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
337         }
338
339         compact_resources(dev);
340 }
341
342 /**
343  * Read the base address registers for a given device.
344  *
345  * @param dev Pointer to the dev structure.
346  * @param howmany How many registers to read (6 for device, 2 for bridge).
347  */
348 static void pci_read_bases(struct device *dev, unsigned int howmany)
349 {
350         unsigned long index;
351
352         for (index = PCI_BASE_ADDRESS_0;
353              (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
354                 struct resource *resource;
355                 resource = pci_get_resource(dev, index);
356                 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
357         }
358
359         compact_resources(dev);
360 }
361
362 static void pci_record_bridge_resource(struct device *dev, resource_t moving,
363                                        unsigned index, unsigned long type)
364 {
365         /* Initialize the constraints on the current bus. */
366         struct resource *resource;
367         resource = NULL;
368         if (moving) {
369                 unsigned long gran;
370                 resource_t step;
371                 resource = new_resource(dev, index);
372                 resource->size = 0;
373                 gran = 0;
374                 step = 1;
375                 while ((moving & step) == 0) {
376                         gran += 1;
377                         step <<= 1;
378                 }
379                 resource->gran = gran;
380                 resource->align = gran;
381                 resource->limit = moving | (step - 1);
382                 resource->flags = type | IORESOURCE_PCI_BRIDGE |
383                                   IORESOURCE_BRIDGE;
384         }
385         return;
386 }
387
388 static void pci_bridge_read_bases(struct device *dev)
389 {
390         resource_t moving_base, moving_limit, moving;
391
392         /* See if the bridge I/O resources are implemented. */
393         moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
394         moving_base |=
395             ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
396
397         moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
398         moving_limit |=
399             ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
400
401         moving = moving_base & moving_limit;
402
403         /* Initialize the I/O space constraints on the current bus. */
404         pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
405
406         /* See if the bridge prefmem resources are implemented. */
407         moving_base =
408             ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
409         moving_base |=
410             ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) <<
411             32;
412
413         moving_limit =
414             ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) <<
415             16;
416         moving_limit |=
417             ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) <<
418             32;
419
420         moving = moving_base & moving_limit;
421         /* Initialize the prefetchable memory constraints on the current bus. */
422         pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
423                                    IORESOURCE_MEM | IORESOURCE_PREFETCH);
424
425         /* See if the bridge mem resources are implemented. */
426         moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
427         moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
428
429         moving = moving_base & moving_limit;
430
431         /* Initialize the memory resources on the current bus. */
432         pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
433                                    IORESOURCE_MEM);
434
435         compact_resources(dev);
436 }
437
438 void pci_dev_read_resources(struct device *dev)
439 {
440         pci_read_bases(dev, 6);
441         pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
442 }
443
444 void pci_bus_read_resources(struct device *dev)
445 {
446         pci_bridge_read_bases(dev);
447         pci_read_bases(dev, 2);
448         pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
449 }
450
451 void pci_domain_read_resources(struct device *dev)
452 {
453         struct resource *res;
454
455         /* Initialize the system-wide I/O space constraints. */
456         res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
457         res->limit = 0xffffUL;
458         res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
459                      IORESOURCE_ASSIGNED;
460
461         /* Initialize the system-wide memory resources constraints. */
462         res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
463         res->limit = 0xffffffffULL;
464         res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
465                      IORESOURCE_ASSIGNED;
466 }
467
468 static void pci_set_resource(struct device *dev, struct resource *resource)
469 {
470         resource_t base, end;
471
472         /* Make certain the resource has actually been assigned a value. */
473         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
474                 printk_err("ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
475                            dev_path(dev), resource->index,
476                            resource_type(resource), resource->size);
477                 return;
478         }
479
480         /* If I have already stored this resource don't worry about it. */
481         if (resource->flags & IORESOURCE_STORED) {
482                 return;
483         }
484
485         /* If the resource is subtractive don't worry about it. */
486         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
487                 return;
488         }
489
490         /* Only handle PCI memory and I/O resources for now. */
491         if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
492                 return;
493
494         /* Enable the resources in the command register. */
495         if (resource->size) {
496                 if (resource->flags & IORESOURCE_MEM) {
497                         dev->command |= PCI_COMMAND_MEMORY;
498                 }
499                 if (resource->flags & IORESOURCE_IO) {
500                         dev->command |= PCI_COMMAND_IO;
501                 }
502                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
503                         dev->command |= PCI_COMMAND_MASTER;
504                 }
505         }
506         /* Get the base address. */
507         base = resource->base;
508
509         /* Get the end. */
510         end = resource_end(resource);
511
512         /* Now store the resource. */
513         resource->flags |= IORESOURCE_STORED;
514
515         /* PCI Bridges have no enable bit.  They are disabled if the base of
516          * the range is greater than the limit.  If the size is zero, disable
517          * by setting the base = limit and end = limit - 2^gran.
518          */
519         if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
520                 base = resource->limit;
521                 end = resource->limit - (1 << resource->gran);
522                 resource->base = base;
523         }
524
525         if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
526                 unsigned long base_lo, base_hi;
527                 /* Some chipsets allow us to set/clear the I/O bit
528                  * (e.g. VIA 82c686a). So set it to be safe.
529                  */
530                 base_lo = base & 0xffffffff;
531                 base_hi = (base >> 32) & 0xffffffff;
532                 if (resource->flags & IORESOURCE_IO) {
533                         base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
534                 }
535                 pci_write_config32(dev, resource->index, base_lo);
536                 if (resource->flags & IORESOURCE_PCI64) {
537                         pci_write_config32(dev, resource->index + 4, base_hi);
538                 }
539         } else if (resource->index == PCI_IO_BASE) {
540                 /* Set the I/O ranges. */
541                 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
542                 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
543                 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
544                 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
545         } else if (resource->index == PCI_MEMORY_BASE) {
546                 /* Set the memory range. */
547                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
548                 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
549         } else if (resource->index == PCI_PREF_MEMORY_BASE) {
550                 /* Set the prefetchable memory range. */
551                 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
552                 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
553                 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
554                 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
555         } else {
556                 /* Don't let me think I stored the resource. */
557                 resource->flags &= ~IORESOURCE_STORED;
558                 printk_err("ERROR: invalid resource->index %lx\n",
559                            resource->index);
560         }
561         report_resource_stored(dev, resource, "");
562         return;
563 }
564
565 void pci_dev_set_resources(struct device *dev)
566 {
567         struct resource *resource, *last;
568         unsigned link;
569         u8 line;
570
571         last = &dev->resource[dev->resources];
572
573         for (resource = &dev->resource[0]; resource < last; resource++) {
574                 pci_set_resource(dev, resource);
575         }
576         for (link = 0; link < dev->links; link++) {
577                 struct bus *bus;
578                 bus = &dev->link[link];
579                 if (bus->children) {
580                         assign_resources(bus);
581                 }
582         }
583
584         /* Set a default latency timer. */
585         pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
586
587         /* Set a default secondary latency timer. */
588         if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
589                 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
590         }
591
592         /* Zero the IRQ settings. */
593         line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
594         if (line) {
595                 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
596         }
597         /* Set the cache line size, so far 64 bytes is good for everyone. */
598         pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
599 }
600
601 void pci_dev_enable_resources(struct device *dev)
602 {
603         const struct pci_operations *ops;
604         u16 command;
605
606         /* Set the subsystem vendor and device id for mainboard devices. */
607         ops = ops_pci(dev);
608         if (dev->on_mainboard && ops && ops->set_subsystem) {
609                 printk_debug("%s subsystem <- %02x/%02x\n",
610                              dev_path(dev),
611                              CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
612                              CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
613                 ops->set_subsystem(dev,
614                                    CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
615                                    CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
616         }
617         command = pci_read_config16(dev, PCI_COMMAND);
618         command |= dev->command;
619         /* v3 has
620          * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR);  // Error check.
621          */
622         printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
623         pci_write_config16(dev, PCI_COMMAND, command);
624 }
625
626 void pci_bus_enable_resources(struct device *dev)
627 {
628         u16 ctrl;
629
630         /* Enable I/O in command register if there is VGA card
631          * connected with (even it does not claim I/O resource).
632          */
633         if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
634                 dev->command |= PCI_COMMAND_IO;
635         ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
636         ctrl |= dev->link[0].bridge_ctrl;
637         ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR);  /* Error check. */
638         printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
639         pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
640
641         pci_dev_enable_resources(dev);
642         enable_childrens_resources(dev);
643 }
644
645 void pci_bus_reset(struct bus *bus)
646 {
647         unsigned ctl;
648         ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
649         ctl |= PCI_BRIDGE_CTL_BUS_RESET;
650         pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
651         mdelay(10);
652         ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
653         pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
654         delay(1);
655 }
656
657 void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
658 {
659         pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
660                            ((device & 0xffff) << 16) | (vendor & 0xffff));
661 }
662
663 /** default handler: only runs the relevant pci bios. */
664 void pci_dev_init(struct device *dev)
665 {
666 #if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
667         void run_bios(struct device *dev, unsigned long addr);
668         struct rom_header *rom, *ram;
669
670 #if CONFIG_PCI_ROM_RUN != 1
671         /* We want to execute VGA option ROMs when CONFIG_VGA_ROM_RUN
672          * is set but CONFIG_PCI_ROM_RUN is not. In this case we skip
673          * all other option ROM types.
674          */
675         if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
676                 return;
677 #endif
678
679         rom = pci_rom_probe(dev);
680         if (rom == NULL)
681                 return;
682
683         ram = pci_rom_load(dev, rom);
684         if (ram == NULL)
685                 return;
686
687         run_bios(dev, (unsigned long)ram);
688
689 #if CONFIG_CONSOLE_VGA == 1
690         if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA)
691                 vga_console_init();
692 #endif /* CONFIG_CONSOLE_VGA */
693 #endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
694 }
695
696 /** Default device operation for PCI devices */
697 static struct pci_operations pci_dev_ops_pci = {
698         .set_subsystem = pci_dev_set_subsystem,
699 };
700
701 struct device_operations default_pci_ops_dev = {
702         .read_resources = pci_dev_read_resources,
703         .set_resources = pci_dev_set_resources,
704         .enable_resources = pci_dev_enable_resources,
705         .init = pci_dev_init,
706         .scan_bus = 0,
707         .enable = 0,
708         .ops_pci = &pci_dev_ops_pci,
709 };
710
711 /** Default device operations for PCI bridges */
712 static struct pci_operations pci_bus_ops_pci = {
713         .set_subsystem = 0,
714 };
715
716 struct device_operations default_pci_ops_bus = {
717         .read_resources = pci_bus_read_resources,
718         .set_resources = pci_dev_set_resources,
719         .enable_resources = pci_bus_enable_resources,
720         .init = 0,
721         .scan_bus = pci_scan_bridge,
722         .enable = 0,
723         .reset_bus = pci_bus_reset,
724         .ops_pci = &pci_bus_ops_pci,
725 };
726
727 /**
728  * @brief Detect the type of downstream bridge
729  *
730  * This function is a heuristic to detect which type of bus is downstream
731  * of a PCI-to-PCI bridge. This functions by looking for various capability
732  * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
733  * Hypertransport all seem to have appropriate capabilities.
734  *
735  * When only a PCI-Express capability is found the type
736  * is examined to see which type of bridge we have.
737  *
738  * @param dev Pointer to the device structure of the bridge.
739  * @return Appropriate bridge operations.
740  */
741 static struct device_operations *get_pci_bridge_ops(device_t dev)
742 {
743         unsigned pos;
744
745 #if CONFIG_PCIX_PLUGIN_SUPPORT == 1
746         pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
747         if (pos) {
748                 printk_debug("%s subbordinate bus PCI-X\n", dev_path(dev));
749                 return &default_pcix_ops_bus;
750         }
751 #endif
752 #if CONFIG_AGP_PLUGIN_SUPPORT == 1
753         /* How do I detect an PCI to AGP bridge? */
754 #endif
755 #if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
756         pos = 0;
757         while ((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
758                 unsigned flags;
759                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
760                 if ((flags >> 13) == 1) {
761                         /* Host or Secondary Interface */
762                         printk_debug("%s subbordinate bus Hypertransport\n",
763                                      dev_path(dev));
764                         return &default_ht_ops_bus;
765                 }
766         }
767 #endif
768 #if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
769         pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
770         if (pos) {
771                 unsigned flags;
772                 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
773                 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
774                 case PCI_EXP_TYPE_ROOT_PORT:
775                 case PCI_EXP_TYPE_UPSTREAM:
776                 case PCI_EXP_TYPE_DOWNSTREAM:
777                         printk_debug("%s subbordinate bus PCI Express\n",
778                                      dev_path(dev));
779                         return &default_pciexp_ops_bus;
780                 case PCI_EXP_TYPE_PCI_BRIDGE:
781                         printk_debug("%s subbordinate PCI\n", dev_path(dev));
782                         return &default_pci_ops_bus;
783                 default:
784                         break;
785                 }
786         }
787 #endif
788         return &default_pci_ops_bus;
789 }
790
791 /**
792  * Set up PCI device operation.  Check if it already has a driver.  If not, use
793  * find_device_operations, or set to a default based on type.
794  *
795  * @param dev Pointer to the device whose pci_ops you want to set.
796  * @see pci_drivers
797  */
798 static void set_pci_ops(struct device *dev)
799 {
800         struct pci_driver *driver;
801         if (dev->ops) {
802                 return;
803         }
804
805         /* Look through the list of setup drivers and find one for
806          * this PCI device.
807          */
808         for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
809                 if ((driver->vendor == dev->vendor) &&
810                     (driver->device == dev->device)) {
811                         dev->ops = driver->ops;
812                         printk_spew("%s [%04x/%04x] %sops\n",
813                                     dev_path(dev),
814                                     driver->vendor, driver->device,
815                                     (driver->ops->scan_bus ? "bus " : ""));
816                         return;
817                 }
818         }
819
820         /* If I don't have a specific driver use the default operations */
821         switch (dev->hdr_type & 0x7f) { /* header type */
822         case PCI_HEADER_TYPE_NORMAL:    /* standard header */
823                 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
824                         goto bad;
825                 dev->ops = &default_pci_ops_dev;
826                 break;
827         case PCI_HEADER_TYPE_BRIDGE:
828                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
829                         goto bad;
830                 dev->ops = get_pci_bridge_ops(dev);
831                 break;
832 #if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
833         case PCI_HEADER_TYPE_CARDBUS:
834                 dev->ops = &default_cardbus_ops_bus;
835                 break;
836 #endif
837         default:
838               bad:
839                 if (dev->enabled) {
840                         printk_err("%s [%04x/%04x/%06x] has unknown header "
841                                    "type %02x, ignoring.\n",
842                                    dev_path(dev),
843                                    dev->vendor, dev->device,
844                                    dev->class >> 8, dev->hdr_type);
845                 }
846         }
847         return;
848 }
849
850 /**
851  * @brief See if we have already allocated a device structure for a given devfn.
852  *
853  * Given a linked list of PCI device structures and a devfn number, find the
854  * device structure correspond to the devfn, if present. This function also
855  * removes the device structure from the linked list.
856  *
857  * @param list The device structure list.
858  * @param devfn A device/function number.
859  *
860  * @return Pointer to the device structure found or NULL if we have not
861  *         allocated a device for this devfn yet.
862  */
863 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
864 {
865         struct device *dev;
866         dev = 0;
867         for (; *list; list = &(*list)->sibling) {
868                 if ((*list)->path.type != DEVICE_PATH_PCI) {
869                         printk_err("child %s not a pci device\n",
870                                    dev_path(*list));
871                         continue;
872                 }
873                 if ((*list)->path.pci.devfn == devfn) {
874                         /* Unlink from the list. */
875                         dev = *list;
876                         *list = (*list)->sibling;
877                         dev->sibling = NULL;
878                         break;
879                 }
880         }
881
882         /* Just like alloc_dev() add the device to the list of devices on the
883          * bus. When the list of devices was formed we removed all of the
884          * parents children, and now we are interleaving static and dynamic
885          * devices in order on the bus.
886          */
887         if (dev) {
888                 struct device *child;
889                 /* Find the last child of our parent. */
890                 for (child = dev->bus->children; child && child->sibling;) {
891                         child = child->sibling;
892                 }
893                 /* Place the device on the list of children of its parent. */
894                 if (child) {
895                         child->sibling = dev;
896                 } else {
897                         dev->bus->children = dev;
898                 }
899         }
900
901         return dev;
902 }
903
904 /**
905  * @brief Scan a PCI bus.
906  *
907  * Determine the existence of a given PCI device. Allocate a new struct device
908  * if dev==NULL was passed in and the device exists in hardware.
909  *
910  * @param bus pointer to the bus structure
911  * @param devfn to look at
912  *
913  * @return The device structure for hte device (if found)
914  *         or the NULL if no device is found.
915  */
916 device_t pci_probe_dev(device_t dev, struct bus * bus, unsigned devfn)
917 {
918         u32 id, class;
919         u8 hdr_type;
920
921         /* Detect if a device is present. */
922         if (!dev) {
923                 struct device dummy;
924                 dummy.bus = bus;
925                 dummy.path.type = DEVICE_PATH_PCI;
926                 dummy.path.pci.devfn = devfn;
927                 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
928                 /* Have we found something?
929                  * Some broken boards return 0 if a slot is empty.
930                  */
931                 if ((id == 0xffffffff) || (id == 0x00000000) ||
932                     (id == 0x0000ffff) || (id == 0xffff0000)) {
933                         printk_spew("%s, bad id 0x%x\n", dev_path(&dummy), id);
934                         return NULL;
935                 }
936                 dev = alloc_dev(bus, &dummy.path);
937         } else {
938                 /* Enable/disable the device. Once we have found the device-
939                  * specific operations this operations we will disable the
940                  * device with those as well.
941                  *
942                  * This is geared toward devices that have subfunctions
943                  * that do not show up by default.
944                  *
945                  * If a device is a stuff option on the motherboard
946                  * it may be absent and enable_dev() must cope.
947                  */
948                 /* Run the magic enable sequence for the device. */
949                 if (dev->chip_ops && dev->chip_ops->enable_dev) {
950                         dev->chip_ops->enable_dev(dev);
951                 }
952                 /* Now read the vendor and device ID. */
953                 id = pci_read_config32(dev, PCI_VENDOR_ID);
954
955                 /* If the device does not have a PCI ID disable it. Possibly
956                  * this is because we have already disabled the device. But
957                  * this also handles optional devices that may not always
958                  * show up.
959                  */
960                 /* If the chain is fully enumerated quit */
961                 if ((id == 0xffffffff) || (id == 0x00000000) ||
962                     (id == 0x0000ffff) || (id == 0xffff0000)) {
963                         if (dev->enabled) {
964                                 printk_info("Disabling static device: %s\n",
965                                             dev_path(dev));
966                                 dev->enabled = 0;
967                         }
968                         return dev;
969                 }
970         }
971         /* Read the rest of the PCI configuration information. */
972         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
973         class = pci_read_config32(dev, PCI_CLASS_REVISION);
974
975         /* Store the interesting information in the device structure. */
976         dev->vendor = id & 0xffff;
977         dev->device = (id >> 16) & 0xffff;
978         dev->hdr_type = hdr_type;
979
980         /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
981         dev->class = class >> 8;
982
983         /* Architectural/System devices always need to be bus masters. */
984         if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
985                 dev->command |= PCI_COMMAND_MASTER;
986         }
987         /* Look at the vendor and device ID, or at least the header type and
988          * class and figure out which set of configuration methods to use.
989          * Unless we already have some PCI ops.
990          */
991         set_pci_ops(dev);
992
993         /* Now run the magic enable/disable sequence for the device. */
994         if (dev->ops && dev->ops->enable) {
995                 dev->ops->enable(dev);
996         }
997
998         /* Display the device. */
999         printk_debug("%s [%04x/%04x] %s%s\n",
1000                      dev_path(dev),
1001                      dev->vendor, dev->device,
1002                      dev->enabled ? "enabled" : "disabled",
1003                      dev->ops ? "" : " No operations");
1004
1005         return dev;
1006 }
1007
1008 /**
1009  * @brief Scan a PCI bus.
1010  *
1011  * Determine the existence of devices and bridges on a PCI bus. If there are
1012  * bridges on the bus, recursively scan the buses behind the bridges.
1013  *
1014  * This function is the default scan_bus() method for the root device
1015  * 'dev_root'.
1016  *
1017  * @param bus pointer to the bus structure
1018  * @param min_devfn minimum devfn to look at in the scan usually 0x00
1019  * @param max_devfn maximum devfn to look at in the scan usually 0xff
1020  * @param max current bus number
1021  *
1022  * @return The maximum bus number found, after scanning all subordinate busses
1023  */
1024 unsigned int pci_scan_bus(struct bus *bus,
1025                           unsigned min_devfn, unsigned max_devfn,
1026                           unsigned int max)
1027 {
1028         unsigned int devfn;
1029         struct device *old_devices;
1030         struct device *child;
1031
1032 #if CONFIG_PCI_BUS_SEGN_BITS
1033         printk_debug("PCI: pci_scan_bus for bus %04x:%02x\n",
1034                      bus->secondary >> 8, bus->secondary & 0xff);
1035 #else
1036         printk_debug("PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1037 #endif
1038
1039         old_devices = bus->children;
1040         bus->children = NULL;
1041
1042         post_code(0x24);
1043         /* Probe all devices/functions on this bus with some optimization for
1044          * non-existence and single function devices.
1045          */
1046         for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
1047                 struct device *dev;
1048
1049                 /* First thing setup the device structure */
1050                 dev = pci_scan_get_dev(&old_devices, devfn);
1051
1052                 /* See if a device is present and setup the device structure. */
1053                 dev = pci_probe_dev(dev, bus, devfn);
1054
1055                 /* If this is not a multi function device, or the device is
1056                  * not present don't waste time probing another function.
1057                  * Skip to next device.
1058                  */
1059                 if ((PCI_FUNC(devfn) == 0x00) &&
1060                     (!dev
1061                      || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
1062                         devfn += 0x07;
1063                 }
1064         }
1065         post_code(0x25);
1066
1067         /* Warn if any leftover static devices are are found.
1068          * There's probably a problem in the Config.lb.
1069          */
1070         if (old_devices) {
1071                 device_t left;
1072                 printk_warning("PCI: Left over static devices:\n");
1073                 for (left = old_devices; left; left = left->sibling) {
1074                         printk_warning("%s\n", dev_path(left));
1075                 }
1076                 printk_warning("PCI: Check your mainboard Config.lb.\n");
1077         }
1078
1079         /* For all children that implement scan_bus() (i.e. bridges)
1080          * scan the bus behind that child.
1081          */
1082         for (child = bus->children; child; child = child->sibling) {
1083                 max = scan_bus(child, max);
1084         }
1085
1086         /* We've scanned the bus and so we know all about what's on the other
1087          * side of any bridges that may be on this bus plus any devices.
1088          * Return how far we've got finding sub-buses.
1089          */
1090         printk_debug("PCI: pci_scan_bus returning with max=%03x\n", max);
1091         post_code(0x55);
1092         return max;
1093 }
1094
1095 /**
1096  * @brief Scan a PCI bridge and the buses behind the bridge.
1097  *
1098  * Determine the existence of buses behind the bridge. Set up the bridge
1099  * according to the result of the scan.
1100  *
1101  * This function is the default scan_bus() method for PCI bridge devices.
1102  *
1103  * @param dev Pointer to the bridge device.
1104  * @param max The highest bus number assigned up to now.
1105  * @return The maximum bus number found, after scanning all subordinate buses.
1106  */
1107 unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
1108                                 unsigned int (*do_scan_bus) (struct bus * bus,
1109                                                              unsigned min_devfn,
1110                                                              unsigned max_devfn,
1111                                                              unsigned int max))
1112 {
1113         struct bus *bus;
1114         u32 buses;
1115         u16 cr;
1116
1117         printk_spew("%s for %s\n", __func__, dev_path(dev));
1118
1119         bus = &dev->link[0];
1120         bus->dev = dev;
1121         dev->links = 1;
1122
1123         /* Set up the primary, secondary and subordinate bus numbers. We have
1124          * no idea how many buses are behind this bridge yet, so we set the
1125          * subordinate bus number to 0xff for the moment.
1126          */
1127         bus->secondary = ++max;
1128         bus->subordinate = 0xff;
1129
1130         /* Clear all status bits and turn off memory, I/O and master enables. */
1131         cr = pci_read_config16(dev, PCI_COMMAND);
1132         pci_write_config16(dev, PCI_COMMAND, 0x0000);
1133         pci_write_config16(dev, PCI_STATUS, 0xffff);
1134
1135         /* Read the existing primary/secondary/subordinate bus
1136          * number configuration.
1137          */
1138         buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
1139
1140         /* Configure the bus numbers for this bridge: the configuration
1141          * transactions will not be propagated by the bridge if it is not
1142          * correctly configured.
1143          */
1144         buses &= 0xff000000;
1145         buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1146                   ((unsigned int)(bus->secondary) << 8) |
1147                   ((unsigned int)(bus->subordinate) << 16));
1148         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1149
1150         /* Now we can scan all subordinate buses
1151          * i.e. the bus behind the bridge.
1152          */
1153         max = do_scan_bus(bus, 0x00, 0xff, max);
1154
1155         /* We know the number of buses behind this bridge. Set the subordinate
1156          * bus number to its real value.
1157          */
1158         bus->subordinate = max;
1159         buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
1160         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1161         pci_write_config16(dev, PCI_COMMAND, cr);
1162
1163         printk_spew("%s returns max %d\n", __func__, max);
1164         return max;
1165 }
1166
1167 /**
1168  * @brief Scan a PCI bridge and the buses behind the bridge.
1169  *
1170  * Determine the existence of buses behind the bridge. Set up the bridge
1171  * according to the result of the scan.
1172  *
1173  * This function is the default scan_bus() method for PCI bridge devices.
1174  *
1175  * @param dev Pointer to the bridge device.
1176  * @param max The highest bus number assigned up to now.
1177  * @return The maximum bus number found, after scanning all subordinate buses.
1178  */
1179 unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1180 {
1181         return do_pci_scan_bridge(dev, max, pci_scan_bus);
1182 }
1183
1184 /**
1185  * @brief Scan a PCI domain.
1186  *
1187  * This function is the default scan_bus() method for PCI domains.
1188  *
1189  * @param dev pointer to the domain
1190  * @param max the highest bus number assgined up to now
1191  *
1192  * @return The maximum bus number found, after scanning all subordinate busses
1193  */
1194 unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1195 {
1196         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
1197         return max;
1198 }
1199
1200 #if CONFIG_PC80_SYSTEM == 1
1201 /**
1202  * 
1203  * @brief Assign IRQ numbers
1204  *
1205  * This function assigns IRQs for all functions contained within the indicated
1206  * device address.  If the device does not exist or does not require interrupts
1207  * then this function has no effect.
1208  *
1209  * This function should be called for each PCI slot in your system.
1210  *
1211  * @param bus
1212  * @param slot
1213  * @param pIntAtoD is an array of IRQ #s that are assigned to PINTA through
1214  *        PINTD of this slot.  The particular irq #s that are passed in 
1215  *        depend on the routing inside your southbridge and on your 
1216  *        motherboard.
1217  */
1218 void pci_assign_irqs(unsigned bus, unsigned slot,
1219         const unsigned char pIntAtoD[4])
1220 {
1221         unsigned int funct;
1222         device_t pdev;
1223         u8 line;
1224         u8 irq;
1225         u8 readback;
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                 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1250                 printk_debug("  Readback = %d\n", readback);
1251 #endif
1252
1253                 // Change to level triggered
1254                 i8259_configure_irq_trigger(pIntAtoD[line - 1], IRQ_LEVEL_TRIGGERED);
1255         }
1256 }
1257 #endif
1258