blob: 2bb437c3f348a5f54fd860f4ea6bc092515445bb [file] [log] [blame]
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Carl-Daniel Hailfinger
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.
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000015 */
16
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000017#include <strings.h>
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000018#include <string.h>
19#include <stdlib.h>
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000020#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000021#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000022#include "hwaccess.h"
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000023
Christian Ruppert0cdb0312009-05-14 18:57:26 +000024struct pci_dev *pci_dev_find_filter(struct pci_filter filter)
25{
26 struct pci_dev *temp;
27
28 for (temp = pacc->devices; temp; temp = temp->next)
29 if (pci_filter_match(&filter, temp))
30 return temp;
31
32 return NULL;
33}
34
Uwe Hermann24c35e42011-07-13 11:22:03 +000035struct pci_dev *pci_dev_find_vendorclass(uint16_t vendor, uint16_t devclass)
Carl-Daniel Hailfinger9f46cfc2009-11-15 17:13:29 +000036{
37 struct pci_dev *temp;
38 struct pci_filter filter;
39 uint16_t tmp2;
40
41 pci_filter_init(NULL, &filter);
42 filter.vendor = vendor;
43
44 for (temp = pacc->devices; temp; temp = temp->next)
45 if (pci_filter_match(&filter, temp)) {
46 /* Read PCI class */
47 tmp2 = pci_read_word(temp, 0x0a);
Uwe Hermann24c35e42011-07-13 11:22:03 +000048 if (tmp2 == devclass)
Carl-Daniel Hailfinger9f46cfc2009-11-15 17:13:29 +000049 return temp;
50 }
51
52 return NULL;
53}
54
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000055struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
56{
57 struct pci_dev *temp;
58 struct pci_filter filter;
59
60 pci_filter_init(NULL, &filter);
61 filter.vendor = vendor;
62 filter.device = device;
63
64 for (temp = pacc->devices; temp; temp = temp->next)
65 if (pci_filter_match(&filter, temp))
66 return temp;
67
68 return NULL;
69}
70
71struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
72 uint16_t card_vendor, uint16_t card_device)
73{
74 struct pci_dev *temp;
75 struct pci_filter filter;
76
77 pci_filter_init(NULL, &filter);
78 filter.vendor = vendor;
79 filter.device = device;
80
81 for (temp = pacc->devices; temp; temp = temp->next)
82 if (pci_filter_match(&filter, temp)) {
83 if ((card_vendor ==
84 pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID))
85 && (card_device ==
86 pci_read_word(temp, PCI_SUBSYSTEM_ID)))
87 return temp;
88 }
89
90 return NULL;
91}
92
Michael Karcher0bdc0922010-02-28 01:33:48 +000093int force_boardenable = 0;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +000094int force_boardmismatch = 0;
Carl-Daniel Hailfinger14e100c2009-12-22 23:42:04 +000095
Nico Huberc8801732017-12-01 18:19:43 +000096#if IS_X86
Carl-Daniel Hailfinger14e100c2009-12-22 23:42:04 +000097void probe_superio(void)
98{
Carl-Daniel Hailfingerf5e62cb2012-05-06 22:48:01 +000099 probe_superio_winbond();
100 /* ITE probe causes SMSC LPC47N217 to power off the serial UART.
101 * Always probe for SMSC first, and if a SMSC Super I/O is detected
102 * at a given I/O port, do _not_ probe that port with the ITE probe.
103 * This means SMSC probing must be done before ITE probing.
104 */
105 //probe_superio_smsc();
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000106 probe_superio_ite();
Carl-Daniel Hailfinger14e100c2009-12-22 23:42:04 +0000107}
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000108
109int superio_count = 0;
110#define SUPERIO_MAX_COUNT 3
111
112struct superio superios[SUPERIO_MAX_COUNT];
113
114int register_superio(struct superio s)
115{
116 if (superio_count == SUPERIO_MAX_COUNT)
117 return 1;
118 superios[superio_count++] = s;
119 return 0;
120}
121
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000122#endif
Carl-Daniel Hailfinger14e100c2009-12-22 23:42:04 +0000123
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000124int is_laptop = 0;
Carl-Daniel Hailfinger580d29a2011-05-05 07:12:40 +0000125int laptop_ok = 0;
Michael Karcher8c1df282010-02-26 09:51:20 +0000126
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000127static void internal_chip_writeb(const struct flashctx *flash, uint8_t val,
128 chipaddr addr);
129static void internal_chip_writew(const struct flashctx *flash, uint16_t val,
130 chipaddr addr);
131static void internal_chip_writel(const struct flashctx *flash, uint32_t val,
132 chipaddr addr);
133static uint8_t internal_chip_readb(const struct flashctx *flash,
134 const chipaddr addr);
135static uint16_t internal_chip_readw(const struct flashctx *flash,
136 const chipaddr addr);
137static uint32_t internal_chip_readl(const struct flashctx *flash,
138 const chipaddr addr);
139static void internal_chip_readn(const struct flashctx *flash, uint8_t *buf,
140 const chipaddr addr, size_t len);
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000141static const struct par_master par_master_internal = {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000142 .chip_readb = internal_chip_readb,
143 .chip_readw = internal_chip_readw,
144 .chip_readl = internal_chip_readl,
145 .chip_readn = internal_chip_readn,
146 .chip_writeb = internal_chip_writeb,
147 .chip_writew = internal_chip_writew,
148 .chip_writel = internal_chip_writel,
149 .chip_writen = fallback_chip_writen,
150};
151
152enum chipbustype internal_buses_supported = BUS_NONE;
153
Uwe Hermanna0869322009-05-14 20:41:57 +0000154int internal_init(void)
155{
156 int ret = 0;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000157 int force_laptop = 0;
Stefan Taunera28087f2011-09-13 23:14:25 +0000158 int not_a_laptop = 0;
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000159 const char *board_vendor = NULL;
160 const char *board_model = NULL;
Nico Huberc8801732017-12-01 18:19:43 +0000161#if IS_X86
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000162 const char *cb_vendor = NULL;
163 const char *cb_model = NULL;
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000164#endif
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000165 char *arg;
Uwe Hermanna0869322009-05-14 20:41:57 +0000166
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000167 arg = extract_programmer_param("boardenable");
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000168 if (arg && !strcmp(arg,"force")) {
169 force_boardenable = 1;
170 } else if (arg && !strlen(arg)) {
171 msg_perr("Missing argument for boardenable.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000172 free(arg);
173 return 1;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000174 } else if (arg) {
175 msg_perr("Unknown argument for boardenable: %s\n", arg);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 free(arg);
177 return 1;
Michael Karcher0bdc0922010-02-28 01:33:48 +0000178 }
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000179 free(arg);
Michael Karcher0bdc0922010-02-28 01:33:48 +0000180
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000181 arg = extract_programmer_param("boardmismatch");
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000182 if (arg && !strcmp(arg,"force")) {
183 force_boardmismatch = 1;
184 } else if (arg && !strlen(arg)) {
185 msg_perr("Missing argument for boardmismatch.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000186 free(arg);
187 return 1;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000188 } else if (arg) {
189 msg_perr("Unknown argument for boardmismatch: %s\n", arg);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000190 free(arg);
191 return 1;
Michael Karcher0bdc0922010-02-28 01:33:48 +0000192 }
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000193 free(arg);
194
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000195 arg = extract_programmer_param("laptop");
Stefan Taunera28087f2011-09-13 23:14:25 +0000196 if (arg && !strcmp(arg, "force_I_want_a_brick"))
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000197 force_laptop = 1;
Stefan Taunera28087f2011-09-13 23:14:25 +0000198 else if (arg && !strcmp(arg, "this_is_not_a_laptop"))
199 not_a_laptop = 1;
200 else if (arg && !strlen(arg)) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000201 msg_perr("Missing argument for laptop.\n");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000202 free(arg);
203 return 1;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000204 } else if (arg) {
205 msg_perr("Unknown argument for laptop: %s\n", arg);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000206 free(arg);
207 return 1;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000208 }
209 free(arg);
210
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +0000211 arg = extract_programmer_param("mainboard");
212 if (arg && strlen(arg)) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000213 if (board_parse_parameter(arg, &board_vendor, &board_model)) {
214 free(arg);
215 return 1;
216 }
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +0000217 } else if (arg && !strlen(arg)) {
218 msg_perr("Missing argument for mainboard.\n");
219 free(arg);
220 return 1;
221 }
222 free(arg);
223
Carl-Daniel Hailfingerd6bb8282012-07-21 17:27:08 +0000224 if (rget_io_perms())
225 return 1;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000226
Michael Karcherb9dbe482011-05-11 17:07:07 +0000227 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000228 * is found, the host controller init routine sets the
229 * internal_buses_supported bitfield.
Michael Karcherb9dbe482011-05-11 17:07:07 +0000230 */
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000231 internal_buses_supported = BUS_NONSPI;
Michael Karcherb9dbe482011-05-11 17:07:07 +0000232
David Hendricksf9a30552015-05-23 20:30:30 -0700233 if (try_mtd() == 0)
234 return 0;
235
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000236 /* Initialize PCI access for flash enables */
Stefan Tauner55619552013-01-04 22:24:58 +0000237 if (pci_init_common() != 0)
238 return 1;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000239
Carl-Daniel Hailfingerb5b161b2010-06-04 19:05:39 +0000240 if (processor_flash_enable()) {
241 msg_perr("Processor detection/init failed.\n"
242 "Aborting.\n");
243 return 1;
244 }
245
Nico Huberc8801732017-12-01 18:19:43 +0000246#if IS_X86
Stefan Taunerfa9fa712012-09-24 21:29:29 +0000247 if ((cb_parse_table(&cb_vendor, &cb_model) == 0) && (board_vendor != NULL) && (board_model != NULL)) {
248 if (strcasecmp(board_vendor, cb_vendor) || strcasecmp(board_model, cb_model)) {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +0000249 msg_pwarn("Warning: The mainboard IDs set by -p internal:mainboard (%s:%s) do not\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000250 " match the current coreboot IDs of the mainboard (%s:%s).\n",
251 board_vendor, board_model, cb_vendor, cb_model);
252 if (!force_boardmismatch)
253 return 1;
254 msg_pinfo("Continuing anyway.\n");
255 }
256 }
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000257#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000258
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000259#if IS_X86
Nico Huber2e50cdc2018-09-23 20:20:26 +0200260 is_laptop = 2; /* Assume that we don't know by default. */
261
Michael Karcher6701ee82010-01-20 14:14:11 +0000262 dmi_init();
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000263
Carl-Daniel Hailfinger580d29a2011-05-05 07:12:40 +0000264 /* In case Super I/O probing would cause pretty explosions. */
265 board_handle_before_superio();
266
Uwe Hermann43959702010-03-13 17:28:29 +0000267 /* Probe for the Super I/O chip and fill global struct superio. */
Carl-Daniel Hailfinger14e100c2009-12-22 23:42:04 +0000268 probe_superio();
Carl-Daniel Hailfingerb5b161b2010-06-04 19:05:39 +0000269#else
270 /* FIXME: Enable cbtable searching on all non-x86 platforms supported
271 * by coreboot.
272 * FIXME: Find a replacement for DMI on non-x86.
273 * FIXME: Enable Super I/O probing once port I/O is possible.
274 */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000275#endif
Carl-Daniel Hailfinger14e100c2009-12-22 23:42:04 +0000276
Carl-Daniel Hailfinger580d29a2011-05-05 07:12:40 +0000277 /* Check laptop whitelist. */
278 board_handle_before_laptop();
279
Nico Huber2e50cdc2018-09-23 20:20:26 +0200280 /*
281 * Disable all internal buses by default if we are not sure
282 * this isn't a laptop. Board-enables may override this,
283 * non-legacy buses (SPI and opaque atm) are probed anyway.
284 */
285 if (is_laptop && !(laptop_ok || force_laptop || (not_a_laptop && is_laptop == 2)))
286 internal_buses_supported = BUS_NONE;
Michael Karcher8c1df282010-02-26 09:51:20 +0000287
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000288 /* try to enable it. Failure IS an option, since not all motherboards
289 * really need this to be done, etc., etc.
290 */
291 ret = chipset_flash_enable();
292 if (ret == -2) {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000293 msg_perr("WARNING: No chipset found. Flash detection "
294 "will most likely fail.\n");
Tadas Slotkusad470342011-09-03 17:15:00 +0000295 } else if (ret == ERROR_FATAL)
296 return ret;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000297
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000298#if IS_X86
Vadim Girlin4dd0f902013-08-24 12:18:17 +0000299 /* Probe unconditionally for ITE Super I/O chips. This enables LPC->SPI translation on IT87* and
300 * parallel writes on IT8705F. Also, this handles the manual chip select for Gigabyte's DualBIOS. */
Carl-Daniel Hailfinger76d4b372010-07-10 16:56:32 +0000301 init_superio_ite();
Carl-Daniel Hailfinger01f3ef42010-03-25 02:50:40 +0000302
Stefan Taunerfa9fa712012-09-24 21:29:29 +0000303 if (board_flash_enable(board_vendor, board_model, cb_vendor, cb_model)) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000304 msg_perr("Aborting to be safe.\n");
305 return 1;
306 }
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000307#endif
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000308
Nico Huber2e50cdc2018-09-23 20:20:26 +0200309 if (internal_buses_supported & BUS_NONSPI)
310 register_par_master(&par_master_internal, internal_buses_supported);
311
312 /* Report if a non-whitelisted laptop is detected that likely uses a legacy bus. */
313 if (is_laptop && !laptop_ok) {
314 msg_pinfo("========================================================================\n");
315 if (is_laptop == 1) {
316 msg_pinfo("You seem to be running flashrom on an unknown laptop. Some\n"
317 "internal buses have been disabled for safety reasons.\n\n");
318 } else {
319 msg_pinfo("You may be running flashrom on an unknown laptop. We could not\n"
320 "detect this for sure because your vendor has not set up the SMBIOS\n"
321 "tables correctly. Some internal buses have been disabled for\n"
322 "safety reasons. You can enforce using all buses by adding\n"
323 " -p internal:laptop=this_is_not_a_laptop\n"
324 "to the command line, but please read the following warning if you\n"
325 "are not sure.\n\n");
326 }
327 msg_perr("Laptops, notebooks and netbooks are difficult to support and we\n"
328 "recommend to use the vendor flashing utility. The embedded controller\n"
329 "(EC) in these machines often interacts badly with flashing.\n"
330 "See the manpage and https://flashrom.org/Laptops for details.\n\n"
331 "If flash is shared with the EC, erase is guaranteed to brick your laptop\n"
332 "and write may brick your laptop.\n"
333 "Read and probe may irritate your EC and cause fan failure, backlight\n"
334 "failure and sudden poweroff.\n"
335 "You have been warned.\n"
336 "========================================================================\n");
337 }
338
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000339 return 0;
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000340}
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000341
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000342static void internal_chip_writeb(const struct flashctx *flash, uint8_t val,
343 chipaddr addr)
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000344{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000345 mmio_writeb(val, (void *) addr);
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000346}
347
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000348static void internal_chip_writew(const struct flashctx *flash, uint16_t val,
349 chipaddr addr)
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000350{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000351 mmio_writew(val, (void *) addr);
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000352}
353
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000354static void internal_chip_writel(const struct flashctx *flash, uint32_t val,
355 chipaddr addr)
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000356{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000357 mmio_writel(val, (void *) addr);
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000358}
359
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000360static uint8_t internal_chip_readb(const struct flashctx *flash,
361 const chipaddr addr)
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000362{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000363 return mmio_readb((void *) addr);
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000364}
365
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000366static uint16_t internal_chip_readw(const struct flashctx *flash,
367 const chipaddr addr)
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000368{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000369 return mmio_readw((void *) addr);
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000370}
371
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000372static uint32_t internal_chip_readl(const struct flashctx *flash,
373 const chipaddr addr)
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000374{
Carl-Daniel Hailfinger78185dc2009-05-17 15:49:24 +0000375 return mmio_readl((void *) addr);
376}
377
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000378static void internal_chip_readn(const struct flashctx *flash, uint8_t *buf,
379 const chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000380{
Carl-Daniel Hailfingerccd71c22012-03-01 22:38:27 +0000381 mmio_readn((void *)addr, buf, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000382 return;
383}