blob: 41b5f25aced815c6a2e8bd587a5830556dabb8c8 [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>
23#include <fcntl.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <errno.h>
27#include "flash.h"
28
Uwe Hermann40595982009-05-16 01:30:48 +000029#define PCI_IO_BASE_ADDRESS 0x10
30
Uwe Hermann515ab3d2009-05-15 17:02:34 +000031uint32_t io_base_addr;
32struct pci_access *pacc;
33struct pci_filter filter;
34char *pcidev_bdf = NULL;
Uwe Hermann8403ccb2009-05-16 21:39:19 +000035struct pci_dev *pcidev_dev = NULL;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000036
37uint32_t pcidev_validate(struct pci_dev *dev, struct pcidev_status *devs)
38{
39 int i;
40 uint32_t addr;
41
42 for (i = 0; devs[i].device_name != NULL; i++) {
43 if (dev->device_id != devs[i].device_id)
44 continue;
45
Uwe Hermann40595982009-05-16 01:30:48 +000046 /* Don't use dev->base_addr[0], won't work on older libpci. */
47 addr = pci_read_long(dev, PCI_IO_BASE_ADDRESS) & ~0x03;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000048
49 printf("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x)\n",
50 devs[i].vendor_name, devs[i].device_name, dev->vendor_id,
51 dev->device_id, dev->bus, dev->dev, dev->func);
52
53 if (devs[i].status == PCI_NT) {
54 printf("===\nThis PCI device is UNTESTED. Please email "
55 "a report including the 'flashrom -p xxxxxx'\n"
56 "output to flashrom@coreboot.org if it works "
57 "for you. Thank you for your help!\n===\n");
58 }
59
60 return addr;
61 }
62
63 return 0;
64}
65
66uint32_t pcidev_init(uint16_t vendor_id, struct pcidev_status *devs)
67{
68 struct pci_dev *dev;
69 char *msg = NULL;
70 int found = 0;
Uwe Hermann8403ccb2009-05-16 21:39:19 +000071 uint32_t addr = 0, curaddr = 0;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000072
73 pacc = pci_alloc(); /* Get the pci_access structure */
74 pci_init(pacc); /* Initialize the PCI library */
75 pci_scan_bus(pacc); /* We want to get the list of devices */
76 pci_filter_init(pacc, &filter);
77
78 /* Filter by vendor and also bb:dd.f (if supplied by the user). */
79 filter.vendor = vendor_id;
80 if (pcidev_bdf != NULL) {
81 if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
82 fprintf(stderr, "Error: %s\n", msg);
83 exit(1);
84 }
85 }
86
87 for (dev = pacc->devices; dev; dev = dev->next) {
88 if (pci_filter_match(&filter, dev)) {
Uwe Hermann8403ccb2009-05-16 21:39:19 +000089 if ((addr = pcidev_validate(dev, devs)) != 0) {
90 curaddr = addr;
91 pcidev_dev = dev;
Uwe Hermann515ab3d2009-05-15 17:02:34 +000092 found++;
Uwe Hermann8403ccb2009-05-16 21:39:19 +000093 }
Uwe Hermann515ab3d2009-05-15 17:02:34 +000094 }
95 }
96
97 /* Only continue if exactly one supported PCI dev has been found. */
98 if (found == 0) {
99 fprintf(stderr, "Error: No supported PCI device found.\n");
100 exit(1);
101 } else if (found > 1) {
102 fprintf(stderr, "Error: Multiple supported PCI devices found. "
103 "Please use 'flashrom -p xxxxxx=bb:dd.f' \n"
104 "to explicitly select the card with the given BDF "
105 "(PCI bus, device, function).\n");
106 exit(1);
107 }
108
Uwe Hermann8403ccb2009-05-16 21:39:19 +0000109 return curaddr;
Uwe Hermann515ab3d2009-05-15 17:02:34 +0000110}
111
112void print_supported_pcidevs(struct pcidev_status *devs)
113{
114 int i;
115
116 for (i = 0; devs[i].vendor_name != NULL; i++) {
117 printf("%s %s [%02x:%02x]%s\n", devs[i].vendor_name,
118 devs[i].device_name, devs[i].vendor_id,
119 devs[i].device_id,
120 (devs[i].status == PCI_NT) ? " (untested)" : "");
121 }
122}