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> |
| 37 | #include <string.h> |
| 38 | #include <unistd.h> |
Shawn Nematbakhsh | b903dfd | 2012-07-24 15:27:00 -0700 | [diff] [blame^] | 39 | #include <sys/time.h> |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 40 | |
| 41 | #include "chipdrivers.h" |
| 42 | #include "flash.h" |
| 43 | #include "programmer.h" |
| 44 | #include "spi.h" |
| 45 | |
| 46 | #define REG_EC_HWVER 0xff00 |
| 47 | #define REG_EC_FWVER 0xff01 |
| 48 | #define REG_EC_EDIID 0xff24 |
| 49 | #define REG_8051_CTRL 0xff14 |
| 50 | |
| 51 | #define HWVER 0xa2 |
| 52 | #define EDIID 0x02 |
| 53 | #define CPU_RESET 1 |
| 54 | |
| 55 | /* Hwardware registers */ |
| 56 | #define REG_SPI_DATA 0xfeab |
| 57 | #define REG_SPI_COMMAND 0xfeac |
| 58 | #define REG_SPI_CONFIG 0xfead |
| 59 | #define CFG_CSn_FORCE_LOW (1 << 4) |
| 60 | #define CFG_COMMAND_WRITE_ENABLE (1 << 3) |
| 61 | #define CFG_STATUS (1 << 1) |
| 62 | #define CFG_ENABLE_BUSY_STATUS_CHECK (1 << 0) |
| 63 | |
| 64 | /* Timeout */ |
| 65 | #define EC_COMMAND_TIMEOUT 4 |
| 66 | #define EC_RESTART_TIMEOUT 10 |
| 67 | #define ENE_SPI_DELAY_CYCLE 4 |
| 68 | |
| 69 | /* Configurable ec command/status */ |
| 70 | static unsigned int port_ec_command = 0x6c; |
| 71 | static unsigned int port_ec_data = 0x68; |
| 72 | |
| 73 | static uint8_t ec_reset_cmd = 0x59; |
| 74 | static uint8_t ec_reset_data = 0xf2; |
| 75 | |
| 76 | static uint8_t ec_restart_cmd = 0x59; |
| 77 | static uint8_t ec_restart_data = 0xf9; |
| 78 | |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 79 | static const uint16_t ec_status_buf = 0xf554; |
| 80 | static const uint8_t ec_is_stopping = 0xa5; |
| 81 | static const uint8_t ec_is_running = 0; |
| 82 | |
| 83 | static const uint8_t mask_input_buffer_full = 2; |
| 84 | static const uint8_t mask_output_buffer_full = 1; |
| 85 | |
| 86 | static unsigned int port_io_base = 0xfd60; |
| 87 | const int port_ene_bank = 1; |
| 88 | const int port_ene_offset = 2; |
| 89 | const int port_ene_data = 3; |
| 90 | |
| 91 | static void ec_command(uint8_t cmd, uint8_t data) |
| 92 | { |
| 93 | struct timeval begin, now; |
| 94 | |
| 95 | /* Spin wait for EC input buffer empty */ |
| 96 | gettimeofday(&begin, NULL); |
| 97 | while (INB(port_ec_command) & mask_input_buffer_full) { |
| 98 | gettimeofday(&now, NULL); |
| 99 | if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) { |
| 100 | msg_pdbg("%s: buf not empty\n", __func__); |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | /* Write command */ |
| 105 | OUTB(cmd, port_ec_command); |
| 106 | |
| 107 | /* Spin wait for EC input buffer empty */ |
| 108 | gettimeofday(&begin, NULL); |
| 109 | while (INB(port_ec_command) & mask_input_buffer_full) { |
| 110 | gettimeofday(&now, NULL); |
| 111 | if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) { |
| 112 | msg_pdbg("%s: buf not empty\n", __func__); |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | /* Write data */ |
| 117 | OUTB(data, port_ec_data); |
| 118 | } |
| 119 | |
| 120 | static uint8_t ene_read(uint16_t addr) |
| 121 | { |
| 122 | uint8_t bank; |
| 123 | uint8_t offset; |
| 124 | uint8_t data; |
| 125 | |
| 126 | bank = addr >> 8; |
| 127 | offset = addr & 0xff; |
| 128 | |
| 129 | OUTB(bank, port_io_base + port_ene_bank); |
| 130 | OUTB(offset, port_io_base + port_ene_offset); |
| 131 | data = INB(port_io_base + port_ene_data); |
| 132 | |
| 133 | return data; |
| 134 | } |
| 135 | |
| 136 | static void ene_write(uint16_t addr, uint8_t data) |
| 137 | { |
| 138 | uint8_t bank; |
| 139 | uint8_t offset; |
| 140 | |
| 141 | bank = addr >> 8; |
| 142 | offset = addr & 0xff; |
| 143 | |
| 144 | OUTB(bank, port_io_base + port_ene_bank); |
| 145 | OUTB(offset, port_io_base + port_ene_offset); |
| 146 | |
| 147 | OUTB(data, port_io_base + port_ene_data); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * wait_cycles, wait for n LPC bus clock cycles |
| 152 | * |
| 153 | * @param n: number of LPC cycles to wait |
| 154 | * @return void |
| 155 | */ |
| 156 | void wait_cycles(int n) |
| 157 | { |
| 158 | while (n--) |
| 159 | INB(port_io_base + port_ene_bank); |
| 160 | } |
| 161 | |
| 162 | static void ene_spi_start(void) |
| 163 | { |
| 164 | int cfg; |
| 165 | |
| 166 | cfg = ene_read(REG_SPI_CONFIG); |
| 167 | cfg |= CFG_CSn_FORCE_LOW; |
| 168 | cfg |= CFG_COMMAND_WRITE_ENABLE; |
| 169 | ene_write(REG_SPI_CONFIG, cfg); |
| 170 | |
| 171 | wait_cycles(ENE_SPI_DELAY_CYCLE); |
| 172 | } |
| 173 | |
| 174 | static void ene_spi_end(void) |
| 175 | { |
| 176 | int cfg; |
| 177 | |
| 178 | cfg = ene_read(REG_SPI_CONFIG); |
| 179 | cfg &= ~CFG_CSn_FORCE_LOW; |
| 180 | cfg |= CFG_COMMAND_WRITE_ENABLE; |
| 181 | ene_write(REG_SPI_CONFIG, cfg); |
| 182 | |
| 183 | wait_cycles(ENE_SPI_DELAY_CYCLE); |
| 184 | } |
| 185 | |
| 186 | static int ene_spi_wait(void) |
| 187 | { |
| 188 | struct timeval begin, now; |
| 189 | |
| 190 | gettimeofday(&begin, NULL); |
| 191 | while(ene_read(REG_SPI_CONFIG) & CFG_STATUS) { |
| 192 | gettimeofday(&now, NULL); |
| 193 | if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) { |
| 194 | msg_pdbg("%s: spi busy\n", __func__); |
| 195 | return 1; |
| 196 | } |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int ene_spi_send_command(unsigned int writecnt, |
| 202 | unsigned int readcnt, |
| 203 | const unsigned char *writearr, |
| 204 | unsigned char *readarr) |
| 205 | { |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 206 | int i; |
| 207 | |
| 208 | ene_spi_start(); |
| 209 | |
| 210 | for (i = 0; i < writecnt; i++) { |
| 211 | ene_write(REG_SPI_COMMAND, writearr[i]); |
| 212 | if (ene_spi_wait()) { |
| 213 | msg_pdbg("%s: write count %d\n", __func__, i); |
| 214 | return 1; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | for (i = 0; i < readcnt; i++) { |
| 219 | /* Push data by clock the serial bus */ |
| 220 | ene_write(REG_SPI_COMMAND, 0); |
| 221 | if (ene_spi_wait()) { |
| 222 | msg_pdbg("%s: read count %d\n", __func__, i); |
| 223 | return 1; |
| 224 | } |
| 225 | readarr[i] = ene_read(REG_SPI_DATA); |
| 226 | if (ene_spi_wait()) { |
| 227 | msg_pdbg("%s: read count %d\n", __func__, i); |
| 228 | return 1; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | ene_spi_end(); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int ene_enter_flash_mode(void) |
| 237 | { |
Shawn Nematbakhsh | b903dfd | 2012-07-24 15:27:00 -0700 | [diff] [blame^] | 238 | uint8_t reg; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 239 | |
| 240 | struct timeval begin, now; |
| 241 | gettimeofday(&begin, NULL); |
| 242 | |
| 243 | /* EC prepare reset */ |
| 244 | ec_command(ec_reset_cmd, ec_reset_data); |
| 245 | |
| 246 | /* Spin wait for EC ready */ |
| 247 | while (ene_read(ec_status_buf) != ec_is_running) { |
| 248 | gettimeofday(&now, NULL); |
| 249 | if ((now.tv_sec - begin.tv_sec) >= EC_COMMAND_TIMEOUT) { |
| 250 | msg_pdbg("%s: ec reset busy\n", __func__); |
| 251 | return -1; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* Wait 1 second */ |
| 256 | sleep(1); |
| 257 | |
| 258 | /* Reset 8051 */ |
| 259 | reg = ene_read(REG_8051_CTRL); |
| 260 | reg |= CPU_RESET; |
| 261 | ene_write(REG_8051_CTRL, reg); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int ene_leave_flash_mode(void *data) |
| 267 | { |
Shawn Nematbakhsh | b903dfd | 2012-07-24 15:27:00 -0700 | [diff] [blame^] | 268 | uint8_t reg; |
Rong Chang | aaa1acf | 2012-06-21 19:21:18 +0800 | [diff] [blame] | 269 | struct timeval begin, now; |
| 270 | |
| 271 | reg = ene_read(REG_8051_CTRL); |
| 272 | reg &= ~CPU_RESET; |
| 273 | ene_write(REG_8051_CTRL, reg); |
| 274 | |
| 275 | gettimeofday(&begin, NULL); |
| 276 | /* EC restart */ |
| 277 | while (ene_read(ec_status_buf) != ec_is_running) { |
| 278 | gettimeofday(&now, NULL); |
| 279 | if ((now.tv_sec - begin.tv_sec) >= EC_RESTART_TIMEOUT) { |
| 280 | msg_pdbg("%s: ec restart busy\n", __func__); |
| 281 | return 1; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | msg_pdbg("%s: send ec restart\n", __func__); |
| 286 | ec_command(ec_restart_cmd, ec_restart_data); |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | static const struct spi_programmer spi_programmer_ene = { |
| 292 | .type = SPI_CONTROLLER_ENE, |
| 293 | .max_data_read = 256, |
| 294 | .max_data_write = 256, |
| 295 | .command = ene_spi_send_command, |
| 296 | .multicommand = default_spi_send_multicommand, |
| 297 | .read = default_spi_read, |
| 298 | .write_256 = default_spi_write_256, |
| 299 | }; |
| 300 | |
| 301 | int ene_probe_spi_flash(const char *name) |
| 302 | { |
| 303 | uint8_t hwver, ediid; |
| 304 | |
| 305 | msg_pdbg("%s\n", __func__); |
| 306 | hwver = ene_read(REG_EC_HWVER); |
| 307 | ediid = ene_read(REG_EC_EDIID); |
| 308 | |
| 309 | if (hwver != HWVER || ediid != EDIID) { |
| 310 | msg_pdbg("ENE EC not found (probe failed) : hwver %02x ediid %02x\n", |
| 311 | hwver, ediid); |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* TODO: probe the EC stop protocol |
| 316 | * |
| 317 | * Compal - ec_command(0x41, 0xa1) returns 43 4f 4d 50 41 4c 9c |
| 318 | */ |
| 319 | |
| 320 | |
| 321 | if (register_shutdown(ene_leave_flash_mode, NULL)) |
| 322 | return 1; |
| 323 | |
| 324 | ene_enter_flash_mode(); |
| 325 | |
| 326 | buses_supported |= BUS_LPC; |
| 327 | register_spi_programmer(&spi_programmer_ene); |
| 328 | msg_pdbg("%s: successfully initialized ene\n", __func__); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | #endif /* __i386__ || __x86_64__ */ |
| 333 | |