summaryrefslogtreecommitdiff
path: root/src/devices/pci_rom.c
blob: 1b7e4bffa8b854b9444c383ebb1d9387673b9b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2005 Li-Ta Lo <ollie@lanl.gov>
 * Copyright (C) 2005 Tyan
 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
 * Copyright (C) 2005 Ronald G. Minnich <rminnich@gmail.com>
 * Copyright (C) 2005-2007 Stefan Reinauer <stepan@openbios.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <console/console.h>
#include <device/device.h>
#include <device/pci.h>
#include <device/pci_ids.h>
#include <device/pci_ops.h>
#include <string.h>

struct rom_header * pci_rom_probe(struct device *dev)
{
	unsigned long rom_address;
	struct rom_header *rom_header;
	struct pci_data *rom_data;

        if (dev->on_mainboard) {
                // in case some device PCI_ROM_ADDRESS can not be set or readonly 
		rom_address = dev->rom_address;
        } else {
		rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);
	}

	if (rom_address == 0x00000000 || rom_address == 0xffffffff) {
		return NULL;
	}

	printk_debug("rom address for %s = %x\n", dev_path(dev), rom_address);
	
	if(!dev->on_mainboard) {
		/* enable expansion ROM address decoding */
		pci_write_config32(dev, PCI_ROM_ADDRESS,
				   rom_address|PCI_ROM_ADDRESS_ENABLE);
	}

	rom_header = (struct rom_header *)rom_address;
	printk_spew("PCI Expansion ROM, signature 0x%04x, INIT size 0x%04x, data ptr 0x%04x\n",
		    le32_to_cpu(rom_header->signature),
		    rom_header->size * 512, le32_to_cpu(rom_header->data));
	if (le32_to_cpu(rom_header->signature) != PCI_ROM_HDR) {
		printk_err("Incorrect Expansion ROM Header Signature %04x\n",
			   le32_to_cpu(rom_header->signature));
		return NULL;
	}

	rom_data = (struct pci_data *) ((void *)rom_header + le32_to_cpu(rom_header->data));
	printk_spew("PCI ROM Image, Vendor %04x, Device %04x,\n",
		    rom_data->vendor, rom_data->device);
	if (dev->vendor != rom_data->vendor || dev->device != rom_data->device) {
		printk_err("Device or Vendor ID mismatch Vendor %04x, Device %04x\n",
			   rom_data->vendor, rom_data->device);
		return NULL;
	}

	printk_spew("PCI ROM Image,  Class Code %04x%02x, Code Type %02x\n",
		    rom_data->class_hi, rom_data->class_lo,
		    rom_data->type);
	if (dev->class != ((rom_data->class_hi << 8) | rom_data->class_lo)) {
		printk_debug("Class Code mismatch ROM %08x, dev %08x\n", 
			    (rom_data->class_hi << 8) | rom_data->class_lo, dev->class);
		//return NULL;
	}

	return rom_header;
}

static void *pci_ram_image_start = (void *)PCI_RAM_IMAGE_START;

struct rom_header *pci_rom_load(struct device *dev, struct rom_header *rom_header)
{
	struct pci_data * rom_data;
	unsigned long rom_address;
	unsigned int rom_size;
	unsigned int image_size=0;

	rom_address = pci_read_config32(dev, PCI_ROM_ADDRESS);

	do {
		rom_header = (struct rom_header *)((void *) rom_header + image_size); // get next image
	        rom_data = (struct pci_data *)((void *) rom_header + le32_to_cpu(rom_header->data));
        	image_size = le32_to_cpu(rom_data->ilen) * 512;
	} while ((rom_data->type!=0) && (rom_data->indicator!=0));  // make sure we got x86 version

	if(rom_data->type!=0) return NULL;

	rom_size = rom_header->size * 512;

	if (PCI_CLASS_DISPLAY_VGA == rom_data->class_hi) {
#if CONFIG_CONSOLE_VGA == 1 && CONFIG_CONSOLE_VGA_MULTI == 0
		extern device_t vga_pri;	// the primary vga device, defined in device.c
		if (dev != vga_pri) return NULL; // only one VGA supported
#endif
		printk_debug("copying VGA ROM Image from 0x%x to 0x%x, 0x%x bytes\n",
			    rom_header, PCI_VGA_RAM_IMAGE_START, rom_size);
		memcpy((void *)PCI_VGA_RAM_IMAGE_START, rom_header, rom_size);
		return (struct rom_header *) (PCI_VGA_RAM_IMAGE_START);
	} else {
		printk_debug("copying non-VGA ROM Image from 0x%x to 0x%x, 0x%x bytes\n",
			    rom_header, pci_ram_image_start, rom_size);
		memcpy(pci_ram_image_start, rom_header, rom_size);
		pci_ram_image_start += rom_size;
		return (struct rom_header *) (pci_ram_image_start-rom_size);
	}
	/* disable expansion ROM address decoding */
	pci_write_config32(dev, PCI_ROM_ADDRESS, rom_address & ~PCI_ROM_ADDRESS_ENABLE);
	
	return NULL;
}