blob: 4b16db2b547a56c5dd78d126a14234ed68b07ba0 [file] [log] [blame]
Uwe Hermann515ab3d2009-05-15 17:02:34 +00001/*
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 Hermann515ab3d2009-05-15 17:02:34 +000023#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Uwe Hermann515ab3d2009-05-15 17:02:34 +000025
26uint32_t io_base_addr;
27struct pci_access *pacc;
Uwe Hermann8403ccb2009-05-16 21:39:19 +000028struct pci_dev *pcidev_dev = NULL;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000029
Uwe Hermann2bc98f62009-09-30 18:29:55 +000030uint32_t pcidev_validate(struct pci_dev *dev, uint32_t bar,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000031 const struct pcidev_status *devs)
Uwe Hermann515ab3d2009-05-15 17:02:34 +000032{
33 int i;
Carl-Daniel Hailfinger295b3af2010-03-17 00:47:56 +000034 /* FIXME: 64 bit memory BARs need a 64 bit addr. */
Uwe Hermann515ab3d2009-05-15 17:02:34 +000035 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 Hermann2bc98f62009-09-30 18:29:55 +000041 /*
42 * Don't use dev->base_addr[x] (as value for 'bar'), won't
43 * work on older libpci.
44 */
Carl-Daniel Hailfinger295b3af2010-03-17 00:47:56 +000045 addr = pci_read_long(dev, bar);
TURBO Jb0912c02009-09-02 23:00:46 +000046
Sean Nelson316a29f2010-05-07 20:09:04 +000047 msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n",
Uwe Hermann2bc98f62009-09-30 18:29:55 +000048 devs[i].vendor_name, devs[i].device_name,
49 dev->vendor_id, dev->device_id, dev->bus, dev->dev,
50 dev->func);
Carl-Daniel Hailfinger295b3af2010-03-17 00:47:56 +000051 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 Hermann515ab3d2009-05-15 17:02:34 +000066
Michael Karcher84486392010-02-24 00:04:40 +000067 if (devs[i].status == NT) {
Sean Nelson316a29f2010-05-07 20:09:04 +000068 msg_pinfo("===\nThis PCI device is UNTESTED. Please "
Paul Menzelab6328f2010-10-08 11:03:02 +000069 "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 Hermann515ab3d2009-05-15 17:02:34 +000074 }
75
76 return addr;
77 }
78
79 return 0;
80}
81
Uwe Hermann2bc98f62009-09-30 18:29:55 +000082uint32_t pcidev_init(uint16_t vendor_id, uint32_t bar,
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000083 const struct pcidev_status *devs)
Uwe Hermann515ab3d2009-05-15 17:02:34 +000084{
85 struct pci_dev *dev;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000086 struct pci_filter filter;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000087 char *pcidev_bdf;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000088 char *msg = NULL;
89 int found = 0;
Uwe Hermann8403ccb2009-05-16 21:39:19 +000090 uint32_t addr = 0, curaddr = 0;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000091
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 Hailfinger2b6dcb32010-07-08 10:13:37 +000099 pcidev_bdf = extract_programmer_param("pci");
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000100 if (pcidev_bdf != NULL) {
101 if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000102 msg_perr("Error: %s\n", msg);
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000103 exit(1);
104 }
105 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000106 free(pcidev_bdf);
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000107
108 for (dev = pacc->devices; dev; dev = dev->next) {
109 if (pci_filter_match(&filter, dev)) {
TURBO Jb0912c02009-09-02 23:00:46 +0000110 if ((addr = pcidev_validate(dev, bar, devs)) != 0) {
Uwe Hermann8403ccb2009-05-16 21:39:19 +0000111 curaddr = addr;
112 pcidev_dev = dev;
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000113 found++;
Uwe Hermann8403ccb2009-05-16 21:39:19 +0000114 }
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000115 }
116 }
117
118 /* Only continue if exactly one supported PCI dev has been found. */
119 if (found == 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000120 msg_perr("Error: No supported PCI device found.\n");
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000121 exit(1);
122 } else if (found > 1) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000123 msg_perr("Error: Multiple supported PCI devices found. "
Carl-Daniel Hailfinger3e854422010-10-06 23:03:21 +0000124 "Use 'flashrom -p xxxx:pci=bb:dd.f' \n"
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000125 "to explicitly select the card with the given BDF "
126 "(PCI bus, device, function).\n");
127 exit(1);
128 }
129
Uwe Hermann8403ccb2009-05-16 21:39:19 +0000130 return curaddr;
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000131}
132
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000133void print_supported_pcidevs(const struct pcidev_status *devs)
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000134{
135 int i;
136
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000137 msg_pinfo("PCI devices:\n");
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000138 for (i = 0; devs[i].vendor_name != NULL; i++) {
Carl-Daniel Hailfingeref697832010-10-07 22:21:45 +0000139 msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000140 devs[i].device_name, devs[i].vendor_id,
141 devs[i].device_id,
Michael Karcher84486392010-02-24 00:04:40 +0000142 (devs[i].status == NT) ? " (untested)" : "");
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000143 }
144}