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