better embedded ROM support, done blindly
[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 <part/hard_reset.h>
22 #include <part/fallback_boot.h>
23 #include <delay.h>
24
25 static uint8_t pci_moving_config8(struct device *dev, unsigned reg)
26 {
27         uint8_t value, ones, zeroes;
28         value = pci_read_config8(dev, reg);
29         
30         pci_write_config8(dev, reg, 0xff);
31         ones = pci_read_config8(dev, reg);
32
33         pci_write_config8(dev, reg, 0x00);
34         zeroes = pci_read_config8(dev, reg);
35
36         pci_write_config8(dev, reg, value);
37
38         return ones ^ zeroes;
39 }
40
41 static uint16_t pci_moving_config16(struct device *dev, unsigned reg)
42 {
43         uint16_t value, ones, zeroes;
44         value = pci_read_config16(dev, reg);
45         
46         pci_write_config16(dev, reg, 0xffff);
47         ones = pci_read_config16(dev, reg);
48
49         pci_write_config16(dev, reg, 0x0000);
50         zeroes = pci_read_config16(dev, reg);
51
52         pci_write_config16(dev, reg, value);
53
54         return ones ^ zeroes;
55 }
56
57 static uint32_t pci_moving_config32(struct device *dev, unsigned reg)
58 {
59         uint32_t value, ones, zeroes;
60         value = pci_read_config32(dev, reg);
61         
62         pci_write_config32(dev, reg, 0xffffffff);
63         ones = pci_read_config32(dev, reg);
64
65         pci_write_config32(dev, reg, 0x00000000);
66         zeroes = pci_read_config32(dev, reg);
67
68         pci_write_config32(dev, reg, value);
69
70         return ones ^ zeroes;
71 }
72
73 unsigned pci_find_capability(device_t dev, unsigned cap)
74 {
75         unsigned pos;
76         pos = 0;
77         switch(dev->hdr_type & 0x7f) {
78         case PCI_HEADER_TYPE_NORMAL:
79         case PCI_HEADER_TYPE_BRIDGE:
80                 pos = PCI_CAPABILITY_LIST;
81                 break;
82         }
83         if (pos > PCI_CAP_LIST_NEXT) {
84                 pos = pci_read_config8(dev, pos);
85         }
86         while (pos != 0) {   /* loop through the linked list */
87                 int this_cap;
88                 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
89                 if (this_cap == cap) {
90                         return pos;
91                 }
92         }
93         return 0;
94 }
95
96 /** Given a device and register, read the size of the BAR for that register. 
97  * @param dev       Pointer to the device structure
98  * @param resource  Pointer to the resource structure
99  * @param index     Address of the pci configuration register
100  */
101 struct resource *pci_get_resource(struct device *dev, unsigned long index)
102 {
103         struct resource *resource;
104         unsigned long value, attr;
105         resource_t  moving, limit;
106
107         /* Initialize the resources to nothing */
108         resource = new_resource(dev, index);
109
110         /* Get the initial value */
111         value = pci_read_config32(dev, index);
112
113         /* See which bits move */
114         moving = pci_moving_config32(dev, index);
115
116         /* Initialize attr to the bits that do not move */
117         attr = value & ~moving;
118
119         /* If it is a 64bit resource look at the high half as well */
120         if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
121             ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64))
122         {
123                 /* Find the high bits that move */
124                 moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
125         }
126         /* Find the resource constraints. 
127          *
128          * Start by finding the bits that move. From there:
129          * - Size is the least significant bit of the bits that move.
130          * - Limit is all of the bits that move plus all of the lower bits.
131          * See PCI Spec 6.2.5.1 ...
132          */
133         limit = 0;
134         if (moving) {
135                 resource->size = 1;
136                 resource->align = resource->gran = 0;
137                 while (!(moving & resource->size)) {
138                         resource->size <<= 1;
139                         resource->align += 1;
140                         resource->gran  += 1;
141                 }
142                 resource->limit = limit = moving | (resource->size - 1);
143         }
144         /*
145          * some broken hardware has read-only registers that do not 
146          * really size correctly.
147          * Example: the acer m7229 has BARs 1-4 normally read-only. 
148          * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
149          * by writing 0xffffffff to it, it will read back as 0x1f1 -- a 
150          * violation of the spec. 
151          * We catch this case and ignore it by observing which bits move,
152          * This also catches the common case unimplemented registers
153          * that always read back as 0.
154          */
155         if (moving == 0) {
156                 if (value != 0) {
157                         printk_debug("%s register %02x(%08x), read-only ignoring it\n",
158                                      dev_path(dev), index, value);
159                 }
160                 resource->flags = 0;
161         } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
162                 /* An I/O mapped base address */
163                 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
164                 resource->flags |= IORESOURCE_IO;
165                 /* I don't want to deal with 32bit I/O resources */
166                 resource->limit = 0xffff;
167         } else {
168                 /* A Memory mapped base address */
169                 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
170                 resource->flags |= IORESOURCE_MEM;
171                 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
172                         resource->flags |= IORESOURCE_PREFETCH;
173                 }
174                 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
175                 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
176                         /* 32bit limit */
177                         resource->limit = 0xffffffffUL;
178                 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
179                         /* 1MB limit */
180                         resource->limit = 0x000fffffUL;
181                 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
182                         /* 64bit limit */
183                         resource->limit = 0xffffffffffffffffULL;
184                         resource->flags |= IORESOURCE_PCI64;
185                 } else {
186                         /* Invalid value */
187                         resource->flags = 0;
188                 }
189         }
190         /* Don't let the limit exceed which bits can move */
191         if (resource->limit > limit) {
192                 resource->limit = limit;
193         }
194 #if 0
195         if (resource->flags) {
196                 printk_debug("%s %02x ->",
197                              dev_path(dev), resource->index);
198                 printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n",
199                              value, zeroes, ones, attr);
200                 printk_debug(
201                         "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s%s\n ",
202                         dev_path(dev),
203                         resource->index,
204                         resource->size, resource->limit,
205                         (resource->flags == 0) ? "unused":
206                         (resource->flags & IORESOURCE_IO)? "io":
207                         (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem",
208                         (resource->flags & IORESOURCE_PCI64)?"64":"");
209         }
210 #endif
211
212         return resource;
213 }
214
215 static void pci_get_rom_resource(struct device *dev, unsigned long index)
216 {
217         struct resource *resource;
218         unsigned long value;
219         resource_t  moving, limit;
220
221         /* Initialize the resources to nothing */
222         resource = new_resource(dev, index);
223
224         /* for on board device with embedded ROM image, the ROM image is at
225          * fixed address specified in the Config.lb, the dev->rom_address is
226          * inited by driver_pci_onboard_ops::enable_dev() */
227         if ((dev->on_mainboard) && (dev->rom_address == 0)) {
228                 resource->base   = dev->rom_address; 
229                 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
230                         IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
231                 return;
232         }
233
234         /* Get the initial value */
235         value = pci_read_config32(dev, index);
236
237         /* See which bits move */
238         moving = pci_moving_config32(dev, index);
239         /* clear the Enable bit */
240         moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
241
242         /* Find the resource constraints. 
243          *
244          * Start by finding the bits that move. From there:
245          * - Size is the least significant bit of the bits that move.
246          * - Limit is all of the bits that move plus all of the lower bits.
247          * See PCI Spec 6.2.5.1 ...
248          */
249         limit = 0;
250
251         if (moving) {
252                 resource->size = 1;
253                 resource->align = resource->gran = 0;
254                 while (!(moving & resource->size)) {
255                         resource->size <<= 1;
256                         resource->align += 1;
257                         resource->gran  += 1;
258                 }
259                 resource->limit = limit = moving | (resource->size - 1);
260         }
261
262         if (moving == 0) {
263                 if (value != 0) {
264                         printk_debug("%s register %02x(%08x), read-only ignoring it\n",
265                                      dev_path(dev), index, value);
266                 }
267                 resource->flags = 0;
268         } else {
269                 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
270         }
271 }
272
273 /** Read the base address registers for a given device. 
274  * @param dev Pointer to the dev structure
275  * @param howmany How many registers to read (6 for device, 2 for bridge)
276  */
277 static void pci_read_bases(struct device *dev, unsigned int howmany, unsigned long rom)
278 {
279         unsigned long index;
280
281         for (index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
282                 struct resource *resource;
283                 resource = pci_get_resource(dev, index);
284                 index += (resource->flags & IORESOURCE_PCI64)?8:4;
285         }
286         if (rom)
287                 pci_get_rom_resource(dev, rom);
288
289         compact_resources(dev);
290 }
291
292 static void pci_set_resource(struct device *dev, struct resource *resource);
293
294 static void pci_record_bridge_resource( struct device *dev, resource_t moving,
295                                         unsigned index, unsigned long mask,
296                                         unsigned long type)
297 {
298         /* Initiliaze the constraints on the current bus */
299         struct resource *resource;
300         resource = 0;
301         if (moving) {
302                 unsigned long gran;
303                 resource_t step;
304                 resource = new_resource(dev, index);
305                 resource->size = 0;
306                 gran = 0;
307                 step = 1;
308                 while((moving & step) == 0) {
309                         gran += 1;
310                         step <<= 1;
311                 }
312                 resource->gran = gran;
313                 resource->align = gran;
314                 resource->limit = moving | (step - 1);
315                 resource->flags = type | IORESOURCE_PCI_BRIDGE;
316                 compute_allocate_resource(&dev->link[0], resource, mask, type);
317                 /* If there is nothing behind the resource,
318                  * clear it and forget it.
319                  */
320                 if (resource->size == 0) {
321                         resource->base = moving;
322                         resource->flags |= IORESOURCE_ASSIGNED;
323                         resource->flags &= ~IORESOURCE_STORED;
324                         pci_set_resource(dev, resource);
325                         resource->flags = 0;
326                 }
327         }
328         return;
329 }
330
331 static void pci_bridge_read_bases(struct device *dev)
332 {
333         resource_t moving_base, moving_limit, moving;
334
335         /* See if the bridge I/O resources are implemented */
336         moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
337         moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
338
339         moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
340         moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
341
342         moving = moving_base & moving_limit;
343
344         /* Initialize the io space constraints on the current bus */
345         pci_record_bridge_resource(dev, moving, PCI_IO_BASE,
346                                    IORESOURCE_IO, IORESOURCE_IO);
347
348         /* See if the bridge prefmem resources are implemented */
349         moving_base =  ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
350         moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
351
352         moving_limit =  ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
353         moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
354         
355         moving = moving_base & moving_limit;
356         /* Initiliaze the prefetchable memory constraints on the current bus */
357         pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE, 
358                                    IORESOURCE_MEM | IORESOURCE_PREFETCH,
359                                    IORESOURCE_MEM | IORESOURCE_PREFETCH);
360
361         /* See if the bridge mem resources are implemented */
362         moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
363         moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
364
365         moving = moving_base & moving_limit;
366
367         /* Initialize the memory resources on the current bus */
368         pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE, 
369                                    IORESOURCE_MEM | IORESOURCE_PREFETCH,
370                                    IORESOURCE_MEM);
371
372         compact_resources(dev);
373 }
374
375 void pci_dev_read_resources(struct device *dev)
376 {
377         uint32_t addr;
378
379         pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
380 }
381
382 void pci_bus_read_resources(struct device *dev)
383 {
384         uint32_t addr;
385
386         pci_bridge_read_bases(dev);
387         pci_read_bases(dev, 2,  PCI_ROM_ADDRESS1);
388 }
389
390 static void pci_set_resource(struct device *dev, struct resource *resource)
391 {
392         resource_t base, end;
393
394         /* Make certain the resource has actually been set */
395         if (!(resource->flags & IORESOURCE_ASSIGNED)) {
396                 printk_err("ERROR: %s %02x not allocated\n",
397                         dev_path(dev), resource->index);
398                 return;
399         }
400
401         /* If I have already stored this resource don't worry about it */
402         if (resource->flags & IORESOURCE_STORED) {
403                 return;
404         }
405
406         /* If the resources is substractive don't worry about it */
407         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
408                 return;
409         }
410
411         /* Only handle PCI memory and IO resources for now */
412         if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
413                 return;
414
415         /* Enable the resources in the command register */
416         if (resource->size) {
417                 if (resource->flags & IORESOURCE_MEM) {
418                         dev->command |= PCI_COMMAND_MEMORY;
419                 }
420                 if (resource->flags & IORESOURCE_IO) {
421                         dev->command |= PCI_COMMAND_IO;
422                 }
423                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
424                         dev->command |= PCI_COMMAND_MASTER;
425                 }
426         }
427         /* Get the base address */
428         base = resource->base;
429
430         /* Get the end */
431         end = resource_end(resource);
432         
433         /* Now store the resource */
434         resource->flags |= IORESOURCE_STORED;
435         if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
436                 unsigned long base_lo, base_hi;
437                 /*
438                  * some chipsets allow us to set/clear the IO bit. 
439                  * (e.g. VIA 82c686a.) So set it to be safe)
440                  */
441                 base_lo = base & 0xffffffff;
442                 base_hi = (base >> 32) & 0xffffffff;
443                 if (resource->flags & IORESOURCE_IO) {
444                         base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
445                 }
446                 pci_write_config32(dev, resource->index, base_lo);
447                 if (resource->flags & IORESOURCE_PCI64) {
448                         pci_write_config32(dev, resource->index + 4, base_hi);
449                 }
450         }
451         else if (resource->index == PCI_IO_BASE) {
452                 /* set the IO ranges */
453                 compute_allocate_resource(&dev->link[0], resource, 
454                         IORESOURCE_IO, IORESOURCE_IO);
455                 pci_write_config8(dev,  PCI_IO_BASE, base >> 8);
456                 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
457                 pci_write_config8(dev,  PCI_IO_LIMIT, end >> 8);
458                 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
459         }
460         else if (resource->index == PCI_MEMORY_BASE) {
461                 /* set the memory range  */
462                 compute_allocate_resource(&dev->link[0], resource,
463                         IORESOURCE_MEM | IORESOURCE_PREFETCH, 
464                         IORESOURCE_MEM);
465                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
466                 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
467         }
468         else if (resource->index == PCI_PREF_MEMORY_BASE) {
469                 /* set the prefetchable memory range */
470                 compute_allocate_resource(&dev->link[0], resource,
471                         IORESOURCE_MEM | IORESOURCE_PREFETCH, 
472                         IORESOURCE_MEM | IORESOURCE_PREFETCH);
473                 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
474                 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
475                 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
476                 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
477         }
478         else {
479                 /* Don't let me think I stored the resource */
480                 resource->flags &= ~IORESOURCE_STORED;
481                 printk_err("ERROR: invalid resource->index %x\n",
482                         resource->index);
483         }
484         report_resource_stored(dev, resource, "");
485         return;
486 }
487
488 void pci_dev_set_resources(struct device *dev)
489 {
490         struct resource *resource, *last;
491         unsigned link;
492         uint8_t line;
493
494         last = &dev->resource[dev->resources];
495
496         for (resource = &dev->resource[0]; resource < last; resource++) {
497                 pci_set_resource(dev, resource);
498         }
499         for (link = 0; link < dev->links; link++) {
500                 struct bus *bus;
501                 bus = &dev->link[link];
502                 if (bus->children) {
503                         assign_resources(bus);
504                 }
505         }
506
507         /* set a default latency timer */
508         pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
509
510         /* set a default secondary latency timer */
511         if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
512                 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
513         }
514
515         /* zero the irq settings */
516         line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
517         if (line) {
518                 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
519         }
520         /* set the cache line size, so far 64 bytes is good for everyone */
521         pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
522 }
523
524 void pci_dev_enable_resources(struct device *dev)
525 {
526         const struct pci_operations *ops;
527         uint16_t command;
528
529         /* Set the subsystem vendor and device id for mainboard devices */
530         ops = ops_pci(dev);
531         if (dev->on_mainboard && ops && ops->set_subsystem) {
532                 printk_debug("%s subsystem <- %02x/%02x\n",
533                         dev_path(dev), 
534                         MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
535                         MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
536                 ops->set_subsystem(dev, 
537                         MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
538                         MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
539         }
540         command = pci_read_config16(dev, PCI_COMMAND);
541         command |= dev->command;
542         command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */
543         printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
544         pci_write_config16(dev, PCI_COMMAND, command);
545 }
546
547 void pci_bus_enable_resources(struct device *dev)
548 {
549         uint16_t ctrl;
550
551         /* enable IO in command register if there is VGA card
552          * connected with (even it does not claim IO resource) */
553         if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
554                 dev->command |= PCI_COMMAND_IO;
555
556         ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
557         ctrl |= dev->link[0].bridge_ctrl;
558         ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
559         printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
560         pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
561
562         pci_dev_enable_resources(dev);
563
564         enable_childrens_resources(dev);
565 }
566
567 void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device)
568 {
569         pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID, 
570                 ((device & 0xffff) << 16) | (vendor & 0xffff));
571 }
572
573 void pci_dev_init(struct device *dev)
574 {
575         struct rom_header *rom, *ram;
576
577         rom = pci_rom_probe(dev);
578         if (rom == NULL)
579                 return;
580         ram = pci_rom_load(dev, rom);
581
582         run_bios(dev, ram);
583 }
584
585 /** Default device operation for PCI devices */
586 static struct pci_operations pci_dev_ops_pci = {
587         .set_subsystem = pci_dev_set_subsystem,
588 };
589
590 struct device_operations default_pci_ops_dev = {
591         .read_resources   = pci_dev_read_resources,
592         .set_resources    = pci_dev_set_resources,
593         .enable_resources = pci_dev_enable_resources,
594         .init             = pci_dev_init,
595         .scan_bus         = 0,
596         .enable           = 0,
597         .ops_pci          = &pci_dev_ops_pci,
598 };
599
600 /** Default device operations for PCI bridges */
601 static struct pci_operations pci_bus_ops_pci = {
602         .set_subsystem = 0,
603 };
604
605 struct device_operations default_pci_ops_bus = {
606         .read_resources   = pci_bus_read_resources,
607         .set_resources    = pci_dev_set_resources,
608         .enable_resources = pci_bus_enable_resources,
609         .init             = 0,
610         .scan_bus         = pci_scan_bridge,
611         .enable           = 0,
612         .ops_pci          = &pci_bus_ops_pci,
613 };
614
615 /**
616  * @brief Set up PCI device operation
617  *
618  *
619  * @param dev 
620  *
621  * @see pci_drivers
622  */
623 static void set_pci_ops(struct device *dev)
624 {
625         struct pci_driver *driver;
626         if (dev->ops) {
627                 return;
628         }
629
630         /* Look through the list of setup drivers and find one for
631          * this pci device 
632          */
633         for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
634                 if ((driver->vendor == dev->vendor) &&
635                     (driver->device == dev->device)) 
636                 {
637                         dev->ops = driver->ops;
638                         printk_debug("%s [%04x/%04x] %sops\n", 
639                                 dev_path(dev),
640                                 driver->vendor, driver->device,
641                                 (driver->ops->scan_bus?"bus ":""));
642                         return;
643                 }
644         }
645
646         /* If I don't have a specific driver use the default operations */
647         switch(dev->hdr_type & 0x7f) {  /* header type */
648         case PCI_HEADER_TYPE_NORMAL:    /* standard header */
649                 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
650                         goto bad;
651                 dev->ops = &default_pci_ops_dev;
652                 break;
653         case PCI_HEADER_TYPE_BRIDGE:
654                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
655                         goto bad;
656                 dev->ops = &default_pci_ops_bus;
657                 break;
658         default:
659         bad:
660                 if (dev->enabled) {
661                         printk_err("%s [%04x/%04x/%06x] has unknown header "
662                                 "type %02x, ignoring.\n",
663                                 dev_path(dev),
664                                 dev->vendor, dev->device, 
665                                 dev->class >> 8, dev->hdr_type);
666                 }
667         }
668         return;
669 }
670
671 /**
672  * @brief See if we have already allocated a device structure for a given devfn.
673  *
674  * Given a linked list of PCI device structures and a devfn number, find the
675  * device structure correspond to the devfn, if present. This function also
676  * removes the device structure from the linked list.
677  *
678  * @param list the device structure list
679  * @param devfn a device/function number
680  *
681  * @return pointer to the device structure found or null of we have not
682  *         allocated a device for this devfn yet.
683  */
684 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
685 {
686         struct device *dev;
687         dev = 0;
688         for(; *list; list = &(*list)->sibling) {
689                 if ((*list)->path.type != DEVICE_PATH_PCI) {
690                         printk_err("child %s not a pci device\n",
691                                    dev_path(*list));
692                         continue;
693                 }
694                 if ((*list)->path.u.pci.devfn == devfn) {
695                         /* Unlink from the list */
696                         dev = *list;
697                         *list = (*list)->sibling;
698                         dev->sibling = 0;
699                         break;
700                 }
701         }
702         /* Just like alloc_dev add the device to the list of device on the bus.
703          * When the list of devices was formed we removed all of the parents
704          * children, and now we are interleaving static and dynamic devices in
705          * order on the bus.
706          */
707         if (dev) {
708                 device_t child;
709                 /* Find the last child of our parent */
710                 for (child = dev->bus->children; child && child->sibling; ) {
711                         child = child->sibling;
712                 }
713                 /* Place the device on the list of children of it's parent. */
714                 if (child) {
715                         child->sibling = dev;
716                 } else {
717                         dev->bus->children = dev;
718                 }
719         }
720
721         return dev;
722 }
723
724 /** 
725  * @brief Scan a PCI bus.
726  *
727  * Determine the existence of devices and bridges on a PCI bus. If there are
728  * bridges on the bus, recursively scan the buses behind the bridges.
729  *
730  * @param bus pointer to the bus structure
731  * @param min_devfn minimum devfn to look at in the scan usually 0x00
732  * @param max_devfn maximum devfn to look at in the scan usually 0xff
733  * @param max current bus number
734  *
735  * @return The maximum bus number found, after scanning all subordinate busses
736  */
737 unsigned int pci_scan_bus(struct bus *bus, unsigned min_devfn, unsigned max_devfn,
738                           unsigned int max)
739 {
740         unsigned int devfn;
741         device_t dev;
742         device_t old_devices;
743         device_t child;
744
745         printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
746
747         old_devices = bus->children;
748         bus->children = 0;
749
750         post_code(0x24);
751
752         /* probe all devices/functions on this bus with some optimization for
753          * non-existence and single funcion devices
754          */
755         for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
756                 uint32_t id, class;
757                 uint8_t hdr_type;
758
759                 /* First thing setup the device structure */
760                 dev = pci_scan_get_dev(&old_devices, devfn);
761         
762                 /* Detect if a device is present */
763                 if (!dev) {
764                         struct device dummy;
765                         dummy.bus              = bus;
766                         dummy.path.type        = DEVICE_PATH_PCI;
767                         dummy.path.u.pci.devfn = devfn;
768                         id = pci_read_config32(&dummy, PCI_VENDOR_ID);
769                         /* some broken boards return 0 if a slot is empty: */
770                         if ((id == 0xffffffff) || (id == 0x00000000) || 
771                             (id == 0x0000ffff) || (id == 0xffff0000))
772                         {
773                                 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
774                                 if (PCI_FUNC(devfn) == 0x00) {
775                                         /* if this is a function 0 device and 
776                                          * it is not present,
777                                          * skip to next device 
778                                          */
779                                         devfn += 0x07;
780                                 }
781                                 /* This function in a multi function device is
782                                  * not present, skip to the next function.
783                                  */
784                                 continue;
785                         }
786                         dev = alloc_dev(bus, &dummy.path);
787                 }
788                 else {
789                         /* Enable/disable the device.  Once we have
790                          * found the device specific operations this
791                          * operations we will disable the device with
792                          * those as well.
793                          * 
794                          * This is geared toward devices that have subfunctions
795                          * that do not show up by default.
796                          * 
797                          * If a device is a stuff option on the motherboard
798                          * it may be absent and enable_dev must cope.
799                          * 
800                          */
801                         if (dev->chip_ops && dev->chip_ops->enable_dev) 
802                         {
803                                 dev->chip_ops->enable_dev(dev);
804                         }
805                         /* Now read the vendor and device id */
806                         id = pci_read_config32(dev, PCI_VENDOR_ID);
807                         
808                         /* If the device does not have a pci id disable it.
809                          * Possibly this is because we have already disabled
810                          * the device.  But this also handles optional devices
811                          * that may not always show up.
812                          */
813                         if (id == 0xffffffff || id == 0x00000000 ||
814                             id == 0x0000ffff || id == 0xffff0000) 
815                         {
816                                 if (dev->enabled) {
817                                         printk_info("Disabling static device: %s\n",
818                                                 dev_path(dev));
819                                         dev->enabled = 0;
820                                 }
821                         }
822                 }
823                 /* Read the rest of the pci configuration information */
824                 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
825                 class = pci_read_config32(dev, PCI_CLASS_REVISION);
826
827                 /* Store the interesting information in the device structure */
828                 dev->vendor = id & 0xffff;
829                 dev->device = (id >> 16) & 0xffff;
830                 dev->hdr_type = hdr_type;
831                 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
832                 dev->class = class >> 8;
833
834                 /* Architectural/System devices always need to
835                  * be bus masters.
836                  */
837                 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
838                         dev->command |= PCI_COMMAND_MASTER;
839                 }
840
841                 /* Look at the vendor and device id, or at least the 
842                  * header type and class and figure out which set of
843                  * configuration methods to use.  Unless we already
844                  * have some pci ops.
845                  */
846                 set_pci_ops(dev);
847                 /* Error if we don't have some pci operations for it */
848                 if (!dev->ops) {
849                         printk_err("%s No device operations\n",
850                                 dev_path(dev));
851                         continue;
852                 }
853
854                 /* Now run the magic enable/disable sequence for the device */
855                 if (dev->ops && dev->ops->enable) {
856                         dev->ops->enable(dev);
857                 }
858
859                 printk_debug("%s [%04x/%04x] %s\n", 
860                         dev_path(dev),
861                         dev->vendor, dev->device, 
862                         dev->enabled?"enabled": "disabled");
863
864                 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
865                         /* if this is not a multi function device, 
866                          * don't waste time probing another function. 
867                          * Skip to next device. 
868                          */
869                         devfn += 0x07;
870                 }
871         }
872         post_code(0x25);
873
874         /* For all children that implement scan_bus (i.e. bridges)
875          * scan the bus behind that child.
876          */
877         for (child = bus->children; child; child = child->sibling) {
878                 if (!child->enabled ||
879                     !child->ops || 
880                     !child->ops->scan_bus) 
881                 {
882                         continue;
883                 }
884                 max = child->ops->scan_bus(child, max);
885         }
886
887         /*
888          * We've scanned the bus and so we know all about what's on
889          * the other side of any bridges that may be on this bus plus
890          * any devices.
891          *
892          * Return how far we've got finding sub-buses.
893          */
894         printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
895         post_code(0x55);
896         return max;
897 }
898
899 /**
900  * @brief Scan a PCI bridge and the buses behind the bridge.
901  *
902  * Determine the existence of buses behind the bridge. Set up the bridge
903  * according to the result of the scan.
904  *
905  * This function is the default scan_bus() method for PCI bridge devices.
906  *
907  * @param dev pointer to the bridge device
908  * @param max the highest bus number assgined up to now
909  *
910  * @return The maximum bus number found, after scanning all subordinate busses
911  */
912 unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
913 {
914         struct bus *bus;
915         uint32_t buses;
916         uint16_t cr;
917
918         printk_spew("%s for %s\n", __func__, dev_path(dev));
919
920         bus = &dev->link[0];
921         bus->dev = dev;
922         dev->links = 1;
923
924         /* Set up the primary, secondary and subordinate bus numbers. We have
925          * no idea how many buses are behind this bridge yet, so we set the
926          * subordinate bus number to 0xff for the moment. 
927          */
928         bus->secondary = ++max;
929         bus->subordinate = 0xff;
930
931         /* Clear all status bits and turn off memory, I/O and master enables. */
932         cr = pci_read_config16(dev, PCI_COMMAND);
933         pci_write_config16(dev, PCI_COMMAND, 0x0000);
934         pci_write_config16(dev, PCI_STATUS, 0xffff);
935
936         /*
937          * Read the existing primary/secondary/subordinate bus
938          * number configuration.
939          */
940         buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
941
942         /* Configure the bus numbers for this bridge: the configuration
943          * transactions will not be propagated by the bridge if it is not
944          * correctly configured.
945          */
946         buses &= 0xff000000;
947         buses |= (((unsigned int) (dev->bus->secondary) << 0) |
948                   ((unsigned int) (bus->secondary) << 8) |
949                   ((unsigned int) (bus->subordinate) << 16));
950         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
951
952         /* Now we can scan all subordinate buses 
953          * i.e. the bus behind the bridge.
954          */
955         max = pci_scan_bus(bus, 0x00, 0xff, max);
956
957         /* We know the number of buses behind this bridge. Set the subordinate
958          * bus number to its real value.
959          */
960         bus->subordinate = max;
961         buses = (buses & 0xff00ffff) |
962                 ((unsigned int) (bus->subordinate) << 16);
963         pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
964         pci_write_config16(dev, PCI_COMMAND, cr);
965
966         printk_spew("%s returns max %d\n", __func__, max);
967         return max;
968 }
969
970 /*
971     Tell the EISA int controller this int must be level triggered
972     THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
973 */
974 static void pci_level_irq(unsigned char intNum)
975 {
976         unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
977
978         printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
979         intBits |= (1 << intNum);
980
981         printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
982
983         // Write new values
984         outb((unsigned char) intBits, 0x4d0);
985         outb((unsigned char) (intBits >> 8), 0x4d1);
986
987         /* this seems like an error but is not ... */
988 #if 1
989         if (inb(0x4d0) != (intBits & 0xf)) {
990           printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
991                      __func__, intBits &0xf, inb(0x4d0));
992         }
993         if (inb(0x4d1) != ((intBits >> 8) & 0xf)) {
994           printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
995                      __func__, (intBits>>8) &0xf, inb(0x4d1));
996         }
997 #endif
998 }
999
1000 /*
1001     This function assigns IRQs for all functions contained within
1002     the indicated device address.  If the device does not exist or does
1003     not require interrupts then this function has no effect.
1004
1005     This function should be called for each PCI slot in your system.  
1006
1007     pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
1008     this slot.  
1009     The particular irq #s that are passed in depend on the routing inside
1010     your southbridge and on your motherboard.
1011
1012     -kevinh@ispiri.com
1013 */
1014 void pci_assign_irqs(unsigned bus, unsigned slot, const unsigned char pIntAtoD[4])
1015 {
1016         unsigned functNum;
1017         device_t pdev;
1018         unsigned char line;
1019         unsigned char irq;
1020         unsigned char readback;
1021
1022         /* Each slot may contain up to eight functions */
1023         for (functNum = 0; functNum < 8; functNum++) {
1024                 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1025
1026                 if (pdev) {
1027                   line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1028
1029                         // PCI spec says all other values are reserved 
1030                         if ((line >= 1) && (line <= 4)) {
1031                                 irq = pIntAtoD[line - 1];
1032
1033                                 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
1034                                         irq, bus, slot, functNum);
1035
1036                                 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
1037                                         pIntAtoD[line - 1]);
1038
1039                                 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1040                                 printk_debug("  Readback = %d\n", readback);
1041
1042                                 // Change to level triggered
1043                                 pci_level_irq(pIntAtoD[line - 1]);
1044                         }
1045                 }
1046         }
1047 }