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