minor reformat
[coreboot.git] / src / devices / pci_device.c
1 /*
2  *      PCI Bus Services, see include/linux/pci.h for further explanation.
3  *
4  *      Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5  *      David Mosberger-Tang
6  *
7  *      Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8  *      
9  *      Copyright 2003 -- Eric Biederman <ebiederman@lnxi.com>
10  */
11
12 #include <console/console.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <bitops.h>
16 #include <string.h>
17 #include <arch/io.h>
18 #include <device/device.h>
19 #include <device/pci.h>
20 #include <device/pci_ids.h>
21 #include <device/chip.h>
22 #include <part/hard_reset.h>
23 #include <part/fallback_boot.h>
24
25 /** Given a device and register, read the size of the BAR for that register. 
26  * @param dev       Pointer to the device structure
27  * @param resource  Pointer to the resource structure
28  * @param index     Address of the pci configuration register
29  */
30 static struct resource *pci_get_resource(struct device *dev, unsigned long index)
31 {
32         struct resource *resource;
33         uint32_t addr, size, base;
34         unsigned long type;
35
36         /* Initialize the resources to nothing */
37         resource = get_resource(dev, index);
38
39         addr = pci_read_config32(dev, index);
40
41         /* FIXME: more consideration for 64-bit PCI devices,
42          * we currently detect their size but otherwise
43          * treat them as 32-bit resources
44          */
45         /* get the size */
46         pci_write_config32(dev, index, ~0);
47         size = pci_read_config32(dev,  index);
48
49         /* get the minimum value the bar can be set to */
50         pci_write_config32(dev, index, 0);
51         base = pci_read_config32(dev, index);
52
53         /* restore addr */
54         pci_write_config32(dev, index, addr);
55
56         /*
57          * some broken hardware has read-only registers that do not 
58          * really size correctly. You can tell this if addr == size
59          * Example: the acer m7229 has BARs 1-4 normally read-only. 
60          * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
61          * by writing 0xffffffff to it, it will read back as 0x1f1 -- a 
62          * violation of the spec. 
63          * We catch this case and ignore it by settting size and type to 0.
64          * This incidentally catches the common case where registers 
65          * read back as 0 for both address and size. 
66          */
67         if ((addr == size) && (addr == base)) {
68                 if (size != 0) {
69                         printk_debug(
70                                 "%s register %02x(%08x), read-only ignoring it\n",
71                                 dev_path(dev),
72                                 index, addr);
73                 }
74                 resource->flags = 0;
75         }
76         /* Now compute the actual size, See PCI Spec 6.2.5.1 ...  */
77         else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
78                 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
79                 /* BUG! Top 16 bits can be zero (or not) 
80                  * So set them to 0xffff so they go away ...
81                  */
82                 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
83                 resource->align = log2(resource->size);
84                 resource->gran = resource->align;
85                 resource->flags |= IORESOURCE_IO;
86                 resource->limit = 0xffff;
87         } 
88         else {
89                 /* A Memory mapped base address */
90                 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
91                 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
92                 resource->align = log2(resource->size);
93                 resource->gran = resource->align;
94                 resource->flags |= IORESOURCE_MEM;
95                 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
96                         resource->flags |= IORESOURCE_PREFETCH;
97                 }
98                 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
99                 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
100                         /* 32bit limit */
101                         resource->limit = 0xffffffffUL;
102                 }
103                 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
104                         /* 1MB limit */
105                         resource->limit = 0x000fffffUL;
106                 }
107                 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
108                         unsigned long index_hi;
109                         /* 64bit limit 
110                          * For now just treat this as a 32bit limit
111                          */
112                         index_hi = index + 4;
113                         resource->limit = 0xffffffffUL;
114                         resource->flags |= IORESOURCE_PCI64;
115                         addr = pci_read_config32( dev, index_hi);
116                         /* get the extended size */
117                         pci_write_config32(dev, index_hi, 0xffffffffUL);
118                         size = pci_read_config32( dev, index_hi);
119
120                         /* get the minimum value the bar can be set to */
121                         pci_write_config32(dev, index_hi, 0);
122                         base = pci_read_config32(dev,  index_hi);
123
124                         /* restore addr */
125                         pci_write_config32(dev, index_hi, addr);
126                         
127                         if ((size == 0xffffffff) && (base == 0)) {
128                                 /* Clear the top half of the bar */
129                                 pci_write_config32(dev, index_hi, 0);
130                         }
131                         else {
132                                 printk_err("%s Unable to handle 64-bit address\n",
133                                         dev_path(dev));
134                                 resource->flags = IORESOURCE_PCI64;
135                         }
136                 } 
137                 else {
138                         /* Invalid value */
139                         resource->flags = 0;
140                 }
141         }
142         /* dev->size holds the flags... */
143         return resource;
144 }
145
146 /** Read the base address registers for a given device. 
147  * @param dev Pointer to the dev structure
148  * @param howmany How many registers to read (6 for device, 2 for bridge)
149  */
150 static void pci_read_bases(struct device *dev, unsigned int howmany)
151 {
152         unsigned long index;
153
154         for (index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
155                 struct resource *resource;
156                 resource = pci_get_resource(dev, index);
157                 index += (resource->flags & IORESOURCE_PCI64)?8:4;
158         }
159         compact_resources(dev);
160 }
161
162 static void pci_bridge_read_bases(struct device *dev)
163 {
164         struct resource *resource;
165
166         /* FIXME handle bridges without some of the optional resources */
167
168         /* Initialize the io space constraints on the current bus */
169         resource = get_resource(dev, PCI_IO_BASE);
170         resource->size  = 0;
171         resource->align = log2(PCI_IO_BRIDGE_ALIGN);
172         resource->gran  = log2(PCI_IO_BRIDGE_ALIGN);
173         resource->limit = 0xffffUL;
174         resource->flags |= IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
175         compute_allocate_resource(&dev->link[0], resource,
176                                   IORESOURCE_IO, IORESOURCE_IO);
177
178         /* Initiliaze the prefetchable memory constraints on the current bus */
179         resource = get_resource(dev, PCI_PREF_MEMORY_BASE);
180         resource->size = 0;
181         resource->align = log2(PCI_MEM_BRIDGE_ALIGN);
182         resource->gran  = log2(PCI_MEM_BRIDGE_ALIGN);
183         resource->limit = 0xffffffffUL;
184         resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
185         resource->index = PCI_PREF_MEMORY_BASE;
186         compute_allocate_resource(&dev->link[0], resource,
187                                   IORESOURCE_MEM | IORESOURCE_PREFETCH, 
188                                   IORESOURCE_MEM | IORESOURCE_PREFETCH);
189
190         /* Initialize the memory resources on the current bus */
191         resource = get_resource(dev, PCI_MEMORY_BASE);
192         resource->size = 0;
193         resource->align = log2(PCI_MEM_BRIDGE_ALIGN);
194         resource->gran  = log2(PCI_MEM_BRIDGE_ALIGN);
195         resource->limit = 0xffffffffUL;
196         resource->flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
197         compute_allocate_resource(&dev->link[0], resource,
198                                   IORESOURCE_MEM | IORESOURCE_PREFETCH,
199                                   IORESOURCE_MEM);
200
201         compact_resources(dev);
202 }
203
204 void pci_dev_read_resources(struct device *dev)
205 {
206         uint32_t addr;
207
208         pci_read_bases(dev, 6);
209
210         addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
211         dev->rom_address = (addr == 0xffffffff)? 0 : addr;
212 }
213
214 void pci_bus_read_resources(struct device *dev)
215 {
216         uint32_t addr;
217
218         pci_bridge_read_bases(dev);
219         pci_read_bases(dev, 2);
220         
221         addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
222         dev->rom_address = (addr == 0xffffffff)? 0 : addr;
223 }
224
225 /**
226  * @brief round a number up to an alignment. 
227  * @param val the starting value
228  * @param roundup Alignment as a power of two
229  * @returns rounded up number
230  */
231 static unsigned long round(unsigned long val, unsigned long roundup)
232 {
233         /* ROUNDUP MUST BE A POWER OF TWO. */
234         unsigned long inverse;
235         inverse = ~(roundup - 1);
236         val += (roundup - 1);
237         val &= inverse;
238         return val;
239 }
240
241 static void pci_set_resource(struct device *dev, struct resource *resource)
242 {
243         unsigned long base, limit;
244         unsigned char buf[10];
245         unsigned long gran;
246
247         /* Make certain the resource has actually been set */
248
249         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
250                 printk_err("ERROR: %s %02x not allocated\n",
251                            dev_path(dev), resource->index);
252                 return;
253         }
254
255         /* If I have already stored this resource don't worry about it */
256         if (resource->flags & IORESOURCE_STORED) {
257                 return;
258         }
259
260         /* Only handle PCI memory and IO resources for now */
261         if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
262                 return;
263
264         if (resource->flags & IORESOURCE_MEM) {
265                 dev->command |= PCI_COMMAND_MEMORY;
266         }
267         if (resource->flags & IORESOURCE_IO) {
268                 dev->command |= PCI_COMMAND_IO;
269         }
270         if (resource->flags & IORESOURCE_PCI_BRIDGE) {
271                 dev->command |= PCI_COMMAND_MASTER;
272         }
273
274         /* Get the base address */
275         base = resource->base;
276
277         /* Get the resource granularity */
278         gran = 1UL << resource->gran;
279
280         /* For a non bridge resource granularity and alignment are the same.
281          * For a bridge resource align is the largest needed alignment below
282          * the bridge.  While the granularity is simply how many low bits of the
283          * address cannot be set.
284          */
285         
286         /* Get the limit (rounded up) */
287         limit = base + round(resource->size, gran) - 1UL;
288         
289         /* Now store the resource */
290         resource->flags |= IORESOURCE_STORED;
291         if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
292                 /* some chipsets allow us to set/clear the IO bit. 
293                  * (e.g. VIA 82c686a.) So set it to be safe) */
294                 limit = base + resource->size -1;
295                 if (resource->flags & IORESOURCE_IO) {
296                         base |= PCI_BASE_ADDRESS_SPACE_IO;
297                 }
298                 pci_write_config32(dev, resource->index, base & 0xffffffff);
299                 if (resource->flags & IORESOURCE_PCI64) {
300                         /* FIXME handle real 64bit base addresses */
301                         pci_write_config32(dev, resource->index + 4, 0);
302                 }
303         } else if (resource->index == PCI_IO_BASE) {
304                 /* set the IO ranges
305                  * WARNING: we don't really do 32-bit addressing for IO yet! 
306                  */
307                 compute_allocate_resource(&dev->link[0], resource, 
308                                           IORESOURCE_IO, IORESOURCE_IO);
309                 pci_write_config8(dev, PCI_IO_BASE,  base >> 8);
310                 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
311                 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
312                 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
313         } else if (resource->index == PCI_MEMORY_BASE) {
314                 /* set the memory range */
315                 compute_allocate_resource(&dev->link[0], resource,
316                                           IORESOURCE_MEM | IORESOURCE_PREFETCH, 
317                                           IORESOURCE_MEM);
318                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
319                 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
320         } else if (resource->index == PCI_PREF_MEMORY_BASE) {
321                 /* set the prefetchable memory range
322                  * WARNING: we don't really do 64-bit addressing for
323                  * prefetchable memory yet! */
324                 compute_allocate_resource(&dev->link[0], resource,
325                                           IORESOURCE_MEM | IORESOURCE_PREFETCH, 
326                                           IORESOURCE_MEM | IORESOURCE_PREFETCH);
327                 pci_write_config16(dev, PCI_PREF_MEMORY_BASE,  base >> 16);
328                 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
329                 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
330                 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
331         } else {
332                 /* Don't let me think I stored the resource */
333                 resource->flags &= ~IORESOURCE_STORED;
334                 printk_err("ERROR: invalid resource->index %x\n",
335                            resource->index);
336         }
337
338         buf[0] = '\0';
339         if (resource->flags & IORESOURCE_PCI_BRIDGE) {
340                 sprintf(buf, "bus %d ", dev->link[0].secondary);
341         }
342         printk_debug("%s %02x <- [0x%08lx - 0x%08lx] %s%s\n",
343                      dev_path(dev), resource->index, resource->base,
344                      limit, buf,
345                      (resource->flags & IORESOURCE_IO)? "io":
346                      (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
347         return;
348 }
349
350 void pci_dev_set_resources(struct device *dev)
351 {
352         struct resource *resource, *last;
353         unsigned link;
354         uint8_t line;
355
356         last = &dev->resource[dev->resources];
357         for (resource = &dev->resource[0]; resource < last; resource++) {
358                 pci_set_resource(dev, resource);
359         }
360
361         for (link = 0; link < dev->links; link++) {
362                 struct bus *bus;
363                 bus = &dev->link[link];
364                 if (bus->children) {
365                         assign_resources(bus);
366                 }
367         }
368
369         /* set a default latency timer */
370         pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
371
372         /* set a default secondary latency timer */
373         if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
374                 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
375         }
376
377         /* zero the irq settings */
378         line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
379         if (line) {
380                 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
381         }
382         /* set the cache line size, so far 64 bytes is good for everyone */
383         pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
384 }
385
386 void pci_dev_enable_resources(struct device *dev)
387 {
388         uint16_t command;
389         command = pci_read_config16(dev, PCI_COMMAND);
390         command |= dev->command;
391         command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */
392         printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
393         pci_write_config16(dev, PCI_COMMAND, command);
394
395         enable_childrens_resources(dev);
396 }
397
398 void pci_bus_enable_resources(struct device *dev)
399 {
400         uint16_t ctrl;
401         ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
402         ctrl |= dev->link[0].bridge_ctrl;
403         ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
404         printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
405         pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
406
407         pci_dev_enable_resources(dev);
408 }
409
410 /** Default device operation for PCI devices */
411 struct device_operations default_pci_ops_dev = {
412         .read_resources   = pci_dev_read_resources,
413         .set_resources    = pci_dev_set_resources,
414         .enable_resources = pci_dev_enable_resources,
415         .init             = 0,
416         .scan_bus         = 0,
417 };
418
419 /** Default device operations for PCI bridges */
420 struct device_operations default_pci_ops_bus = {
421         .read_resources   = pci_bus_read_resources,
422         .set_resources    = pci_dev_set_resources,
423         .enable_resources = pci_bus_enable_resources,
424         .init             = 0,
425         .scan_bus         = pci_scan_bridge,
426 };
427
428 /**
429  * @brief Set up PCI device operation
430  *
431  *
432  * @param dev 
433  *
434  * @see pci_drivers
435  */
436 static void set_pci_ops(struct device *dev)
437 {
438         struct pci_driver *driver;
439
440         if (dev->ops) {
441                 return;
442         }
443
444         /* Look through the list of setup drivers and find one for
445          * this pci device */
446         for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
447                 if ((driver->vendor == dev->vendor) &&
448                     (driver->device == dev->device)) {
449                         dev->ops = driver->ops;
450
451                         printk_debug("%s [%04x/%04x] %sops\n", dev_path(dev),
452                                      driver->vendor, driver->device,
453                                      (driver->ops->scan_bus?"bus ":""));
454
455                         return;
456                 }
457         }
458
459 #if 0
460         extern struct pci_driver generic_vga_driver;
461         /* TODO: Install generic VGA driver for VGA devices, base on the
462          * class ID */
463         if ((dev->class >> 8)  == PCI_CLASS_DISPLAY_VGA) {
464                 printk_debug("setting up generic VGA driver\n");
465                 dev->ops = generic_vga_driver.ops;
466                 return;
467         }
468 #endif
469
470         /* If I don't have a specific driver use the default operations */
471         switch(dev->hdr_type & 0x7f) {  /* header type */
472         case PCI_HEADER_TYPE_NORMAL:    /* standard header */
473                 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
474                         goto bad;
475                 dev->ops = &default_pci_ops_dev;
476                 break;
477         case PCI_HEADER_TYPE_BRIDGE:
478                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
479                         goto bad;
480                 dev->ops = &default_pci_ops_bus;
481                 break;
482         default:
483         bad:
484                 if (dev->enabled) {
485                         printk_err("%s [%04x/%04x/%06x] has unknown header "
486                                    "type %02x, ignoring.\n",
487                                    dev_path(dev),
488                                    dev->vendor, dev->device, 
489                                    dev->class >> 8, dev->hdr_type);
490                 }
491         }
492         return;
493 }
494
495 /**
496  * @brief Find a specific device structure on a list of device structures
497  *
498  * Given a linked list of PCI device structures and a devfn number, find the
499  * device structure correspond to the devfn.
500  *
501  * @param list the device structure list
502  * @param devfn a device/function number
503  *
504  * @return pointer to the device structure found
505  */
506 static struct device *pci_scan_get_dev(struct device **list,
507                                        unsigned int devfn)
508 {
509         struct device *dev = 0;
510
511         for (; *list; list = &(*list)->sibling) {
512                 if ((*list)->path.type != DEVICE_PATH_PCI) {
513                         printk_err("child %s not a pci device\n",
514                                    dev_path(*list));
515                         continue;
516                 }
517                 if ((*list)->path.u.pci.devfn == devfn) {
518                         /* Unlink from the list */
519                         dev = *list;
520                         *list = (*list)->sibling;
521                         dev->sibling = 0;
522                         break;
523                 }
524         }
525
526         /* FIXME: why are we doing this ? Isn't there some order between the
527          * structures before ? */
528         if (dev) {
529                 device_t child;
530                 /* Find the last child of our parent */
531                 for (child = dev->bus->children; child && child->sibling; ) {
532                         child = child->sibling;
533                 }
534                 /* Place the device on the list of children of it's parent. */
535                 if (child) {
536                         child->sibling = dev;
537                 } else {
538                         dev->bus->children = dev;
539                 }
540         }
541
542         return dev;
543 }
544
545 /**
546  * @brief Scan a PCI bus
547  *
548  * Determine the existence of devices and bridges on a PCI bus. If there are
549  * bridges on the bus, recursively scan the buses behind the bridges.
550  *
551  * This function is the default scan_bus() method for the root device
552  * 'dev_root'.
553  *
554  * @param bus pointer to the bus structure
555  * @param min_devfn minimum devfn to look at in the scan usually 0x00
556  * @param max_devfn maximum devfn to look at in the scan usually 0xff
557  * @param max current bus number
558  *
559  * @return The maximum bus number found, after scanning all subordinate busses
560  */
561 unsigned int pci_scan_bus(struct bus *bus, unsigned min_devfn,
562                           unsigned max_devfn, unsigned int max)
563 {
564         unsigned int devfn;
565         device_t dev;
566         device_t old_devices;
567         device_t child;
568
569         printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
570
571         old_devices = bus->children;
572         bus->children = 0;
573
574         post_code(0x24);
575
576         /* probe all devices/functions on this bus with some optimization for
577          * non-existence and single funcion devices */
578         for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
579                 uint32_t id, class;
580                 uint8_t hdr_type;
581
582                 /* device structures for PCI devices associated with static
583                  * devices are already created during the static device
584                  * enumeration, find out if it is the case for this devfn */
585                 dev = pci_scan_get_dev(&old_devices, devfn);
586
587                 if (!dev) {
588                         /* it's not associated with a static device, detect if
589                          * this device is present */
590                         struct device dummy;
591                         dummy.bus              = bus;
592                         dummy.path.type        = DEVICE_PATH_PCI;
593                         dummy.path.u.pci.devfn = devfn;
594                         id = pci_read_config32(&dummy, PCI_VENDOR_ID);
595                         /* some broken boards return 0 if a slot is empty: */
596                         if ((id == 0xffffffff) || (id == 0x00000000) || 
597                             (id == 0x0000ffff) || (id == 0xffff0000)) {
598                                 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n",
599                                             devfn, id);
600                                 if (PCI_FUNC(devfn) == 0x00) {
601                                         /* if this is a function 0 device and
602                                          * it is not present, skip to next
603                                          * device */
604                                         devfn += 0x07;
605                                 }
606                                 /* this function in a multi function device is
607                                  * not present, skip to next function */
608                                 continue;
609                         }
610                         dev = alloc_dev(bus, &dummy.path);
611                 } else {
612                         /* Run the magic enable/disable sequence for the
613                          * device */
614                         /* FIXME: What happen if this PCI device listed as
615                          * static device but does not exist ? This calls
616                          * some arbitray code without any justification
617                          * Also, it calls the enable function regardlessly
618                          * the value of dev->enabled  */
619                         if (dev->chip && dev->chip->control &&
620                             dev->chip->control->enable_dev) {
621                                 int enabled  = dev->enabled;
622                                 dev->enabled = 1;
623                                 dev->chip->control->enable_dev(dev);
624                                 dev->enabled = enabled;
625                         }
626                         /* Now read the vendor and device id */
627                         id = pci_read_config32(dev, PCI_VENDOR_ID);
628                 }
629                 /* Read the rest of the pci configuration information */
630                 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
631                 class = pci_read_config32(dev, PCI_CLASS_REVISION);
632
633                 /* Store the interesting information in the device structure */
634                 dev->vendor = id & 0xffff;
635                 dev->device = (id >> 16) & 0xffff;
636                 dev->hdr_type = hdr_type;
637                 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
638                 dev->class = class >> 8;
639
640                 /* Look at the vendor and device id, or at least the 
641                  * header type and class and figure out which set of
642                  * configuration methods to use.  Unless we already
643                  * have some pci ops.
644                  */
645                 set_pci_ops(dev);
646                 /* Error if we don't have some pci operations for it */
647                 if (!dev->ops) {
648                         printk_err("%s No device operations\n",
649                                    dev_path(dev));
650                         continue;
651                 }
652
653                 /* Now run the magic enable/disable sequence for the device */
654                 if (dev->ops && dev->ops->enable) {
655                         dev->ops->enable(dev);
656                 } else if (dev->chip && dev->chip->control &&
657                            dev->chip->control->enable_dev) {
658                         dev->chip->control->enable_dev(dev);
659                 }
660
661                 printk_debug("%s [%04x/%04x] %s\n", 
662                              dev_path(dev),
663                              dev->vendor, dev->device, 
664                              dev->enabled?"enabled": "disabled");
665
666                 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
667                         /* if this is not a multi function device, don't
668                          * waste time probe another function.
669                          * Skip to next device. */
670                         devfn += 0x07;
671                 }
672         }
673         post_code(0x25);
674
675         /* if a child provides scan_bus(), for example a bridge, scan
676          * buses behind that child */
677         for (child = bus->children; child; child = child->sibling) {
678                 if (!child->ops->scan_bus) {
679                         continue;
680                 }
681                 max = child->ops->scan_bus(child, max);
682         }
683
684         /*
685          * We've scanned the bus and so we know all about what's on
686          * the other side of any bridges that may be on this bus plus
687          * any devices.
688          *
689          * Return how far we've got finding sub-buses.
690          */
691         printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
692         post_code(0x55);
693         return max;
694 }
695
696 /**
697  * @brief Scan a PCI bridge and the buses behind the bridge.
698  *
699  * Determine the existence of buses behind the bridge. Set up the bridge
700  * according to the result of the scan.
701  *
702  * This function is the default scan_bus() method for PCI bridge devices.
703  *
704  * @param dev pointer to the bridge device
705  * @param max the highest bus number assgined up to now
706  *
707  * @return The maximum bus number found, after scanning all subordinate busses
708  */
709 unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
710 {
711         struct bus *bus;
712         uint32_t buses;
713         uint16_t cr;
714
715         bus = &dev->link[0];
716         dev->links = 1;
717
718         /* Set up the primary, secondary and subordinate bus numbers. We have
719          * no idea how many buses are behind this bridge yet, so we set the
720          * subordinate bus number to 0xff for the moment. */
721         bus->secondary = ++max;
722         bus->subordinate = 0xff;
723
724         /* Clear all status bits and turn off memory, I/O and master enables. */
725         cr = pci_read_config16(dev, PCI_COMMAND);
726         pci_write_config16(dev, PCI_COMMAND, 0x0000);
727         pci_write_config16(dev, PCI_STATUS, 0xffff);
728
729         /* Read the existing primary/secondary/subordinate bus
730          * number configuration. */
731         buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
732
733         /* Configure the bus numbers for this bridge: the configuration
734          * transactions will not be propagated by the bridge if it is not
735          * correctly configured */
736         buses &= 0xff000000;
737         buses |= (((unsigned int) (dev->bus->secondary) << 0) |
738                   ((unsigned int) (bus->secondary) << 8) |
739                   ((unsigned int) (bus->subordinate) << 16));
740         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
741         
742         /* Now we can scan all subordinate buses i.e. the buses behind the
743          * bridge */
744         max = pci_scan_bus(bus, 0x00, 0xff, max);
745         
746         /* We know the number of buses behind this bridge. Set the subordinate
747          * bus number to its real value */
748         bus->subordinate = max;
749         buses = (buses & 0xff00ffff) |
750                 ((unsigned int) (bus->subordinate) << 16);
751         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
752         pci_write_config16(dev, PCI_COMMAND, cr);
753
754         printk_spew("%s returns max %d\n", __FUNCTION__, max);
755         return max;
756 }
757
758 /*
759     Tell the EISA int controller this int must be level triggered
760     THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
761 */
762 static void pci_level_irq(unsigned char intNum)
763 {
764         unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
765
766         printk_spew("%s: current ints are 0x%x\n", __FUNCTION__, intBits);
767         intBits |= (1 << intNum);
768
769         printk_spew("%s: try to set ints 0x%x\n", __FUNCTION__, intBits);
770
771         // Write new values
772         outb((unsigned char) intBits, 0x4d0);
773         outb((unsigned char) (intBits >> 8), 0x4d1);
774
775         /* this seems like an error but is not ... */
776 #if 0
777         if (inb(0x4d0) != (intBits & 0xf)) {
778           printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
779                      __FUNCTION__, intBits &0xf, inb(0x4d0));
780         }
781         if (inb(0x4d1) != ((intBits >> 8) & 0xf)) {
782           printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
783                      __FUNCTION__, (intBits>>8) &0xf, inb(0x4d1));
784         }
785 #endif
786 }
787
788 /*
789     This function assigns IRQs for all functions contained within
790     the indicated device address.  If the device does not exist or does
791     not require interrupts then this function has no effect.
792
793     This function should be called for each PCI slot in your system.  
794
795     pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
796     this slot.  
797     The particular irq #s that are passed in depend on the routing inside
798     your southbridge and on your motherboard.
799
800     -kevinh@ispiri.com
801 */
802 void pci_assign_irqs(unsigned bus, unsigned slot,
803         const unsigned char pIntAtoD[4])
804 {
805         unsigned functNum;
806         device_t pdev;
807         unsigned char line;
808         unsigned char irq;
809         unsigned char readback;
810
811         /* Each slot may contain up to eight functions */
812         for (functNum = 0; functNum < 8; functNum++) {
813                 pdev = dev_find_slot(bus, (slot << 3) + functNum);
814
815                 if (pdev) {
816                   line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
817
818                         // PCI spec says all other values are reserved 
819                         if ((line >= 1) && (line <= 4)) {
820                                 irq = pIntAtoD[line - 1];
821
822                                 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
823                                         irq, bus, slot, functNum);
824
825                                 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
826                                         pIntAtoD[line - 1]);
827
828                                 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
829                                 printk_debug("  Readback = %d\n", readback);
830
831                                 // Change to level triggered
832                                 pci_level_irq(pIntAtoD[line - 1]);
833                         }
834                 }
835         }
836 }