internal.c 7.75 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 24 25
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009 Carl-Daniel Hailfinger
 *
 * 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 <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include "flash.h"
26
#include "programmer.h"
27

28
#if NEED_PCI == 1
29 30 31 32 33 34 35 36 37 38 39
struct pci_dev *pci_dev_find_filter(struct pci_filter filter)
{
	struct pci_dev *temp;

	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp))
			return temp;

	return NULL;
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
struct pci_dev *pci_dev_find_vendorclass(uint16_t vendor, uint16_t class)
{
	struct pci_dev *temp;
	struct pci_filter filter;
	uint16_t tmp2;

	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;

	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp)) {
			/* Read PCI class */
			tmp2 = pci_read_word(temp, 0x0a);
			if (tmp2 == class)
				return temp;
		}

	return NULL;
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
{
	struct pci_dev *temp;
	struct pci_filter filter;

	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;
	filter.device = device;

	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp))
			return temp;

	return NULL;
}

struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
			      uint16_t card_vendor, uint16_t card_device)
{
	struct pci_dev *temp;
	struct pci_filter filter;

	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;
	filter.device = device;

	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp)) {
			if ((card_vendor ==
			     pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID))
			    && (card_device ==
				pci_read_word(temp, PCI_SUBSYSTEM_ID)))
				return temp;
		}

	return NULL;
}
97
#endif
98

99
#if CONFIG_INTERNAL == 1
100
int force_boardenable = 0;
101
int force_boardmismatch = 0;
102

103 104 105
#if defined(__i386__) || defined(__x86_64__)
struct superio superio = {};

106 107 108
void probe_superio(void)
{
	superio = probe_superio_ite();
109 110
#if 0
	/* Winbond Super I/O code is not yet available. */
111 112 113 114
	if (superio.vendor == SUPERIO_VENDOR_NONE)
		superio = probe_superio_winbond();
#endif
}
115
#endif
116

117
int is_laptop = 0;
Michael Karcher's avatar
Michael Karcher committed
118

