Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2012 Google Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * |
| 13 | * Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * |
| 17 | * Neither the name of Google or the names of contributors or |
| 18 | * licensors may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | * |
| 21 | * This software is provided "AS IS," without a warranty of any kind. |
| 22 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 23 | * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 24 | * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. |
| 25 | * GOOGLE INC AND ITS LICENSORS SHALL NOT BE LIABLE |
| 26 | * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING |
| 27 | * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL |
| 28 | * GOOGLE OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, |
| 29 | * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR |
| 30 | * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF |
| 31 | * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, |
| 32 | * EVEN IF GOOGLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 33 | */ |
| 34 | |
| 35 | #if defined(__i386__) || defined(__x86_64__) |
| 36 | #include <inttypes.h> |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame^] | 37 | #include <stdlib.h> |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <unistd.h> |
Shawn Nematbakhsh | b903dfd | 2012-07-24 15:27:00 -0700 | [diff] [blame] | 40 | #include <sys/time.h> |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 41 | |
| 42 | #include "chipdrivers.h" |
| 43 | #include "flash.h" |
| 44 | #include "programmer.h" |
| 45 | #include "spi.h" |
| 46 | |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 47 | /* Supported ENE ECs, ENE_LAST should always be LAST member */ |
| 48 | enum ene_chip_id { |
| 49 | ENE_KB932 = 0, |
| 50 | ENE_KB94X, |
| 51 | ENE_LAST |
| 52 | }; |
| 53 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 54 | /* EC state */ |
| 55 | enum ene_ec_state { |
| 56 | EC_STATE_NORMAL, |
| 57 | EC_STATE_IDLE, |
| 58 | EC_STATE_RESET, |
| 59 | EC_STATE_UNKNOWN |
| 60 | }; |
| 61 | |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 62 | /* chip-specific parameters */ |
| 63 | typedef struct { |
| 64 | enum ene_chip_id chip_id; |
| 65 | uint8_t hwver; |
| 66 | uint8_t ediid; |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 67 | uint32_t port_bios; |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 68 | uint32_t port_ec_command; |
| 69 | uint32_t port_ec_data; |
| 70 | uint8_t ec_reset_cmd; |
| 71 | uint8_t ec_reset_data; |
| 72 | uint8_t ec_restart_cmd; |
| 73 | uint8_t ec_restart_data; |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 74 | uint8_t ec_pause_cmd; |
| 75 | uint8_t ec_pause_data; |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 76 | uint16_t ec_status_buf; |
| 77 | uint8_t ec_is_stopping; |
| 78 | uint8_t ec_is_running; |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 79 | uint8_t ec_is_pausing; |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 80 | uint32_t port_io_base; |
| 81 | } ene_chip; |
| 82 | |
| 83 | /* table of supported chips + parameters */ |
| 84 | static ene_chip ene_chips[] = { |
| 85 | { ENE_KB932, /* chip_id */ |
| 86 | 0xa2, 0x02, /* hwver + ediid */ |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 87 | 0x66, /* port_bios */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 88 | 0x6c, 0x68, /* port_ec_{command,data} */ |
| 89 | 0x59, 0xf2, /* ec_reset_{cmd,data} */ |
| 90 | 0x59, 0xf9, /* ec_restart_{cmd,data} */ |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 91 | 0x59, 0xf1, /* ec_pause_{cmd,data} */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 92 | 0xf554, /* ec_status_buf */ |
| 93 | 0xa5, 0x00, /* ec_is_{stopping,running} masks */ |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 94 | 0x33, /* ec_is_pausing mask */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 95 | 0xfd60 }, /* port_io_base */ |
| 96 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 97 | { ENE_KB94X, /* chip_id */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 98 | 0xa3, 0x05, /* hwver + ediid */ |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 99 | 0x66, /* port_bios */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 100 | 0x66, 0x68, /* port_ec_{command,data} */ |
| 101 | 0x7d, 0x10, /* ec_reset_{cmd,data} */ |
| 102 | 0x7f, 0x10, /* ec_restart_{cmd,data} */ |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 103 | 0x7e, 0x10, /* ec_pause_{cmd,data} */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 104 | 0xf710, /* ec_status_buf */ |
| 105 | 0x02, 0x00, /* ec_is_{stopping,running} masks */ |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 106 | 0x01, /* ec_is_pausing mask */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 107 | 0x0380 }, /* port_io_base */ |
| 108 | }; |
| 109 | |
| 110 | /* pointer to table entry of identified chip */ |
| 111 | static ene_chip *found_chip; |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 112 | /* current ec state */ |
| 113 | static enum ene_ec_state ec_state = EC_STATE_NORMAL; |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 114 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 115 | #define REG_EC_HWVER 0xff00 |
| 116 | #define REG_EC_FWVER 0xff01 |
| 117 | #define REG_EC_EDIID 0xff24 |
| 118 | #define REG_8051_CTRL 0xff14 |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 119 | #define REG_EC_EXTCMD 0xff10 |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 120 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 121 | #define CPU_RESET 1 |
| 122 | |
| 123 | /* Hwardware registers */ |
| 124 | #define REG_SPI_DATA 0xfeab |
| 125 | #define REG_SPI_COMMAND 0xfeac |
| 126 | #define REG_SPI_CONFIG 0xfead |
| 127 | #define CFG_CSn_FORCE_LOW (1 << 4) |
| 128 | #define CFG_COMMAND_WRITE_ENABLE (1 << 3) |
| 129 | #define CFG_STATUS (1 << 1) |
| 130 | #define CFG_ENABLE_BUSY_STATUS_CHECK (1 << 0) |
| 131 | |
| 132 | /* Timeout */ |
| 133 | #define EC_COMMAND_TIMEOUT 4 |
| 134 | #define EC_RESTART_TIMEOUT 10 |
| 135 | #define ENE_SPI_DELAY_CYCLE 4 |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 136 | #define EC_PAUSE_TIMEOUT 12 |
Shawn Nematbakhsh | 9c0cd8d | 2013-05-15 17:07:13 -0700 | [diff] [blame] | 137 | #define EC_RESET_TRIES 3 |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 138 | |
| 139 | #define ENE_KB94X_PAUSE_WAKEUP_PORT 0x64 |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 140 | |
David Hendricks | 835b310 | 2015-11-05 20:33:00 -0800 | [diff] [blame] | 141 | #define MASK_INPUT_BUFFER_FULL 2 |
| 142 | #define MASK_OUTPUT_BUFFER_FULL 1 |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 143 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 144 | const int port_ene_bank = 1; |
| 145 | const int port_ene_offset = 2; |
| 146 | const int port_ene_data = 3; |
| 147 | |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 148 | static struct timeval pause_begin, pause_now; |
| 149 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 150 | static void ec_command(uint8_t cmd, uint8_t data) |
| 151 | { |
| 152 | struct timeval begin, now; |
| 153 | |
| 154 | /* Spin wait for EC input buffer empty */ |
| 155 | gettimeofday(&begin, NULL); |
David Hendricks | 835b310 | 2015-11-05 20:33:00 -0800 | [diff] [blame] | 156 | while (INB(found_chip->port_ec_command) & MASK_INPUT_BUFFER_FULL) { |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 157 | gettimeofday(&now, NULL); |
| 158 | if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) { |
| 159 | msg_pdbg("%s: buf not empty\n", __func__); |
| 160 | return; |
| 161 | } |
| 162 | } |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 163 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 164 | /* Write command */ |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 165 | OUTB(cmd, found_chip->port_ec_command); |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 166 | |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 167 | if (found_chip->chip_id == ENE_KB932) { |
| 168 | /* Spin wait for EC input buffer empty */ |
| 169 | gettimeofday(&begin, NULL); |
| 170 | while (INB(found_chip->port_ec_command) & |
David Hendricks | 835b310 | 2015-11-05 20:33:00 -0800 | [diff] [blame] | 171 | MASK_INPUT_BUFFER_FULL) { |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 172 | gettimeofday(&now, NULL); |
| 173 | if ((now.tv_sec - begin.tv_sec) >= |
| 174 | EC_COMMAND_TIMEOUT) { |
| 175 | msg_pdbg("%s: buf not empty\n", __func__); |
| 176 | return; |
| 177 | } |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 178 | } |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 179 | /* Write data */ |
| 180 | OUTB(data, found_chip->port_ec_data); |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 181 | } |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static uint8_t ene_read(uint16_t addr) |
| 185 | { |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 186 | uint8_t bank; |
| 187 | uint8_t offset; |
| 188 | uint8_t data; |
| 189 | uint32_t port_io_base; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 190 | |
| 191 | bank = addr >> 8; |
| 192 | offset = addr & 0xff; |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 193 | port_io_base = found_chip->port_io_base; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 194 | |
| 195 | OUTB(bank, port_io_base + port_ene_bank); |
| 196 | OUTB(offset, port_io_base + port_ene_offset); |
| 197 | data = INB(port_io_base + port_ene_data); |
| 198 | |
| 199 | return data; |
| 200 | } |
| 201 | |
| 202 | static void ene_write(uint16_t addr, uint8_t data) |
| 203 | { |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 204 | uint8_t bank; |
| 205 | uint8_t offset; |
| 206 | uint32_t port_io_base; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 207 | |
| 208 | bank = addr >> 8; |
| 209 | offset = addr & 0xff; |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 210 | port_io_base = found_chip->port_io_base; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 211 | |
| 212 | OUTB(bank, port_io_base + port_ene_bank); |
| 213 | OUTB(offset, port_io_base + port_ene_offset); |
| 214 | |
| 215 | OUTB(data, port_io_base + port_ene_data); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * wait_cycles, wait for n LPC bus clock cycles |
| 220 | * |
| 221 | * @param n: number of LPC cycles to wait |
| 222 | * @return void |
| 223 | */ |
| 224 | void wait_cycles(int n) |
| 225 | { |
| 226 | while (n--) |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 227 | INB(found_chip->port_io_base + port_ene_bank); |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 228 | } |
| 229 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 230 | static int is_spicmd_write(uint8_t cmd) |
| 231 | { |
| 232 | switch (cmd) { |
Rong Chang | 7797e4c | 2012-08-19 00:40:49 +0800 | [diff] [blame] | 233 | case JEDEC_WREN: |
| 234 | /* Chip Write Enable */ |
| 235 | case JEDEC_EWSR: |
| 236 | /* Write Status Enable */ |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 237 | case JEDEC_CE_60: |
| 238 | /* Chip Erase 0x60 */ |
| 239 | case JEDEC_CE_C7: |
| 240 | /* Chip Erase 0xc7 */ |
| 241 | case JEDEC_BE_52: |
| 242 | /* Block Erase 0x52 */ |
| 243 | case JEDEC_BE_D8: |
| 244 | /* Block Erase 0xd8 */ |
| 245 | case JEDEC_BE_D7: |
| 246 | /* Block Erase 0xd7 */ |
| 247 | case JEDEC_SE: |
| 248 | /* Sector Erase */ |
| 249 | case JEDEC_BYTE_PROGRAM: |
| 250 | /* Write memory byte */ |
| 251 | case JEDEC_AAI_WORD_PROGRAM: |
| 252 | /* Write AAI word */ |
| 253 | return 1; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 258 | static void ene_spi_start(void) |
| 259 | { |
| 260 | int cfg; |
| 261 | |
| 262 | cfg = ene_read(REG_SPI_CONFIG); |
| 263 | cfg |= CFG_CSn_FORCE_LOW; |
| 264 | cfg |= CFG_COMMAND_WRITE_ENABLE; |
| 265 | ene_write(REG_SPI_CONFIG, cfg); |
| 266 | |
| 267 | wait_cycles(ENE_SPI_DELAY_CYCLE); |
| 268 | } |
| 269 | |
| 270 | static void ene_spi_end(void) |
| 271 | { |
| 272 | int cfg; |
| 273 | |
| 274 | cfg = ene_read(REG_SPI_CONFIG); |
| 275 | cfg &= ~CFG_CSn_FORCE_LOW; |
| 276 | cfg |= CFG_COMMAND_WRITE_ENABLE; |
| 277 | ene_write(REG_SPI_CONFIG, cfg); |
| 278 | |
| 279 | wait_cycles(ENE_SPI_DELAY_CYCLE); |
| 280 | } |
| 281 | |
| 282 | static int ene_spi_wait(void) |
| 283 | { |
| 284 | struct timeval begin, now; |
| 285 | |
| 286 | gettimeofday(&begin, NULL); |
| 287 | while(ene_read(REG_SPI_CONFIG) & CFG_STATUS) { |
| 288 | gettimeofday(&now, NULL); |
| 289 | if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) { |
| 290 | msg_pdbg("%s: spi busy\n", __func__); |
| 291 | return 1; |
| 292 | } |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 297 | static int ene_pause_ec(void) |
| 298 | { |
| 299 | struct timeval begin, now; |
| 300 | |
| 301 | if (!found_chip->ec_pause_cmd) |
| 302 | return -1; |
| 303 | |
| 304 | /* EC prepare pause */ |
| 305 | ec_command(found_chip->ec_pause_cmd, found_chip->ec_pause_data); |
| 306 | |
| 307 | gettimeofday(&begin, NULL); |
| 308 | /* Spin wait for EC ready */ |
| 309 | while (ene_read(found_chip->ec_status_buf) != |
| 310 | found_chip->ec_is_pausing) { |
| 311 | gettimeofday(&now, NULL); |
| 312 | if ((now.tv_sec - begin.tv_sec) >= |
| 313 | EC_COMMAND_TIMEOUT) { |
| 314 | msg_pdbg("%s: unable to pause ec\n", __func__); |
| 315 | return -1; |
| 316 | } |
| 317 | } |
| 318 | |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 319 | |
| 320 | gettimeofday(&pause_begin, NULL); |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 321 | ec_state = EC_STATE_IDLE; |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int ene_resume_ec(void) |
| 326 | { |
| 327 | struct timeval begin, now; |
| 328 | |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 329 | |
| 330 | if (found_chip->chip_id == ENE_KB94X) |
| 331 | OUTB(0xff, ENE_KB94X_PAUSE_WAKEUP_PORT); |
| 332 | else |
| 333 | /* Trigger 8051 interrupt to resume */ |
| 334 | ene_write(REG_EC_EXTCMD, 0xff); |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 335 | |
| 336 | gettimeofday(&begin, NULL); |
| 337 | while (ene_read(found_chip->ec_status_buf) != |
| 338 | found_chip->ec_is_running) { |
| 339 | gettimeofday(&now, NULL); |
| 340 | if ((now.tv_sec - begin.tv_sec) >= |
| 341 | EC_COMMAND_TIMEOUT) { |
| 342 | msg_pdbg("%s: unable to resume ec\n", __func__); |
| 343 | return -1; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | ec_state = EC_STATE_NORMAL; |
| 348 | return 0; |
| 349 | } |
| 350 | |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 351 | static int ene_pause_timeout_check(void) |
| 352 | { |
| 353 | gettimeofday(&pause_now, NULL); |
| 354 | if ((pause_now.tv_sec - pause_begin.tv_sec) >= |
| 355 | EC_PAUSE_TIMEOUT) { |
| 356 | if(ene_resume_ec() == 0) |
| 357 | ene_pause_ec(); |
| 358 | |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 363 | static int ene_reset_ec(void) |
| 364 | { |
| 365 | uint8_t reg; |
| 366 | |
| 367 | struct timeval begin, now; |
| 368 | gettimeofday(&begin, NULL); |
| 369 | |
| 370 | /* EC prepare reset */ |
| 371 | ec_command(found_chip->ec_reset_cmd, found_chip->ec_reset_data); |
| 372 | |
| 373 | /* Spin wait for EC ready */ |
| 374 | while (ene_read(found_chip->ec_status_buf) != |
| 375 | found_chip->ec_is_stopping) { |
| 376 | gettimeofday(&now, NULL); |
| 377 | if ((now.tv_sec - begin.tv_sec) >= |
| 378 | EC_COMMAND_TIMEOUT) { |
| 379 | msg_pdbg("%s: unable to reset ec\n", __func__); |
| 380 | return -1; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* Wait 1 second */ |
| 385 | sleep(1); |
| 386 | |
| 387 | /* Reset 8051 */ |
| 388 | reg = ene_read(REG_8051_CTRL); |
| 389 | reg |= CPU_RESET; |
| 390 | ene_write(REG_8051_CTRL, reg); |
| 391 | |
| 392 | ec_state = EC_STATE_RESET; |
| 393 | return 0; |
| 394 | } |
| 395 | |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 396 | static int ene_enter_flash_mode(void) |
| 397 | { |
| 398 | if (ene_pause_ec()) |
| 399 | return ene_reset_ec(); |
| 400 | return 0; |
| 401 | } |
| 402 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 403 | static int ene_spi_send_command(const struct flashctx *flash, |
| 404 | unsigned int writecnt, |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 405 | unsigned int readcnt, |
| 406 | const unsigned char *writearr, |
| 407 | unsigned char *readarr) |
| 408 | { |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 409 | int i; |
Shawn Nematbakhsh | 9c0cd8d | 2013-05-15 17:07:13 -0700 | [diff] [blame] | 410 | int tries = EC_RESET_TRIES; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 411 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 412 | if (ec_state == EC_STATE_IDLE && is_spicmd_write(writearr[0])) { |
Shawn Nematbakhsh | 9c0cd8d | 2013-05-15 17:07:13 -0700 | [diff] [blame] | 413 | do { |
| 414 | /* Enter reset mode if we need to write/erase */ |
| 415 | if (ene_resume_ec()) |
| 416 | continue; |
| 417 | |
| 418 | if (!ene_reset_ec()) |
| 419 | break; |
| 420 | } while (--tries > 0); |
| 421 | |
| 422 | if (!tries) { |
| 423 | msg_perr("%s: EC failed reset, skipping write\n", |
| 424 | __func__); |
| 425 | ec_state = EC_STATE_IDLE; |
| 426 | return 1; |
| 427 | } |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 428 | } |
agnescheng | 433ff58 | 2012-09-26 12:33:35 +0800 | [diff] [blame] | 429 | else if(found_chip->chip_id == ENE_KB94X && ec_state == EC_STATE_IDLE) |
| 430 | ene_pause_timeout_check(); |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 431 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 432 | ene_spi_start(); |
| 433 | |
| 434 | for (i = 0; i < writecnt; i++) { |
| 435 | ene_write(REG_SPI_COMMAND, writearr[i]); |
| 436 | if (ene_spi_wait()) { |
| 437 | msg_pdbg("%s: write count %d\n", __func__, i); |
| 438 | return 1; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | for (i = 0; i < readcnt; i++) { |
| 443 | /* Push data by clock the serial bus */ |
| 444 | ene_write(REG_SPI_COMMAND, 0); |
| 445 | if (ene_spi_wait()) { |
| 446 | msg_pdbg("%s: read count %d\n", __func__, i); |
| 447 | return 1; |
| 448 | } |
| 449 | readarr[i] = ene_read(REG_SPI_DATA); |
| 450 | if (ene_spi_wait()) { |
| 451 | msg_pdbg("%s: read count %d\n", __func__, i); |
| 452 | return 1; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | ene_spi_end(); |
| 457 | return 0; |
| 458 | } |
| 459 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 460 | static int ene_leave_flash_mode(void *data) |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 461 | { |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 462 | int rv = 0; |
Shawn Nematbakhsh | b903dfd | 2012-07-24 15:27:00 -0700 | [diff] [blame] | 463 | uint8_t reg; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 464 | struct timeval begin, now; |
| 465 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 466 | if (ec_state == EC_STATE_RESET) { |
| 467 | reg = ene_read(REG_8051_CTRL); |
| 468 | reg &= ~CPU_RESET; |
| 469 | ene_write(REG_8051_CTRL, reg); |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 470 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 471 | gettimeofday(&begin, NULL); |
| 472 | /* EC restart */ |
| 473 | while (ene_read(found_chip->ec_status_buf) != |
| 474 | found_chip->ec_is_running) { |
| 475 | gettimeofday(&now, NULL); |
| 476 | if ((now.tv_sec - begin.tv_sec) >= |
| 477 | EC_RESTART_TIMEOUT) { |
| 478 | msg_pdbg("%s: ec restart busy\n", __func__); |
| 479 | rv = 1; |
| 480 | goto exit; |
| 481 | } |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 482 | } |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 483 | msg_pdbg("%s: send ec restart\n", __func__); |
| 484 | ec_command(found_chip->ec_restart_cmd, |
| 485 | found_chip->ec_restart_data); |
| 486 | |
| 487 | ec_state = EC_STATE_NORMAL; |
| 488 | rv = 0; |
| 489 | goto exit; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 490 | } |
| 491 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 492 | rv = ene_resume_ec(); |
Rong Chang | ea1ec81 | 2012-07-21 11:41:32 +0800 | [diff] [blame] | 493 | |
Rong Chang | d8889e5 | 2012-07-27 21:42:25 +0800 | [diff] [blame] | 494 | exit: |
| 495 | /* |
| 496 | * Trigger ec interrupt after pause/reset by sending 0x80 |
| 497 | * to bios command port. |
| 498 | */ |
| 499 | OUTB(0x80, found_chip->port_bios); |
| 500 | return rv; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static const struct spi_programmer spi_programmer_ene = { |
| 504 | .type = SPI_CONTROLLER_ENE, |
| 505 | .max_data_read = 256, |
| 506 | .max_data_write = 256, |
| 507 | .command = ene_spi_send_command, |
| 508 | .multicommand = default_spi_send_multicommand, |
| 509 | .read = default_spi_read, |
| 510 | .write_256 = default_spi_write_256, |
| 511 | }; |
| 512 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 513 | int ene_probe_spi_flash(const char *name) |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 514 | { |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 515 | uint8_t hwver, ediid, i; |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame^] | 516 | int ret = 0; |
| 517 | char *p = NULL; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 518 | |
David Hendricks | ba0827a | 2013-05-03 20:25:40 -0700 | [diff] [blame] | 519 | if (alias && alias->type != ALIAS_EC) |
| 520 | return 1; |
| 521 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 522 | msg_pdbg("%s\n", __func__); |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 523 | |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame^] | 524 | p = extract_programmer_param("type"); |
| 525 | if (p && strcmp(p, "ec")) { |
| 526 | msg_pdbg("ene_lpc only supports \"ec\" type devices\n"); |
| 527 | ret = 1; |
| 528 | goto ene_probe_spi_flash_exit; |
| 529 | } |
| 530 | |
Shawn Nematbakhsh | 36046f5 | 2012-07-26 09:42:36 -0700 | [diff] [blame] | 531 | for (i = 0; i < ENE_LAST; ++i) { |
| 532 | found_chip = &ene_chips[i]; |
| 533 | |
| 534 | hwver = ene_read(REG_EC_HWVER); |
| 535 | ediid = ene_read(REG_EC_EDIID); |
| 536 | |
| 537 | if(hwver == ene_chips[i].hwver && |
| 538 | ediid == ene_chips[i].ediid) { |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (i == ENE_LAST) { |
| 544 | msg_pdbg("ENE EC not found (probe failed)\n"); |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame^] | 545 | ret = 1; |
| 546 | goto ene_probe_spi_flash_exit; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* TODO: probe the EC stop protocol |
| 550 | * |
| 551 | * Compal - ec_command(0x41, 0xa1) returns 43 4f 4d 50 41 4c 9c |
| 552 | */ |
| 553 | |
| 554 | |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame^] | 555 | if (register_shutdown(ene_leave_flash_mode, NULL)) { |
| 556 | ret = 1; |
| 557 | goto ene_probe_spi_flash_exit; |
| 558 | } |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 559 | |
| 560 | ene_enter_flash_mode(); |
| 561 | |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 562 | buses_supported |= BUS_LPC; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 563 | register_spi_programmer(&spi_programmer_ene); |
| 564 | msg_pdbg("%s: successfully initialized ene\n", __func__); |
David Hendricks | 2b56e47 | 2016-10-21 19:05:21 -0700 | [diff] [blame^] | 565 | ene_probe_spi_flash_exit: |
| 566 | free(p); |
| 567 | return ret; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | #endif /* __i386__ || __x86_64__ */ |
| 571 | |