Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 24 | #include "programmer.h" |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 25 | |
| 26 | uint32_t io_base_addr; |
| 27 | struct pci_access *pacc; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 28 | struct pci_dev *pcidev_dev = NULL; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 29 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 30 | uint32_t pcidev_validate(struct pci_dev *dev, uint32_t bar, |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 31 | const struct pcidev_status *devs) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 32 | { |
| 33 | int i; |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 34 | /* FIXME: 64 bit memory BARs need a 64 bit addr. */ |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 35 | uint32_t addr; |
| 36 | |
| 37 | for (i = 0; devs[i].device_name != NULL; i++) { |
| 38 | if (dev->device_id != devs[i].device_id) |
| 39 | continue; |
| 40 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 41 | /* |
| 42 | * Don't use dev->base_addr[x] (as value for 'bar'), won't |
| 43 | * work on older libpci. |
| 44 | */ |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 45 | addr = pci_read_long(dev, bar); |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 46 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 47 | msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n", |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 48 | devs[i].vendor_name, devs[i].device_name, |
| 49 | dev->vendor_id, dev->device_id, dev->bus, dev->dev, |
| 50 | dev->func); |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 51 | msg_pdbg("Requested BAR is %s", (addr & 0x1) ? "IO" : "MEM"); |
| 52 | if (addr & 0x1) { |
| 53 | /* Mask off IO space indicator and reserved bit. */ |
| 54 | msg_pdbg("\n"); |
| 55 | addr &= ~0x3; |
| 56 | } else { |
| 57 | msg_pdbg(", %sbit, %sprefetchable\n", |
| 58 | ((addr & 0x6) == 0x0) ? "32" : |
| 59 | (((addr & 0x6) == 0x4) ? "64" : "reserved"), |
| 60 | (addr & 0x8) ? "" : "not "); |
| 61 | /* Mask off Mem space indicator, 32/64bit type indicator |
| 62 | * and Prefetchable indicator. |
| 63 | */ |
| 64 | addr &= ~0xf; |
| 65 | } |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 66 | |
Michael Karcher | 8448639 | 2010-02-24 00:04:40 +0000 | [diff] [blame] | 67 | if (devs[i].status == NT) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 68 | msg_pinfo("===\nThis PCI device is UNTESTED. Please " |
Paul Menzel | ab6328f | 2010-10-08 11:03:02 +0000 | [diff] [blame] | 69 | "report the 'flashrom -p xxxx' output \n" |
| 70 | "to flashrom@flashrom.org if it works " |
| 71 | "for you. Please add the name of your\n" |
| 72 | "PCI device to the subject. Thank you for " |
| 73 | "your help!\n===\n"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return addr; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 82 | uint32_t pcidev_init(uint16_t vendor_id, uint32_t bar, |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 83 | const struct pcidev_status *devs) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 84 | { |
| 85 | struct pci_dev *dev; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 86 | struct pci_filter filter; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 87 | char *pcidev_bdf; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 88 | char *msg = NULL; |
| 89 | int found = 0; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 90 | uint32_t addr = 0, curaddr = 0; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 91 | |
| 92 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 93 | pci_init(pacc); /* Initialize the PCI library */ |
| 94 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 95 | pci_filter_init(pacc, &filter); |
| 96 | |
| 97 | /* Filter by vendor and also bb:dd.f (if supplied by the user). */ |
| 98 | filter.vendor = vendor_id; |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 99 | pcidev_bdf = extract_programmer_param("pci"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 100 | if (pcidev_bdf != NULL) { |
| 101 | if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 102 | msg_perr("Error: %s\n", msg); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 103 | exit(1); |
| 104 | } |
| 105 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 106 | free(pcidev_bdf); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 107 | |
| 108 | for (dev = pacc->devices; dev; dev = dev->next) { |
| 109 | if (pci_filter_match(&filter, dev)) { |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 110 | if ((addr = pcidev_validate(dev, bar, devs)) != 0) { |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 111 | curaddr = addr; |
| 112 | pcidev_dev = dev; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 113 | found++; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 114 | } |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | /* Only continue if exactly one supported PCI dev has been found. */ |
| 119 | if (found == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 120 | msg_perr("Error: No supported PCI device found.\n"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 121 | exit(1); |
| 122 | } else if (found > 1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 123 | msg_perr("Error: Multiple supported PCI devices found. " |
Carl-Daniel Hailfinger | 3e85442 | 2010-10-06 23:03:21 +0000 | [diff] [blame] | 124 | "Use 'flashrom -p xxxx:pci=bb:dd.f' \n" |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 125 | "to explicitly select the card with the given BDF " |
| 126 | "(PCI bus, device, function).\n"); |
| 127 | exit(1); |
| 128 | } |
| 129 | |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 130 | return curaddr; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 133 | void print_supported_pcidevs(const struct pcidev_status *devs) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 134 | { |
| 135 | int i; |
| 136 | |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 137 | msg_pinfo("PCI devices:\n"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 138 | for (i = 0; devs[i].vendor_name != NULL; i++) { |
Carl-Daniel Hailfinger | ef69783 | 2010-10-07 22:21:45 +0000 | [diff] [blame] | 139 | msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name, |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 140 | devs[i].device_name, devs[i].vendor_id, |
| 141 | devs[i].device_id, |
Michael Karcher | 8448639 | 2010-02-24 00:04:40 +0000 | [diff] [blame] | 142 | (devs[i].status == NT) ? " (untested)" : ""); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 145 | |
| 146 | enum pci_write_type { |
| 147 | pci_write_type_byte, |
| 148 | pci_write_type_word, |
| 149 | pci_write_type_long, |
| 150 | }; |
| 151 | |
| 152 | struct undo_pci_write_data { |
| 153 | struct pci_dev dev; |
| 154 | int reg; |
| 155 | enum pci_write_type type; |
| 156 | union { |
| 157 | uint8_t bytedata; |
| 158 | uint16_t worddata; |
| 159 | uint32_t longdata; |
| 160 | }; |
| 161 | }; |
| 162 | |
| 163 | void undo_pci_write(void *p) |
| 164 | { |
| 165 | struct undo_pci_write_data *data = p; |
| 166 | msg_pdbg("Restoring PCI config space for %02x:%02x:%01x reg 0x%02x\n", |
| 167 | data->dev.bus, data->dev.dev, data->dev.func, data->reg); |
| 168 | switch (data->type) { |
| 169 | case pci_write_type_byte: |
| 170 | pci_write_byte(&data->dev, data->reg, data->bytedata); |
| 171 | break; |
| 172 | case pci_write_type_word: |
| 173 | pci_write_word(&data->dev, data->reg, data->worddata); |
| 174 | break; |
| 175 | case pci_write_type_long: |
| 176 | pci_write_long(&data->dev, data->reg, data->longdata); |
| 177 | break; |
| 178 | } |
| 179 | /* p was allocated in register_undo_pci_write. */ |
| 180 | free(p); |
| 181 | } |
| 182 | |
| 183 | #define register_undo_pci_write(a, b, c) \ |
| 184 | { \ |
| 185 | struct undo_pci_write_data *undo_pci_write_data; \ |
| 186 | undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \ |
| 187 | undo_pci_write_data->dev = *a; \ |
| 188 | undo_pci_write_data->reg = b; \ |
| 189 | undo_pci_write_data->type = pci_write_type_##c; \ |
| 190 | undo_pci_write_data->c##data = pci_read_##c(dev, reg); \ |
| 191 | register_shutdown(undo_pci_write, undo_pci_write_data); \ |
| 192 | } |
| 193 | |
| 194 | #define register_undo_pci_write_byte(a, b) register_undo_pci_write(a, b, byte) |
| 195 | #define register_undo_pci_write_word(a, b) register_undo_pci_write(a, b, word) |
| 196 | #define register_undo_pci_write_long(a, b) register_undo_pci_write(a, b, long) |
| 197 | |
| 198 | int rpci_write_byte(struct pci_dev *dev, int reg, uint8_t data) |
| 199 | { |
| 200 | register_undo_pci_write_byte(dev, reg); |
| 201 | return pci_write_byte(dev, reg, data); |
| 202 | } |
| 203 | |
| 204 | int rpci_write_word(struct pci_dev *dev, int reg, uint16_t data) |
| 205 | { |
| 206 | register_undo_pci_write_word(dev, reg); |
| 207 | return pci_write_word(dev, reg, data); |
| 208 | } |
| 209 | |
| 210 | int rpci_write_long(struct pci_dev *dev, int reg, uint32_t data) |
| 211 | { |
| 212 | register_undo_pci_write_long(dev, reg); |
| 213 | return pci_write_long(dev, reg, data); |
| 214 | } |