17ddb8f658b7439c015865b7b6938d849268fd63
[coreboot.git] / src / devices / pci_rom.c
1 /*
2  * This file is part of the LinuxBIOS 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
30 struct rom_header * pci_rom_probe(struct device *dev)
31 {
32         unsigned long rom_address;
33         struct rom_header *rom_header;
34         struct pci_data *rom_data;
35
36         if (dev->on_mainboard) {
37                 // in case some device PCI_ROM_ADDRESS can not be set or readonly 
38                 rom_address = dev->rom_address;
39         } else {
40                 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
41         }
42
43         if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
44                 return NULL;
45         }
46
47         printk_debug("rom address for %s = %x\n", dev_path(dev), rom_address);
48         
49         if(!dev->on_mainboard) {
50                 /* enable expansion ROM address decoding */
51                 pci_write_config32(dev, PCI_ROM_ADDRESS,
52                                    rom_address|PCI_ROM_ADDRESS_ENABLE);
53         }
54
55         rom_header = (struct rom_header *)rom_address;
56         printk_spew("PCI Expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
57                     le32_to_cpu(rom_header->signature),
58                     rom_header->size * 512, le32_to_cpu(rom_header->data));
59         if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
60                 printk_err("Incorrect Expansion ROM Header Signature %04x\n",
61                            le32_to_cpu(rom_header->signature));
62                 return NULL;
63         }
64
65         rom_data = (unsigned char *) rom_header + le32_to_cpu(rom_header->data);
66         printk_spew("PCI ROM Image, Vendor %04x, Device %04x,\n",
67                     rom_data->vendor, rom_data->device);
68         if (dev->vendor != rom_data->vendor || dev->device != rom_data->device) {
69                 printk_err("Device or Vendor ID mismatch Vendor %04x, Device %04x\n",
70                            rom_data->vendor, rom_data->device);
71                 return NULL;
72         }
73
74         printk_spew("PCI ROM Image,  Class Code %04x%02x, Code Type %02x\n",
75                     rom_data->class_hi, rom_data->class_lo,
76                     rom_data->type);
77         if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
78                 printk_debug("Class Code mismatch ROM %08x, dev %08x\n", 
79                             (rom_data->class_hi << 8) | rom_data->class_lo, dev->class);
80                 //return NULL;
81         }
82
83         return rom_header;
84 }
85
86 static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
87
88 struct rom_header *pci_rom_load(struct device *dev, struct rom_header *rom_header)
89 {
90         struct pci_data * rom_data;
91         unsigned long rom_address;
92         unsigned int rom_size;
93         unsigned int image_size=0;
94
95         rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
96
97         do {
98                 rom_header = (unsigned char *) rom_header + image_size; // get next image
99                 rom_data = (unsigned char *) rom_header + le32_to_cpu(rom_header->data);
100                 image_size = le32_to_cpu(rom_data->ilen) * 512;
101         } while ((rom_data->type!=0) && (rom_data->indicator!=0));  // make sure we got x86 version
102
103         if(rom_data->type!=0) return NULL;
104
105         rom_size = rom_header->size * 512;
106
107         if (PCI_CLASS_DISPLAY_VGA == rom_data->class_hi) {
108 #if CONFIG_CONSOLE_VGA == 1 && CONFIG_CONSOLE_VGA_MULTI == 0
109                 extern device_t vga_pri;        // the primary vga device, defined in device.c
110                 if (dev != vga_pri) return NULL; // only one VGA supported
111 #endif
112                 printk_debug("copying VGA ROM Image from 0x%x to 0x%x, 0x%x bytes\n",
113                             rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
114                 memcpy(PCI_VGA_RAM_IMAGE_START, rom_header, rom_size);
115                 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
116         } else {
117                 printk_debug("copying non-VGA ROM Image from 0x%x to 0x%x, 0x%x bytes\n",
118                             rom_header, pci_ram_image_start, rom_size);
119                 memcpy(pci_ram_image_start, rom_header, rom_size);
120                 pci_ram_image_start += rom_size;
121                 return (struct rom_header *) (pci_ram_image_start-rom_size);
122         }
123         /* disable expansion ROM address decoding */
124         pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address & ~PCI_ROM_ADDRESS_ENABLE);
125         
126         return NULL;
127 }