1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <types.h>
27#include <io.h>
28#include <string.h>
29#include <lib.h>
30#include <console.h>
31#include <device/device.h>
32#include <device/pci.h>
33#include <device/pci_ids.h>
34
35#ifndef CONFIG_NO_PCIX_SUPPORT
36#include <device/pcix.h>
37#endif
38#ifndef CONFIG_NO_AGP_SUPPORT
39#include <device/agp.h>
40#endif
41#ifdef CONFIG_HYPERTRANSPORT_SUPPORT
42#include <device/hypertransport.h>
43#endif
44#ifndef CONFIG_NO_PCIE_SUPPORT
45#include <device/pcie.h>
46#endif
47#ifndef CONFIG_NO_CARDBUS_SUPPORT
48#include <device/cardbus.h>
49#endif
50
51#include <statictree.h>
52
53u8 pci_moving_config8(struct device *dev, unsigned int reg)
54{
55 u8 value, ones, zeroes;
56 value = pci_read_config8(dev, reg);
57
58 pci_write_config8(dev, reg, 0xff);
59 ones = pci_read_config8(dev, reg);
60
61 pci_write_config8(dev, reg, 0x00);
62 zeroes = pci_read_config8(dev, reg);
63
64 pci_write_config8(dev, reg, value);
65
66 return ones ^ zeroes;
67}
68
69u16 pci_moving_config16(struct device *dev, unsigned int reg)
70{
71 u16 value, ones, zeroes;
72 value = pci_read_config16(dev, reg);
73
74 pci_write_config16(dev, reg, 0xffff);
75 ones = pci_read_config16(dev, reg);
76
77 pci_write_config16(dev, reg, 0x0000);
78 zeroes = pci_read_config16(dev, reg);
79
80 pci_write_config16(dev, reg, value);
81
82 return ones ^ zeroes;
83}
84
85u32 pci_moving_config32(struct device *dev, unsigned int reg)
86{
87 u32 value, ones, zeroes;
88 value = pci_read_config32(dev, reg);
89
90 pci_write_config32(dev, reg, 0xffffffff);
91 ones = pci_read_config32(dev, reg);
92
93 pci_write_config32(dev, reg, 0x00000000);
94 zeroes = pci_read_config32(dev, reg);
95
96 pci_write_config32(dev, reg, value);
97
98 return ones ^ zeroes;
99}
100
101
102
103
104
105
106
107
108
109unsigned int pci_find_next_capability(struct device *dev, unsigned int cap_type,
110 unsigned int last_pos)
111{
112 unsigned int pos;
113 unsigned int status;
114 unsigned int reps = 48;
115 pos = 0;
116 status = pci_read_config16(dev, PCI_STATUS);
117 if (!(status & PCI_STATUS_CAP_LIST)) {
118 return 0;
119 }
120 switch (dev->hdr_type & 0x7f) {
121 case PCI_HEADER_TYPE_NORMAL:
122 case PCI_HEADER_TYPE_BRIDGE:
123 pos = PCI_CAPABILITY_LIST;
124 break;
125 case PCI_HEADER_TYPE_CARDBUS:
126 pos = PCI_CB_CAPABILITY_LIST;
127 break;
128 default:
129 return 0;
130 }
131 pos = pci_read_config8(dev, pos);
132 while (reps-- && (pos >= 0x40)) {
133 int this_cap_type;
134 pos &= ~3;
135 this_cap_type = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
136 printk(BIOS_SPEW, "Capability: 0x%02x @ 0x%02x\n",
137 this_cap_type, pos);
138 if (this_cap_type == 0xff) {
139 break;
140 }
141 if (!last_pos && (this_cap_type == cap_type)) {
142 return pos;
143 }
144 if (last_pos == pos) {
145 last_pos = 0;
146 }
147 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
148 }
149 return 0;
150}
151
152
153
154
155
156
157
158
159unsigned int pci_find_capability(struct device *dev, unsigned int cap_type)
160{
161 return pci_find_next_capability(dev, cap_type, 0);
162}
163
164
165
166
167
168
169
170struct resource *pci_get_resource(struct device *dev, unsigned long index)
171{
172 struct resource *resource;
173 unsigned long value, attr;
174 resource_t moving, limit;
175
176
177 resource = new_resource(dev, index);
178
179
180 value = pci_read_config32(dev, index);
181
182
183 moving = pci_moving_config32(dev, index);
184
185
186 attr = value & ~moving;
187
188
189 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
190 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
191 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
192
193 moving |=
194 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
195 resource->flags |= IORESOURCE_PCI64;
196 }
197
198
199
200
201
202
203
204 limit = 0;
205 if (moving) {
206 resource->size = 1;
207 resource->align = resource->gran = 0;
208 while (!(moving & resource->size)) {
209 resource->size <<= 1;
210 resource->align += 1;
211 resource->gran += 1;
212 }
213 resource->limit = limit = moving | (resource->size - 1);
214 }
215
216
217
218
219
220
221
222
223
224
225
226 if (moving == 0) {
227 if (value != 0) {
228 printk(BIOS_DEBUG,
229 "%s register %02lx(%08lx), read-only ignoring it\n",
230 dev_path(dev), index, value);
231 }
232
233
234
235
236 resource->flags &= IORESOURCE_PCI64;
237 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
238
239 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
240 resource->flags |= IORESOURCE_IO;
241
242 resource->limit = 0xffff;
243 } else {
244
245 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
246 resource->flags |= IORESOURCE_MEM;
247 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
248 resource->flags |= IORESOURCE_PREFETCH;
249 }
250 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
251 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
252
253 resource->limit = 0xffffffffUL;
254 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
255
256 resource->limit = 0x000fffffUL;
257 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
258
259 resource->limit = 0xffffffffffffffffULL;
260 } else {
261
262 printk(BIOS_ERR,"Broken BAR with value %lx\n",attr);
263 printk(BIOS_ERR," on dev %s at index %02lx\n",dev->dtsname,index);
264 resource->flags = 0;
265 }
266 }
267
268 if (resource->limit > limit) {
269 resource->limit = limit;
270 }
271
272 return resource;
273}
274
275
276
277
278
279
280
281static void pci_get_rom_resource(struct device *dev, unsigned long index)
282{
283 struct resource *resource;
284 unsigned long value;
285 resource_t moving, limit;
286
287 if ((dev->on_mainboard) && (dev->rom_address == 0)) {
288
289 return;
290 }
291
292
293 resource = new_resource(dev, index);
294
295
296 value = pci_read_config32(dev, index);
297
298
299 moving = pci_moving_config32(dev, index);
300
301
302 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
303
304
305
306
307
308
309
310 limit = 0;
311
312 if (moving) {
313 resource->size = 1;
314 resource->align = resource->gran = 0;
315 while (!(moving & resource->size)) {
316 resource->size <<= 1;
317 resource->align += 1;
318 resource->gran += 1;
319 }
320 resource->limit = limit = moving | (resource->size - 1);
321 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
322 } else {
323 if (value != 0) {
324 printk(BIOS_DEBUG,
325 "%s register %02lx(%08lx), read-only ignoring it\n",
326 dev_path(dev), index, value);
327 }
328 resource->flags = 0;
329 }
330
331
332
333
334 if ((dev->on_mainboard) && (dev->rom_address != 0)) {
335 resource->base = dev->rom_address;
336 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
337 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
338 }
339
340 compact_resources(dev);
341}
342
343
344
345
346
347
348
349static void pci_read_bases(struct device *dev, unsigned int howmany)
350{
351 unsigned long index;
352
353 for (index = PCI_BASE_ADDRESS_0;
354 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
355 struct resource *resource;
356 resource = pci_get_resource(dev, index);
357 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
358 }
359
360 compact_resources(dev);
361}
362
363static void pci_record_bridge_resource(struct device *dev, resource_t moving,
364 unsigned int index, unsigned long type)
365{
366
367 struct resource *resource;
368 resource = NULL;
369 if (moving) {
370 unsigned long gran;
371 resource_t step;
372 resource = new_resource(dev, index);
373 resource->size = 0;
374 gran = 0;
375 step = 1;
376 while ((moving & step) == 0) {
377 gran += 1;
378 step <<= 1;
379 }
380 resource->gran = gran;
381 resource->align = gran;
382 resource->limit = moving | (step - 1);
383 resource->flags = type | IORESOURCE_PCI_BRIDGE | IORESOURCE_BRIDGE;
384 }
385 return;
386}
387
388static void pci_bridge_read_bases(struct device *dev)
389{
390 resource_t moving_base, moving_limit, moving;
391
392
393 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
394 moving_base |=
395 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
396
397 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
398 moving_limit |=
399 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
400
401 moving = moving_base & moving_limit;
402
403
404 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
405
406
407 moving_base =
408 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
409 moving_base |= ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
410
411 moving_limit = ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
412 moving_limit |= ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
413
414 moving = moving_base & moving_limit;
415
416 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
417 IORESOURCE_MEM | IORESOURCE_PREFETCH);
418
419
420 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
421 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
422
423 moving = moving_base & moving_limit;
424
425
426 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
427 IORESOURCE_MEM);
428
429 compact_resources(dev);
430}
431
432void pci_dev_read_resources(struct device *dev)
433{
434 pci_read_bases(dev, 6);
435 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
436}
437
438void pci_bus_read_resources(struct device *dev)
439{
440 struct device *child;
441
442 printk(BIOS_DEBUG, "%s: %s bus %s\n",
443 __func__, dev_path(dev), dev->bus? dev_path(dev->bus->dev):"NULL");
444 pci_bridge_read_bases(dev);
445 pci_read_bases(dev, 2);
446 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
447 if (!dev->bus){
448 printk(BIOS_ERR, "%s: %s bus %s\n",
449 __func__, dev_path(dev), dev->bus? dev_path(dev->bus->dev):"NULL");
450 }
451
452 for (child = dev->link[0].children; child; child = child->sibling)
453 if (child->ops && child->ops->phase4_read_resources)
454 child->ops->phase4_read_resources(child);
455 else
456 printk(BIOS_ERR, "%s: %s missing Phase4\n",
457 __func__, dev_path(child));
458}
459
460
461
462
463
464
465
466
467
468
469void pci_domain_read_resources(struct device *dev)
470{
471 struct resource *res;
472
473
474 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
475 res->limit = 0xffffUL;
476 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
477 IORESOURCE_ASSIGNED | IORESOURCE_BRIDGE;
478
479
480 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
481 res->limit = 0xffffffffULL;
482 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
483 IORESOURCE_ASSIGNED | IORESOURCE_BRIDGE;
484}
485
486static void pci_set_resource(struct device *dev, struct resource *resource)
487{
488 resource_t base, end;
489
490
491 if (!(resource->flags & IORESOURCE_ASSIGNED) && resource->size!=0) {
492 printk(BIOS_ERR,
493 "ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
494 dev_path(dev), resource->index, resource_type(resource),
495 resource->size);
496 return;
497 }
498
499
500 if (resource->flags & IORESOURCE_STORED) {
501 return;
502 }
503
504
505 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
506 return;
507 }
508
509
510 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
511 return;
512
513
514 if (resource->size) {
515 if (resource->flags & IORESOURCE_MEM) {
516 dev->command |= PCI_COMMAND_MEMORY;
517 }
518 if (resource->flags & IORESOURCE_IO) {
519 dev->command |= PCI_COMMAND_IO;
520 }
521 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
522 dev->command |= PCI_COMMAND_MASTER;
523 }
524 }
525
526 base = resource->base;
527
528
529 end = resource_end(resource);
530
531
532 resource->flags |= IORESOURCE_STORED;
533
534
535
536
537 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
538 base = resource->limit;
539 end = resource->limit - (1<<resource->gran);
540 resource->base = base;
541 }
542
543 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
544 unsigned long base_lo, base_hi;
545
546
547
548 base_lo = base & 0xffffffff;
549 base_hi = (base >> 32) & 0xffffffff;
550 if (resource->flags & IORESOURCE_IO) {
551 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
552 }
553 pci_write_config32(dev, resource->index, base_lo);
554 if (resource->flags & IORESOURCE_PCI64) {
555 pci_write_config32(dev, resource->index + 4, base_hi);
556 }
557 } else if (resource->index == PCI_IO_BASE) {
558
559 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
560 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
561 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
562 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
563 } else if (resource->index == PCI_MEMORY_BASE) {
564
565 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
566 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
567 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
568
569 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
570 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
571 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
572 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
573 } else {
574
575 resource->flags &= ~IORESOURCE_STORED;
576 printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
577 resource->index);
578 }
579 report_resource_stored(dev, resource, __func__);
580 return;
581}
582
583void pci_set_resources(struct device *dev)
584{
585 struct resource *resource, *last;
586 unsigned int link;
587 u8 line;
588
589 last = &dev->resource[dev->resources];
590
591 for (resource = &dev->resource[0]; resource < last; resource++) {
592 pci_set_resource(dev, resource);
593 }
594 for (link = 0; link < dev->links; link++) {
595 struct bus *bus;
596 bus = &dev->link[link];
597 if (bus->children) {
598 phase4_set_resources(bus);
599 }
600 }
601
602
603 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
604
605
606 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
607 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
608 }
609
610
611 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
612 if (line) {
613 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
614 }
615
616 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
617}
618
619
620
621
622
623
624
625
626
627
628void ram_resource(struct device *dev, unsigned long index,
629 unsigned long basek, unsigned long sizek)
630{
631 struct resource *res;
632
633 if (!sizek)
634 return;
635
636 res = new_resource(dev, index);
637 res->base = ((resource_t) basek) << 10;
638 res->size = ((resource_t) sizek) << 10;
639 res->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE |
640 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
641
642 printk(BIOS_SPEW, "Adding RAM resource (%lld bytes)\n", res->size);
643}
644
645void pci_dev_set_subsystem_wrapper(struct device *dev)
646{
647 const struct pci_operations *ops;
648 u16 vendor = dev->id.pci.vendor;
649 u16 device = dev->id.pci.device;
650
651 ops = ops_pci(dev);
652
653
654 if (ops && ops->set_subsystem && vendor && device) {
655
656
657 vendor = dev->subsystem_vendor;
658 device = dev->subsystem_device;
659
660
661 if (dev->on_mainboard) {
662 if (!vendor)
663 vendor = dev_root.subsystem_vendor;
664 if (!device)
665 device = dev_root.subsystem_device;
666
667 printk(BIOS_DEBUG,
668 "%s: Setting subsystem VID/DID to %02x/%02x\n",
669 dev_path(dev), vendor, device);
670
671 ops->set_subsystem(dev, vendor, device);
672
673 } else {
674 printk(BIOS_DEBUG, "%s: Device not on_mainboard\n",
675 dev_path(dev));
676 }
677 }
678
679}
680
681void pci_dev_enable_resources(struct device *dev)
682{
683 u16 command;
684
685 pci_dev_set_subsystem_wrapper(dev);
686
687 command = pci_read_config16(dev, PCI_COMMAND);
688 command |= dev->command;
689 command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR);
690 printk(BIOS_DEBUG, "%s: %s (%s) cmd <- %02x\n", __func__, dev->dtsname,
691 dev_path(dev), command);
692 pci_write_config16(dev, PCI_COMMAND, command);
693}
694
695void pci_bus_enable_resources(struct device *dev)
696{
697 u16 ctrl;
698
699
700
701
702 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
703 dev->command |= PCI_COMMAND_IO;
704 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
705 ctrl |= dev->link[0].bridge_ctrl;
706 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR);
707 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
708 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
709
710 pci_dev_enable_resources(dev);
711 enable_childrens_resources(dev);
712}
713
714void pci_bus_reset(struct bus *bus)
715{
716 unsigned ctl;
717 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
718 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
719 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
720 mdelay(10);
721 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
722 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
723 delay(1);
724}
725
726void pci_dev_set_subsystem(struct device *dev, u16 vendor, u16 device)
727{
728 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
729 ((device & 0xffff) << 16) | (vendor & 0xffff));
730}
731
732void pci_dev_init(struct device *dev)
733{
734 printk(BIOS_SPEW, "PCI: pci_dev_init %s\n", dev->dtsname);
735#ifdef CONFIG_PCI_OPTION_ROM_RUN
736 void run_bios(struct device *dev, unsigned long addr);
737 struct rom_header *rom, *ram;
738
739 printk(BIOS_INFO, "Probing for option ROM\n");
740 rom = pci_rom_probe(dev);
741 if (rom == NULL)
742 return;
743 ram = pci_rom_load(dev, rom);
744 if (ram == NULL)
745 return;
746 run_bios(dev, (unsigned long)ram);
747#endif
748}
749
750
751const struct pci_operations pci_dev_ops_pci = {
752 .set_subsystem = pci_dev_set_subsystem,
753};
754
755const struct device_operations default_pci_ops_dev = {
756 .phase4_read_resources = pci_dev_read_resources,
757 .phase4_set_resources = pci_set_resources,
758 .phase5_enable_resources = pci_dev_enable_resources,
759 .phase6_init = pci_dev_init,
760 .phase3_scan = 0,
761 .ops_pci = &pci_dev_ops_pci,
762};
763
764
765const struct pci_operations pci_bus_ops_pci = {
766 .set_subsystem = 0,
767};
768
769const struct device_operations default_pci_ops_bus = {
770 .phase3_scan = pci_scan_bridge,
771 .phase4_read_resources = pci_bus_read_resources,
772 .phase4_set_resources = pci_set_resources,
773 .phase5_enable_resources = pci_bus_enable_resources,
774 .phase6_init = 0,
775 .reset_bus = pci_bus_reset,
776 .ops_pci = &pci_bus_ops_pci,
777};
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793static const struct device_operations *get_pci_bridge_ops(struct device *dev)
794{
795#ifndef CONFIG_NO_PCIX_SUPPORT
796 unsigned int pcix_pos;
797 pcix_pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
798 if (pcix_pos) {
799 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n",
800 dev_path(dev));
801 return &default_pcix_ops_bus;
802 }
803#endif
804#ifndef CONFIG_NO_AGP_SUPPORT
805
806#warning AGP detection not implemented, so AGP bridge plugin not supported.
807
808#endif
809#ifdef CONFIG_HYPERTRANSPORT_SUPPORT
810 unsigned int ht_pos;
811 ht_pos = 0;
812 while ((ht_pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, ht_pos))) {
813 unsigned int flags;
814 flags = pci_read_config16(dev, ht_pos + PCI_CAP_FLAGS);
815 if ((flags >> 13) == 1) {
816
817 printk(BIOS_DEBUG,
818 "%s subordinate bus Hypertransport\n",
819 dev_path(dev));
820 return &default_ht_ops_bus;
821 }
822 }
823#endif
824#ifndef CONFIG_NO_PCIE_SUPPORT
825 unsigned int pcie_pos;
826 pcie_pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
827 if (pcie_pos) {
828 unsigned int flags;
829 flags = pci_read_config16(dev, pcie_pos + PCI_EXP_FLAGS);
830 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
831 case PCI_EXP_TYPE_ROOT_PORT:
832 case PCI_EXP_TYPE_UPSTREAM:
833 case PCI_EXP_TYPE_DOWNSTREAM:
834 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
835 dev_path(dev));
836 return &default_pcie_ops_bus;
837 case PCI_EXP_TYPE_PCI_BRIDGE:
838 printk(BIOS_DEBUG, "%s subordinate PCI\n",
839 dev_path(dev));
840 return &default_pci_ops_bus;
841 default:
842 break;
843 }
844 }
845#endif
846 return &default_pci_ops_bus;
847}
848
849
850
851
852
853
854
855
856static void set_pci_ops(struct device *dev)
857{
858 struct device_operations *c;
859
860 if (dev->ops) {
861 printk(BIOS_SPEW, "%s: dev %s already has ops of type %x\n",
862 __func__, dev->dtsname, dev->ops->id.type);
863 return;
864 }
865
866
867
868
869 c = find_device_operations(&dev->id);
870 if (c) {
871 dev->ops = c;
872 printk(BIOS_SPEW, "%s id %s %sops\n",
873 dev_path(dev), dev_id_string(&dev->id),
874 (dev->ops->phase3_scan ? "bus " : ""));
875 return;
876 }
877
878
879 switch (dev->hdr_type & 0x7f) {
880 case PCI_HEADER_TYPE_NORMAL:
881 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
882 printk(BIOS_ERR,
883 "%s [%s] hdr_type %02x doesn't match"
884 "class %06x, ignoring.\n", dev_path(dev),
885 dev_id_string(&dev->id), dev->class >> 8,
886 dev->hdr_type);
887 else
888 dev->ops = &default_pci_ops_dev;
889 break;
890 case PCI_HEADER_TYPE_BRIDGE:
891 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
892 printk(BIOS_ERR,
893 "%s [%s] hdr_type %02x doesn't match"
894 "class %06x, ignoring.\n", dev_path(dev),
895 dev_id_string(&dev->id), dev->class >> 8,
896 dev->hdr_type);
897 else
898 dev->ops = get_pci_bridge_ops(dev);
899 break;
900#ifndef CONFIG_NO_CARDBUS_SUPPORT
901 case PCI_HEADER_TYPE_CARDBUS:
902 dev->ops = &default_cardbus_ops_bus;
903 break;
904#endif
905 default:
906 if (dev->enabled) {
907 printk(BIOS_ERR,
908 "%s [%s/%06x] has unknown header "
909 "type %02x, ignoring.\n", dev_path(dev),
910 dev_id_string(&dev->id), dev->class >> 8,
911 dev->hdr_type);
912 }
913 }
914 printk(BIOS_INFO, "%s: dev %s set ops to type %x\n", __func__,
915 dev->dtsname, dev->ops? dev->ops->id.type : 0);
916 return;
917}
918
919
920
921
922
923
924
925
926
927
928
929
930
931static struct device *pci_get_dev(struct device **list, unsigned int devfn)
932{
933 struct device *dev;
934 dev = 0;
935 printk(BIOS_SPEW, "%s: list is %sNULL, *list is %sNULL\n", __func__,
936 list?"NOT ":"", *list?"NOT ":"");
937 for (; *list; list = &(*list)->sibling) {
938 printk(BIOS_SPEW, "%s: check dev %s \n", __func__,
939 (*list)->dtsname);
940 if ((*list)->path.type != DEVICE_PATH_PCI) {
941 printk(BIOS_NOTICE,
942 "%s: child %s(%s) not a pci device: it's type %d\n",
943 __func__, (*list)->dtsname, dev_path(*list),
944 (*list)->path.type);
945 continue;
946 }
947 printk(BIOS_SPEW, "%s: check dev %s it has devfn 0x%02x\n",
948 __func__, (*list)->dtsname, (*list)->path.pci.devfn);
949 if ((*list)->path.pci.devfn == devfn) {
950
951 dev = *list;
952 *list = (*list)->sibling;
953 dev->sibling = NULL;
954 break;
955 }
956 }
957
958
959
960
961
962
963 if (dev) {
964 struct device *child;
965
966 for (child = dev->bus->children; child && child->sibling;) {
967 child = child->sibling;
968 }
969
970 if (child) {
971 child->sibling = dev;
972 } else {
973 dev->bus->children = dev;
974 }
975 }
976
977 return dev;
978}
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997struct device *pci_probe_dev(struct device *dev, struct bus *bus,
998 unsigned int devfn)
999{
1000 u32 id, class;
1001 u8 hdr_type;
1002
1003
1004 if (!dev) {
1005 struct device dummy;
1006 struct device_id devid;
1007 dummy.bus = bus;
1008 dummy.path.type = DEVICE_PATH_PCI;
1009 dummy.path.pci.devfn = devfn;
1010 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
1011
1012
1013
1014 if ((id == 0xffffffff) || (id == 0x00000000) ||
1015 (id == 0x0000ffff) || (id == 0xffff0000)) {
1016 printk(BIOS_SPEW, "PCI: devfn 0x%x, bad id 0x%x\n",
1017 devfn, id);
1018 return NULL;
1019 }
1020 devid.type = DEVICE_ID_PCI;
1021 devid.pci.vendor = id & 0xffff;
1022 devid.pci.device = id >> 16;
1023 dev = alloc_dev(bus, &dummy.path, &devid);
1024 } else {
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036 if (dev->ops && dev->ops->phase3_chip_setup_dev) {
1037 dev->ops->phase3_chip_setup_dev(dev);
1038 }
1039
1040 id = pci_read_config32(dev, PCI_VENDOR_ID);
1041
1042
1043
1044
1045
1046
1047 if ((id == 0xffffffff) || (id == 0x00000000) ||
1048 (id == 0x0000ffff) || (id == 0xffff0000)) {
1049 if (dev->enabled) {
1050 printk(BIOS_INFO,
1051 "Disabling static device: %s\n",
1052 dev_path(dev));
1053 dev->enabled = 0;
1054 }
1055 return dev;
1056 }
1057 }
1058
1059
1060 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1061 class = pci_read_config32(dev, PCI_CLASS_REVISION);
1062 dev->status = pci_read_config16(dev, PCI_STATUS);
1063 dev->revision = pci_read_config8(dev, PCI_REVISION_ID);
1064 dev->cache_line = pci_read_config8(dev, PCI_CACHE_LINE_SIZE);
1065 dev->irq_line = pci_read_config8(dev, PCI_INTERRUPT_LINE);
1066 dev->irq_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1067 dev->min_gnt = pci_read_config8(dev, PCI_MIN_GNT);
1068 dev->max_lat = pci_read_config8(dev, PCI_MAX_LAT);
1069
1070
1071
1072
1073 if (!dev->subsystem_vendor && !dev->subsystem_device) {
1074 dev->subsystem_vendor =
1075 pci_read_config16(dev, PCI_SUBSYSTEM_VENDOR_ID);
1076 dev->subsystem_device =
1077 pci_read_config16(dev, PCI_SUBSYSTEM_ID);
1078 }
1079
1080 dev->id.type = DEVICE_ID_PCI;
1081 dev->id.pci.vendor = id & 0xffff;
1082 dev->id.pci.device = (id >> 16) & 0xffff;
1083 dev->hdr_type = hdr_type;
1084
1085
1086 dev->class = class >> 8;
1087
1088
1089 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
1090 dev->command |= PCI_COMMAND_MASTER;
1091 }
1092
1093
1094
1095
1096 set_pci_ops(dev);
1097
1098
1099 if (dev->ops && dev->ops->phase3_enable) {
1100 dev->ops->phase3_enable(dev);
1101 }
1102
1103
1104 printk(BIOS_DEBUG, "%s [%s] %s%s\n",
1105 dev_path(dev), dev_id_string(&dev->id),
1106 dev->enabled ? "enabled" : "disabled",
1107 dev->ops ? "" : " No operations");
1108
1109 return dev;
1110}
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127unsigned int pci_scan_bus(struct bus *bus, unsigned int min_devfn,
1128 unsigned int max_devfn, unsigned int curr_bus)
1129{
1130 unsigned int devfn;
1131 struct device *old_devices;
1132 struct device *child;
1133
1134 printk(BIOS_DEBUG, "%s start bus->dev %s bus %x\n", __func__,
1135 bus->dev->dtsname, bus->link);
1136
1137#warning This check needs checking.
1138 if (bus->dev->path.type != DEVICE_PATH_PCI_BUS)
1139 printk(BIOS_ERR, "ERROR: pci_scan_bus called with incorrect "
1140 "bus->dev->path.type, path is %s\n", dev_path(bus->dev));
1141
1142#if PCI_BUS_SEGN_BITS
1143 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
1144 bus->secondary >> 8, bus->secondary & 0xff);
1145#else
1146 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1147#endif
1148
1149
1150 old_devices = bus->children;
1151 printk(BIOS_DEBUG, "%s: old_devices %s, dev for this bus %s\n",
1152 __func__, old_devices?old_devices->dtsname:"NULL", bus->dev->dtsname);
1153 bus->children = NULL;
1154
1155 post_code(POST_STAGE2_PCISCANBUS_ENTER);
1156 printk(BIOS_SPEW, "PCI: scan devfn 0x%x to 0x%x\n", min_devfn,
1157 max_devfn);
1158
1159
1160
1161 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
1162 struct device *dev;
1163 printk(BIOS_SPEW, "PCI: devfn 0x%x\n", devfn);
1164
1165
1166 dev = pci_get_dev(&old_devices, devfn);
1167
1168 printk(BIOS_SPEW,
1169 "PCI: pci_scan_bus pci_get_dev returns dev %s\n",
1170 dev ? dev->dtsname : "None (no dev in tree yet)");
1171
1172
1173 dev = pci_probe_dev(dev, bus, devfn);
1174 printk(BIOS_SPEW,
1175 "PCI: pci_scan_bus pci_probe_dev returns dev %s\n",
1176 dev ? dev->dtsname : "None (no response)");
1177
1178
1179
1180
1181
1182 if ((PCI_FUNC(devfn) == 0x00) &&
1183 (!dev
1184 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
1185 printk(BIOS_SPEW, "Not a multi function device, or the "
1186 "device is not present. Skip to next device.\n");
1187 devfn += 0x07;
1188 }
1189 }
1190 printk(BIOS_SPEW, "PCI: Done for loop\n");
1191 post_code(POST_STAGE2_PCISCANBUS_DONEFORLOOP);
1192
1193
1194
1195
1196 if (old_devices) {
1197 struct device *left;
1198 printk(BIOS_INFO, "PCI: Left over static devices:\n");
1199 for (left = old_devices; left; left = left->sibling) {
1200 printk(BIOS_INFO, "%s\n", left->dtsname);
1201 }
1202 printk(BIOS_INFO, "PCI: End of leftover list.\n");
1203 }
1204
1205
1206
1207
1208 for (child = bus->children; child; child = child->sibling) {
1209 curr_bus = dev_phase3_scan(child, curr_bus);
1210 }
1211
1212
1213
1214
1215
1216 printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with curr_bus=%03x\n",
1217 curr_bus);
1218 post_code(POST_STAGE2_PCISCANBUS_EXIT);
1219 return curr_bus;
1220}
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232unsigned int pci_domain_scan_bus(struct device *dev, unsigned int curr_bus)
1233{
1234 printk(BIOS_SPEW, "pci_domain_scan_bus: calling pci_scan_bus\n");
1235
1236 return pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, curr_bus);
1237}
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
1252 unsigned int (*do_scan_bus) (struct bus * bus,
1253 unsigned int min_devfn,
1254 unsigned int max_devfn,
1255 unsigned int max))
1256{
1257 struct bus *bus;
1258 u32 buses;
1259 u16 cr;
1260
1261 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
1262
1263 bus = &dev->link[0];
1264 bus->dev = dev;
1265 dev->links = 1;
1266
1267
1268
1269
1270
1271 bus->secondary = ++max;
1272 bus->subordinate = 0xff;
1273
1274
1275 cr = pci_read_config16(dev, PCI_COMMAND);
1276 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1277 pci_write_config16(dev, PCI_STATUS, 0xffff);
1278
1279
1280
1281
1282 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
1283
1284
1285
1286
1287
1288 buses &= 0xff000000;
1289 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1290 ((unsigned int)(bus->secondary) << 8) |
1291 ((unsigned int)(bus->subordinate) << 16));
1292 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1293
1294
1295
1296
1297 max = do_scan_bus(bus, 0x00, 0xff, max);
1298
1299
1300
1301
1302 bus->subordinate = max;
1303 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
1304 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1305 pci_write_config16(dev, PCI_COMMAND, cr);
1306
1307 printk(BIOS_DEBUG, "%s DONE: [%x, %x, %x]\n", __func__,
1308 dev->bus->secondary, bus->secondary, bus->subordinate);
1309 printk(BIOS_SPEW, "%s returns max %d\n", __func__, max);
1310 return max;
1311}
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1326{
1327 printk(BIOS_SPEW, "pci_scan_bridge: calling pci_scan_bus\n");
1328 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1329}
1330
1331
1332
1333
1334
1335
1336
1337
1338void pci_level_irq(unsigned char intNum)
1339{
1340 unsigned short intBits = inb(0x4d0) | (((unsigned)inb(0x4d1)) << 8);
1341
1342 printk(BIOS_SPEW, "%s: current ints are 0x%x\n", __func__, intBits);
1343 intBits |= (1 << intNum);
1344
1345 printk(BIOS_SPEW, "%s: try to set ints 0x%x\n", __func__, intBits);
1346
1347
1348 outb((unsigned char)intBits, 0x4d0);
1349 outb((unsigned char)(intBits >> 8), 0x4d1);
1350
1351
1352 if (inb(0x4d0) != (intBits & 0xff)) {
1353 printk(BIOS_ERR,
1354 "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
1355 __func__, intBits & 0xff, inb(0x4d0));
1356 }
1357 if (inb(0x4d1) != ((intBits >> 8) & 0xff)) {
1358 printk(BIOS_ERR,
1359 "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
1360 __func__, (intBits >> 8) & 0xff, inb(0x4d1));
1361 }
1362}
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383void pci_assign_irqs(unsigned int bus, unsigned int slot,
1384 const unsigned char pIntAtoD[4])
1385{
1386 unsigned int functNum;
1387 struct device *pdev;
1388 unsigned char line;
1389 unsigned char irq;
1390 unsigned char readback;
1391
1392
1393 for (functNum = 0; functNum < 8; functNum++) {
1394 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1395
1396 if (pdev) {
1397 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1398
1399
1400 if ((line >= 1) && (line <= 4)) {
1401 irq = pIntAtoD[line - 1];
1402
1403 printk(BIOS_DEBUG,
1404 "Assigning IRQ %d to %d:%x.%d\n", irq,
1405 bus, slot, functNum);
1406
1407 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
1408 pIntAtoD[line - 1]);
1409
1410 readback =
1411 pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1412 printk(BIOS_DEBUG, " Readback = %d\n",
1413 readback);
1414
1415
1416 pci_level_irq(pIntAtoD[line - 1]);
1417 }
1418 }
1419 }
1420}
1421