blob: 7efc680ef306afe2944212ea00f0d5d93bfb2f22 [file] [log] [blame]
Donald Huang44ebb042011-02-22 17:16:34 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
5 * Copyright (C) 2008 Ronald Hoogenboom <ronald@zonnet.nl>
6 * Copyright (C) 2008 coresystems GmbH
7 * Copyright (C) 2010 Google Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/*
24 * Contains the ITE IT85* SPI specific routines
25 */
26
27#if defined(__i386__) || defined(__x86_64__)
28
29#include <string.h>
David Hendricks4e748392011-02-28 23:58:15 +000030#include <stdio.h>
Donald Huang44ebb042011-02-22 17:16:34 +000031#include <stdlib.h>
32#include "flash.h"
Donald Huang44ebb042011-02-22 17:16:34 +000033#include "spi.h"
34#include "programmer.h"
Patrick Georgi32508eb2012-07-20 20:35:14 +000035#include "hwaccess.h"
Donald Huang44ebb042011-02-22 17:16:34 +000036
David Hendricks4e748392011-02-28 23:58:15 +000037#define MAX_TIMEOUT 100000
38#define MAX_TRY 5
39
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +000040/* Constants for I/O ports */
Donald Huang44ebb042011-02-22 17:16:34 +000041#define ITE_SUPERIO_PORT1 0x2e
42#define ITE_SUPERIO_PORT2 0x4e
43
44/* Legacy I/O */
David Hendricks4e748392011-02-28 23:58:15 +000045#define LEGACY_KBC_PORT_DATA 0x60
46#define LEGACY_KBC_PORT_CMD 0x64
Donald Huang44ebb042011-02-22 17:16:34 +000047
48/* Constants for Logical Device registers */
49#define LDNSEL 0x07
Donald Huang44ebb042011-02-22 17:16:34 +000050
51/* These are standard Super I/O 16-bit base address registers */
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +000052#define SHM_IO_BAR0 0x60 /* big-endian, this is high bits */
53#define SHM_IO_BAR1 0x61
Donald Huang44ebb042011-02-22 17:16:34 +000054
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +000055/* The 8042 keyboard controller uses an input buffer and an output buffer to
56 * communicate with the host CPU. Both buffers are 1-byte depth. That means
57 * IBF is set to 1 when the host CPU sends a command to the input buffer
58 * of the EC. IBF is cleared to 0 once the command is read by the EC.
59 */
David Hendricks4e748392011-02-28 23:58:15 +000060#define KB_IBF (1 << 1) /* Input Buffer Full */
61#define KB_OBF (1 << 0) /* Output Buffer Full */
62
Donald Huang44ebb042011-02-22 17:16:34 +000063/* IT8502 supports two access modes:
64 * LPC_MEMORY: through the memory window in 0xFFFFFxxx (follow mode)
65 * LPC_IO: through I/O port (so called indirect memory)
66 */
67#undef LPC_MEMORY
68#define LPC_IO
69
70#ifdef LPC_IO
71/* macro to fill in indirect-access registers. */
72#define INDIRECT_A0(base, value) OUTB(value, (base) + 0) /* little-endian */
73#define INDIRECT_A1(base, value) OUTB(value, (base) + 1)
74#define INDIRECT_A2(base, value) OUTB(value, (base) + 2)
75#define INDIRECT_A3(base, value) OUTB(value, (base) + 3)
76#define INDIRECT_READ(base) INB((base) + 4)
77#define INDIRECT_WRITE(base, value) OUTB(value, (base) + 4)
78#endif /* LPC_IO */
79
80#ifdef LPC_IO
81unsigned int shm_io_base;
82#endif
83unsigned char *ce_high, *ce_low;
84static int it85xx_scratch_rom_reenter = 0;
85
David Hendricks4e748392011-02-28 23:58:15 +000086/* This function will poll the keyboard status register until either
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000087 * an expected value shows up, or the timeout is reached.
88 * timeout is in usec.
David Hendricks4e748392011-02-28 23:58:15 +000089 *
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000090 * Returns: 0 -- the expected value showed up.
91 * 1 -- timeout.
David Hendricks4e748392011-02-28 23:58:15 +000092 */
Uwe Hermann91f4afa2011-07-28 08:13:25 +000093static int wait_for(const unsigned int mask, const unsigned int expected_value,
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +000094 const int timeout, const char * error_message,
95 const char * function_name, const int lineno)
Uwe Hermann91f4afa2011-07-28 08:13:25 +000096{
David Hendricks4e748392011-02-28 23:58:15 +000097 int time_passed;
98
99 for (time_passed = 0;; ++time_passed) {
100 if ((INB(LEGACY_KBC_PORT_CMD) & mask) == expected_value)
101 return 0;
102 if (time_passed >= timeout)
103 break;
104 programmer_delay(1);
105 }
106 if (error_message)
107 msg_perr("%s():%d %s", function_name, lineno, error_message);
108 return 1;
109}
110
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000111/* IT8502 employs a scratch RAM when flash is being updated. Call the following
David Hendricks4e748392011-02-28 23:58:15 +0000112 * two functions before/after flash erase/program. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000113void it85xx_enter_scratch_rom(void)
Donald Huang44ebb042011-02-22 17:16:34 +0000114{
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000115 int ret, tries;
David Hendricks4e748392011-02-28 23:58:15 +0000116
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000117 msg_pdbg("%s():%d was called ...\n", __func__, __LINE__);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000118 if (it85xx_scratch_rom_reenter > 0)
119 return;
David Hendricks4e748392011-02-28 23:58:15 +0000120
121#if 0
122 /* FIXME: this a workaround for the bug that SMBus signal would
123 * interfere the EC firmware update. Should be removed if
124 * we find out the root cause. */
125 ret = system("stop powerd >&2");
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000126 if (ret)
David Hendricks4e748392011-02-28 23:58:15 +0000127 msg_perr("Cannot stop powerd.\n");
David Hendricks4e748392011-02-28 23:58:15 +0000128#endif
129
130 for (tries = 0; tries < MAX_TRY; ++tries) {
131 /* Wait until IBF (input buffer) is not full. */
132 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
133 "* timeout at waiting for IBF==0.\n",
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000134 __func__, __LINE__))
David Hendricks4e748392011-02-28 23:58:15 +0000135 continue;
136
137 /* Copy EC firmware to SRAM. */
138 OUTB(0xb4, LEGACY_KBC_PORT_CMD);
139
140 /* Confirm EC has taken away the command. */
141 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
142 "* timeout at taking command.\n",
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000143 __func__, __LINE__))
David Hendricks4e748392011-02-28 23:58:15 +0000144 continue;
145
146 /* Waiting for OBF (output buffer) has data.
147 * Note sometimes the replied command might be stolen by kernel
148 * ISR so that it is okay as long as the command is 0xFA. */
149 if (wait_for(KB_OBF, KB_OBF, MAX_TIMEOUT, NULL, NULL, 0))
150 msg_pdbg("%s():%d * timeout at waiting for OBF.\n",
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000151 __func__, __LINE__);
David Hendricks4e748392011-02-28 23:58:15 +0000152 if ((ret = INB(LEGACY_KBC_PORT_DATA)) == 0xFA) {
153 break;
154 } else {
155 msg_perr("%s():%d * not run on SRAM ret=%d\n",
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000156 __func__, __LINE__, ret);
David Hendricks4e748392011-02-28 23:58:15 +0000157 continue;
158 }
159 }
160
161 if (tries < MAX_TRY) {
162 /* EC already runs on SRAM */
163 it85xx_scratch_rom_reenter++;
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000164 msg_pdbg("%s():%d * SUCCESS.\n", __func__, __LINE__);
David Hendricks4e748392011-02-28 23:58:15 +0000165 } else {
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000166 msg_perr("%s():%d * Max try reached.\n", __func__, __LINE__);
David Hendricks4e748392011-02-28 23:58:15 +0000167 }
Donald Huang44ebb042011-02-22 17:16:34 +0000168}
169
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000170void it85xx_exit_scratch_rom(void)
Donald Huang44ebb042011-02-22 17:16:34 +0000171{
David Hendricks4e748392011-02-28 23:58:15 +0000172#if 0
173 int ret;
174#endif
175 int tries;
176
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000177 msg_pdbg("%s():%d was called ...\n", __func__, __LINE__);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000178 if (it85xx_scratch_rom_reenter <= 0)
179 return;
David Hendricks4e748392011-02-28 23:58:15 +0000180
181 for (tries = 0; tries < MAX_TRY; ++tries) {
182 /* Wait until IBF (input buffer) is not full. */
183 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
184 "* timeout at waiting for IBF==0.\n",
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000185 __func__, __LINE__))
David Hendricks4e748392011-02-28 23:58:15 +0000186 continue;
187
188 /* Exit SRAM. Run on flash. */
189 OUTB(0xFE, LEGACY_KBC_PORT_CMD);
190
191 /* Confirm EC has taken away the command. */
192 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
193 "* timeout at taking command.\n",
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000194 __func__, __LINE__)) {
David Hendricks4e748392011-02-28 23:58:15 +0000195 /* We cannot ensure if EC has exited update mode.
196 * If EC is in normal mode already, a further 0xFE
197 * command will reboot system. So, exit loop here. */
198 tries = MAX_TRY;
199 break;
200 }
201
202 break;
203 }
204
205 if (tries < MAX_TRY) {
206 it85xx_scratch_rom_reenter = 0;
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000207 msg_pdbg("%s():%d * SUCCESS.\n", __func__, __LINE__);
David Hendricks4e748392011-02-28 23:58:15 +0000208 } else {
Uwe Hermanne187d5e2011-07-29 20:13:45 +0000209 msg_perr("%s():%d * Max try reached.\n", __func__, __LINE__);
David Hendricks4e748392011-02-28 23:58:15 +0000210 }
211
212#if 0
213 /* FIXME: this a workaround for the bug that SMBus signal would
214 * interfere the EC firmware update. Should be removed if
215 * we find out the root cause. */
216 ret = system("start powerd >&2");
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000217 if (ret)
David Hendricks4e748392011-02-28 23:58:15 +0000218 msg_perr("Cannot start powerd again.\n");
David Hendricks4e748392011-02-28 23:58:15 +0000219#endif
Donald Huang44ebb042011-02-22 17:16:34 +0000220}
221
David Hendricks8bb20212011-06-14 01:35:36 +0000222static int it85xx_shutdown(void *data)
223{
224 msg_pdbg("%s():%d\n", __func__, __LINE__);
225 it85xx_exit_scratch_rom();
226
227 return 0; /* FIXME: Should probably return something meaningful */
228}
229
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000230static int it85xx_spi_common_init(struct superio s)
Donald Huang44ebb042011-02-22 17:16:34 +0000231{
232 chipaddr base;
233
234 msg_pdbg("%s():%d superio.vendor=0x%02x\n", __func__, __LINE__,
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000235 s.vendor);
Donald Huang44ebb042011-02-22 17:16:34 +0000236
David Hendricks8bb20212011-06-14 01:35:36 +0000237 if (register_shutdown(it85xx_shutdown, NULL))
238 return 1;
239
Donald Huang44ebb042011-02-22 17:16:34 +0000240#ifdef LPC_IO
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000241 /* Get LPCPNP of SHM. That's big-endian. */
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000242 sio_write(s.port, LDNSEL, 0x0F); /* Set LDN to SHM (0x0F) */
243 shm_io_base = (sio_read(s.port, SHM_IO_BAR0) << 8) +
244 sio_read(s.port, SHM_IO_BAR1);
Donald Huang44ebb042011-02-22 17:16:34 +0000245 msg_pdbg("%s():%d shm_io_base=0x%04x\n", __func__, __LINE__,
246 shm_io_base);
247
248 /* These pointers are not used directly. They will be send to EC's
249 * register for indirect access. */
250 base = 0xFFFFF000;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000251 ce_high = ((unsigned char *)base) + 0xE00; /* 0xFFFFFE00 */
252 ce_low = ((unsigned char *)base) + 0xD00; /* 0xFFFFFD00 */
Donald Huang44ebb042011-02-22 17:16:34 +0000253
254 /* pre-set indirect-access registers since in most of cases they are
255 * 0xFFFFxx00. */
256 INDIRECT_A0(shm_io_base, base & 0xFF);
257 INDIRECT_A2(shm_io_base, (base >> 16) & 0xFF);
258 INDIRECT_A3(shm_io_base, (base >> 24));
259#endif
260#ifdef LPC_MEMORY
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000261 /* FIXME: We should block accessing that region for anything else.
262 * Major TODO here, and it will be a lot of work.
263 */
264 base = (chipaddr)physmap("it85 communication", 0xFFFFF000, 0x1000);
Stefan Tauner7fb5aa02013-08-14 15:48:44 +0000265 if (base == (chipaddr)ERROR_PTR)
266 return 1;
267
Donald Huang44ebb042011-02-22 17:16:34 +0000268 msg_pdbg("%s():%d base=0x%08x\n", __func__, __LINE__,
269 (unsigned int)base);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000270 ce_high = (unsigned char *)(base + 0xE00); /* 0xFFFFFE00 */
271 ce_low = (unsigned char *)(base + 0xD00); /* 0xFFFFFD00 */
Donald Huang44ebb042011-02-22 17:16:34 +0000272#endif
273
Donald Huang44ebb042011-02-22 17:16:34 +0000274 return 0;
275}
276
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000277static int it85xx_spi_send_command(struct flashctx *flash,
278 unsigned int writecnt, unsigned int readcnt,
279 const unsigned char *writearr,
280 unsigned char *readarr);
Michael Karcherb9dbe482011-05-11 17:07:07 +0000281
282static const struct spi_programmer spi_programmer_it85xx = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000283 .type = SPI_CONTROLLER_IT85XX,
284 .max_data_read = 64,
285 .max_data_write = 64,
286 .command = it85xx_spi_send_command,
287 .multicommand = default_spi_send_multicommand,
288 .read = default_spi_read,
289 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000290 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000291};
292
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000293int it85xx_spi_init(struct superio s)
Donald Huang44ebb042011-02-22 17:16:34 +0000294{
295 int ret;
296
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000297 if (!(internal_buses_supported & BUS_FWH)) {
Donald Huang44ebb042011-02-22 17:16:34 +0000298 msg_pdbg("%s():%d buses not support FWH\n", __func__, __LINE__);
299 return 1;
300 }
Carl-Daniel Hailfingerbfecef62011-04-27 14:34:08 +0000301 ret = it85xx_spi_common_init(s);
Donald Huang44ebb042011-02-22 17:16:34 +0000302 msg_pdbg("FWH: %s():%d ret=%d\n", __func__, __LINE__, ret);
303 if (!ret) {
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000304 msg_pdbg("%s: internal_buses_supported=0x%x\n", __func__,
305 internal_buses_supported);
306 /* Check for FWH because IT85 listens to FWH cycles.
307 * FIXME: The big question is whether FWH cycles are necessary
308 * for communication even if LPC_IO is defined.
309 */
310 if (internal_buses_supported & BUS_FWH)
311 msg_pdbg("Registering IT85 SPI.\n");
312 /* FIXME: Really leave FWH enabled? We can't use this region
313 * anymore since accessing it would mess up IT85 communication.
314 * If we decide to disable FWH for this region, we should print
315 * a debug message about it.
316 */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000317 /* Set this as SPI controller. */
Michael Karcherb9dbe482011-05-11 17:07:07 +0000318 register_spi_programmer(&spi_programmer_it85xx);
Donald Huang44ebb042011-02-22 17:16:34 +0000319 }
320 return ret;
321}
322
Donald Huang44ebb042011-02-22 17:16:34 +0000323/* According to ITE 8502 document, the procedure to follow mode is following:
324 * 1. write 0x00 to LPC/FWH address 0xffff_fexxh (drive CE# high)
325 * 2. write data to LPC/FWH address 0xffff_fdxxh (drive CE# low and MOSI
326 * with data)
327 * 3. read date from LPC/FWH address 0xffff_fdxxh (drive CE# low and get
328 * data from MISO)
329 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000330static int it85xx_spi_send_command(struct flashctx *flash,
331 unsigned int writecnt, unsigned int readcnt,
332 const unsigned char *writearr,
333 unsigned char *readarr)
Donald Huang44ebb042011-02-22 17:16:34 +0000334{
335 int i;
336
337 it85xx_enter_scratch_rom();
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000338 /* Exit scratch ROM ONLY when programmer shuts down. Otherwise, the
339 * temporary flash state may halt the EC.
340 */
Donald Huang44ebb042011-02-22 17:16:34 +0000341
342#ifdef LPC_IO
343 INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff);
344 INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/
345 INDIRECT_A1(shm_io_base, (((unsigned long int)ce_low) >> 8) & 0xff);
346#endif
347#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000348 mmio_writeb(0, ce_high);
Donald Huang44ebb042011-02-22 17:16:34 +0000349#endif
350 for (i = 0; i < writecnt; ++i) {
351#ifdef LPC_IO
352 INDIRECT_WRITE(shm_io_base, writearr[i]);
353#endif
354#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000355 mmio_writeb(writearr[i], ce_low);
Donald Huang44ebb042011-02-22 17:16:34 +0000356#endif
357 }
358 for (i = 0; i < readcnt; ++i) {
359#ifdef LPC_IO
360 readarr[i] = INDIRECT_READ(shm_io_base);
361#endif
362#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000363 readarr[i] = mmio_readb(ce_low);
Donald Huang44ebb042011-02-22 17:16:34 +0000364#endif
365 }
David Hendricks4e748392011-02-28 23:58:15 +0000366#ifdef LPC_IO
367 INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff);
368 INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/
369#endif
370#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000371 mmio_writeb(0, ce_high);
David Hendricks4e748392011-02-28 23:58:15 +0000372#endif
373
Donald Huang44ebb042011-02-22 17:16:34 +0000374 return 0;
375}
376
Donald Huang44ebb042011-02-22 17:16:34 +0000377#endif