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