blob: fe67515936a0875ed7df7f81bb25a4685b3fdeb8 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
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
Li-Ta Lo883b8792005-01-10 23:16:22 +000024#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>
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +000029#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000030#include <cbfs.h>
Li-Ta Lo883b8792005-01-10 23:16:22 +000031
Uwe Hermannc1ee4292010-10-17 19:01:48 +000032struct rom_header *pci_rom_probe(struct device *dev)
Li-Ta Lo883b8792005-01-10 23:16:22 +000033{
Li-Ta Lo883b8792005-01-10 23:16:22 +000034 struct rom_header *rom_header;
35 struct pci_data *rom_data;
36
Myles Watsond27c08c2009-11-06 23:42:26 +000037 /* If it's in FLASH, then don't check device for ROM. */
38 rom_header = cbfs_load_optionrom(dev->vendor, dev->device, NULL);
Ronald G. Minnich308312c2009-04-06 20:38:34 +000039
Martin Rothe9dfdd92012-04-26 16:04:18 -060040 u32 vendev = (dev->vendor << 16) | dev->device;
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080041 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) {
Martin Rothe9dfdd92012-04-26 16:04:18 -060048 rom_header = cbfs_load_optionrom(mapped_vendev >> 16,
49 mapped_vendev & 0xffff , NULL);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -080050 }
51 }
52
Myles Watsond27c08c2009-11-06 23:42:26 +000053 if (rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +000054 printk(BIOS_DEBUG, "In CBFS, ROM address for %s = %p\n",
55 dev_path(dev), rom_header);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +000056 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000057 u32 rom_address;
Myles Watsond27c08c2009-11-06 23:42:26 +000058
arch import user (historical)23053642005-07-06 16:49:59 +000059 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
Myles Watsond27c08c2009-11-06 23:42:26 +000060
61 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
Stefan Reinauer1d888a92011-04-21 20:24:43 +000062#if CONFIG_BOARD_EMULATION_QEMU_X86
Myles Watsonae3e9982009-11-17 15:20:22 +000063 if ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
64 rom_address = 0xc0000;
65 else
Uwe Hermanne4870472010-11-04 23:23:47 +000066#endif
Myles Watsonae3e9982009-11-17 15:20:22 +000067 return NULL;
Myles Watsond27c08c2009-11-06 23:42:26 +000068 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +000069 /* Enable expansion ROM address decoding. */
Myles Watsond27c08c2009-11-06 23:42:26 +000070 pci_write_config32(dev, PCI_ROM_ADDRESS,
71 rom_address|PCI_ROM_ADDRESS_ENABLE);
72 }
73
Stefan Reinauerafaa2572011-10-06 16:47:51 -070074#if CONFIG_ON_DEVICE_ROM_RUN
75 printk(BIOS_DEBUG, "Option ROM address for %s = %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +000076 dev_path(dev), (unsigned long)rom_address);
Myles Watsond27c08c2009-11-06 23:42:26 +000077 rom_header = (struct rom_header *)rom_address;
Stefan Reinauerafaa2572011-10-06 16:47:51 -070078#else
79 printk(BIOS_DEBUG, "Option ROM execution disabled "
80 "for %s\n", dev_path(dev));
81 return NULL;
82#endif
arch import user (historical)23053642005-07-06 16:49:59 +000083 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +000084
Uwe Hermanne4870472010-11-04 23:23:47 +000085 printk(BIOS_SPEW, "PCI expansion ROM, signature 0x%04x, "
86 "INIT size 0x%04x, data ptr 0x%04x\n",
87 le32_to_cpu(rom_header->signature),
88 rom_header->size * 512, le32_to_cpu(rom_header->data));
89
Li-Ta Lo883b8792005-01-10 23:16:22 +000090 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Uwe Hermanne4870472010-11-04 23:23:47 +000091 printk(BIOS_ERR, "Incorrect expansion ROM header "
92 "signature %04x\n", le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +000093 return NULL;
94 }
95
Myles Watsond27c08c2009-11-06 23:42:26 +000096 rom_data = (((void *)rom_header) + le32_to_cpu(rom_header->data));
97
Uwe Hermanne4870472010-11-04 23:23:47 +000098 printk(BIOS_SPEW, "PCI ROM image, vendor ID %04x, device ID %04x,\n",
99 rom_data->vendor, rom_data->device);
Stefan Reinauerc0a6c6b2012-01-23 14:17:52 -0800100 /* If the device id is mapped, a mismatch is expected */
101 if ((dev->vendor != rom_data->vendor
102 || dev->device != rom_data->device)
103 && (vendev == mapped_vendev)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000104 printk(BIOS_ERR, "ID mismatch: vendor ID %04x, "
105 "device ID %04x\n", rom_data->vendor, rom_data->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000106 return NULL;
107 }
108
Uwe Hermanne4870472010-11-04 23:23:47 +0000109 printk(BIOS_SPEW, "PCI ROM image, Class Code %04x%02x, "
110 "Code Type %02x\n", rom_data->class_hi, rom_data->class_lo,
111 rom_data->type);
112
Yinghai Lu54ab3112005-01-18 03:10:46 +0000113 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000114 printk(BIOS_DEBUG, "Class Code mismatch ROM %08x, dev %08x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000115 (rom_data->class_hi << 8) | rom_data->class_lo,
116 dev->class);
117 // return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000118 }
119
120 return rom_header;
121}
122
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000123static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000124
Uwe Hermanne4870472010-11-04 23:23:47 +0000125struct rom_header *pci_rom_load(struct device *dev,
126 struct rom_header *rom_header)
Li-Ta Lo883b8792005-01-10 23:16:22 +0000127{
128 struct pci_data * rom_data;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000129 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000130 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000131
arch import user (historical)23053642005-07-06 16:49:59 +0000132 do {
Uwe Hermanne4870472010-11-04 23:23:47 +0000133 /* Get next image. */
134 rom_header = (struct rom_header *)((void *) rom_header
135 + image_size);
136
137 rom_data = (struct pci_data *)((void *) rom_header
138 + le32_to_cpu(rom_header->data));
139
140 image_size = le32_to_cpu(rom_data->ilen) * 512;
141 } while ((rom_data->type != 0) && (rom_data->indicator != 0)); // make sure we got x86 version
arch import user (historical)23053642005-07-06 16:49:59 +0000142
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000143 if (rom_data->type != 0)
144 return NULL;
arch import user (historical)23053642005-07-06 16:49:59 +0000145
146 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000147
Uwe Hermanne4870472010-11-04 23:23:47 +0000148 /*
149 * We check to see if the device thinks it is a VGA device not
150 * whether the ROM image is for a VGA device because some
151 * devices have a mismatch between the hardware and the ROM.
152 */
Mark Marshall2fa7c2e2009-11-05 08:10:12 +0000153 if (PCI_CLASS_DISPLAY_VGA == (dev->class >> 8)) {
Patrick Georgie1667822012-05-05 15:29:32 +0200154#if !CONFIG_MULTIPLE_VGA_ADAPTERS
Uwe Hermanne4870472010-11-04 23:23:47 +0000155 extern device_t vga_pri; /* Primary VGA device (device.c). */
156 if (dev != vga_pri) return NULL; /* Only one VGA supported. */
Roman Kononovd6d7a112007-04-09 22:50:12 +0000157#endif
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000158 if ((void *)PCI_VGA_RAM_IMAGE_START != rom_header) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000159 printk(BIOS_DEBUG, "Copying VGA ROM Image from %p to "
160 "0x%x, 0x%x bytes\n", rom_header,
161 PCI_VGA_RAM_IMAGE_START, rom_size);
162 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header,
163 rom_size);
Ronald G. Minnicha88db7b2009-06-09 14:44:37 +0000164 }
Li-Ta Lo883b8792005-01-10 23:16:22 +0000165 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000166 }
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000167
Uwe Hermanne4870472010-11-04 23:23:47 +0000168 printk(BIOS_DEBUG, "Copying non-VGA ROM image from %p to %p, 0x%x "
169 "bytes\n", rom_header, pci_ram_image_start, rom_size);
Stefan Reinauer4d59eaa2009-04-22 16:32:18 +0000170
171 memcpy(pci_ram_image_start, rom_header, rom_size);
172 pci_ram_image_start += rom_size;
173 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000174}