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