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 if (cmd_version != 0 && !dev->cmd_version_is_supported) {
136 debug("%s: Command version >0 unsupported\n", __func__);
139 len = send_command(dev, cmd, cmd_version, dout, dout_len,
142 /* If the command doesn't complete, wait a while */
143 if (len == -EC_RES_IN_PROGRESS) {
144 struct ec_response_get_comms_status *resp;
147 /* Wait for command to complete */
148 start = get_timer(0);
152 mdelay(50); /* Insert some reasonable delay */
153 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
155 (uint8_t **)&resp, sizeof(*resp));
159 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
160 debug("%s: Command %#02x timeout\n",
162 return -EC_RES_TIMEOUT;
164 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
166 /* OK it completed, so read the status response */
167 /* not sure why it was 0 for the last argument */
168 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
169 NULL, 0, &din, din_len);
172 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
174 /* If we have any data to return, it must be 64bit-aligned */
175 assert(len <= 0 || !((uintptr_t)din & 7));
183 * Send a command to the CROS-EC device and return the reply.
185 * The device's internal input/output buffers are used.
187 * @param dev CROS-EC device
188 * @param cmd Command to send (EC_CMD_...)
189 * @param cmd_version Version of command to send (EC_VER_...)
190 * @param dout Output data (may be NULL If dout_len=0)
191 * @param dout_len Size of output data in bytes
192 * @param din Response data (may be NULL If din_len=0).
193 * It not NULL, it is a place for ec_command() to copy the
195 * @param din_len Maximum size of response in bytes
196 * @return number of bytes in response, or -1 on error
198 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
199 const void *dout, int dout_len,
200 void *din, int din_len)
205 assert((din_len == 0) || din);
206 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
207 &in_buffer, din_len);
210 * If we were asked to put it somewhere, do so, otherwise just
211 * disregard the result.
213 if (din && in_buffer) {
214 assert(len <= din_len);
215 memmove(din, in_buffer, len);
221 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
223 if (ec_command(dev, EC_CMD_CROS_EC_STATE, 0, NULL, 0, scan,
224 sizeof(scan->data)) < sizeof(scan->data))
230 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
232 struct ec_response_get_version *r;
234 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
235 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
238 if (maxlen > sizeof(r->version_string_ro))
239 maxlen = sizeof(r->version_string_ro);
241 switch (r->current_image) {
243 memcpy(id, r->version_string_ro, maxlen);
246 memcpy(id, r->version_string_rw, maxlen);
252 id[maxlen - 1] = '\0';
256 int cros_ec_read_version(struct cros_ec_dev *dev,
257 struct ec_response_get_version **versionp)
259 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
260 (uint8_t **)versionp, sizeof(**versionp))
261 < sizeof(**versionp))
267 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
269 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
270 (uint8_t **)strp, EC_HOST_PARAM_SIZE) < 0)
276 int cros_ec_read_current_image(struct cros_ec_dev *dev,
277 enum ec_current_image *image)
279 struct ec_response_get_version *r;
281 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
282 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
285 *image = r->current_image;
289 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
290 struct ec_response_vboot_hash *hash)
292 struct ec_params_vboot_hash p;
295 start = get_timer(0);
296 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
297 mdelay(50); /* Insert some reasonable delay */
299 p.cmd = EC_VBOOT_HASH_GET;
300 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
301 hash, sizeof(*hash)) < 0)
304 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
305 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
306 return -EC_RES_TIMEOUT;
313 int cros_ec_read_hash(struct cros_ec_dev *dev,
314 struct ec_response_vboot_hash *hash)
316 struct ec_params_vboot_hash p;
319 p.cmd = EC_VBOOT_HASH_GET;
320 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
321 hash, sizeof(*hash)) < 0)
324 /* If the EC is busy calculating the hash, fidget until it's done. */
325 rv = cros_ec_wait_on_hash_done(dev, hash);
329 /* If the hash is valid, we're done. Otherwise, we have to kick it off
330 * again and wait for it to complete. Note that we explicitly assume
331 * that hashing zero bytes is always wrong, even though that would
332 * produce a valid hash value. */
333 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
336 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
337 __func__, hash->status, hash->size);
339 p.cmd = EC_VBOOT_HASH_RECALC;
340 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
342 p.offset = EC_VBOOT_HASH_OFFSET_RW;
344 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
345 hash, sizeof(*hash)) < 0)
348 rv = cros_ec_wait_on_hash_done(dev, hash);
352 debug("%s: hash done\n", __func__);
357 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
359 struct ec_params_vboot_hash p;
360 struct ec_response_vboot_hash *hash;
362 /* We don't have an explict command for the EC to discard its current
363 * hash value, so we'll just tell it to calculate one that we know is
364 * wrong (we claim that hashing zero bytes is always invalid).
366 p.cmd = EC_VBOOT_HASH_RECALC;
367 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
372 debug("%s:\n", __func__);
374 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
375 (uint8_t **)&hash, sizeof(*hash)) < 0)
378 /* No need to wait for it to finish */
382 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
385 struct ec_params_reboot_ec p;
390 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
394 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
396 * EC reboot will take place immediately so delay to allow it
397 * to complete. Note that some reboot types (EC_REBOOT_COLD)
398 * will reboot the AP as well, in which case we won't actually
402 * TODO(rspangler@chromium.org): Would be nice if we had a
403 * better way to determine when the reboot is complete. Could
404 * we poll a memory-mapped LPC value?
412 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
414 /* no interrupt support : always poll */
415 if (!fdt_gpio_isvalid(&dev->ec_int))
418 return !gpio_get_value(dev->ec_int.gpio);
421 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_cros_ec_info *info)
423 if (ec_command(dev, EC_CMD_CROS_EC_INFO, 0, NULL, 0, info,
424 sizeof(*info)) < sizeof(*info))
430 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
432 struct ec_response_host_event_mask *resp;
435 * Use the B copy of the event flags, because the main copy is already
438 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
439 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
442 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
445 *events_ptr = resp->mask;
449 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
451 struct ec_params_host_event_mask params;
453 params.mask = events;
456 * Use the B copy of the event flags, so it affects the data returned
457 * by cros_ec_get_host_events().
459 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
460 ¶ms, sizeof(params), NULL, 0) < 0)
466 int cros_ec_flash_protect(struct cros_ec_dev *dev,
467 uint32_t set_mask, uint32_t set_flags,
468 struct ec_response_flash_protect *resp)
470 struct ec_params_flash_protect params;
472 params.mask = set_mask;
473 params.flags = set_flags;
475 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
476 ¶ms, sizeof(params),
477 resp, sizeof(*resp)) < sizeof(*resp))
483 static int cros_ec_check_version(struct cros_ec_dev *dev)
485 struct ec_params_hello req;
486 struct ec_response_hello *resp;
488 #ifdef CONFIG_CROS_EC_LPC
489 /* LPC has its own way of doing this */
490 if (dev->interface == CROS_EC_IF_LPC)
491 return cros_ec_lpc_check_version(dev);
495 * TODO(sjg@chromium.org).
496 * There is a strange oddity here with the EC. We could just ignore
497 * the response, i.e. pass the last two parameters as NULL and 0.
498 * In this case we won't read back very many bytes from the EC.
499 * On the I2C bus the EC gets upset about this and will try to send
500 * the bytes anyway. This means that we will have to wait for that
501 * to complete before continuing with a new EC command.
503 * This problem is probably unique to the I2C bus.
505 * So for now, just read all the data anyway.
507 dev->cmd_version_is_supported = 1;
508 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
509 (uint8_t **)&resp, sizeof(*resp)) > 0) {
510 /* It appears to understand new version commands */
511 dev->cmd_version_is_supported = 1;
513 dev->cmd_version_is_supported = 0;
514 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req,
515 sizeof(req), (uint8_t **)&resp,
516 sizeof(*resp)) < 0) {
517 debug("%s: Failed both old and new command style\n",
526 int cros_ec_test(struct cros_ec_dev *dev)
528 struct ec_params_hello req;
529 struct ec_response_hello *resp;
531 req.in_data = 0x12345678;
532 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
533 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
534 printf("ec_command_inptr() returned error\n");
537 if (resp->out_data != req.in_data + 0x01020304) {
538 printf("Received invalid handshake %x\n", resp->out_data);
545 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
546 uint32_t *offset, uint32_t *size)
548 struct ec_params_flash_region_info p;
549 struct ec_response_flash_region_info *r;
553 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
554 EC_VER_FLASH_REGION_INFO,
555 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
556 if (ret != sizeof(*r))
567 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
569 struct ec_params_flash_erase p;
573 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
578 * Write a single block to the flash
580 * Write a block of data to the EC flash. The size must not exceed the flash
581 * write block size which you can obtain from cros_ec_flash_write_burst_size().
583 * The offset starts at 0. You can obtain the region information from
584 * cros_ec_flash_offset() to find out where to write for a particular region.
586 * Attempting to write to the region where the EC is currently running from
587 * will result in an error.
589 * @param dev CROS-EC device
590 * @param data Pointer to data buffer to write
591 * @param offset Offset within flash to write to.
592 * @param size Number of bytes to write
593 * @return 0 if ok, -1 on error
595 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
596 const uint8_t *data, uint32_t offset, uint32_t size)
598 struct ec_params_flash_write p;
602 assert(data && p.size <= sizeof(p.data));
603 memcpy(p.data, data, p.size);
605 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
606 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
610 * Return optimal flash write burst size
612 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
614 struct ec_params_flash_write p;
615 return sizeof(p.data);
619 * Check if a block of data is erased (all 0xff)
621 * This function is useful when dealing with flash, for checking whether a
622 * data block is erased and thus does not need to be programmed.
624 * @param data Pointer to data to check (must be word-aligned)
625 * @param size Number of bytes to check (must be word-aligned)
626 * @return 0 if erased, non-zero if any word is not erased
628 static int cros_ec_data_is_erased(const uint32_t *data, int size)
631 size /= sizeof(uint32_t);
632 for (; size > 0; size -= 4, data++)
639 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
640 uint32_t offset, uint32_t size)
642 uint32_t burst = cros_ec_flash_write_burst_size(dev);
647 * TODO: round up to the nearest multiple of write size. Can get away
648 * without that on link right now because its write size is 4 bytes.
651 for (off = offset; off < end; off += burst, data += burst) {
654 /* If the data is empty, there is no point in programming it */
655 todo = min(end - off, burst);
656 if (dev->optimise_flash_write &&
657 cros_ec_data_is_erased((uint32_t *)data, todo))
660 ret = cros_ec_flash_write_block(dev, data, off, todo);
669 * Read a single block from the flash
671 * Read a block of data from the EC flash. The size must not exceed the flash
672 * write block size which you can obtain from cros_ec_flash_write_burst_size().
674 * The offset starts at 0. You can obtain the region information from
675 * cros_ec_flash_offset() to find out where to read for a particular region.
677 * @param dev CROS-EC device
678 * @param data Pointer to data buffer to read into
679 * @param offset Offset within flash to read from
680 * @param size Number of bytes to read
681 * @return 0 if ok, -1 on error
683 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
684 uint32_t offset, uint32_t size)
686 struct ec_params_flash_read p;
691 return ec_command(dev, EC_CMD_FLASH_READ, 0,
692 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
695 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
698 uint32_t burst = cros_ec_flash_write_burst_size(dev);
703 for (off = offset; off < end; off += burst, data += burst) {
704 ret = cros_ec_flash_read_block(dev, data, off,
705 min(end - off, burst));
713 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
714 const uint8_t *image, int image_size)
716 uint32_t rw_offset, rw_size;
719 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
721 if (image_size > rw_size)
724 /* Invalidate the existing hash, just in case the AP reboots
725 * unexpectedly during the update. If that happened, the EC RW firmware
726 * would be invalid, but the EC would still have the original hash.
728 ret = cros_ec_invalidate_hash(dev);
733 * Erase the entire RW section, so that the EC doesn't see any garbage
734 * past the new image if it's smaller than the current image.
736 * TODO: could optimize this to erase just the current image, since
737 * presumably everything past that is 0xff's. But would still need to
738 * round up to the nearest multiple of erase size.
740 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
744 /* Write the image */
745 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
752 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
754 struct ec_params_vbnvcontext p;
757 p.op = EC_VBNV_CONTEXT_OP_READ;
759 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
760 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
761 if (len < EC_VBNV_BLOCK_SIZE)
767 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
769 struct ec_params_vbnvcontext p;
772 p.op = EC_VBNV_CONTEXT_OP_WRITE;
773 memcpy(p.block, block, sizeof(p.block));
775 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
776 &p, sizeof(p), NULL, 0);
783 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
785 struct ec_params_ldo_set params;
787 params.index = index;
788 params.state = state;
790 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
791 ¶ms, sizeof(params),
798 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
800 struct ec_params_ldo_get params;
801 struct ec_response_ldo_get *resp;
803 params.index = index;
805 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
806 ¶ms, sizeof(params),
807 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
810 *state = resp->state;
816 * Decode MBKP details from the device tree and allocate a suitable device.
818 * @param blob Device tree blob
819 * @param node Node to decode from
820 * @param devp Returns a pointer to the new allocated device
821 * @return 0 if ok, -1 on error
823 static int cros_ec_decode_fdt(const void *blob, int node,
824 struct cros_ec_dev **devp)
826 enum fdt_compat_id compat;
827 struct cros_ec_dev *dev;
830 /* See what type of parent we are inside (this is expensive) */
831 parent = fdt_parent_offset(blob, node);
833 debug("%s: Cannot find node parent\n", __func__);
839 dev->parent_node = parent;
841 compat = fdtdec_lookup(blob, parent);
843 #ifdef CONFIG_CROS_EC_SPI
844 case COMPAT_SAMSUNG_EXYNOS_SPI:
845 dev->interface = CROS_EC_IF_SPI;
846 if (cros_ec_spi_decode_fdt(dev, blob))
850 #ifdef CONFIG_CROS_EC_I2C
851 case COMPAT_SAMSUNG_S3C2440_I2C:
852 dev->interface = CROS_EC_IF_I2C;
853 if (cros_ec_i2c_decode_fdt(dev, blob))
857 #ifdef CONFIG_CROS_EC_LPC
858 case COMPAT_INTEL_LPC:
859 dev->interface = CROS_EC_IF_LPC;
863 debug("%s: Unknown compat id %d\n", __func__, compat);
867 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
868 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
869 "optimise-flash-write");
875 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
878 struct cros_ec_dev *dev;
883 node = fdtdec_next_compatible(blob, node,
884 COMPAT_GOOGLE_CROS_EC);
886 debug("%s: Node not found\n", __func__);
889 } while (!fdtdec_get_is_enabled(blob, node));
891 if (cros_ec_decode_fdt(blob, node, &dev)) {
892 debug("%s: Failed to decode device.\n", __func__);
893 return -CROS_EC_ERR_FDT_DECODE;
896 switch (dev->interface) {
897 #ifdef CONFIG_CROS_EC_SPI
899 if (cros_ec_spi_init(dev, blob)) {
900 debug("%s: Could not setup SPI interface\n", __func__);
901 return -CROS_EC_ERR_DEV_INIT;
905 #ifdef CONFIG_CROS_EC_I2C
907 if (cros_ec_i2c_init(dev, blob))
908 return -CROS_EC_ERR_DEV_INIT;
911 #ifdef CONFIG_CROS_EC_LPC
913 if (cros_ec_lpc_init(dev, blob))
914 return -CROS_EC_ERR_DEV_INIT;
917 case CROS_EC_IF_NONE:
922 /* we will poll the EC interrupt line */
923 fdtdec_setup_gpio(&dev->ec_int);
924 if (fdt_gpio_isvalid(&dev->ec_int))
925 gpio_direction_input(dev->ec_int.gpio);
927 if (cros_ec_check_version(dev)) {
928 debug("%s: Could not detect CROS-EC version\n", __func__);
929 return -CROS_EC_ERR_CHECK_VERSION;
932 if (cros_ec_read_id(dev, id, sizeof(id))) {
933 debug("%s: Could not read KBC ID\n", __func__);
934 return -CROS_EC_ERR_READ_ID;
937 /* Remember this device for use by the cros_ec command */
938 last_dev = *cros_ecp = dev;
939 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
944 #ifdef CONFIG_CMD_CROS_EC
945 int cros_ec_decode_region(int argc, char * const argv[])
948 if (0 == strcmp(*argv, "rw"))
949 return EC_FLASH_REGION_RW;
950 else if (0 == strcmp(*argv, "ro"))
951 return EC_FLASH_REGION_RO;
953 debug("%s: Invalid region '%s'\n", __func__, *argv);
955 debug("%s: Missing region parameter\n", __func__);
962 * Perform a flash read or write command
964 * @param dev CROS-EC device to read/write
965 * @param is_write 1 do to a write, 0 to do a read
966 * @param argc Number of arguments
967 * @param argv Arguments (2 is region, 3 is address)
968 * @return 0 for ok, 1 for a usage error or -ve for ec command error
969 * (negative EC_RES_...)
971 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
974 uint32_t offset, size = -1U, region_size;
980 region = cros_ec_decode_region(argc - 2, argv + 2);
985 addr = simple_strtoul(argv[3], &endp, 16);
986 if (*argv[3] == 0 || *endp != 0)
989 size = simple_strtoul(argv[4], &endp, 16);
990 if (*argv[4] == 0 || *endp != 0)
994 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
996 debug("%s: Could not read region info\n", __func__);
1003 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1004 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1006 debug("%s: Could not %s region\n", __func__,
1007 is_write ? "write" : "read");
1014 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1016 struct cros_ec_dev *dev = last_dev;
1021 return CMD_RET_USAGE;
1024 if (0 == strcmp("init", cmd)) {
1025 ret = cros_ec_init(gd->fdt_blob, &dev);
1027 printf("Could not init cros_ec device (err %d)\n", ret);
1033 /* Just use the last allocated device; there should be only one */
1035 printf("No CROS-EC device available\n");
1038 if (0 == strcmp("id", cmd)) {
1041 if (cros_ec_read_id(dev, id, sizeof(id))) {
1042 debug("%s: Could not read KBC ID\n", __func__);
1046 } else if (0 == strcmp("info", cmd)) {
1047 struct ec_response_cros_ec_info info;
1049 if (cros_ec_info(dev, &info)) {
1050 debug("%s: Could not read KBC info\n", __func__);
1053 printf("rows = %u\n", info.rows);
1054 printf("cols = %u\n", info.cols);
1055 printf("switches = %#x\n", info.switches);
1056 } else if (0 == strcmp("curimage", cmd)) {
1057 enum ec_current_image image;
1059 if (cros_ec_read_current_image(dev, &image)) {
1060 debug("%s: Could not read KBC image\n", __func__);
1063 printf("%d\n", image);
1064 } else if (0 == strcmp("hash", cmd)) {
1065 struct ec_response_vboot_hash hash;
1068 if (cros_ec_read_hash(dev, &hash)) {
1069 debug("%s: Could not read KBC hash\n", __func__);
1073 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1074 printf("type: SHA-256\n");
1076 printf("type: %d\n", hash.hash_type);
1078 printf("offset: 0x%08x\n", hash.offset);
1079 printf("size: 0x%08x\n", hash.size);
1082 for (i = 0; i < hash.digest_size; i++)
1083 printf("%02x", hash.hash_digest[i]);
1085 } else if (0 == strcmp("reboot", cmd)) {
1087 enum ec_reboot_cmd cmd;
1089 if (argc >= 3 && !strcmp(argv[2], "cold"))
1090 cmd = EC_REBOOT_COLD;
1092 region = cros_ec_decode_region(argc - 2, argv + 2);
1093 if (region == EC_FLASH_REGION_RO)
1094 cmd = EC_REBOOT_JUMP_RO;
1095 else if (region == EC_FLASH_REGION_RW)
1096 cmd = EC_REBOOT_JUMP_RW;
1098 return CMD_RET_USAGE;
1101 if (cros_ec_reboot(dev, cmd, 0)) {
1102 debug("%s: Could not reboot KBC\n", __func__);
1105 } else if (0 == strcmp("events", cmd)) {
1108 if (cros_ec_get_host_events(dev, &events)) {
1109 debug("%s: Could not read host events\n", __func__);
1112 printf("0x%08x\n", events);
1113 } else if (0 == strcmp("clrevents", cmd)) {
1114 uint32_t events = 0x7fffffff;
1117 events = simple_strtol(argv[2], NULL, 0);
1119 if (cros_ec_clear_host_events(dev, events)) {
1120 debug("%s: Could not clear host events\n", __func__);
1123 } else if (0 == strcmp("read", cmd)) {
1124 ret = do_read_write(dev, 0, argc, argv);
1126 return CMD_RET_USAGE;
1127 } else if (0 == strcmp("write", cmd)) {
1128 ret = do_read_write(dev, 1, argc, argv);
1130 return CMD_RET_USAGE;
1131 } else if (0 == strcmp("erase", cmd)) {
1132 int region = cros_ec_decode_region(argc - 2, argv + 2);
1133 uint32_t offset, size;
1136 return CMD_RET_USAGE;
1137 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1138 debug("%s: Could not read region info\n", __func__);
1141 ret = cros_ec_flash_erase(dev, offset, size);
1143 debug("%s: Could not erase region\n",
1147 } else if (0 == strcmp("regioninfo", cmd)) {
1148 int region = cros_ec_decode_region(argc - 2, argv + 2);
1149 uint32_t offset, size;
1152 return CMD_RET_USAGE;
1153 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1155 debug("%s: Could not read region info\n", __func__);
1157 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1159 printf("Offset: %x\n", offset);
1160 printf("Size: %x\n", size);
1162 } else if (0 == strcmp("vbnvcontext", cmd)) {
1163 uint8_t block[EC_VBNV_BLOCK_SIZE];
1166 unsigned long result;
1169 ret = cros_ec_read_vbnvcontext(dev, block);
1171 printf("vbnv_block: ");
1172 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1173 printf("%02x", block[i]);
1178 * TODO(clchiou): Move this to a utility function as
1179 * cmd_spi might want to call it.
1181 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1182 len = strlen(argv[2]);
1184 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1187 buf[0] = argv[2][i * 2];
1188 if (i * 2 + 1 >= len)
1191 buf[1] = argv[2][i * 2 + 1];
1192 strict_strtoul(buf, 16, &result);
1195 ret = cros_ec_write_vbnvcontext(dev, block);
1198 debug("%s: Could not %s VbNvContext\n", __func__,
1199 argc <= 2 ? "read" : "write");
1201 } else if (0 == strcmp("test", cmd)) {
1202 int result = cros_ec_test(dev);
1205 printf("Test failed with error %d\n", result);
1207 puts("Test passed\n");
1208 } else if (0 == strcmp("version", cmd)) {
1209 struct ec_response_get_version *p;
1212 ret = cros_ec_read_version(dev, &p);
1214 /* Print versions */
1215 printf("RO version: %1.*s\n",
1216 sizeof(p->version_string_ro),
1217 p->version_string_ro);
1218 printf("RW version: %1.*s\n",
1219 sizeof(p->version_string_rw),
1220 p->version_string_rw);
1221 printf("Firmware copy: %s\n",
1223 ARRAY_SIZE(ec_current_image_name) ?
1224 ec_current_image_name[p->current_image] :
1226 ret = cros_ec_read_build_info(dev, &build_string);
1228 printf("Build info: %s\n", build_string);
1230 } else if (0 == strcmp("ldo", cmd)) {
1231 uint8_t index, state;
1235 return CMD_RET_USAGE;
1236 index = simple_strtoul(argv[2], &endp, 10);
1237 if (*argv[2] == 0 || *endp != 0)
1238 return CMD_RET_USAGE;
1240 state = simple_strtoul(argv[3], &endp, 10);
1241 if (*argv[3] == 0 || *endp != 0)
1242 return CMD_RET_USAGE;
1243 ret = cros_ec_set_ldo(dev, index, state);
1245 ret = cros_ec_get_ldo(dev, index, &state);
1247 printf("LDO%d: %s\n", index,
1248 state == EC_LDO_STATE_ON ?
1254 debug("%s: Could not access LDO%d\n", __func__, index);
1258 return CMD_RET_USAGE;
1262 printf("Error: CROS-EC command failed (error %d)\n", ret);
1270 crosec, 5, 1, do_cros_ec,
1271 "CROS-EC utility command",
1272 "init Re-init CROS-EC (done on startup automatically)\n"
1273 "crosec id Read CROS-EC ID\n"
1274 "crosec info Read CROS-EC info\n"
1275 "crosec curimage Read CROS-EC current image\n"
1276 "crosec hash Read CROS-EC hash\n"
1277 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1278 "crosec events Read CROS-EC host events\n"
1279 "crosec clrevents [mask] Clear CROS-EC host events\n"
1280 "crosec regioninfo <ro|rw> Read image info\n"
1281 "crosec erase <ro|rw> Erase EC image\n"
1282 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1283 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1284 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1285 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1286 "crosec test run tests on cros_ec\n"
1287 "crosec version Read CROS-EC version"