Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 1 | /* |
| 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 Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include "flash.h" |
| 33 | #include "chipdrivers.h" |
| 34 | #include "spi.h" |
| 35 | #include "programmer.h" |
| 36 | |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 37 | #define MAX_TIMEOUT 100000 |
| 38 | #define MAX_TRY 5 |
| 39 | |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 40 | /* Constants for I/O ports */ |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 41 | #define ITE_SUPERIO_PORT1 0x2e |
| 42 | #define ITE_SUPERIO_PORT2 0x4e |
| 43 | |
| 44 | /* Legacy I/O */ |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 45 | #define LEGACY_KBC_PORT_DATA 0x60 |
| 46 | #define LEGACY_KBC_PORT_CMD 0x64 |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 47 | |
| 48 | /* Constants for Logical Device registers */ |
| 49 | #define LDNSEL 0x07 |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 50 | |
| 51 | /* These are standard Super I/O 16-bit base address registers */ |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 52 | #define SHM_IO_BAR0 0x60 /* big-endian, this is high bits */ |
| 53 | #define SHM_IO_BAR1 0x61 |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 54 | |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 55 | /* 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 Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 60 | #define KB_IBF (1 << 1) /* Input Buffer Full */ |
| 61 | #define KB_OBF (1 << 0) /* Output Buffer Full */ |
| 62 | |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 63 | /* 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 |
| 81 | unsigned int shm_io_base; |
| 82 | #endif |
| 83 | unsigned char *ce_high, *ce_low; |
| 84 | static int it85xx_scratch_rom_reenter = 0; |
| 85 | |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 86 | /* This function will poll the keyboard status register until either |
| 87 | * an expected value shows up, or |
| 88 | * timeout reaches. |
| 89 | * |
| 90 | * Returns: 0 -- the expected value has shown. |
| 91 | * 1 -- timeout reached. |
| 92 | */ |
| 93 | static int wait_for( |
| 94 | const unsigned int mask, |
| 95 | const unsigned int expected_value, |
| 96 | const int timeout, /* in usec */ |
| 97 | const char* error_message, |
| 98 | const char* function_name, |
| 99 | const int lineno |
| 100 | ) { |
| 101 | int time_passed; |
| 102 | |
| 103 | for (time_passed = 0;; ++time_passed) { |
| 104 | if ((INB(LEGACY_KBC_PORT_CMD) & mask) == expected_value) |
| 105 | return 0; |
| 106 | if (time_passed >= timeout) |
| 107 | break; |
| 108 | programmer_delay(1); |
| 109 | } |
| 110 | if (error_message) |
| 111 | msg_perr("%s():%d %s", function_name, lineno, error_message); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | /* IT8502 employs a scratch ram when flash is being updated. Call the following |
| 116 | * two functions before/after flash erase/program. */ |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 117 | void it85xx_enter_scratch_rom() |
| 118 | { |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 119 | int ret; |
| 120 | int tries; |
| 121 | |
| 122 | msg_pdbg("%s():%d was called ...\n", __FUNCTION__, __LINE__); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 123 | if (it85xx_scratch_rom_reenter > 0) return; |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 124 | |
| 125 | #if 0 |
| 126 | /* FIXME: this a workaround for the bug that SMBus signal would |
| 127 | * interfere the EC firmware update. Should be removed if |
| 128 | * we find out the root cause. */ |
| 129 | ret = system("stop powerd >&2"); |
| 130 | if (ret) { |
| 131 | msg_perr("Cannot stop powerd.\n"); |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | for (tries = 0; tries < MAX_TRY; ++tries) { |
| 136 | /* Wait until IBF (input buffer) is not full. */ |
| 137 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 138 | "* timeout at waiting for IBF==0.\n", |
| 139 | __FUNCTION__, __LINE__)) |
| 140 | continue; |
| 141 | |
| 142 | /* Copy EC firmware to SRAM. */ |
| 143 | OUTB(0xb4, LEGACY_KBC_PORT_CMD); |
| 144 | |
| 145 | /* Confirm EC has taken away the command. */ |
| 146 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 147 | "* timeout at taking command.\n", |
| 148 | __FUNCTION__, __LINE__)) |
| 149 | continue; |
| 150 | |
| 151 | /* Waiting for OBF (output buffer) has data. |
| 152 | * Note sometimes the replied command might be stolen by kernel |
| 153 | * ISR so that it is okay as long as the command is 0xFA. */ |
| 154 | if (wait_for(KB_OBF, KB_OBF, MAX_TIMEOUT, NULL, NULL, 0)) |
| 155 | msg_pdbg("%s():%d * timeout at waiting for OBF.\n", |
| 156 | __FUNCTION__, __LINE__); |
| 157 | if ((ret = INB(LEGACY_KBC_PORT_DATA)) == 0xFA) { |
| 158 | break; |
| 159 | } else { |
| 160 | msg_perr("%s():%d * not run on SRAM ret=%d\n", |
| 161 | __FUNCTION__, __LINE__, ret); |
| 162 | continue; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (tries < MAX_TRY) { |
| 167 | /* EC already runs on SRAM */ |
| 168 | it85xx_scratch_rom_reenter++; |
| 169 | msg_pdbg("%s():%d * SUCCESS.\n", __FUNCTION__, __LINE__); |
| 170 | } else { |
| 171 | msg_perr("%s():%d * Max try reached.\n", |
| 172 | __FUNCTION__, __LINE__); |
| 173 | } |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void it85xx_exit_scratch_rom() |
| 177 | { |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 178 | #if 0 |
| 179 | int ret; |
| 180 | #endif |
| 181 | int tries; |
| 182 | |
| 183 | msg_pdbg("%s():%d was called ...\n", __FUNCTION__, __LINE__); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 184 | if (it85xx_scratch_rom_reenter <= 0) return; |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 185 | |
| 186 | for (tries = 0; tries < MAX_TRY; ++tries) { |
| 187 | /* Wait until IBF (input buffer) is not full. */ |
| 188 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 189 | "* timeout at waiting for IBF==0.\n", |
| 190 | __FUNCTION__, __LINE__)) |
| 191 | continue; |
| 192 | |
| 193 | /* Exit SRAM. Run on flash. */ |
| 194 | OUTB(0xFE, LEGACY_KBC_PORT_CMD); |
| 195 | |
| 196 | /* Confirm EC has taken away the command. */ |
| 197 | if (wait_for(KB_IBF, 0, MAX_TIMEOUT, |
| 198 | "* timeout at taking command.\n", |
| 199 | __FUNCTION__, __LINE__)) { |
| 200 | /* We cannot ensure if EC has exited update mode. |
| 201 | * If EC is in normal mode already, a further 0xFE |
| 202 | * command will reboot system. So, exit loop here. */ |
| 203 | tries = MAX_TRY; |
| 204 | break; |
| 205 | } |
| 206 | |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | if (tries < MAX_TRY) { |
| 211 | it85xx_scratch_rom_reenter = 0; |
| 212 | msg_pdbg("%s():%d * SUCCESS.\n", __FUNCTION__, __LINE__); |
| 213 | } else { |
| 214 | msg_perr("%s():%d * Max try reached.\n", |
| 215 | __FUNCTION__, __LINE__); |
| 216 | } |
| 217 | |
| 218 | #if 0 |
| 219 | /* FIXME: this a workaround for the bug that SMBus signal would |
| 220 | * interfere the EC firmware update. Should be removed if |
| 221 | * we find out the root cause. */ |
| 222 | ret = system("start powerd >&2"); |
| 223 | if (ret) { |
| 224 | msg_perr("Cannot start powerd again.\n"); |
| 225 | } |
| 226 | #endif |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 227 | } |
| 228 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 229 | static int it85xx_shutdown(void *data) |
| 230 | { |
| 231 | msg_pdbg("%s():%d\n", __func__, __LINE__); |
| 232 | it85xx_exit_scratch_rom(); |
| 233 | |
| 234 | return 0; /* FIXME: Should probably return something meaningful */ |
| 235 | } |
| 236 | |
Carl-Daniel Hailfinger | bfecef6 | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 237 | static int it85xx_spi_common_init(struct superio s) |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 238 | { |
| 239 | chipaddr base; |
| 240 | |
| 241 | msg_pdbg("%s():%d superio.vendor=0x%02x\n", __func__, __LINE__, |
Carl-Daniel Hailfinger | bfecef6 | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 242 | s.vendor); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 243 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 244 | if (register_shutdown(it85xx_shutdown, NULL)) |
| 245 | return 1; |
| 246 | |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 247 | #ifdef LPC_IO |
| 248 | /* Get LPCPNP of SHM. That's big-endian */ |
Carl-Daniel Hailfinger | bfecef6 | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 249 | sio_write(s.port, LDNSEL, 0x0F); /* Set LDN to SHM (0x0F) */ |
| 250 | shm_io_base = (sio_read(s.port, SHM_IO_BAR0) << 8) + |
| 251 | sio_read(s.port, SHM_IO_BAR1); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 252 | msg_pdbg("%s():%d shm_io_base=0x%04x\n", __func__, __LINE__, |
| 253 | shm_io_base); |
| 254 | |
| 255 | /* These pointers are not used directly. They will be send to EC's |
| 256 | * register for indirect access. */ |
| 257 | base = 0xFFFFF000; |
| 258 | ce_high = ((unsigned char*)base) + 0xE00; /* 0xFFFFFE00 */ |
| 259 | ce_low = ((unsigned char*)base) + 0xD00; /* 0xFFFFFD00 */ |
| 260 | |
| 261 | /* pre-set indirect-access registers since in most of cases they are |
| 262 | * 0xFFFFxx00. */ |
| 263 | INDIRECT_A0(shm_io_base, base & 0xFF); |
| 264 | INDIRECT_A2(shm_io_base, (base >> 16) & 0xFF); |
| 265 | INDIRECT_A3(shm_io_base, (base >> 24)); |
| 266 | #endif |
| 267 | #ifdef LPC_MEMORY |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 268 | base = (chipaddr)programmer_map_flash_region("it85 communication", |
| 269 | 0xFFFFF000, 0x1000); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 270 | msg_pdbg("%s():%d base=0x%08x\n", __func__, __LINE__, |
| 271 | (unsigned int)base); |
| 272 | ce_high = (unsigned char*)(base + 0xE00); /* 0xFFFFFE00 */ |
| 273 | ce_low = (unsigned char*)(base + 0xD00); /* 0xFFFFFD00 */ |
| 274 | #endif |
| 275 | |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 279 | static int it85xx_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 280 | const unsigned char *writearr, unsigned char *readarr); |
| 281 | |
| 282 | static const struct spi_programmer spi_programmer_it85xx = { |
| 283 | .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, |
| 290 | }; |
| 291 | |
Carl-Daniel Hailfinger | bfecef6 | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 292 | int it85xx_spi_init(struct superio s) |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 293 | { |
| 294 | int ret; |
| 295 | |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame^] | 296 | if (!(buses_supported & BUS_FWH)) { |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 297 | msg_pdbg("%s():%d buses not support FWH\n", __func__, __LINE__); |
| 298 | return 1; |
| 299 | } |
Carl-Daniel Hailfinger | bfecef6 | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 300 | ret = it85xx_spi_common_init(s); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 301 | msg_pdbg("FWH: %s():%d ret=%d\n", __func__, __LINE__, ret); |
| 302 | if (!ret) { |
| 303 | msg_pdbg("%s():%d buses_supported=0x%x\n", __func__, __LINE__, |
| 304 | buses_supported); |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame^] | 305 | if (buses_supported & BUS_FWH) |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 306 | msg_pdbg("Overriding chipset SPI with IT85 FWH|SPI.\n"); |
Carl-Daniel Hailfinger | bfecef6 | 2011-04-27 14:34:08 +0000 | [diff] [blame] | 307 | /* Really leave FWH enabled? */ |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 308 | /* Set this as spi controller. */ |
| 309 | register_spi_programmer(&spi_programmer_it85xx); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 310 | } |
| 311 | return ret; |
| 312 | } |
| 313 | |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 314 | /* According to ITE 8502 document, the procedure to follow mode is following: |
| 315 | * 1. write 0x00 to LPC/FWH address 0xffff_fexxh (drive CE# high) |
| 316 | * 2. write data to LPC/FWH address 0xffff_fdxxh (drive CE# low and MOSI |
| 317 | * with data) |
| 318 | * 3. read date from LPC/FWH address 0xffff_fdxxh (drive CE# low and get |
| 319 | * data from MISO) |
| 320 | */ |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 321 | static int it85xx_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 322 | const unsigned char *writearr, unsigned char *readarr) |
| 323 | { |
| 324 | int i; |
| 325 | |
| 326 | it85xx_enter_scratch_rom(); |
| 327 | /* exit scratch rom ONLY when programmer shuts down. Otherwise, the |
| 328 | * temporary flash state may halt EC. */ |
| 329 | |
| 330 | #ifdef LPC_IO |
| 331 | INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff); |
| 332 | INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/ |
| 333 | INDIRECT_A1(shm_io_base, (((unsigned long int)ce_low) >> 8) & 0xff); |
| 334 | #endif |
| 335 | #ifdef LPC_MEMORY |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 336 | mmio_writeb(0, ce_high); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 337 | #endif |
| 338 | for (i = 0; i < writecnt; ++i) { |
| 339 | #ifdef LPC_IO |
| 340 | INDIRECT_WRITE(shm_io_base, writearr[i]); |
| 341 | #endif |
| 342 | #ifdef LPC_MEMORY |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 343 | mmio_writeb(writearr[i], ce_low); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 344 | #endif |
| 345 | } |
| 346 | for (i = 0; i < readcnt; ++i) { |
| 347 | #ifdef LPC_IO |
| 348 | readarr[i] = INDIRECT_READ(shm_io_base); |
| 349 | #endif |
| 350 | #ifdef LPC_MEMORY |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 351 | readarr[i] = mmio_readb(ce_low); |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 352 | #endif |
| 353 | } |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 354 | #ifdef LPC_IO |
| 355 | INDIRECT_A1(shm_io_base, (((unsigned long int)ce_high) >> 8) & 0xff); |
| 356 | INDIRECT_WRITE(shm_io_base, 0xFF); /* Write anything to this address.*/ |
| 357 | #endif |
| 358 | #ifdef LPC_MEMORY |
Carl-Daniel Hailfinger | 7f517a7 | 2011-03-08 00:23:49 +0000 | [diff] [blame] | 359 | mmio_writeb(0, ce_high); |
David Hendricks | 4e74839 | 2011-02-28 23:58:15 +0000 | [diff] [blame] | 360 | #endif |
| 361 | |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Donald Huang | 44ebb04 | 2011-02-22 17:16:34 +0000 | [diff] [blame] | 365 | #endif |