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