1 // SPDX-License-Identifier: GPL-2.0+
3 * Chromium OS cros_ec driver
5 * Copyright (c) 2012 The Chromium OS Authors.
9 * This is the interface to the Chrome OS EC. It provides keyboard functions,
10 * power control and battery management. Quite a few other functions are
11 * provided to enable the EC software to be updated, talk to the EC's I2C bus
12 * and store a small amount of data in a memory which persists while the EC
16 #define LOG_CATEGORY UCLASS_CROS_EC
28 #include <linux/delay.h>
29 #include <linux/errno.h>
31 #include <asm-generic/gpio.h>
32 #include <dm/device-internal.h>
33 #include <dm/of_extra.h>
34 #include <dm/uclass-internal.h>
37 #define debug_trace(fmt, b...) debug(fmt, #b)
39 #define debug_trace(fmt, b...)
43 /* Timeout waiting for a flash erase command to complete */
44 CROS_EC_CMD_TIMEOUT_MS = 5000,
45 /* Timeout waiting for a synchronous hash to be recomputed */
46 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
48 /* Wait 10 ms between attempts to check if EC's hash is ready */
49 CROS_EC_HASH_CHECK_DELAY_MS = 10,
53 #define INVALID_HCMD 0xFF
56 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
57 * which does not support UHEPI command.
64 [EC_HOST_EVENT_MAIN] = {
65 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
69 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
70 EC_CMD_HOST_EVENT_GET_B,
72 [EC_HOST_EVENT_SCI_MASK] = {
73 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
74 EC_CMD_HOST_EVENT_GET_SCI_MASK,
76 [EC_HOST_EVENT_SMI_MASK] = {
77 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
78 EC_CMD_HOST_EVENT_GET_SMI_MASK,
80 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
81 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
83 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
84 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
85 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
87 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
88 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
89 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
91 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
92 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
93 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
95 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
96 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
97 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
101 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
106 printf("%s: ", name);
108 printf("cmd=%#x: ", cmd);
109 for (i = 0; i < len; i++)
110 printf("%02x ", data[i]);
116 * Calculate a simple 8-bit checksum of a data block
118 * @param data Data block to checksum
119 * @param size Size of data block in bytes
120 * @return checksum value (0 to 255)
122 int cros_ec_calc_checksum(const uint8_t *data, int size)
126 for (i = csum = 0; i < size; i++)
132 * Create a request packet for protocol version 3.
134 * The packet is stored in the device's internal output buffer.
136 * @param dev CROS-EC device
137 * @param cmd Command to send (EC_CMD_...)
138 * @param cmd_version Version of command to send (EC_VER_...)
139 * @param dout Output data (may be NULL If dout_len=0)
140 * @param dout_len Size of output data in bytes
141 * @return packet size in bytes, or <0 if error.
143 static int create_proto3_request(struct cros_ec_dev *cdev,
144 int cmd, int cmd_version,
145 const void *dout, int dout_len)
147 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
148 int out_bytes = dout_len + sizeof(*rq);
150 /* Fail if output size is too big */
151 if (out_bytes > (int)sizeof(cdev->dout)) {
152 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
153 return -EC_RES_REQUEST_TRUNCATED;
156 /* Fill in request packet */
157 rq->struct_version = EC_HOST_REQUEST_VERSION;
160 rq->command_version = cmd_version;
162 rq->data_len = dout_len;
164 /* Copy data after header */
165 memcpy(rq + 1, dout, dout_len);
167 /* Write checksum field so the entire packet sums to 0 */
168 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
170 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
172 /* Return size of request packet */
177 * Prepare the device to receive a protocol version 3 response.
179 * @param dev CROS-EC device
180 * @param din_len Maximum size of response in bytes
181 * @return maximum expected number of bytes in response, or <0 if error.
183 static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
185 int in_bytes = din_len + sizeof(struct ec_host_response);
187 /* Fail if input size is too big */
188 if (in_bytes > (int)sizeof(cdev->din)) {
189 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
190 return -EC_RES_RESPONSE_TOO_BIG;
193 /* Return expected size of response packet */
198 * Handle a protocol version 3 response packet.
200 * The packet must already be stored in the device's internal input buffer.
202 * @param dev CROS-EC device
203 * @param dinp Returns pointer to response data
204 * @param din_len Maximum size of response in bytes
205 * @return number of bytes of response data, or <0 if error. Note that error
206 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
209 static int handle_proto3_response(struct cros_ec_dev *dev,
210 uint8_t **dinp, int din_len)
212 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
216 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
218 /* Check input data */
219 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
220 debug("%s: EC response version mismatch\n", __func__);
221 return -EC_RES_INVALID_RESPONSE;
225 debug("%s: EC response reserved != 0\n", __func__);
226 return -EC_RES_INVALID_RESPONSE;
229 if (rs->data_len > din_len) {
230 debug("%s: EC returned too much data\n", __func__);
231 return -EC_RES_RESPONSE_TOO_BIG;
234 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
236 /* Update in_bytes to actual data size */
237 in_bytes = sizeof(*rs) + rs->data_len;
239 /* Verify checksum */
240 csum = cros_ec_calc_checksum(dev->din, in_bytes);
242 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
244 return -EC_RES_INVALID_CHECKSUM;
247 /* Return error result, if any */
249 return -(int)rs->result;
251 /* If we're still here, set response data pointer and return length */
252 *dinp = (uint8_t *)(rs + 1);
257 static int send_command_proto3(struct cros_ec_dev *cdev,
258 int cmd, int cmd_version,
259 const void *dout, int dout_len,
260 uint8_t **dinp, int din_len)
262 struct dm_cros_ec_ops *ops;
263 int out_bytes, in_bytes;
266 /* Create request packet */
267 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
272 /* Prepare response buffer */
273 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
277 ops = dm_cros_ec_get_ops(cdev->dev);
278 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
283 /* Process the response */
284 return handle_proto3_response(cdev, dinp, din_len);
287 static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
288 const void *dout, int dout_len,
289 uint8_t **dinp, int din_len)
291 struct dm_cros_ec_ops *ops;
294 /* Handle protocol version 3 support */
295 if (dev->protocol_version == 3) {
296 return send_command_proto3(dev, cmd, cmd_version,
297 dout, dout_len, dinp, din_len);
300 ops = dm_cros_ec_get_ops(dev->dev);
301 ret = ops->command(dev->dev, cmd, cmd_version,
302 (const uint8_t *)dout, dout_len, dinp, din_len);
308 * Send a command to the CROS-EC device and return the reply.
310 * The device's internal input/output buffers are used.
312 * @param dev CROS-EC device
313 * @param cmd Command to send (EC_CMD_...)
314 * @param cmd_version Version of command to send (EC_VER_...)
315 * @param dout Output data (may be NULL If dout_len=0)
316 * @param dout_len Size of output data in bytes
317 * @param dinp Response data (may be NULL If din_len=0).
318 * If not NULL, it will be updated to point to the data
319 * and will always be double word aligned (64-bits)
320 * @param din_len Maximum size of response in bytes
321 * @return number of bytes in response, or -ve on error
323 static int ec_command_inptr(struct udevice *dev, uint cmd,
324 int cmd_version, const void *dout, int dout_len,
325 uint8_t **dinp, int din_len)
327 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
331 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
334 /* If the command doesn't complete, wait a while */
335 if (len == -EC_RES_IN_PROGRESS) {
336 struct ec_response_get_comms_status *resp = NULL;
339 /* Wait for command to complete */
340 start = get_timer(0);
344 mdelay(50); /* Insert some reasonable delay */
345 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
347 (uint8_t **)&resp, sizeof(*resp));
351 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
352 debug("%s: Command %#02x timeout\n",
354 return -EC_RES_TIMEOUT;
356 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
358 /* OK it completed, so read the status response */
359 /* not sure why it was 0 for the last argument */
360 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
364 debug("%s: len=%d, din=%p\n", __func__, len, din);
366 /* If we have any data to return, it must be 64bit-aligned */
367 assert(len <= 0 || !((uintptr_t)din & 7));
375 * Send a command to the CROS-EC device and return the reply.
377 * The device's internal input/output buffers are used.
379 * @param dev CROS-EC device
380 * @param cmd Command to send (EC_CMD_...)
381 * @param cmd_version Version of command to send (EC_VER_...)
382 * @param dout Output data (may be NULL If dout_len=0)
383 * @param dout_len Size of output data in bytes
384 * @param din Response data (may be NULL If din_len=0).
385 * It not NULL, it is a place for ec_command() to copy the
387 * @param din_len Maximum size of response in bytes
388 * @return number of bytes in response, or -ve on error
390 static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
391 const void *dout, int dout_len,
392 void *din, int din_len)
397 assert((din_len == 0) || din);
398 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
399 &in_buffer, din_len);
402 * If we were asked to put it somewhere, do so, otherwise just
403 * disregard the result.
405 if (din && in_buffer) {
406 assert(len <= din_len);
407 memmove(din, in_buffer, len);
413 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
415 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
416 sizeof(scan->data)) != sizeof(scan->data))
422 int cros_ec_get_next_event(struct udevice *dev,
423 struct ec_response_get_next_event *event)
427 ret = ec_command(dev, EC_CMD_GET_NEXT_EVENT, 0, NULL, 0,
428 event, sizeof(*event));
431 else if (ret != sizeof(*event))
432 return -EC_RES_INVALID_RESPONSE;
437 int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
439 struct ec_response_get_version *r;
442 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
443 (uint8_t **)&r, sizeof(*r));
444 if (ret != sizeof(*r)) {
445 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
449 if (maxlen > (int)sizeof(r->version_string_ro))
450 maxlen = sizeof(r->version_string_ro);
452 switch (r->current_image) {
454 memcpy(id, r->version_string_ro, maxlen);
457 memcpy(id, r->version_string_rw, maxlen);
460 log_err("Invalid EC image %d\n", r->current_image);
464 id[maxlen - 1] = '\0';
468 int cros_ec_read_version(struct udevice *dev,
469 struct ec_response_get_version **versionp)
471 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
472 (uint8_t **)versionp, sizeof(**versionp))
473 != sizeof(**versionp))
479 int cros_ec_read_build_info(struct udevice *dev, char **strp)
481 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
482 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
488 int cros_ec_read_current_image(struct udevice *dev,
489 enum ec_current_image *image)
491 struct ec_response_get_version *r;
493 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
494 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
497 *image = r->current_image;
501 static int cros_ec_wait_on_hash_done(struct udevice *dev,
502 struct ec_params_vboot_hash *p,
503 struct ec_response_vboot_hash *hash)
507 start = get_timer(0);
508 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
509 mdelay(CROS_EC_HASH_CHECK_DELAY_MS);
511 p->cmd = EC_VBOOT_HASH_GET;
513 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, p, sizeof(*p), hash,
517 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
518 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
519 return -EC_RES_TIMEOUT;
525 int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
526 struct ec_response_vboot_hash *hash)
528 struct ec_params_vboot_hash p;
531 p.cmd = EC_VBOOT_HASH_GET;
532 p.offset = hash_offset;
533 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
534 hash, sizeof(*hash)) < 0)
537 /* If the EC is busy calculating the hash, fidget until it's done. */
538 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
542 /* If the hash is valid, we're done. Otherwise, we have to kick it off
543 * again and wait for it to complete. Note that we explicitly assume
544 * that hashing zero bytes is always wrong, even though that would
545 * produce a valid hash value. */
546 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
549 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
550 __func__, hash->status, hash->size);
552 p.cmd = EC_VBOOT_HASH_START;
553 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
555 p.offset = hash_offset;
557 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
558 hash, sizeof(*hash)) < 0)
561 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
564 if (hash->status != EC_VBOOT_HASH_STATUS_DONE) {
565 log_err("Hash did not complete, status=%d\n", hash->status);
569 debug("%s: hash done\n", __func__);
574 static int cros_ec_invalidate_hash(struct udevice *dev)
576 struct ec_params_vboot_hash p;
577 struct ec_response_vboot_hash *hash;
579 /* We don't have an explict command for the EC to discard its current
580 * hash value, so we'll just tell it to calculate one that we know is
581 * wrong (we claim that hashing zero bytes is always invalid).
583 p.cmd = EC_VBOOT_HASH_RECALC;
584 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
589 debug("%s:\n", __func__);
591 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
592 (uint8_t **)&hash, sizeof(*hash)) < 0)
595 /* No need to wait for it to finish */
599 int cros_ec_hello(struct udevice *dev, uint *handshakep)
601 struct ec_params_hello req;
602 struct ec_response_hello *resp;
604 req.in_data = 0x12345678;
605 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
606 (uint8_t **)&resp, sizeof(*resp)) < 0)
608 if (resp->out_data != req.in_data + 0x01020304) {
609 printf("Received invalid handshake %x\n", resp->out_data);
611 *handshakep = req.in_data;
618 int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
620 struct ec_params_reboot_ec p;
625 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
629 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
633 * EC reboot will take place immediately so delay to allow it
634 * to complete. Note that some reboot types (EC_REBOOT_COLD)
635 * will reboot the AP as well, in which case we won't actually
639 start = get_timer(0);
640 while (cros_ec_hello(dev, NULL)) {
641 if (get_timer(start) > 3000) {
642 log_err("EC did not return from reboot\n");
652 int cros_ec_interrupt_pending(struct udevice *dev)
654 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
656 /* no interrupt support : always poll */
657 if (!dm_gpio_is_valid(&cdev->ec_int))
660 return dm_gpio_get_value(&cdev->ec_int);
663 int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
665 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
666 sizeof(*info)) != sizeof(*info))
672 int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
674 struct ec_response_host_event_mask rsp;
677 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
680 else if (ret != sizeof(rsp))
688 int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
690 struct ec_params_host_event_mask req;
695 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
702 int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
704 struct ec_response_host_event_mask *resp;
707 * Use the B copy of the event flags, because the main copy is already
710 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
711 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
714 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
717 *events_ptr = resp->mask;
721 int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
723 struct ec_params_host_event_mask params;
725 params.mask = events;
728 * Use the B copy of the event flags, so it affects the data returned
729 * by cros_ec_get_host_events().
731 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
732 ¶ms, sizeof(params), NULL, 0) < 0)
738 int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
740 struct ec_response_flash_protect *resp)
742 struct ec_params_flash_protect params;
744 params.mask = set_mask;
745 params.flags = set_flags;
747 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
748 ¶ms, sizeof(params),
749 resp, sizeof(*resp)) != sizeof(*resp))
755 int cros_ec_entering_mode(struct udevice *dev, int mode)
759 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
766 static int cros_ec_check_version(struct udevice *dev)
768 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
769 struct ec_params_hello req;
771 struct dm_cros_ec_ops *ops;
774 ops = dm_cros_ec_get_ops(dev);
775 if (ops->check_version) {
776 ret = ops->check_version(dev);
782 * TODO(sjg@chromium.org).
783 * There is a strange oddity here with the EC. We could just ignore
784 * the response, i.e. pass the last two parameters as NULL and 0.
785 * In this case we won't read back very many bytes from the EC.
786 * On the I2C bus the EC gets upset about this and will try to send
787 * the bytes anyway. This means that we will have to wait for that
788 * to complete before continuing with a new EC command.
790 * This problem is probably unique to the I2C bus.
792 * So for now, just read all the data anyway.
795 /* Try sending a version 3 packet */
796 cdev->protocol_version = 3;
798 ret = cros_ec_hello(dev, NULL);
799 if (!ret || ret == -ENOTSYNC)
802 /* Try sending a version 2 packet */
803 cdev->protocol_version = 2;
804 ret = cros_ec_hello(dev, NULL);
805 if (!ret || ret == -ENOTSYNC)
809 * Fail if we're still here, since the EC doesn't understand any
810 * protcol version we speak. Version 1 interface without command
811 * version is no longer supported, and we don't know about any new
814 cdev->protocol_version = 0;
815 printf("%s: ERROR: old EC interface not supported\n", __func__);
819 int cros_ec_test(struct udevice *dev)
824 ret = cros_ec_hello(dev, &out_data);
825 if (ret == -ENOTSYNC) {
826 printf("Received invalid handshake %x\n", out_data);
829 printf("ec_command_inptr() returned error\n");
836 int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
837 uint32_t *offset, uint32_t *size)
839 struct ec_params_flash_region_info p;
840 struct ec_response_flash_region_info *r;
844 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
845 EC_VER_FLASH_REGION_INFO,
846 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
847 if (ret != sizeof(*r))
858 int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
860 struct ec_params_flash_erase p;
864 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
869 * Write a single block to the flash
871 * Write a block of data to the EC flash. The size must not exceed the flash
872 * write block size which you can obtain from cros_ec_flash_write_burst_size().
874 * The offset starts at 0. You can obtain the region information from
875 * cros_ec_flash_offset() to find out where to write for a particular region.
877 * Attempting to write to the region where the EC is currently running from
878 * will result in an error.
880 * @param dev CROS-EC device
881 * @param data Pointer to data buffer to write
882 * @param offset Offset within flash to write to.
883 * @param size Number of bytes to write
884 * @return 0 if ok, -1 on error
886 static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
887 uint32_t offset, uint32_t size)
889 struct ec_params_flash_write *p;
892 p = malloc(sizeof(*p) + size);
898 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
899 memcpy(p + 1, data, p->size);
901 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
902 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
910 * Return optimal flash write burst size
912 static int cros_ec_flash_write_burst_size(struct udevice *dev)
914 return EC_FLASH_WRITE_VER0_SIZE;
918 * Check if a block of data is erased (all 0xff)
920 * This function is useful when dealing with flash, for checking whether a
921 * data block is erased and thus does not need to be programmed.
923 * @param data Pointer to data to check (must be word-aligned)
924 * @param size Number of bytes to check (must be word-aligned)
925 * @return 0 if erased, non-zero if any word is not erased
927 static int cros_ec_data_is_erased(const uint32_t *data, int size)
930 size /= sizeof(uint32_t);
931 for (; size > 0; size -= 4, data++)
939 * Read back flash parameters
941 * This function reads back parameters of the flash as reported by the EC
943 * @param dev Pointer to device
944 * @param info Pointer to output flash info struct
946 int cros_ec_read_flashinfo(struct udevice *dev,
947 struct ec_response_flash_info *info)
951 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
952 NULL, 0, info, sizeof(*info));
956 return ret < sizeof(*info) ? -1 : 0;
959 int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
960 uint32_t offset, uint32_t size)
962 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
963 uint32_t burst = cros_ec_flash_write_burst_size(dev);
971 * TODO: round up to the nearest multiple of write size. Can get away
972 * without that on link right now because its write size is 4 bytes.
975 for (off = offset; off < end; off += burst, data += burst) {
978 /* If the data is empty, there is no point in programming it */
979 todo = min(end - off, burst);
980 if (cdev->optimise_flash_write &&
981 cros_ec_data_is_erased((uint32_t *)data, todo))
984 ret = cros_ec_flash_write_block(dev, data, off, todo);
993 * Run verification on a slot
995 * @param me CrosEc instance
996 * @param region Region to run verification on
997 * @return 0 if success or not applicable. Non-zero if verification failed.
999 int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
1001 struct ec_params_efs_verify p;
1004 log_info("EFS: EC is verifying updated image...\n");
1007 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
1009 log_info("EFS: Verification success\n");
1012 if (rv == -EC_RES_INVALID_COMMAND) {
1013 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
1016 log_info("EFS: Verification failed\n");
1022 * Read a single block from the flash
1024 * Read a block of data from the EC flash. The size must not exceed the flash
1025 * write block size which you can obtain from cros_ec_flash_write_burst_size().
1027 * The offset starts at 0. You can obtain the region information from
1028 * cros_ec_flash_offset() to find out where to read for a particular region.
1030 * @param dev CROS-EC device
1031 * @param data Pointer to data buffer to read into
1032 * @param offset Offset within flash to read from
1033 * @param size Number of bytes to read
1034 * @return 0 if ok, -1 on error
1036 static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
1037 uint32_t offset, uint32_t size)
1039 struct ec_params_flash_read p;
1044 return ec_command(dev, EC_CMD_FLASH_READ, 0,
1045 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1048 int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
1051 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1055 end = offset + size;
1056 for (off = offset; off < end; off += burst, data += burst) {
1057 ret = cros_ec_flash_read_block(dev, data, off,
1058 min(end - off, burst));
1066 int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
1069 uint32_t rw_offset, rw_size;
1072 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1075 if (image_size > (int)rw_size)
1078 /* Invalidate the existing hash, just in case the AP reboots
1079 * unexpectedly during the update. If that happened, the EC RW firmware
1080 * would be invalid, but the EC would still have the original hash.
1082 ret = cros_ec_invalidate_hash(dev);
1087 * Erase the entire RW section, so that the EC doesn't see any garbage
1088 * past the new image if it's smaller than the current image.
1090 * TODO: could optimize this to erase just the current image, since
1091 * presumably everything past that is 0xff's. But would still need to
1092 * round up to the nearest multiple of erase size.
1094 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1098 /* Write the image */
1099 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1106 int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
1108 struct ec_params_vbnvcontext p;
1111 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1114 p.op = EC_VBNV_CONTEXT_OP_READ;
1116 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1117 &p, sizeof(uint32_t) + size, block, size);
1119 log_err("Expected %d bytes, got %d\n", size, len);
1126 int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
1128 struct ec_params_vbnvcontext p;
1131 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1133 p.op = EC_VBNV_CONTEXT_OP_WRITE;
1134 memcpy(p.block, block, size);
1136 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1137 &p, sizeof(uint32_t) + size, NULL, 0);
1144 int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1146 struct ec_params_battery_cutoff p;
1150 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1158 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
1160 struct ec_params_ldo_set params;
1162 params.index = index;
1163 params.state = state;
1165 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
1172 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
1174 struct ec_params_ldo_get params;
1175 struct ec_response_ldo_get *resp;
1177 params.index = index;
1179 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
1180 (uint8_t **)&resp, sizeof(*resp)) !=
1184 *state = resp->state;
1189 int cros_ec_register(struct udevice *dev)
1191 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1195 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1197 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1199 if (cros_ec_check_version(dev)) {
1200 debug("%s: Could not detect CROS-EC version\n", __func__);
1201 return -CROS_EC_ERR_CHECK_VERSION;
1204 if (cros_ec_read_id(dev, id, sizeof(id))) {
1205 debug("%s: Could not read KBC ID\n", __func__);
1206 return -CROS_EC_ERR_READ_ID;
1209 /* Remember this device for use by the cros_ec command */
1210 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1211 cdev->protocol_version, id);
1216 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1218 ofnode flash_node, node;
1220 flash_node = dev_read_subnode(dev, "flash");
1221 if (!ofnode_valid(flash_node)) {
1222 debug("Failed to find flash node\n");
1226 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
1227 debug("Failed to decode flash node in chrome-ec\n");
1231 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1233 ofnode_for_each_subnode(node, flash_node) {
1234 const char *name = ofnode_get_name(node);
1235 enum ec_flash_region region;
1237 if (0 == strcmp(name, "ro")) {
1238 region = EC_FLASH_REGION_RO;
1239 } else if (0 == strcmp(name, "rw")) {
1240 region = EC_FLASH_REGION_ACTIVE;
1241 } else if (0 == strcmp(name, "wp-ro")) {
1242 region = EC_FLASH_REGION_WP_RO;
1244 debug("Unknown EC flash region name '%s'\n", name);
1248 if (ofnode_read_fmap_entry(node, &config->region[region])) {
1249 debug("Failed to decode flash region in chrome-ec'\n");
1257 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1261 struct ec_params_i2c_passthru p;
1262 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1265 struct ec_response_i2c_passthru r;
1266 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1268 struct ec_params_i2c_passthru *p = ¶ms.p;
1269 struct ec_response_i2c_passthru *r = &response.r;
1270 struct ec_params_i2c_passthru_msg *msg;
1271 uint8_t *pdata, *read_ptr = NULL;
1279 p->num_msgs = nmsgs;
1280 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1282 /* Create a message to write the register address and optional data */
1283 pdata = (uint8_t *)p + size;
1286 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1287 bool is_read = in->flags & I2C_M_RD;
1289 msg->addr_flags = in->addr;
1292 msg->addr_flags |= EC_I2C_FLAG_READ;
1293 read_len += in->len;
1295 if (sizeof(*r) + read_len > sizeof(response)) {
1296 puts("Read length too big for buffer\n");
1300 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1301 puts("Params too large for buffer\n");
1304 memcpy(pdata, in->buf, in->len);
1309 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1310 r, sizeof(*r) + read_len);
1314 /* Parse response */
1315 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1316 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1320 if (rv < sizeof(*r) + read_len) {
1321 puts("Truncated read response\n");
1325 /* We only support a single read message for each transfer */
1327 memcpy(read_ptr, r->data, read_len);
1332 int cros_ec_check_feature(struct udevice *dev, int feature)
1334 struct ec_response_get_features r;
1337 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1341 if (feature >= 8 * sizeof(r.flags))
1344 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1348 * Query the EC for specified mask indicating enabled events.
1349 * The EC maintains separate event masks for SMI, SCI and WAKE.
1351 static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1355 struct ec_params_host_event req;
1356 struct ec_response_host_event rsp;
1358 req.action = action;
1359 req.mask_type = mask;
1360 if (action != EC_HOST_EVENT_GET)
1364 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1367 if (action != EC_HOST_EVENT_GET)
1375 static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1376 uint action, uint64_t *value)
1379 struct ec_params_host_event_mask req;
1380 struct ec_response_host_event_mask rsp;
1382 if (hcmd == INVALID_HCMD)
1385 if (action != EC_HOST_EVENT_GET)
1386 req.mask = (uint32_t)*value;
1390 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1391 if (action != EC_HOST_EVENT_GET)
1399 bool cros_ec_is_uhepi_supported(struct udevice *dev)
1401 #define UHEPI_SUPPORTED 1
1402 #define UHEPI_NOT_SUPPORTED 2
1403 static int uhepi_support;
1405 if (!uhepi_support) {
1406 uhepi_support = cros_ec_check_feature(dev,
1407 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1408 UHEPI_NOT_SUPPORTED;
1409 log_debug("Chrome EC: UHEPI %s\n",
1410 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1413 return uhepi_support == UHEPI_SUPPORTED;
1416 static int cros_ec_get_mask(struct udevice *dev, uint type)
1420 if (cros_ec_is_uhepi_supported(dev)) {
1421 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1423 assert(type < ARRAY_SIZE(event_map));
1424 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1425 EC_HOST_EVENT_GET, &value);
1430 static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1432 if (cros_ec_is_uhepi_supported(dev))
1433 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1435 assert(type < ARRAY_SIZE(event_map));
1437 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1438 EC_HOST_EVENT_CLEAR, &mask);
1441 uint64_t cros_ec_get_events_b(struct udevice *dev)
1443 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1446 int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1448 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1450 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1453 int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1455 struct ec_params_charge_state p;
1456 struct ec_response_charge_state r;
1459 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1460 p.get_param.param = CS_PARAM_LIMIT_POWER;
1461 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1465 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1466 * LIMIT_POWER is not requested.
1468 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1469 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1473 if (ret != sizeof(r.get_param))
1476 *limit_powerp = r.get_param.value;
1480 int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1482 struct ec_params_config_power_button params;
1485 params.flags = flags;
1486 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1487 ¶ms, sizeof(params), NULL, 0);
1494 int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1499 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1504 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1507 int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1512 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1517 /* Set lid close event state in the EC SMI event mask */
1519 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1521 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1523 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1527 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1531 UCLASS_DRIVER(cros_ec) = {
1532 .id = UCLASS_CROS_EC,
1534 .per_device_auto = sizeof(struct cros_ec_dev),
1535 .post_bind = dm_scan_fdt_dev,
1536 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,