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++)
78 * Create a request packet for protocol version 3.
80 * The packet is stored in the device's internal output buffer.
82 * @param dev CROS-EC device
83 * @param cmd Command to send (EC_CMD_...)
84 * @param cmd_version Version of command to send (EC_VER_...)
85 * @param dout Output data (may be NULL If dout_len=0)
86 * @param dout_len Size of output data in bytes
87 * @return packet size in bytes, or <0 if error.
89 static int create_proto3_request(struct cros_ec_dev *dev,
90 int cmd, int cmd_version,
91 const void *dout, int dout_len)
93 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
94 int out_bytes = dout_len + sizeof(*rq);
96 /* Fail if output size is too big */
97 if (out_bytes > (int)sizeof(dev->dout)) {
98 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
99 return -EC_RES_REQUEST_TRUNCATED;
102 /* Fill in request packet */
103 rq->struct_version = EC_HOST_REQUEST_VERSION;
106 rq->command_version = cmd_version;
108 rq->data_len = dout_len;
110 /* Copy data after header */
111 memcpy(rq + 1, dout, dout_len);
113 /* Write checksum field so the entire packet sums to 0 */
114 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
116 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
118 /* Return size of request packet */
123 * Prepare the device to receive a protocol version 3 response.
125 * @param dev CROS-EC device
126 * @param din_len Maximum size of response in bytes
127 * @return maximum expected number of bytes in response, or <0 if error.
129 static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
131 int in_bytes = din_len + sizeof(struct ec_host_response);
133 /* Fail if input size is too big */
134 if (in_bytes > (int)sizeof(dev->din)) {
135 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
136 return -EC_RES_RESPONSE_TOO_BIG;
139 /* Return expected size of response packet */
144 * Handle a protocol version 3 response packet.
146 * The packet must already be stored in the device's internal input buffer.
148 * @param dev CROS-EC device
149 * @param dinp Returns pointer to response data
150 * @param din_len Maximum size of response in bytes
151 * @return number of bytes of response data, or <0 if error
153 static int handle_proto3_response(struct cros_ec_dev *dev,
154 uint8_t **dinp, int din_len)
156 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
160 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
162 /* Check input data */
163 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
164 debug("%s: EC response version mismatch\n", __func__);
165 return -EC_RES_INVALID_RESPONSE;
169 debug("%s: EC response reserved != 0\n", __func__);
170 return -EC_RES_INVALID_RESPONSE;
173 if (rs->data_len > din_len) {
174 debug("%s: EC returned too much data\n", __func__);
175 return -EC_RES_RESPONSE_TOO_BIG;
178 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
180 /* Update in_bytes to actual data size */
181 in_bytes = sizeof(*rs) + rs->data_len;
183 /* Verify checksum */
184 csum = cros_ec_calc_checksum(dev->din, in_bytes);
186 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
188 return -EC_RES_INVALID_CHECKSUM;
191 /* Return error result, if any */
193 return -(int)rs->result;
195 /* If we're still here, set response data pointer and return length */
196 *dinp = (uint8_t *)(rs + 1);
201 static int send_command_proto3(struct cros_ec_dev *dev,
202 int cmd, int cmd_version,
203 const void *dout, int dout_len,
204 uint8_t **dinp, int din_len)
206 int out_bytes, in_bytes;
209 /* Create request packet */
210 out_bytes = create_proto3_request(dev, cmd, cmd_version,
215 /* Prepare response buffer */
216 in_bytes = prepare_proto3_response_buffer(dev, din_len);
220 switch (dev->interface) {
221 case CROS_EC_IF_NONE:
222 /* TODO: support protocol 3 for LPC, I2C; for now fall through */
224 debug("%s: Unsupported interface\n", __func__);
230 /* Process the response */
231 return handle_proto3_response(dev, dinp, din_len);
234 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
235 const void *dout, int dout_len,
236 uint8_t **dinp, int din_len)
240 /* Handle protocol version 3 support */
241 if (dev->protocol_version == 3) {
242 return send_command_proto3(dev, cmd, cmd_version,
243 dout, dout_len, dinp, din_len);
246 switch (dev->interface) {
247 #ifdef CONFIG_CROS_EC_SPI
249 ret = cros_ec_spi_command(dev, cmd, cmd_version,
250 (const uint8_t *)dout, dout_len,
254 #ifdef CONFIG_CROS_EC_I2C
256 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
257 (const uint8_t *)dout, dout_len,
261 #ifdef CONFIG_CROS_EC_LPC
263 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
264 (const uint8_t *)dout, dout_len,
268 case CROS_EC_IF_NONE:
277 * Send a command to the CROS-EC device and return the reply.
279 * The device's internal input/output buffers are used.
281 * @param dev CROS-EC device
282 * @param cmd Command to send (EC_CMD_...)
283 * @param cmd_version Version of command to send (EC_VER_...)
284 * @param dout Output data (may be NULL If dout_len=0)
285 * @param dout_len Size of output data in bytes
286 * @param dinp Response data (may be NULL If din_len=0).
287 * If not NULL, it will be updated to point to the data
288 * and will always be double word aligned (64-bits)
289 * @param din_len Maximum size of response in bytes
290 * @return number of bytes in response, or -1 on error
292 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
293 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
299 len = send_command(dev, cmd, cmd_version, dout, dout_len,
302 /* If the command doesn't complete, wait a while */
303 if (len == -EC_RES_IN_PROGRESS) {
304 struct ec_response_get_comms_status *resp;
307 /* Wait for command to complete */
308 start = get_timer(0);
312 mdelay(50); /* Insert some reasonable delay */
313 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
315 (uint8_t **)&resp, sizeof(*resp));
319 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
320 debug("%s: Command %#02x timeout\n",
322 return -EC_RES_TIMEOUT;
324 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
326 /* OK it completed, so read the status response */
327 /* not sure why it was 0 for the last argument */
328 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
329 NULL, 0, &din, din_len);
332 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp, *dinp);
334 /* If we have any data to return, it must be 64bit-aligned */
335 assert(len <= 0 || !((uintptr_t)din & 7));
343 * Send a command to the CROS-EC device and return the reply.
345 * The device's internal input/output buffers are used.
347 * @param dev CROS-EC device
348 * @param cmd Command to send (EC_CMD_...)
349 * @param cmd_version Version of command to send (EC_VER_...)
350 * @param dout Output data (may be NULL If dout_len=0)
351 * @param dout_len Size of output data in bytes
352 * @param din Response data (may be NULL If din_len=0).
353 * It not NULL, it is a place for ec_command() to copy the
355 * @param din_len Maximum size of response in bytes
356 * @return number of bytes in response, or -1 on error
358 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
359 const void *dout, int dout_len,
360 void *din, int din_len)
365 assert((din_len == 0) || din);
366 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
367 &in_buffer, din_len);
370 * If we were asked to put it somewhere, do so, otherwise just
371 * disregard the result.
373 if (din && in_buffer) {
374 assert(len <= din_len);
375 memmove(din, in_buffer, len);
381 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
383 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
384 sizeof(scan->data)) < sizeof(scan->data))
390 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
392 struct ec_response_get_version *r;
394 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
395 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
398 if (maxlen > sizeof(r->version_string_ro))
399 maxlen = sizeof(r->version_string_ro);
401 switch (r->current_image) {
403 memcpy(id, r->version_string_ro, maxlen);
406 memcpy(id, r->version_string_rw, maxlen);
412 id[maxlen - 1] = '\0';
416 int cros_ec_read_version(struct cros_ec_dev *dev,
417 struct ec_response_get_version **versionp)
419 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
420 (uint8_t **)versionp, sizeof(**versionp))
421 < sizeof(**versionp))
427 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
429 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
430 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
436 int cros_ec_read_current_image(struct cros_ec_dev *dev,
437 enum ec_current_image *image)
439 struct ec_response_get_version *r;
441 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
442 (uint8_t **)&r, sizeof(*r)) < sizeof(*r))
445 *image = r->current_image;
449 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
450 struct ec_response_vboot_hash *hash)
452 struct ec_params_vboot_hash p;
455 start = get_timer(0);
456 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
457 mdelay(50); /* Insert some reasonable delay */
459 p.cmd = EC_VBOOT_HASH_GET;
460 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
461 hash, sizeof(*hash)) < 0)
464 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
465 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
466 return -EC_RES_TIMEOUT;
473 int cros_ec_read_hash(struct cros_ec_dev *dev,
474 struct ec_response_vboot_hash *hash)
476 struct ec_params_vboot_hash p;
479 p.cmd = EC_VBOOT_HASH_GET;
480 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
481 hash, sizeof(*hash)) < 0)
484 /* If the EC is busy calculating the hash, fidget until it's done. */
485 rv = cros_ec_wait_on_hash_done(dev, hash);
489 /* If the hash is valid, we're done. Otherwise, we have to kick it off
490 * again and wait for it to complete. Note that we explicitly assume
491 * that hashing zero bytes is always wrong, even though that would
492 * produce a valid hash value. */
493 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
496 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
497 __func__, hash->status, hash->size);
499 p.cmd = EC_VBOOT_HASH_START;
500 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
502 p.offset = EC_VBOOT_HASH_OFFSET_RW;
504 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
505 hash, sizeof(*hash)) < 0)
508 rv = cros_ec_wait_on_hash_done(dev, hash);
512 debug("%s: hash done\n", __func__);
517 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
519 struct ec_params_vboot_hash p;
520 struct ec_response_vboot_hash *hash;
522 /* We don't have an explict command for the EC to discard its current
523 * hash value, so we'll just tell it to calculate one that we know is
524 * wrong (we claim that hashing zero bytes is always invalid).
526 p.cmd = EC_VBOOT_HASH_RECALC;
527 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
532 debug("%s:\n", __func__);
534 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
535 (uint8_t **)&hash, sizeof(*hash)) < 0)
538 /* No need to wait for it to finish */
542 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
545 struct ec_params_reboot_ec p;
550 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
554 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
556 * EC reboot will take place immediately so delay to allow it
557 * to complete. Note that some reboot types (EC_REBOOT_COLD)
558 * will reboot the AP as well, in which case we won't actually
562 * TODO(rspangler@chromium.org): Would be nice if we had a
563 * better way to determine when the reboot is complete. Could
564 * we poll a memory-mapped LPC value?
572 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
574 /* no interrupt support : always poll */
575 if (!fdt_gpio_isvalid(&dev->ec_int))
578 return !gpio_get_value(dev->ec_int.gpio);
581 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
583 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
584 sizeof(*info)) < sizeof(*info))
590 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
592 struct ec_response_host_event_mask *resp;
595 * Use the B copy of the event flags, because the main copy is already
598 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
599 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
602 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
605 *events_ptr = resp->mask;
609 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
611 struct ec_params_host_event_mask params;
613 params.mask = events;
616 * Use the B copy of the event flags, so it affects the data returned
617 * by cros_ec_get_host_events().
619 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
620 ¶ms, sizeof(params), NULL, 0) < 0)
626 int cros_ec_flash_protect(struct cros_ec_dev *dev,
627 uint32_t set_mask, uint32_t set_flags,
628 struct ec_response_flash_protect *resp)
630 struct ec_params_flash_protect params;
632 params.mask = set_mask;
633 params.flags = set_flags;
635 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
636 ¶ms, sizeof(params),
637 resp, sizeof(*resp)) < sizeof(*resp))
643 static int cros_ec_check_version(struct cros_ec_dev *dev)
645 struct ec_params_hello req;
646 struct ec_response_hello *resp;
648 #ifdef CONFIG_CROS_EC_LPC
649 /* LPC has its own way of doing this */
650 if (dev->interface == CROS_EC_IF_LPC)
651 return cros_ec_lpc_check_version(dev);
655 * TODO(sjg@chromium.org).
656 * There is a strange oddity here with the EC. We could just ignore
657 * the response, i.e. pass the last two parameters as NULL and 0.
658 * In this case we won't read back very many bytes from the EC.
659 * On the I2C bus the EC gets upset about this and will try to send
660 * the bytes anyway. This means that we will have to wait for that
661 * to complete before continuing with a new EC command.
663 * This problem is probably unique to the I2C bus.
665 * So for now, just read all the data anyway.
668 /* Try sending a version 2 packet */
669 dev->protocol_version = 2;
670 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
671 (uint8_t **)&resp, sizeof(*resp)) > 0) {
676 * Fail if we're still here, since the EC doesn't understand any
677 * protcol version we speak. Version 1 interface without command
678 * version is no longer supported, and we don't know about any new
681 dev->protocol_version = 0;
682 printf("%s: ERROR: old EC interface not supported\n", __func__);
686 int cros_ec_test(struct cros_ec_dev *dev)
688 struct ec_params_hello req;
689 struct ec_response_hello *resp;
691 req.in_data = 0x12345678;
692 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
693 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
694 printf("ec_command_inptr() returned error\n");
697 if (resp->out_data != req.in_data + 0x01020304) {
698 printf("Received invalid handshake %x\n", resp->out_data);
705 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
706 uint32_t *offset, uint32_t *size)
708 struct ec_params_flash_region_info p;
709 struct ec_response_flash_region_info *r;
713 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
714 EC_VER_FLASH_REGION_INFO,
715 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
716 if (ret != sizeof(*r))
727 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
729 struct ec_params_flash_erase p;
733 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
738 * Write a single block to the flash
740 * Write a block of data to the EC flash. The size must not exceed the flash
741 * write block size which you can obtain from cros_ec_flash_write_burst_size().
743 * The offset starts at 0. You can obtain the region information from
744 * cros_ec_flash_offset() to find out where to write for a particular region.
746 * Attempting to write to the region where the EC is currently running from
747 * will result in an error.
749 * @param dev CROS-EC device
750 * @param data Pointer to data buffer to write
751 * @param offset Offset within flash to write to.
752 * @param size Number of bytes to write
753 * @return 0 if ok, -1 on error
755 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
756 const uint8_t *data, uint32_t offset, uint32_t size)
758 struct ec_params_flash_write p;
762 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
763 memcpy(&p + 1, data, p.size);
765 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
766 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
770 * Return optimal flash write burst size
772 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
774 return EC_FLASH_WRITE_VER0_SIZE;
778 * Check if a block of data is erased (all 0xff)
780 * This function is useful when dealing with flash, for checking whether a
781 * data block is erased and thus does not need to be programmed.
783 * @param data Pointer to data to check (must be word-aligned)
784 * @param size Number of bytes to check (must be word-aligned)
785 * @return 0 if erased, non-zero if any word is not erased
787 static int cros_ec_data_is_erased(const uint32_t *data, int size)
790 size /= sizeof(uint32_t);
791 for (; size > 0; size -= 4, data++)
798 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
799 uint32_t offset, uint32_t size)
801 uint32_t burst = cros_ec_flash_write_burst_size(dev);
806 * TODO: round up to the nearest multiple of write size. Can get away
807 * without that on link right now because its write size is 4 bytes.
810 for (off = offset; off < end; off += burst, data += burst) {
813 /* If the data is empty, there is no point in programming it */
814 todo = min(end - off, burst);
815 if (dev->optimise_flash_write &&
816 cros_ec_data_is_erased((uint32_t *)data, todo))
819 ret = cros_ec_flash_write_block(dev, data, off, todo);
828 * Read a single block from the flash
830 * Read a block of data from the EC flash. The size must not exceed the flash
831 * write block size which you can obtain from cros_ec_flash_write_burst_size().
833 * The offset starts at 0. You can obtain the region information from
834 * cros_ec_flash_offset() to find out where to read for a particular region.
836 * @param dev CROS-EC device
837 * @param data Pointer to data buffer to read into
838 * @param offset Offset within flash to read from
839 * @param size Number of bytes to read
840 * @return 0 if ok, -1 on error
842 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
843 uint32_t offset, uint32_t size)
845 struct ec_params_flash_read p;
850 return ec_command(dev, EC_CMD_FLASH_READ, 0,
851 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
854 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
857 uint32_t burst = cros_ec_flash_write_burst_size(dev);
862 for (off = offset; off < end; off += burst, data += burst) {
863 ret = cros_ec_flash_read_block(dev, data, off,
864 min(end - off, burst));
872 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
873 const uint8_t *image, int image_size)
875 uint32_t rw_offset, rw_size;
878 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
880 if (image_size > rw_size)
883 /* Invalidate the existing hash, just in case the AP reboots
884 * unexpectedly during the update. If that happened, the EC RW firmware
885 * would be invalid, but the EC would still have the original hash.
887 ret = cros_ec_invalidate_hash(dev);
892 * Erase the entire RW section, so that the EC doesn't see any garbage
893 * past the new image if it's smaller than the current image.
895 * TODO: could optimize this to erase just the current image, since
896 * presumably everything past that is 0xff's. But would still need to
897 * round up to the nearest multiple of erase size.
899 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
903 /* Write the image */
904 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
911 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
913 struct ec_params_vbnvcontext p;
916 p.op = EC_VBNV_CONTEXT_OP_READ;
918 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
919 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
920 if (len < EC_VBNV_BLOCK_SIZE)
926 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
928 struct ec_params_vbnvcontext p;
931 p.op = EC_VBNV_CONTEXT_OP_WRITE;
932 memcpy(p.block, block, sizeof(p.block));
934 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
935 &p, sizeof(p), NULL, 0);
942 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
944 struct ec_params_ldo_set params;
946 params.index = index;
947 params.state = state;
949 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
950 ¶ms, sizeof(params),
957 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
959 struct ec_params_ldo_get params;
960 struct ec_response_ldo_get *resp;
962 params.index = index;
964 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
965 ¶ms, sizeof(params),
966 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp))
969 *state = resp->state;
975 * Decode EC interface details from the device tree and allocate a suitable
978 * @param blob Device tree blob
979 * @param node Node to decode from
980 * @param devp Returns a pointer to the new allocated device
981 * @return 0 if ok, -1 on error
983 static int cros_ec_decode_fdt(const void *blob, int node,
984 struct cros_ec_dev **devp)
986 enum fdt_compat_id compat;
987 struct cros_ec_dev *dev;
990 /* See what type of parent we are inside (this is expensive) */
991 parent = fdt_parent_offset(blob, node);
993 debug("%s: Cannot find node parent\n", __func__);
999 dev->parent_node = parent;
1001 compat = fdtdec_lookup(blob, parent);
1003 #ifdef CONFIG_CROS_EC_SPI
1004 case COMPAT_SAMSUNG_EXYNOS_SPI:
1005 dev->interface = CROS_EC_IF_SPI;
1006 if (cros_ec_spi_decode_fdt(dev, blob))
1010 #ifdef CONFIG_CROS_EC_I2C
1011 case COMPAT_SAMSUNG_S3C2440_I2C:
1012 dev->interface = CROS_EC_IF_I2C;
1013 if (cros_ec_i2c_decode_fdt(dev, blob))
1017 #ifdef CONFIG_CROS_EC_LPC
1018 case COMPAT_INTEL_LPC:
1019 dev->interface = CROS_EC_IF_LPC;
1023 debug("%s: Unknown compat id %d\n", __func__, compat);
1027 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
1028 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
1029 "optimise-flash-write");
1035 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
1038 struct cros_ec_dev *dev;
1043 node = fdtdec_next_compatible(blob, node,
1044 COMPAT_GOOGLE_CROS_EC);
1046 debug("%s: Node not found\n", __func__);
1049 } while (!fdtdec_get_is_enabled(blob, node));
1051 if (cros_ec_decode_fdt(blob, node, &dev)) {
1052 debug("%s: Failed to decode device.\n", __func__);
1053 return -CROS_EC_ERR_FDT_DECODE;
1056 switch (dev->interface) {
1057 #ifdef CONFIG_CROS_EC_SPI
1058 case CROS_EC_IF_SPI:
1059 if (cros_ec_spi_init(dev, blob)) {
1060 debug("%s: Could not setup SPI interface\n", __func__);
1061 return -CROS_EC_ERR_DEV_INIT;
1065 #ifdef CONFIG_CROS_EC_I2C
1066 case CROS_EC_IF_I2C:
1067 if (cros_ec_i2c_init(dev, blob))
1068 return -CROS_EC_ERR_DEV_INIT;
1071 #ifdef CONFIG_CROS_EC_LPC
1072 case CROS_EC_IF_LPC:
1073 if (cros_ec_lpc_init(dev, blob))
1074 return -CROS_EC_ERR_DEV_INIT;
1077 case CROS_EC_IF_NONE:
1082 /* we will poll the EC interrupt line */
1083 fdtdec_setup_gpio(&dev->ec_int);
1084 if (fdt_gpio_isvalid(&dev->ec_int))
1085 gpio_direction_input(dev->ec_int.gpio);
1087 if (cros_ec_check_version(dev)) {
1088 debug("%s: Could not detect CROS-EC version\n", __func__);
1089 return -CROS_EC_ERR_CHECK_VERSION;
1092 if (cros_ec_read_id(dev, id, sizeof(id))) {
1093 debug("%s: Could not read KBC ID\n", __func__);
1094 return -CROS_EC_ERR_READ_ID;
1097 /* Remember this device for use by the cros_ec command */
1098 last_dev = *cros_ecp = dev;
1099 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1104 int cros_ec_decode_region(int argc, char * const argv[])
1107 if (0 == strcmp(*argv, "rw"))
1108 return EC_FLASH_REGION_RW;
1109 else if (0 == strcmp(*argv, "ro"))
1110 return EC_FLASH_REGION_RO;
1112 debug("%s: Invalid region '%s'\n", __func__, *argv);
1114 debug("%s: Missing region parameter\n", __func__);
1120 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
1122 int flash_node, node;
1124 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
1126 debug("Failed to find chrome-ec node'\n");
1130 flash_node = fdt_subnode_offset(blob, node, "flash");
1131 if (flash_node < 0) {
1132 debug("Failed to find flash node\n");
1136 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1138 debug("Failed to decode flash node in chrome-ec'\n");
1142 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1144 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1145 node = fdt_next_subnode(blob, node)) {
1146 const char *name = fdt_get_name(blob, node, NULL);
1147 enum ec_flash_region region;
1149 if (0 == strcmp(name, "ro")) {
1150 region = EC_FLASH_REGION_RO;
1151 } else if (0 == strcmp(name, "rw")) {
1152 region = EC_FLASH_REGION_RW;
1153 } else if (0 == strcmp(name, "wp-ro")) {
1154 region = EC_FLASH_REGION_WP_RO;
1156 debug("Unknown EC flash region name '%s'\n", name);
1160 if (fdtdec_read_fmap_entry(blob, node, "reg",
1161 &config->region[region])) {
1162 debug("Failed to decode flash region in chrome-ec'\n");
1170 #ifdef CONFIG_CMD_CROS_EC
1173 * Perform a flash read or write command
1175 * @param dev CROS-EC device to read/write
1176 * @param is_write 1 do to a write, 0 to do a read
1177 * @param argc Number of arguments
1178 * @param argv Arguments (2 is region, 3 is address)
1179 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1180 * (negative EC_RES_...)
1182 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1183 char * const argv[])
1185 uint32_t offset, size = -1U, region_size;
1191 region = cros_ec_decode_region(argc - 2, argv + 2);
1196 addr = simple_strtoul(argv[3], &endp, 16);
1197 if (*argv[3] == 0 || *endp != 0)
1200 size = simple_strtoul(argv[4], &endp, 16);
1201 if (*argv[4] == 0 || *endp != 0)
1205 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
1207 debug("%s: Could not read region info\n", __func__);
1214 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1215 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1217 debug("%s: Could not %s region\n", __func__,
1218 is_write ? "write" : "read");
1225 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1227 struct cros_ec_dev *dev = last_dev;
1232 return CMD_RET_USAGE;
1235 if (0 == strcmp("init", cmd)) {
1236 ret = cros_ec_init(gd->fdt_blob, &dev);
1238 printf("Could not init cros_ec device (err %d)\n", ret);
1244 /* Just use the last allocated device; there should be only one */
1246 printf("No CROS-EC device available\n");
1249 if (0 == strcmp("id", cmd)) {
1252 if (cros_ec_read_id(dev, id, sizeof(id))) {
1253 debug("%s: Could not read KBC ID\n", __func__);
1257 } else if (0 == strcmp("info", cmd)) {
1258 struct ec_response_mkbp_info info;
1260 if (cros_ec_info(dev, &info)) {
1261 debug("%s: Could not read KBC info\n", __func__);
1264 printf("rows = %u\n", info.rows);
1265 printf("cols = %u\n", info.cols);
1266 printf("switches = %#x\n", info.switches);
1267 } else if (0 == strcmp("curimage", cmd)) {
1268 enum ec_current_image image;
1270 if (cros_ec_read_current_image(dev, &image)) {
1271 debug("%s: Could not read KBC image\n", __func__);
1274 printf("%d\n", image);
1275 } else if (0 == strcmp("hash", cmd)) {
1276 struct ec_response_vboot_hash hash;
1279 if (cros_ec_read_hash(dev, &hash)) {
1280 debug("%s: Could not read KBC hash\n", __func__);
1284 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1285 printf("type: SHA-256\n");
1287 printf("type: %d\n", hash.hash_type);
1289 printf("offset: 0x%08x\n", hash.offset);
1290 printf("size: 0x%08x\n", hash.size);
1293 for (i = 0; i < hash.digest_size; i++)
1294 printf("%02x", hash.hash_digest[i]);
1296 } else if (0 == strcmp("reboot", cmd)) {
1298 enum ec_reboot_cmd cmd;
1300 if (argc >= 3 && !strcmp(argv[2], "cold"))
1301 cmd = EC_REBOOT_COLD;
1303 region = cros_ec_decode_region(argc - 2, argv + 2);
1304 if (region == EC_FLASH_REGION_RO)
1305 cmd = EC_REBOOT_JUMP_RO;
1306 else if (region == EC_FLASH_REGION_RW)
1307 cmd = EC_REBOOT_JUMP_RW;
1309 return CMD_RET_USAGE;
1312 if (cros_ec_reboot(dev, cmd, 0)) {
1313 debug("%s: Could not reboot KBC\n", __func__);
1316 } else if (0 == strcmp("events", cmd)) {
1319 if (cros_ec_get_host_events(dev, &events)) {
1320 debug("%s: Could not read host events\n", __func__);
1323 printf("0x%08x\n", events);
1324 } else if (0 == strcmp("clrevents", cmd)) {
1325 uint32_t events = 0x7fffffff;
1328 events = simple_strtol(argv[2], NULL, 0);
1330 if (cros_ec_clear_host_events(dev, events)) {
1331 debug("%s: Could not clear host events\n", __func__);
1334 } else if (0 == strcmp("read", cmd)) {
1335 ret = do_read_write(dev, 0, argc, argv);
1337 return CMD_RET_USAGE;
1338 } else if (0 == strcmp("write", cmd)) {
1339 ret = do_read_write(dev, 1, argc, argv);
1341 return CMD_RET_USAGE;
1342 } else if (0 == strcmp("erase", cmd)) {
1343 int region = cros_ec_decode_region(argc - 2, argv + 2);
1344 uint32_t offset, size;
1347 return CMD_RET_USAGE;
1348 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1349 debug("%s: Could not read region info\n", __func__);
1352 ret = cros_ec_flash_erase(dev, offset, size);
1354 debug("%s: Could not erase region\n",
1358 } else if (0 == strcmp("regioninfo", cmd)) {
1359 int region = cros_ec_decode_region(argc - 2, argv + 2);
1360 uint32_t offset, size;
1363 return CMD_RET_USAGE;
1364 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1366 debug("%s: Could not read region info\n", __func__);
1368 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1370 printf("Offset: %x\n", offset);
1371 printf("Size: %x\n", size);
1373 } else if (0 == strcmp("vbnvcontext", cmd)) {
1374 uint8_t block[EC_VBNV_BLOCK_SIZE];
1377 unsigned long result;
1380 ret = cros_ec_read_vbnvcontext(dev, block);
1382 printf("vbnv_block: ");
1383 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1384 printf("%02x", block[i]);
1389 * TODO(clchiou): Move this to a utility function as
1390 * cmd_spi might want to call it.
1392 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1393 len = strlen(argv[2]);
1395 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1398 buf[0] = argv[2][i * 2];
1399 if (i * 2 + 1 >= len)
1402 buf[1] = argv[2][i * 2 + 1];
1403 strict_strtoul(buf, 16, &result);
1406 ret = cros_ec_write_vbnvcontext(dev, block);
1409 debug("%s: Could not %s VbNvContext\n", __func__,
1410 argc <= 2 ? "read" : "write");
1412 } else if (0 == strcmp("test", cmd)) {
1413 int result = cros_ec_test(dev);
1416 printf("Test failed with error %d\n", result);
1418 puts("Test passed\n");
1419 } else if (0 == strcmp("version", cmd)) {
1420 struct ec_response_get_version *p;
1423 ret = cros_ec_read_version(dev, &p);
1425 /* Print versions */
1426 printf("RO version: %1.*s\n",
1427 sizeof(p->version_string_ro),
1428 p->version_string_ro);
1429 printf("RW version: %1.*s\n",
1430 sizeof(p->version_string_rw),
1431 p->version_string_rw);
1432 printf("Firmware copy: %s\n",
1434 ARRAY_SIZE(ec_current_image_name) ?
1435 ec_current_image_name[p->current_image] :
1437 ret = cros_ec_read_build_info(dev, &build_string);
1439 printf("Build info: %s\n", build_string);
1441 } else if (0 == strcmp("ldo", cmd)) {
1442 uint8_t index, state;
1446 return CMD_RET_USAGE;
1447 index = simple_strtoul(argv[2], &endp, 10);
1448 if (*argv[2] == 0 || *endp != 0)
1449 return CMD_RET_USAGE;
1451 state = simple_strtoul(argv[3], &endp, 10);
1452 if (*argv[3] == 0 || *endp != 0)
1453 return CMD_RET_USAGE;
1454 ret = cros_ec_set_ldo(dev, index, state);
1456 ret = cros_ec_get_ldo(dev, index, &state);
1458 printf("LDO%d: %s\n", index,
1459 state == EC_LDO_STATE_ON ?
1465 debug("%s: Could not access LDO%d\n", __func__, index);
1469 return CMD_RET_USAGE;
1473 printf("Error: CROS-EC command failed (error %d)\n", ret);
1481 crosec, 5, 1, do_cros_ec,
1482 "CROS-EC utility command",
1483 "init Re-init CROS-EC (done on startup automatically)\n"
1484 "crosec id Read CROS-EC ID\n"
1485 "crosec info Read CROS-EC info\n"
1486 "crosec curimage Read CROS-EC current image\n"
1487 "crosec hash Read CROS-EC hash\n"
1488 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1489 "crosec events Read CROS-EC host events\n"
1490 "crosec clrevents [mask] Clear CROS-EC host events\n"
1491 "crosec regioninfo <ro|rw> Read image info\n"
1492 "crosec erase <ro|rw> Erase EC image\n"
1493 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1494 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1495 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1496 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1497 "crosec test run tests on cros_ec\n"
1498 "crosec version Read CROS-EC version"