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