blob: 71878dd7dcbed21c6a4f1adf6bd4534c0be5bb36 [file] [log] [blame]
wdenkabda5ca2003-05-31 18:35:21 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenkabda5ca2003-05-31 18:35:21 +00004 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
wdenk57b2d802003-06-27 21:31:46 +000024#include <common.h>
25#include <pci.h>
26#include <asm/io.h>
27#include <asm/pci.h>
wdenkabda5ca2003-05-31 18:35:21 +000028
wdenkabda5ca2003-05-31 18:35:21 +000029#undef PCI_ROM_SCAN_VERBOSE
30
wdenk57b2d802003-06-27 21:31:46 +000031int pci_shadow_rom(pci_dev_t dev, unsigned char *dest)
wdenkabda5ca2003-05-31 18:35:21 +000032{
33 struct pci_controller *hose;
34 int res = -1;
35 int i;
wdenk57b2d802003-06-27 21:31:46 +000036
wdenkabda5ca2003-05-31 18:35:21 +000037 u32 rom_addr;
38 u32 addr_reg;
39 u32 size;
wdenk57b2d802003-06-27 21:31:46 +000040
wdenkabda5ca2003-05-31 18:35:21 +000041 u16 vendor;
42 u16 device;
43 u32 class_code;
44
Graeme Russ883c6032011-11-08 02:33:15 +000045 u32 pci_data;
46
wdenkabda5ca2003-05-31 18:35:21 +000047 hose = pci_bus_to_hose(PCI_BUS(dev));
Graeme Russ883c6032011-11-08 02:33:15 +000048
49 debug("pci_shadow_rom() asked to shadow device %x to %x\n",
wdenkabda5ca2003-05-31 18:35:21 +000050 dev, (u32)dest);
Graeme Russ883c6032011-11-08 02:33:15 +000051
wdenkabda5ca2003-05-31 18:35:21 +000052 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
53 pci_read_config_word(dev, PCI_DEVICE_ID, &device);
54 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_code);
wdenk57b2d802003-06-27 21:31:46 +000055
Wolfgang Denka1be4762008-05-20 16:00:29 +020056 class_code &= 0xffffff00;
wdenkabda5ca2003-05-31 18:35:21 +000057 class_code >>= 8;
58
Graeme Russ45fc1d82011-04-13 19:43:26 +100059 debug("PCI Header Vendor %04x device %04x class %06x\n",
wdenkabda5ca2003-05-31 18:35:21 +000060 vendor, device, class_code);
Graeme Russ45fc1d82011-04-13 19:43:26 +100061
wdenkabda5ca2003-05-31 18:35:21 +000062 /* Enable the rom addess decoder */
Graeme Russd0811ab2009-08-23 12:59:50 +100063 pci_write_config_dword(dev, PCI_ROM_ADDRESS, (u32)PCI_ROM_ADDRESS_MASK);
wdenkabda5ca2003-05-31 18:35:21 +000064 pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr_reg);
65
66 if (!addr_reg) {
67 /* register unimplemented */
68 printf("pci_chadow_rom: device do not seem to have a rom\n");
69 return -1;
70 }
wdenk57b2d802003-06-27 21:31:46 +000071
Graeme Russ883c6032011-11-08 02:33:15 +000072 size = (~(addr_reg&PCI_ROM_ADDRESS_MASK)) + 1;
wdenk57b2d802003-06-27 21:31:46 +000073
Graeme Russ45fc1d82011-04-13 19:43:26 +100074 debug("ROM is %d bytes\n", size);
75
wdenkabda5ca2003-05-31 18:35:21 +000076 rom_addr = pci_get_rom_window(hose, size);
Graeme Russ45fc1d82011-04-13 19:43:26 +100077
78 debug("ROM mapped at %x\n", rom_addr);
79
wdenk57b2d802003-06-27 21:31:46 +000080 pci_write_config_dword(dev, PCI_ROM_ADDRESS,
wdenkabda5ca2003-05-31 18:35:21 +000081 pci_phys_to_mem(dev, rom_addr)
82 |PCI_ROM_ADDRESS_ENABLE);
83
84
Graeme Russ883c6032011-11-08 02:33:15 +000085 for (i = rom_addr; i < rom_addr + size; i += 512) {
wdenkabda5ca2003-05-31 18:35:21 +000086 if (readw(i) == 0xaa55) {
wdenkabda5ca2003-05-31 18:35:21 +000087#ifdef PCI_ROM_SCAN_VERBOSE
88 printf("ROM signature found\n");
wdenk57b2d802003-06-27 21:31:46 +000089#endif
Graeme Russ883c6032011-11-08 02:33:15 +000090 pci_data = readw(0x18 + i);
wdenkabda5ca2003-05-31 18:35:21 +000091 pci_data += i;
wdenk57b2d802003-06-27 21:31:46 +000092
Graeme Russ883c6032011-11-08 02:33:15 +000093 if (0 == memcmp((void *)pci_data, "PCIR", 4)) {
wdenk57b2d802003-06-27 21:31:46 +000094#ifdef PCI_ROM_SCAN_VERBOSE
Graeme Russ883c6032011-11-08 02:33:15 +000095 printf("Fount PCI rom image at offset %d\n",
96 i - rom_addr);
wdenkabda5ca2003-05-31 18:35:21 +000097 printf("Vendor %04x device %04x class %06x\n",
Graeme Russ883c6032011-11-08 02:33:15 +000098 readw(pci_data + 4), readw(pci_data + 6),
99 readl(pci_data + 0x0d) & 0xffffff);
wdenk57b2d802003-06-27 21:31:46 +0000100 printf("%s\n",
Graeme Russ883c6032011-11-08 02:33:15 +0000101 (readw(pci_data + 0x15) & 0x80) ?
102 "Last image" : "More images follow");
103 switch (readb(pci_data + 0x14)) {
wdenkabda5ca2003-05-31 18:35:21 +0000104 case 0:
105 printf("X86 code\n");
106 break;
107 case 1:
108 printf("Openfirmware code\n");
109 break;
110 case 2:
111 printf("PARISC code\n");
112 break;
113 }
Graeme Russ883c6032011-11-08 02:33:15 +0000114 printf("Image size %d\n",
115 readw(pci_data + 0x10) * 512);
wdenk57b2d802003-06-27 21:31:46 +0000116#endif
Graeme Russ883c6032011-11-08 02:33:15 +0000117 /*
118 * FixMe: I think we should compare the class
119 * code bytes as well but I have no reference
120 * on the exact order of these bytes in the PCI
121 * ROM header
122 */
123 if (readw(pci_data + 4) == vendor &&
124 readw(pci_data + 6) == device &&
125 readb(pci_data + 0x14) == 0) {
wdenk57b2d802003-06-27 21:31:46 +0000126#ifdef PCI_ROM_SCAN_VERBOSE
Graeme Russ883c6032011-11-08 02:33:15 +0000127 printf("Suitable ROM image found\n");
wdenk57b2d802003-06-27 21:31:46 +0000128#endif
Graeme Russ883c6032011-11-08 02:33:15 +0000129 memmove(dest, (void *)rom_addr,
130 readw(pci_data + 0x10) * 512);
wdenkabda5ca2003-05-31 18:35:21 +0000131 res = 0;
132 break;
wdenk57b2d802003-06-27 21:31:46 +0000133
wdenkabda5ca2003-05-31 18:35:21 +0000134 }
Graeme Russ883c6032011-11-08 02:33:15 +0000135
136 if (readw(pci_data + 0x15) & 0x80)
wdenkabda5ca2003-05-31 18:35:21 +0000137 break;
wdenkabda5ca2003-05-31 18:35:21 +0000138 }
139 }
wdenk57b2d802003-06-27 21:31:46 +0000140
wdenkabda5ca2003-05-31 18:35:21 +0000141 }
wdenk57b2d802003-06-27 21:31:46 +0000142
wdenkabda5ca2003-05-31 18:35:21 +0000143#ifdef PCI_ROM_SCAN_VERBOSE
Graeme Russ883c6032011-11-08 02:33:15 +0000144 if (res)
wdenkabda5ca2003-05-31 18:35:21 +0000145 printf("No suitable image found\n");
wdenk57b2d802003-06-27 21:31:46 +0000146#endif
wdenkabda5ca2003-05-31 18:35:21 +0000147 /* disable PAR register and PCI device ROM address devocer */
148 pci_remove_rom_window(hose, rom_addr);
wdenk57b2d802003-06-27 21:31:46 +0000149
wdenkabda5ca2003-05-31 18:35:21 +0000150 pci_write_config_dword(dev, PCI_ROM_ADDRESS, 0);
151
152 return res;
153}
Graeme Russ19fc0d52011-11-08 02:33:20 +0000154
155#ifdef PCI_BIOS_DEBUG
156
157void print_bios_bios_stat(void)
158{
159 printf("16 bit functions:\n");
160 printf("pci_bios_present: %d\n",
161 RELOC_16_LONG(0xf000, num_pci_bios_present));
162 printf("pci_bios_find_device: %d\n",
163 RELOC_16_LONG(0xf000, num_pci_bios_find_device));
164 printf("pci_bios_find_class: %d\n",
165 RELOC_16_LONG(0xf000, num_pci_bios_find_class));
166 printf("pci_bios_generate_special_cycle: %d\n",
167 RELOC_16_LONG(0xf000,
168 num_pci_bios_generate_special_cycle));
169 printf("pci_bios_read_cfg_byte: %d\n",
170 RELOC_16_LONG(0xf000, num_pci_bios_read_cfg_byte));
171 printf("pci_bios_read_cfg_word: %d\n",
172 RELOC_16_LONG(0xf000, num_pci_bios_read_cfg_word));
173 printf("pci_bios_read_cfg_dword: %d\n",
174 RELOC_16_LONG(0xf000, num_pci_bios_read_cfg_dword));
175 printf("pci_bios_write_cfg_byte: %d\n",
176 RELOC_16_LONG(0xf000, num_pci_bios_write_cfg_byte));
177 printf("pci_bios_write_cfg_word: %d\n",
178 RELOC_16_LONG(0xf000, num_pci_bios_write_cfg_word));
179 printf("pci_bios_write_cfg_dword: %d\n",
180 RELOC_16_LONG(0xf000, num_pci_bios_write_cfg_dword));
181 printf("pci_bios_get_irq_routing: %d\n",
182 RELOC_16_LONG(0xf000, num_pci_bios_get_irq_routing));
183 printf("pci_bios_set_irq: %d\n",
184 RELOC_16_LONG(0xf000, num_pci_bios_set_irq));
185 printf("pci_bios_unknown_function: %d\n",
186 RELOC_16_LONG(0xf000, num_pci_bios_unknown_function));
187}
188#endif