2 * Chromium OS cros_ec driver
4 * Copyright (c) 2012 The Chromium OS Authors.
6 * SPDX-License-Identifier: GPL-2.0+
10 * The Matrix Keyboard Protocol driver handles talking to the keyboard
11 * controller chip. Mostly this is for keyboard functions, but some other
12 * things have slipped in, so we provide generic services to talk to the
24 #include <asm-generic/gpio.h>
27 #define debug_trace(fmt, b...) debug(fmt, #b)
29 #define debug_trace(fmt, b...)
33 /* Timeout waiting for a flash erase command to complete */
34 CROS_EC_CMD_TIMEOUT_MS = 5000,
35 /* Timeout waiting for a synchronous hash to be recomputed */
36 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
39 static struct cros_ec_dev static_dev, *last_dev;
41 DECLARE_GLOBAL_DATA_PTR;
43 /* Note: depends on enum ec_current_image */
44 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
46 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
53 printf("cmd=%#x: ", cmd);
54 for (i = 0; i < len; i++)
55 printf("%02x ", data[i]);
61 * Calculate a simple 8-bit checksum of a data block
63 * @param data Data block to checksum
64 * @param size Size of data block in bytes
65 * @return checksum value (0 to 255)
67 int cros_ec_calc_checksum(const uint8_t *data, int size)
71 for (i = csum = 0; i < size; i++)
76 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
77 const void *dout, int dout_len,
78 uint8_t **dinp, int din_len)
82 switch (dev->interface) {
83 #ifdef CONFIG_CROS_EC_SPI
85 ret = cros_ec_spi_command(dev, cmd, cmd_version,
86 (const uint8_t *)dout, dout_len,
90 #ifdef CONFIG_CROS_EC_I2C
92 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
93 (const uint8_t *)dout, dout_len,
97 #ifdef CONFIG_CROS_EC_LPC
99 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
100 (const uint8_t *)dout, dout_len,
104 case CROS_EC_IF_NONE:
113 * Send a command to the CROS-EC device and return the reply.
115 * The device's internal input/output buffers are used.
117 * @param dev CROS-EC device
118 * @param cmd Command to send (EC_CMD_...)
119 * @param cmd_version Version of command to send (EC_VER_...)
120 * @param dout Output data (may be NULL If dout_len=0)
121 * @param dout_len Size of output data in bytes
122 * @param dinp Response data (may be NULL If din_len=0).
123 * If not NULL, it will be updated to point to the data
124 * and will always be double word aligned (64-bits)
125 * @param din_len Maximum size of response in bytes
126 * @return number of bytes in response, or -1 on error
128 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
129 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
135 len = send_command(dev, cmd, cmd_version, dout, dout_len,
138 /* If the command doesn't complete, wait a while */
139 if (len == -EC_RES_IN_PROGRESS) {
140 struct ec_response_get_comms_status *resp;
143 /* Wait for command to complete */
144 start = get_timer(0);
148 mdelay(50); /* Insert some reasonable delay */
149 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
151 (uint8_t **)&resp, sizeof(*resp));
155 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
156 debug("%s: Command %#02x timeout\n",
158 return -EC_RES_TIMEOUT;
160 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
162 /* OK it completed, so read the status response */
163 /* not sure why it was 0 for the last argument */
164 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
165 NULL, 0, &din, din_len);
168 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
170 /* If we have any data to return, it must be 64bit-aligned */
171 assert(len <= 0 || !((uintptr_t)din & 7));
179 * Send a command to the CROS-EC device and return the reply.
181 * The device's internal input/output buffers are used.
183 * @param dev CROS-EC device
184 * @param cmd Command to send (EC_CMD_...)
185 * @param cmd_version Version of command to send (EC_VER_...)
186 * @param dout Output data (may be NULL If dout_len=0)
187 * @param dout_len Size of output data in bytes
188 * @param din Response data (may be NULL If din_len=0).
189 * It not NULL, it is a place for ec_command() to copy the
191 * @param din_len Maximum size of response in bytes
192 * @return number of bytes in response, or -1 on error
194 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
195 const void *dout, int dout_len,
196 void *din, int din_len)
201 assert((din_len == 0) || din);
202 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
203 &in_buffer, din_len);
206 * If we were asked to put it somewhere, do so, otherwise just
207 * disregard the result.
209 if (din && in_buffer) {
210 assert(len <= din_len);
211 memmove(din, in_buffer, len);
217 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
219 if (ec_command(dev, EC_CMD_CROS_EC_STATE, 0, NULL, 0, scan,
220 sizeof(scan->data)) < sizeof(scan->data))
226 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
228 struct ec_response_get_version *r;
230 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
231 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
234 if (maxlen > sizeof(r->version_string_ro))
235 maxlen = sizeof(r->version_string_ro);
237 switch (r->current_image) {
239 memcpy(id, r->version_string_ro, maxlen);
242 memcpy(id, r->version_string_rw, maxlen);
248 id[maxlen - 1] = '\0';
252 int cros_ec_read_version(struct cros_ec_dev *dev,
253 struct ec_response_get_version **versionp)
255 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
256 (uint8_t **)versionp, sizeof(**versionp))
257 < sizeof(**versionp))
263 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
265 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
266 (uint8_t **)strp, EC_HOST_PARAM_SIZE) < 0)
272 int cros_ec_read_current_image(struct cros_ec_dev *dev,
273 enum ec_current_image *image)
275 struct ec_response_get_version *r;
277 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
278 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
281 *image = r->current_image;
285 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
286 struct ec_response_vboot_hash *hash)
288 struct ec_params_vboot_hash p;
291 start = get_timer(0);
292 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
293 mdelay(50); /* Insert some reasonable delay */
295 p.cmd = EC_VBOOT_HASH_GET;
296 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
297 hash, sizeof(*hash)) < 0)
300 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
301 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
302 return -EC_RES_TIMEOUT;
309 int cros_ec_read_hash(struct cros_ec_dev *dev,
310 struct ec_response_vboot_hash *hash)
312 struct ec_params_vboot_hash p;
315 p.cmd = EC_VBOOT_HASH_GET;
316 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
317 hash, sizeof(*hash)) < 0)
320 /* If the EC is busy calculating the hash, fidget until it's done. */
321 rv = cros_ec_wait_on_hash_done(dev, hash);
325 /* If the hash is valid, we're done. Otherwise, we have to kick it off
326 * again and wait for it to complete. Note that we explicitly assume
327 * that hashing zero bytes is always wrong, even though that would
328 * produce a valid hash value. */
329 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
332 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
333 __func__, hash->status, hash->size);
335 p.cmd = EC_VBOOT_HASH_RECALC;
336 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
338 p.offset = EC_VBOOT_HASH_OFFSET_RW;
340 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
341 hash, sizeof(*hash)) < 0)
344 rv = cros_ec_wait_on_hash_done(dev, hash);
348 debug("%s: hash done\n", __func__);
353 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
355 struct ec_params_vboot_hash p;
356 struct ec_response_vboot_hash *hash;
358 /* We don't have an explict command for the EC to discard its current
359 * hash value, so we'll just tell it to calculate one that we know is
360 * wrong (we claim that hashing zero bytes is always invalid).
362 p.cmd = EC_VBOOT_HASH_RECALC;
363 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
368 debug("%s:\n", __func__);
370 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
371 (uint8_t **)&hash, sizeof(*hash)) < 0)
374 /* No need to wait for it to finish */
378 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
381 struct ec_params_reboot_ec p;
386 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
390 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
392 * EC reboot will take place immediately so delay to allow it
393 * to complete. Note that some reboot types (EC_REBOOT_COLD)
394 * will reboot the AP as well, in which case we won't actually
398 * TODO(rspangler@chromium.org): Would be nice if we had a
399 * better way to determine when the reboot is complete. Could
400 * we poll a memory-mapped LPC value?
408 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
410 /* no interrupt support : always poll */
411 if (!fdt_gpio_isvalid(&dev->ec_int))
414 return !gpio_get_value(dev->ec_int.gpio);
417 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_cros_ec_info *info)
419 if (ec_command(dev, EC_CMD_CROS_EC_INFO, 0, NULL, 0, info,
420 sizeof(*info)) < sizeof(*info))
426 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
428 struct ec_response_host_event_mask *resp;
431 * Use the B copy of the event flags, because the main copy is already
434 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
435 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
438 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
441 *events_ptr = resp->mask;
445 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
447 struct ec_params_host_event_mask params;
449 params.mask = events;
452 * Use the B copy of the event flags, so it affects the data returned
453 * by cros_ec_get_host_events().
455 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
456 ¶ms, sizeof(params), NULL, 0) < 0)
462 int cros_ec_flash_protect(struct cros_ec_dev *dev,
463 uint32_t set_mask, uint32_t set_flags,
464 struct ec_response_flash_protect *resp)
466 struct ec_params_flash_protect params;
468 params.mask = set_mask;
469 params.flags = set_flags;
471 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
472 ¶ms, sizeof(params),
473 resp, sizeof(*resp)) < sizeof(*resp))
479 static int cros_ec_check_version(struct cros_ec_dev *dev)
481 struct ec_params_hello req;
482 struct ec_response_hello *resp;
484 #ifdef CONFIG_CROS_EC_LPC
485 /* LPC has its own way of doing this */
486 if (dev->interface == CROS_EC_IF_LPC)
487 return cros_ec_lpc_check_version(dev);
491 * TODO(sjg@chromium.org).
492 * There is a strange oddity here with the EC. We could just ignore
493 * the response, i.e. pass the last two parameters as NULL and 0.
494 * In this case we won't read back very many bytes from the EC.
495 * On the I2C bus the EC gets upset about this and will try to send
496 * the bytes anyway. This means that we will have to wait for that
497 * to complete before continuing with a new EC command.
499 * This problem is probably unique to the I2C bus.
501 * So for now, just read all the data anyway.
503 dev->cmd_version_is_supported = 1;
504 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
505 (uint8_t **)&resp, sizeof(*resp)) > 0) {
506 /* It appears to understand new version commands */
507 dev->cmd_version_is_supported = 1;
509 printf("%s: ERROR: old EC interface not supported\n",
517 int cros_ec_test(struct cros_ec_dev *dev)
519 struct ec_params_hello req;
520 struct ec_response_hello *resp;
522 req.in_data = 0x12345678;
523 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
524 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
525 printf("ec_command_inptr() returned error\n");
528 if (resp->out_data != req.in_data + 0x01020304) {
529 printf("Received invalid handshake %x\n", resp->out_data);
536 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
537 uint32_t *offset, uint32_t *size)
539 struct ec_params_flash_region_info p;
540 struct ec_response_flash_region_info *r;
544 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
545 EC_VER_FLASH_REGION_INFO,
546 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
547 if (ret != sizeof(*r))
558 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
560 struct ec_params_flash_erase p;
564 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
569 * Write a single block to the flash
571 * Write a block of data to the EC flash. The size must not exceed the flash
572 * write block size which you can obtain from cros_ec_flash_write_burst_size().
574 * The offset starts at 0. You can obtain the region information from
575 * cros_ec_flash_offset() to find out where to write for a particular region.
577 * Attempting to write to the region where the EC is currently running from
578 * will result in an error.
580 * @param dev CROS-EC device
581 * @param data Pointer to data buffer to write
582 * @param offset Offset within flash to write to.
583 * @param size Number of bytes to write
584 * @return 0 if ok, -1 on error
586 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
587 const uint8_t *data, uint32_t offset, uint32_t size)
589 struct ec_params_flash_write p;
593 assert(data && p.size <= sizeof(p.data));
594 memcpy(p.data, data, p.size);
596 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
597 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
601 * Return optimal flash write burst size
603 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
605 struct ec_params_flash_write p;
606 return sizeof(p.data);
610 * Check if a block of data is erased (all 0xff)
612 * This function is useful when dealing with flash, for checking whether a
613 * data block is erased and thus does not need to be programmed.
615 * @param data Pointer to data to check (must be word-aligned)
616 * @param size Number of bytes to check (must be word-aligned)
617 * @return 0 if erased, non-zero if any word is not erased
619 static int cros_ec_data_is_erased(const uint32_t *data, int size)
622 size /= sizeof(uint32_t);
623 for (; size > 0; size -= 4, data++)
630 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
631 uint32_t offset, uint32_t size)
633 uint32_t burst = cros_ec_flash_write_burst_size(dev);
638 * TODO: round up to the nearest multiple of write size. Can get away
639 * without that on link right now because its write size is 4 bytes.
642 for (off = offset; off < end; off += burst, data += burst) {
645 /* If the data is empty, there is no point in programming it */
646 todo = min(end - off, burst);
647 if (dev->optimise_flash_write &&
648 cros_ec_data_is_erased((uint32_t *)data, todo))
651 ret = cros_ec_flash_write_block(dev, data, off, todo);
660 * Read a single block from the flash
662 * Read a block of data from the EC flash. The size must not exceed the flash
663 * write block size which you can obtain from cros_ec_flash_write_burst_size().
665 * The offset starts at 0. You can obtain the region information from
666 * cros_ec_flash_offset() to find out where to read for a particular region.
668 * @param dev CROS-EC device
669 * @param data Pointer to data buffer to read into
670 * @param offset Offset within flash to read from
671 * @param size Number of bytes to read
672 * @return 0 if ok, -1 on error
674 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
675 uint32_t offset, uint32_t size)
677 struct ec_params_flash_read p;
682 return ec_command(dev, EC_CMD_FLASH_READ, 0,
683 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
686 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
689 uint32_t burst = cros_ec_flash_write_burst_size(dev);
694 for (off = offset; off < end; off += burst, data += burst) {
695 ret = cros_ec_flash_read_block(dev, data, off,
696 min(end - off, burst));
704 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
705 const uint8_t *image, int image_size)
707 uint32_t rw_offset, rw_size;
710 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
712 if (image_size > rw_size)
715 /* Invalidate the existing hash, just in case the AP reboots
716 * unexpectedly during the update. If that happened, the EC RW firmware
717 * would be invalid, but the EC would still have the original hash.
719 ret = cros_ec_invalidate_hash(dev);
724 * Erase the entire RW section, so that the EC doesn't see any garbage
725 * past the new image if it's smaller than the current image.
727 * TODO: could optimize this to erase just the current image, since
728 * presumably everything past that is 0xff's. But would still need to
729 * round up to the nearest multiple of erase size.
731 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
735 /* Write the image */
736 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
743 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
745 struct ec_params_vbnvcontext p;
748 p.op = EC_VBNV_CONTEXT_OP_READ;
750 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
751 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
752 if (len < EC_VBNV_BLOCK_SIZE)
758 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
760 struct ec_params_vbnvcontext p;
763 p.op = EC_VBNV_CONTEXT_OP_WRITE;
764 memcpy(p.block, block, sizeof(p.block));
766 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
767 &p, sizeof(p), NULL, 0);
774 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
776 struct ec_params_ldo_set params;
778 params.index = index;
779 params.state = state;
781 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
782 ¶ms, sizeof(params),
789 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
791 struct ec_params_ldo_get params;
792 struct ec_response_ldo_get *resp;
794 params.index = index;
796 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
797 ¶ms, sizeof(params),
798 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
801 *state = resp->state;
807 * Decode MBKP details from the device tree and allocate a suitable device.
809 * @param blob Device tree blob
810 * @param node Node to decode from
811 * @param devp Returns a pointer to the new allocated device
812 * @return 0 if ok, -1 on error
814 static int cros_ec_decode_fdt(const void *blob, int node,
815 struct cros_ec_dev **devp)
817 enum fdt_compat_id compat;
818 struct cros_ec_dev *dev;
821 /* See what type of parent we are inside (this is expensive) */
822 parent = fdt_parent_offset(blob, node);
824 debug("%s: Cannot find node parent\n", __func__);
830 dev->parent_node = parent;
832 compat = fdtdec_lookup(blob, parent);
834 #ifdef CONFIG_CROS_EC_SPI
835 case COMPAT_SAMSUNG_EXYNOS_SPI:
836 dev->interface = CROS_EC_IF_SPI;
837 if (cros_ec_spi_decode_fdt(dev, blob))
841 #ifdef CONFIG_CROS_EC_I2C
842 case COMPAT_SAMSUNG_S3C2440_I2C:
843 dev->interface = CROS_EC_IF_I2C;
844 if (cros_ec_i2c_decode_fdt(dev, blob))
848 #ifdef CONFIG_CROS_EC_LPC
849 case COMPAT_INTEL_LPC:
850 dev->interface = CROS_EC_IF_LPC;
854 debug("%s: Unknown compat id %d\n", __func__, compat);
858 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
859 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
860 "optimise-flash-write");
866 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
869 struct cros_ec_dev *dev;
874 node = fdtdec_next_compatible(blob, node,
875 COMPAT_GOOGLE_CROS_EC);
877 debug("%s: Node not found\n", __func__);
880 } while (!fdtdec_get_is_enabled(blob, node));
882 if (cros_ec_decode_fdt(blob, node, &dev)) {
883 debug("%s: Failed to decode device.\n", __func__);
884 return -CROS_EC_ERR_FDT_DECODE;
887 switch (dev->interface) {
888 #ifdef CONFIG_CROS_EC_SPI
890 if (cros_ec_spi_init(dev, blob)) {
891 debug("%s: Could not setup SPI interface\n", __func__);
892 return -CROS_EC_ERR_DEV_INIT;
896 #ifdef CONFIG_CROS_EC_I2C
898 if (cros_ec_i2c_init(dev, blob))
899 return -CROS_EC_ERR_DEV_INIT;
902 #ifdef CONFIG_CROS_EC_LPC
904 if (cros_ec_lpc_init(dev, blob))
905 return -CROS_EC_ERR_DEV_INIT;
908 case CROS_EC_IF_NONE:
913 /* we will poll the EC interrupt line */
914 fdtdec_setup_gpio(&dev->ec_int);
915 if (fdt_gpio_isvalid(&dev->ec_int))
916 gpio_direction_input(dev->ec_int.gpio);
918 if (cros_ec_check_version(dev)) {
919 debug("%s: Could not detect CROS-EC version\n", __func__);
920 return -CROS_EC_ERR_CHECK_VERSION;
923 if (cros_ec_read_id(dev, id, sizeof(id))) {
924 debug("%s: Could not read KBC ID\n", __func__);
925 return -CROS_EC_ERR_READ_ID;
928 /* Remember this device for use by the cros_ec command */
929 last_dev = *cros_ecp = dev;
930 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
935 int cros_ec_decode_region(int argc, char * const argv[])
938 if (0 == strcmp(*argv, "rw"))
939 return EC_FLASH_REGION_RW;
940 else if (0 == strcmp(*argv, "ro"))
941 return EC_FLASH_REGION_RO;
943 debug("%s: Invalid region '%s'\n", __func__, *argv);
945 debug("%s: Missing region parameter\n", __func__);
951 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
953 int flash_node, node;
955 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
957 debug("Failed to find chrome-ec node'\n");
961 flash_node = fdt_subnode_offset(blob, node, "flash");
962 if (flash_node < 0) {
963 debug("Failed to find flash node\n");
967 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
969 debug("Failed to decode flash node in chrome-ec'\n");
973 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
975 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
976 node = fdt_next_subnode(blob, node)) {
977 const char *name = fdt_get_name(blob, node, NULL);
978 enum ec_flash_region region;
980 if (0 == strcmp(name, "ro")) {
981 region = EC_FLASH_REGION_RO;
982 } else if (0 == strcmp(name, "rw")) {
983 region = EC_FLASH_REGION_RW;
984 } else if (0 == strcmp(name, "wp-ro")) {
985 region = EC_FLASH_REGION_WP_RO;
987 debug("Unknown EC flash region name '%s'\n", name);
991 if (fdtdec_read_fmap_entry(blob, node, "reg",
992 &config->region[region])) {
993 debug("Failed to decode flash region in chrome-ec'\n");
1001 #ifdef CONFIG_CMD_CROS_EC
1004 * Perform a flash read or write command
1006 * @param dev CROS-EC device to read/write
1007 * @param is_write 1 do to a write, 0 to do a read
1008 * @param argc Number of arguments
1009 * @param argv Arguments (2 is region, 3 is address)
1010 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1011 * (negative EC_RES_...)
1013 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1014 char * const argv[])
1016 uint32_t offset, size = -1U, region_size;
1022 region = cros_ec_decode_region(argc - 2, argv + 2);
1027 addr = simple_strtoul(argv[3], &endp, 16);
1028 if (*argv[3] == 0 || *endp != 0)
1031 size = simple_strtoul(argv[4], &endp, 16);
1032 if (*argv[4] == 0 || *endp != 0)
1036 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
1038 debug("%s: Could not read region info\n", __func__);
1045 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1046 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1048 debug("%s: Could not %s region\n", __func__,
1049 is_write ? "write" : "read");
1056 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1058 struct cros_ec_dev *dev = last_dev;
1063 return CMD_RET_USAGE;
1066 if (0 == strcmp("init", cmd)) {
1067 ret = cros_ec_init(gd->fdt_blob, &dev);
1069 printf("Could not init cros_ec device (err %d)\n", ret);
1075 /* Just use the last allocated device; there should be only one */
1077 printf("No CROS-EC device available\n");
1080 if (0 == strcmp("id", cmd)) {
1083 if (cros_ec_read_id(dev, id, sizeof(id))) {
1084 debug("%s: Could not read KBC ID\n", __func__);
1088 } else if (0 == strcmp("info", cmd)) {
1089 struct ec_response_cros_ec_info info;
1091 if (cros_ec_info(dev, &info)) {
1092 debug("%s: Could not read KBC info\n", __func__);
1095 printf("rows = %u\n", info.rows);
1096 printf("cols = %u\n", info.cols);
1097 printf("switches = %#x\n", info.switches);
1098 } else if (0 == strcmp("curimage", cmd)) {
1099 enum ec_current_image image;
1101 if (cros_ec_read_current_image(dev, &image)) {
1102 debug("%s: Could not read KBC image\n", __func__);
1105 printf("%d\n", image);
1106 } else if (0 == strcmp("hash", cmd)) {
1107 struct ec_response_vboot_hash hash;
1110 if (cros_ec_read_hash(dev, &hash)) {
1111 debug("%s: Could not read KBC hash\n", __func__);
1115 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1116 printf("type: SHA-256\n");
1118 printf("type: %d\n", hash.hash_type);
1120 printf("offset: 0x%08x\n", hash.offset);
1121 printf("size: 0x%08x\n", hash.size);
1124 for (i = 0; i < hash.digest_size; i++)
1125 printf("%02x", hash.hash_digest[i]);
1127 } else if (0 == strcmp("reboot", cmd)) {
1129 enum ec_reboot_cmd cmd;
1131 if (argc >= 3 && !strcmp(argv[2], "cold"))
1132 cmd = EC_REBOOT_COLD;
1134 region = cros_ec_decode_region(argc - 2, argv + 2);
1135 if (region == EC_FLASH_REGION_RO)
1136 cmd = EC_REBOOT_JUMP_RO;
1137 else if (region == EC_FLASH_REGION_RW)
1138 cmd = EC_REBOOT_JUMP_RW;
1140 return CMD_RET_USAGE;
1143 if (cros_ec_reboot(dev, cmd, 0)) {
1144 debug("%s: Could not reboot KBC\n", __func__);
1147 } else if (0 == strcmp("events", cmd)) {
1150 if (cros_ec_get_host_events(dev, &events)) {
1151 debug("%s: Could not read host events\n", __func__);
1154 printf("0x%08x\n", events);
1155 } else if (0 == strcmp("clrevents", cmd)) {
1156 uint32_t events = 0x7fffffff;
1159 events = simple_strtol(argv[2], NULL, 0);
1161 if (cros_ec_clear_host_events(dev, events)) {
1162 debug("%s: Could not clear host events\n", __func__);
1165 } else if (0 == strcmp("read", cmd)) {
1166 ret = do_read_write(dev, 0, argc, argv);
1168 return CMD_RET_USAGE;
1169 } else if (0 == strcmp("write", cmd)) {
1170 ret = do_read_write(dev, 1, argc, argv);
1172 return CMD_RET_USAGE;
1173 } else if (0 == strcmp("erase", cmd)) {
1174 int region = cros_ec_decode_region(argc - 2, argv + 2);
1175 uint32_t offset, size;
1178 return CMD_RET_USAGE;
1179 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1180 debug("%s: Could not read region info\n", __func__);
1183 ret = cros_ec_flash_erase(dev, offset, size);
1185 debug("%s: Could not erase region\n",
1189 } else if (0 == strcmp("regioninfo", cmd)) {
1190 int region = cros_ec_decode_region(argc - 2, argv + 2);
1191 uint32_t offset, size;
1194 return CMD_RET_USAGE;
1195 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1197 debug("%s: Could not read region info\n", __func__);
1199 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1201 printf("Offset: %x\n", offset);
1202 printf("Size: %x\n", size);
1204 } else if (0 == strcmp("vbnvcontext", cmd)) {
1205 uint8_t block[EC_VBNV_BLOCK_SIZE];
1208 unsigned long result;
1211 ret = cros_ec_read_vbnvcontext(dev, block);
1213 printf("vbnv_block: ");
1214 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1215 printf("%02x", block[i]);
1220 * TODO(clchiou): Move this to a utility function as
1221 * cmd_spi might want to call it.
1223 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1224 len = strlen(argv[2]);
1226 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1229 buf[0] = argv[2][i * 2];
1230 if (i * 2 + 1 >= len)
1233 buf[1] = argv[2][i * 2 + 1];
1234 strict_strtoul(buf, 16, &result);
1237 ret = cros_ec_write_vbnvcontext(dev, block);
1240 debug("%s: Could not %s VbNvContext\n", __func__,
1241 argc <= 2 ? "read" : "write");
1243 } else if (0 == strcmp("test", cmd)) {
1244 int result = cros_ec_test(dev);
1247 printf("Test failed with error %d\n", result);
1249 puts("Test passed\n");
1250 } else if (0 == strcmp("version", cmd)) {
1251 struct ec_response_get_version *p;
1254 ret = cros_ec_read_version(dev, &p);
1256 /* Print versions */
1257 printf("RO version: %1.*s\n",
1258 sizeof(p->version_string_ro),
1259 p->version_string_ro);
1260 printf("RW version: %1.*s\n",
1261 sizeof(p->version_string_rw),
1262 p->version_string_rw);
1263 printf("Firmware copy: %s\n",
1265 ARRAY_SIZE(ec_current_image_name) ?
1266 ec_current_image_name[p->current_image] :
1268 ret = cros_ec_read_build_info(dev, &build_string);
1270 printf("Build info: %s\n", build_string);
1272 } else if (0 == strcmp("ldo", cmd)) {
1273 uint8_t index, state;
1277 return CMD_RET_USAGE;
1278 index = simple_strtoul(argv[2], &endp, 10);
1279 if (*argv[2] == 0 || *endp != 0)
1280 return CMD_RET_USAGE;
1282 state = simple_strtoul(argv[3], &endp, 10);
1283 if (*argv[3] == 0 || *endp != 0)
1284 return CMD_RET_USAGE;
1285 ret = cros_ec_set_ldo(dev, index, state);
1287 ret = cros_ec_get_ldo(dev, index, &state);
1289 printf("LDO%d: %s\n", index,
1290 state == EC_LDO_STATE_ON ?
1296 debug("%s: Could not access LDO%d\n", __func__, index);
1300 return CMD_RET_USAGE;
1304 printf("Error: CROS-EC command failed (error %d)\n", ret);
1312 crosec, 5, 1, do_cros_ec,
1313 "CROS-EC utility command",
1314 "init Re-init CROS-EC (done on startup automatically)\n"
1315 "crosec id Read CROS-EC ID\n"
1316 "crosec info Read CROS-EC info\n"
1317 "crosec curimage Read CROS-EC current image\n"
1318 "crosec hash Read CROS-EC hash\n"
1319 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1320 "crosec events Read CROS-EC host events\n"
1321 "crosec clrevents [mask] Clear CROS-EC host events\n"
1322 "crosec regioninfo <ro|rw> Read image info\n"
1323 "crosec erase <ro|rw> Erase EC image\n"
1324 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1325 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1326 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1327 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1328 "crosec test run tests on cros_ec\n"
1329 "crosec version Read CROS-EC version"