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/errno.h>
30 #include <asm-generic/gpio.h>
31 #include <dm/device-internal.h>
32 #include <dm/of_extra.h>
33 #include <dm/uclass-internal.h>
36 #define debug_trace(fmt, b...) debug(fmt, #b)
38 #define debug_trace(fmt, b...)
42 /* Timeout waiting for a flash erase command to complete */
43 CROS_EC_CMD_TIMEOUT_MS = 5000,
44 /* Timeout waiting for a synchronous hash to be recomputed */
45 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
48 #define INVALID_HCMD 0xFF
51 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
52 * which does not support UHEPI command.
59 [EC_HOST_EVENT_MAIN] = {
60 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
64 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
65 EC_CMD_HOST_EVENT_GET_B,
67 [EC_HOST_EVENT_SCI_MASK] = {
68 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
69 EC_CMD_HOST_EVENT_GET_SCI_MASK,
71 [EC_HOST_EVENT_SMI_MASK] = {
72 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
73 EC_CMD_HOST_EVENT_GET_SMI_MASK,
75 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
76 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
78 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
79 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
80 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
82 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
83 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
84 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
86 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
87 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
88 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
90 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
91 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
92 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
96 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
101 printf("%s: ", name);
103 printf("cmd=%#x: ", cmd);
104 for (i = 0; i < len; i++)
105 printf("%02x ", data[i]);
111 * Calculate a simple 8-bit checksum of a data block
113 * @param data Data block to checksum
114 * @param size Size of data block in bytes
115 * @return checksum value (0 to 255)
117 int cros_ec_calc_checksum(const uint8_t *data, int size)
121 for (i = csum = 0; i < size; i++)
127 * Create a request packet for protocol version 3.
129 * The packet is stored in the device's internal output buffer.
131 * @param dev CROS-EC device
132 * @param cmd Command to send (EC_CMD_...)
133 * @param cmd_version Version of command to send (EC_VER_...)
134 * @param dout Output data (may be NULL If dout_len=0)
135 * @param dout_len Size of output data in bytes
136 * @return packet size in bytes, or <0 if error.
138 static int create_proto3_request(struct cros_ec_dev *cdev,
139 int cmd, int cmd_version,
140 const void *dout, int dout_len)
142 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
143 int out_bytes = dout_len + sizeof(*rq);
145 /* Fail if output size is too big */
146 if (out_bytes > (int)sizeof(cdev->dout)) {
147 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
148 return -EC_RES_REQUEST_TRUNCATED;
151 /* Fill in request packet */
152 rq->struct_version = EC_HOST_REQUEST_VERSION;
155 rq->command_version = cmd_version;
157 rq->data_len = dout_len;
159 /* Copy data after header */
160 memcpy(rq + 1, dout, dout_len);
162 /* Write checksum field so the entire packet sums to 0 */
163 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
165 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
167 /* Return size of request packet */
172 * Prepare the device to receive a protocol version 3 response.
174 * @param dev CROS-EC device
175 * @param din_len Maximum size of response in bytes
176 * @return maximum expected number of bytes in response, or <0 if error.
178 static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
180 int in_bytes = din_len + sizeof(struct ec_host_response);
182 /* Fail if input size is too big */
183 if (in_bytes > (int)sizeof(cdev->din)) {
184 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
185 return -EC_RES_RESPONSE_TOO_BIG;
188 /* Return expected size of response packet */
193 * Handle a protocol version 3 response packet.
195 * The packet must already be stored in the device's internal input buffer.
197 * @param dev CROS-EC device
198 * @param dinp Returns pointer to response data
199 * @param din_len Maximum size of response in bytes
200 * @return number of bytes of response data, or <0 if error. Note that error
201 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
204 static int handle_proto3_response(struct cros_ec_dev *dev,
205 uint8_t **dinp, int din_len)
207 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
211 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
213 /* Check input data */
214 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
215 debug("%s: EC response version mismatch\n", __func__);
216 return -EC_RES_INVALID_RESPONSE;
220 debug("%s: EC response reserved != 0\n", __func__);
221 return -EC_RES_INVALID_RESPONSE;
224 if (rs->data_len > din_len) {
225 debug("%s: EC returned too much data\n", __func__);
226 return -EC_RES_RESPONSE_TOO_BIG;
229 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
231 /* Update in_bytes to actual data size */
232 in_bytes = sizeof(*rs) + rs->data_len;
234 /* Verify checksum */
235 csum = cros_ec_calc_checksum(dev->din, in_bytes);
237 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
239 return -EC_RES_INVALID_CHECKSUM;
242 /* Return error result, if any */
244 return -(int)rs->result;
246 /* If we're still here, set response data pointer and return length */
247 *dinp = (uint8_t *)(rs + 1);
252 static int send_command_proto3(struct cros_ec_dev *cdev,
253 int cmd, int cmd_version,
254 const void *dout, int dout_len,
255 uint8_t **dinp, int din_len)
257 struct dm_cros_ec_ops *ops;
258 int out_bytes, in_bytes;
261 /* Create request packet */
262 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
267 /* Prepare response buffer */
268 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
272 ops = dm_cros_ec_get_ops(cdev->dev);
273 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
278 /* Process the response */
279 return handle_proto3_response(cdev, dinp, din_len);
282 static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
283 const void *dout, int dout_len,
284 uint8_t **dinp, int din_len)
286 struct dm_cros_ec_ops *ops;
289 /* Handle protocol version 3 support */
290 if (dev->protocol_version == 3) {
291 return send_command_proto3(dev, cmd, cmd_version,
292 dout, dout_len, dinp, din_len);
295 ops = dm_cros_ec_get_ops(dev->dev);
296 ret = ops->command(dev->dev, cmd, cmd_version,
297 (const uint8_t *)dout, dout_len, dinp, din_len);
303 * Send a command to the CROS-EC device and return the reply.
305 * The device's internal input/output buffers are used.
307 * @param dev CROS-EC device
308 * @param cmd Command to send (EC_CMD_...)
309 * @param cmd_version Version of command to send (EC_VER_...)
310 * @param dout Output data (may be NULL If dout_len=0)
311 * @param dout_len Size of output data in bytes
312 * @param dinp Response data (may be NULL If din_len=0).
313 * If not NULL, it will be updated to point to the data
314 * and will always be double word aligned (64-bits)
315 * @param din_len Maximum size of response in bytes
316 * @return number of bytes in response, or -ve on error
318 static int ec_command_inptr(struct udevice *dev, uint cmd,
319 int cmd_version, const void *dout, int dout_len,
320 uint8_t **dinp, int din_len)
322 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
326 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
329 /* If the command doesn't complete, wait a while */
330 if (len == -EC_RES_IN_PROGRESS) {
331 struct ec_response_get_comms_status *resp = NULL;
334 /* Wait for command to complete */
335 start = get_timer(0);
339 mdelay(50); /* Insert some reasonable delay */
340 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
342 (uint8_t **)&resp, sizeof(*resp));
346 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
347 debug("%s: Command %#02x timeout\n",
349 return -EC_RES_TIMEOUT;
351 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
353 /* OK it completed, so read the status response */
354 /* not sure why it was 0 for the last argument */
355 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
359 debug("%s: len=%d, din=%p\n", __func__, len, din);
361 /* If we have any data to return, it must be 64bit-aligned */
362 assert(len <= 0 || !((uintptr_t)din & 7));
370 * Send a command to the CROS-EC device and return the reply.
372 * The device's internal input/output buffers are used.
374 * @param dev CROS-EC device
375 * @param cmd Command to send (EC_CMD_...)
376 * @param cmd_version Version of command to send (EC_VER_...)
377 * @param dout Output data (may be NULL If dout_len=0)
378 * @param dout_len Size of output data in bytes
379 * @param din Response data (may be NULL If din_len=0).
380 * It not NULL, it is a place for ec_command() to copy the
382 * @param din_len Maximum size of response in bytes
383 * @return number of bytes in response, or -ve on error
385 static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
386 const void *dout, int dout_len,
387 void *din, int din_len)
392 assert((din_len == 0) || din);
393 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
394 &in_buffer, din_len);
397 * If we were asked to put it somewhere, do so, otherwise just
398 * disregard the result.
400 if (din && in_buffer) {
401 assert(len <= din_len);
402 memmove(din, in_buffer, len);
408 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
410 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
411 sizeof(scan->data)) != sizeof(scan->data))
417 int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
419 struct ec_response_get_version *r;
422 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
423 (uint8_t **)&r, sizeof(*r));
424 if (ret != sizeof(*r)) {
425 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
429 if (maxlen > (int)sizeof(r->version_string_ro))
430 maxlen = sizeof(r->version_string_ro);
432 switch (r->current_image) {
434 memcpy(id, r->version_string_ro, maxlen);
437 memcpy(id, r->version_string_rw, maxlen);
440 log_err("Invalid EC image %d\n", r->current_image);
444 id[maxlen - 1] = '\0';
448 int cros_ec_read_version(struct udevice *dev,
449 struct ec_response_get_version **versionp)
451 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
452 (uint8_t **)versionp, sizeof(**versionp))
453 != sizeof(**versionp))
459 int cros_ec_read_build_info(struct udevice *dev, char **strp)
461 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
462 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
468 int cros_ec_read_current_image(struct udevice *dev,
469 enum ec_current_image *image)
471 struct ec_response_get_version *r;
473 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
474 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
477 *image = r->current_image;
481 static int cros_ec_wait_on_hash_done(struct udevice *dev,
482 struct ec_response_vboot_hash *hash)
484 struct ec_params_vboot_hash p;
487 start = get_timer(0);
488 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
489 mdelay(50); /* Insert some reasonable delay */
491 p.cmd = EC_VBOOT_HASH_GET;
492 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
493 hash, sizeof(*hash)) < 0)
496 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
497 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
498 return -EC_RES_TIMEOUT;
504 int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
505 struct ec_response_vboot_hash *hash)
507 struct ec_params_vboot_hash p;
510 p.cmd = EC_VBOOT_HASH_GET;
511 p.offset = hash_offset;
512 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
513 hash, sizeof(*hash)) < 0)
516 /* If the EC is busy calculating the hash, fidget until it's done. */
517 rv = cros_ec_wait_on_hash_done(dev, hash);
521 /* If the hash is valid, we're done. Otherwise, we have to kick it off
522 * again and wait for it to complete. Note that we explicitly assume
523 * that hashing zero bytes is always wrong, even though that would
524 * produce a valid hash value. */
525 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
528 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
529 __func__, hash->status, hash->size);
531 p.cmd = EC_VBOOT_HASH_START;
532 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
534 p.offset = hash_offset;
536 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
537 hash, sizeof(*hash)) < 0)
540 rv = cros_ec_wait_on_hash_done(dev, hash);
544 debug("%s: hash done\n", __func__);
549 static int cros_ec_invalidate_hash(struct udevice *dev)
551 struct ec_params_vboot_hash p;
552 struct ec_response_vboot_hash *hash;
554 /* We don't have an explict command for the EC to discard its current
555 * hash value, so we'll just tell it to calculate one that we know is
556 * wrong (we claim that hashing zero bytes is always invalid).
558 p.cmd = EC_VBOOT_HASH_RECALC;
559 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
564 debug("%s:\n", __func__);
566 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
567 (uint8_t **)&hash, sizeof(*hash)) < 0)
570 /* No need to wait for it to finish */
574 int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
576 struct ec_params_reboot_ec p;
581 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
585 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
587 * EC reboot will take place immediately so delay to allow it
588 * to complete. Note that some reboot types (EC_REBOOT_COLD)
589 * will reboot the AP as well, in which case we won't actually
593 * TODO(rspangler@chromium.org): Would be nice if we had a
594 * better way to determine when the reboot is complete. Could
595 * we poll a memory-mapped LPC value?
603 int cros_ec_interrupt_pending(struct udevice *dev)
605 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
607 /* no interrupt support : always poll */
608 if (!dm_gpio_is_valid(&cdev->ec_int))
611 return dm_gpio_get_value(&cdev->ec_int);
614 int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
616 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
617 sizeof(*info)) != sizeof(*info))
623 int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
625 struct ec_response_host_event_mask rsp;
628 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
631 else if (ret != sizeof(rsp))
639 int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
641 struct ec_params_host_event_mask req;
646 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
653 int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
655 struct ec_response_host_event_mask *resp;
658 * Use the B copy of the event flags, because the main copy is already
661 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
662 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
665 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
668 *events_ptr = resp->mask;
672 int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
674 struct ec_params_host_event_mask params;
676 params.mask = events;
679 * Use the B copy of the event flags, so it affects the data returned
680 * by cros_ec_get_host_events().
682 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
683 ¶ms, sizeof(params), NULL, 0) < 0)
689 int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
691 struct ec_response_flash_protect *resp)
693 struct ec_params_flash_protect params;
695 params.mask = set_mask;
696 params.flags = set_flags;
698 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
699 ¶ms, sizeof(params),
700 resp, sizeof(*resp)) != sizeof(*resp))
706 int cros_ec_entering_mode(struct udevice *dev, int mode)
710 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
717 static int cros_ec_check_version(struct udevice *dev)
719 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
720 struct ec_params_hello req;
721 struct ec_response_hello *resp;
723 struct dm_cros_ec_ops *ops;
726 ops = dm_cros_ec_get_ops(dev);
727 if (ops->check_version) {
728 ret = ops->check_version(dev);
734 * TODO(sjg@chromium.org).
735 * There is a strange oddity here with the EC. We could just ignore
736 * the response, i.e. pass the last two parameters as NULL and 0.
737 * In this case we won't read back very many bytes from the EC.
738 * On the I2C bus the EC gets upset about this and will try to send
739 * the bytes anyway. This means that we will have to wait for that
740 * to complete before continuing with a new EC command.
742 * This problem is probably unique to the I2C bus.
744 * So for now, just read all the data anyway.
747 /* Try sending a version 3 packet */
748 cdev->protocol_version = 3;
750 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
751 (uint8_t **)&resp, sizeof(*resp)) > 0)
754 /* Try sending a version 2 packet */
755 cdev->protocol_version = 2;
756 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
757 (uint8_t **)&resp, sizeof(*resp)) > 0)
761 * Fail if we're still here, since the EC doesn't understand any
762 * protcol version we speak. Version 1 interface without command
763 * version is no longer supported, and we don't know about any new
766 cdev->protocol_version = 0;
767 printf("%s: ERROR: old EC interface not supported\n", __func__);
771 int cros_ec_test(struct udevice *dev)
773 struct ec_params_hello req;
774 struct ec_response_hello *resp;
776 req.in_data = 0x12345678;
777 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
778 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
779 printf("ec_command_inptr() returned error\n");
782 if (resp->out_data != req.in_data + 0x01020304) {
783 printf("Received invalid handshake %x\n", resp->out_data);
790 int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
791 uint32_t *offset, uint32_t *size)
793 struct ec_params_flash_region_info p;
794 struct ec_response_flash_region_info *r;
798 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
799 EC_VER_FLASH_REGION_INFO,
800 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
801 if (ret != sizeof(*r))
812 int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
814 struct ec_params_flash_erase p;
818 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
823 * Write a single block to the flash
825 * Write a block of data to the EC flash. The size must not exceed the flash
826 * write block size which you can obtain from cros_ec_flash_write_burst_size().
828 * The offset starts at 0. You can obtain the region information from
829 * cros_ec_flash_offset() to find out where to write for a particular region.
831 * Attempting to write to the region where the EC is currently running from
832 * will result in an error.
834 * @param dev CROS-EC device
835 * @param data Pointer to data buffer to write
836 * @param offset Offset within flash to write to.
837 * @param size Number of bytes to write
838 * @return 0 if ok, -1 on error
840 static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
841 uint32_t offset, uint32_t size)
843 struct ec_params_flash_write *p;
846 p = malloc(sizeof(*p) + size);
852 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
853 memcpy(p + 1, data, p->size);
855 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
856 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
864 * Return optimal flash write burst size
866 static int cros_ec_flash_write_burst_size(struct udevice *dev)
868 return EC_FLASH_WRITE_VER0_SIZE;
872 * Check if a block of data is erased (all 0xff)
874 * This function is useful when dealing with flash, for checking whether a
875 * data block is erased and thus does not need to be programmed.
877 * @param data Pointer to data to check (must be word-aligned)
878 * @param size Number of bytes to check (must be word-aligned)
879 * @return 0 if erased, non-zero if any word is not erased
881 static int cros_ec_data_is_erased(const uint32_t *data, int size)
884 size /= sizeof(uint32_t);
885 for (; size > 0; size -= 4, data++)
893 * Read back flash parameters
895 * This function reads back parameters of the flash as reported by the EC
897 * @param dev Pointer to device
898 * @param info Pointer to output flash info struct
900 int cros_ec_read_flashinfo(struct udevice *dev,
901 struct ec_response_flash_info *info)
905 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
906 NULL, 0, info, sizeof(*info));
910 return ret < sizeof(*info) ? -1 : 0;
913 int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
914 uint32_t offset, uint32_t size)
916 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
917 uint32_t burst = cros_ec_flash_write_burst_size(dev);
925 * TODO: round up to the nearest multiple of write size. Can get away
926 * without that on link right now because its write size is 4 bytes.
929 for (off = offset; off < end; off += burst, data += burst) {
932 /* If the data is empty, there is no point in programming it */
933 todo = min(end - off, burst);
934 if (cdev->optimise_flash_write &&
935 cros_ec_data_is_erased((uint32_t *)data, todo))
938 ret = cros_ec_flash_write_block(dev, data, off, todo);
947 * Run verification on a slot
949 * @param me CrosEc instance
950 * @param region Region to run verification on
951 * @return 0 if success or not applicable. Non-zero if verification failed.
953 int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
955 struct ec_params_efs_verify p;
958 log_info("EFS: EC is verifying updated image...\n");
961 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
963 log_info("EFS: Verification success\n");
966 if (rv == -EC_RES_INVALID_COMMAND) {
967 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
970 log_info("EFS: Verification failed\n");
976 * Read a single block from the flash
978 * Read a block of data from the EC flash. The size must not exceed the flash
979 * write block size which you can obtain from cros_ec_flash_write_burst_size().
981 * The offset starts at 0. You can obtain the region information from
982 * cros_ec_flash_offset() to find out where to read for a particular region.
984 * @param dev CROS-EC device
985 * @param data Pointer to data buffer to read into
986 * @param offset Offset within flash to read from
987 * @param size Number of bytes to read
988 * @return 0 if ok, -1 on error
990 static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
991 uint32_t offset, uint32_t size)
993 struct ec_params_flash_read p;
998 return ec_command(dev, EC_CMD_FLASH_READ, 0,
999 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1002 int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
1005 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1009 end = offset + size;
1010 for (off = offset; off < end; off += burst, data += burst) {
1011 ret = cros_ec_flash_read_block(dev, data, off,
1012 min(end - off, burst));
1020 int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
1023 uint32_t rw_offset, rw_size;
1026 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1029 if (image_size > (int)rw_size)
1032 /* Invalidate the existing hash, just in case the AP reboots
1033 * unexpectedly during the update. If that happened, the EC RW firmware
1034 * would be invalid, but the EC would still have the original hash.
1036 ret = cros_ec_invalidate_hash(dev);
1041 * Erase the entire RW section, so that the EC doesn't see any garbage
1042 * past the new image if it's smaller than the current image.
1044 * TODO: could optimize this to erase just the current image, since
1045 * presumably everything past that is 0xff's. But would still need to
1046 * round up to the nearest multiple of erase size.
1048 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1052 /* Write the image */
1053 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1060 int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
1062 struct ec_params_vbnvcontext p;
1065 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1068 p.op = EC_VBNV_CONTEXT_OP_READ;
1070 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1071 &p, sizeof(uint32_t) + size, block, size);
1073 log_err("Expected %d bytes, got %d\n", size, len);
1080 int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
1082 struct ec_params_vbnvcontext p;
1085 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1087 p.op = EC_VBNV_CONTEXT_OP_WRITE;
1088 memcpy(p.block, block, size);
1090 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1091 &p, sizeof(uint32_t) + size, NULL, 0);
1098 int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1100 struct ec_params_battery_cutoff p;
1104 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1112 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
1114 struct ec_params_ldo_set params;
1116 params.index = index;
1117 params.state = state;
1119 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
1126 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
1128 struct ec_params_ldo_get params;
1129 struct ec_response_ldo_get *resp;
1131 params.index = index;
1133 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
1134 (uint8_t **)&resp, sizeof(*resp)) !=
1138 *state = resp->state;
1143 int cros_ec_register(struct udevice *dev)
1145 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1149 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1151 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1153 if (cros_ec_check_version(dev)) {
1154 debug("%s: Could not detect CROS-EC version\n", __func__);
1155 return -CROS_EC_ERR_CHECK_VERSION;
1158 if (cros_ec_read_id(dev, id, sizeof(id))) {
1159 debug("%s: Could not read KBC ID\n", __func__);
1160 return -CROS_EC_ERR_READ_ID;
1163 /* Remember this device for use by the cros_ec command */
1164 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1165 cdev->protocol_version, id);
1170 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1172 ofnode flash_node, node;
1174 flash_node = dev_read_subnode(dev, "flash");
1175 if (!ofnode_valid(flash_node)) {
1176 debug("Failed to find flash node\n");
1180 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
1181 debug("Failed to decode flash node in chrome-ec\n");
1185 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1187 ofnode_for_each_subnode(node, flash_node) {
1188 const char *name = ofnode_get_name(node);
1189 enum ec_flash_region region;
1191 if (0 == strcmp(name, "ro")) {
1192 region = EC_FLASH_REGION_RO;
1193 } else if (0 == strcmp(name, "rw")) {
1194 region = EC_FLASH_REGION_ACTIVE;
1195 } else if (0 == strcmp(name, "wp-ro")) {
1196 region = EC_FLASH_REGION_WP_RO;
1198 debug("Unknown EC flash region name '%s'\n", name);
1202 if (ofnode_read_fmap_entry(node, &config->region[region])) {
1203 debug("Failed to decode flash region in chrome-ec'\n");
1211 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1215 struct ec_params_i2c_passthru p;
1216 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1219 struct ec_response_i2c_passthru r;
1220 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1222 struct ec_params_i2c_passthru *p = ¶ms.p;
1223 struct ec_response_i2c_passthru *r = &response.r;
1224 struct ec_params_i2c_passthru_msg *msg;
1225 uint8_t *pdata, *read_ptr = NULL;
1233 p->num_msgs = nmsgs;
1234 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1236 /* Create a message to write the register address and optional data */
1237 pdata = (uint8_t *)p + size;
1240 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1241 bool is_read = in->flags & I2C_M_RD;
1243 msg->addr_flags = in->addr;
1246 msg->addr_flags |= EC_I2C_FLAG_READ;
1247 read_len += in->len;
1249 if (sizeof(*r) + read_len > sizeof(response)) {
1250 puts("Read length too big for buffer\n");
1254 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1255 puts("Params too large for buffer\n");
1258 memcpy(pdata, in->buf, in->len);
1263 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1264 r, sizeof(*r) + read_len);
1268 /* Parse response */
1269 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1270 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1274 if (rv < sizeof(*r) + read_len) {
1275 puts("Truncated read response\n");
1279 /* We only support a single read message for each transfer */
1281 memcpy(read_ptr, r->data, read_len);
1286 int cros_ec_check_feature(struct udevice *dev, int feature)
1288 struct ec_response_get_features r;
1291 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1295 if (feature >= 8 * sizeof(r.flags))
1298 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1302 * Query the EC for specified mask indicating enabled events.
1303 * The EC maintains separate event masks for SMI, SCI and WAKE.
1305 static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1309 struct ec_params_host_event req;
1310 struct ec_response_host_event rsp;
1312 req.action = action;
1313 req.mask_type = mask;
1314 if (action != EC_HOST_EVENT_GET)
1318 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1321 if (action != EC_HOST_EVENT_GET)
1329 static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1330 uint action, uint64_t *value)
1333 struct ec_params_host_event_mask req;
1334 struct ec_response_host_event_mask rsp;
1336 if (hcmd == INVALID_HCMD)
1339 if (action != EC_HOST_EVENT_GET)
1340 req.mask = (uint32_t)*value;
1344 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1345 if (action != EC_HOST_EVENT_GET)
1353 bool cros_ec_is_uhepi_supported(struct udevice *dev)
1355 #define UHEPI_SUPPORTED 1
1356 #define UHEPI_NOT_SUPPORTED 2
1357 static int uhepi_support;
1359 if (!uhepi_support) {
1360 uhepi_support = cros_ec_check_feature(dev,
1361 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1362 UHEPI_NOT_SUPPORTED;
1363 log_debug("Chrome EC: UHEPI %s\n",
1364 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1367 return uhepi_support == UHEPI_SUPPORTED;
1370 static int cros_ec_get_mask(struct udevice *dev, uint type)
1374 if (cros_ec_is_uhepi_supported(dev)) {
1375 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1377 assert(type < ARRAY_SIZE(event_map));
1378 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1379 EC_HOST_EVENT_GET, &value);
1384 static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1386 if (cros_ec_is_uhepi_supported(dev))
1387 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1389 assert(type < ARRAY_SIZE(event_map));
1391 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1392 EC_HOST_EVENT_CLEAR, &mask);
1395 uint64_t cros_ec_get_events_b(struct udevice *dev)
1397 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1400 int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1402 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1404 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1407 int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1409 struct ec_params_charge_state p;
1410 struct ec_response_charge_state r;
1413 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1414 p.get_param.param = CS_PARAM_LIMIT_POWER;
1415 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1419 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1420 * LIMIT_POWER is not requested.
1422 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1423 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1427 if (ret != sizeof(r.get_param))
1430 *limit_powerp = r.get_param.value;
1434 int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1436 struct ec_params_config_power_button params;
1439 params.flags = flags;
1440 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1441 ¶ms, sizeof(params), NULL, 0);
1448 int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1453 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1458 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1461 int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1466 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1471 /* Set lid close event state in the EC SMI event mask */
1473 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1475 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1477 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1481 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1485 UCLASS_DRIVER(cros_ec) = {
1486 .id = UCLASS_CROS_EC,
1488 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1489 .post_bind = dm_scan_fdt_dev,
1490 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,