pcidev.c 4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <stdlib.h>
#include <string.h>
#include "flash.h"
24
#include "programmer.h"
25 26 27

uint32_t io_base_addr;
struct pci_access *pacc;
28
struct pci_dev *pcidev_dev = NULL;
29

30
uint32_t pcidev_validate(struct pci_dev *dev, uint32_t bar,
31
			 const struct pcidev_status *devs)
32 33
{
	int i;
34
	/* FIXME: 64 bit memory BARs need a 64 bit addr. */
35 36 37 38 39 40
	uint32_t addr;

	for (i = 0; devs[i].device_name != NULL; i++) {
		if (dev->device_id != devs[i].device_id)
			continue;

41 42 43 44
		/*
		 * Don't use dev->base_addr[x] (as value for 'bar'), won't
		 * work on older libpci.
		 */
45
		addr = pci_read_long(dev, bar);
46
		
47
		msg_pinfo("Found \"%s %s\" (%04x:%04x, BDF %02x:%02x.%x).\n",
48 49 50
		       devs[i].vendor_name, devs[i].device_name,
		       dev->vendor_id, dev->device_id, dev->bus, dev->dev,
		       dev->func);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		msg_pdbg("Requested BAR is %s", (addr & 0x1) ? "IO" : "MEM");
		if (addr & 0x1) {
			/* Mask off IO space indicator and reserved bit. */
			msg_pdbg("\n");
			addr &= ~0x3;
		} else {
			msg_pdbg(", %sbit, %sprefetchable\n",
				 ((addr & 0x6) == 0x0) ? "32" :
				 (((addr & 0x6) == 0x4) ? "64" : "reserved"),
				 (addr & 0x8) ? "" : "not ");
			/* Mask off Mem space indicator, 32/64bit type indicator
			 * and Prefetchable indicator.
			 */
			addr &= ~0xf;
		}
66

67
		if (devs[i].status == NT) {
68
			msg_pinfo("===\nThis PCI device is UNTESTED. Please "
69
			       "report the 'flashrom -p xxxx' output \n"
70
			       "to flashrom@flashrom.org if it works "
71 72 73 74 75 76 77 78 79
			       "for you. Thank you for your help!\n===\n");
		}

		return addr;
	}

	return 0;
}

80
uint32_t pcidev_init(uint16_t vendor_id, uint32_t bar,
81
		     const struct pcidev_status *devs)
82 83
{
	struct pci_dev *dev;
84
	struct pci_filter filter;
85
	char *pcidev_bdf;
86 87
	char *msg = NULL;
	int found = 0;
88
	uint32_t addr = 0, curaddr = 0;
89 90 91 92 93 94 95 96

	pacc = pci_alloc();     /* Get the pci_access structure */
	pci_init(pacc);         /* Initialize the PCI library */
	pci_scan_bus(pacc);     /* We want to get the list of devices */
	pci_filter_init(pacc, &filter);

	/* Filter by vendor and also bb:dd.f (if supplied by the user). */
	filter.vendor = vendor_id;
97
	pcidev_bdf = extract_programmer_param("pci");
98 99
	if (pcidev_bdf != NULL) {
		if ((msg = pci_filter_parse_slot(&filter, pcidev_bdf))) {
100
			msg_perr("Error: %s\n", msg);
101 102 103
			exit(1);
		}
	}
104
	free(pcidev_bdf);
105 106 107

	for (dev = pacc->devices; dev; dev = dev->next) {
		if (pci_filter_match(&filter, dev)) {
108
			if ((addr = pcidev_validate(dev, bar, devs)) != 0) {
109 110
				curaddr = addr;
				pcidev_dev = dev;
111
				found++;
112
			}
113 114 115 116 117
		}
	}

	/* Only continue if exactly one supported PCI dev has been found. */
	if (found == 0) {
118
		msg_perr("Error: No supported PCI device found.\n");
119 120
		exit(1);
	} else if (found > 1) {
121
		msg_perr("Error: Multiple supported PCI devices found. "
122
			"Use 'flashrom -p xxxx:pci=bb:dd.f' \n"
123 124 125 126 127
			"to explicitly select the card with the given BDF "
			"(PCI bus, device, function).\n");
		exit(1);
	}

128
	return curaddr;
129 130
}

131
void print_supported_pcidevs(const struct pcidev_status *devs)
132 133 134
{
	int i;

135
	msg_pinfo("PCI devices:\n");
136
	for (i = 0; devs[i].vendor_name != NULL; i++) {
137
		msg_pinfo("%s %s [%02x:%02x]%s\n", devs[i].vendor_name,
138 139
		       devs[i].device_name, devs[i].vendor_id,
		       devs[i].device_id,
140
		       (devs[i].status == NT) ? " (untested)" : "");
141 142
	}
}