119 120 121
int internal_init(void)
{
	int ret = 0;
122 123 124
	int force_laptop = 0;
	char *arg;

125
	arg = extract_programmer_param("boardenable");
126 127 128 129
	if (arg && !strcmp(arg,"force")) {
		force_boardenable = 1;
	} else if (arg && !strlen(arg)) {
		msg_perr("Missing argument for boardenable.\n");
130 131
		free(arg);
		return 1;
132 133
	} else if (arg) {
		msg_perr("Unknown argument for boardenable: %s\n", arg);
134 135
		free(arg);
		return 1;
136 137 138
	}
	free(arg);

139
	arg = extract_programmer_param("boardmismatch");
140 141 142 143
	if (arg && !strcmp(arg,"force")) {
		force_boardmismatch = 1;
	} else if (arg && !strlen(arg)) {
		msg_perr("Missing argument for boardmismatch.\n");
144 145
		free(arg);
		return 1;
146 147
	} else if (arg) {
		msg_perr("Unknown argument for boardmismatch: %s\n", arg);
148 149
		free(arg);
		return 1;
150
	}
151 152
	free(arg);

153
	arg = extract_programmer_param("laptop");
154 155 156 157
	if (arg && !strcmp(arg,"force_I_want_a_brick")) {
		force_laptop = 1;
	} else if (arg && !strlen(arg)) {
		msg_perr("Missing argument for laptop.\n");
158 159
		free(arg);
		return 1;
160 161
	} else if (arg) {
		msg_perr("Unknown argument for laptop: %s\n", arg);
162 163
		free(arg);
		return 1;
164
	}
165 166
	free(arg);

167
	get_io_perms();
168 169 170 171 172 173 174

	/* Initialize PCI access for flash enables */
	pacc = pci_alloc();	/* Get the pci_access structure */
	/* Set all options you want -- here we stick with the defaults */
	pci_init(pacc);		/* Initialize the PCI library */
	pci_scan_bus(pacc);	/* We want to get the list of devices */

175 176 177 178 179 180 181 182
	if (processor_flash_enable()) {
		msg_perr("Processor detection/init failed.\n"
			 "Aborting.\n");
		return 1;
	}

#if defined(__i386__) || defined(__x86_64__)
	/* We look at the cbtable first to see if we need a
183 184 185
	 * mainboard specific flash enable sequence.
	 */
	coreboot_init();
186

Michael Karcher's avatar
Michael Karcher committed
187
	dmi_init();
188

189
	/* Probe for the Super I/O chip and fill global struct superio. */
190
	probe_superio();
191 192 193 194 195 196
#else
	/* FIXME: Enable cbtable searching on all non-x86 platforms supported
	 *        by coreboot.
	 * FIXME: Find a replacement for DMI on non-x86.
	 * FIXME: Enable Super I/O probing once port I/O is possible.
	 */
197
#endif
198

199
	/* Warn if a laptop is detected. */
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	if (is_laptop) {
		msg_perr("========================================================================\n"
			 "WARNING! You seem to be running flashrom on a laptop.\n"
			 "Laptops, notebooks and netbooks are difficult to support and we recommend\n"
			 "to use the vendor flashing utility. The embedded controller (EC) in these\n"
			 "machines often interacts badly with flashing.\n"
			 "See http://www.flashrom.org/Laptops for details.\n\n"
			 "If flash is shared with the EC, erase is guaranteed to brick your laptop\n"
			 "and write may brick your laptop.\n"
			 "Read and probe may irritate your EC and cause fan failure, backlight\n"
			 "failure and sudden poweroff.\n"
			 "You have been warned.\n"
			 "========================================================================\n");
		if (force_laptop) {
			msg_perr("Proceeding anyway because user specified "
				 "laptop=force_I_want_a_brick\n");
		} else {
			msg_perr("Aborting.\n");
			exit(1);
		}
	}
Michael Karcher's avatar
Michael Karcher committed
221

222
#if __FLASHROM_LITTLE_ENDIAN__
223 224 225 226 227
	/* try to enable it. Failure IS an option, since not all motherboards
	 * really need this to be done, etc., etc.
	 */
	ret = chipset_flash_enable();
	if (ret == -2) {
228 229
		msg_perr("WARNING: No chipset found. Flash detection "
			 "will most likely fail.\n");
230 231
	}

232
#if defined(__i386__) || defined(__x86_64__)
233 234 235 236
	/* Probe unconditionally for IT87* LPC->SPI translation and for
	 * IT87* Parallel write enable.
	 */
	init_superio_ite();
237
#endif
238

239 240
	board_flash_enable(lb_vendor, lb_part);

241 242 243 244
	/* Even if chipset init returns an error code, we don't want to abort.
	 * The error code might have been a warning only.
	 * Besides that, we don't check the board enable return code either.
	 */
245
#if defined(__i386__) || defined(__x86_64__)
246
	return 0;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
#else
	msg_perr("Your platform is not supported yet for the internal "
		 "programmer due to missing\n"
		 "flash_base and top/bottom alignment information.\n"
		 "Aborting.\n");
	return 1;
#endif
#else
	/* FIXME: Remove this unconditional abort once all PCI drivers are
	 * converted to use little-endian accesses for memory BARs.
	 */
	msg_perr("Your platform is not supported yet for the internal "
		 "programmer because it has\n"
		 "not been converted from native endian to little endian "
		 "access yet.\n"
		 "Aborting.\n");
	return 1;
#endif
265 266 267 268
}

int internal_shutdown(void)
{
269
	release_io_perms();
270 271 272

	return 0;
}
273
#endif
274

275
void internal_chip_writeb(uint8_t val, chipaddr addr)
276
{
277
	mmio_writeb(val, (void *) addr);
278 279
}

280
void internal_chip_writew(uint16_t val, chipaddr addr)
281
{
282
	mmio_writew(val, (void *) addr);
283 284
}

285
void internal_chip_writel(uint32_t val, chipaddr addr)
286
{
287
	mmio_writel(val, (void *) addr);
288 289
}

290
uint8_t internal_chip_readb(const chipaddr addr)
291
{
292
	return mmio_readb((void *) addr);
293 294
}

295
uint16_t internal_chip_readw(const chipaddr addr)
296
{
297
	return mmio_readw((void *) addr);
298 299
}

300
uint32_t internal_chip_readl(const chipaddr addr)
301 302 303 304
{
	return mmio_readl((void *) addr);
}

305 306 307 308 309
void internal_chip_readn(uint8_t *buf, const chipaddr addr, size_t len)
{
	memcpy(buf, (void *)addr, len);
	return;
}