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-generic/gpio.h>
28 #define debug_trace(fmt, b...) debug(fmt, #b)
30 #define debug_trace(fmt, b...)
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,
40 static struct cros_ec_dev static_dev, *last_dev;
42 DECLARE_GLOBAL_DATA_PTR;
44 /* Note: depends on enum ec_current_image */
45 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
47 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
54 printf("cmd=%#x: ", cmd);
55 for (i = 0; i < len; i++)
56 printf("%02x ", data[i]);
62 * Calculate a simple 8-bit checksum of a data block
64 * @param data Data block to checksum
65 * @param size Size of data block in bytes
66 * @return checksum value (0 to 255)
68 int cros_ec_calc_checksum(const uint8_t *data, int size)
72 for (i = csum = 0; i < size; i++)
77 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
78 const void *dout, int dout_len,
79 uint8_t **dinp, int din_len)
83 switch (dev->interface) {
84 #ifdef CONFIG_CROS_EC_SPI
86 ret = cros_ec_spi_command(dev, cmd, cmd_version,
87 (const uint8_t *)dout, dout_len,
91 #ifdef CONFIG_CROS_EC_I2C
93 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
94 (const uint8_t *)dout, dout_len,
98 #ifdef CONFIG_CROS_EC_LPC
100 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
101 (const uint8_t *)dout, dout_len,
105 case CROS_EC_IF_NONE:
114 * Send a command to the CROS-EC device and return the reply.
116 * The device's internal input/output buffers are used.
118 * @param dev CROS-EC device
119 * @param cmd Command to send (EC_CMD_...)
120 * @param cmd_version Version of command to send (EC_VER_...)
121 * @param dout Output data (may be NULL If dout_len=0)
122 * @param dout_len Size of output data in bytes
123 * @param dinp Response data (may be NULL If din_len=0).
124 * If not NULL, it will be updated to point to the data
125 * and will always be double word aligned (64-bits)
126 * @param din_len Maximum size of response in bytes
127 * @return number of bytes in response, or -1 on error
129 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
130 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
136 len = send_command(dev, cmd, cmd_version, dout, dout_len,
139 /* If the command doesn't complete, wait a while */
140 if (len == -EC_RES_IN_PROGRESS) {
141 struct ec_response_get_comms_status *resp;
144 /* Wait for command to complete */
145 start = get_timer(0);
149 mdelay(50); /* Insert some reasonable delay */
150 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
152 (uint8_t **)&resp, sizeof(*resp));
156 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
157 debug("%s: Command %#02x timeout\n",
159 return -EC_RES_TIMEOUT;
161 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
163 /* OK it completed, so read the status response */
164 /* not sure why it was 0 for the last argument */
165 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
166 NULL, 0, &din, din_len);
169 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
171 /* If we have any data to return, it must be 64bit-aligned */
172 assert(len <= 0 || !((uintptr_t)din & 7));
180 * Send a command to the CROS-EC device and return the reply.
182 * The device's internal input/output buffers are used.
184 * @param dev CROS-EC device
185 * @param cmd Command to send (EC_CMD_...)
186 * @param cmd_version Version of command to send (EC_VER_...)
187 * @param dout Output data (may be NULL If dout_len=0)
188 * @param dout_len Size of output data in bytes
189 * @param din Response data (may be NULL If din_len=0).
190 * It not NULL, it is a place for ec_command() to copy the
192 * @param din_len Maximum size of response in bytes
193 * @return number of bytes in response, or -1 on error
195 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
196 const void *dout, int dout_len,
197 void *din, int din_len)
202 assert((din_len == 0) || din);
203 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
204 &in_buffer, din_len);
207 * If we were asked to put it somewhere, do so, otherwise just
208 * disregard the result.
210 if (din && in_buffer) {
211 assert(len <= din_len);
212 memmove(din, in_buffer, len);
218 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
220 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
221 sizeof(scan->data)) < sizeof(scan->data))
227 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
229 struct ec_response_get_version *r;
231 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
232 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
235 if (maxlen > sizeof(r->version_string_ro))
236 maxlen = sizeof(r->version_string_ro);
238 switch (r->current_image) {
240 memcpy(id, r->version_string_ro, maxlen);
243 memcpy(id, r->version_string_rw, maxlen);
249 id[maxlen - 1] = '\0';
253 int cros_ec_read_version(struct cros_ec_dev *dev,
254 struct ec_response_get_version **versionp)
256 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
257 (uint8_t **)versionp, sizeof(**versionp))
258 < sizeof(**versionp))
264 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
266 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
267 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
273 int cros_ec_read_current_image(struct cros_ec_dev *dev,
274 enum ec_current_image *image)
276 struct ec_response_get_version *r;
278 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
279 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
282 *image = r->current_image;
286 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
287 struct ec_response_vboot_hash *hash)
289 struct ec_params_vboot_hash p;
292 start = get_timer(0);
293 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
294 mdelay(50); /* Insert some reasonable delay */
296 p.cmd = EC_VBOOT_HASH_GET;
297 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
298 hash, sizeof(*hash)) < 0)
301 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
302 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
303 return -EC_RES_TIMEOUT;
310 int cros_ec_read_hash(struct cros_ec_dev *dev,
311 struct ec_response_vboot_hash *hash)
313 struct ec_params_vboot_hash p;
316 p.cmd = EC_VBOOT_HASH_GET;
317 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
318 hash, sizeof(*hash)) < 0)
321 /* If the EC is busy calculating the hash, fidget until it's done. */
322 rv = cros_ec_wait_on_hash_done(dev, hash);
326 /* If the hash is valid, we're done. Otherwise, we have to kick it off
327 * again and wait for it to complete. Note that we explicitly assume
328 * that hashing zero bytes is always wrong, even though that would
329 * produce a valid hash value. */
330 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
333 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
334 __func__, hash->status, hash->size);
336 p.cmd = EC_VBOOT_HASH_START;
337 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
339 p.offset = EC_VBOOT_HASH_OFFSET_RW;
341 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
342 hash, sizeof(*hash)) < 0)
345 rv = cros_ec_wait_on_hash_done(dev, hash);
349 debug("%s: hash done\n", __func__);
354 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
356 struct ec_params_vboot_hash p;
357 struct ec_response_vboot_hash *hash;
359 /* We don't have an explict command for the EC to discard its current
360 * hash value, so we'll just tell it to calculate one that we know is
361 * wrong (we claim that hashing zero bytes is always invalid).
363 p.cmd = EC_VBOOT_HASH_RECALC;
364 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
369 debug("%s:\n", __func__);
371 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
372 (uint8_t **)&hash, sizeof(*hash)) < 0)
375 /* No need to wait for it to finish */
379 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
382 struct ec_params_reboot_ec p;
387 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
391 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
393 * EC reboot will take place immediately so delay to allow it
394 * to complete. Note that some reboot types (EC_REBOOT_COLD)
395 * will reboot the AP as well, in which case we won't actually
399 * TODO(rspangler@chromium.org): Would be nice if we had a
400 * better way to determine when the reboot is complete. Could
401 * we poll a memory-mapped LPC value?
409 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
411 /* no interrupt support : always poll */
412 if (!fdt_gpio_isvalid(&dev->ec_int))
415 return !gpio_get_value(dev->ec_int.gpio);
418 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
420 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
421 sizeof(*info)) < sizeof(*info))
427 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
429 struct ec_response_host_event_mask *resp;
432 * Use the B copy of the event flags, because the main copy is already
435 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
436 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
439 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
442 *events_ptr = resp->mask;
446 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
448 struct ec_params_host_event_mask params;
450 params.mask = events;
453 * Use the B copy of the event flags, so it affects the data returned
454 * by cros_ec_get_host_events().
456 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
457 ¶ms, sizeof(params), NULL, 0) < 0)
463 int cros_ec_flash_protect(struct cros_ec_dev *dev,
464 uint32_t set_mask, uint32_t set_flags,
465 struct ec_response_flash_protect *resp)
467 struct ec_params_flash_protect params;
469 params.mask = set_mask;
470 params.flags = set_flags;
472 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
473 ¶ms, sizeof(params),
474 resp, sizeof(*resp)) < sizeof(*resp))
480 static int cros_ec_check_version(struct cros_ec_dev *dev)
482 struct ec_params_hello req;
483 struct ec_response_hello *resp;
485 #ifdef CONFIG_CROS_EC_LPC
486 /* LPC has its own way of doing this */
487 if (dev->interface == CROS_EC_IF_LPC)
488 return cros_ec_lpc_check_version(dev);
492 * TODO(sjg@chromium.org).
493 * There is a strange oddity here with the EC. We could just ignore
494 * the response, i.e. pass the last two parameters as NULL and 0.
495 * In this case we won't read back very many bytes from the EC.
496 * On the I2C bus the EC gets upset about this and will try to send
497 * the bytes anyway. This means that we will have to wait for that
498 * to complete before continuing with a new EC command.
500 * This problem is probably unique to the I2C bus.
502 * So for now, just read all the data anyway.
505 /* Try sending a version 2 packet */
506 dev->protocol_version = 2;
507 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
508 (uint8_t **)&resp, sizeof(*resp)) > 0) {
513 * Fail if we're still here, since the EC doesn't understand any
514 * protcol version we speak. Version 1 interface without command
515 * version is no longer supported, and we don't know about any new
518 dev->protocol_version = 0;
519 printf("%s: ERROR: old EC interface not supported\n", __func__);
523 int cros_ec_test(struct cros_ec_dev *dev)
525 struct ec_params_hello req;
526 struct ec_response_hello *resp;
528 req.in_data = 0x12345678;
529 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
530 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
531 printf("ec_command_inptr() returned error\n");
534 if (resp->out_data != req.in_data + 0x01020304) {
535 printf("Received invalid handshake %x\n", resp->out_data);
542 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
543 uint32_t *offset, uint32_t *size)
545 struct ec_params_flash_region_info p;
546 struct ec_response_flash_region_info *r;
550 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
551 EC_VER_FLASH_REGION_INFO,
552 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
553 if (ret != sizeof(*r))
564 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
566 struct ec_params_flash_erase p;
570 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
575 * Write a single block to the flash
577 * Write a block of data to the EC flash. The size must not exceed the flash
578 * write block size which you can obtain from cros_ec_flash_write_burst_size().
580 * The offset starts at 0. You can obtain the region information from
581 * cros_ec_flash_offset() to find out where to write for a particular region.
583 * Attempting to write to the region where the EC is currently running from
584 * will result in an error.
586 * @param dev CROS-EC device
587 * @param data Pointer to data buffer to write
588 * @param offset Offset within flash to write to.
589 * @param size Number of bytes to write
590 * @return 0 if ok, -1 on error
592 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
593 const uint8_t *data, uint32_t offset, uint32_t size)
595 struct ec_params_flash_write p;
599 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
600 memcpy(&p + 1, data, p.size);
602 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
603 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
607 * Return optimal flash write burst size
609 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
611 return EC_FLASH_WRITE_VER0_SIZE;
615 * Check if a block of data is erased (all 0xff)
617 * This function is useful when dealing with flash, for checking whether a
618 * data block is erased and thus does not need to be programmed.
620 * @param data Pointer to data to check (must be word-aligned)
621 * @param size Number of bytes to check (must be word-aligned)
622 * @return 0 if erased, non-zero if any word is not erased
624 static int cros_ec_data_is_erased(const uint32_t *data, int size)
627 size /= sizeof(uint32_t);
628 for (; size > 0; size -= 4, data++)
635 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
636 uint32_t offset, uint32_t size)
638 uint32_t burst = cros_ec_flash_write_burst_size(dev);
643 * TODO: round up to the nearest multiple of write size. Can get away
644 * without that on link right now because its write size is 4 bytes.
647 for (off = offset; off < end; off += burst, data += burst) {
650 /* If the data is empty, there is no point in programming it */
651 todo = min(end - off, burst);
652 if (dev->optimise_flash_write &&
653 cros_ec_data_is_erased((uint32_t *)data, todo))
656 ret = cros_ec_flash_write_block(dev, data, off, todo);
665 * Read a single block from the flash
667 * Read a block of data from the EC flash. The size must not exceed the flash
668 * write block size which you can obtain from cros_ec_flash_write_burst_size().
670 * The offset starts at 0. You can obtain the region information from
671 * cros_ec_flash_offset() to find out where to read for a particular region.
673 * @param dev CROS-EC device
674 * @param data Pointer to data buffer to read into
675 * @param offset Offset within flash to read from
676 * @param size Number of bytes to read
677 * @return 0 if ok, -1 on error
679 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
680 uint32_t offset, uint32_t size)
682 struct ec_params_flash_read p;
687 return ec_command(dev, EC_CMD_FLASH_READ, 0,
688 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
691 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
694 uint32_t burst = cros_ec_flash_write_burst_size(dev);
699 for (off = offset; off < end; off += burst, data += burst) {
700 ret = cros_ec_flash_read_block(dev, data, off,
701 min(end - off, burst));
709 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
710 const uint8_t *image, int image_size)
712 uint32_t rw_offset, rw_size;
715 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
717 if (image_size > rw_size)
720 /* Invalidate the existing hash, just in case the AP reboots
721 * unexpectedly during the update. If that happened, the EC RW firmware
722 * would be invalid, but the EC would still have the original hash.
724 ret = cros_ec_invalidate_hash(dev);
729 * Erase the entire RW section, so that the EC doesn't see any garbage
730 * past the new image if it's smaller than the current image.
732 * TODO: could optimize this to erase just the current image, since
733 * presumably everything past that is 0xff's. But would still need to
734 * round up to the nearest multiple of erase size.
736 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
740 /* Write the image */
741 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
748 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
750 struct ec_params_vbnvcontext p;
753 p.op = EC_VBNV_CONTEXT_OP_READ;
755 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
756 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
757 if (len < EC_VBNV_BLOCK_SIZE)
763 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
765 struct ec_params_vbnvcontext p;
768 p.op = EC_VBNV_CONTEXT_OP_WRITE;
769 memcpy(p.block, block, sizeof(p.block));
771 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
772 &p, sizeof(p), NULL, 0);
779 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
781 struct ec_params_ldo_set params;
783 params.index = index;
784 params.state = state;
786 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
787 ¶ms, sizeof(params),
794 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
796 struct ec_params_ldo_get params;
797 struct ec_response_ldo_get *resp;
799 params.index = index;
801 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
802 ¶ms, sizeof(params),
803 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
806 *state = resp->state;
812 * Decode EC interface details from the device tree and allocate a suitable
815 * @param blob Device tree blob
816 * @param node Node to decode from
817 * @param devp Returns a pointer to the new allocated device
818 * @return 0 if ok, -1 on error
820 static int cros_ec_decode_fdt(const void *blob, int node,
821 struct cros_ec_dev **devp)
823 enum fdt_compat_id compat;
824 struct cros_ec_dev *dev;
827 /* See what type of parent we are inside (this is expensive) */
828 parent = fdt_parent_offset(blob, node);
830 debug("%s: Cannot find node parent\n", __func__);
836 dev->parent_node = parent;
838 compat = fdtdec_lookup(blob, parent);
840 #ifdef CONFIG_CROS_EC_SPI
841 case COMPAT_SAMSUNG_EXYNOS_SPI:
842 dev->interface = CROS_EC_IF_SPI;
843 if (cros_ec_spi_decode_fdt(dev, blob))
847 #ifdef CONFIG_CROS_EC_I2C
848 case COMPAT_SAMSUNG_S3C2440_I2C:
849 dev->interface = CROS_EC_IF_I2C;
850 if (cros_ec_i2c_decode_fdt(dev, blob))
854 #ifdef CONFIG_CROS_EC_LPC
855 case COMPAT_INTEL_LPC:
856 dev->interface = CROS_EC_IF_LPC;
860 debug("%s: Unknown compat id %d\n", __func__, compat);
864 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
865 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
866 "optimise-flash-write");
872 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
875 struct cros_ec_dev *dev;
880 node = fdtdec_next_compatible(blob, node,
881 COMPAT_GOOGLE_CROS_EC);
883 debug("%s: Node not found\n", __func__);
886 } while (!fdtdec_get_is_enabled(blob, node));
888 if (cros_ec_decode_fdt(blob, node, &dev)) {
889 debug("%s: Failed to decode device.\n", __func__);
890 return -CROS_EC_ERR_FDT_DECODE;
893 switch (dev->interface) {
894 #ifdef CONFIG_CROS_EC_SPI
896 if (cros_ec_spi_init(dev, blob)) {
897 debug("%s: Could not setup SPI interface\n", __func__);
898 return -CROS_EC_ERR_DEV_INIT;
902 #ifdef CONFIG_CROS_EC_I2C
904 if (cros_ec_i2c_init(dev, blob))
905 return -CROS_EC_ERR_DEV_INIT;
908 #ifdef CONFIG_CROS_EC_LPC
910 if (cros_ec_lpc_init(dev, blob))
911 return -CROS_EC_ERR_DEV_INIT;
914 case CROS_EC_IF_NONE:
919 /* we will poll the EC interrupt line */
920 fdtdec_setup_gpio(&dev->ec_int);
921 if (fdt_gpio_isvalid(&dev->ec_int))
922 gpio_direction_input(dev->ec_int.gpio);
924 if (cros_ec_check_version(dev)) {
925 debug("%s: Could not detect CROS-EC version\n", __func__);
926 return -CROS_EC_ERR_CHECK_VERSION;
929 if (cros_ec_read_id(dev, id, sizeof(id))) {
930 debug("%s: Could not read KBC ID\n", __func__);
931 return -CROS_EC_ERR_READ_ID;
934 /* Remember this device for use by the cros_ec command */
935 last_dev = *cros_ecp = dev;
936 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
941 int cros_ec_decode_region(int argc, char * const argv[])
944 if (0 == strcmp(*argv, "rw"))
945 return EC_FLASH_REGION_RW;
946 else if (0 == strcmp(*argv, "ro"))
947 return EC_FLASH_REGION_RO;
949 debug("%s: Invalid region '%s'\n", __func__, *argv);
951 debug("%s: Missing region parameter\n", __func__);
957 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
959 int flash_node, node;
961 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
963 debug("Failed to find chrome-ec node'\n");
967 flash_node = fdt_subnode_offset(blob, node, "flash");
968 if (flash_node < 0) {
969 debug("Failed to find flash node\n");
973 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
975 debug("Failed to decode flash node in chrome-ec'\n");
979 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
981 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
982 node = fdt_next_subnode(blob, node)) {
983 const char *name = fdt_get_name(blob, node, NULL);
984 enum ec_flash_region region;
986 if (0 == strcmp(name, "ro")) {
987 region = EC_FLASH_REGION_RO;
988 } else if (0 == strcmp(name, "rw")) {
989 region = EC_FLASH_REGION_RW;
990 } else if (0 == strcmp(name, "wp-ro")) {
991 region = EC_FLASH_REGION_WP_RO;
993 debug("Unknown EC flash region name '%s'\n", name);
997 if (fdtdec_read_fmap_entry(blob, node, "reg",
998 &config->region[region])) {
999 debug("Failed to decode flash region in chrome-ec'\n");
1007 #ifdef CONFIG_CMD_CROS_EC
1010 * Perform a flash read or write command
1012 * @param dev CROS-EC device to read/write
1013 * @param is_write 1 do to a write, 0 to do a read
1014 * @param argc Number of arguments
1015 * @param argv Arguments (2 is region, 3 is address)
1016 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1017 * (negative EC_RES_...)
1019 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1020 char * const argv[])
1022 uint32_t offset, size = -1U, region_size;
1028 region = cros_ec_decode_region(argc - 2, argv + 2);
1033 addr = simple_strtoul(argv[3], &endp, 16);
1034 if (*argv[3] == 0 || *endp != 0)
1037 size = simple_strtoul(argv[4], &endp, 16);
1038 if (*argv[4] == 0 || *endp != 0)
1042 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
1044 debug("%s: Could not read region info\n", __func__);
1051 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1052 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1054 debug("%s: Could not %s region\n", __func__,
1055 is_write ? "write" : "read");
1062 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1064 struct cros_ec_dev *dev = last_dev;
1069 return CMD_RET_USAGE;
1072 if (0 == strcmp("init", cmd)) {
1073 ret = cros_ec_init(gd->fdt_blob, &dev);
1075 printf("Could not init cros_ec device (err %d)\n", ret);
1081 /* Just use the last allocated device; there should be only one */
1083 printf("No CROS-EC device available\n");
1086 if (0 == strcmp("id", cmd)) {
1089 if (cros_ec_read_id(dev, id, sizeof(id))) {
1090 debug("%s: Could not read KBC ID\n", __func__);
1094 } else if (0 == strcmp("info", cmd)) {
1095 struct ec_response_mkbp_info info;
1097 if (cros_ec_info(dev, &info)) {
1098 debug("%s: Could not read KBC info\n", __func__);
1101 printf("rows = %u\n", info.rows);
1102 printf("cols = %u\n", info.cols);
1103 printf("switches = %#x\n", info.switches);
1104 } else if (0 == strcmp("curimage", cmd)) {
1105 enum ec_current_image image;
1107 if (cros_ec_read_current_image(dev, &image)) {
1108 debug("%s: Could not read KBC image\n", __func__);
1111 printf("%d\n", image);
1112 } else if (0 == strcmp("hash", cmd)) {
1113 struct ec_response_vboot_hash hash;
1116 if (cros_ec_read_hash(dev, &hash)) {
1117 debug("%s: Could not read KBC hash\n", __func__);
1121 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1122 printf("type: SHA-256\n");
1124 printf("type: %d\n", hash.hash_type);
1126 printf("offset: 0x%08x\n", hash.offset);
1127 printf("size: 0x%08x\n", hash.size);
1130 for (i = 0; i < hash.digest_size; i++)
1131 printf("%02x", hash.hash_digest[i]);
1133 } else if (0 == strcmp("reboot", cmd)) {
1135 enum ec_reboot_cmd cmd;
1137 if (argc >= 3 && !strcmp(argv[2], "cold"))
1138 cmd = EC_REBOOT_COLD;
1140 region = cros_ec_decode_region(argc - 2, argv + 2);
1141 if (region == EC_FLASH_REGION_RO)
1142 cmd = EC_REBOOT_JUMP_RO;
1143 else if (region == EC_FLASH_REGION_RW)
1144 cmd = EC_REBOOT_JUMP_RW;
1146 return CMD_RET_USAGE;
1149 if (cros_ec_reboot(dev, cmd, 0)) {
1150 debug("%s: Could not reboot KBC\n", __func__);
1153 } else if (0 == strcmp("events", cmd)) {
1156 if (cros_ec_get_host_events(dev, &events)) {
1157 debug("%s: Could not read host events\n", __func__);
1160 printf("0x%08x\n", events);
1161 } else if (0 == strcmp("clrevents", cmd)) {
1162 uint32_t events = 0x7fffffff;
1165 events = simple_strtol(argv[2], NULL, 0);
1167 if (cros_ec_clear_host_events(dev, events)) {
1168 debug("%s: Could not clear host events\n", __func__);
1171 } else if (0 == strcmp("read", cmd)) {
1172 ret = do_read_write(dev, 0, argc, argv);
1174 return CMD_RET_USAGE;
1175 } else if (0 == strcmp("write", cmd)) {
1176 ret = do_read_write(dev, 1, argc, argv);
1178 return CMD_RET_USAGE;
1179 } else if (0 == strcmp("erase", cmd)) {
1180 int region = cros_ec_decode_region(argc - 2, argv + 2);
1181 uint32_t offset, size;
1184 return CMD_RET_USAGE;
1185 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1186 debug("%s: Could not read region info\n", __func__);
1189 ret = cros_ec_flash_erase(dev, offset, size);
1191 debug("%s: Could not erase region\n",
1195 } else if (0 == strcmp("regioninfo", cmd)) {
1196 int region = cros_ec_decode_region(argc - 2, argv + 2);
1197 uint32_t offset, size;
1200 return CMD_RET_USAGE;
1201 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1203 debug("%s: Could not read region info\n", __func__);
1205 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1207 printf("Offset: %x\n", offset);
1208 printf("Size: %x\n", size);
1210 } else if (0 == strcmp("vbnvcontext", cmd)) {
1211 uint8_t block[EC_VBNV_BLOCK_SIZE];
1214 unsigned long result;
1217 ret = cros_ec_read_vbnvcontext(dev, block);
1219 printf("vbnv_block: ");
1220 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1221 printf("%02x", block[i]);
1226 * TODO(clchiou): Move this to a utility function as
1227 * cmd_spi might want to call it.
1229 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1230 len = strlen(argv[2]);
1232 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1235 buf[0] = argv[2][i * 2];
1236 if (i * 2 + 1 >= len)
1239 buf[1] = argv[2][i * 2 + 1];
1240 strict_strtoul(buf, 16, &result);
1243 ret = cros_ec_write_vbnvcontext(dev, block);
1246 debug("%s: Could not %s VbNvContext\n", __func__,
1247 argc <= 2 ? "read" : "write");
1249 } else if (0 == strcmp("test", cmd)) {
1250 int result = cros_ec_test(dev);
1253 printf("Test failed with error %d\n", result);
1255 puts("Test passed\n");
1256 } else if (0 == strcmp("version", cmd)) {
1257 struct ec_response_get_version *p;
1260 ret = cros_ec_read_version(dev, &p);
1262 /* Print versions */
1263 printf("RO version: %1.*s\n",
1264 sizeof(p->version_string_ro),
1265 p->version_string_ro);
1266 printf("RW version: %1.*s\n",
1267 sizeof(p->version_string_rw),
1268 p->version_string_rw);
1269 printf("Firmware copy: %s\n",
1271 ARRAY_SIZE(ec_current_image_name) ?
1272 ec_current_image_name[p->current_image] :
1274 ret = cros_ec_read_build_info(dev, &build_string);
1276 printf("Build info: %s\n", build_string);
1278 } else if (0 == strcmp("ldo", cmd)) {
1279 uint8_t index, state;
1283 return CMD_RET_USAGE;
1284 index = simple_strtoul(argv[2], &endp, 10);
1285 if (*argv[2] == 0 || *endp != 0)
1286 return CMD_RET_USAGE;
1288 state = simple_strtoul(argv[3], &endp, 10);
1289 if (*argv[3] == 0 || *endp != 0)
1290 return CMD_RET_USAGE;
1291 ret = cros_ec_set_ldo(dev, index, state);
1293 ret = cros_ec_get_ldo(dev, index, &state);
1295 printf("LDO%d: %s\n", index,
1296 state == EC_LDO_STATE_ON ?
1302 debug("%s: Could not access LDO%d\n", __func__, index);
1306 return CMD_RET_USAGE;
1310 printf("Error: CROS-EC command failed (error %d)\n", ret);
1318 crosec, 5, 1, do_cros_ec,
1319 "CROS-EC utility command",
1320 "init Re-init CROS-EC (done on startup automatically)\n"
1321 "crosec id Read CROS-EC ID\n"
1322 "crosec info Read CROS-EC info\n"
1323 "crosec curimage Read CROS-EC current image\n"
1324 "crosec hash Read CROS-EC hash\n"
1325 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1326 "crosec events Read CROS-EC host events\n"
1327 "crosec clrevents [mask] Clear CROS-EC host events\n"
1328 "crosec regioninfo <ro|rw> Read image info\n"
1329 "crosec erase <ro|rw> Erase EC image\n"
1330 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1331 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1332 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1333 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1334 "crosec test run tests on cros_ec\n"
1335 "crosec version Read CROS-EC version"