This patch corrects r2587. It makes sure that the VGA is initialized
[coreboot.git] / src / devices / pci_rom.c
1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/pci.h>
4 #include <device/pci_ids.h>
5 #include <device/pci_ops.h>
6
7 struct rom_header * pci_rom_probe(struct device *dev)
8 {
9         unsigned long rom_address;
10         struct rom_header *rom_header;
11         struct pci_data *rom_data;
12
13         if (dev->on_mainboard) {
14                 // in case some device PCI_ROM_ADDRESS can not be set or readonly 
15                 rom_address = dev->rom_address;
16         } else {
17                 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
18         }
19
20         if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
21                 return NULL;
22         }
23
24         printk_debug("rom address for %s = %x\n", dev_path(dev), rom_address);
25         
26         if(!dev->on_mainboard) {
27                 /* enable expansion ROM address decoding */
28                 pci_write_config32(dev, PCI_ROM_ADDRESS,
29                                    rom_address|PCI_ROM_ADDRESS_ENABLE);
30         }
31
32         rom_header = (struct rom_header *)rom_address;
33         printk_spew("PCI Expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
34                     le32_to_cpu(rom_header->signature),
35                     rom_header->size * 512, le32_to_cpu(rom_header->data));
36         if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
37                 printk_err("Incorrect Expansion ROM Header Signature %04x\n",
38                            le32_to_cpu(rom_header->signature));
39                 return NULL;
40         }
41
42         rom_data = (unsigned char *) rom_header + le32_to_cpu(rom_header->data);
43         printk_spew("PCI ROM Image, Vendor %04x, Device %04x,\n",
44                     rom_data->vendor, rom_data->device);
45         if (dev->vendor != rom_data->vendor || dev->device != rom_data->device) {
46                 printk_err("Device or Vendor ID mismatch Vendor %04x, Device %04x\n",
47                            rom_data->vendor, rom_data->device);
48                 return NULL;
49         }
50
51         printk_spew("PCI ROM Image,  Class Code %04x%02x, Code Type %02x\n",
52                     rom_data->class_hi, rom_data->class_lo,
53                     rom_data->type);
54         if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
55                 printk_debug("Class Code mismatch ROM %08x, dev %08x\n", 
56                             (rom_data->class_hi << 8) | rom_data->class_lo, dev->class);
57                 //return NULL;
58         }
59
60         return rom_header;
61 }
62
63 static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
64
65 struct rom_header *pci_rom_load(struct device *dev, struct rom_header *rom_header)
66 {
67         struct pci_data * rom_data;
68         unsigned long rom_address;
69         unsigned int rom_size;
70         unsigned int image_size=0;
71
72         rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
73
74         do {
75                 rom_header = (unsigned char *) rom_header + image_size; // get next image
76                 rom_data = (unsigned char *) rom_header + le32_to_cpu(rom_header->data);
77                 image_size = le32_to_cpu(rom_data->ilen) * 512;
78         } while ((rom_data->type!=0) && (rom_data->indicator!=0));  // make sure we got x86 version
79
80         if(rom_data->type!=0) return NULL;
81
82         rom_size = rom_header->size * 512;
83
84         if (PCI_CLASS_DISPLAY_VGA == rom_data->class_hi) {
85 #if CONFIG_CONSOLE_VGA == 1 && CONFIG_CONSOLE_VGA_MULTI == 0
86                 extern device_t vga_pri;        // the primary vga device, defined in device.c
87                 if (dev != vga_pri) return NULL; // only one VGA supported
88 #endif
89                 printk_debug("copying VGA ROM Image from 0x%x to 0x%x, 0x%x bytes\n",
90                             rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
91                 memcpy(PCI_VGA_RAM_IMAGE_START, rom_header, rom_size);
92                 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
93         } else {
94                 printk_debug("copying non-VGA ROM Image from 0x%x to 0x%x, 0x%x bytes\n",
95                             rom_header, pci_ram_image_start, rom_size);
96                 memcpy(pci_ram_image_start, rom_header, rom_size);
97                 pci_ram_image_start += rom_size;
98                 return (struct rom_header *) (pci_ram_image_start-rom_size);
99         }
100         /* disable expansion ROM address decoding */
101         pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address & ~PCI_ROM_ADDRESS_ENABLE);
102         
103         return NULL;
104 }