blob: 491b9aa23684a6485920443f869b0e25b4bb0ec8 [file] [log] [blame]
hailfinger2c361e42008-05-13 23:03:12 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger4500b082009-07-11 18:05:42 +00004 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
hailfinger2c361e42008-05-13 23:03:12 +00005 * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl>
stepan3bdf6182008-06-30 23:45:22 +00006 * Copyright (C) 2008 coresystems GmbH
hailfinger2c361e42008-05-13 23:03:12 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
hailfinger2c361e42008-05-13 23:03:12 +000016 */
17
18/*
19 * Contains the ITE IT87* SPI specific routines
20 */
21
hailfinger324a9cc2010-05-26 01:45:41 +000022#if defined(__i386__) || defined(__x86_64__)
23
hailfinger2c361e42008-05-13 23:03:12 +000024#include <string.h>
hailfinger4500b082009-07-11 18:05:42 +000025#include <stdlib.h>
Patrick Georgi048dbdb2017-04-11 20:45:07 +020026#include <errno.h>
hailfinger2c361e42008-05-13 23:03:12 +000027#include "flash.h"
snelson8913d082010-02-26 05:48:29 +000028#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000029#include "programmer.h"
Patrick Georgi048dbdb2017-04-11 20:45:07 +020030#include "hwaccess.h"
hailfinger2c361e42008-05-13 23:03:12 +000031#include "spi.h"
32
33#define ITE_SUPERIO_PORT1 0x2e
34#define ITE_SUPERIO_PORT2 0x4e
35
Edward O'Callaghan85726e52020-11-17 18:11:47 +110036#define CHIP_ID_BYTE1_REG 0x20
37#define CHIP_ID_BYTE2_REG 0x21
38#define CHIP_VER_REG 0x22
39
Patrick Georgi048dbdb2017-04-11 20:45:07 +020040static uint16_t it8716f_flashport = 0;
hailfinger2c361e42008-05-13 23:03:12 +000041/* use fast 33MHz SPI (<>0) or slow 16MHz (0) */
hailfinger1ff33dc2010-07-03 11:02:10 +000042static int fast_spi = 1;
hailfinger2c361e42008-05-13 23:03:12 +000043
hailfinger2c361e42008-05-13 23:03:12 +000044/* Helper functions for most recent ITE IT87xx Super I/O chips */
hailfinger7bac0e52009-05-25 23:26:50 +000045void enter_conf_mode_ite(uint16_t port)
hailfinger2c361e42008-05-13 23:03:12 +000046{
hailfingere1f062f2008-05-22 13:22:45 +000047 OUTB(0x87, port);
48 OUTB(0x01, port);
49 OUTB(0x55, port);
hailfinger2c361e42008-05-13 23:03:12 +000050 if (port == ITE_SUPERIO_PORT1)
hailfingere1f062f2008-05-22 13:22:45 +000051 OUTB(0x55, port);
hailfinger2c361e42008-05-13 23:03:12 +000052 else
hailfingere1f062f2008-05-22 13:22:45 +000053 OUTB(0xaa, port);
hailfinger2c361e42008-05-13 23:03:12 +000054}
55
hailfinger7bac0e52009-05-25 23:26:50 +000056void exit_conf_mode_ite(uint16_t port)
hailfinger2c361e42008-05-13 23:03:12 +000057{
hailfinger7bac0e52009-05-25 23:26:50 +000058 sio_write(port, 0x02, 0x02);
hailfinger2c361e42008-05-13 23:03:12 +000059}
60
Edward O'Callaghan95052952020-05-09 22:22:37 +100061static uint16_t probe_id_ite(uint16_t port)
hailfingerc236f9e2009-12-22 23:42:04 +000062{
63 uint16_t id;
64
65 enter_conf_mode_ite(port);
66 id = sio_read(port, CHIP_ID_BYTE1_REG) << 8;
67 id |= sio_read(port, CHIP_ID_BYTE2_REG);
68 exit_conf_mode_ite(port);
69
70 return id;
71}
72
hailfinger94e090c2011-04-27 14:34:08 +000073void probe_superio_ite(void)
hailfingerc236f9e2009-12-22 23:42:04 +000074{
Patrick Georgi8ddfee92017-03-20 14:54:28 +010075 struct superio s = {0};
hailfingerc236f9e2009-12-22 23:42:04 +000076 uint16_t ite_ports[] = {ITE_SUPERIO_PORT1, ITE_SUPERIO_PORT2, 0};
77 uint16_t *i = ite_ports;
78
hailfinger94e090c2011-04-27 14:34:08 +000079 s.vendor = SUPERIO_VENDOR_ITE;
hailfingerc236f9e2009-12-22 23:42:04 +000080 for (; *i; i++) {
hailfinger94e090c2011-04-27 14:34:08 +000081 s.port = *i;
82 s.model = probe_id_ite(s.port);
83 switch (s.model >> 8) {
hailfingerc236f9e2009-12-22 23:42:04 +000084 case 0x82:
85 case 0x86:
86 case 0x87:
hailfinger94e090c2011-04-27 14:34:08 +000087 /* FIXME: Print revision for all models? */
Patrick Georgi05482992017-03-20 21:56:33 +010088 msg_pdbg("Found ITE Super I/O, ID 0x%04hx on port 0x%x\n", s.model, s.port);
hailfinger94e090c2011-04-27 14:34:08 +000089 register_superio(s);
90 break;
91 case 0x85:
Patrick Georgi05482992017-03-20 21:56:33 +010092 msg_pdbg("Found ITE EC, ID 0x%04hx, Rev 0x%02x on port 0x%x.\n",
Edward O'Callaghan95052952020-05-09 22:22:37 +100093 s.model, sio_read(s.port, CHIP_VER_REG), s.port);
hailfinger94e090c2011-04-27 14:34:08 +000094 register_superio(s);
95 break;
hailfingerc236f9e2009-12-22 23:42:04 +000096 }
97 }
98
hailfinger94e090c2011-04-27 14:34:08 +000099 return;
hailfingerc236f9e2009-12-22 23:42:04 +0000100}
101
Edward O'Callaghan85726e52020-11-17 18:11:47 +1100102/* Page size is usually 256 bytes */
103static int it8716f_spi_page_program(struct flashctx *flash, const uint8_t *buf, unsigned int start)
104{
105 unsigned int i;
106 int result;
107 chipaddr bios = flash->virtual_memory;
108
109 result = spi_write_enable(flash);
110 if (result)
111 return result;
112 /* FIXME: The command below seems to be redundant or wrong. */
113 OUTB(0x06, it8716f_flashport + 1);
114 OUTB(((2 + (fast_spi ? 1 : 0)) << 4), it8716f_flashport);
115 for (i = 0; i < flash->chip->page_size; i++)
116 mmio_writeb(buf[i], (void *)(bios + start + i));
117 OUTB(0, it8716f_flashport);
118 /* Wait until the Write-In-Progress bit is cleared.
119 * This usually takes 1-10 ms, so wait in 1 ms steps.
120 */
121 while (spi_read_status_register(flash) & SPI_SR_WIP)
122 programmer_delay(1000);
123 return 0;
124}
125
126/*
127 * The IT8716F only supports commands with length 1,2,4,5 bytes including
128 * command byte and can not read more than 3 bytes from the device.
129 *
130 * This function expects writearr[0] to be the first byte sent to the device,
131 * whereas the IT8716F splits commands internally into address and non-address
132 * commands with the address in inverse wire order. That's why the register
133 * ordering in case 4 and 5 may seem strange.
134 */
Patrick Georgi05482992017-03-20 21:56:33 +0100135static int it8716f_spi_send_command(const struct flashctx *flash,
136 unsigned int writecnt, unsigned int readcnt,
137 const unsigned char *writearr,
Edward O'Callaghan85726e52020-11-17 18:11:47 +1100138 unsigned char *readarr)
139{
140 uint8_t busy, writeenc;
141
142 do {
143 busy = INB(it8716f_flashport) & 0x80;
144 } while (busy);
145 if (readcnt > 3) {
146 msg_pinfo("%s called with unsupported readcnt %i.\n",
147 __func__, readcnt);
148 return SPI_INVALID_LENGTH;
149 }
150 switch (writecnt) {
151 case 1:
152 OUTB(writearr[0], it8716f_flashport + 1);
153 writeenc = 0x0;
154 break;
155 case 2:
156 OUTB(writearr[0], it8716f_flashport + 1);
157 OUTB(writearr[1], it8716f_flashport + 7);
158 writeenc = 0x1;
159 break;
160 case 4:
161 OUTB(writearr[0], it8716f_flashport + 1);
162 OUTB(writearr[1], it8716f_flashport + 4);
163 OUTB(writearr[2], it8716f_flashport + 3);
164 OUTB(writearr[3], it8716f_flashport + 2);
165 writeenc = 0x2;
166 break;
167 case 5:
168 OUTB(writearr[0], it8716f_flashport + 1);
169 OUTB(writearr[1], it8716f_flashport + 4);
170 OUTB(writearr[2], it8716f_flashport + 3);
171 OUTB(writearr[3], it8716f_flashport + 2);
172 OUTB(writearr[4], it8716f_flashport + 7);
173 writeenc = 0x3;
174 break;
175 default:
176 msg_pinfo("%s called with unsupported writecnt %i.\n",
177 __func__, writecnt);
178 return SPI_INVALID_LENGTH;
179 }
180 /*
181 * Start IO, 33 or 16 MHz, readcnt input bytes, writecnt output bytes.
182 * Note:
183 * We can't use writecnt directly, but have to use a strange encoding.
184 */
185 OUTB(((0x4 + (fast_spi ? 1 : 0)) << 4)
186 | ((readcnt & 0x3) << 2) | (writeenc), it8716f_flashport);
187
188 if (readcnt > 0) {
189 unsigned int i;
190
191 do {
192 busy = INB(it8716f_flashport) & 0x80;
193 } while (busy);
194
195 for (i = 0; i < readcnt; i++)
196 readarr[i] = INB(it8716f_flashport + 5 + i);
197 }
198
199 return 0;
200}
201
202/*
203 * IT8716F only allows maximum of 512 kb SPI mapped to LPC memory cycles
204 * Need to read this big flash using firmware cycles 3 byte at a time.
205 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700206static int it8716f_spi_chip_read(struct flashctx *flash, uint8_t *buf,
Edward O'Callaghan85726e52020-11-17 18:11:47 +1100207 unsigned int start, unsigned int len)
208{
209 fast_spi = 0;
210
211 /* FIXME: Check if someone explicitly requested to use IT87 SPI although
212 * the mainboard does not use IT87 SPI translation. This should be done
213 * via a programmer parameter for the internal programmer.
214 */
215 if ((flash->chip->total_size * 1024 > 512 * 1024)) {
216 default_spi_read(flash, buf, start, len);
217 } else {
218 mmio_readn((void *)(flash->virtual_memory + start), buf, len);
219 }
220
221 return 0;
222}
223
Patrick Georgiab8353e2017-02-03 18:32:01 +0100224static int it8716f_spi_chip_write_256(struct flashctx *flash, const uint8_t *buf,
Edward O'Callaghan85726e52020-11-17 18:11:47 +1100225 unsigned int start, unsigned int len)
226{
227 const struct flashchip *chip = flash->chip;
228 /*
229 * IT8716F only allows maximum of 512 kb SPI chip size for memory
230 * mapped access. It also can't write more than 1+3+256 bytes at once,
231 * so page_size > 256 bytes needs a fallback.
232 * FIXME: Split too big page writes into chunks IT87* can handle instead
233 * of degrading to single-byte program.
234 * FIXME: Check if someone explicitly requested to use IT87 SPI although
235 * the mainboard does not use IT87 SPI translation. This should be done
236 * via a programmer parameter for the internal programmer.
237 */
238 if ((chip->total_size * 1024 > 512 * 1024) || (chip->page_size > 256)) {
239 spi_chip_write_1(flash, buf, start, len);
240 } else {
241 unsigned int lenhere;
242
243 if (start % chip->page_size) {
244 /* start to the end of the page or to start + len,
245 * whichever is smaller.
246 */
247 lenhere = min(len, chip->page_size - start % chip->page_size);
248 spi_chip_write_1(flash, buf, start, lenhere);
249 start += lenhere;
250 len -= lenhere;
251 buf += lenhere;
252 }
253
254 while (len >= chip->page_size) {
255 it8716f_spi_page_program(flash, buf, start);
256 start += chip->page_size;
257 len -= chip->page_size;
258 buf += chip->page_size;
259 }
260 if (len)
261 spi_chip_write_1(flash, buf, start, len);
262 }
263
264 return 0;
265}
mkarcherd264e9e2011-05-11 17:07:07 +0000266
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100267static const struct spi_master spi_master_it87xx = {
Edward O'Callaghan3941bee2020-10-09 13:00:17 +1100268 .max_data_read = 3,
uwe8d342eb2011-07-28 08:13:25 +0000269 .max_data_write = MAX_DATA_UNSPECIFIED,
270 .command = it8716f_spi_send_command,
271 .multicommand = default_spi_send_multicommand,
272 .read = it8716f_spi_chip_read,
273 .write_256 = it8716f_spi_chip_write_256,
Edward O'Callaghan04ac7302020-05-14 18:03:40 +1000274 .write_aai = spi_chip_write_1,
mkarcherd264e9e2011-05-11 17:07:07 +0000275};
276
hailfingerc73ce6e2010-07-10 16:56:32 +0000277static uint16_t it87spi_probe(uint16_t port)
hailfinger2c361e42008-05-13 23:03:12 +0000278{
279 uint8_t tmp = 0;
hailfingerc236f9e2009-12-22 23:42:04 +0000280 uint16_t flashport = 0;
hailfinger2c361e42008-05-13 23:03:12 +0000281
hailfingerc73ce6e2010-07-10 16:56:32 +0000282 enter_conf_mode_ite(port);
Edward O'Callaghan95052952020-05-09 22:22:37 +1000283
284 char *param = extract_programmer_param("dualbiosindex");
285 if (param != NULL) {
286 sio_write(port, 0x07, 0x07); /* Select GPIO LDN */
287 tmp = sio_read(port, 0xEF);
288 if (*param == '\0') { /* Print current setting only. */
289 free(param);
290 } else {
291 char *dualbiosindex_suffix;
292 errno = 0;
293 long chip_index = strtol(param, &dualbiosindex_suffix, 0);
294 free(param);
295 if (errno != 0 || *dualbiosindex_suffix != '\0' || chip_index < 0 || chip_index > 1) {
296 msg_perr("DualBIOS: Invalid chip index requested - choose 0 or 1.\n");
297 exit_conf_mode_ite(port);
298 return 1;
299 }
300 if (chip_index != (tmp & 1)) {
301 msg_pdbg("DualBIOS: Previous chip index: %d\n", tmp & 1);
302 sio_write(port, 0xEF, (tmp & 0xFE) | chip_index);
303 tmp = sio_read(port, 0xEF);
304 if ((tmp & 1) != chip_index) {
305 msg_perr("DualBIOS: Chip selection failed.\n");
306 exit_conf_mode_ite(port);
307 return 1;
308 }
309 }
310 }
311 msg_pinfo("DualBIOS: Selected chip: %d\n", tmp & 1);
312 }
313
hailfingerc73ce6e2010-07-10 16:56:32 +0000314 /* NOLDN, reg 0x24, mask out lowest bit (suspend) */
315 tmp = sio_read(port, 0x24) & 0xFE;
hailfinger969e2f32011-09-08 00:00:29 +0000316 /* Check if LPC->SPI translation is active. */
317 if (!(tmp & 0x0e)) {
hailfingerc73ce6e2010-07-10 16:56:32 +0000318 msg_pdbg("No IT87* serial flash segment enabled.\n");
319 exit_conf_mode_ite(port);
320 /* Nothing to do. */
David Hendricks5e79c9f2013-11-04 22:05:08 -0800321 return 1;
hailfingerc73ce6e2010-07-10 16:56:32 +0000322 }
323 msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
324 0xFFFE0000, 0xFFFFFFFF, (tmp & 1 << 1) ? "en" : "dis");
325 msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
326 0x000E0000, 0x000FFFFF, (tmp & 1 << 1) ? "en" : "dis");
327 msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
328 0xFFEE0000, 0xFFEFFFFF, (tmp & 1 << 2) ? "en" : "dis");
329 msg_pdbg("Serial flash segment 0x%08x-0x%08x %sabled\n",
330 0xFFF80000, 0xFFFEFFFF, (tmp & 1 << 3) ? "en" : "dis");
331 msg_pdbg("LPC write to serial flash %sabled\n",
332 (tmp & 1 << 4) ? "en" : "dis");
333 /* The LPC->SPI force write enable below only makes sense for
334 * non-programmer mode.
335 */
336 /* If any serial flash segment is enabled, enable writing. */
337 if ((tmp & 0xe) && (!(tmp & 1 << 4))) {
338 msg_pdbg("Enabling LPC write to serial flash\n");
339 tmp |= 1 << 4;
340 sio_write(port, 0x24, tmp);
341 }
342 msg_pdbg("Serial flash pin %i\n", (tmp & 1 << 5) ? 87 : 29);
343 /* LDN 0x7, reg 0x64/0x65 */
344 sio_write(port, 0x07, 0x7);
345 flashport = sio_read(port, 0x64) << 8;
346 flashport |= sio_read(port, 0x65);
347 msg_pdbg("Serial flash port 0x%04x\n", flashport);
348 /* Non-default port requested? */
Patrick Georgi048dbdb2017-04-11 20:45:07 +0200349 param = extract_programmer_param("it87spiport");
350 if (param) {
hailfingerc73ce6e2010-07-10 16:56:32 +0000351 char *endptr = NULL;
352 unsigned long forced_flashport;
Patrick Georgi048dbdb2017-04-11 20:45:07 +0200353 forced_flashport = strtoul(param, &endptr, 0);
hailfingerc73ce6e2010-07-10 16:56:32 +0000354 /* Port 0, port >0x1000, unaligned ports and garbage strings
355 * are rejected.
hailfingerddd5d7b2010-03-25 02:50:40 +0000356 */
hailfingerc73ce6e2010-07-10 16:56:32 +0000357 if (!forced_flashport || (forced_flashport >= 0x1000) ||
358 (forced_flashport & 0x7) || (*endptr != '\0')) {
359 /* Using ports below 0x100 is a really bad idea, and
360 * should only be done if no port between 0x100 and
361 * 0xff8 works due to routing issues.
362 */
363 msg_perr("Error: it87spiport specified, but no valid "
364 "port specified.\nPort must be a multiple of "
365 "0x8 and lie between 0x100 and 0xff8.\n");
Patrick Georgi048dbdb2017-04-11 20:45:07 +0200366 exit_conf_mode_ite(port);
367 free(param);
hailfinger94e090c2011-04-27 14:34:08 +0000368 return 1;
hailfingerc73ce6e2010-07-10 16:56:32 +0000369 } else {
370 flashport = (uint16_t)forced_flashport;
371 msg_pinfo("Forcing serial flash port 0x%04x\n",
372 flashport);
373 sio_write(port, 0x64, (flashport >> 8));
374 sio_write(port, 0x65, (flashport & 0xff));
hailfinger4500b082009-07-11 18:05:42 +0000375 }
hailfinger2c361e42008-05-13 23:03:12 +0000376 }
Patrick Georgi048dbdb2017-04-11 20:45:07 +0200377 free(param);
hailfingerc73ce6e2010-07-10 16:56:32 +0000378 exit_conf_mode_ite(port);
379 it8716f_flashport = flashport;
hailfinger76bb7e92011-11-09 23:40:00 +0000380 if (internal_buses_supported & BUS_SPI)
hailfingerc73ce6e2010-07-10 16:56:32 +0000381 msg_pdbg("Overriding chipset SPI with IT87 SPI.\n");
hailfinger94e090c2011-04-27 14:34:08 +0000382 /* FIXME: Add the SPI bus or replace the other buses with it? */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100383 register_spi_master(&spi_master_it87xx);
hailfingerc73ce6e2010-07-10 16:56:32 +0000384 return 0;
hailfinger2c361e42008-05-13 23:03:12 +0000385}
386
David Hendricksac1d25c2016-08-09 17:00:58 -0700387int init_superio_ite(void)
hailfinger2c361e42008-05-13 23:03:12 +0000388{
Patrick Georgi048dbdb2017-04-11 20:45:07 +0200389 int i;
390 int ret = 0;
391 int chips_found = 0;
stepan3bdf6182008-06-30 23:45:22 +0000392
hailfinger94e090c2011-04-27 14:34:08 +0000393 for (i = 0; i < superio_count; i++) {
394 if (superios[i].vendor != SUPERIO_VENDOR_ITE)
395 continue;
hailfinger2c361e42008-05-13 23:03:12 +0000396
hailfinger94e090c2011-04-27 14:34:08 +0000397 switch (superios[i].model) {
398 case 0x8500:
399 case 0x8502:
400 case 0x8510:
401 case 0x8511:
402 case 0x8512:
403 /* FIXME: This should be enabled, but we need a check
404 * for laptop whitelisting due to the amount of things
405 * which can go wrong if the EC firmware does not
406 * implement the interface we want.
407 */
David Hendricksac1d25c2016-08-09 17:00:58 -0700408 if (!it85xx_spi_init(superios[i]))
David Hendricks5e79c9f2013-11-04 22:05:08 -0800409 chips_found++;
hailfinger94e090c2011-04-27 14:34:08 +0000410 break;
Shawn Nematbakhsh3404b1a2012-07-26 15:19:58 -0700411 case 0x8518:
David Hendricksac1d25c2016-08-09 17:00:58 -0700412 if (!it8518_spi_init(superios[i]))
David Hendricks5e79c9f2013-11-04 22:05:08 -0800413 chips_found++;
Shawn Nematbakhsh3404b1a2012-07-26 15:19:58 -0700414 break;
hailfinger94e090c2011-04-27 14:34:08 +0000415 case 0x8705:
David Hendricks5e79c9f2013-11-04 22:05:08 -0800416 if (!it8705f_write_enable(superios[i].port))
417 chips_found++;
hailfinger94e090c2011-04-27 14:34:08 +0000418 break;
419 case 0x8716:
420 case 0x8718:
421 case 0x8720:
Edward O'Callaghan95052952020-05-09 22:22:37 +1000422 case 0x8728:
David Hendricks5e79c9f2013-11-04 22:05:08 -0800423 if (!it87spi_probe(superios[i].port))
424 chips_found++;
hailfinger94e090c2011-04-27 14:34:08 +0000425 break;
426 default:
Patrick Georgi048dbdb2017-04-11 20:45:07 +0200427 msg_pdbg2("Super I/O ID 0x%04hx is not on the list of flash-capable controllers.\n",
428 superios[i].model);
hailfinger94e090c2011-04-27 14:34:08 +0000429 }
hailfingerd9f5da22009-06-28 10:57:58 +0000430 }
David Hendricks5e79c9f2013-11-04 22:05:08 -0800431
432 if (chips_found == 0) {
433 ret = 1; /* failed to probe/initialize/enable chip */
434 } else if (chips_found == 1) {
435 ret = 0; /* success */
436 } else {
437 msg_pdbg("%s: Found %d programmable ECs/SuperIOs, aborting.\n",
438 __func__, chips_found);
439 ret = 1;
440 }
hailfingera916b422009-06-01 02:08:58 +0000441 return ret;
hailfinger26e212b2009-05-31 18:00:57 +0000442}
443
Edward O'Callaghan85726e52020-11-17 18:11:47 +1100444#endif /* defined(__i386__) || defined(__x86_64__) */