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