works for PCI vga cards too
[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         rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
14         if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
15                 /* FixME: search in the LinuxBIOS Image for integrated
16                  * devices? */
17                 return NULL;
18         }
19
20         printk_spew("%s, rom address for %s = %x\n",
21                     __func__, dev_path(dev), rom_address);
22
23         /* enable expansion ROM address decoding */
24         pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address|PCI_ROM_ADDRESS_ENABLE);
25
26         rom_header = rom_address;
27         printk_spew("%s, PCI Expansion ROM, signature 0x%04x, \n\t"
28                     "INIT size 0x%04x, data ptr 0x%04x\n",
29                     __func__, le32_to_cpu(rom_header->signature),
30                     rom_header->size * 512, le32_to_cpu(rom_header->data));
31         if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
32                 printk_err("%s, Incorrect Expansion ROM Header Signature %04x\n",
33                            __func__, le32_to_cpu(rom_header->signature));
34                 return NULL;
35         }
36
37         rom_data = (unsigned char *) rom_header + le32_to_cpu(rom_header->data);
38         printk_spew("%s, PCI ROM Image,  Vendor %04x, Device %04x,\n",
39                     __func__, rom_data->vendor, rom_data->device);
40         if (dev->vendor != rom_data->vendor || dev->device != rom_data->device) {
41                 printk_err("%s, Device or Vendor ID mismatch\n");
42                 return NULL;
43         }
44
45         printk_spew("%s, PCI ROM Image,  Class Code %02x%04x, Code Type %02x\n",
46                     __func__, rom_data->class_hi, rom_data->class_lo,
47                     rom_data->type);
48         if ((dev->class >> 8) != (rom_data->class_hi << 16 | rom_data->class_lo)) {
49                 printk_err("%s, Class Code mismatch %x\n",
50                            __func__, dev->class);
51                 return NULL;
52         }
53
54         return rom_header;
55 }
56
57 static void *pci_ram_image_start = PCI_RAM_IMAGE_START;
58 struct rom_header *pci_rom_load(struct device *dev, struct rom_header *rom_header)
59 {
60         struct pci_data * rom_data;
61         unsigned long rom_address;
62         unsigned int rom_size;
63
64         rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
65         rom_data = (unsigned char *) rom_header + le32_to_cpu(rom_header->data);
66         rom_size = rom_header->size*512;
67
68         if (PCI_CLASS_DISPLAY_VGA == (rom_data->class_hi << 16 | rom_data->class_lo)) {
69                 printk_spew("%s, copying VGA ROM Image from %x to %x, %x bytes\n",
70                             __func__, rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
71                 memcpy(PCI_VGA_RAM_IMAGE_START, rom_header, rom_size);
72                 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
73         } else {
74                 printk_spew("%s, copying non-VGA ROM Image from %x to %x, %x bytes\n",
75                             __func__, rom_header, pci_ram_image_start, rom_size);
76                 memcpy(pci_ram_image_start, rom_header, rom_size);
77                 pci_ram_image_start += rom_size;
78                 return (struct rom_header *) pci_ram_image_start;
79         }
80         /* disable expansion ROM address decoding */
81         pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address & ~PCI_ROM_ADDRESS_ENABLE);
82 }