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 <sys/types.h> |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 24 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 26 | |
| 27 | uint32_t io_base_addr; |
| 28 | struct pci_access *pacc; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 29 | struct pci_dev *pcidev_dev = NULL; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 30 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 31 | uint32_t pcidev_validate(struct pci_dev *dev, uint32_t bar, |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 32 | const struct pcidev_status *devs) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 33 | { |
| 34 | int i; |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 35 | /* FIXME: 64 bit memory BARs need a 64 bit addr. */ |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 36 | uint32_t addr; |
| 37 | |
| 38 | for (i = 0; devs[i].device_name != NULL; i++) { |
| 39 | if (dev->device_id != devs[i].device_id) |
| 40 | continue; |
| 41 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Don't use dev->base_addr[x] (as value for 'bar'), won't |
| 44 | * work on older libpci. |
| 45 | */ |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 46 | addr = pci_read_long(dev, bar); |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 47 | |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 48 | msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n", |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 49 | devs[i].vendor_name, devs[i].device_name, |
| 50 | dev->vendor_id, dev->device_id, dev->bus, dev->dev, |
| 51 | dev->func); |
Carl-Daniel Hailfinger | 295b3af | 2010-03-17 00:47:56 +0000 | [diff] [blame] | 52 | msg_pdbg("Requested BAR is %s", (addr & 0x1) ? "IO" : "MEM"); |
| 53 | if (addr & 0x1) { |
| 54 | /* Mask off IO space indicator and reserved bit. */ |
| 55 | msg_pdbg("\n"); |
| 56 | addr &= ~0x3; |
| 57 | } else { |
| 58 | msg_pdbg(", %sbit, %sprefetchable\n", |
| 59 | ((addr & 0x6) == 0x0) ? "32" : |
| 60 | (((addr & 0x6) == 0x4) ? "64" : "reserved"), |
| 61 | (addr & 0x8) ? "" : "not "); |
| 62 | /* Mask off Mem space indicator, 32/64bit type indicator |
| 63 | * and Prefetchable indicator. |
| 64 | */ |
| 65 | addr &= ~0xf; |
| 66 | } |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 67 | |
Michael Karcher | 8448639 | 2010-02-24 00:04:40 +0000 | [diff] [blame] | 68 | if (devs[i].status == NT) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 69 | msg_pinfo("===\nThis PCI device is UNTESTED. Please " |
Uwe Hermann | eaefb48 | 2009-05-17 22:57:34 +0000 | [diff] [blame] | 70 | "report the 'flashrom -p xxxx' output \n" |
Stefan Reinauer | 22ea8cd | 2009-07-30 13:32:26 +0000 | [diff] [blame] | 71 | "to flashrom@flashrom.org if it works " |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 72 | "for you. Thank you for your help!\n===\n"); |
| 73 | } |
| 74 | |
| 75 | return addr; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 81 | uint32_t pcidev_init(uint16_t vendor_id, uint32_t bar, |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 82 | const struct pcidev_status *devs) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 83 | { |
| 84 | struct pci_dev *dev; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 85 | struct pci_filter filter; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 86 | char *pcidev_bdf; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 87 | char *msg = NULL; |
| 88 | int found = 0; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 89 | uint32_t addr = 0, curaddr = 0; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 90 | |
| 91 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 92 | pci_init(pacc); /* Initialize the PCI library */ |
| 93 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 94 | pci_filter_init(pacc, &filter); |
| 95 | |
| 96 | /* Filter by vendor and also bb:dd.f (if supplied by the user). */ |
| 97 | filter.vendor = vendor_id; |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 98 | pcidev_bdf = extract_programmer_param("pci"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 99 | if (pcidev_bdf != NULL) { |
| 100 | if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 101 | msg_perr("Error: %s\n", msg); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 102 | exit(1); |
| 103 | } |
| 104 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 105 | free(pcidev_bdf); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 106 | |
| 107 | for (dev = pacc->devices; dev; dev = dev->next) { |
| 108 | if (pci_filter_match(&filter, dev)) { |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 109 | if ((addr = pcidev_validate(dev, bar, devs)) != 0) { |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 110 | curaddr = addr; |
| 111 | pcidev_dev = dev; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 112 | found++; |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 113 | } |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | /* Only continue if exactly one supported PCI dev has been found. */ |
| 118 | if (found == 0) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 119 | msg_perr("Error: No supported PCI device found.\n"); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 120 | exit(1); |
| 121 | } else if (found > 1) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 122 | msg_perr("Error: Multiple supported PCI devices found. " |
Carl-Daniel Hailfinger | 3291030 | 2009-10-30 21:12:39 +0000 | [diff] [blame] | 123 | "Use 'flashrom -p xxxx:bb:dd.f' \n" |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 124 | "to explicitly select the card with the given BDF " |
| 125 | "(PCI bus, device, function).\n"); |
| 126 | exit(1); |
| 127 | } |
| 128 | |
Uwe Hermann | 8403ccb | 2009-05-16 21:39:19 +0000 | [diff] [blame] | 129 | return curaddr; |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 132 | void print_supported_pcidevs(const struct pcidev_status *devs) |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 133 | { |
| 134 | int i; |
| 135 | |
| 136 | for (i = 0; devs[i].vendor_name != NULL; i++) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 137 | msg_pinfo("%s %s [%02x:%02x]%s\n", devs[i].vendor_name, |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 138 | devs[i].device_name, devs[i].vendor_id, |
| 139 | devs[i].device_id, |
Michael Karcher | 8448639 | 2010-02-24 00:04:40 +0000 | [diff] [blame] | 140 | (devs[i].status == NT) ? " (untested)" : ""); |
Uwe Hermann | 515ab3d | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 141 | } |
| 142 | } |