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 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
52 printf("cmd=%#x: ", cmd);
53 for (i = 0; i < len; i++)
54 printf("%02x ", data[i]);
60 * Calculate a simple 8-bit checksum of a data block
62 * @param data Data block to checksum
63 * @param size Size of data block in bytes
64 * @return checksum value (0 to 255)
66 int cros_ec_calc_checksum(const uint8_t *data, int size)
70 for (i = csum = 0; i < size; i++)
76 * Create a request packet for protocol version 3.
78 * The packet is stored in the device's internal output buffer.
80 * @param dev CROS-EC device
81 * @param cmd Command to send (EC_CMD_...)
82 * @param cmd_version Version of command to send (EC_VER_...)
83 * @param dout Output data (may be NULL If dout_len=0)
84 * @param dout_len Size of output data in bytes
85 * @return packet size in bytes, or <0 if error.
87 static int create_proto3_request(struct cros_ec_dev *dev,
88 int cmd, int cmd_version,
89 const void *dout, int dout_len)
91 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
92 int out_bytes = dout_len + sizeof(*rq);
94 /* Fail if output size is too big */
95 if (out_bytes > (int)sizeof(dev->dout)) {
96 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
97 return -EC_RES_REQUEST_TRUNCATED;
100 /* Fill in request packet */
101 rq->struct_version = EC_HOST_REQUEST_VERSION;
104 rq->command_version = cmd_version;
106 rq->data_len = dout_len;
108 /* Copy data after header */
109 memcpy(rq + 1, dout, dout_len);
111 /* Write checksum field so the entire packet sums to 0 */
112 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
114 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
116 /* Return size of request packet */
121 * Prepare the device to receive a protocol version 3 response.
123 * @param dev CROS-EC device
124 * @param din_len Maximum size of response in bytes
125 * @return maximum expected number of bytes in response, or <0 if error.
127 static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
129 int in_bytes = din_len + sizeof(struct ec_host_response);
131 /* Fail if input size is too big */
132 if (in_bytes > (int)sizeof(dev->din)) {
133 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
134 return -EC_RES_RESPONSE_TOO_BIG;
137 /* Return expected size of response packet */
142 * Handle a protocol version 3 response packet.
144 * The packet must already be stored in the device's internal input buffer.
146 * @param dev CROS-EC device
147 * @param dinp Returns pointer to response data
148 * @param din_len Maximum size of response in bytes
149 * @return number of bytes of response data, or <0 if error. Note that error
150 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
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 struct dm_cros_ec_ops *ops;
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 ops = dm_cros_ec_get_ops(dev->dev);
222 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
226 /* Process the response */
227 return handle_proto3_response(dev, dinp, din_len);
230 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
231 const void *dout, int dout_len,
232 uint8_t **dinp, int din_len)
234 struct dm_cros_ec_ops *ops;
237 /* Handle protocol version 3 support */
238 if (dev->protocol_version == 3) {
239 return send_command_proto3(dev, cmd, cmd_version,
240 dout, dout_len, dinp, din_len);
243 ops = dm_cros_ec_get_ops(dev->dev);
244 ret = ops->command(dev->dev, cmd, cmd_version,
245 (const uint8_t *)dout, dout_len, dinp, din_len);
251 * Send a command to the CROS-EC device and return the reply.
253 * The device's internal input/output buffers are used.
255 * @param dev CROS-EC device
256 * @param cmd Command to send (EC_CMD_...)
257 * @param cmd_version Version of command to send (EC_VER_...)
258 * @param dout Output data (may be NULL If dout_len=0)
259 * @param dout_len Size of output data in bytes
260 * @param dinp Response data (may be NULL If din_len=0).
261 * If not NULL, it will be updated to point to the data
262 * and will always be double word aligned (64-bits)
263 * @param din_len Maximum size of response in bytes
264 * @return number of bytes in response, or -ve on error
266 static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
267 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
273 len = send_command(dev, cmd, cmd_version, dout, dout_len,
276 /* If the command doesn't complete, wait a while */
277 if (len == -EC_RES_IN_PROGRESS) {
278 struct ec_response_get_comms_status *resp = NULL;
281 /* Wait for command to complete */
282 start = get_timer(0);
286 mdelay(50); /* Insert some reasonable delay */
287 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
289 (uint8_t **)&resp, sizeof(*resp));
293 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
294 debug("%s: Command %#02x timeout\n",
296 return -EC_RES_TIMEOUT;
298 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
300 /* OK it completed, so read the status response */
301 /* not sure why it was 0 for the last argument */
302 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
303 NULL, 0, &din, din_len);
306 debug("%s: len=%d, din=%p\n", __func__, len, din);
308 /* If we have any data to return, it must be 64bit-aligned */
309 assert(len <= 0 || !((uintptr_t)din & 7));
317 * Send a command to the CROS-EC device and return the reply.
319 * The device's internal input/output buffers are used.
321 * @param dev CROS-EC device
322 * @param cmd Command to send (EC_CMD_...)
323 * @param cmd_version Version of command to send (EC_VER_...)
324 * @param dout Output data (may be NULL If dout_len=0)
325 * @param dout_len Size of output data in bytes
326 * @param din Response data (may be NULL If din_len=0).
327 * It not NULL, it is a place for ec_command() to copy the
329 * @param din_len Maximum size of response in bytes
330 * @return number of bytes in response, or -ve on error
332 static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
333 const void *dout, int dout_len,
334 void *din, int din_len)
339 assert((din_len == 0) || din);
340 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
341 &in_buffer, din_len);
344 * If we were asked to put it somewhere, do so, otherwise just
345 * disregard the result.
347 if (din && in_buffer) {
348 assert(len <= din_len);
349 memmove(din, in_buffer, len);
355 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
357 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
359 if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
360 sizeof(scan->data)) != sizeof(scan->data))
366 int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
368 struct ec_response_get_version *r;
370 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
371 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
374 if (maxlen > (int)sizeof(r->version_string_ro))
375 maxlen = sizeof(r->version_string_ro);
377 switch (r->current_image) {
379 memcpy(id, r->version_string_ro, maxlen);
382 memcpy(id, r->version_string_rw, maxlen);
388 id[maxlen - 1] = '\0';
392 int cros_ec_read_version(struct cros_ec_dev *dev,
393 struct ec_response_get_version **versionp)
395 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
396 (uint8_t **)versionp, sizeof(**versionp))
397 != sizeof(**versionp))
403 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
405 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
406 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
412 int cros_ec_read_current_image(struct cros_ec_dev *dev,
413 enum ec_current_image *image)
415 struct ec_response_get_version *r;
417 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
418 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
421 *image = r->current_image;
425 static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
426 struct ec_response_vboot_hash *hash)
428 struct ec_params_vboot_hash p;
431 start = get_timer(0);
432 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
433 mdelay(50); /* Insert some reasonable delay */
435 p.cmd = EC_VBOOT_HASH_GET;
436 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
437 hash, sizeof(*hash)) < 0)
440 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
441 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
442 return -EC_RES_TIMEOUT;
449 int cros_ec_read_hash(struct cros_ec_dev *dev,
450 struct ec_response_vboot_hash *hash)
452 struct ec_params_vboot_hash p;
455 p.cmd = EC_VBOOT_HASH_GET;
456 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
457 hash, sizeof(*hash)) < 0)
460 /* If the EC is busy calculating the hash, fidget until it's done. */
461 rv = cros_ec_wait_on_hash_done(dev, hash);
465 /* If the hash is valid, we're done. Otherwise, we have to kick it off
466 * again and wait for it to complete. Note that we explicitly assume
467 * that hashing zero bytes is always wrong, even though that would
468 * produce a valid hash value. */
469 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
472 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
473 __func__, hash->status, hash->size);
475 p.cmd = EC_VBOOT_HASH_START;
476 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
478 p.offset = EC_VBOOT_HASH_OFFSET_RW;
480 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
481 hash, sizeof(*hash)) < 0)
484 rv = cros_ec_wait_on_hash_done(dev, hash);
488 debug("%s: hash done\n", __func__);
493 static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
495 struct ec_params_vboot_hash p;
496 struct ec_response_vboot_hash *hash;
498 /* We don't have an explict command for the EC to discard its current
499 * hash value, so we'll just tell it to calculate one that we know is
500 * wrong (we claim that hashing zero bytes is always invalid).
502 p.cmd = EC_VBOOT_HASH_RECALC;
503 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
508 debug("%s:\n", __func__);
510 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
511 (uint8_t **)&hash, sizeof(*hash)) < 0)
514 /* No need to wait for it to finish */
518 int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
521 struct ec_params_reboot_ec p;
526 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
530 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
532 * EC reboot will take place immediately so delay to allow it
533 * to complete. Note that some reboot types (EC_REBOOT_COLD)
534 * will reboot the AP as well, in which case we won't actually
538 * TODO(rspangler@chromium.org): Would be nice if we had a
539 * better way to determine when the reboot is complete. Could
540 * we poll a memory-mapped LPC value?
548 int cros_ec_interrupt_pending(struct udevice *dev)
550 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
552 /* no interrupt support : always poll */
553 if (!dm_gpio_is_valid(&cdev->ec_int))
556 return dm_gpio_get_value(&cdev->ec_int);
559 int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
561 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
562 sizeof(*info)) != sizeof(*info))
568 int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
570 struct ec_response_host_event_mask *resp;
573 * Use the B copy of the event flags, because the main copy is already
576 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
577 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
580 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
583 *events_ptr = resp->mask;
587 int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
589 struct ec_params_host_event_mask params;
591 params.mask = events;
594 * Use the B copy of the event flags, so it affects the data returned
595 * by cros_ec_get_host_events().
597 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
598 ¶ms, sizeof(params), NULL, 0) < 0)
604 int cros_ec_flash_protect(struct cros_ec_dev *dev,
605 uint32_t set_mask, uint32_t set_flags,
606 struct ec_response_flash_protect *resp)
608 struct ec_params_flash_protect params;
610 params.mask = set_mask;
611 params.flags = set_flags;
613 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
614 ¶ms, sizeof(params),
615 resp, sizeof(*resp)) != sizeof(*resp))
621 static int cros_ec_check_version(struct cros_ec_dev *dev)
623 struct ec_params_hello req;
624 struct ec_response_hello *resp;
626 struct dm_cros_ec_ops *ops;
629 ops = dm_cros_ec_get_ops(dev->dev);
630 if (ops->check_version) {
631 ret = ops->check_version(dev->dev);
637 * TODO(sjg@chromium.org).
638 * There is a strange oddity here with the EC. We could just ignore
639 * the response, i.e. pass the last two parameters as NULL and 0.
640 * In this case we won't read back very many bytes from the EC.
641 * On the I2C bus the EC gets upset about this and will try to send
642 * the bytes anyway. This means that we will have to wait for that
643 * to complete before continuing with a new EC command.
645 * This problem is probably unique to the I2C bus.
647 * So for now, just read all the data anyway.
650 /* Try sending a version 3 packet */
651 dev->protocol_version = 3;
653 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
654 (uint8_t **)&resp, sizeof(*resp)) > 0) {
658 /* Try sending a version 2 packet */
659 dev->protocol_version = 2;
660 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
661 (uint8_t **)&resp, sizeof(*resp)) > 0) {
666 * Fail if we're still here, since the EC doesn't understand any
667 * protcol version we speak. Version 1 interface without command
668 * version is no longer supported, and we don't know about any new
671 dev->protocol_version = 0;
672 printf("%s: ERROR: old EC interface not supported\n", __func__);
676 int cros_ec_test(struct cros_ec_dev *dev)
678 struct ec_params_hello req;
679 struct ec_response_hello *resp;
681 req.in_data = 0x12345678;
682 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
683 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
684 printf("ec_command_inptr() returned error\n");
687 if (resp->out_data != req.in_data + 0x01020304) {
688 printf("Received invalid handshake %x\n", resp->out_data);
695 int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
696 uint32_t *offset, uint32_t *size)
698 struct ec_params_flash_region_info p;
699 struct ec_response_flash_region_info *r;
703 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
704 EC_VER_FLASH_REGION_INFO,
705 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
706 if (ret != sizeof(*r))
717 int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
719 struct ec_params_flash_erase p;
723 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
728 * Write a single block to the flash
730 * Write a block of data to the EC flash. The size must not exceed the flash
731 * write block size which you can obtain from cros_ec_flash_write_burst_size().
733 * The offset starts at 0. You can obtain the region information from
734 * cros_ec_flash_offset() to find out where to write for a particular region.
736 * Attempting to write to the region where the EC is currently running from
737 * will result in an error.
739 * @param dev CROS-EC device
740 * @param data Pointer to data buffer to write
741 * @param offset Offset within flash to write to.
742 * @param size Number of bytes to write
743 * @return 0 if ok, -1 on error
745 static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
746 const uint8_t *data, uint32_t offset, uint32_t size)
748 struct ec_params_flash_write *p;
751 p = malloc(sizeof(*p) + size);
757 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
758 memcpy(p + 1, data, p->size);
760 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
761 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
769 * Return optimal flash write burst size
771 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
773 return EC_FLASH_WRITE_VER0_SIZE;
777 * Check if a block of data is erased (all 0xff)
779 * This function is useful when dealing with flash, for checking whether a
780 * data block is erased and thus does not need to be programmed.
782 * @param data Pointer to data to check (must be word-aligned)
783 * @param size Number of bytes to check (must be word-aligned)
784 * @return 0 if erased, non-zero if any word is not erased
786 static int cros_ec_data_is_erased(const uint32_t *data, int size)
789 size /= sizeof(uint32_t);
790 for (; size > 0; size -= 4, data++)
798 * Read back flash parameters
800 * This function reads back parameters of the flash as reported by the EC
802 * @param dev Pointer to device
803 * @param info Pointer to output flash info struct
805 int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
806 struct ec_response_flash_info *info)
810 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
811 NULL, 0, info, sizeof(*info));
815 return ret < sizeof(*info) ? -1 : 0;
818 int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
819 uint32_t offset, uint32_t size)
821 uint32_t burst = cros_ec_flash_write_burst_size(dev);
826 * TODO: round up to the nearest multiple of write size. Can get away
827 * without that on link right now because its write size is 4 bytes.
830 for (off = offset; off < end; off += burst, data += burst) {
833 /* If the data is empty, there is no point in programming it */
834 todo = min(end - off, burst);
835 if (dev->optimise_flash_write &&
836 cros_ec_data_is_erased((uint32_t *)data, todo))
839 ret = cros_ec_flash_write_block(dev, data, off, todo);
848 * Read a single block from the flash
850 * Read a block of data from the EC flash. The size must not exceed the flash
851 * write block size which you can obtain from cros_ec_flash_write_burst_size().
853 * The offset starts at 0. You can obtain the region information from
854 * cros_ec_flash_offset() to find out where to read for a particular region.
856 * @param dev CROS-EC device
857 * @param data Pointer to data buffer to read into
858 * @param offset Offset within flash to read from
859 * @param size Number of bytes to read
860 * @return 0 if ok, -1 on error
862 static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
863 uint32_t offset, uint32_t size)
865 struct ec_params_flash_read p;
870 return ec_command(dev, EC_CMD_FLASH_READ, 0,
871 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
874 int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
877 uint32_t burst = cros_ec_flash_write_burst_size(dev);
882 for (off = offset; off < end; off += burst, data += burst) {
883 ret = cros_ec_flash_read_block(dev, data, off,
884 min(end - off, burst));
892 int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
893 const uint8_t *image, int image_size)
895 uint32_t rw_offset, rw_size;
898 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
900 if (image_size > (int)rw_size)
903 /* Invalidate the existing hash, just in case the AP reboots
904 * unexpectedly during the update. If that happened, the EC RW firmware
905 * would be invalid, but the EC would still have the original hash.
907 ret = cros_ec_invalidate_hash(dev);
912 * Erase the entire RW section, so that the EC doesn't see any garbage
913 * past the new image if it's smaller than the current image.
915 * TODO: could optimize this to erase just the current image, since
916 * presumably everything past that is 0xff's. But would still need to
917 * round up to the nearest multiple of erase size.
919 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
923 /* Write the image */
924 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
931 int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
933 struct ec_params_vbnvcontext p;
936 p.op = EC_VBNV_CONTEXT_OP_READ;
938 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
939 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
940 if (len < EC_VBNV_BLOCK_SIZE)
946 int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
948 struct ec_params_vbnvcontext p;
951 p.op = EC_VBNV_CONTEXT_OP_WRITE;
952 memcpy(p.block, block, sizeof(p.block));
954 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
955 &p, sizeof(p), NULL, 0);
962 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
964 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
965 struct ec_params_ldo_set params;
967 params.index = index;
968 params.state = state;
970 if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
977 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
979 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
980 struct ec_params_ldo_get params;
981 struct ec_response_ldo_get *resp;
983 params.index = index;
985 if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
986 (uint8_t **)&resp, sizeof(*resp)) !=
990 *state = resp->state;
995 int cros_ec_register(struct udevice *dev)
997 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1001 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1003 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1005 if (cros_ec_check_version(cdev)) {
1006 debug("%s: Could not detect CROS-EC version\n", __func__);
1007 return -CROS_EC_ERR_CHECK_VERSION;
1010 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1011 debug("%s: Could not read KBC ID\n", __func__);
1012 return -CROS_EC_ERR_READ_ID;
1015 /* Remember this device for use by the cros_ec command */
1016 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1017 cdev->protocol_version, id);
1022 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1024 ofnode flash_node, node;
1026 flash_node = dev_read_subnode(dev, "flash");
1027 if (!ofnode_valid(flash_node)) {
1028 debug("Failed to find flash node\n");
1032 if (of_read_fmap_entry(flash_node, "flash", &config->flash)) {
1033 debug("Failed to decode flash node in chrome-ec\n");
1037 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1039 ofnode_for_each_subnode(node, flash_node) {
1040 const char *name = ofnode_get_name(node);
1041 enum ec_flash_region region;
1043 if (0 == strcmp(name, "ro")) {
1044 region = EC_FLASH_REGION_RO;
1045 } else if (0 == strcmp(name, "rw")) {
1046 region = EC_FLASH_REGION_RW;
1047 } else if (0 == strcmp(name, "wp-ro")) {
1048 region = EC_FLASH_REGION_WP_RO;
1050 debug("Unknown EC flash region name '%s'\n", name);
1054 if (of_read_fmap_entry(node, "reg", &config->region[region])) {
1055 debug("Failed to decode flash region in chrome-ec'\n");
1063 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1066 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1068 struct ec_params_i2c_passthru p;
1069 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1072 struct ec_response_i2c_passthru r;
1073 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1075 struct ec_params_i2c_passthru *p = ¶ms.p;
1076 struct ec_response_i2c_passthru *r = &response.r;
1077 struct ec_params_i2c_passthru_msg *msg;
1078 uint8_t *pdata, *read_ptr = NULL;
1086 p->num_msgs = nmsgs;
1087 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1089 /* Create a message to write the register address and optional data */
1090 pdata = (uint8_t *)p + size;
1093 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1094 bool is_read = in->flags & I2C_M_RD;
1096 msg->addr_flags = in->addr;
1099 msg->addr_flags |= EC_I2C_FLAG_READ;
1100 read_len += in->len;
1102 if (sizeof(*r) + read_len > sizeof(response)) {
1103 puts("Read length too big for buffer\n");
1107 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1108 puts("Params too large for buffer\n");
1111 memcpy(pdata, in->buf, in->len);
1116 rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1117 r, sizeof(*r) + read_len);
1121 /* Parse response */
1122 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1123 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1127 if (rv < sizeof(*r) + read_len) {
1128 puts("Truncated read response\n");
1132 /* We only support a single read message for each transfer */
1134 memcpy(read_ptr, r->data, read_len);
1139 UCLASS_DRIVER(cros_ec) = {
1140 .id = UCLASS_CROS_EC,
1142 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1143 .post_bind = dm_scan_fdt_dev,