wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
Graeme Russ | 45fc1d8 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 3 | * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se> |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 4 | * |
| 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 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 24 | #include <common.h> |
| 25 | #include <pci.h> |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/pci.h> |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 28 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 29 | #undef PCI_ROM_SCAN_VERBOSE |
| 30 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 31 | int pci_shadow_rom(pci_dev_t dev, unsigned char *dest) |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 32 | { |
| 33 | struct pci_controller *hose; |
| 34 | int res = -1; |
| 35 | int i; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 36 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 37 | u32 rom_addr; |
| 38 | u32 addr_reg; |
| 39 | u32 size; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 40 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 41 | u16 vendor; |
| 42 | u16 device; |
| 43 | u32 class_code; |
| 44 | |
| 45 | hose = pci_bus_to_hose(PCI_BUS(dev)); |
| 46 | #if 0 |
| 47 | printf("pci_shadow_rom() asked to shadow device %x to %x\n", |
| 48 | dev, (u32)dest); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 49 | #endif |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 50 | pci_read_config_word(dev, PCI_VENDOR_ID, &vendor); |
| 51 | pci_read_config_word(dev, PCI_DEVICE_ID, &device); |
| 52 | pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_code); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 53 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 54 | class_code &= 0xffffff00; |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 55 | class_code >>= 8; |
| 56 | |
Graeme Russ | 45fc1d8 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 57 | debug("PCI Header Vendor %04x device %04x class %06x\n", |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 58 | vendor, device, class_code); |
Graeme Russ | 45fc1d8 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 59 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 60 | /* Enable the rom addess decoder */ |
Graeme Russ | d0811ab | 2009-08-23 12:59:50 +1000 | [diff] [blame] | 61 | pci_write_config_dword(dev, PCI_ROM_ADDRESS, (u32)PCI_ROM_ADDRESS_MASK); |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 62 | pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr_reg); |
| 63 | |
| 64 | if (!addr_reg) { |
| 65 | /* register unimplemented */ |
| 66 | printf("pci_chadow_rom: device do not seem to have a rom\n"); |
| 67 | return -1; |
| 68 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 69 | |
| 70 | size = (~(addr_reg&PCI_ROM_ADDRESS_MASK))+1; |
| 71 | |
Graeme Russ | 45fc1d8 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 72 | debug("ROM is %d bytes\n", size); |
| 73 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 74 | rom_addr = pci_get_rom_window(hose, size); |
Graeme Russ | 45fc1d8 | 2011-04-13 19:43:26 +1000 | [diff] [blame] | 75 | |
| 76 | debug("ROM mapped at %x\n", rom_addr); |
| 77 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 78 | pci_write_config_dword(dev, PCI_ROM_ADDRESS, |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 79 | pci_phys_to_mem(dev, rom_addr) |
| 80 | |PCI_ROM_ADDRESS_ENABLE); |
| 81 | |
| 82 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 83 | for (i=rom_addr;i<rom_addr+size; i+=512) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 84 | |
| 85 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 86 | if (readw(i) == 0xaa55) { |
| 87 | u32 pci_data; |
| 88 | #ifdef PCI_ROM_SCAN_VERBOSE |
| 89 | printf("ROM signature found\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 90 | #endif |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 91 | pci_data = readw(0x18+i); |
| 92 | pci_data += i; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 93 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 94 | if (0==memcmp((void*)pci_data, "PCIR", 4)) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 95 | #ifdef PCI_ROM_SCAN_VERBOSE |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 96 | printf("Fount PCI rom image at offset %d\n", i-rom_addr); |
| 97 | printf("Vendor %04x device %04x class %06x\n", |
| 98 | readw(pci_data+4), readw(pci_data+6), |
| 99 | readl(pci_data+0x0d)&0xffffff); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 100 | printf("%s\n", |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 101 | (readw(pci_data+0x15) &0x80)? |
| 102 | "Last image":"More images follow"); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 103 | switch (readb(pci_data+0x14)) { |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 104 | 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 | } |
| 114 | printf("Image size %d\n", readw(pci_data+0x10) * 512); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 115 | #endif |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 116 | /* FixMe: I think we should compare the class code |
| 117 | * bytes as well but I have no reference on the |
| 118 | * exact order of these bytes in the PCI ROM header */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 119 | if (readw(pci_data+4) == vendor && |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 120 | readw(pci_data+6) == device && |
| 121 | /* (readl(pci_data+0x0d)&0xffffff) == class_code && */ |
| 122 | readb(pci_data+0x14) == 0 /* x86 code image */ ) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 123 | #ifdef PCI_ROM_SCAN_VERBOSE |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 124 | printf("Suitable ROM image found, copying\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | #endif |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 126 | memmove(dest, (void*)rom_addr, readw(pci_data+0x10) * 512); |
| 127 | res = 0; |
| 128 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 129 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 130 | } |
| 131 | if (readw(pci_data+0x15) &0x80) { |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 136 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 137 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 139 | #ifdef PCI_ROM_SCAN_VERBOSE |
| 140 | if (res) { |
| 141 | printf("No suitable image found\n"); |
| 142 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | #endif |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 144 | /* disable PAR register and PCI device ROM address devocer */ |
| 145 | pci_remove_rom_window(hose, rom_addr); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 146 | |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 147 | pci_write_config_dword(dev, PCI_ROM_ADDRESS, 0); |
| 148 | |
| 149 | return res; |
| 150 | } |