blob: 702f010eb543dd7c8f4ec23d4cb290f7e0aa28e1 [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>
Ronald G. Minnich662d52d2009-04-06 16:03:53 +000030#include <romfs.h>
Li-Ta Lo883b8792005-01-10 23:16:22 +000031
32struct rom_header * pci_rom_probe(struct device *dev)
33{
34 unsigned long rom_address;
35 struct rom_header *rom_header;
36 struct pci_data *rom_data;
37
Ronald G. Minnich662d52d2009-04-06 16:03:53 +000038 if (CONFIG_ROMFS) {
39 rom_address = (unsigned long) romfs_load_optionrom(dev->vendor, dev->device, NULL);
40 /* if it's in FLASH, then it's as if dev->on_mainboard was true */
41 dev->on_mainboard = 1;
42 /* and we might as well set the address correctly */
43 dev->rom_address = rom_address;
44 } else if (dev->on_mainboard) {
45 /* this is here as a legacy path. We hope it goes away soon. Users should not have to
46 * compute the ROM address at build time!
47 */
arch import user (historical)23053642005-07-06 16:49:59 +000048 // in case some device PCI_ROM_ADDRESS can not be set or readonly
49 rom_address = dev->rom_address;
50 } else {
51 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
52 }
Yinghai Lu9e4faef2005-01-14 22:04:49 +000053
Li-Ta Lo883b8792005-01-10 23:16:22 +000054 if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
arch import user (historical)23053642005-07-06 16:49:59 +000055 return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +000056 }
57
Myles Watsonc4ddbff2009-02-09 17:52:54 +000058 printk_debug("rom address for %s = %lx\n", dev_path(dev), rom_address);
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000059
60 if(!dev->on_mainboard) {
61 /* enable expansion ROM address decoding */
62 pci_write_config32(dev, PCI_ROM_ADDRESS,
63 rom_address|PCI_ROM_ADDRESS_ENABLE);
64 }
Li-Ta Lo883b8792005-01-10 23:16:22 +000065
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000066 rom_header = (struct rom_header *)rom_address;
67 printk_spew("PCI Expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
Yinghai Lu54ab3112005-01-18 03:10:46 +000068 le32_to_cpu(rom_header->signature),
Li-Ta Lo515f6c72005-01-11 22:48:54 +000069 rom_header->size * 512, le32_to_cpu(rom_header->data));
Li-Ta Lo883b8792005-01-10 23:16:22 +000070 if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
Yinghai Lu54ab3112005-01-18 03:10:46 +000071 printk_err("Incorrect Expansion ROM Header Signature %04x\n",
72 le32_to_cpu(rom_header->signature));
Li-Ta Lo883b8792005-01-10 23:16:22 +000073 return NULL;
74 }
75
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +000076 rom_data = (struct pci_data *) ((void *)rom_header + le32_to_cpu(rom_header->data));
Li-Ta Lobec039c2005-01-19 23:19:26 +000077 printk_spew("PCI ROM Image, Vendor %04x, Device %04x,\n",
Yinghai Lu54ab3112005-01-18 03:10:46 +000078 rom_data->vendor, rom_data->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +000079 if (dev->vendor != rom_data->vendor || dev->device != rom_data->device) {
Li-Ta Lobec039c2005-01-19 23:19:26 +000080 printk_err("Device or Vendor ID mismatch Vendor %04x, Device %04x\n",
81 rom_data->vendor, rom_data->device);
Li-Ta Lo883b8792005-01-10 23:16:22 +000082 return NULL;
83 }
84
Yinghai Lu54ab3112005-01-18 03:10:46 +000085 printk_spew("PCI ROM Image, Class Code %04x%02x, Code Type %02x\n",
86 rom_data->class_hi, rom_data->class_lo,
Li-Ta Lo515f6c72005-01-11 22:48:54 +000087 rom_data->type);
Yinghai Lu54ab3112005-01-18 03:10:46 +000088 if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000089 printk_debug("Class Code mismatch ROM %08x, dev %08x\n",
Yinghai Lu54ab3112005-01-18 03:10:46 +000090 (rom_data->class_hi << 8) | rom_data->class_lo, dev->class);
arch import user (historical)dc811182005-07-06 17:16:09 +000091 //return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +000092 }
93
94 return rom_header;
95}
96
Stefan Reinauer7ce8c542005-12-02 21:52:30 +000097static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;
Yinghai Lu9e4faef2005-01-14 22:04:49 +000098
Li-Ta Lo883b8792005-01-10 23:16:22 +000099struct rom_header *pci_rom_load(struct device *dev, struct rom_header *rom_header)
100{
101 struct pci_data * rom_data;
102 unsigned long rom_address;
103 unsigned int rom_size;
arch import user (historical)23053642005-07-06 16:49:59 +0000104 unsigned int image_size=0;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000105
106 rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
arch import user (historical)23053642005-07-06 16:49:59 +0000107
108 do {
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000109 rom_header = (struct rom_header *)((void *) rom_header + image_size); // get next image
110 rom_data = (struct pci_data *)((void *) rom_header + le32_to_cpu(rom_header->data));
arch import user (historical)23053642005-07-06 16:49:59 +0000111 image_size = le32_to_cpu(rom_data->ilen) * 512;
112 } while ((rom_data->type!=0) && (rom_data->indicator!=0)); // make sure we got x86 version
113
114 if(rom_data->type!=0) return NULL;
115
116 rom_size = rom_header->size * 512;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000117
Yinghai Lu54ab3112005-01-18 03:10:46 +0000118 if (PCI_CLASS_DISPLAY_VGA == rom_data->class_hi) {
Roman Kononovd6d7a112007-04-09 22:50:12 +0000119#if CONFIG_CONSOLE_VGA == 1 && CONFIG_CONSOLE_VGA_MULTI == 0
120 extern device_t vga_pri; // the primary vga device, defined in device.c
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000121 if (dev != vga_pri) return NULL; // only one VGA supported
Roman Kononovd6d7a112007-04-09 22:50:12 +0000122#endif
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000123 printk_debug("copying VGA ROM Image from %p to 0x%x, 0x%x bytes\n",
arch import user (historical)23053642005-07-06 16:49:59 +0000124 rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000125 memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header, rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000126 return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
127 } else {
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000128 printk_debug("copying non-VGA ROM Image from %p to %p, 0x%x bytes\n",
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000129 rom_header, pci_ram_image_start, rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000130 memcpy(pci_ram_image_start, rom_header, rom_size);
131 pci_ram_image_start += rom_size;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000132 return (struct rom_header *) (pci_ram_image_start-rom_size);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000133 }
134 /* disable expansion ROM address decoding */
135 pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address & ~PCI_ROM_ADDRESS_ENABLE);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000136
137 return NULL;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000138}