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