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 <linux/errno.h>
27 #include <asm-generic/gpio.h>
28 #include <dm/device-internal.h>
29 #include <dm/of_extra.h>
30 #include <dm/uclass-internal.h>
33 #define debug_trace(fmt, b...) debug(fmt, #b)
35 #define debug_trace(fmt, b...)
39 /* Timeout waiting for a flash erase command to complete */
40 CROS_EC_CMD_TIMEOUT_MS = 5000,
41 /* Timeout waiting for a synchronous hash to be recomputed */
42 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
45 DECLARE_GLOBAL_DATA_PTR;
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. Note that error
152 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
155 static int handle_proto3_response(struct cros_ec_dev *dev,
156 uint8_t **dinp, int din_len)
158 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
162 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
164 /* Check input data */
165 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
166 debug("%s: EC response version mismatch\n", __func__);
167 return -EC_RES_INVALID_RESPONSE;
171 debug("%s: EC response reserved != 0\n", __func__);
172 return -EC_RES_INVALID_RESPONSE;
175 if (rs->data_len > din_len) {
176 debug("%s: EC returned too much data\n", __func__);
177 return -EC_RES_RESPONSE_TOO_BIG;
180 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
182 /* Update in_bytes to actual data size */
183 in_bytes = sizeof(*rs) + rs->data_len;
185 /* Verify checksum */
186 csum = cros_ec_calc_checksum(dev->din, in_bytes);
188 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
190 return -EC_RES_INVALID_CHECKSUM;
193 /* Return error result, if any */
195 return -(int)rs->result;
197 /* If we're still here, set response data pointer and return length */
198 *dinp = (uint8_t *)(rs + 1);
203 static int send_command_proto3(struct cros_ec_dev *dev,
204 int cmd, int cmd_version,
205 const void *dout, int dout_len,
206 uint8_t **dinp, int din_len)
208 struct dm_cros_ec_ops *ops;
209 int out_bytes, in_bytes;
212 /* Create request packet */
213 out_bytes = create_proto3_request(dev, cmd, cmd_version,
218 /* Prepare response buffer */
219 in_bytes = prepare_proto3_response_buffer(dev, din_len);
223 ops = dm_cros_ec_get_ops(dev->dev);
224 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
228 /* Process the response */
229 return handle_proto3_response(dev, dinp, din_len);
232 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
233 const void *dout, int dout_len,
234 uint8_t **dinp, int din_len)
236 struct dm_cros_ec_ops *ops;
239 /* Handle protocol version 3 support */
240 if (dev->protocol_version == 3) {
241 return send_command_proto3(dev, cmd, cmd_version,
242 dout, dout_len, dinp, din_len);
245 ops = dm_cros_ec_get_ops(dev->dev);
246 ret = ops->command(dev->dev, cmd, cmd_version,
247 (const uint8_t *)dout, dout_len, dinp, din_len);
253 * Send a command to the CROS-EC device and return the reply.
255 * The device's internal input/output buffers are used.
257 * @param dev CROS-EC device
258 * @param cmd Command to send (EC_CMD_...)
259 * @param cmd_version Version of command to send (EC_VER_...)
260 * @param dout Output data (may be NULL If dout_len=0)
261 * @param dout_len Size of output data in bytes
262 * @param dinp Response data (may be NULL If din_len=0).
263 * If not NULL, it will be updated to point to the data
264 * and will always be double word aligned (64-bits)
265 * @param din_len Maximum size of response in bytes
266 * @return number of bytes in response, or -ve on error
268 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
269 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
275 len = send_command(dev, cmd, cmd_version, dout, dout_len,
278 /* If the command doesn't complete, wait a while */
279 if (len == -EC_RES_IN_PROGRESS) {
280 struct ec_response_get_comms_status *resp = NULL;
283 /* Wait for command to complete */
284 start = get_timer(0);
288 mdelay(50); /* Insert some reasonable delay */
289 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
291 (uint8_t **)&resp, sizeof(*resp));
295 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
296 debug("%s: Command %#02x timeout\n",
298 return -EC_RES_TIMEOUT;
300 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
302 /* OK it completed, so read the status response */
303 /* not sure why it was 0 for the last argument */
304 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
305 NULL, 0, &din, din_len);
308 debug("%s: len=%d, din=%p\n", __func__, len, din);
310 /* If we have any data to return, it must be 64bit-aligned */
311 assert(len <= 0 || !((uintptr_t)din & 7));
319 * Send a command to the CROS-EC device and return the reply.
321 * The device's internal input/output buffers are used.
323 * @param dev CROS-EC device
324 * @param cmd Command to send (EC_CMD_...)
325 * @param cmd_version Version of command to send (EC_VER_...)
326 * @param dout Output data (may be NULL If dout_len=0)
327 * @param dout_len Size of output data in bytes
328 * @param din Response data (may be NULL If din_len=0).
329 * It not NULL, it is a place for ec_command() to copy the
331 * @param din_len Maximum size of response in bytes
332 * @return number of bytes in response, or -ve on error
334 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
335 const void *dout, int dout_len,
336 void *din, int din_len)
341 assert((din_len == 0) || din);
342 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
343 &in_buffer, din_len);
346 * If we were asked to put it somewhere, do so, otherwise just
347 * disregard the result.
349 if (din && in_buffer) {
350 assert(len <= din_len);
351 memmove(din, in_buffer, len);
357 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
359 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
361 if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
362 sizeof(scan->data)) != sizeof(scan->data))
368 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
370 struct ec_response_get_version *r;
372 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
373 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
376 if (maxlen > (int)sizeof(r->version_string_ro))
377 maxlen = sizeof(r->version_string_ro);
379 switch (r->current_image) {
381 memcpy(id, r->version_string_ro, maxlen);
384 memcpy(id, r->version_string_rw, maxlen);
390 id[maxlen - 1] = '\0';
394 int cros_ec_read_version(struct cros_ec_dev *dev,
395 struct ec_response_get_version **versionp)
397 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
398 (uint8_t **)versionp, sizeof(**versionp))
399 != sizeof(**versionp))
405 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
407 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
408 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
414 int cros_ec_read_current_image(struct cros_ec_dev *dev,
415 enum ec_current_image *image)
417 struct ec_response_get_version *r;
419 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
420 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
423 *image = r->current_image;
427 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
428 struct ec_response_vboot_hash *hash)
430 struct ec_params_vboot_hash p;
433 start = get_timer(0);
434 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
435 mdelay(50); /* Insert some reasonable delay */
437 p.cmd = EC_VBOOT_HASH_GET;
438 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
439 hash, sizeof(*hash)) < 0)
442 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
443 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
444 return -EC_RES_TIMEOUT;
451 int cros_ec_read_hash(struct cros_ec_dev *dev,
452 struct ec_response_vboot_hash *hash)
454 struct ec_params_vboot_hash p;
457 p.cmd = EC_VBOOT_HASH_GET;
458 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
459 hash, sizeof(*hash)) < 0)
462 /* If the EC is busy calculating the hash, fidget until it's done. */
463 rv = cros_ec_wait_on_hash_done(dev, hash);
467 /* If the hash is valid, we're done. Otherwise, we have to kick it off
468 * again and wait for it to complete. Note that we explicitly assume
469 * that hashing zero bytes is always wrong, even though that would
470 * produce a valid hash value. */
471 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
474 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
475 __func__, hash->status, hash->size);
477 p.cmd = EC_VBOOT_HASH_START;
478 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
480 p.offset = EC_VBOOT_HASH_OFFSET_RW;
482 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
483 hash, sizeof(*hash)) < 0)
486 rv = cros_ec_wait_on_hash_done(dev, hash);
490 debug("%s: hash done\n", __func__);
495 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
497 struct ec_params_vboot_hash p;
498 struct ec_response_vboot_hash *hash;
500 /* We don't have an explict command for the EC to discard its current
501 * hash value, so we'll just tell it to calculate one that we know is
502 * wrong (we claim that hashing zero bytes is always invalid).
504 p.cmd = EC_VBOOT_HASH_RECALC;
505 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
510 debug("%s:\n", __func__);
512 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
513 (uint8_t **)&hash, sizeof(*hash)) < 0)
516 /* No need to wait for it to finish */
520 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
523 struct ec_params_reboot_ec p;
528 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
532 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
534 * EC reboot will take place immediately so delay to allow it
535 * to complete. Note that some reboot types (EC_REBOOT_COLD)
536 * will reboot the AP as well, in which case we won't actually
540 * TODO(rspangler@chromium.org): Would be nice if we had a
541 * better way to determine when the reboot is complete. Could
542 * we poll a memory-mapped LPC value?
550 int cros_ec_interrupt_pending(struct udevice *dev)
552 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
554 /* no interrupt support : always poll */
555 if (!dm_gpio_is_valid(&cdev->ec_int))
558 return dm_gpio_get_value(&cdev->ec_int);
561 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
563 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
564 sizeof(*info)) != sizeof(*info))
570 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
572 struct ec_response_host_event_mask *resp;
575 * Use the B copy of the event flags, because the main copy is already
578 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
579 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
582 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
585 *events_ptr = resp->mask;
589 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
591 struct ec_params_host_event_mask params;
593 params.mask = events;
596 * Use the B copy of the event flags, so it affects the data returned
597 * by cros_ec_get_host_events().
599 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
600 ¶ms, sizeof(params), NULL, 0) < 0)
606 int cros_ec_flash_protect(struct cros_ec_dev *dev,
607 uint32_t set_mask, uint32_t set_flags,
608 struct ec_response_flash_protect *resp)
610 struct ec_params_flash_protect params;
612 params.mask = set_mask;
613 params.flags = set_flags;
615 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
616 ¶ms, sizeof(params),
617 resp, sizeof(*resp)) != sizeof(*resp))
623 static int cros_ec_check_version(struct cros_ec_dev *dev)
625 struct ec_params_hello req;
626 struct ec_response_hello *resp;
628 struct dm_cros_ec_ops *ops;
631 ops = dm_cros_ec_get_ops(dev->dev);
632 if (ops->check_version) {
633 ret = ops->check_version(dev->dev);
639 * TODO(sjg@chromium.org).
640 * There is a strange oddity here with the EC. We could just ignore
641 * the response, i.e. pass the last two parameters as NULL and 0.
642 * In this case we won't read back very many bytes from the EC.
643 * On the I2C bus the EC gets upset about this and will try to send
644 * the bytes anyway. This means that we will have to wait for that
645 * to complete before continuing with a new EC command.
647 * This problem is probably unique to the I2C bus.
649 * So for now, just read all the data anyway.
652 /* Try sending a version 3 packet */
653 dev->protocol_version = 3;
655 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
656 (uint8_t **)&resp, sizeof(*resp)) > 0) {
660 /* Try sending a version 2 packet */
661 dev->protocol_version = 2;
662 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
663 (uint8_t **)&resp, sizeof(*resp)) > 0) {
668 * Fail if we're still here, since the EC doesn't understand any
669 * protcol version we speak. Version 1 interface without command
670 * version is no longer supported, and we don't know about any new
673 dev->protocol_version = 0;
674 printf("%s: ERROR: old EC interface not supported\n", __func__);
678 int cros_ec_test(struct cros_ec_dev *dev)
680 struct ec_params_hello req;
681 struct ec_response_hello *resp;
683 req.in_data = 0x12345678;
684 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
685 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
686 printf("ec_command_inptr() returned error\n");
689 if (resp->out_data != req.in_data + 0x01020304) {
690 printf("Received invalid handshake %x\n", resp->out_data);
697 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
698 uint32_t *offset, uint32_t *size)
700 struct ec_params_flash_region_info p;
701 struct ec_response_flash_region_info *r;
705 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
706 EC_VER_FLASH_REGION_INFO,
707 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
708 if (ret != sizeof(*r))
719 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
721 struct ec_params_flash_erase p;
725 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
730 * Write a single block to the flash
732 * Write a block of data to the EC flash. The size must not exceed the flash
733 * write block size which you can obtain from cros_ec_flash_write_burst_size().
735 * The offset starts at 0. You can obtain the region information from
736 * cros_ec_flash_offset() to find out where to write for a particular region.
738 * Attempting to write to the region where the EC is currently running from
739 * will result in an error.
741 * @param dev CROS-EC device
742 * @param data Pointer to data buffer to write
743 * @param offset Offset within flash to write to.
744 * @param size Number of bytes to write
745 * @return 0 if ok, -1 on error
747 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
748 const uint8_t *data, uint32_t offset, uint32_t size)
750 struct ec_params_flash_write *p;
753 p = malloc(sizeof(*p) + size);
759 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
760 memcpy(p + 1, data, p->size);
762 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
763 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
771 * Return optimal flash write burst size
773 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
775 return EC_FLASH_WRITE_VER0_SIZE;
779 * Check if a block of data is erased (all 0xff)
781 * This function is useful when dealing with flash, for checking whether a
782 * data block is erased and thus does not need to be programmed.
784 * @param data Pointer to data to check (must be word-aligned)
785 * @param size Number of bytes to check (must be word-aligned)
786 * @return 0 if erased, non-zero if any word is not erased
788 static int cros_ec_data_is_erased(const uint32_t *data, int size)
791 size /= sizeof(uint32_t);
792 for (; size > 0; size -= 4, data++)
800 * Read back flash parameters
802 * This function reads back parameters of the flash as reported by the EC
804 * @param dev Pointer to device
805 * @param info Pointer to output flash info struct
807 int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
808 struct ec_response_flash_info *info)
812 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
813 NULL, 0, info, sizeof(*info));
817 return ret < sizeof(*info) ? -1 : 0;
820 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
821 uint32_t offset, uint32_t size)
823 uint32_t burst = cros_ec_flash_write_burst_size(dev);
828 * TODO: round up to the nearest multiple of write size. Can get away
829 * without that on link right now because its write size is 4 bytes.
832 for (off = offset; off < end; off += burst, data += burst) {
835 /* If the data is empty, there is no point in programming it */
836 todo = min(end - off, burst);
837 if (dev->optimise_flash_write &&
838 cros_ec_data_is_erased((uint32_t *)data, todo))
841 ret = cros_ec_flash_write_block(dev, data, off, todo);
850 * Read a single block from the flash
852 * Read a block of data from the EC flash. The size must not exceed the flash
853 * write block size which you can obtain from cros_ec_flash_write_burst_size().
855 * The offset starts at 0. You can obtain the region information from
856 * cros_ec_flash_offset() to find out where to read for a particular region.
858 * @param dev CROS-EC device
859 * @param data Pointer to data buffer to read into
860 * @param offset Offset within flash to read from
861 * @param size Number of bytes to read
862 * @return 0 if ok, -1 on error
864 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
865 uint32_t offset, uint32_t size)
867 struct ec_params_flash_read p;
872 return ec_command(dev, EC_CMD_FLASH_READ, 0,
873 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
876 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
879 uint32_t burst = cros_ec_flash_write_burst_size(dev);
884 for (off = offset; off < end; off += burst, data += burst) {
885 ret = cros_ec_flash_read_block(dev, data, off,
886 min(end - off, burst));
894 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
895 const uint8_t *image, int image_size)
897 uint32_t rw_offset, rw_size;
900 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
902 if (image_size > (int)rw_size)
905 /* Invalidate the existing hash, just in case the AP reboots
906 * unexpectedly during the update. If that happened, the EC RW firmware
907 * would be invalid, but the EC would still have the original hash.
909 ret = cros_ec_invalidate_hash(dev);
914 * Erase the entire RW section, so that the EC doesn't see any garbage
915 * past the new image if it's smaller than the current image.
917 * TODO: could optimize this to erase just the current image, since
918 * presumably everything past that is 0xff's. But would still need to
919 * round up to the nearest multiple of erase size.
921 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
925 /* Write the image */
926 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
933 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
935 struct ec_params_vbnvcontext p;
938 p.op = EC_VBNV_CONTEXT_OP_READ;
940 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
941 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
942 if (len < EC_VBNV_BLOCK_SIZE)
948 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
950 struct ec_params_vbnvcontext p;
953 p.op = EC_VBNV_CONTEXT_OP_WRITE;
954 memcpy(p.block, block, sizeof(p.block));
956 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
957 &p, sizeof(p), NULL, 0);
964 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
966 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
967 struct ec_params_ldo_set params;
969 params.index = index;
970 params.state = state;
972 if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
979 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
981 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
982 struct ec_params_ldo_get params;
983 struct ec_response_ldo_get *resp;
985 params.index = index;
987 if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
988 (uint8_t **)&resp, sizeof(*resp)) !=
992 *state = resp->state;
997 int cros_ec_register(struct udevice *dev)
999 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1003 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1005 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1007 if (cros_ec_check_version(cdev)) {
1008 debug("%s: Could not detect CROS-EC version\n", __func__);
1009 return -CROS_EC_ERR_CHECK_VERSION;
1012 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1013 debug("%s: Could not read KBC ID\n", __func__);
1014 return -CROS_EC_ERR_READ_ID;
1017 /* Remember this device for use by the cros_ec command */
1018 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1019 cdev->protocol_version, id);
1024 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1026 ofnode flash_node, node;
1028 flash_node = dev_read_subnode(dev, "flash");
1029 if (!ofnode_valid(flash_node)) {
1030 debug("Failed to find flash node\n");
1034 if (of_read_fmap_entry(flash_node, "flash", &config->flash)) {
1035 debug("Failed to decode flash node in chrome-ec\n");
1039 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1041 ofnode_for_each_subnode(node, flash_node) {
1042 const char *name = ofnode_get_name(node);
1043 enum ec_flash_region region;
1045 if (0 == strcmp(name, "ro")) {
1046 region = EC_FLASH_REGION_RO;
1047 } else if (0 == strcmp(name, "rw")) {
1048 region = EC_FLASH_REGION_RW;
1049 } else if (0 == strcmp(name, "wp-ro")) {
1050 region = EC_FLASH_REGION_WP_RO;
1052 debug("Unknown EC flash region name '%s'\n", name);
1056 if (of_read_fmap_entry(node, "reg", &config->region[region])) {
1057 debug("Failed to decode flash region in chrome-ec'\n");
1065 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1068 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1070 struct ec_params_i2c_passthru p;
1071 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1074 struct ec_response_i2c_passthru r;
1075 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1077 struct ec_params_i2c_passthru *p = ¶ms.p;
1078 struct ec_response_i2c_passthru *r = &response.r;
1079 struct ec_params_i2c_passthru_msg *msg;
1080 uint8_t *pdata, *read_ptr = NULL;
1088 p->num_msgs = nmsgs;
1089 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1091 /* Create a message to write the register address and optional data */
1092 pdata = (uint8_t *)p + size;
1095 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1096 bool is_read = in->flags & I2C_M_RD;
1098 msg->addr_flags = in->addr;
1101 msg->addr_flags |= EC_I2C_FLAG_READ;
1102 read_len += in->len;
1104 if (sizeof(*r) + read_len > sizeof(response)) {
1105 puts("Read length too big for buffer\n");
1109 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1110 puts("Params too large for buffer\n");
1113 memcpy(pdata, in->buf, in->len);
1118 rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1119 r, sizeof(*r) + read_len);
1123 /* Parse response */
1124 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1125 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1129 if (rv < sizeof(*r) + read_len) {
1130 puts("Truncated read response\n");
1134 /* We only support a single read message for each transfer */
1136 memcpy(read_ptr, r->data, read_len);
1141 UCLASS_DRIVER(cros_ec) = {
1142 .id = UCLASS_CROS_EC,
1144 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1145 .post_bind = dm_scan_fdt_dev,