Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <unistd.h> |
| 9 | #include "flashchips.h" |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 10 | #include "fmap.h" |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 11 | #include "gec_lpc_commands.h" |
| 12 | #include "programmer.h" |
| 13 | #include "spi.h" |
| 14 | #include "writeprotect.h" |
| 15 | |
| 16 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 17 | /* 1 if we detect a GEC on system */ |
| 18 | static int detected = 0; |
| 19 | |
| 20 | /* 1 if we want the flashrom to call erase_and_write_flash() again. */ |
| 21 | static int need_2nd_pass = 0; |
| 22 | |
| 23 | /* The range of each firmware copy from the image file to update. |
| 24 | * But re-define the .flags as the valid flag to indicate the firmware is |
| 25 | * new or not (if flags = 1). |
| 26 | */ |
| 27 | static struct fmap_area fwcopy[4]; // [0] is not used. |
| 28 | |
| 29 | /* The names of enum lpc_current_image to match in FMAP area names. */ |
| 30 | static const char *sections[4] = { |
| 31 | "UNKNOWN SECTION", // EC_LPC_IMAGE_UNKNOWN -- never matches |
| 32 | "RO_SECTION", // EC_LPC_IMAGE_RO |
| 33 | "RW_SECTION_A", // EC_LPC_IMAGE_RW_A |
| 34 | "RW_SECTION_B", // EC_LPC_IMAGE_RW_B |
| 35 | }; |
| 36 | |
David Hendricks | cb760a2 | 2012-01-05 12:33:04 -0800 | [diff] [blame] | 37 | static int ec_timeout_usec = 1000000; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 38 | |
| 39 | /* Waits for the EC to be unbusy. Returns 1 if busy, 0 if not busy. */ |
| 40 | static int ec_busy(int timeout_usec) |
| 41 | { |
| 42 | int i; |
| 43 | for (i = 0; i < timeout_usec; i += 10) { |
| 44 | usleep(10); /* Delay first, in case we just sent a command */ |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 45 | if (!(inb(EC_LPC_ADDR_USER_CMD) & EC_LPC_STATUS_BUSY_MASK)) |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | return 1; /* Timeout */ |
| 49 | } |
| 50 | |
| 51 | |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 52 | static enum lpc_status gec_get_result() { |
| 53 | return inb(EC_LPC_ADDR_USER_DATA); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | /* Sends a command to the EC. Returns the command status code, or |
| 58 | * -1 if other error. */ |
| 59 | int ec_command(int command, const void *indata, int insize, |
| 60 | void *outdata, int outsize) { |
| 61 | uint8_t *d; |
| 62 | int i; |
| 63 | |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 64 | if ((insize + outsize) > EC_LPC_PARAM_SIZE) { |
| 65 | msg_pdbg2("Data size too big for buffer.\n"); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | |
David Hendricks | cb760a2 | 2012-01-05 12:33:04 -0800 | [diff] [blame] | 69 | if (ec_busy(ec_timeout_usec)) { |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 70 | msg_pdbg2("Timeout waiting for EC ready\n"); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | /* Write data, if any */ |
| 75 | /* TODO: optimized copy using outl() */ |
| 76 | for (i = 0, d = (uint8_t *)indata; i < insize; i++, d++) { |
| 77 | msg_pdbg2("GEC: Port[0x%x] <-- 0x%x\n", |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 78 | EC_LPC_ADDR_USER_PARAM + i, *d); |
| 79 | outb(*d, EC_LPC_ADDR_USER_PARAM + i); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | msg_pdbg2("GEC: Run EC Command: 0x%x ----\n", command); |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 83 | outb(command, EC_LPC_ADDR_USER_CMD); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 84 | |
| 85 | if (ec_busy(1000000)) { |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 86 | msg_pdbg2("Timeout waiting for EC response\n"); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | /* Check status */ |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 91 | if ((i = gec_get_result()) != EC_LPC_RESULT_SUCCESS) { |
| 92 | msg_pdbg2("EC returned error status %d\n", i); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 93 | return i; |
| 94 | } |
| 95 | |
| 96 | /* Read data, if any */ |
| 97 | for (i = 0, d = (uint8_t *)outdata; i < outsize; i++, d++) { |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 98 | *d = inb(EC_LPC_ADDR_USER_PARAM + i); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 99 | msg_pdbg2("GEC: Port[0x%x] ---> 0x%x\n", |
Louis Yung-Chieh Lo | c738d9d | 2012-03-06 13:06:26 +0800 | [diff] [blame] | 100 | EC_LPC_ADDR_USER_PARAM + i, *d); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | #ifdef SUPPORT_CHECKSUM |
| 108 | static verify_checksum(uint8_t* expected, |
| 109 | unsigned int addr, |
| 110 | unsigned int count) { |
| 111 | int rc; |
| 112 | struct lpc_params_flash_checksum csp; |
| 113 | struct lpc_response_flash_checksum csr; |
| 114 | uint8_t cs; |
| 115 | int j; |
| 116 | |
| 117 | csp.offset = addr; |
| 118 | csp.size = count; |
| 119 | |
| 120 | rc = ec_command(EC_LPC_COMMAND_FLASH_CHECKSUM, |
| 121 | &csp, sizeof(csp), &csr, sizeof(csr)); |
| 122 | if (rc) { |
| 123 | msg_perr("GEC: verify_checksum() error.\n"); |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | for (cs = 0, j = 0; j < count; ++j) { |
| 128 | BYTE_IN(cs, expected[j]); |
| 129 | } |
| 130 | if (cs != csr.checksum) { |
| 131 | msg_pdbg("GEC: checksum dismatch at 0x%02x " |
| 132 | "(ec: 0x%02x, local: 0x%02x). Retry.\n", |
| 133 | addr, csr.checksum, cs); |
| 134 | msg_pdbg("GEC: "); |
| 135 | for (j = 0; j < count; ++j) { |
| 136 | msg_pdbg("%02x-", expected[j]); |
| 137 | if ((j & 15) == 15) msg_pdbg("\nGEC: "); |
| 138 | } |
| 139 | programmer_delay(1000); |
| 140 | return 1; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | #endif /* SUPPORT_CHECKSUM */ |
| 145 | |
| 146 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 147 | /* Given the range not able to update, mark the corresponding |
| 148 | * firmware as old. |
| 149 | */ |
| 150 | static void gec_invalidate_copy(unsigned int addr, unsigned int len) |
| 151 | { |
| 152 | int i; |
| 153 | |
| 154 | for (i = EC_LPC_IMAGE_RO; i < ARRAY_SIZE(fwcopy); i++) { |
| 155 | struct fmap_area *fw = &fwcopy[i]; |
| 156 | if ((addr >= fw->offset && (addr < fw->offset + fw->size)) || |
| 157 | (fw->offset >= addr && (fw->offset < addr + len))) { |
| 158 | msg_pdbg("Mark firmware [%s] as old.\n", |
| 159 | sections[i]); |
| 160 | fw->flags = 0; // mark as old |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /* Asks EC to jump to a firmware copy. If target is EC_LPC_IMAGE_UNKNOWN, |
| 167 | * then this functions picks a NEW firmware copy and jumps to it. Note that |
| 168 | * RO is preferred, then A, finally B. |
| 169 | * |
| 170 | * Returns 0 for success. |
| 171 | */ |
| 172 | static int gec_jump_copy(enum lpc_current_image target) { |
| 173 | struct lpc_params_reboot_ec p; |
| 174 | int rc; |
| 175 | |
| 176 | p.target = target != EC_LPC_IMAGE_UNKNOWN ? target : |
| 177 | fwcopy[EC_LPC_IMAGE_RO].flags ? EC_LPC_IMAGE_RO : |
| 178 | fwcopy[EC_LPC_IMAGE_RW_A].flags ? EC_LPC_IMAGE_RW_A : |
| 179 | fwcopy[EC_LPC_IMAGE_RW_B].flags ? EC_LPC_IMAGE_RW_B : |
| 180 | EC_LPC_IMAGE_UNKNOWN; |
| 181 | msg_pdbg("GEC is jumping to [%s]\n", sections[p.target]); |
| 182 | if (p.target == EC_LPC_IMAGE_UNKNOWN) return 1; |
| 183 | |
| 184 | rc = ec_command(EC_LPC_COMMAND_REBOOT_EC, |
| 185 | &p, sizeof(p), NULL, 0); |
| 186 | if (rc) { |
| 187 | msg_perr("GEC cannot jump to [%s]\n", sections[p.target]); |
| 188 | } else { |
| 189 | msg_pdbg("GEC has jumped to [%s]\n", sections[p.target]); |
| 190 | } |
| 191 | |
| 192 | /* Sleep 1 sec to wait the EC re-init. */ |
| 193 | usleep(1000000); |
| 194 | |
| 195 | return rc; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* Given an image, this function parses FMAP and recognize the firmware |
| 200 | * ranges. |
| 201 | */ |
| 202 | int gec_prepare(uint8_t *image, int size) { |
| 203 | struct fmap *fmap; |
| 204 | int i, j; |
| 205 | |
| 206 | if (!detected) return 0; |
| 207 | |
| 208 | // Parse the fmap in the image file and cache the firmware ranges. |
| 209 | fmap = fmap_find_in_memory(image, size); |
| 210 | if (!fmap) return 0; |
| 211 | |
| 212 | // Lookup RO/A/B sections in FMAP. |
| 213 | for (i = 0; i < fmap->nareas; i++) { |
| 214 | struct fmap_area *fa = &fmap->areas[i]; |
| 215 | for (j = EC_LPC_IMAGE_RO; j < ARRAY_SIZE(sections); j++) { |
| 216 | if (!strcmp(sections[j], fa->name)) { |
| 217 | msg_pdbg("Found '%s' in image.\n", fa->name); |
| 218 | memcpy(&fwcopy[j], fa, sizeof(*fa)); |
| 219 | fwcopy[j].flags = 1; // mark as new |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return gec_jump_copy(EC_LPC_IMAGE_RO); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /* Returns >0 if we need 2nd pass of erase_and_write_flash(). |
| 229 | * <0 if we cannot jump to any firmware copy. |
| 230 | * ==0 if no more pass is needed. |
| 231 | * |
| 232 | * This function also jumps to new-updated firmware copy before return >0. |
| 233 | */ |
| 234 | int gec_need_2nd_pass(void) { |
| 235 | if (!detected) return 0; |
| 236 | |
| 237 | if (need_2nd_pass) { |
| 238 | if (gec_jump_copy(EC_LPC_IMAGE_UNKNOWN)) { |
| 239 | return -1; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return need_2nd_pass; |
| 244 | } |
| 245 | |
| 246 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 247 | int gec_read(struct flashchip *flash, uint8_t *readarr, |
| 248 | unsigned int blockaddr, unsigned int readcnt) { |
| 249 | int i; |
| 250 | int rc = 0; |
| 251 | struct lpc_params_flash_read p; |
| 252 | struct lpc_response_flash_read r; |
| 253 | |
| 254 | for (i = 0; i < readcnt; i += EC_LPC_FLASH_SIZE_MAX) { |
| 255 | p.offset = blockaddr + i; |
| 256 | p.size = min(readcnt - i, EC_LPC_FLASH_SIZE_MAX); |
| 257 | rc = ec_command(EC_LPC_COMMAND_FLASH_READ, |
| 258 | &p, sizeof(p), &r, sizeof(r)); |
| 259 | if (rc) { |
| 260 | msg_perr("GEC: Flash read error at offset 0x%x\n", |
| 261 | blockaddr + i); |
| 262 | return rc; |
| 263 | } |
| 264 | |
| 265 | #ifdef SUPPORT_CHECKSUM |
| 266 | if (verify_checksum(r.data, blockaddr + i, |
| 267 | min(readcnt - i, EC_LPC_FLASH_SIZE_MAX))) { |
| 268 | msg_pdbg("GEC: re-read...\n"); |
| 269 | i -= EC_LPC_FLASH_SIZE_MAX; |
| 270 | continue; |
| 271 | } |
| 272 | #endif |
| 273 | memcpy(readarr + i, r.data, p.size); |
| 274 | } |
| 275 | |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static int gec_block_erase(struct flashchip *flash, |
| 281 | unsigned int blockaddr, |
| 282 | unsigned int len) { |
| 283 | struct lpc_params_flash_erase erase; |
| 284 | int rc; |
| 285 | uint8_t *blank; |
| 286 | |
| 287 | #ifdef SUPPORT_CHECKSUM |
| 288 | re_erase: |
| 289 | #endif |
| 290 | erase.offset = blockaddr; |
| 291 | erase.size = len; |
| 292 | rc = ec_command(EC_LPC_COMMAND_FLASH_ERASE, &erase, sizeof(erase), |
| 293 | NULL, 0); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 294 | if (rc == EC_LPC_RESULT_ACCESS_DENIED) { |
| 295 | // this is active image. |
| 296 | gec_invalidate_copy(blockaddr, len); |
| 297 | need_2nd_pass = 1; |
| 298 | return ACCESS_DENIED; |
| 299 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 300 | if (rc) { |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 301 | msg_perr("GEC: Flash erase error at address 0x%x, rc=%d\n", |
| 302 | blockaddr, rc); |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | #ifdef SUPPORT_CHECKSUM |
| 307 | blank = malloc(len); |
| 308 | memset(blank, 0xff, len); |
| 309 | if (verify_checksum(blank, blockaddr, len)) { |
| 310 | msg_pdbg("GEC: Re-erase...\n"); |
| 311 | goto re_erase; |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | return rc; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | int gec_write(struct flashchip *flash, uint8_t *buf, unsigned int addr, |
| 320 | unsigned int nbytes) { |
| 321 | int i, rc = 0; |
| 322 | unsigned int written = 0; |
| 323 | struct lpc_params_flash_write p; |
| 324 | |
| 325 | for (i = 0; i < nbytes; i += written) { |
| 326 | written = min(nbytes - i, EC_LPC_FLASH_SIZE_MAX); |
| 327 | p.offset = addr + i; |
| 328 | p.size = written; |
| 329 | memcpy(p.data, &buf[i], written); |
| 330 | rc = ec_command(EC_LPC_COMMAND_FLASH_WRITE, &p, sizeof(p), |
| 331 | NULL, 0); |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 332 | if (rc == EC_LPC_RESULT_ACCESS_DENIED) { |
| 333 | // this is active image. |
| 334 | gec_invalidate_copy(addr, nbytes); |
| 335 | need_2nd_pass = 1; |
| 336 | return ACCESS_DENIED; |
| 337 | } |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 338 | |
| 339 | #ifdef SUPPORT_CHECKSUM |
| 340 | if (verify_checksum(&buf[i], addr + i, written)) { |
| 341 | msg_pdbg("GEC: re-write...\n"); |
| 342 | i -= written; |
| 343 | continue; |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | if (rc) break; |
| 348 | } |
| 349 | |
| 350 | return rc; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static int gec_list_ranges(const struct flashchip *flash) { |
| 355 | msg_pinfo("You can specify any range:\n"); |
| 356 | msg_pinfo(" from: 0x%06x, to: 0x%06x\n", 0, flash->total_size * 1024); |
| 357 | msg_pinfo(" unit: 0x%06x (%dKB)\n", 2048, 2048); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | static int gec_set_range(const struct flashchip *flash, |
| 363 | unsigned int start, unsigned int len) { |
| 364 | struct lpc_params_flash_wp_range p; |
| 365 | int rc; |
| 366 | |
| 367 | p.offset = start; |
| 368 | p.size = len; |
| 369 | rc = ec_command(EC_LPC_COMMAND_FLASH_WP_SET_RANGE, &p, sizeof(p), |
| 370 | NULL, 0); |
| 371 | if (rc) { |
| 372 | msg_perr("GEC: wp_set_range error: rc=%d\n", rc); |
| 373 | return rc; |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | static int gec_enable_writeprotect(const struct flashchip *flash) { |
| 381 | struct lpc_params_flash_wp_enable p; |
| 382 | int rc; |
| 383 | |
| 384 | p.enable_wp = 1; |
| 385 | rc = ec_command(EC_LPC_COMMAND_FLASH_WP_ENABLE, &p, sizeof(p), |
| 386 | NULL, 0); |
| 387 | if (rc) { |
| 388 | msg_perr("GEC: wp_enable_wp error: rc=%d\n", rc); |
| 389 | } |
| 390 | |
| 391 | return rc; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | static int gec_disable_writeprotect(const struct flashchip *flash) { |
| 396 | struct lpc_params_flash_wp_enable p; |
| 397 | int rc; |
| 398 | |
| 399 | p.enable_wp = 0; |
| 400 | rc = ec_command(EC_LPC_COMMAND_FLASH_WP_ENABLE, &p, sizeof(p), |
| 401 | NULL, 0); |
| 402 | if (rc) { |
| 403 | msg_perr("GEC: wp_disable_wp error: rc=%d\n", rc); |
| 404 | } else { |
| 405 | msg_pinfo("Disabled WP. Reboot EC and de-assert #WP.\n"); |
| 406 | } |
| 407 | |
| 408 | return rc; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | static int gec_wp_status(const struct flashchip *flash) { |
| 413 | int rc; |
| 414 | struct lpc_response_flash_wp_range range; |
| 415 | struct lpc_response_flash_wp_enable en; |
| 416 | uint8_t value; |
| 417 | |
| 418 | rc = ec_command(EC_LPC_COMMAND_FLASH_WP_GET_RANGE, NULL, 0, |
| 419 | &range, sizeof(range)); |
| 420 | if (rc) { |
| 421 | msg_perr("GEC: wp_get_wp_range error: rc=%d\n", rc); |
| 422 | return rc; |
| 423 | } |
| 424 | rc = ec_command(EC_LPC_COMMAND_FLASH_WP_GET_STATE, NULL, 0, |
| 425 | &en, sizeof(en)); |
| 426 | if (rc) { |
| 427 | msg_perr("GEC: wp_get_wp_state error: rc=%d\n", rc); |
| 428 | return rc; |
| 429 | } |
| 430 | |
| 431 | /* TODO: Fix scripts which rely on SPI-specific terminology. */ |
| 432 | value = (en.enable_wp << 7); |
| 433 | msg_pinfo("WP: status: 0x%02x\n", value); |
| 434 | msg_pinfo("WP: status.srp0: %x\n", en.enable_wp); |
| 435 | msg_pinfo("WP: write protect is %s.\n", |
| 436 | en.enable_wp ? "enabled" : "disabled"); |
| 437 | msg_pinfo("WP: write protect range: start=0x%08x, len=0x%08x\n", |
| 438 | range.offset, range.size); |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | static int gec_probe_size(struct flashchip *flash) { |
| 445 | int rc; |
| 446 | struct lpc_response_flash_info info; |
| 447 | struct block_eraser *eraser; |
| 448 | static struct wp wp = { |
| 449 | .list_ranges = gec_list_ranges, |
| 450 | .set_range = gec_set_range, |
| 451 | .enable = gec_enable_writeprotect, |
| 452 | .disable = gec_disable_writeprotect, |
| 453 | .wp_status = gec_wp_status, |
| 454 | }; |
| 455 | |
| 456 | rc = ec_command(EC_LPC_COMMAND_FLASH_INFO, NULL, 0, |
| 457 | &info, sizeof(info)); |
| 458 | if (rc) return 0; |
| 459 | |
| 460 | flash->total_size = info.flash_size / 1024; |
| 461 | flash->page_size = min(info.write_block_size, |
| 462 | info.erase_block_size); |
| 463 | flash->tested = TEST_OK_PREW; |
| 464 | eraser = &flash->block_erasers[0]; |
| 465 | eraser->eraseblocks[0].size = info.erase_block_size; |
| 466 | eraser->eraseblocks[0].count = info.flash_size / |
| 467 | eraser->eraseblocks[0].size; |
| 468 | flash->wp = ℘ |
| 469 | |
| 470 | return 1; |
| 471 | }; |
| 472 | |
| 473 | |
| 474 | static const struct opaque_programmer opaque_programmer_gec = { |
| 475 | .max_data_read = EC_LPC_FLASH_SIZE_MAX, |
| 476 | .max_data_write = EC_LPC_FLASH_SIZE_MAX, |
| 477 | .probe = gec_probe_size, |
| 478 | .read = gec_read, |
| 479 | .write = gec_write, |
| 480 | .erase = gec_block_erase, |
| 481 | }; |
| 482 | |
| 483 | |
| 484 | /* Sends HELLO command to ACPI port and expects a value from Google EC. |
| 485 | * |
| 486 | * TODO: This is an intrusive command for non-Google ECs. Needs a more proper |
| 487 | * and more friendly way to detect. |
| 488 | */ |
| 489 | static int detect_ec(void) { |
| 490 | struct lpc_params_hello request; |
| 491 | struct lpc_response_hello response; |
| 492 | int rc = 0; |
David Hendricks | cb760a2 | 2012-01-05 12:33:04 -0800 | [diff] [blame] | 493 | int old_timeout = ec_timeout_usec; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 494 | |
| 495 | if (target_bus != BUS_LPC) { |
| 496 | msg_pdbg("%s():%d target_bus is not LPC.\n", __func__, __LINE__); |
| 497 | return 1; |
| 498 | } |
| 499 | |
David Hendricks | cb760a2 | 2012-01-05 12:33:04 -0800 | [diff] [blame] | 500 | /* reduce timeout period temporarily in case EC is not present */ |
| 501 | ec_timeout_usec = 25000; |
| 502 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 503 | /* Say hello to EC. */ |
| 504 | request.in_data = 0xf0e0d0c0; /* Expect EC will add on 0x01020304. */ |
| 505 | rc = ec_command(EC_LPC_COMMAND_HELLO, &request, sizeof(request), |
| 506 | &response, sizeof(response)); |
David Hendricks | cb760a2 | 2012-01-05 12:33:04 -0800 | [diff] [blame] | 507 | |
| 508 | ec_timeout_usec = old_timeout; |
| 509 | |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 510 | if (rc || response.out_data != 0xf1e2d3c4) { |
| 511 | msg_pdbg("response.out_data is not 0xf1e2d3c4.\n" |
| 512 | "rc=%d, request=0x%x response=0x%x\n", |
| 513 | rc, request.in_data, response.out_data); |
| 514 | #ifdef SUPPORT_CHECKSUM |
| 515 | /* In this mode, we can tolerate some bit errors. */ |
| 516 | { |
| 517 | int diff = response.out_data ^ 0xf1e2d3c4; |
| 518 | if (!(diff = (diff - 1) & diff)) return 0;// 1-bit error |
| 519 | if (!(diff = (diff - 1) & diff)) return 0;// 2-bit error |
| 520 | if (!(diff = (diff - 1) & diff)) return 0;// 3-bit error |
| 521 | if (!(diff = (diff - 1) & diff)) return 0;// 4-bit error |
| 522 | } |
| 523 | #endif |
| 524 | return 1; |
| 525 | } |
| 526 | |
Louis Yung-Chieh Lo | 8d0971e | 2012-03-23 00:07:38 +0800 | [diff] [blame^] | 527 | detected = 1; |
Louis Yung-Chieh Lo | edb0cba | 2011-12-09 17:06:54 +0800 | [diff] [blame] | 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | /* Called by internal_init() */ |
| 532 | int gec_probe_programmer(const char *name) { |
| 533 | struct pci_dev *dev; |
| 534 | |
| 535 | msg_pdbg("%s():%d ...\n", __func__, __LINE__); |
| 536 | |
| 537 | if (detect_ec()) return 1; |
| 538 | |
| 539 | register_opaque_programmer(&opaque_programmer_gec); |
| 540 | if (buses_supported & BUS_SPI) { |
| 541 | msg_pdbg("%s():%d remove BUS_SPI from buses_supported.\n", |
| 542 | __func__, __LINE__); |
| 543 | buses_supported &= ~BUS_SPI; |
| 544 | } |
| 545 | buses_supported |= BUS_LPC; |
| 546 | |
| 547 | return 0; |
| 548 | } |