blob: d122a1886345c4a32a9f15de27dab1c93cd41921 [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"
33#include "chipdrivers.h"
34#include "spi.h"
35#include "programmer.h"
36
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
50#define CHIP_ID_BYTE1_REG 0x20
51#define CHIP_ID_BYTE2_REG 0x21
52#define CHIP_CHIP_VER_REG 0x22
53
54/* These are standard Super I/O 16-bit base address registers */
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +000055#define SHM_IO_BAR0 0x60 /* big-endian, this is high bits */
56#define SHM_IO_BAR1 0x61
Donald Huang44ebb042011-02-22 17:16:34 +000057
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +000058/* The 8042 keyboard controller uses an input buffer and an output buffer to
59 * communicate with the host CPU. Both buffers are 1-byte depth. That means
60 * IBF is set to 1 when the host CPU sends a command to the input buffer
61 * of the EC. IBF is cleared to 0 once the command is read by the EC.
62 */
David Hendricks4e748392011-02-28 23:58:15 +000063#define KB_IBF (1 << 1) /* Input Buffer Full */
64#define KB_OBF (1 << 0) /* Output Buffer Full */
65
Donald Huang44ebb042011-02-22 17:16:34 +000066/* IT8502 supports two access modes:
67 * LPC_MEMORY: through the memory window in 0xFFFFFxxx (follow mode)
68 * LPC_IO: through I/O port (so called indirect memory)
69 */
70#undef LPC_MEMORY
71#define LPC_IO
72
73#ifdef LPC_IO
74/* macro to fill in indirect-access registers. */
75#define INDIRECT_A0(base, value) OUTB(value, (base) + 0) /* little-endian */
76#define INDIRECT_A1(base, value) OUTB(value, (base) + 1)
77#define INDIRECT_A2(base, value) OUTB(value, (base) + 2)
78#define INDIRECT_A3(base, value) OUTB(value, (base) + 3)
79#define INDIRECT_READ(base) INB((base) + 4)
80#define INDIRECT_WRITE(base, value) OUTB(value, (base) + 4)
81#endif /* LPC_IO */
82
83#ifdef LPC_IO
84unsigned int shm_io_base;
85#endif
86unsigned char *ce_high, *ce_low;
87static int it85xx_scratch_rom_reenter = 0;
88
89uint16_t probe_id_ite85(uint16_t port)
90{
91 uint16_t id;
92
93 id = sio_read(port, CHIP_ID_BYTE1_REG) << 8 |
94 sio_read(port, CHIP_ID_BYTE2_REG);
95
96 return id;
97}
98
99struct superio probe_superio_ite85xx(void)
100{
101 struct superio ret = {};
102 uint16_t ite_ports[] = {ITE_SUPERIO_PORT1, ITE_SUPERIO_PORT2, 0};
103 uint16_t *i = ite_ports;
104
105 ret.vendor = SUPERIO_VENDOR_ITE;
106 for (; *i; i++) {
107 ret.port = *i;
108 ret.model = probe_id_ite85(ret.port);
109 switch (ret.model >> 8) {
110 case 0x85:
David Hendricks4e748392011-02-28 23:58:15 +0000111 msg_pdbg("Found EC: ITE85xx (Vendor:0x%02x,ID:0x%02x,"
112 "Rev:0x%02x) on sio_port:0x%x.\n",
113 ret.model >> 8, ret.model & 0xff,
114 sio_read(ret.port, CHIP_CHIP_VER_REG),
115 ret.port);
Donald Huang44ebb042011-02-22 17:16:34 +0000116 return ret;
117 }
118 }
119
120 /* No good ID found. */
121 ret.vendor = SUPERIO_VENDOR_NONE;
122 ret.port = 0;
123 ret.model = 0;
124 return ret;
125}
126
David Hendricks4e748392011-02-28 23:58:15 +0000127/* This function will poll the keyboard status register until either
128 * an expected value shows up, or
129 * timeout reaches.
130 *
131 * Returns: 0 -- the expected value has shown.
132 * 1 -- timeout reached.
133 */
134static int wait_for(
135 const unsigned int mask,
136 const unsigned int expected_value,
137 const int timeout, /* in usec */
138 const char* error_message,
139 const char* function_name,
140 const int lineno
141) {
142 int time_passed;
143
144 for (time_passed = 0;; ++time_passed) {
145 if ((INB(LEGACY_KBC_PORT_CMD) & mask) == expected_value)
146 return 0;
147 if (time_passed >= timeout)
148 break;
149 programmer_delay(1);
150 }
151 if (error_message)
152 msg_perr("%s():%d %s", function_name, lineno, error_message);
153 return 1;
154}
155
156/* IT8502 employs a scratch ram when flash is being updated. Call the following
157 * two functions before/after flash erase/program. */
Donald Huang44ebb042011-02-22 17:16:34 +0000158void it85xx_enter_scratch_rom()
159{
David Hendricks4e748392011-02-28 23:58:15 +0000160 int ret;
161 int tries;
162
163 msg_pdbg("%s():%d was called ...\n", __FUNCTION__, __LINE__);
Donald Huang44ebb042011-02-22 17:16:34 +0000164 if (it85xx_scratch_rom_reenter > 0) return;
David Hendricks4e748392011-02-28 23:58:15 +0000165
166#if 0
167 /* FIXME: this a workaround for the bug that SMBus signal would
168 * interfere the EC firmware update. Should be removed if
169 * we find out the root cause. */
170 ret = system("stop powerd >&2");
171 if (ret) {
172 msg_perr("Cannot stop powerd.\n");
173 }
174#endif
175
176 for (tries = 0; tries < MAX_TRY; ++tries) {
177 /* Wait until IBF (input buffer) is not full. */
178 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
179 "* timeout at waiting for IBF==0.\n",
180 __FUNCTION__, __LINE__))
181 continue;
182
183 /* Copy EC firmware to SRAM. */
184 OUTB(0xb4, LEGACY_KBC_PORT_CMD);
185
186 /* Confirm EC has taken away the command. */
187 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
188 "* timeout at taking command.\n",
189 __FUNCTION__, __LINE__))
190 continue;
191
192 /* Waiting for OBF (output buffer) has data.
193 * Note sometimes the replied command might be stolen by kernel
194 * ISR so that it is okay as long as the command is 0xFA. */
195 if (wait_for(KB_OBF, KB_OBF, MAX_TIMEOUT, NULL, NULL, 0))
196 msg_pdbg("%s():%d * timeout at waiting for OBF.\n",
197 __FUNCTION__, __LINE__);
198 if ((ret = INB(LEGACY_KBC_PORT_DATA)) == 0xFA) {
199 break;
200 } else {
201 msg_perr("%s():%d * not run on SRAM ret=%d\n",
202 __FUNCTION__, __LINE__, ret);
203 continue;
204 }
205 }
206
207 if (tries < MAX_TRY) {
208 /* EC already runs on SRAM */
209 it85xx_scratch_rom_reenter++;
210 msg_pdbg("%s():%d * SUCCESS.\n", __FUNCTION__, __LINE__);
211 } else {
212 msg_perr("%s():%d * Max try reached.\n",
213 __FUNCTION__, __LINE__);
214 }
Donald Huang44ebb042011-02-22 17:16:34 +0000215}
216
217void it85xx_exit_scratch_rom()
218{
David Hendricks4e748392011-02-28 23:58:15 +0000219#if 0
220 int ret;
221#endif
222 int tries;
223
224 msg_pdbg("%s():%d was called ...\n", __FUNCTION__, __LINE__);
Donald Huang44ebb042011-02-22 17:16:34 +0000225 if (it85xx_scratch_rom_reenter <= 0) return;
David Hendricks4e748392011-02-28 23:58:15 +0000226
227 for (tries = 0; tries < MAX_TRY; ++tries) {
228 /* Wait until IBF (input buffer) is not full. */
229 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
230 "* timeout at waiting for IBF==0.\n",
231 __FUNCTION__, __LINE__))
232 continue;
233
234 /* Exit SRAM. Run on flash. */
235 OUTB(0xFE, LEGACY_KBC_PORT_CMD);
236
237 /* Confirm EC has taken away the command. */
238 if (wait_for(KB_IBF, 0, MAX_TIMEOUT,
239 "* timeout at taking command.\n",
240 __FUNCTION__, __LINE__)) {
241 /* We cannot ensure if EC has exited update mode.
242 * If EC is in normal mode already, a further 0xFE
243 * command will reboot system. So, exit loop here. */
244 tries = MAX_TRY;
245 break;
246 }
247
248 break;
249 }
250
251 if (tries < MAX_TRY) {
252 it85xx_scratch_rom_reenter = 0;
253 msg_pdbg("%s():%d * SUCCESS.\n", __FUNCTION__, __LINE__);
254 } else {
255 msg_perr("%s():%d * Max try reached.\n",
256 __FUNCTION__, __LINE__);
257 }
258
259#if 0
260 /* FIXME: this a workaround for the bug that SMBus signal would
261 * interfere the EC firmware update. Should be removed if
262 * we find out the root cause. */
263 ret = system("start powerd >&2");
264 if (ret) {
265 msg_perr("Cannot start powerd again.\n");
266 }
267#endif
Donald Huang44ebb042011-02-22 17:16:34 +0000268}
269
270int it85xx_spi_common_init(void)
271{
272 chipaddr base;
273
274 msg_pdbg("%s():%d superio.vendor=0x%02x\n", __func__, __LINE__,
275 superio.vendor);
276 if (superio.vendor != SUPERIO_VENDOR_ITE)
277 return 1;
278
279#ifdef LPC_IO
280 /* Get LPCPNP of SHM. That's big-endian */
281 sio_write(superio.port, LDNSEL, 0x0F); /* Set LDN to SHM (0x0F) */
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000282 shm_io_base = (sio_read(superio.port, SHM_IO_BAR0) << 8) +
283 sio_read(superio.port, SHM_IO_BAR1);
Donald Huang44ebb042011-02-22 17:16:34 +0000284 msg_pdbg("%s():%d shm_io_base=0x%04x\n", __func__, __LINE__,
285 shm_io_base);
286
287 /* These pointers are not used directly. They will be send to EC's
288 * register for indirect access. */
289 base = 0xFFFFF000;
290 ce_high = ((unsigned char*)base) + 0xE00; /* 0xFFFFFE00 */
291 ce_low = ((unsigned char*)base) + 0xD00; /* 0xFFFFFD00 */
292
293 /* pre-set indirect-access registers since in most of cases they are
294 * 0xFFFFxx00. */
295 INDIRECT_A0(shm_io_base, base & 0xFF);
296 INDIRECT_A2(shm_io_base, (base >> 16) & 0xFF);
297 INDIRECT_A3(shm_io_base, (base >> 24));
298#endif
299#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000300 base = (chipaddr)programmer_map_flash_region("it85 communication",
301 0xFFFFF000, 0x1000);
Donald Huang44ebb042011-02-22 17:16:34 +0000302 msg_pdbg("%s():%d base=0x%08x\n", __func__, __LINE__,
303 (unsigned int)base);
304 ce_high = (unsigned char*)(base + 0xE00); /* 0xFFFFFE00 */
305 ce_low = (unsigned char*)(base + 0xD00); /* 0xFFFFFD00 */
306#endif
307
308 /* Set this as spi controller. */
309 spi_controller = SPI_CONTROLLER_IT85XX;
310
311 return 0;
312}
313
314/* Called by programmer_entry .init */
315int it85xx_spi_init(void)
316{
317 int ret;
318
319 get_io_perms();
320 /* Probe for the Super I/O chip and fill global struct superio. */
321 probe_superio();
322 ret = it85xx_spi_common_init();
323 if (!ret) {
324 buses_supported = CHIP_BUSTYPE_SPI;
325 } else {
326 buses_supported = CHIP_BUSTYPE_NONE;
327 }
328 return ret;
329}
330
331/* Called by internal_init() */
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000332int it85xx_probe_spi_flash(void)
Donald Huang44ebb042011-02-22 17:16:34 +0000333{
334 int ret;
335
336 if (!(buses_supported & CHIP_BUSTYPE_FWH)) {
337 msg_pdbg("%s():%d buses not support FWH\n", __func__, __LINE__);
338 return 1;
339 }
340 ret = it85xx_spi_common_init();
341 msg_pdbg("FWH: %s():%d ret=%d\n", __func__, __LINE__, ret);
342 if (!ret) {
343 msg_pdbg("%s():%d buses_supported=0x%x\n", __func__, __LINE__,
344 buses_supported);
345 if (buses_supported & CHIP_BUSTYPE_FWH)
346 msg_pdbg("Overriding chipset SPI with IT85 FWH|SPI.\n");
347 buses_supported |= CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI;
348 }
349 return ret;
350}
351
352int it85xx_shutdown(void)
353{
354 msg_pdbg("%s():%d\n", __func__, __LINE__);
355 it85xx_exit_scratch_rom();
356 return 0;
357}
358
359/* According to ITE 8502 document, the procedure to follow mode is following:
360 * 1. write 0x00 to LPC/FWH address 0xffff_fexxh (drive CE# high)
361 * 2. write data to LPC/FWH address 0xffff_fdxxh (drive CE# low and MOSI
362 * with data)
363 * 3. read date from LPC/FWH address 0xffff_fdxxh (drive CE# low and get
364 * data from MISO)
365 */
366int it85xx_spi_send_command(unsigned int writecnt, unsigned int readcnt,
367 const unsigned char *writearr, unsigned char *readarr)
368{
369 int i;
370
371 it85xx_enter_scratch_rom();
372 /* exit scratch rom ONLY when programmer shuts down. Otherwise, the
373 * temporary flash state may halt EC. */
374
375#ifdef LPC_IO
376 INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff);
377 INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/
378 INDIRECT_A1(shm_io_base, (((unsigned long int)ce_low) >> 8) & 0xff);
379#endif
380#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000381 mmio_writeb(0, ce_high);
Donald Huang44ebb042011-02-22 17:16:34 +0000382#endif
383 for (i = 0; i < writecnt; ++i) {
384#ifdef LPC_IO
385 INDIRECT_WRITE(shm_io_base, writearr[i]);
386#endif
387#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000388 mmio_writeb(writearr[i], ce_low);
Donald Huang44ebb042011-02-22 17:16:34 +0000389#endif
390 }
391 for (i = 0; i < readcnt; ++i) {
392#ifdef LPC_IO
393 readarr[i] = INDIRECT_READ(shm_io_base);
394#endif
395#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000396 readarr[i] = mmio_readb(ce_low);
Donald Huang44ebb042011-02-22 17:16:34 +0000397#endif
398 }
David Hendricks4e748392011-02-28 23:58:15 +0000399#ifdef LPC_IO
400 INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff);
401 INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/
402#endif
403#ifdef LPC_MEMORY
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000404 mmio_writeb(0, ce_high);
David Hendricks4e748392011-02-28 23:58:15 +0000405#endif
406
Donald Huang44ebb042011-02-22 17:16:34 +0000407 return 0;
408}
409
Carl-Daniel Hailfinger7f517a72011-03-08 00:23:49 +0000410int it85_spi_read(struct flashchip *flash, uint8_t * buf, int start, int len)
411{
412 return spi_read_chunked(flash, buf, start, len, 64);
413}
414
415int it85_spi_write_256(struct flashchip *flash, uint8_t * buf, int start, int len)
416{
417 return spi_write_chunked(flash, buf, start, len, 64);
418}
419
Donald Huang44ebb042011-02-22 17:16:34 +0000420#endif