1 /* 2 * Chromium OS cros_ec driver 3 * 4 * Copyright (c) 2012 The Chromium OS Authors. 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 /* 10 * The Matrix Keyboard Protocol driver handles talking to the keyboard 11 * controller chip. Mostly this is for keyboard functions, but some other 12 * things have slipped in, so we provide generic services to talk to the 13 * KBC. 14 */ 15 16 #include <common.h> 17 #include <command.h> 18 #include <i2c.h> 19 #include <cros_ec.h> 20 #include <fdtdec.h> 21 #include <malloc.h> 22 #include <spi.h> 23 #include <asm/io.h> 24 #include <asm-generic/gpio.h> 25 26 #ifdef DEBUG_TRACE 27 #define debug_trace(fmt, b...) debug(fmt, #b) 28 #else 29 #define debug_trace(fmt, b...) 30 #endif 31 32 enum { 33 /* Timeout waiting for a flash erase command to complete */ 34 CROS_EC_CMD_TIMEOUT_MS = 5000, 35 /* Timeout waiting for a synchronous hash to be recomputed */ 36 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000, 37 }; 38 39 static struct cros_ec_dev static_dev, *last_dev; 40 41 DECLARE_GLOBAL_DATA_PTR; 42 43 /* Note: depends on enum ec_current_image */ 44 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"}; 45 46 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len) 47 { 48 #ifdef DEBUG 49 int i; 50 51 printf("%s: ", name); 52 if (cmd != -1) 53 printf("cmd=%#x: ", cmd); 54 for (i = 0; i < len; i++) 55 printf("%02x ", data[i]); 56 printf("\n"); 57 #endif 58 } 59 60 /* 61 * Calculate a simple 8-bit checksum of a data block 62 * 63 * @param data Data block to checksum 64 * @param size Size of data block in bytes 65 * @return checksum value (0 to 255) 66 */ 67 int cros_ec_calc_checksum(const uint8_t *data, int size) 68 { 69 int csum, i; 70 71 for (i = csum = 0; i < size; i++) 72 csum += data[i]; 73 return csum & 0xff; 74 } 75 76 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, 77 const void *dout, int dout_len, 78 uint8_t **dinp, int din_len) 79 { 80 int ret; 81 82 switch (dev->interface) { 83 #ifdef CONFIG_CROS_EC_SPI 84 case CROS_EC_IF_SPI: 85 ret = cros_ec_spi_command(dev, cmd, cmd_version, 86 (const uint8_t *)dout, dout_len, 87 dinp, din_len); 88 break; 89 #endif 90 #ifdef CONFIG_CROS_EC_I2C 91 case CROS_EC_IF_I2C: 92 ret = cros_ec_i2c_command(dev, cmd, cmd_version, 93 (const uint8_t *)dout, dout_len, 94 dinp, din_len); 95 break; 96 #endif 97 #ifdef CONFIG_CROS_EC_LPC 98 case CROS_EC_IF_LPC: 99 ret = cros_ec_lpc_command(dev, cmd, cmd_version, 100 (const uint8_t *)dout, dout_len, 101 dinp, din_len); 102 break; 103 #endif 104 case CROS_EC_IF_NONE: 105 default: 106 ret = -1; 107 } 108 109 return ret; 110 } 111 112 /** 113 * Send a command to the CROS-EC device and return the reply. 114 * 115 * The device's internal input/output buffers are used. 116 * 117 * @param dev CROS-EC device 118 * @param cmd Command to send (EC_CMD_...) 119 * @param cmd_version Version of command to send (EC_VER_...) 120 * @param dout Output data (may be NULL If dout_len=0) 121 * @param dout_len Size of output data in bytes 122 * @param dinp Response data (may be NULL If din_len=0). 123 * If not NULL, it will be updated to point to the data 124 * and will always be double word aligned (64-bits) 125 * @param din_len Maximum size of response in bytes 126 * @return number of bytes in response, or -1 on error 127 */ 128 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd, 129 int cmd_version, const void *dout, int dout_len, uint8_t **dinp, 130 int din_len) 131 { 132 uint8_t *din; 133 int len; 134 135 len = send_command(dev, cmd, cmd_version, dout, dout_len, 136 &din, din_len); 137 138 /* If the command doesn't complete, wait a while */ 139 if (len == -EC_RES_IN_PROGRESS) { 140 struct ec_response_get_comms_status *resp; 141 ulong start; 142 143 /* Wait for command to complete */ 144 start = get_timer(0); 145 do { 146 int ret; 147 148 mdelay(50); /* Insert some reasonable delay */ 149 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0, 150 NULL, 0, 151 (uint8_t **)&resp, sizeof(*resp)); 152 if (ret < 0) 153 return ret; 154 155 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) { 156 debug("%s: Command %#02x timeout\n", 157 __func__, cmd); 158 return -EC_RES_TIMEOUT; 159 } 160 } while (resp->flags & EC_COMMS_STATUS_PROCESSING); 161 162 /* OK it completed, so read the status response */ 163 /* not sure why it was 0 for the last argument */ 164 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0, 165 NULL, 0, &din, din_len); 166 } 167 168 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp); 169 if (dinp) { 170 /* If we have any data to return, it must be 64bit-aligned */ 171 assert(len <= 0 || !((uintptr_t)din & 7)); 172 *dinp = din; 173 } 174 175 return len; 176 } 177 178 /** 179 * Send a command to the CROS-EC device and return the reply. 180 * 181 * The device's internal input/output buffers are used. 182 * 183 * @param dev CROS-EC device 184 * @param cmd Command to send (EC_CMD_...) 185 * @param cmd_version Version of command to send (EC_VER_...) 186 * @param dout Output data (may be NULL If dout_len=0) 187 * @param dout_len Size of output data in bytes 188 * @param din Response data (may be NULL If din_len=0). 189 * It not NULL, it is a place for ec_command() to copy the 190 * data to. 191 * @param din_len Maximum size of response in bytes 192 * @return number of bytes in response, or -1 on error 193 */ 194 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version, 195 const void *dout, int dout_len, 196 void *din, int din_len) 197 { 198 uint8_t *in_buffer; 199 int len; 200 201 assert((din_len == 0) || din); 202 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len, 203 &in_buffer, din_len); 204 if (len > 0) { 205 /* 206 * If we were asked to put it somewhere, do so, otherwise just 207 * disregard the result. 208 */ 209 if (din && in_buffer) { 210 assert(len <= din_len); 211 memmove(din, in_buffer, len); 212 } 213 } 214 return len; 215 } 216 217 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan) 218 { 219 if (ec_command(dev, EC_CMD_CROS_EC_STATE, 0, NULL, 0, scan, 220 sizeof(scan->data)) < sizeof(scan->data)) 221 return -1; 222 223 return 0; 224 } 225 226 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen) 227 { 228 struct ec_response_get_version *r; 229 230 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, 231 (uint8_t **)&r, sizeof(*r)) < sizeof(*r)) 232 return -1; 233 234 if (maxlen > sizeof(r->version_string_ro)) 235 maxlen = sizeof(r->version_string_ro); 236 237 switch (r->current_image) { 238 case EC_IMAGE_RO: 239 memcpy(id, r->version_string_ro, maxlen); 240 break; 241 case EC_IMAGE_RW: 242 memcpy(id, r->version_string_rw, maxlen); 243 break; 244 default: 245 return -1; 246 } 247 248 id[maxlen - 1] = '\0'; 249 return 0; 250 } 251 252 int cros_ec_read_version(struct cros_ec_dev *dev, 253 struct ec_response_get_version **versionp) 254 { 255 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, 256 (uint8_t **)versionp, sizeof(**versionp)) 257 < sizeof(**versionp)) 258 return -1; 259 260 return 0; 261 } 262 263 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp) 264 { 265 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0, 266 (uint8_t **)strp, EC_HOST_PARAM_SIZE) < 0) 267 return -1; 268 269 return 0; 270 } 271 272 int cros_ec_read_current_image(struct cros_ec_dev *dev, 273 enum ec_current_image *image) 274 { 275 struct ec_response_get_version *r; 276 277 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0, 278 (uint8_t **)&r, sizeof(*r)) < sizeof(*r)) 279 return -1; 280 281 *image = r->current_image; 282 return 0; 283 } 284 285 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev, 286 struct ec_response_vboot_hash *hash) 287 { 288 struct ec_params_vboot_hash p; 289 ulong start; 290 291 start = get_timer(0); 292 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) { 293 mdelay(50); /* Insert some reasonable delay */ 294 295 p.cmd = EC_VBOOT_HASH_GET; 296 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), 297 hash, sizeof(*hash)) < 0) 298 return -1; 299 300 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) { 301 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__); 302 return -EC_RES_TIMEOUT; 303 } 304 } 305 return 0; 306 } 307 308 309 int cros_ec_read_hash(struct cros_ec_dev *dev, 310 struct ec_response_vboot_hash *hash) 311 { 312 struct ec_params_vboot_hash p; 313 int rv; 314 315 p.cmd = EC_VBOOT_HASH_GET; 316 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), 317 hash, sizeof(*hash)) < 0) 318 return -1; 319 320 /* If the EC is busy calculating the hash, fidget until it's done. */ 321 rv = cros_ec_wait_on_hash_done(dev, hash); 322 if (rv) 323 return rv; 324 325 /* If the hash is valid, we're done. Otherwise, we have to kick it off 326 * again and wait for it to complete. Note that we explicitly assume 327 * that hashing zero bytes is always wrong, even though that would 328 * produce a valid hash value. */ 329 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size) 330 return 0; 331 332 debug("%s: No valid hash (status=%d size=%d). Compute one...\n", 333 __func__, hash->status, hash->size); 334 335 p.cmd = EC_VBOOT_HASH_RECALC; 336 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; 337 p.nonce_size = 0; 338 p.offset = EC_VBOOT_HASH_OFFSET_RW; 339 340 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), 341 hash, sizeof(*hash)) < 0) 342 return -1; 343 344 rv = cros_ec_wait_on_hash_done(dev, hash); 345 if (rv) 346 return rv; 347 348 debug("%s: hash done\n", __func__); 349 350 return 0; 351 } 352 353 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev) 354 { 355 struct ec_params_vboot_hash p; 356 struct ec_response_vboot_hash *hash; 357 358 /* We don't have an explict command for the EC to discard its current 359 * hash value, so we'll just tell it to calculate one that we know is 360 * wrong (we claim that hashing zero bytes is always invalid). 361 */ 362 p.cmd = EC_VBOOT_HASH_RECALC; 363 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256; 364 p.nonce_size = 0; 365 p.offset = 0; 366 p.size = 0; 367 368 debug("%s:\n", __func__); 369 370 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p), 371 (uint8_t **)&hash, sizeof(*hash)) < 0) 372 return -1; 373 374 /* No need to wait for it to finish */ 375 return 0; 376 } 377 378 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd, 379 uint8_t flags) 380 { 381 struct ec_params_reboot_ec p; 382 383 p.cmd = cmd; 384 p.flags = flags; 385 386 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0) 387 < 0) 388 return -1; 389 390 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) { 391 /* 392 * EC reboot will take place immediately so delay to allow it 393 * to complete. Note that some reboot types (EC_REBOOT_COLD) 394 * will reboot the AP as well, in which case we won't actually 395 * get to this point. 396 */ 397 /* 398 * TODO(rspangler@chromium.org): Would be nice if we had a 399 * better way to determine when the reboot is complete. Could 400 * we poll a memory-mapped LPC value? 401 */ 402 udelay(50000); 403 } 404 405 return 0; 406 } 407 408 int cros_ec_interrupt_pending(struct cros_ec_dev *dev) 409 { 410 /* no interrupt support : always poll */ 411 if (!fdt_gpio_isvalid(&dev->ec_int)) 412 return 1; 413 414 return !gpio_get_value(dev->ec_int.gpio); 415 } 416 417 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_cros_ec_info *info) 418 { 419 if (ec_command(dev, EC_CMD_CROS_EC_INFO, 0, NULL, 0, info, 420 sizeof(*info)) < sizeof(*info)) 421 return -1; 422 423 return 0; 424 } 425 426 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr) 427 { 428 struct ec_response_host_event_mask *resp; 429 430 /* 431 * Use the B copy of the event flags, because the main copy is already 432 * used by ACPI/SMI. 433 */ 434 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0, 435 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) 436 return -1; 437 438 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID)) 439 return -1; 440 441 *events_ptr = resp->mask; 442 return 0; 443 } 444 445 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events) 446 { 447 struct ec_params_host_event_mask params; 448 449 params.mask = events; 450 451 /* 452 * Use the B copy of the event flags, so it affects the data returned 453 * by cros_ec_get_host_events(). 454 */ 455 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0, 456 ¶ms, sizeof(params), NULL, 0) < 0) 457 return -1; 458 459 return 0; 460 } 461 462 int cros_ec_flash_protect(struct cros_ec_dev *dev, 463 uint32_t set_mask, uint32_t set_flags, 464 struct ec_response_flash_protect *resp) 465 { 466 struct ec_params_flash_protect params; 467 468 params.mask = set_mask; 469 params.flags = set_flags; 470 471 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT, 472 ¶ms, sizeof(params), 473 resp, sizeof(*resp)) < sizeof(*resp)) 474 return -1; 475 476 return 0; 477 } 478 479 static int cros_ec_check_version(struct cros_ec_dev *dev) 480 { 481 struct ec_params_hello req; 482 struct ec_response_hello *resp; 483 484 #ifdef CONFIG_CROS_EC_LPC 485 /* LPC has its own way of doing this */ 486 if (dev->interface == CROS_EC_IF_LPC) 487 return cros_ec_lpc_check_version(dev); 488 #endif 489 490 /* 491 * TODO(sjg@chromium.org). 492 * There is a strange oddity here with the EC. We could just ignore 493 * the response, i.e. pass the last two parameters as NULL and 0. 494 * In this case we won't read back very many bytes from the EC. 495 * On the I2C bus the EC gets upset about this and will try to send 496 * the bytes anyway. This means that we will have to wait for that 497 * to complete before continuing with a new EC command. 498 * 499 * This problem is probably unique to the I2C bus. 500 * 501 * So for now, just read all the data anyway. 502 */ 503 dev->cmd_version_is_supported = 1; 504 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), 505 (uint8_t **)&resp, sizeof(*resp)) > 0) { 506 /* It appears to understand new version commands */ 507 dev->cmd_version_is_supported = 1; 508 } else { 509 printf("%s: ERROR: old EC interface not supported\n", 510 __func__); 511 return -1; 512 } 513 514 return 0; 515 } 516 517 int cros_ec_test(struct cros_ec_dev *dev) 518 { 519 struct ec_params_hello req; 520 struct ec_response_hello *resp; 521 522 req.in_data = 0x12345678; 523 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req), 524 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) { 525 printf("ec_command_inptr() returned error\n"); 526 return -1; 527 } 528 if (resp->out_data != req.in_data + 0x01020304) { 529 printf("Received invalid handshake %x\n", resp->out_data); 530 return -1; 531 } 532 533 return 0; 534 } 535 536 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region, 537 uint32_t *offset, uint32_t *size) 538 { 539 struct ec_params_flash_region_info p; 540 struct ec_response_flash_region_info *r; 541 int ret; 542 543 p.region = region; 544 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO, 545 EC_VER_FLASH_REGION_INFO, 546 &p, sizeof(p), (uint8_t **)&r, sizeof(*r)); 547 if (ret != sizeof(*r)) 548 return -1; 549 550 if (offset) 551 *offset = r->offset; 552 if (size) 553 *size = r->size; 554 555 return 0; 556 } 557 558 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size) 559 { 560 struct ec_params_flash_erase p; 561 562 p.offset = offset; 563 p.size = size; 564 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p), 565 NULL, 0); 566 } 567 568 /** 569 * Write a single block to the flash 570 * 571 * Write a block of data to the EC flash. The size must not exceed the flash 572 * write block size which you can obtain from cros_ec_flash_write_burst_size(). 573 * 574 * The offset starts at 0. You can obtain the region information from 575 * cros_ec_flash_offset() to find out where to write for a particular region. 576 * 577 * Attempting to write to the region where the EC is currently running from 578 * will result in an error. 579 * 580 * @param dev CROS-EC device 581 * @param data Pointer to data buffer to write 582 * @param offset Offset within flash to write to. 583 * @param size Number of bytes to write 584 * @return 0 if ok, -1 on error 585 */ 586 static int cros_ec_flash_write_block(struct cros_ec_dev *dev, 587 const uint8_t *data, uint32_t offset, uint32_t size) 588 { 589 struct ec_params_flash_write p; 590 591 p.offset = offset; 592 p.size = size; 593 assert(data && p.size <= sizeof(p.data)); 594 memcpy(p.data, data, p.size); 595 596 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0, 597 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1; 598 } 599 600 /** 601 * Return optimal flash write burst size 602 */ 603 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev) 604 { 605 struct ec_params_flash_write p; 606 return sizeof(p.data); 607 } 608 609 /** 610 * Check if a block of data is erased (all 0xff) 611 * 612 * This function is useful when dealing with flash, for checking whether a 613 * data block is erased and thus does not need to be programmed. 614 * 615 * @param data Pointer to data to check (must be word-aligned) 616 * @param size Number of bytes to check (must be word-aligned) 617 * @return 0 if erased, non-zero if any word is not erased 618 */ 619 static int cros_ec_data_is_erased(const uint32_t *data, int size) 620 { 621 assert(!(size & 3)); 622 size /= sizeof(uint32_t); 623 for (; size > 0; size -= 4, data++) 624 if (*data != -1U) 625 return 0; 626 627 return 1; 628 } 629 630 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data, 631 uint32_t offset, uint32_t size) 632 { 633 uint32_t burst = cros_ec_flash_write_burst_size(dev); 634 uint32_t end, off; 635 int ret; 636 637 /* 638 * TODO: round up to the nearest multiple of write size. Can get away 639 * without that on link right now because its write size is 4 bytes. 640 */ 641 end = offset + size; 642 for (off = offset; off < end; off += burst, data += burst) { 643 uint32_t todo; 644 645 /* If the data is empty, there is no point in programming it */ 646 todo = min(end - off, burst); 647 if (dev->optimise_flash_write && 648 cros_ec_data_is_erased((uint32_t *)data, todo)) 649 continue; 650 651 ret = cros_ec_flash_write_block(dev, data, off, todo); 652 if (ret) 653 return ret; 654 } 655 656 return 0; 657 } 658 659 /** 660 * Read a single block from the flash 661 * 662 * Read a block of data from the EC flash. The size must not exceed the flash 663 * write block size which you can obtain from cros_ec_flash_write_burst_size(). 664 * 665 * The offset starts at 0. You can obtain the region information from 666 * cros_ec_flash_offset() to find out where to read for a particular region. 667 * 668 * @param dev CROS-EC device 669 * @param data Pointer to data buffer to read into 670 * @param offset Offset within flash to read from 671 * @param size Number of bytes to read 672 * @return 0 if ok, -1 on error 673 */ 674 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data, 675 uint32_t offset, uint32_t size) 676 { 677 struct ec_params_flash_read p; 678 679 p.offset = offset; 680 p.size = size; 681 682 return ec_command(dev, EC_CMD_FLASH_READ, 0, 683 &p, sizeof(p), data, size) >= 0 ? 0 : -1; 684 } 685 686 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset, 687 uint32_t size) 688 { 689 uint32_t burst = cros_ec_flash_write_burst_size(dev); 690 uint32_t end, off; 691 int ret; 692 693 end = offset + size; 694 for (off = offset; off < end; off += burst, data += burst) { 695 ret = cros_ec_flash_read_block(dev, data, off, 696 min(end - off, burst)); 697 if (ret) 698 return ret; 699 } 700 701 return 0; 702 } 703 704 int cros_ec_flash_update_rw(struct cros_ec_dev *dev, 705 const uint8_t *image, int image_size) 706 { 707 uint32_t rw_offset, rw_size; 708 int ret; 709 710 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size)) 711 return -1; 712 if (image_size > rw_size) 713 return -1; 714 715 /* Invalidate the existing hash, just in case the AP reboots 716 * unexpectedly during the update. If that happened, the EC RW firmware 717 * would be invalid, but the EC would still have the original hash. 718 */ 719 ret = cros_ec_invalidate_hash(dev); 720 if (ret) 721 return ret; 722 723 /* 724 * Erase the entire RW section, so that the EC doesn't see any garbage 725 * past the new image if it's smaller than the current image. 726 * 727 * TODO: could optimize this to erase just the current image, since 728 * presumably everything past that is 0xff's. But would still need to 729 * round up to the nearest multiple of erase size. 730 */ 731 ret = cros_ec_flash_erase(dev, rw_offset, rw_size); 732 if (ret) 733 return ret; 734 735 /* Write the image */ 736 ret = cros_ec_flash_write(dev, image, rw_offset, image_size); 737 if (ret) 738 return ret; 739 740 return 0; 741 } 742 743 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block) 744 { 745 struct ec_params_vbnvcontext p; 746 int len; 747 748 p.op = EC_VBNV_CONTEXT_OP_READ; 749 750 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, 751 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE); 752 if (len < EC_VBNV_BLOCK_SIZE) 753 return -1; 754 755 return 0; 756 } 757 758 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block) 759 { 760 struct ec_params_vbnvcontext p; 761 int len; 762 763 p.op = EC_VBNV_CONTEXT_OP_WRITE; 764 memcpy(p.block, block, sizeof(p.block)); 765 766 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT, 767 &p, sizeof(p), NULL, 0); 768 if (len < 0) 769 return -1; 770 771 return 0; 772 } 773 774 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state) 775 { 776 struct ec_params_ldo_set params; 777 778 params.index = index; 779 params.state = state; 780 781 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, 782 ¶ms, sizeof(params), 783 NULL, 0)) 784 return -1; 785 786 return 0; 787 } 788 789 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state) 790 { 791 struct ec_params_ldo_get params; 792 struct ec_response_ldo_get *resp; 793 794 params.index = index; 795 796 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, 797 ¶ms, sizeof(params), 798 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) 799 return -1; 800 801 *state = resp->state; 802 803 return 0; 804 } 805 806 /** 807 * Decode MBKP details from the device tree and allocate a suitable device. 808 * 809 * @param blob Device tree blob 810 * @param node Node to decode from 811 * @param devp Returns a pointer to the new allocated device 812 * @return 0 if ok, -1 on error 813 */ 814 static int cros_ec_decode_fdt(const void *blob, int node, 815 struct cros_ec_dev **devp) 816 { 817 enum fdt_compat_id compat; 818 struct cros_ec_dev *dev; 819 int parent; 820 821 /* See what type of parent we are inside (this is expensive) */ 822 parent = fdt_parent_offset(blob, node); 823 if (parent < 0) { 824 debug("%s: Cannot find node parent\n", __func__); 825 return -1; 826 } 827 828 dev = &static_dev; 829 dev->node = node; 830 dev->parent_node = parent; 831 832 compat = fdtdec_lookup(blob, parent); 833 switch (compat) { 834 #ifdef CONFIG_CROS_EC_SPI 835 case COMPAT_SAMSUNG_EXYNOS_SPI: 836 dev->interface = CROS_EC_IF_SPI; 837 if (cros_ec_spi_decode_fdt(dev, blob)) 838 return -1; 839 break; 840 #endif 841 #ifdef CONFIG_CROS_EC_I2C 842 case COMPAT_SAMSUNG_S3C2440_I2C: 843 dev->interface = CROS_EC_IF_I2C; 844 if (cros_ec_i2c_decode_fdt(dev, blob)) 845 return -1; 846 break; 847 #endif 848 #ifdef CONFIG_CROS_EC_LPC 849 case COMPAT_INTEL_LPC: 850 dev->interface = CROS_EC_IF_LPC; 851 break; 852 #endif 853 default: 854 debug("%s: Unknown compat id %d\n", __func__, compat); 855 return -1; 856 } 857 858 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int); 859 dev->optimise_flash_write = fdtdec_get_bool(blob, node, 860 "optimise-flash-write"); 861 *devp = dev; 862 863 return 0; 864 } 865 866 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp) 867 { 868 char id[MSG_BYTES]; 869 struct cros_ec_dev *dev; 870 int node = 0; 871 872 *cros_ecp = NULL; 873 do { 874 node = fdtdec_next_compatible(blob, node, 875 COMPAT_GOOGLE_CROS_EC); 876 if (node < 0) { 877 debug("%s: Node not found\n", __func__); 878 return 0; 879 } 880 } while (!fdtdec_get_is_enabled(blob, node)); 881 882 if (cros_ec_decode_fdt(blob, node, &dev)) { 883 debug("%s: Failed to decode device.\n", __func__); 884 return -CROS_EC_ERR_FDT_DECODE; 885 } 886 887 switch (dev->interface) { 888 #ifdef CONFIG_CROS_EC_SPI 889 case CROS_EC_IF_SPI: 890 if (cros_ec_spi_init(dev, blob)) { 891 debug("%s: Could not setup SPI interface\n", __func__); 892 return -CROS_EC_ERR_DEV_INIT; 893 } 894 break; 895 #endif 896 #ifdef CONFIG_CROS_EC_I2C 897 case CROS_EC_IF_I2C: 898 if (cros_ec_i2c_init(dev, blob)) 899 return -CROS_EC_ERR_DEV_INIT; 900 break; 901 #endif 902 #ifdef CONFIG_CROS_EC_LPC 903 case CROS_EC_IF_LPC: 904 if (cros_ec_lpc_init(dev, blob)) 905 return -CROS_EC_ERR_DEV_INIT; 906 break; 907 #endif 908 case CROS_EC_IF_NONE: 909 default: 910 return 0; 911 } 912 913 /* we will poll the EC interrupt line */ 914 fdtdec_setup_gpio(&dev->ec_int); 915 if (fdt_gpio_isvalid(&dev->ec_int)) 916 gpio_direction_input(dev->ec_int.gpio); 917 918 if (cros_ec_check_version(dev)) { 919 debug("%s: Could not detect CROS-EC version\n", __func__); 920 return -CROS_EC_ERR_CHECK_VERSION; 921 } 922 923 if (cros_ec_read_id(dev, id, sizeof(id))) { 924 debug("%s: Could not read KBC ID\n", __func__); 925 return -CROS_EC_ERR_READ_ID; 926 } 927 928 /* Remember this device for use by the cros_ec command */ 929 last_dev = *cros_ecp = dev; 930 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id); 931 932 return 0; 933 } 934 935 int cros_ec_decode_region(int argc, char * const argv[]) 936 { 937 if (argc > 0) { 938 if (0 == strcmp(*argv, "rw")) 939 return EC_FLASH_REGION_RW; 940 else if (0 == strcmp(*argv, "ro")) 941 return EC_FLASH_REGION_RO; 942 943 debug("%s: Invalid region '%s'\n", __func__, *argv); 944 } else { 945 debug("%s: Missing region parameter\n", __func__); 946 } 947 948 return -1; 949 } 950 951 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config) 952 { 953 int flash_node, node; 954 955 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC); 956 if (node < 0) { 957 debug("Failed to find chrome-ec node'\n"); 958 return -1; 959 } 960 961 flash_node = fdt_subnode_offset(blob, node, "flash"); 962 if (flash_node < 0) { 963 debug("Failed to find flash node\n"); 964 return -1; 965 } 966 967 if (fdtdec_read_fmap_entry(blob, flash_node, "flash", 968 &config->flash)) { 969 debug("Failed to decode flash node in chrome-ec'\n"); 970 return -1; 971 } 972 973 config->flash_erase_value = fdtdec_get_int(blob, flash_node, 974 "erase-value", -1); 975 for (node = fdt_first_subnode(blob, flash_node); node >= 0; 976 node = fdt_next_subnode(blob, node)) { 977 const char *name = fdt_get_name(blob, node, NULL); 978 enum ec_flash_region region; 979 980 if (0 == strcmp(name, "ro")) { 981 region = EC_FLASH_REGION_RO; 982 } else if (0 == strcmp(name, "rw")) { 983 region = EC_FLASH_REGION_RW; 984 } else if (0 == strcmp(name, "wp-ro")) { 985 region = EC_FLASH_REGION_WP_RO; 986 } else { 987 debug("Unknown EC flash region name '%s'\n", name); 988 return -1; 989 } 990 991 if (fdtdec_read_fmap_entry(blob, node, "reg", 992 &config->region[region])) { 993 debug("Failed to decode flash region in chrome-ec'\n"); 994 return -1; 995 } 996 } 997 998 return 0; 999 } 1000 1001 #ifdef CONFIG_CMD_CROS_EC 1002 1003 /** 1004 * Perform a flash read or write command 1005 * 1006 * @param dev CROS-EC device to read/write 1007 * @param is_write 1 do to a write, 0 to do a read 1008 * @param argc Number of arguments 1009 * @param argv Arguments (2 is region, 3 is address) 1010 * @return 0 for ok, 1 for a usage error or -ve for ec command error 1011 * (negative EC_RES_...) 1012 */ 1013 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc, 1014 char * const argv[]) 1015 { 1016 uint32_t offset, size = -1U, region_size; 1017 unsigned long addr; 1018 char *endp; 1019 int region; 1020 int ret; 1021 1022 region = cros_ec_decode_region(argc - 2, argv + 2); 1023 if (region == -1) 1024 return 1; 1025 if (argc < 4) 1026 return 1; 1027 addr = simple_strtoul(argv[3], &endp, 16); 1028 if (*argv[3] == 0 || *endp != 0) 1029 return 1; 1030 if (argc > 4) { 1031 size = simple_strtoul(argv[4], &endp, 16); 1032 if (*argv[4] == 0 || *endp != 0) 1033 return 1; 1034 } 1035 1036 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size); 1037 if (ret) { 1038 debug("%s: Could not read region info\n", __func__); 1039 return ret; 1040 } 1041 if (size == -1U) 1042 size = region_size; 1043 1044 ret = is_write ? 1045 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) : 1046 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size); 1047 if (ret) { 1048 debug("%s: Could not %s region\n", __func__, 1049 is_write ? "write" : "read"); 1050 return ret; 1051 } 1052 1053 return 0; 1054 } 1055 1056 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 1057 { 1058 struct cros_ec_dev *dev = last_dev; 1059 const char *cmd; 1060 int ret = 0; 1061 1062 if (argc < 2) 1063 return CMD_RET_USAGE; 1064 1065 cmd = argv[1]; 1066 if (0 == strcmp("init", cmd)) { 1067 ret = cros_ec_init(gd->fdt_blob, &dev); 1068 if (ret) { 1069 printf("Could not init cros_ec device (err %d)\n", ret); 1070 return 1; 1071 } 1072 return 0; 1073 } 1074 1075 /* Just use the last allocated device; there should be only one */ 1076 if (!last_dev) { 1077 printf("No CROS-EC device available\n"); 1078 return 1; 1079 } 1080 if (0 == strcmp("id", cmd)) { 1081 char id[MSG_BYTES]; 1082 1083 if (cros_ec_read_id(dev, id, sizeof(id))) { 1084 debug("%s: Could not read KBC ID\n", __func__); 1085 return 1; 1086 } 1087 printf("%s\n", id); 1088 } else if (0 == strcmp("info", cmd)) { 1089 struct ec_response_cros_ec_info info; 1090 1091 if (cros_ec_info(dev, &info)) { 1092 debug("%s: Could not read KBC info\n", __func__); 1093 return 1; 1094 } 1095 printf("rows = %u\n", info.rows); 1096 printf("cols = %u\n", info.cols); 1097 printf("switches = %#x\n", info.switches); 1098 } else if (0 == strcmp("curimage", cmd)) { 1099 enum ec_current_image image; 1100 1101 if (cros_ec_read_current_image(dev, &image)) { 1102 debug("%s: Could not read KBC image\n", __func__); 1103 return 1; 1104 } 1105 printf("%d\n", image); 1106 } else if (0 == strcmp("hash", cmd)) { 1107 struct ec_response_vboot_hash hash; 1108 int i; 1109 1110 if (cros_ec_read_hash(dev, &hash)) { 1111 debug("%s: Could not read KBC hash\n", __func__); 1112 return 1; 1113 } 1114 1115 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256) 1116 printf("type: SHA-256\n"); 1117 else 1118 printf("type: %d\n", hash.hash_type); 1119 1120 printf("offset: 0x%08x\n", hash.offset); 1121 printf("size: 0x%08x\n", hash.size); 1122 1123 printf("digest: "); 1124 for (i = 0; i < hash.digest_size; i++) 1125 printf("%02x", hash.hash_digest[i]); 1126 printf("\n"); 1127 } else if (0 == strcmp("reboot", cmd)) { 1128 int region; 1129 enum ec_reboot_cmd cmd; 1130 1131 if (argc >= 3 && !strcmp(argv[2], "cold")) 1132 cmd = EC_REBOOT_COLD; 1133 else { 1134 region = cros_ec_decode_region(argc - 2, argv + 2); 1135 if (region == EC_FLASH_REGION_RO) 1136 cmd = EC_REBOOT_JUMP_RO; 1137 else if (region == EC_FLASH_REGION_RW) 1138 cmd = EC_REBOOT_JUMP_RW; 1139 else 1140 return CMD_RET_USAGE; 1141 } 1142 1143 if (cros_ec_reboot(dev, cmd, 0)) { 1144 debug("%s: Could not reboot KBC\n", __func__); 1145 return 1; 1146 } 1147 } else if (0 == strcmp("events", cmd)) { 1148 uint32_t events; 1149 1150 if (cros_ec_get_host_events(dev, &events)) { 1151 debug("%s: Could not read host events\n", __func__); 1152 return 1; 1153 } 1154 printf("0x%08x\n", events); 1155 } else if (0 == strcmp("clrevents", cmd)) { 1156 uint32_t events = 0x7fffffff; 1157 1158 if (argc >= 3) 1159 events = simple_strtol(argv[2], NULL, 0); 1160 1161 if (cros_ec_clear_host_events(dev, events)) { 1162 debug("%s: Could not clear host events\n", __func__); 1163 return 1; 1164 } 1165 } else if (0 == strcmp("read", cmd)) { 1166 ret = do_read_write(dev, 0, argc, argv); 1167 if (ret > 0) 1168 return CMD_RET_USAGE; 1169 } else if (0 == strcmp("write", cmd)) { 1170 ret = do_read_write(dev, 1, argc, argv); 1171 if (ret > 0) 1172 return CMD_RET_USAGE; 1173 } else if (0 == strcmp("erase", cmd)) { 1174 int region = cros_ec_decode_region(argc - 2, argv + 2); 1175 uint32_t offset, size; 1176 1177 if (region == -1) 1178 return CMD_RET_USAGE; 1179 if (cros_ec_flash_offset(dev, region, &offset, &size)) { 1180 debug("%s: Could not read region info\n", __func__); 1181 ret = -1; 1182 } else { 1183 ret = cros_ec_flash_erase(dev, offset, size); 1184 if (ret) { 1185 debug("%s: Could not erase region\n", 1186 __func__); 1187 } 1188 } 1189 } else if (0 == strcmp("regioninfo", cmd)) { 1190 int region = cros_ec_decode_region(argc - 2, argv + 2); 1191 uint32_t offset, size; 1192 1193 if (region == -1) 1194 return CMD_RET_USAGE; 1195 ret = cros_ec_flash_offset(dev, region, &offset, &size); 1196 if (ret) { 1197 debug("%s: Could not read region info\n", __func__); 1198 } else { 1199 printf("Region: %s\n", region == EC_FLASH_REGION_RO ? 1200 "RO" : "RW"); 1201 printf("Offset: %x\n", offset); 1202 printf("Size: %x\n", size); 1203 } 1204 } else if (0 == strcmp("vbnvcontext", cmd)) { 1205 uint8_t block[EC_VBNV_BLOCK_SIZE]; 1206 char buf[3]; 1207 int i, len; 1208 unsigned long result; 1209 1210 if (argc <= 2) { 1211 ret = cros_ec_read_vbnvcontext(dev, block); 1212 if (!ret) { 1213 printf("vbnv_block: "); 1214 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) 1215 printf("%02x", block[i]); 1216 putc('\n'); 1217 } 1218 } else { 1219 /* 1220 * TODO(clchiou): Move this to a utility function as 1221 * cmd_spi might want to call it. 1222 */ 1223 memset(block, 0, EC_VBNV_BLOCK_SIZE); 1224 len = strlen(argv[2]); 1225 buf[2] = '\0'; 1226 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) { 1227 if (i * 2 >= len) 1228 break; 1229 buf[0] = argv[2][i * 2]; 1230 if (i * 2 + 1 >= len) 1231 buf[1] = '0'; 1232 else 1233 buf[1] = argv[2][i * 2 + 1]; 1234 strict_strtoul(buf, 16, &result); 1235 block[i] = result; 1236 } 1237 ret = cros_ec_write_vbnvcontext(dev, block); 1238 } 1239 if (ret) { 1240 debug("%s: Could not %s VbNvContext\n", __func__, 1241 argc <= 2 ? "read" : "write"); 1242 } 1243 } else if (0 == strcmp("test", cmd)) { 1244 int result = cros_ec_test(dev); 1245 1246 if (result) 1247 printf("Test failed with error %d\n", result); 1248 else 1249 puts("Test passed\n"); 1250 } else if (0 == strcmp("version", cmd)) { 1251 struct ec_response_get_version *p; 1252 char *build_string; 1253 1254 ret = cros_ec_read_version(dev, &p); 1255 if (!ret) { 1256 /* Print versions */ 1257 printf("RO version: %1.*s\n", 1258 sizeof(p->version_string_ro), 1259 p->version_string_ro); 1260 printf("RW version: %1.*s\n", 1261 sizeof(p->version_string_rw), 1262 p->version_string_rw); 1263 printf("Firmware copy: %s\n", 1264 (p->current_image < 1265 ARRAY_SIZE(ec_current_image_name) ? 1266 ec_current_image_name[p->current_image] : 1267 "?")); 1268 ret = cros_ec_read_build_info(dev, &build_string); 1269 if (!ret) 1270 printf("Build info: %s\n", build_string); 1271 } 1272 } else if (0 == strcmp("ldo", cmd)) { 1273 uint8_t index, state; 1274 char *endp; 1275 1276 if (argc < 3) 1277 return CMD_RET_USAGE; 1278 index = simple_strtoul(argv[2], &endp, 10); 1279 if (*argv[2] == 0 || *endp != 0) 1280 return CMD_RET_USAGE; 1281 if (argc > 3) { 1282 state = simple_strtoul(argv[3], &endp, 10); 1283 if (*argv[3] == 0 || *endp != 0) 1284 return CMD_RET_USAGE; 1285 ret = cros_ec_set_ldo(dev, index, state); 1286 } else { 1287 ret = cros_ec_get_ldo(dev, index, &state); 1288 if (!ret) { 1289 printf("LDO%d: %s\n", index, 1290 state == EC_LDO_STATE_ON ? 1291 "on" : "off"); 1292 } 1293 } 1294 1295 if (ret) { 1296 debug("%s: Could not access LDO%d\n", __func__, index); 1297 return ret; 1298 } 1299 } else { 1300 return CMD_RET_USAGE; 1301 } 1302 1303 if (ret < 0) { 1304 printf("Error: CROS-EC command failed (error %d)\n", ret); 1305 ret = 1; 1306 } 1307 1308 return ret; 1309 } 1310 1311 U_BOOT_CMD( 1312 crosec, 5, 1, do_cros_ec, 1313 "CROS-EC utility command", 1314 "init Re-init CROS-EC (done on startup automatically)\n" 1315 "crosec id Read CROS-EC ID\n" 1316 "crosec info Read CROS-EC info\n" 1317 "crosec curimage Read CROS-EC current image\n" 1318 "crosec hash Read CROS-EC hash\n" 1319 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" 1320 "crosec events Read CROS-EC host events\n" 1321 "crosec clrevents [mask] Clear CROS-EC host events\n" 1322 "crosec regioninfo <ro|rw> Read image info\n" 1323 "crosec erase <ro|rw> Erase EC image\n" 1324 "crosec read <ro|rw> <addr> [<size>] Read EC image\n" 1325 "crosec write <ro|rw> <addr> [<size>] Write EC image\n" 1326 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" 1327 "crosec ldo <idx> [<state>] Switch/Read LDO state\n" 1328 "crosec test run tests on cros_ec\n" 1329 "crosec version Read CROS-EC version" 1330 ); 1331 #endif 1332