1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <pci.h>
26#include <asm/io.h>
27#include <asm/pci.h>
28
29#ifdef CONFIG_PCI
30#undef PCI_ROM_SCAN_VERBOSE
31
32int pci_shadow_rom(pci_dev_t dev, unsigned char *dest)
33{
34 struct pci_controller *hose;
35 int res = -1;
36 int i;
37
38 u32 rom_addr;
39 u32 addr_reg;
40 u32 size;
41
42 u16 vendor;
43 u16 device;
44 u32 class_code;
45
46 hose = pci_bus_to_hose(PCI_BUS(dev));
47#if 0
48 printf("pci_shadow_rom() asked to shadow device %x to %x\n",
49 dev, (u32)dest);
50#endif
51 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
52 pci_read_config_word(dev, PCI_DEVICE_ID, &device);
53 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_code);
54
55 class_code &= 0xffffff00;
56 class_code >>= 8;
57
58#if 0
59 printf("PCI Header Vendor %04x device %04x class %06x\n",
60 vendor, device, class_code);
61#endif
62
63 pci_write_config_dword(dev, PCI_ROM_ADDRESS, PCI_ROM_ADDRESS_MASK);
64 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr_reg);
65
66 if (!addr_reg) {
67
68 printf("pci_chadow_rom: device do not seem to have a rom\n");
69 return -1;
70 }
71
72 size = (~(addr_reg&PCI_ROM_ADDRESS_MASK))+1;
73
74#if 0
75 printf("ROM is %d bytes\n", size);
76#endif
77 rom_addr = pci_get_rom_window(hose, size);
78#if 0
79 printf("ROM mapped at %x \n", rom_addr);
80#endif
81 pci_write_config_dword(dev, PCI_ROM_ADDRESS,
82 pci_phys_to_mem(dev, rom_addr)
83 |PCI_ROM_ADDRESS_ENABLE);
84
85
86 for (i=rom_addr;i<rom_addr+size; i+=512) {
87
88
89 if (readw(i) == 0xaa55) {
90 u32 pci_data;
91#ifdef PCI_ROM_SCAN_VERBOSE
92 printf("ROM signature found\n");
93#endif
94 pci_data = readw(0x18+i);
95 pci_data += i;
96
97 if (0==memcmp((void*)pci_data, "PCIR", 4)) {
98#ifdef PCI_ROM_SCAN_VERBOSE
99 printf("Fount PCI rom image at offset %d\n", i-rom_addr);
100 printf("Vendor %04x device %04x class %06x\n",
101 readw(pci_data+4), readw(pci_data+6),
102 readl(pci_data+0x0d)&0xffffff);
103 printf("%s\n",
104 (readw(pci_data+0x15) &0x80)?
105 "Last image":"More images follow");
106 switch (readb(pci_data+0x14)) {
107 case 0:
108 printf("X86 code\n");
109 break;
110 case 1:
111 printf("Openfirmware code\n");
112 break;
113 case 2:
114 printf("PARISC code\n");
115 break;
116 }
117 printf("Image size %d\n", readw(pci_data+0x10) * 512);
118#endif
119
120
121
122 if (readw(pci_data+4) == vendor &&
123 readw(pci_data+6) == device &&
124
125 readb(pci_data+0x14) == 0 ) {
126#ifdef PCI_ROM_SCAN_VERBOSE
127 printf("Suitable ROM image found, copying\n");
128#endif
129 memmove(dest, (void*)rom_addr, readw(pci_data+0x10) * 512);
130 res = 0;
131 break;
132
133 }
134 if (readw(pci_data+0x15) &0x80) {
135 break;
136 }
137 }
138 }
139
140 }
141
142#ifdef PCI_ROM_SCAN_VERBOSE
143 if (res) {
144 printf("No suitable image found\n");
145 }
146#endif
147
148 pci_remove_rom_window(hose, rom_addr);
149
150 pci_write_config_dword(dev, PCI_ROM_ADDRESS, 0);
151
152 return res;
153}
154
155#endif
156