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
27 #include <linux/errno.h>
29 #include <asm-generic/gpio.h>
30 #include <dm/device-internal.h>
31 #include <dm/of_extra.h>
32 #include <dm/uclass-internal.h>
35 #define debug_trace(fmt, b...) debug(fmt, #b)
37 #define debug_trace(fmt, b...)
41 /* Timeout waiting for a flash erase command to complete */
42 CROS_EC_CMD_TIMEOUT_MS = 5000,
43 /* Timeout waiting for a synchronous hash to be recomputed */
44 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
47 #define INVALID_HCMD 0xFF
50 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
51 * which does not support UHEPI command.
58 [EC_HOST_EVENT_MAIN] = {
59 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
63 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
64 EC_CMD_HOST_EVENT_GET_B,
66 [EC_HOST_EVENT_SCI_MASK] = {
67 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
68 EC_CMD_HOST_EVENT_GET_SCI_MASK,
70 [EC_HOST_EVENT_SMI_MASK] = {
71 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
72 EC_CMD_HOST_EVENT_GET_SMI_MASK,
74 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
75 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
77 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
78 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
79 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
81 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
82 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
83 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
85 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
86 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
87 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
89 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
90 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
91 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
95 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
100 printf("%s: ", name);
102 printf("cmd=%#x: ", cmd);
103 for (i = 0; i < len; i++)
104 printf("%02x ", data[i]);
110 * Calculate a simple 8-bit checksum of a data block
112 * @param data Data block to checksum
113 * @param size Size of data block in bytes
114 * @return checksum value (0 to 255)
116 int cros_ec_calc_checksum(const uint8_t *data, int size)
120 for (i = csum = 0; i < size; i++)
126 * Create a request packet for protocol version 3.
128 * The packet is stored in the device's internal output buffer.
130 * @param dev CROS-EC device
131 * @param cmd Command to send (EC_CMD_...)
132 * @param cmd_version Version of command to send (EC_VER_...)
133 * @param dout Output data (may be NULL If dout_len=0)
134 * @param dout_len Size of output data in bytes
135 * @return packet size in bytes, or <0 if error.
137 static int create_proto3_request(struct cros_ec_dev *cdev,
138 int cmd, int cmd_version,
139 const void *dout, int dout_len)
141 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
142 int out_bytes = dout_len + sizeof(*rq);
144 /* Fail if output size is too big */
145 if (out_bytes > (int)sizeof(cdev->dout)) {
146 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
147 return -EC_RES_REQUEST_TRUNCATED;
150 /* Fill in request packet */
151 rq->struct_version = EC_HOST_REQUEST_VERSION;
154 rq->command_version = cmd_version;
156 rq->data_len = dout_len;
158 /* Copy data after header */
159 memcpy(rq + 1, dout, dout_len);
161 /* Write checksum field so the entire packet sums to 0 */
162 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
164 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
166 /* Return size of request packet */
171 * Prepare the device to receive a protocol version 3 response.
173 * @param dev CROS-EC device
174 * @param din_len Maximum size of response in bytes
175 * @return maximum expected number of bytes in response, or <0 if error.
177 static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
179 int in_bytes = din_len + sizeof(struct ec_host_response);
181 /* Fail if input size is too big */
182 if (in_bytes > (int)sizeof(cdev->din)) {
183 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
184 return -EC_RES_RESPONSE_TOO_BIG;
187 /* Return expected size of response packet */
192 * Handle a protocol version 3 response packet.
194 * The packet must already be stored in the device's internal input buffer.
196 * @param dev CROS-EC device
197 * @param dinp Returns pointer to response data
198 * @param din_len Maximum size of response in bytes
199 * @return number of bytes of response data, or <0 if error. Note that error
200 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
203 static int handle_proto3_response(struct cros_ec_dev *dev,
204 uint8_t **dinp, int din_len)
206 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
210 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
212 /* Check input data */
213 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
214 debug("%s: EC response version mismatch\n", __func__);
215 return -EC_RES_INVALID_RESPONSE;
219 debug("%s: EC response reserved != 0\n", __func__);
220 return -EC_RES_INVALID_RESPONSE;
223 if (rs->data_len > din_len) {
224 debug("%s: EC returned too much data\n", __func__);
225 return -EC_RES_RESPONSE_TOO_BIG;
228 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
230 /* Update in_bytes to actual data size */
231 in_bytes = sizeof(*rs) + rs->data_len;
233 /* Verify checksum */
234 csum = cros_ec_calc_checksum(dev->din, in_bytes);
236 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
238 return -EC_RES_INVALID_CHECKSUM;
241 /* Return error result, if any */
243 return -(int)rs->result;
245 /* If we're still here, set response data pointer and return length */
246 *dinp = (uint8_t *)(rs + 1);
251 static int send_command_proto3(struct cros_ec_dev *cdev,
252 int cmd, int cmd_version,
253 const void *dout, int dout_len,
254 uint8_t **dinp, int din_len)
256 struct dm_cros_ec_ops *ops;
257 int out_bytes, in_bytes;
260 /* Create request packet */
261 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
266 /* Prepare response buffer */
267 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
271 ops = dm_cros_ec_get_ops(cdev->dev);
272 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
277 /* Process the response */
278 return handle_proto3_response(cdev, dinp, din_len);
281 static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
282 const void *dout, int dout_len,
283 uint8_t **dinp, int din_len)
285 struct dm_cros_ec_ops *ops;
288 /* Handle protocol version 3 support */
289 if (dev->protocol_version == 3) {
290 return send_command_proto3(dev, cmd, cmd_version,
291 dout, dout_len, dinp, din_len);
294 ops = dm_cros_ec_get_ops(dev->dev);
295 ret = ops->command(dev->dev, cmd, cmd_version,
296 (const uint8_t *)dout, dout_len, dinp, din_len);
302 * Send a command to the CROS-EC device and return the reply.
304 * The device's internal input/output buffers are used.
306 * @param dev CROS-EC device
307 * @param cmd Command to send (EC_CMD_...)
308 * @param cmd_version Version of command to send (EC_VER_...)
309 * @param dout Output data (may be NULL If dout_len=0)
310 * @param dout_len Size of output data in bytes
311 * @param dinp Response data (may be NULL If din_len=0).
312 * If not NULL, it will be updated to point to the data
313 * and will always be double word aligned (64-bits)
314 * @param din_len Maximum size of response in bytes
315 * @return number of bytes in response, or -ve on error
317 static int ec_command_inptr(struct udevice *dev, uint cmd,
318 int cmd_version, const void *dout, int dout_len,
319 uint8_t **dinp, int din_len)
321 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
325 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
328 /* If the command doesn't complete, wait a while */
329 if (len == -EC_RES_IN_PROGRESS) {
330 struct ec_response_get_comms_status *resp = NULL;
333 /* Wait for command to complete */
334 start = get_timer(0);
338 mdelay(50); /* Insert some reasonable delay */
339 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
341 (uint8_t **)&resp, sizeof(*resp));
345 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
346 debug("%s: Command %#02x timeout\n",
348 return -EC_RES_TIMEOUT;
350 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
352 /* OK it completed, so read the status response */
353 /* not sure why it was 0 for the last argument */
354 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
358 debug("%s: len=%d, din=%p\n", __func__, len, din);
360 /* If we have any data to return, it must be 64bit-aligned */
361 assert(len <= 0 || !((uintptr_t)din & 7));
369 * Send a command to the CROS-EC device and return the reply.
371 * The device's internal input/output buffers are used.
373 * @param dev CROS-EC device
374 * @param cmd Command to send (EC_CMD_...)
375 * @param cmd_version Version of command to send (EC_VER_...)
376 * @param dout Output data (may be NULL If dout_len=0)
377 * @param dout_len Size of output data in bytes
378 * @param din Response data (may be NULL If din_len=0).
379 * It not NULL, it is a place for ec_command() to copy the
381 * @param din_len Maximum size of response in bytes
382 * @return number of bytes in response, or -ve on error
384 static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
385 const void *dout, int dout_len,
386 void *din, int din_len)
391 assert((din_len == 0) || din);
392 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
393 &in_buffer, din_len);
396 * If we were asked to put it somewhere, do so, otherwise just
397 * disregard the result.
399 if (din && in_buffer) {
400 assert(len <= din_len);
401 memmove(din, in_buffer, len);
407 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
409 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
410 sizeof(scan->data)) != sizeof(scan->data))
416 int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
418 struct ec_response_get_version *r;
421 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
422 (uint8_t **)&r, sizeof(*r));
423 if (ret != sizeof(*r)) {
424 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
428 if (maxlen > (int)sizeof(r->version_string_ro))
429 maxlen = sizeof(r->version_string_ro);
431 switch (r->current_image) {
433 memcpy(id, r->version_string_ro, maxlen);
436 memcpy(id, r->version_string_rw, maxlen);
439 log_err("Invalid EC image %d\n", r->current_image);
443 id[maxlen - 1] = '\0';
447 int cros_ec_read_version(struct udevice *dev,
448 struct ec_response_get_version **versionp)
450 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
451 (uint8_t **)versionp, sizeof(**versionp))
452 != sizeof(**versionp))
458 int cros_ec_read_build_info(struct udevice *dev, char **strp)
460 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
461 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
467 int cros_ec_read_current_image(struct udevice *dev,
468 enum ec_current_image *image)
470 struct ec_response_get_version *r;
472 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
473 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
476 *image = r->current_image;
480 static int cros_ec_wait_on_hash_done(struct udevice *dev,
481 struct ec_response_vboot_hash *hash)
483 struct ec_params_vboot_hash p;
486 start = get_timer(0);
487 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
488 mdelay(50); /* Insert some reasonable delay */
490 p.cmd = EC_VBOOT_HASH_GET;
491 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
492 hash, sizeof(*hash)) < 0)
495 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
496 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
497 return -EC_RES_TIMEOUT;
503 int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
504 struct ec_response_vboot_hash *hash)
506 struct ec_params_vboot_hash p;
509 p.cmd = EC_VBOOT_HASH_GET;
510 p.offset = hash_offset;
511 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
512 hash, sizeof(*hash)) < 0)
515 /* If the EC is busy calculating the hash, fidget until it's done. */
516 rv = cros_ec_wait_on_hash_done(dev, hash);
520 /* If the hash is valid, we're done. Otherwise, we have to kick it off
521 * again and wait for it to complete. Note that we explicitly assume
522 * that hashing zero bytes is always wrong, even though that would
523 * produce a valid hash value. */
524 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
527 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
528 __func__, hash->status, hash->size);
530 p.cmd = EC_VBOOT_HASH_START;
531 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
533 p.offset = hash_offset;
535 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
536 hash, sizeof(*hash)) < 0)
539 rv = cros_ec_wait_on_hash_done(dev, hash);
543 debug("%s: hash done\n", __func__);
548 static int cros_ec_invalidate_hash(struct udevice *dev)
550 struct ec_params_vboot_hash p;
551 struct ec_response_vboot_hash *hash;
553 /* We don't have an explict command for the EC to discard its current
554 * hash value, so we'll just tell it to calculate one that we know is
555 * wrong (we claim that hashing zero bytes is always invalid).
557 p.cmd = EC_VBOOT_HASH_RECALC;
558 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
563 debug("%s:\n", __func__);
565 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
566 (uint8_t **)&hash, sizeof(*hash)) < 0)
569 /* No need to wait for it to finish */
573 int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
575 struct ec_params_reboot_ec p;
580 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
584 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
586 * EC reboot will take place immediately so delay to allow it
587 * to complete. Note that some reboot types (EC_REBOOT_COLD)
588 * will reboot the AP as well, in which case we won't actually
592 * TODO(rspangler@chromium.org): Would be nice if we had a
593 * better way to determine when the reboot is complete. Could
594 * we poll a memory-mapped LPC value?
602 int cros_ec_interrupt_pending(struct udevice *dev)
604 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
606 /* no interrupt support : always poll */
607 if (!dm_gpio_is_valid(&cdev->ec_int))
610 return dm_gpio_get_value(&cdev->ec_int);
613 int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
615 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
616 sizeof(*info)) != sizeof(*info))
622 int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
624 struct ec_response_host_event_mask rsp;
627 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
630 else if (ret != sizeof(rsp))
638 int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
640 struct ec_params_host_event_mask req;
645 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
652 int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
654 struct ec_response_host_event_mask *resp;
657 * Use the B copy of the event flags, because the main copy is already
660 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
661 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
664 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
667 *events_ptr = resp->mask;
671 int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
673 struct ec_params_host_event_mask params;
675 params.mask = events;
678 * Use the B copy of the event flags, so it affects the data returned
679 * by cros_ec_get_host_events().
681 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
682 ¶ms, sizeof(params), NULL, 0) < 0)
688 int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
690 struct ec_response_flash_protect *resp)
692 struct ec_params_flash_protect params;
694 params.mask = set_mask;
695 params.flags = set_flags;
697 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
698 ¶ms, sizeof(params),
699 resp, sizeof(*resp)) != sizeof(*resp))
705 int cros_ec_entering_mode(struct udevice *dev, int mode)
709 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
716 static int cros_ec_check_version(struct udevice *dev)
718 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
719 struct ec_params_hello req;
720 struct ec_response_hello *resp;
722 struct dm_cros_ec_ops *ops;
725 ops = dm_cros_ec_get_ops(dev);
726 if (ops->check_version) {
727 ret = ops->check_version(dev);
733 * TODO(sjg@chromium.org).
734 * There is a strange oddity here with the EC. We could just ignore
735 * the response, i.e. pass the last two parameters as NULL and 0.
736 * In this case we won't read back very many bytes from the EC.
737 * On the I2C bus the EC gets upset about this and will try to send
738 * the bytes anyway. This means that we will have to wait for that
739 * to complete before continuing with a new EC command.
741 * This problem is probably unique to the I2C bus.
743 * So for now, just read all the data anyway.
746 /* Try sending a version 3 packet */
747 cdev->protocol_version = 3;
749 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
750 (uint8_t **)&resp, sizeof(*resp)) > 0)
753 /* Try sending a version 2 packet */
754 cdev->protocol_version = 2;
755 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
756 (uint8_t **)&resp, sizeof(*resp)) > 0)
760 * Fail if we're still here, since the EC doesn't understand any
761 * protcol version we speak. Version 1 interface without command
762 * version is no longer supported, and we don't know about any new
765 cdev->protocol_version = 0;
766 printf("%s: ERROR: old EC interface not supported\n", __func__);
770 int cros_ec_test(struct udevice *dev)
772 struct ec_params_hello req;
773 struct ec_response_hello *resp;
775 req.in_data = 0x12345678;
776 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
777 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
778 printf("ec_command_inptr() returned error\n");
781 if (resp->out_data != req.in_data + 0x01020304) {
782 printf("Received invalid handshake %x\n", resp->out_data);
789 int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
790 uint32_t *offset, uint32_t *size)
792 struct ec_params_flash_region_info p;
793 struct ec_response_flash_region_info *r;
797 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
798 EC_VER_FLASH_REGION_INFO,
799 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
800 if (ret != sizeof(*r))
811 int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
813 struct ec_params_flash_erase p;
817 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
822 * Write a single block to the flash
824 * Write a block of data to the EC flash. The size must not exceed the flash
825 * write block size which you can obtain from cros_ec_flash_write_burst_size().
827 * The offset starts at 0. You can obtain the region information from
828 * cros_ec_flash_offset() to find out where to write for a particular region.
830 * Attempting to write to the region where the EC is currently running from
831 * will result in an error.
833 * @param dev CROS-EC device
834 * @param data Pointer to data buffer to write
835 * @param offset Offset within flash to write to.
836 * @param size Number of bytes to write
837 * @return 0 if ok, -1 on error
839 static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
840 uint32_t offset, uint32_t size)
842 struct ec_params_flash_write *p;
845 p = malloc(sizeof(*p) + size);
851 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
852 memcpy(p + 1, data, p->size);
854 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
855 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
863 * Return optimal flash write burst size
865 static int cros_ec_flash_write_burst_size(struct udevice *dev)
867 return EC_FLASH_WRITE_VER0_SIZE;
871 * Check if a block of data is erased (all 0xff)
873 * This function is useful when dealing with flash, for checking whether a
874 * data block is erased and thus does not need to be programmed.
876 * @param data Pointer to data to check (must be word-aligned)
877 * @param size Number of bytes to check (must be word-aligned)
878 * @return 0 if erased, non-zero if any word is not erased
880 static int cros_ec_data_is_erased(const uint32_t *data, int size)
883 size /= sizeof(uint32_t);
884 for (; size > 0; size -= 4, data++)
892 * Read back flash parameters
894 * This function reads back parameters of the flash as reported by the EC
896 * @param dev Pointer to device
897 * @param info Pointer to output flash info struct
899 int cros_ec_read_flashinfo(struct udevice *dev,
900 struct ec_response_flash_info *info)
904 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
905 NULL, 0, info, sizeof(*info));
909 return ret < sizeof(*info) ? -1 : 0;
912 int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
913 uint32_t offset, uint32_t size)
915 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
916 uint32_t burst = cros_ec_flash_write_burst_size(dev);
924 * TODO: round up to the nearest multiple of write size. Can get away
925 * without that on link right now because its write size is 4 bytes.
928 for (off = offset; off < end; off += burst, data += burst) {
931 /* If the data is empty, there is no point in programming it */
932 todo = min(end - off, burst);
933 if (cdev->optimise_flash_write &&
934 cros_ec_data_is_erased((uint32_t *)data, todo))
937 ret = cros_ec_flash_write_block(dev, data, off, todo);
946 * Run verification on a slot
948 * @param me CrosEc instance
949 * @param region Region to run verification on
950 * @return 0 if success or not applicable. Non-zero if verification failed.
952 int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
954 struct ec_params_efs_verify p;
957 log_info("EFS: EC is verifying updated image...\n");
960 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
962 log_info("EFS: Verification success\n");
965 if (rv == -EC_RES_INVALID_COMMAND) {
966 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
969 log_info("EFS: Verification failed\n");
975 * Read a single block from the flash
977 * Read a block of data from the EC flash. The size must not exceed the flash
978 * write block size which you can obtain from cros_ec_flash_write_burst_size().
980 * The offset starts at 0. You can obtain the region information from
981 * cros_ec_flash_offset() to find out where to read for a particular region.
983 * @param dev CROS-EC device
984 * @param data Pointer to data buffer to read into
985 * @param offset Offset within flash to read from
986 * @param size Number of bytes to read
987 * @return 0 if ok, -1 on error
989 static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
990 uint32_t offset, uint32_t size)
992 struct ec_params_flash_read p;
997 return ec_command(dev, EC_CMD_FLASH_READ, 0,
998 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1001 int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
1004 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1008 end = offset + size;
1009 for (off = offset; off < end; off += burst, data += burst) {
1010 ret = cros_ec_flash_read_block(dev, data, off,
1011 min(end - off, burst));
1019 int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
1022 uint32_t rw_offset, rw_size;
1025 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1028 if (image_size > (int)rw_size)
1031 /* Invalidate the existing hash, just in case the AP reboots
1032 * unexpectedly during the update. If that happened, the EC RW firmware
1033 * would be invalid, but the EC would still have the original hash.
1035 ret = cros_ec_invalidate_hash(dev);
1040 * Erase the entire RW section, so that the EC doesn't see any garbage
1041 * past the new image if it's smaller than the current image.
1043 * TODO: could optimize this to erase just the current image, since
1044 * presumably everything past that is 0xff's. But would still need to
1045 * round up to the nearest multiple of erase size.
1047 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1051 /* Write the image */
1052 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1059 int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
1061 struct ec_params_vbnvcontext p;
1064 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1067 p.op = EC_VBNV_CONTEXT_OP_READ;
1069 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1070 &p, sizeof(uint32_t) + size, block, size);
1072 log_err("Expected %d bytes, got %d\n", size, len);
1079 int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
1081 struct ec_params_vbnvcontext p;
1084 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1086 p.op = EC_VBNV_CONTEXT_OP_WRITE;
1087 memcpy(p.block, block, size);
1089 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1090 &p, sizeof(uint32_t) + size, NULL, 0);
1097 int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1099 struct ec_params_battery_cutoff p;
1103 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1111 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
1113 struct ec_params_ldo_set params;
1115 params.index = index;
1116 params.state = state;
1118 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
1125 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
1127 struct ec_params_ldo_get params;
1128 struct ec_response_ldo_get *resp;
1130 params.index = index;
1132 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
1133 (uint8_t **)&resp, sizeof(*resp)) !=
1137 *state = resp->state;
1142 int cros_ec_register(struct udevice *dev)
1144 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1148 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1150 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1152 if (cros_ec_check_version(dev)) {
1153 debug("%s: Could not detect CROS-EC version\n", __func__);
1154 return -CROS_EC_ERR_CHECK_VERSION;
1157 if (cros_ec_read_id(dev, id, sizeof(id))) {
1158 debug("%s: Could not read KBC ID\n", __func__);
1159 return -CROS_EC_ERR_READ_ID;
1162 /* Remember this device for use by the cros_ec command */
1163 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1164 cdev->protocol_version, id);
1169 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1171 ofnode flash_node, node;
1173 flash_node = dev_read_subnode(dev, "flash");
1174 if (!ofnode_valid(flash_node)) {
1175 debug("Failed to find flash node\n");
1179 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
1180 debug("Failed to decode flash node in chrome-ec\n");
1184 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1186 ofnode_for_each_subnode(node, flash_node) {
1187 const char *name = ofnode_get_name(node);
1188 enum ec_flash_region region;
1190 if (0 == strcmp(name, "ro")) {
1191 region = EC_FLASH_REGION_RO;
1192 } else if (0 == strcmp(name, "rw")) {
1193 region = EC_FLASH_REGION_ACTIVE;
1194 } else if (0 == strcmp(name, "wp-ro")) {
1195 region = EC_FLASH_REGION_WP_RO;
1197 debug("Unknown EC flash region name '%s'\n", name);
1201 if (ofnode_read_fmap_entry(node, &config->region[region])) {
1202 debug("Failed to decode flash region in chrome-ec'\n");
1210 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1214 struct ec_params_i2c_passthru p;
1215 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1218 struct ec_response_i2c_passthru r;
1219 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1221 struct ec_params_i2c_passthru *p = ¶ms.p;
1222 struct ec_response_i2c_passthru *r = &response.r;
1223 struct ec_params_i2c_passthru_msg *msg;
1224 uint8_t *pdata, *read_ptr = NULL;
1232 p->num_msgs = nmsgs;
1233 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1235 /* Create a message to write the register address and optional data */
1236 pdata = (uint8_t *)p + size;
1239 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1240 bool is_read = in->flags & I2C_M_RD;
1242 msg->addr_flags = in->addr;
1245 msg->addr_flags |= EC_I2C_FLAG_READ;
1246 read_len += in->len;
1248 if (sizeof(*r) + read_len > sizeof(response)) {
1249 puts("Read length too big for buffer\n");
1253 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1254 puts("Params too large for buffer\n");
1257 memcpy(pdata, in->buf, in->len);
1262 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1263 r, sizeof(*r) + read_len);
1267 /* Parse response */
1268 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1269 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1273 if (rv < sizeof(*r) + read_len) {
1274 puts("Truncated read response\n");
1278 /* We only support a single read message for each transfer */
1280 memcpy(read_ptr, r->data, read_len);
1285 int cros_ec_check_feature(struct udevice *dev, int feature)
1287 struct ec_response_get_features r;
1290 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1294 if (feature >= 8 * sizeof(r.flags))
1297 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1301 * Query the EC for specified mask indicating enabled events.
1302 * The EC maintains separate event masks for SMI, SCI and WAKE.
1304 static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1308 struct ec_params_host_event req;
1309 struct ec_response_host_event rsp;
1311 req.action = action;
1312 req.mask_type = mask;
1313 if (action != EC_HOST_EVENT_GET)
1317 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1320 if (action != EC_HOST_EVENT_GET)
1328 static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1329 uint action, uint64_t *value)
1332 struct ec_params_host_event_mask req;
1333 struct ec_response_host_event_mask rsp;
1335 if (hcmd == INVALID_HCMD)
1338 if (action != EC_HOST_EVENT_GET)
1339 req.mask = (uint32_t)*value;
1343 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1344 if (action != EC_HOST_EVENT_GET)
1352 bool cros_ec_is_uhepi_supported(struct udevice *dev)
1354 #define UHEPI_SUPPORTED 1
1355 #define UHEPI_NOT_SUPPORTED 2
1356 static int uhepi_support;
1358 if (!uhepi_support) {
1359 uhepi_support = cros_ec_check_feature(dev,
1360 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1361 UHEPI_NOT_SUPPORTED;
1362 log_debug("Chrome EC: UHEPI %s\n",
1363 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1366 return uhepi_support == UHEPI_SUPPORTED;
1369 static int cros_ec_get_mask(struct udevice *dev, uint type)
1373 if (cros_ec_is_uhepi_supported(dev)) {
1374 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1376 assert(type < ARRAY_SIZE(event_map));
1377 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1378 EC_HOST_EVENT_GET, &value);
1383 static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1385 if (cros_ec_is_uhepi_supported(dev))
1386 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1388 assert(type < ARRAY_SIZE(event_map));
1390 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1391 EC_HOST_EVENT_CLEAR, &mask);
1394 uint64_t cros_ec_get_events_b(struct udevice *dev)
1396 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1399 int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1401 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1403 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1406 int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1408 struct ec_params_charge_state p;
1409 struct ec_response_charge_state r;
1412 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1413 p.get_param.param = CS_PARAM_LIMIT_POWER;
1414 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1418 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1419 * LIMIT_POWER is not requested.
1421 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1422 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1426 if (ret != sizeof(r.get_param))
1429 *limit_powerp = r.get_param.value;
1433 int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1435 struct ec_params_config_power_button params;
1438 params.flags = flags;
1439 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1440 ¶ms, sizeof(params), NULL, 0);
1447 int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1452 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1457 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1460 int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1465 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1470 /* Set lid close event state in the EC SMI event mask */
1472 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1474 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1476 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1480 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1484 UCLASS_DRIVER(cros_ec) = {
1485 .id = UCLASS_CROS_EC,
1487 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1488 .post_bind = dm_scan_fdt_dev,
1489 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,