Add OPROM mapping support to coreboot
[coreboot.git] / src / devices / pci_rom.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2005 Li-Ta Lo <ollie@lanl.gov>
5  * Copyright (C) 2005 Tyan
6  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
7  * Copyright (C) 2005 Ronald G. Minnich <rminnich@gmail.com>
8  * Copyright (C) 2005-2007 Stefan Reinauer <stepan@openbios.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2 of the License.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22  */
23
24 #include <console/console.h>
25 #include <device/device.h>
26 #include <device/pci.h>
27 #include <device/pci_ids.h>
28 #include <device/pci_ops.h>
29 #include <string.h>
30 #include <cbfs.h>
31
32 struct rom_header *pci_rom_probe(struct device *dev)
33 {
34         struct rom_header *rom_header;
35         struct pci_data *rom_data;
36
37         /* If it's in FLASH, then don't check device for ROM. */
38         rom_header = cbfs_load_optionrom(dev->vendor, dev->device, NULL);
39
40         u32 vendev = dev->vendor | (dev->device << 16);
41         u32 mapped_vendev = vendev;
42
43         if (map_oprom_vendev)
44                 mapped_vendev = map_oprom_vendev(vendev);
45
46         if (!rom_header) {
47                 if (vendev != mapped_vendev) {
48                         rom_header = cbfs_load_optionrom(mapped_vendev &
49                                         0xffff, mapped_vendev >> 16, NULL);
50                 }
51         }
52
53         if (rom_header) {
54                 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
55                        dev_path(dev), rom_header);
56         } else {
57                 u32 rom_address;
58
59                 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
60
61                 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
62 #if CONFIG_BOARD_EMULATION_QEMU_X86
63                         if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
64                                 rom_address = 0xc0000;
65                         else
66 #endif
67                                 return NULL;
68                 } else {
69                         /* Enable expansion ROM address decoding. */
70                         pci_write_config32(dev, PCI_ROM_ADDRESS,
71                                            rom_address|PCI_ROM_ADDRESS_ENABLE);
72                 }
73
74                 printk(BIOS_DEBUG, "On card, ROM address for %s = %lx\n",
75                        dev_path(dev), (unsigned long)rom_address);
76                 rom_header = (struct rom_header *)rom_address;
77         }
78
79         printk(BIOS_SPEW, "PCI expansion ROM, signature 0x%04x, "
80                "INIT size 0x%04x, data ptr 0x%04x\n",
81                le32_to_cpu(rom_header->signature),
82                rom_header->size * 512, le32_to_cpu(rom_header->data));
83
84         if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
85                 printk(BIOS_ERR, "Incorrect expansion ROM header "
86                        "signature %04x\n", le32_to_cpu(rom_header->signature));
87                 return NULL;
88         }
89
90         rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
91
92         printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
93                rom_data->vendor, rom_data->device);
94         /* If the device id is mapped, a mismatch is expected */
95         if ((dev->vendor != rom_data->vendor
96             || dev->device != rom_data->device)
97             && (vendev == mapped_vendev)) {
98                 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, "
99                        "device ID %04x\n", rom_data->vendor, rom_data->device);
100                 return NULL;
101         }
102
103         printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, "
104                "Code Type %02x\n", rom_data->class_hi, rom_data->class_lo,
105                rom_data->type);
106
107         if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
108                 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
109                        (rom_data->class_hi << 8) | rom_data->class_lo,
110                        dev->class);
111                 // return NULL;
112         }
113
114         return rom_header;
115 }
116
117 static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
118
119 struct rom_header *pci_rom_load(struct device *dev,
120                                 struct rom_header *rom_header)
121 {
122         struct pci_data * rom_data;
123         unsigned int rom_size;
124         unsigned int image_size=0;
125
126         do {
127                 /* Get next image. */
128                 rom_header = (struct rom_header *)((void *) rom_header
129                                                             + image_size);
130
131                 rom_data = (struct pci_data *)((void *) rom_header
132                                 + le32_to_cpu(rom_header->data));
133
134                 image_size = le32_to_cpu(rom_data->ilen) * 512;
135         } while ((rom_data->type != 0) && (rom_data->indicator != 0)); // make sure we got x86 version
136
137         if (rom_data->type != 0)
138                 return NULL;
139
140         rom_size = rom_header->size * 512;
141
142         /*
143          * We check to see if the device thinks it is a VGA device not
144          * whether the ROM image is for a VGA device because some
145          * devices have a mismatch between the hardware and the ROM.
146          */
147         if (PCI_CLASS_DISPLAY_VGA == (dev->class >> 8)) {
148 #if CONFIG_MULTIPLE_VGA_ADAPTERS == 0
149                 extern device_t vga_pri; /* Primary VGA device (device.c). */
150                 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
151 #endif
152                 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
153                         printk(BIOS_DEBUG, "Copying VGA ROM Image from %p to "
154                                "0x%x, 0x%x bytes\n", rom_header,
155                                PCI_VGA_RAM_IMAGE_START, rom_size);
156                         memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
157                                rom_size);
158                 }
159                 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
160         }
161
162         printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x "
163                "bytes\n", rom_header, pci_ram_image_start, rom_size);
164
165         memcpy(pci_ram_image_start, rom_header, rom_size);
166         pci_ram_image_start += rom_size;
167         return (struct rom_header *) (pci_ram_image_start-rom_size);
168 }