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