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
24 #include <asm/errno.h>
26 #include <asm-generic/gpio.h>
29 #define debug_trace(fmt, b...) debug(fmt, #b)
31 #define debug_trace(fmt, b...)
35 /* Timeout waiting for a flash erase command to complete */
36 CROS_EC_CMD_TIMEOUT_MS = 5000,
37 /* Timeout waiting for a synchronous hash to be recomputed */
38 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
41 static struct cros_ec_dev static_dev, *last_dev;
43 DECLARE_GLOBAL_DATA_PTR;
45 /* Note: depends on enum ec_current_image */
46 static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
48 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
55 printf("cmd=%#x: ", cmd);
56 for (i = 0; i < len; i++)
57 printf("%02x ", data[i]);
63 * Calculate a simple 8-bit checksum of a data block
65 * @param data Data block to checksum
66 * @param size Size of data block in bytes
67 * @return checksum value (0 to 255)
69 int cros_ec_calc_checksum(const uint8_t *data, int size)
73 for (i = csum = 0; i < size; i++)
79 * Create a request packet for protocol version 3.
81 * The packet is stored in the device's internal output buffer.
83 * @param dev CROS-EC device
84 * @param cmd Command to send (EC_CMD_...)
85 * @param cmd_version Version of command to send (EC_VER_...)
86 * @param dout Output data (may be NULL If dout_len=0)
87 * @param dout_len Size of output data in bytes
88 * @return packet size in bytes, or <0 if error.
90 static int create_proto3_request(struct cros_ec_dev *dev,
91 int cmd, int cmd_version,
92 const void *dout, int dout_len)
94 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
95 int out_bytes = dout_len + sizeof(*rq);
97 /* Fail if output size is too big */
98 if (out_bytes > (int)sizeof(dev->dout)) {
99 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
100 return -EC_RES_REQUEST_TRUNCATED;
103 /* Fill in request packet */
104 rq->struct_version = EC_HOST_REQUEST_VERSION;
107 rq->command_version = cmd_version;
109 rq->data_len = dout_len;
111 /* Copy data after header */
112 memcpy(rq + 1, dout, dout_len);
114 /* Write checksum field so the entire packet sums to 0 */
115 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
117 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
119 /* Return size of request packet */
124 * Prepare the device to receive a protocol version 3 response.
126 * @param dev CROS-EC device
127 * @param din_len Maximum size of response in bytes
128 * @return maximum expected number of bytes in response, or <0 if error.
130 static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
132 int in_bytes = din_len + sizeof(struct ec_host_response);
134 /* Fail if input size is too big */
135 if (in_bytes > (int)sizeof(dev->din)) {
136 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
137 return -EC_RES_RESPONSE_TOO_BIG;
140 /* Return expected size of response packet */
145 * Handle a protocol version 3 response packet.
147 * The packet must already be stored in the device's internal input buffer.
149 * @param dev CROS-EC device
150 * @param dinp Returns pointer to response data
151 * @param din_len Maximum size of response in bytes
152 * @return number of bytes of response data, or <0 if error
154 static int handle_proto3_response(struct cros_ec_dev *dev,
155 uint8_t **dinp, int din_len)
157 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
161 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
163 /* Check input data */
164 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
165 debug("%s: EC response version mismatch\n", __func__);
166 return -EC_RES_INVALID_RESPONSE;
170 debug("%s: EC response reserved != 0\n", __func__);
171 return -EC_RES_INVALID_RESPONSE;
174 if (rs->data_len > din_len) {
175 debug("%s: EC returned too much data\n", __func__);
176 return -EC_RES_RESPONSE_TOO_BIG;
179 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
181 /* Update in_bytes to actual data size */
182 in_bytes = sizeof(*rs) + rs->data_len;
184 /* Verify checksum */
185 csum = cros_ec_calc_checksum(dev->din, in_bytes);
187 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
189 return -EC_RES_INVALID_CHECKSUM;
192 /* Return error result, if any */
194 return -(int)rs->result;
196 /* If we're still here, set response data pointer and return length */
197 *dinp = (uint8_t *)(rs + 1);
202 static int send_command_proto3(struct cros_ec_dev *dev,
203 int cmd, int cmd_version,
204 const void *dout, int dout_len,
205 uint8_t **dinp, int din_len)
207 int out_bytes, in_bytes;
210 /* Create request packet */
211 out_bytes = create_proto3_request(dev, cmd, cmd_version,
216 /* Prepare response buffer */
217 in_bytes = prepare_proto3_response_buffer(dev, din_len);
221 switch (dev->interface) {
222 #ifdef CONFIG_CROS_EC_SPI
224 rv = cros_ec_spi_packet(dev, out_bytes, in_bytes);
227 case CROS_EC_IF_NONE:
228 /* TODO: support protocol 3 for LPC, I2C; for now fall through */
230 debug("%s: Unsupported interface\n", __func__);
236 /* Process the response */
237 return handle_proto3_response(dev, dinp, din_len);
240 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
241 const void *dout, int dout_len,
242 uint8_t **dinp, int din_len)
246 /* Handle protocol version 3 support */
247 if (dev->protocol_version == 3) {
248 return send_command_proto3(dev, cmd, cmd_version,
249 dout, dout_len, dinp, din_len);
252 switch (dev->interface) {
253 #ifdef CONFIG_CROS_EC_SPI
255 ret = cros_ec_spi_command(dev, cmd, cmd_version,
256 (const uint8_t *)dout, dout_len,
260 #ifdef CONFIG_CROS_EC_I2C
262 ret = cros_ec_i2c_command(dev, cmd, cmd_version,
263 (const uint8_t *)dout, dout_len,
267 #ifdef CONFIG_CROS_EC_LPC
269 ret = cros_ec_lpc_command(dev, cmd, cmd_version,
270 (const uint8_t *)dout, dout_len,
274 case CROS_EC_IF_NONE:
283 * Send a command to the CROS-EC device and return the reply.
285 * The device's internal input/output buffers are used.
287 * @param dev CROS-EC device
288 * @param cmd Command to send (EC_CMD_...)
289 * @param cmd_version Version of command to send (EC_VER_...)
290 * @param dout Output data (may be NULL If dout_len=0)
291 * @param dout_len Size of output data in bytes
292 * @param dinp Response data (may be NULL If din_len=0).
293 * If not NULL, it will be updated to point to the data
294 * and will always be double word aligned (64-bits)
295 * @param din_len Maximum size of response in bytes
296 * @return number of bytes in response, or -1 on error
298 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
299 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
305 len = send_command(dev, cmd, cmd_version, dout, dout_len,
308 /* If the command doesn't complete, wait a while */
309 if (len == -EC_RES_IN_PROGRESS) {
310 struct ec_response_get_comms_status *resp = NULL;
313 /* Wait for command to complete */
314 start = get_timer(0);
318 mdelay(50); /* Insert some reasonable delay */
319 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
321 (uint8_t **)&resp, sizeof(*resp));
325 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
326 debug("%s: Command %#02x timeout\n",
328 return -EC_RES_TIMEOUT;
330 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
332 /* OK it completed, so read the status response */
333 /* not sure why it was 0 for the last argument */
334 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
335 NULL, 0, &din, din_len);
338 debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
339 dinp ? *dinp : NULL);
341 /* If we have any data to return, it must be 64bit-aligned */
342 assert(len <= 0 || !((uintptr_t)din & 7));
350 * Send a command to the CROS-EC device and return the reply.
352 * The device's internal input/output buffers are used.
354 * @param dev CROS-EC device
355 * @param cmd Command to send (EC_CMD_...)
356 * @param cmd_version Version of command to send (EC_VER_...)
357 * @param dout Output data (may be NULL If dout_len=0)
358 * @param dout_len Size of output data in bytes
359 * @param din Response data (may be NULL If din_len=0).
360 * It not NULL, it is a place for ec_command() to copy the
362 * @param din_len Maximum size of response in bytes
363 * @return number of bytes in response, or -1 on error
365 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
366 const void *dout, int dout_len,
367 void *din, int din_len)
372 assert((din_len == 0) || din);
373 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
374 &in_buffer, din_len);
377 * If we were asked to put it somewhere, do so, otherwise just
378 * disregard the result.
380 if (din && in_buffer) {
381 assert(len <= din_len);
382 memmove(din, in_buffer, len);
388 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
390 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
391 sizeof(scan->data)) != sizeof(scan->data))
397 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
399 struct ec_response_get_version *r;
401 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
402 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
405 if (maxlen > (int)sizeof(r->version_string_ro))
406 maxlen = sizeof(r->version_string_ro);
408 switch (r->current_image) {
410 memcpy(id, r->version_string_ro, maxlen);
413 memcpy(id, r->version_string_rw, maxlen);
419 id[maxlen - 1] = '\0';
423 int cros_ec_read_version(struct cros_ec_dev *dev,
424 struct ec_response_get_version **versionp)
426 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
427 (uint8_t **)versionp, sizeof(**versionp))
428 != sizeof(**versionp))
434 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
436 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
437 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
443 int cros_ec_read_current_image(struct cros_ec_dev *dev,
444 enum ec_current_image *image)
446 struct ec_response_get_version *r;
448 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
449 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
452 *image = r->current_image;
456 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
457 struct ec_response_vboot_hash *hash)
459 struct ec_params_vboot_hash p;
462 start = get_timer(0);
463 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
464 mdelay(50); /* Insert some reasonable delay */
466 p.cmd = EC_VBOOT_HASH_GET;
467 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
468 hash, sizeof(*hash)) < 0)
471 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
472 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
473 return -EC_RES_TIMEOUT;
480 int cros_ec_read_hash(struct cros_ec_dev *dev,
481 struct ec_response_vboot_hash *hash)
483 struct ec_params_vboot_hash p;
486 p.cmd = EC_VBOOT_HASH_GET;
487 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
488 hash, sizeof(*hash)) < 0)
491 /* If the EC is busy calculating the hash, fidget until it's done. */
492 rv = cros_ec_wait_on_hash_done(dev, hash);
496 /* If the hash is valid, we're done. Otherwise, we have to kick it off
497 * again and wait for it to complete. Note that we explicitly assume
498 * that hashing zero bytes is always wrong, even though that would
499 * produce a valid hash value. */
500 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
503 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
504 __func__, hash->status, hash->size);
506 p.cmd = EC_VBOOT_HASH_START;
507 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
509 p.offset = EC_VBOOT_HASH_OFFSET_RW;
511 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
512 hash, sizeof(*hash)) < 0)
515 rv = cros_ec_wait_on_hash_done(dev, hash);
519 debug("%s: hash done\n", __func__);
524 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
526 struct ec_params_vboot_hash p;
527 struct ec_response_vboot_hash *hash;
529 /* We don't have an explict command for the EC to discard its current
530 * hash value, so we'll just tell it to calculate one that we know is
531 * wrong (we claim that hashing zero bytes is always invalid).
533 p.cmd = EC_VBOOT_HASH_RECALC;
534 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
539 debug("%s:\n", __func__);
541 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
542 (uint8_t **)&hash, sizeof(*hash)) < 0)
545 /* No need to wait for it to finish */
549 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
552 struct ec_params_reboot_ec p;
557 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
561 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
563 * EC reboot will take place immediately so delay to allow it
564 * to complete. Note that some reboot types (EC_REBOOT_COLD)
565 * will reboot the AP as well, in which case we won't actually
570 * better way to determine when the reboot is complete. Could
571 * we poll a memory-mapped LPC value?
579 int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
581 /* no interrupt support : always poll */
582 if (!fdt_gpio_isvalid(&dev->ec_int))
585 return !gpio_get_value(dev->ec_int.gpio);
588 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
590 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
591 sizeof(*info)) != sizeof(*info))
597 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
599 struct ec_response_host_event_mask *resp;
602 * Use the B copy of the event flags, because the main copy is already
605 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
606 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
609 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
612 *events_ptr = resp->mask;
616 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
618 struct ec_params_host_event_mask params;
620 params.mask = events;
623 * Use the B copy of the event flags, so it affects the data returned
624 * by cros_ec_get_host_events().
626 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
627 ¶ms, sizeof(params), NULL, 0) < 0)
633 int cros_ec_flash_protect(struct cros_ec_dev *dev,
634 uint32_t set_mask, uint32_t set_flags,
635 struct ec_response_flash_protect *resp)
637 struct ec_params_flash_protect params;
639 params.mask = set_mask;
640 params.flags = set_flags;
642 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
643 ¶ms, sizeof(params),
644 resp, sizeof(*resp)) != sizeof(*resp))
650 static int cros_ec_check_version(struct cros_ec_dev *dev)
652 struct ec_params_hello req;
653 struct ec_response_hello *resp;
655 #ifdef CONFIG_CROS_EC_LPC
656 /* LPC has its own way of doing this */
657 if (dev->interface == CROS_EC_IF_LPC)
658 return cros_ec_lpc_check_version(dev);
663 * There is a strange oddity here with the EC. We could just ignore
664 * the response, i.e. pass the last two parameters as NULL and 0.
665 * In this case we won't read back very many bytes from the EC.
666 * On the I2C bus the EC gets upset about this and will try to send
667 * the bytes anyway. This means that we will have to wait for that
668 * to complete before continuing with a new EC command.
670 * This problem is probably unique to the I2C bus.
672 * So for now, just read all the data anyway.
675 /* Try sending a version 3 packet */
676 dev->protocol_version = 3;
677 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
678 (uint8_t **)&resp, sizeof(*resp)) > 0) {
682 /* Try sending a version 2 packet */
683 dev->protocol_version = 2;
684 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
685 (uint8_t **)&resp, sizeof(*resp)) > 0) {
690 * Fail if we're still here, since the EC doesn't understand any
691 * protcol version we speak. Version 1 interface without command
692 * version is no longer supported, and we don't know about any new
695 dev->protocol_version = 0;
696 printf("%s: ERROR: old EC interface not supported\n", __func__);
700 int cros_ec_test(struct cros_ec_dev *dev)
702 struct ec_params_hello req;
703 struct ec_response_hello *resp;
705 req.in_data = 0x12345678;
706 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
707 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
708 printf("ec_command_inptr() returned error\n");
711 if (resp->out_data != req.in_data + 0x01020304) {
712 printf("Received invalid handshake %x\n", resp->out_data);
719 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
720 uint32_t *offset, uint32_t *size)
722 struct ec_params_flash_region_info p;
723 struct ec_response_flash_region_info *r;
727 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
728 EC_VER_FLASH_REGION_INFO,
729 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
730 if (ret != sizeof(*r))
741 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
743 struct ec_params_flash_erase p;
747 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
752 * Write a single block to the flash
754 * Write a block of data to the EC flash. The size must not exceed the flash
755 * write block size which you can obtain from cros_ec_flash_write_burst_size().
757 * The offset starts at 0. You can obtain the region information from
758 * cros_ec_flash_offset() to find out where to write for a particular region.
760 * Attempting to write to the region where the EC is currently running from
761 * will result in an error.
763 * @param dev CROS-EC device
764 * @param data Pointer to data buffer to write
765 * @param offset Offset within flash to write to.
766 * @param size Number of bytes to write
767 * @return 0 if ok, -1 on error
769 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
770 const uint8_t *data, uint32_t offset, uint32_t size)
772 struct ec_params_flash_write p;
776 assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
777 memcpy(&p + 1, data, p.size);
779 return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
780 &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
784 * Return optimal flash write burst size
786 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
788 return EC_FLASH_WRITE_VER0_SIZE;
792 * Check if a block of data is erased (all 0xff)
794 * This function is useful when dealing with flash, for checking whether a
795 * data block is erased and thus does not need to be programmed.
797 * @param data Pointer to data to check (must be word-aligned)
798 * @param size Number of bytes to check (must be word-aligned)
799 * @return 0 if erased, non-zero if any word is not erased
801 static int cros_ec_data_is_erased(const uint32_t *data, int size)
804 size /= sizeof(uint32_t);
805 for (; size > 0; size -= 4, data++)
812 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
813 uint32_t offset, uint32_t size)
815 uint32_t burst = cros_ec_flash_write_burst_size(dev);
820 * TODO: round up to the nearest multiple of write size. Can get away
821 * without that on link right now because its write size is 4 bytes.
824 for (off = offset; off < end; off += burst, data += burst) {
827 /* If the data is empty, there is no point in programming it */
828 todo = min(end - off, burst);
829 if (dev->optimise_flash_write &&
830 cros_ec_data_is_erased((uint32_t *)data, todo))
833 ret = cros_ec_flash_write_block(dev, data, off, todo);
842 * Read a single block from the flash
844 * Read a block of data from the EC flash. The size must not exceed the flash
845 * write block size which you can obtain from cros_ec_flash_write_burst_size().
847 * The offset starts at 0. You can obtain the region information from
848 * cros_ec_flash_offset() to find out where to read for a particular region.
850 * @param dev CROS-EC device
851 * @param data Pointer to data buffer to read into
852 * @param offset Offset within flash to read from
853 * @param size Number of bytes to read
854 * @return 0 if ok, -1 on error
856 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
857 uint32_t offset, uint32_t size)
859 struct ec_params_flash_read p;
864 return ec_command(dev, EC_CMD_FLASH_READ, 0,
865 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
868 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
871 uint32_t burst = cros_ec_flash_write_burst_size(dev);
876 for (off = offset; off < end; off += burst, data += burst) {
877 ret = cros_ec_flash_read_block(dev, data, off,
878 min(end - off, burst));
886 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
887 const uint8_t *image, int image_size)
889 uint32_t rw_offset, rw_size;
892 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
894 if (image_size > (int)rw_size)
897 /* Invalidate the existing hash, just in case the AP reboots
898 * unexpectedly during the update. If that happened, the EC RW firmware
899 * would be invalid, but the EC would still have the original hash.
901 ret = cros_ec_invalidate_hash(dev);
906 * Erase the entire RW section, so that the EC doesn't see any garbage
907 * past the new image if it's smaller than the current image.
909 * TODO: could optimize this to erase just the current image, since
910 * presumably everything past that is 0xff's. But would still need to
911 * round up to the nearest multiple of erase size.
913 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
917 /* Write the image */
918 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
925 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
927 struct ec_params_vbnvcontext p;
930 p.op = EC_VBNV_CONTEXT_OP_READ;
932 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
933 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
934 if (len < EC_VBNV_BLOCK_SIZE)
940 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
942 struct ec_params_vbnvcontext p;
945 p.op = EC_VBNV_CONTEXT_OP_WRITE;
946 memcpy(p.block, block, sizeof(p.block));
948 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
949 &p, sizeof(p), NULL, 0);
956 int cros_ec_set_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t state)
958 struct ec_params_ldo_set params;
960 params.index = index;
961 params.state = state;
963 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0,
964 ¶ms, sizeof(params),
971 int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
973 struct ec_params_ldo_get params;
974 struct ec_response_ldo_get *resp;
976 params.index = index;
978 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0,
979 ¶ms, sizeof(params),
980 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
983 *state = resp->state;
989 * Decode EC interface details from the device tree and allocate a suitable
992 * @param blob Device tree blob
993 * @param node Node to decode from
994 * @param devp Returns a pointer to the new allocated device
995 * @return 0 if ok, -1 on error
997 static int cros_ec_decode_fdt(const void *blob, int node,
998 struct cros_ec_dev **devp)
1000 enum fdt_compat_id compat;
1001 struct cros_ec_dev *dev;
1004 /* See what type of parent we are inside (this is expensive) */
1005 parent = fdt_parent_offset(blob, node);
1007 debug("%s: Cannot find node parent\n", __func__);
1013 dev->parent_node = parent;
1015 compat = fdtdec_lookup(blob, parent);
1017 #ifdef CONFIG_CROS_EC_SPI
1018 case COMPAT_SAMSUNG_EXYNOS_SPI:
1019 dev->interface = CROS_EC_IF_SPI;
1020 if (cros_ec_spi_decode_fdt(dev, blob))
1024 #ifdef CONFIG_CROS_EC_I2C
1025 case COMPAT_SAMSUNG_S3C2440_I2C:
1026 dev->interface = CROS_EC_IF_I2C;
1027 if (cros_ec_i2c_decode_fdt(dev, blob))
1031 #ifdef CONFIG_CROS_EC_LPC
1032 case COMPAT_INTEL_LPC:
1033 dev->interface = CROS_EC_IF_LPC;
1037 debug("%s: Unknown compat id %d\n", __func__, compat);
1041 fdtdec_decode_gpio(blob, node, "ec-interrupt", &dev->ec_int);
1042 dev->optimise_flash_write = fdtdec_get_bool(blob, node,
1043 "optimise-flash-write");
1049 int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
1052 struct cros_ec_dev *dev;
1057 node = fdtdec_next_compatible(blob, node,
1058 COMPAT_GOOGLE_CROS_EC);
1060 debug("%s: Node not found\n", __func__);
1063 } while (!fdtdec_get_is_enabled(blob, node));
1065 if (cros_ec_decode_fdt(blob, node, &dev)) {
1066 debug("%s: Failed to decode device.\n", __func__);
1067 return -CROS_EC_ERR_FDT_DECODE;
1070 switch (dev->interface) {
1071 #ifdef CONFIG_CROS_EC_SPI
1072 case CROS_EC_IF_SPI:
1073 if (cros_ec_spi_init(dev, blob)) {
1074 debug("%s: Could not setup SPI interface\n", __func__);
1075 return -CROS_EC_ERR_DEV_INIT;
1079 #ifdef CONFIG_CROS_EC_I2C
1080 case CROS_EC_IF_I2C:
1081 if (cros_ec_i2c_init(dev, blob))
1082 return -CROS_EC_ERR_DEV_INIT;
1085 #ifdef CONFIG_CROS_EC_LPC
1086 case CROS_EC_IF_LPC:
1087 if (cros_ec_lpc_init(dev, blob))
1088 return -CROS_EC_ERR_DEV_INIT;
1091 case CROS_EC_IF_NONE:
1096 /* we will poll the EC interrupt line */
1097 fdtdec_setup_gpio(&dev->ec_int);
1098 if (fdt_gpio_isvalid(&dev->ec_int))
1099 gpio_direction_input(dev->ec_int.gpio);
1101 if (cros_ec_check_version(dev)) {
1102 debug("%s: Could not detect CROS-EC version\n", __func__);
1103 return -CROS_EC_ERR_CHECK_VERSION;
1106 if (cros_ec_read_id(dev, id, sizeof(id))) {
1107 debug("%s: Could not read KBC ID\n", __func__);
1108 return -CROS_EC_ERR_READ_ID;
1111 /* Remember this device for use by the cros_ec command */
1112 last_dev = *cros_ecp = dev;
1113 debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
1118 int cros_ec_decode_region(int argc, char * const argv[])
1121 if (0 == strcmp(*argv, "rw"))
1122 return EC_FLASH_REGION_RW;
1123 else if (0 == strcmp(*argv, "ro"))
1124 return EC_FLASH_REGION_RO;
1126 debug("%s: Invalid region '%s'\n", __func__, *argv);
1128 debug("%s: Missing region parameter\n", __func__);
1134 int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
1136 int flash_node, node;
1138 node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
1140 debug("Failed to find chrome-ec node'\n");
1144 flash_node = fdt_subnode_offset(blob, node, "flash");
1145 if (flash_node < 0) {
1146 debug("Failed to find flash node\n");
1150 if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
1152 debug("Failed to decode flash node in chrome-ec'\n");
1156 config->flash_erase_value = fdtdec_get_int(blob, flash_node,
1158 for (node = fdt_first_subnode(blob, flash_node); node >= 0;
1159 node = fdt_next_subnode(blob, node)) {
1160 const char *name = fdt_get_name(blob, node, NULL);
1161 enum ec_flash_region region;
1163 if (0 == strcmp(name, "ro")) {
1164 region = EC_FLASH_REGION_RO;
1165 } else if (0 == strcmp(name, "rw")) {
1166 region = EC_FLASH_REGION_RW;
1167 } else if (0 == strcmp(name, "wp-ro")) {
1168 region = EC_FLASH_REGION_WP_RO;
1170 debug("Unknown EC flash region name '%s'\n", name);
1174 if (fdtdec_read_fmap_entry(blob, node, "reg",
1175 &config->region[region])) {
1176 debug("Failed to decode flash region in chrome-ec'\n");
1184 #ifdef CONFIG_CMD_CROS_EC
1187 * Perform a flash read or write command
1189 * @param dev CROS-EC device to read/write
1190 * @param is_write 1 do to a write, 0 to do a read
1191 * @param argc Number of arguments
1192 * @param argv Arguments (2 is region, 3 is address)
1193 * @return 0 for ok, 1 for a usage error or -ve for ec command error
1194 * (negative EC_RES_...)
1196 static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
1197 char * const argv[])
1199 uint32_t offset, size = -1U, region_size;
1205 region = cros_ec_decode_region(argc - 2, argv + 2);
1210 addr = simple_strtoul(argv[3], &endp, 16);
1211 if (*argv[3] == 0 || *endp != 0)
1214 size = simple_strtoul(argv[4], &endp, 16);
1215 if (*argv[4] == 0 || *endp != 0)
1219 ret = cros_ec_flash_offset(dev, region, &offset, ®ion_size);
1221 debug("%s: Could not read region info\n", __func__);
1228 cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
1229 cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
1231 debug("%s: Could not %s region\n", __func__,
1232 is_write ? "write" : "read");
1239 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1241 struct cros_ec_dev *dev = last_dev;
1246 return CMD_RET_USAGE;
1249 if (0 == strcmp("init", cmd)) {
1250 ret = cros_ec_init(gd->fdt_blob, &dev);
1252 printf("Could not init cros_ec device (err %d)\n", ret);
1258 /* Just use the last allocated device; there should be only one */
1260 printf("No CROS-EC device available\n");
1263 if (0 == strcmp("id", cmd)) {
1266 if (cros_ec_read_id(dev, id, sizeof(id))) {
1267 debug("%s: Could not read KBC ID\n", __func__);
1271 } else if (0 == strcmp("info", cmd)) {
1272 struct ec_response_mkbp_info info;
1274 if (cros_ec_info(dev, &info)) {
1275 debug("%s: Could not read KBC info\n", __func__);
1278 printf("rows = %u\n", info.rows);
1279 printf("cols = %u\n", info.cols);
1280 printf("switches = %#x\n", info.switches);
1281 } else if (0 == strcmp("curimage", cmd)) {
1282 enum ec_current_image image;
1284 if (cros_ec_read_current_image(dev, &image)) {
1285 debug("%s: Could not read KBC image\n", __func__);
1288 printf("%d\n", image);
1289 } else if (0 == strcmp("hash", cmd)) {
1290 struct ec_response_vboot_hash hash;
1293 if (cros_ec_read_hash(dev, &hash)) {
1294 debug("%s: Could not read KBC hash\n", __func__);
1298 if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
1299 printf("type: SHA-256\n");
1301 printf("type: %d\n", hash.hash_type);
1303 printf("offset: 0x%08x\n", hash.offset);
1304 printf("size: 0x%08x\n", hash.size);
1307 for (i = 0; i < hash.digest_size; i++)
1308 printf("%02x", hash.hash_digest[i]);
1310 } else if (0 == strcmp("reboot", cmd)) {
1312 enum ec_reboot_cmd cmd;
1314 if (argc >= 3 && !strcmp(argv[2], "cold"))
1315 cmd = EC_REBOOT_COLD;
1317 region = cros_ec_decode_region(argc - 2, argv + 2);
1318 if (region == EC_FLASH_REGION_RO)
1319 cmd = EC_REBOOT_JUMP_RO;
1320 else if (region == EC_FLASH_REGION_RW)
1321 cmd = EC_REBOOT_JUMP_RW;
1323 return CMD_RET_USAGE;
1326 if (cros_ec_reboot(dev, cmd, 0)) {
1327 debug("%s: Could not reboot KBC\n", __func__);
1330 } else if (0 == strcmp("events", cmd)) {
1333 if (cros_ec_get_host_events(dev, &events)) {
1334 debug("%s: Could not read host events\n", __func__);
1337 printf("0x%08x\n", events);
1338 } else if (0 == strcmp("clrevents", cmd)) {
1339 uint32_t events = 0x7fffffff;
1342 events = simple_strtol(argv[2], NULL, 0);
1344 if (cros_ec_clear_host_events(dev, events)) {
1345 debug("%s: Could not clear host events\n", __func__);
1348 } else if (0 == strcmp("read", cmd)) {
1349 ret = do_read_write(dev, 0, argc, argv);
1351 return CMD_RET_USAGE;
1352 } else if (0 == strcmp("write", cmd)) {
1353 ret = do_read_write(dev, 1, argc, argv);
1355 return CMD_RET_USAGE;
1356 } else if (0 == strcmp("erase", cmd)) {
1357 int region = cros_ec_decode_region(argc - 2, argv + 2);
1358 uint32_t offset, size;
1361 return CMD_RET_USAGE;
1362 if (cros_ec_flash_offset(dev, region, &offset, &size)) {
1363 debug("%s: Could not read region info\n", __func__);
1366 ret = cros_ec_flash_erase(dev, offset, size);
1368 debug("%s: Could not erase region\n",
1372 } else if (0 == strcmp("regioninfo", cmd)) {
1373 int region = cros_ec_decode_region(argc - 2, argv + 2);
1374 uint32_t offset, size;
1377 return CMD_RET_USAGE;
1378 ret = cros_ec_flash_offset(dev, region, &offset, &size);
1380 debug("%s: Could not read region info\n", __func__);
1382 printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
1384 printf("Offset: %x\n", offset);
1385 printf("Size: %x\n", size);
1387 } else if (0 == strcmp("vbnvcontext", cmd)) {
1388 uint8_t block[EC_VBNV_BLOCK_SIZE];
1391 unsigned long result;
1394 ret = cros_ec_read_vbnvcontext(dev, block);
1396 printf("vbnv_block: ");
1397 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
1398 printf("%02x", block[i]);
1403 * TODO(clchiou): Move this to a utility function as
1404 * cmd_spi might want to call it.
1406 memset(block, 0, EC_VBNV_BLOCK_SIZE);
1407 len = strlen(argv[2]);
1409 for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
1412 buf[0] = argv[2][i * 2];
1413 if (i * 2 + 1 >= len)
1416 buf[1] = argv[2][i * 2 + 1];
1417 strict_strtoul(buf, 16, &result);
1420 ret = cros_ec_write_vbnvcontext(dev, block);
1423 debug("%s: Could not %s VbNvContext\n", __func__,
1424 argc <= 2 ? "read" : "write");
1426 } else if (0 == strcmp("test", cmd)) {
1427 int result = cros_ec_test(dev);
1430 printf("Test failed with error %d\n", result);
1432 puts("Test passed\n");
1433 } else if (0 == strcmp("version", cmd)) {
1434 struct ec_response_get_version *p;
1437 ret = cros_ec_read_version(dev, &p);
1439 /* Print versions */
1440 printf("RO version: %1.*s\n",
1441 (int)sizeof(p->version_string_ro),
1442 p->version_string_ro);
1443 printf("RW version: %1.*s\n",
1444 (int)sizeof(p->version_string_rw),
1445 p->version_string_rw);
1446 printf("Firmware copy: %s\n",
1448 ARRAY_SIZE(ec_current_image_name) ?
1449 ec_current_image_name[p->current_image] :
1451 ret = cros_ec_read_build_info(dev, &build_string);
1453 printf("Build info: %s\n", build_string);
1455 } else if (0 == strcmp("ldo", cmd)) {
1456 uint8_t index, state;
1460 return CMD_RET_USAGE;
1461 index = simple_strtoul(argv[2], &endp, 10);
1462 if (*argv[2] == 0 || *endp != 0)
1463 return CMD_RET_USAGE;
1465 state = simple_strtoul(argv[3], &endp, 10);
1466 if (*argv[3] == 0 || *endp != 0)
1467 return CMD_RET_USAGE;
1468 ret = cros_ec_set_ldo(dev, index, state);
1470 ret = cros_ec_get_ldo(dev, index, &state);
1472 printf("LDO%d: %s\n", index,
1473 state == EC_LDO_STATE_ON ?
1479 debug("%s: Could not access LDO%d\n", __func__, index);
1483 return CMD_RET_USAGE;
1487 printf("Error: CROS-EC command failed (error %d)\n", ret);
1495 crosec, 5, 1, do_cros_ec,
1496 "CROS-EC utility command",
1497 "init Re-init CROS-EC (done on startup automatically)\n"
1498 "crosec id Read CROS-EC ID\n"
1499 "crosec info Read CROS-EC info\n"
1500 "crosec curimage Read CROS-EC current image\n"
1501 "crosec hash Read CROS-EC hash\n"
1502 "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
1503 "crosec events Read CROS-EC host events\n"
1504 "crosec clrevents [mask] Clear CROS-EC host events\n"
1505 "crosec regioninfo <ro|rw> Read image info\n"
1506 "crosec erase <ro|rw> Erase EC image\n"
1507 "crosec read <ro|rw> <addr> [<size>] Read EC image\n"
1508 "crosec write <ro|rw> <addr> [<size>] Write EC image\n"
1509 "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n"
1510 "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
1511 "crosec test run tests on cros_ec\n"
1512 "crosec version Read CROS-EC version"