2 * Chromium OS cros_ec driver
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
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
25 #include <asm/errno.h>
27 #include <asm-generic/gpio.h>
28 #include <dm/device-internal.h>
29 #include <dm/uclass-internal.h>
32 #define debug_trace(fmt, b...) debug(fmt, #b)
34 #define debug_trace(fmt, b...)
38 /* Timeout waiting for a flash erase command to complete */
39 CROS_EC_CMD_TIMEOUT_MS = 5000,
40 /* Timeout waiting for a synchronous hash to be recomputed */
41 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
44 #ifndef CONFIG_DM_CROS_EC
45 static struct cros_ec_dev static_dev, *last_dev;
48 DECLARE_GLOBAL_DATA_PTR;
50 /* Note: depends on enum ec_current_image */
51 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
53 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
60 printf("cmd=%#x: ", cmd);
61 for (i = 0; i < len; i++)
62 printf("%02x ", data[i]);
68 * Calculate a simple 8-bit checksum of a data block
70 * @param data Data block to checksum
71 * @param size Size of data block in bytes
72 * @return checksum value (0 to 255)
74 int cros_ec_calc_checksum(const uint8_t *data, int size)
78 for (i = csum = 0; i < size; i++)
84 * Create a request packet for protocol version 3.
86 * The packet is stored in the device's internal output buffer.
88 * @param dev CROS-EC device
89 * @param cmd Command to send (EC_CMD_...)
90 * @param cmd_version Version of command to send (EC_VER_...)
91 * @param dout Output data (may be NULL If dout_len=0)
92 * @param dout_len Size of output data in bytes
93 * @return packet size in bytes, or <0 if error.
95 static int create_proto3_request(struct cros_ec_dev *dev,
96 int cmd, int cmd_version,
97 const void *dout, int dout_len)
99 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
100 int out_bytes = dout_len + sizeof(*rq);
102 /* Fail if output size is too big */
103 if (out_bytes > (int)sizeof(dev->dout)) {
104 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
105 return -EC_RES_REQUEST_TRUNCATED;
108 /* Fill in request packet */
109 rq->struct_version = EC_HOST_REQUEST_VERSION;
112 rq->command_version = cmd_version;
114 rq->data_len = dout_len;
116 /* Copy data after header */
117 memcpy(rq + 1, dout, dout_len);
119 /* Write checksum field so the entire packet sums to 0 */
120 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
122 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
124 /* Return size of request packet */
129 * Prepare the device to receive a protocol version 3 response.
131 * @param dev CROS-EC device
132 * @param din_len Maximum size of response in bytes
133 * @return maximum expected number of bytes in response, or <0 if error.
135 static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
137 int in_bytes = din_len + sizeof(struct ec_host_response);
139 /* Fail if input size is too big */
140 if (in_bytes > (int)sizeof(dev->din)) {
141 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
142 return -EC_RES_RESPONSE_TOO_BIG;
145 /* Return expected size of response packet */
150 * Handle a protocol version 3 response packet.
152 * The packet must already be stored in the device's internal input buffer.
154 * @param dev CROS-EC device
155 * @param dinp Returns pointer to response data
156 * @param din_len Maximum size of response in bytes
157 * @return number of bytes of response data, or <0 if error. Note that error
158 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
161 static int handle_proto3_response(struct cros_ec_dev *dev,
162 uint8_t **dinp, int din_len)
164 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
168 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
170 /* Check input data */
171 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
172 debug("%s: EC response version mismatch\n", __func__);
173 return -EC_RES_INVALID_RESPONSE;
177 debug("%s: EC response reserved != 0\n", __func__);
178 return -EC_RES_INVALID_RESPONSE;
181 if (rs->data_len > din_len) {
182 debug("%s: EC returned too much data\n", __func__);
183 return -EC_RES_RESPONSE_TOO_BIG;
186 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
188 /* Update in_bytes to actual data size */
189 in_bytes = sizeof(*rs) + rs->data_len;
191 /* Verify checksum */
192 csum = cros_ec_calc_checksum(dev->din, in_bytes);
194 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
196 return -EC_RES_INVALID_CHECKSUM;
199 /* Return error result, if any */
201 return -(int)rs->result;
203 /* If we're still here, set response data pointer and return length */
204 *dinp = (uint8_t *)(rs + 1);
209 static int send_command_proto3(struct cros_ec_dev *dev,
210 int cmd, int cmd_version,
211 const void *dout, int dout_len,
212 uint8_t **dinp, int din_len)
214 #ifdef CONFIG_DM_CROS_EC
215 struct dm_cros_ec_ops *ops;
217 int out_bytes, in_bytes;
220 /* Create request packet */
221 out_bytes = create_proto3_request(dev, cmd, cmd_version,
226 /* Prepare response buffer */
227 in_bytes = prepare_proto3_response_buffer(dev, din_len);
231 #ifdef CONFIG_DM_CROS_EC
232 ops = dm_cros_ec_get_ops(dev->dev);
233 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
235 switch (dev->interface) {
236 #ifdef CONFIG_CROS_EC_SPI
238 rv = cros_ec_spi_packet(dev, out_bytes, in_bytes);
241 #ifdef CONFIG_CROS_EC_SANDBOX
242 case CROS_EC_IF_SANDBOX:
243 rv = cros_ec_sandbox_packet(dev, out_bytes, in_bytes);
246 case CROS_EC_IF_NONE:
247 /* TODO: support protocol 3 for LPC, I2C; for now fall through */
249 debug("%s: Unsupported interface\n", __func__);
256 /* Process the response */
257 return handle_proto3_response(dev, dinp, din_len);
260 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
261 const void *dout, int dout_len,
262 uint8_t **dinp, int din_len)
264 #ifdef CONFIG_DM_CROS_EC
265 struct dm_cros_ec_ops *ops;
269 /* Handle protocol version 3 support */
270 if (dev->protocol_version == 3) {
271 return send_command_proto3(dev, cmd, cmd_version,
272 dout, dout_len, dinp, din_len);
275 #ifdef CONFIG_DM_CROS_EC
276 ops = dm_cros_ec_get_ops(dev->dev);
277 ret = ops->command(dev->dev, cmd, cmd_version,
278 (const uint8_t *)dout, dout_len, dinp, din_len);
280 switch (dev->interface) {
281 #ifdef CONFIG_CROS_EC_SPI
283 ret = cros_ec_spi_command(dev, cmd, cmd_version,
284 (const uint8_t *)dout, dout_len,
288 #ifdef CONFIG_CROS_EC_I2C
290 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
291 (const uint8_t *)dout, dout_len,
295 #ifdef CONFIG_CROS_EC_LPC
297 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
298 (const uint8_t *)dout, dout_len,
302 case CROS_EC_IF_NONE:
312 * Send a command to the CROS-EC device and return the reply.
314 * The device's internal input/output buffers are used.
316 * @param dev CROS-EC device
317 * @param cmd Command to send (EC_CMD_...)
318 * @param cmd_version Version of command to send (EC_VER_...)
319 * @param dout Output data (may be NULL If dout_len=0)
320 * @param dout_len Size of output data in bytes
321 * @param dinp Response data (may be NULL If din_len=0).
322 * If not NULL, it will be updated to point to the data
323 * and will always be double word aligned (64-bits)
324 * @param din_len Maximum size of response in bytes
325 * @return number of bytes in response, or -ve on error
327 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
328 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
334 len = send_command(dev, cmd, cmd_version, dout, dout_len,
337 /* If the command doesn't complete, wait a while */
338 if (len == -EC_RES_IN_PROGRESS) {
339 struct ec_response_get_comms_status *resp = NULL;
342 /* Wait for command to complete */
343 start = get_timer(0);
347 mdelay(50); /* Insert some reasonable delay */
348 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
350 (uint8_t **)&resp, sizeof(*resp));
354 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
355 debug("%s: Command %#02x timeout\n",
357 return -EC_RES_TIMEOUT;
359 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
361 /* OK it completed, so read the status response */
362 /* not sure why it was 0 for the last argument */
363 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
364 NULL, 0, &din, din_len);
367 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
368 dinp ? *dinp : NULL);
370 /* If we have any data to return, it must be 64bit-aligned */
371 assert(len <= 0 || !((uintptr_t)din & 7));
379 * Send a command to the CROS-EC device and return the reply.
381 * The device's internal input/output buffers are used.
383 * @param dev CROS-EC device
384 * @param cmd Command to send (EC_CMD_...)
385 * @param cmd_version Version of command to send (EC_VER_...)
386 * @param dout Output data (may be NULL If dout_len=0)
387 * @param dout_len Size of output data in bytes
388 * @param din Response data (may be NULL If din_len=0).
389 * It not NULL, it is a place for ec_command() to copy the
391 * @param din_len Maximum size of response in bytes
392 * @return number of bytes in response, or -ve on error
394 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
395 const void *dout, int dout_len,
396 void *din, int din_len)
401 assert((din_len == 0) || din);
402 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
403 &in_buffer, din_len);
406 * If we were asked to put it somewhere, do so, otherwise just
407 * disregard the result.
409 if (din && in_buffer) {
410 assert(len <= din_len);
411 memmove(din, in_buffer, len);
417 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
419 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
420 sizeof(scan->data)) != sizeof(scan->data))
426 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
428 struct ec_response_get_version *r;
430 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
431 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
434 if (maxlen > (int)sizeof(r->version_string_ro))
435 maxlen = sizeof(r->version_string_ro);
437 switch (r->current_image) {
439 memcpy(id, r->version_string_ro, maxlen);
442 memcpy(id, r->version_string_rw, maxlen);
448 id[maxlen - 1] = '\0';
452 int cros_ec_read_version(struct cros_ec_dev *dev,
453 struct ec_response_get_version **versionp)
455 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
456 (uint8_t **)versionp, sizeof(**versionp))
457 != sizeof(**versionp))
463 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
465 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
466 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
472 int cros_ec_read_current_image(struct cros_ec_dev *dev,
473 enum ec_current_image *image)
475 struct ec_response_get_version *r;
477 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
478 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
481 *image = r->current_image;
485 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
486 struct ec_response_vboot_hash *hash)
488 struct ec_params_vboot_hash p;
491 start = get_timer(0);
492 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
493 mdelay(50); /* Insert some reasonable delay */
495 p.cmd = EC_VBOOT_HASH_GET;
496 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
497 hash, sizeof(*hash)) < 0)
500 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
501 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
502 return -EC_RES_TIMEOUT;
509 int cros_ec_read_hash(struct cros_ec_dev *dev,
510 struct ec_response_vboot_hash *hash)
512 struct ec_params_vboot_hash p;
515 p.cmd = EC_VBOOT_HASH_GET;
516 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
517 hash, sizeof(*hash)) < 0)
520 /* If the EC is busy calculating the hash, fidget until it's done. */
521 rv = cros_ec_wait_on_hash_done(dev, hash);
525 /* If the hash is valid, we're done. Otherwise, we have to kick it off
526 * again and wait for it to complete. Note that we explicitly assume
527 * that hashing zero bytes is always wrong, even though that would
528 * produce a valid hash value. */
529 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
532 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
533 __func__, hash->status, hash->size);
535 p.cmd = EC_VBOOT_HASH_START;
536 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
538 p.offset = EC_VBOOT_HASH_OFFSET_RW;
540 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
541 hash, sizeof(*hash)) < 0)
544 rv = cros_ec_wait_on_hash_done(dev, hash);
548 debug("%s: hash done\n", __func__);
553 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
555 struct ec_params_vboot_hash p;
556 struct ec_response_vboot_hash *hash;
558 /* We don't have an explict command for the EC to discard its current
559 * hash value, so we'll just tell it to calculate one that we know is
560 * wrong (we claim that hashing zero bytes is always invalid).
562 p.cmd = EC_VBOOT_HASH_RECALC;
563 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
568 debug("%s:\n", __func__);
570 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
571 (uint8_t **)&hash, sizeof(*hash)) < 0)
574 /* No need to wait for it to finish */
578 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
581 struct ec_params_reboot_ec p;
586 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
590 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
592 * EC reboot will take place immediately so delay to allow it
593 * to complete. Note that some reboot types (EC_REBOOT_COLD)
594 * will reboot the AP as well, in which case we won't actually
598 * TODO(rspangler@chromium.org): Would be nice if we had a
599 * better way to determine when the reboot is complete. Could
600 * we poll a memory-mapped LPC value?
608 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
610 /* no interrupt support : always poll */
611 if (!dm_gpio_is_valid(&dev->ec_int))
614 return dm_gpio_get_value(&dev->ec_int);
617 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
619 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
620 sizeof(*info)) != sizeof(*info))
626 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
628 struct ec_response_host_event_mask *resp;
631 * Use the B copy of the event flags, because the main copy is already
634 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
635 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
638 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
641 *events_ptr = resp->mask;
645 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
647 struct ec_params_host_event_mask params;
649 params.mask = events;
652 * Use the B copy of the event flags, so it affects the data returned
653 * by cros_ec_get_host_events().
655 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
656 ¶ms, sizeof(params), NULL, 0) < 0)
662 int cros_ec_flash_protect(struct cros_ec_dev *dev,
663 uint32_t set_mask, uint32_t set_flags,
664 struct ec_response_flash_protect *resp)
666 struct ec_params_flash_protect params;
668 params.mask = set_mask;
669 params.flags = set_flags;
671 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
672 ¶ms, sizeof(params),
673 resp, sizeof(*resp)) != sizeof(*resp))
679 static int cros_ec_check_version(struct cros_ec_dev *dev)
681 struct ec_params_hello req;
682 struct ec_response_hello *resp;
684 #ifdef CONFIG_DM_CROS_EC
685 struct dm_cros_ec_ops *ops;
688 ops = dm_cros_ec_get_ops(dev->dev);
689 if (ops->check_version) {
690 ret = ops->check_version(dev->dev);
695 #ifdef CONFIG_CROS_EC_LPC
696 /* LPC has its own way of doing this */
697 if (dev->interface == CROS_EC_IF_LPC)
698 return cros_ec_lpc_check_version(dev);
703 * TODO(sjg@chromium.org).
704 * There is a strange oddity here with the EC. We could just ignore
705 * the response, i.e. pass the last two parameters as NULL and 0.
706 * In this case we won't read back very many bytes from the EC.
707 * On the I2C bus the EC gets upset about this and will try to send
708 * the bytes anyway. This means that we will have to wait for that
709 * to complete before continuing with a new EC command.
711 * This problem is probably unique to the I2C bus.
713 * So for now, just read all the data anyway.
716 /* Try sending a version 3 packet */
717 dev->protocol_version = 3;
719 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
720 (uint8_t **)&resp, sizeof(*resp)) > 0) {
724 /* Try sending a version 2 packet */
725 dev->protocol_version = 2;
726 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
727 (uint8_t **)&resp, sizeof(*resp)) > 0) {
732 * Fail if we're still here, since the EC doesn't understand any
733 * protcol version we speak. Version 1 interface without command
734 * version is no longer supported, and we don't know about any new
737 dev->protocol_version = 0;
738 printf("%s: ERROR: old EC interface not supported\n", __func__);
742 int cros_ec_test(struct cros_ec_dev *dev)
744 struct ec_params_hello req;
745 struct ec_response_hello *resp;
747 req.in_data = 0x12345678;
748 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
749 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
750 printf("ec_command_inptr() returned error\n");
753 if (resp->out_data != req.in_data + 0x01020304) {
754 printf("Received invalid handshake %x\n", resp->out_data);
761 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
762 uint32_t *offset, uint32_t *size)
764 struct ec_params_flash_region_info p;
765 struct ec_response_flash_region_info *r;
769 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
770 EC_VER_FLASH_REGION_INFO,
771 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
772 if (ret != sizeof(*r))
783 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
785 struct ec_params_flash_erase p;
789 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
794 * Write a single block to the flash
796 * Write a block of data to the EC flash. The size must not exceed the flash
797 * write block size which you can obtain from cros_ec_flash_write_burst_size().
799 * The offset starts at 0. You can obtain the region information from
800 * cros_ec_flash_offset() to find out where to write for a particular region.
802 * Attempting to write to the region where the EC is currently running from
803 * will result in an error.
805 * @param dev CROS-EC device
806 * @param data Pointer to data buffer to write
807 * @param offset Offset within flash to write to.
808 * @param size Number of bytes to write
809 * @return 0 if ok, -1 on error
811 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
812 const uint8_t *data, uint32_t offset, uint32_t size)
814 struct ec_params_flash_write p;
818 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
819 memcpy(&p + 1, data, p.size);
821 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
822 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
826 * Return optimal flash write burst size
828 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
830 return EC_FLASH_WRITE_VER0_SIZE;
834 * Check if a block of data is erased (all 0xff)
836 * This function is useful when dealing with flash, for checking whether a
837 * data block is erased and thus does not need to be programmed.
839 * @param data Pointer to data to check (must be word-aligned)
840 * @param size Number of bytes to check (must be word-aligned)
841 * @return 0 if erased, non-zero if any word is not erased
843 static int cros_ec_data_is_erased(const uint32_t *data, int size)
846 size /= sizeof(uint32_t);
847 for (; size > 0; size -= 4, data++)
854 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
855 uint32_t offset, uint32_t size)
857 uint32_t burst = cros_ec_flash_write_burst_size(dev);
862 * TODO: round up to the nearest multiple of write size. Can get away
863 * without that on link right now because its write size is 4 bytes.
866 for (off = offset; off < end; off += burst, data += burst) {
869 /* If the data is empty, there is no point in programming it */
870 todo = min(end - off, burst);
871 if (dev->optimise_flash_write &&
872 cros_ec_data_is_erased((uint32_t *)data, todo))
875 ret = cros_ec_flash_write_block(dev, data, off, todo);
884 * Read a single block from the flash
886 * Read a block of data from the EC flash. The size must not exceed the flash
887 * write block size which you can obtain from cros_ec_flash_write_burst_size().
889 * The offset starts at 0. You can obtain the region information from
890 * cros_ec_flash_offset() to find out where to read for a particular region.
892 * @param dev CROS-EC device
893 * @param data Pointer to data buffer to read into
894 * @param offset Offset within flash to read from
895 * @param size Number of bytes to read
896 * @return 0 if ok, -1 on error
898 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
899 uint32_t offset, uint32_t size)
901 struct ec_params_flash_read p;
906 return ec_command(dev, EC_CMD_FLASH_READ, 0,
907 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
910 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
913 uint32_t burst = cros_ec_flash_write_burst_size(dev);
918 for (off = offset; off < end; off += burst, data += burst) {
919 ret = cros_ec_flash_read_block(dev, data, off,
920 min(end - off, burst));
928 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
929 const uint8_t *image, int image_size)
931 uint32_t rw_offset, rw_size;
934 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
936 if (image_size > (int)rw_size)
939 /* Invalidate the existing hash, just in case the AP reboots
940 * unexpectedly during the update. If that happened, the EC RW firmware
941 * would be invalid, but the EC would still have the original hash.
943 ret = cros_ec_invalidate_hash(dev);
948 * Erase the entire RW section, so that the EC doesn't see any garbage
949 * past the new image if it's smaller than the current image.
951 * TODO: could optimize this to erase just the current image, since
952 * presumably everything past that is 0xff's. But would still need to
953 * round up to the nearest multiple of erase size.
955 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
959 /* Write the image */
960 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
967 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
969 struct ec_params_vbnvcontext p;
972 p.op = EC_VBNV_CONTEXT_OP_READ;
974 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
975 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
976 if (len < EC_VBNV_BLOCK_SIZE)
982 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
984 struct ec_params_vbnvcontext p;
987 p.op = EC_VBNV_CONTEXT_OP_WRITE;
988 memcpy(p.block, block, sizeof(p.block));
990 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
991 &p, sizeof(p), NULL, 0);
998 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
1000 struct ec_params_ldo_set params;
1002 params.index = index;
1003 params.state = state;
1005 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
1006 ¶ms, sizeof(params),
1013 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
1015 struct ec_params_ldo_get params;
1016 struct ec_response_ldo_get *resp;
1018 params.index = index;
1020 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
1021 ¶ms, sizeof(params),
1022 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1025 *state = resp->state;
1030 #ifndef CONFIG_DM_CROS_EC
1032 * Decode EC interface details from the device tree and allocate a suitable
1035 * @param blob Device tree blob
1036 * @param node Node to decode from
1037 * @param devp Returns a pointer to the new allocated device
1038 * @return 0 if ok, -1 on error
1040 static int cros_ec_decode_fdt(const void *blob, int node,
1041 struct cros_ec_dev **devp)
1043 enum fdt_compat_id compat;
1044 struct cros_ec_dev *dev;
1047 /* See what type of parent we are inside (this is expensive) */
1048 parent = fdt_parent_offset(blob, node);
1050 debug("%s: Cannot find node parent\n", __func__);
1056 dev->parent_node = parent;
1058 compat = fdtdec_lookup(blob, parent);
1060 #ifdef CONFIG_CROS_EC_SPI
1061 case COMPAT_SAMSUNG_EXYNOS_SPI:
1062 dev->interface = CROS_EC_IF_SPI;
1063 if (cros_ec_spi_decode_fdt(dev, blob))
1067 #ifdef CONFIG_CROS_EC_I2C
1068 case COMPAT_SAMSUNG_S3C2440_I2C:
1069 dev->interface = CROS_EC_IF_I2C;
1070 if (cros_ec_i2c_decode_fdt(dev, blob))
1074 #ifdef CONFIG_CROS_EC_LPC
1075 case COMPAT_INTEL_LPC:
1076 dev->interface = CROS_EC_IF_LPC;
1079 #ifdef CONFIG_CROS_EC_SANDBOX
1080 case COMPAT_SANDBOX_HOST_EMULATION:
1081 dev->interface = CROS_EC_IF_SANDBOX;
1085 debug("%s: Unknown compat id %d\n", __func__, compat);
1089 gpio_request_by_name_nodev(blob, node, "ec-interrupt", 0, &dev->ec_int,
1091 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
1092 "optimise-flash-write");
1099 #ifdef CONFIG_DM_CROS_EC
1100 int cros_ec_register(struct udevice *dev)
1102 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1103 const void *blob = gd->fdt_blob;
1104 int node = dev->of_offset;
1108 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1110 cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
1111 "optimise-flash-write");
1113 if (cros_ec_check_version(cdev)) {
1114 debug("%s: Could not detect CROS-EC version\n", __func__);
1115 return -CROS_EC_ERR_CHECK_VERSION;
1118 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1119 debug("%s: Could not read KBC ID\n", __func__);
1120 return -CROS_EC_ERR_READ_ID;
1123 /* Remember this device for use by the cros_ec command */
1124 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1129 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
1131 struct cros_ec_dev *dev;
1133 #ifdef CONFIG_DM_CROS_EC
1134 struct udevice *udev;
1137 ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
1139 device_remove(udev);
1140 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1143 dev = dev_get_uclass_priv(udev);
1150 node = fdtdec_next_compatible(blob, node,
1151 COMPAT_GOOGLE_CROS_EC);
1153 debug("%s: Node not found\n", __func__);
1156 } while (!fdtdec_get_is_enabled(blob, node));
1158 if (cros_ec_decode_fdt(blob, node, &dev)) {
1159 debug("%s: Failed to decode device.\n", __func__);
1160 return -CROS_EC_ERR_FDT_DECODE;
1163 switch (dev->interface) {
1164 #ifdef CONFIG_CROS_EC_SPI
1165 case CROS_EC_IF_SPI:
1166 if (cros_ec_spi_init(dev, blob)) {
1167 debug("%s: Could not setup SPI interface\n", __func__);
1168 return -CROS_EC_ERR_DEV_INIT;
1172 #ifdef CONFIG_CROS_EC_I2C
1173 case CROS_EC_IF_I2C:
1174 if (cros_ec_i2c_init(dev, blob))
1175 return -CROS_EC_ERR_DEV_INIT;
1178 #ifdef CONFIG_CROS_EC_LPC
1179 case CROS_EC_IF_LPC:
1180 if (cros_ec_lpc_init(dev, blob))
1181 return -CROS_EC_ERR_DEV_INIT;
1184 #ifdef CONFIG_CROS_EC_SANDBOX
1185 case CROS_EC_IF_SANDBOX:
1186 if (cros_ec_sandbox_init(dev, blob))
1187 return -CROS_EC_ERR_DEV_INIT;
1190 case CROS_EC_IF_NONE:
1196 if (cros_ec_check_version(dev)) {
1197 debug("%s: Could not detect CROS-EC version\n", __func__);
1198 return -CROS_EC_ERR_CHECK_VERSION;
1201 if (cros_ec_read_id(dev, id, sizeof(id))) {
1202 debug("%s: Could not read KBC ID\n", __func__);
1203 return -CROS_EC_ERR_READ_ID;
1206 /* Remember this device for use by the cros_ec command */
1208 #ifndef CONFIG_DM_CROS_EC
1211 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1217 int cros_ec_decode_region(int argc, char * const argv[])
1220 if (0 == strcmp(*argv, "rw"))
1221 return EC_FLASH_REGION_RW;
1222 else if (0 == strcmp(*argv, "ro"))
1223 return EC_FLASH_REGION_RO;
1225 debug("%s: Invalid region '%s'\n", __func__, *argv);
1227 debug("%s: Missing region parameter\n", __func__);
1233 int cros_ec_decode_ec_flash(const void *blob, int node,
1234 struct fdt_cros_ec *config)
1238 flash_node = fdt_subnode_offset(blob, node, "flash");
1239 if (flash_node < 0) {
1240 debug("Failed to find flash node\n");
1244 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1246 debug("Failed to decode flash node in chrome-ec'\n");
1250 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1252 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1253 node = fdt_next_subnode(blob, node)) {
1254 const char *name = fdt_get_name(blob, node, NULL);
1255 enum ec_flash_region region;
1257 if (0 == strcmp(name, "ro")) {
1258 region = EC_FLASH_REGION_RO;
1259 } else if (0 == strcmp(name, "rw")) {
1260 region = EC_FLASH_REGION_RW;
1261 } else if (0 == strcmp(name, "wp-ro")) {
1262 region = EC_FLASH_REGION_WP_RO;
1264 debug("Unknown EC flash region name '%s'\n", name);
1268 if (fdtdec_read_fmap_entry(blob, node, "reg",
1269 &config->region[region])) {
1270 debug("Failed to decode flash region in chrome-ec'\n");
1278 int cros_ec_i2c_xfer(struct cros_ec_dev *dev, uchar chip, uint addr,
1279 int alen, uchar *buffer, int len, int is_read)
1282 struct ec_params_i2c_passthru p;
1283 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1286 struct ec_response_i2c_passthru r;
1287 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1289 struct ec_params_i2c_passthru *p = ¶ms.p;
1290 struct ec_response_i2c_passthru *r = &response.r;
1291 struct ec_params_i2c_passthru_msg *msg = p->msg;
1293 int read_len, write_len;
1300 printf("Unsupported address length %d\n", alen);
1309 write_len = alen + len;
1313 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1314 if (size + write_len > sizeof(params)) {
1315 puts("Params too large for buffer\n");
1318 if (sizeof(*r) + read_len > sizeof(response)) {
1319 puts("Read length too big for buffer\n");
1323 /* Create a message to write the register address and optional data */
1324 pdata = (uint8_t *)p + size;
1325 msg->addr_flags = chip;
1326 msg->len = write_len;
1329 memcpy(pdata + 1, buffer, len);
1333 msg->addr_flags = chip | EC_I2C_FLAG_READ;
1334 msg->len = read_len;
1337 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, size + write_len,
1338 r, sizeof(*r) + read_len);
1342 /* Parse response */
1343 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1344 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1348 if (rv < sizeof(*r) + read_len) {
1349 puts("Truncated read response\n");
1354 memcpy(buffer, r->data, read_len);
1359 #ifdef CONFIG_CMD_CROS_EC
1362 * Perform a flash read or write command
1364 * @param dev CROS-EC device to read/write
1365 * @param is_write 1 do to a write, 0 to do a read
1366 * @param argc Number of arguments
1367 * @param argv Arguments (2 is region, 3 is address)
1368 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1369 * (negative EC_RES_...)
1371 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1372 char * const argv[])
1374 uint32_t offset, size = -1U, region_size;
1380 region = cros_ec_decode_region(argc - 2, argv + 2);
1385 addr = simple_strtoul(argv[3], &endp, 16);
1386 if (*argv[3] == 0 || *endp != 0)
1389 size = simple_strtoul(argv[4], &endp, 16);
1390 if (*argv[4] == 0 || *endp != 0)
1394 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
1396 debug("%s: Could not read region info\n", __func__);
1403 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1404 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1406 debug("%s: Could not %s region\n", __func__,
1407 is_write ? "write" : "read");
1415 * get_alen() - Small parser helper function to get address length
1417 * Returns the address length.
1419 static uint get_alen(char *arg)
1425 for (j = 0; j < 8; j++) {
1426 if (arg[j] == '.') {
1427 alen = arg[j+1] - '0';
1429 } else if (arg[j] == '\0') {
1436 #define DISP_LINE_LEN 16
1439 * TODO(sjg@chromium.org): This code copied almost verbatim from cmd_i2c.c
1440 * so we can remove it later.
1442 static int cros_ec_i2c_md(struct cros_ec_dev *dev, int flag, int argc,
1443 char * const argv[])
1446 uint addr, alen, length = 0x10;
1447 int j, nbytes, linebytes;
1450 return CMD_RET_USAGE;
1452 if (1 || (flag & CMD_FLAG_REPEAT) == 0) {
1454 * New command specified.
1460 chip = simple_strtoul(argv[0], NULL, 16);
1463 * I2C data address within the chip. This can be 1 or
1464 * 2 bytes long. Some day it might be 3 bytes long :-).
1466 addr = simple_strtoul(argv[1], NULL, 16);
1467 alen = get_alen(argv[1]);
1469 return CMD_RET_USAGE;
1472 * If another parameter, it is the length to display.
1473 * Length is the number of objects, not number of bytes.
1476 length = simple_strtoul(argv[2], NULL, 16);
1482 * We buffer all read data, so we can make sure data is read only
1487 unsigned char linebuf[DISP_LINE_LEN];
1490 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
1492 if (cros_ec_i2c_xfer(dev, chip, addr, alen, linebuf, linebytes,
1494 puts("Error reading the chip.\n");
1496 printf("%04x:", addr);
1498 for (j = 0; j < linebytes; j++) {
1499 printf(" %02x", *cp++);
1504 for (j = 0; j < linebytes; j++) {
1505 if ((*cp < 0x20) || (*cp > 0x7e))
1513 nbytes -= linebytes;
1514 } while (nbytes > 0);
1519 static int cros_ec_i2c_mw(struct cros_ec_dev *dev, int flag, int argc,
1520 char * const argv[])
1528 if ((argc < 3) || (argc > 4))
1529 return CMD_RET_USAGE;
1532 * Chip is always specified.
1534 chip = simple_strtoul(argv[0], NULL, 16);
1537 * Address is always specified.
1539 addr = simple_strtoul(argv[1], NULL, 16);
1540 alen = get_alen(argv[1]);
1542 return CMD_RET_USAGE;
1545 * Value to write is always specified.
1547 byte = simple_strtoul(argv[2], NULL, 16);
1553 count = simple_strtoul(argv[3], NULL, 16);
1557 while (count-- > 0) {
1558 if (cros_ec_i2c_xfer(dev, chip, addr++, alen, &byte, 1, 0))
1559 puts("Error writing the chip.\n");
1561 * Wait for the write to complete. The write can take
1562 * up to 10mSec (we allow a little more time).
1565 * No write delay with FRAM devices.
1567 #if !defined(CONFIG_SYS_I2C_FRAM)
1575 /* Temporary code until we have driver model and can use the i2c command */
1576 static int cros_ec_i2c_passthrough(struct cros_ec_dev *dev, int flag,
1577 int argc, char * const argv[])
1582 return CMD_RET_USAGE;
1585 if (0 == strcmp("md", cmd))
1586 cros_ec_i2c_md(dev, flag, argc, argv);
1587 else if (0 == strcmp("mw", cmd))
1588 cros_ec_i2c_mw(dev, flag, argc, argv);
1590 return CMD_RET_USAGE;
1595 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1597 struct cros_ec_dev *dev;
1598 #ifdef CONFIG_DM_CROS_EC
1599 struct udevice *udev;
1605 return CMD_RET_USAGE;
1608 if (0 == strcmp("init", cmd)) {
1609 #ifdef CONFIG_DM_CROS_EC
1610 /* Remove any existing device */
1611 ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
1613 device_remove(udev);
1614 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1616 ret = cros_ec_init(gd->fdt_blob, &dev);
1619 printf("Could not init cros_ec device (err %d)\n", ret);
1625 #ifdef CONFIG_DM_CROS_EC
1626 ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
1628 printf("Cannot get cros-ec device (err=%d)\n", ret);
1631 dev = dev_get_uclass_priv(udev);
1633 /* Just use the last allocated device; there should be only one */
1635 printf("No CROS-EC device available\n");
1640 if (0 == strcmp("id", cmd)) {
1643 if (cros_ec_read_id(dev, id, sizeof(id))) {
1644 debug("%s: Could not read KBC ID\n", __func__);
1648 } else if (0 == strcmp("info", cmd)) {
1649 struct ec_response_mkbp_info info;
1651 if (cros_ec_info(dev, &info)) {
1652 debug("%s: Could not read KBC info\n", __func__);
1655 printf("rows = %u\n", info.rows);
1656 printf("cols = %u\n", info.cols);
1657 printf("switches = %#x\n", info.switches);
1658 } else if (0 == strcmp("curimage", cmd)) {
1659 enum ec_current_image image;
1661 if (cros_ec_read_current_image(dev, &image)) {
1662 debug("%s: Could not read KBC image\n", __func__);
1665 printf("%d\n", image);
1666 } else if (0 == strcmp("hash", cmd)) {
1667 struct ec_response_vboot_hash hash;
1670 if (cros_ec_read_hash(dev, &hash)) {
1671 debug("%s: Could not read KBC hash\n", __func__);
1675 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1676 printf("type: SHA-256\n");
1678 printf("type: %d\n", hash.hash_type);
1680 printf("offset: 0x%08x\n", hash.offset);
1681 printf("size: 0x%08x\n", hash.size);
1684 for (i = 0; i < hash.digest_size; i++)
1685 printf("%02x", hash.hash_digest[i]);
1687 } else if (0 == strcmp("reboot", cmd)) {
1689 enum ec_reboot_cmd cmd;
1691 if (argc >= 3 && !strcmp(argv[2], "cold"))
1692 cmd = EC_REBOOT_COLD;
1694 region = cros_ec_decode_region(argc - 2, argv + 2);
1695 if (region == EC_FLASH_REGION_RO)
1696 cmd = EC_REBOOT_JUMP_RO;
1697 else if (region == EC_FLASH_REGION_RW)
1698 cmd = EC_REBOOT_JUMP_RW;
1700 return CMD_RET_USAGE;
1703 if (cros_ec_reboot(dev, cmd, 0)) {
1704 debug("%s: Could not reboot KBC\n", __func__);
1707 } else if (0 == strcmp("events", cmd)) {
1710 if (cros_ec_get_host_events(dev, &events)) {
1711 debug("%s: Could not read host events\n", __func__);
1714 printf("0x%08x\n", events);
1715 } else if (0 == strcmp("clrevents", cmd)) {
1716 uint32_t events = 0x7fffffff;
1719 events = simple_strtol(argv[2], NULL, 0);
1721 if (cros_ec_clear_host_events(dev, events)) {
1722 debug("%s: Could not clear host events\n", __func__);
1725 } else if (0 == strcmp("read", cmd)) {
1726 ret = do_read_write(dev, 0, argc, argv);
1728 return CMD_RET_USAGE;
1729 } else if (0 == strcmp("write", cmd)) {
1730 ret = do_read_write(dev, 1, argc, argv);
1732 return CMD_RET_USAGE;
1733 } else if (0 == strcmp("erase", cmd)) {
1734 int region = cros_ec_decode_region(argc - 2, argv + 2);
1735 uint32_t offset, size;
1738 return CMD_RET_USAGE;
1739 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1740 debug("%s: Could not read region info\n", __func__);
1743 ret = cros_ec_flash_erase(dev, offset, size);
1745 debug("%s: Could not erase region\n",
1749 } else if (0 == strcmp("regioninfo", cmd)) {
1750 int region = cros_ec_decode_region(argc - 2, argv + 2);
1751 uint32_t offset, size;
1754 return CMD_RET_USAGE;
1755 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1757 debug("%s: Could not read region info\n", __func__);
1759 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1761 printf("Offset: %x\n", offset);
1762 printf("Size: %x\n", size);
1764 } else if (0 == strcmp("vbnvcontext", cmd)) {
1765 uint8_t block[EC_VBNV_BLOCK_SIZE];
1768 unsigned long result;
1771 ret = cros_ec_read_vbnvcontext(dev, block);
1773 printf("vbnv_block: ");
1774 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1775 printf("%02x", block[i]);
1780 * TODO(clchiou): Move this to a utility function as
1781 * cmd_spi might want to call it.
1783 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1784 len = strlen(argv[2]);
1786 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1789 buf[0] = argv[2][i * 2];
1790 if (i * 2 + 1 >= len)
1793 buf[1] = argv[2][i * 2 + 1];
1794 strict_strtoul(buf, 16, &result);
1797 ret = cros_ec_write_vbnvcontext(dev, block);
1800 debug("%s: Could not %s VbNvContext\n", __func__,
1801 argc <= 2 ? "read" : "write");
1803 } else if (0 == strcmp("test", cmd)) {
1804 int result = cros_ec_test(dev);
1807 printf("Test failed with error %d\n", result);
1809 puts("Test passed\n");
1810 } else if (0 == strcmp("version", cmd)) {
1811 struct ec_response_get_version *p;
1814 ret = cros_ec_read_version(dev, &p);
1816 /* Print versions */
1817 printf("RO version: %1.*s\n",
1818 (int)sizeof(p->version_string_ro),
1819 p->version_string_ro);
1820 printf("RW version: %1.*s\n",
1821 (int)sizeof(p->version_string_rw),
1822 p->version_string_rw);
1823 printf("Firmware copy: %s\n",
1825 ARRAY_SIZE(ec_current_image_name) ?
1826 ec_current_image_name[p->current_image] :
1828 ret = cros_ec_read_build_info(dev, &build_string);
1830 printf("Build info: %s\n", build_string);
1832 } else if (0 == strcmp("ldo", cmd)) {
1833 uint8_t index, state;
1837 return CMD_RET_USAGE;
1838 index = simple_strtoul(argv[2], &endp, 10);
1839 if (*argv[2] == 0 || *endp != 0)
1840 return CMD_RET_USAGE;
1842 state = simple_strtoul(argv[3], &endp, 10);
1843 if (*argv[3] == 0 || *endp != 0)
1844 return CMD_RET_USAGE;
1845 ret = cros_ec_set_ldo(dev, index, state);
1847 ret = cros_ec_get_ldo(dev, index, &state);
1849 printf("LDO%d: %s\n", index,
1850 state == EC_LDO_STATE_ON ?
1856 debug("%s: Could not access LDO%d\n", __func__, index);
1859 } else if (0 == strcmp("i2c", cmd)) {
1860 ret = cros_ec_i2c_passthrough(dev, flag, argc - 2, argv + 2);
1862 return CMD_RET_USAGE;
1866 printf("Error: CROS-EC command failed (error %d)\n", ret);
1874 crosec, 6, 1, do_cros_ec,
1875 "CROS-EC utility command",
1876 "init Re-init CROS-EC (done on startup automatically)\n"
1877 "crosec id Read CROS-EC ID\n"
1878 "crosec info Read CROS-EC info\n"
1879 "crosec curimage Read CROS-EC current image\n"
1880 "crosec hash Read CROS-EC hash\n"
1881 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1882 "crosec events Read CROS-EC host events\n"
1883 "crosec clrevents [mask] Clear CROS-EC host events\n"
1884 "crosec regioninfo <ro|rw> Read image info\n"
1885 "crosec erase <ro|rw> Erase EC image\n"
1886 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1887 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1888 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1889 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1890 "crosec test run tests on cros_ec\n"
1891 "crosec version Read CROS-EC version\n"
1892 "crosec i2c md chip address[.0, .1, .2] [# of objects] - read from I2C passthru\n"
1893 "crosec i2c mw chip address[.0, .1, .2] value [count] - write to I2C passthru (fill)"
1897 #ifdef CONFIG_DM_CROS_EC
1898 UCLASS_DRIVER(cros_ec) = {
1899 .id = UCLASS_CROS_EC,
1901 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),