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