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/delay.h>
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,
47 /* Wait 10 ms between attempts to check if EC's hash is ready */
48 CROS_EC_HASH_CHECK_DELAY_MS = 10,
52 #define INVALID_HCMD 0xFF
55 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
56 * which does not support UHEPI command.
63 [EC_HOST_EVENT_MAIN] = {
64 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
68 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
69 EC_CMD_HOST_EVENT_GET_B,
71 [EC_HOST_EVENT_SCI_MASK] = {
72 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
73 EC_CMD_HOST_EVENT_GET_SCI_MASK,
75 [EC_HOST_EVENT_SMI_MASK] = {
76 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
77 EC_CMD_HOST_EVENT_GET_SMI_MASK,
79 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
80 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
82 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
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_S0IX] = {
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_S3] = {
91 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
92 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
94 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
95 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
96 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
100 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
105 printf("%s: ", name);
107 printf("cmd=%#x: ", cmd);
108 for (i = 0; i < len; i++)
109 printf("%02x ", data[i]);
115 * Calculate a simple 8-bit checksum of a data block
117 * @param data Data block to checksum
118 * @param size Size of data block in bytes
119 * Return: checksum value (0 to 255)
121 int cros_ec_calc_checksum(const uint8_t *data, int size)
125 for (i = csum = 0; i < size; i++)
131 * Create a request packet for protocol version 3.
133 * The packet is stored in the device's internal output buffer.
135 * @param dev CROS-EC device
136 * @param cmd Command to send (EC_CMD_...)
137 * @param cmd_version Version of command to send (EC_VER_...)
138 * @param dout Output data (may be NULL If dout_len=0)
139 * @param dout_len Size of output data in bytes
140 * Return: packet size in bytes, or <0 if error.
142 static int create_proto3_request(struct cros_ec_dev *cdev,
143 int cmd, int cmd_version,
144 const void *dout, int dout_len)
146 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
147 int out_bytes = dout_len + sizeof(*rq);
149 /* Fail if output size is too big */
150 if (out_bytes > (int)sizeof(cdev->dout)) {
151 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
152 return -EC_RES_REQUEST_TRUNCATED;
155 /* Fill in request packet */
156 rq->struct_version = EC_HOST_REQUEST_VERSION;
159 rq->command_version = cmd_version;
161 rq->data_len = dout_len;
163 /* Copy data after header */
164 memcpy(rq + 1, dout, dout_len);
166 /* Write checksum field so the entire packet sums to 0 */
167 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
169 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
171 /* Return size of request packet */
176 * Prepare the device to receive a protocol version 3 response.
178 * @param dev CROS-EC device
179 * @param din_len Maximum size of response in bytes
180 * Return: maximum expected number of bytes in response, or <0 if error.
182 static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
184 int in_bytes = din_len + sizeof(struct ec_host_response);
186 /* Fail if input size is too big */
187 if (in_bytes > (int)sizeof(cdev->din)) {
188 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
189 return -EC_RES_RESPONSE_TOO_BIG;
192 /* Return expected size of response packet */
197 * Handle a protocol version 3 response packet.
199 * The packet must already be stored in the device's internal input buffer.
201 * @param dev CROS-EC device
202 * @param dinp Returns pointer to response data
203 * @param din_len Maximum size of response in bytes
204 * Return: number of bytes of response data, or <0 if error. Note that error
205 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
208 static int handle_proto3_response(struct cros_ec_dev *dev,
209 uint8_t **dinp, int din_len)
211 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
215 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
217 /* Check input data */
218 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
219 debug("%s: EC response version mismatch\n", __func__);
220 return -EC_RES_INVALID_RESPONSE;
224 debug("%s: EC response reserved != 0\n", __func__);
225 return -EC_RES_INVALID_RESPONSE;
228 if (rs->data_len > din_len) {
229 debug("%s: EC returned too much data\n", __func__);
230 return -EC_RES_RESPONSE_TOO_BIG;
233 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
235 /* Update in_bytes to actual data size */
236 in_bytes = sizeof(*rs) + rs->data_len;
238 /* Verify checksum */
239 csum = cros_ec_calc_checksum(dev->din, in_bytes);
241 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
243 return -EC_RES_INVALID_CHECKSUM;
246 /* Return error result, if any */
248 return -(int)rs->result;
250 /* If we're still here, set response data pointer and return length */
251 *dinp = (uint8_t *)(rs + 1);
256 static int send_command_proto3(struct cros_ec_dev *cdev,
257 int cmd, int cmd_version,
258 const void *dout, int dout_len,
259 uint8_t **dinp, int din_len)
261 struct dm_cros_ec_ops *ops;
262 int out_bytes, in_bytes;
265 /* Create request packet */
266 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
271 /* Prepare response buffer */
272 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
276 ops = dm_cros_ec_get_ops(cdev->dev);
277 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
282 /* Process the response */
283 return handle_proto3_response(cdev, dinp, din_len);
286 static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
287 const void *dout, int dout_len,
288 uint8_t **dinp, int din_len)
290 struct dm_cros_ec_ops *ops;
293 /* Handle protocol version 3 support */
294 if (dev->protocol_version == 3) {
295 return send_command_proto3(dev, cmd, cmd_version,
296 dout, dout_len, dinp, din_len);
299 ops = dm_cros_ec_get_ops(dev->dev);
300 ret = ops->command(dev->dev, cmd, cmd_version,
301 (const uint8_t *)dout, dout_len, dinp, din_len);
307 * Send a command to the CROS-EC device and return the reply.
309 * The device's internal input/output buffers are used.
311 * @param dev CROS-EC device
312 * @param cmd Command to send (EC_CMD_...)
313 * @param cmd_version Version of command to send (EC_VER_...)
314 * @param dout Output data (may be NULL If dout_len=0)
315 * @param dout_len Size of output data in bytes
316 * @param dinp Response data (may be NULL If din_len=0).
317 * If not NULL, it will be updated to point to the data
318 * and will always be double word aligned (64-bits)
319 * @param din_len Maximum size of response in bytes
320 * Return: number of bytes in response, or -ve on error
322 static int ec_command_inptr(struct udevice *dev, uint cmd,
323 int cmd_version, const void *dout, int dout_len,
324 uint8_t **dinp, int din_len)
326 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
330 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
333 /* If the command doesn't complete, wait a while */
334 if (len == -EC_RES_IN_PROGRESS) {
335 struct ec_response_get_comms_status *resp = NULL;
338 /* Wait for command to complete */
339 start = get_timer(0);
343 mdelay(50); /* Insert some reasonable delay */
344 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
346 (uint8_t **)&resp, sizeof(*resp));
350 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
351 debug("%s: Command %#02x timeout\n",
353 return -EC_RES_TIMEOUT;
355 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
357 /* OK it completed, so read the status response */
358 /* not sure why it was 0 for the last argument */
359 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
363 debug("%s: len=%d, din=%p\n", __func__, len, din);
365 /* If we have any data to return, it must be 64bit-aligned */
366 assert(len <= 0 || !((uintptr_t)din & 7));
374 * Send a command to the CROS-EC device and return the reply.
376 * The device's internal input/output buffers are used.
378 * @param dev CROS-EC device
379 * @param cmd Command to send (EC_CMD_...)
380 * @param cmd_version Version of command to send (EC_VER_...)
381 * @param dout Output data (may be NULL If dout_len=0)
382 * @param dout_len Size of output data in bytes
383 * @param din Response data (may be NULL If din_len=0).
384 * It not NULL, it is a place for ec_command() to copy the
386 * @param din_len Maximum size of response in bytes
387 * Return: number of bytes in response, or -ve on error
389 static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
390 const void *dout, int dout_len,
391 void *din, int din_len)
396 assert((din_len == 0) || din);
397 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
398 &in_buffer, din_len);
401 * If we were asked to put it somewhere, do so, otherwise just
402 * disregard the result.
404 if (din && in_buffer) {
405 assert(len <= din_len);
408 memmove(din, in_buffer, len);
414 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
416 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
417 sizeof(scan->data)) != sizeof(scan->data))
423 int cros_ec_get_next_event(struct udevice *dev,
424 struct ec_response_get_next_event *event)
428 ret = ec_command(dev, EC_CMD_GET_NEXT_EVENT, 0, NULL, 0,
429 event, sizeof(*event));
432 else if (ret != sizeof(*event))
433 return -EC_RES_INVALID_RESPONSE;
438 int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
440 struct ec_response_get_version *r;
443 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
444 (uint8_t **)&r, sizeof(*r));
445 if (ret != sizeof(*r)) {
446 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
450 if (maxlen > (int)sizeof(r->version_string_ro))
451 maxlen = sizeof(r->version_string_ro);
453 switch (r->current_image) {
455 memcpy(id, r->version_string_ro, maxlen);
458 memcpy(id, r->version_string_rw, maxlen);
461 log_err("Invalid EC image %d\n", r->current_image);
465 id[maxlen - 1] = '\0';
469 int cros_ec_read_version(struct udevice *dev,
470 struct ec_response_get_version **versionp)
472 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
473 (uint8_t **)versionp, sizeof(**versionp))
474 != sizeof(**versionp))
480 int cros_ec_read_build_info(struct udevice *dev, char **strp)
482 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
483 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
489 int cros_ec_read_current_image(struct udevice *dev,
490 enum ec_current_image *image)
492 struct ec_response_get_version *r;
494 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
495 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
498 *image = r->current_image;
502 static int cros_ec_wait_on_hash_done(struct udevice *dev,
503 struct ec_params_vboot_hash *p,
504 struct ec_response_vboot_hash *hash)
508 start = get_timer(0);
509 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
510 mdelay(CROS_EC_HASH_CHECK_DELAY_MS);
512 p->cmd = EC_VBOOT_HASH_GET;
514 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, p, sizeof(*p), hash,
518 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
519 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
520 return -EC_RES_TIMEOUT;
526 int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
527 struct ec_response_vboot_hash *hash)
529 struct ec_params_vboot_hash p;
532 p.cmd = EC_VBOOT_HASH_GET;
533 p.offset = hash_offset;
534 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
535 hash, sizeof(*hash)) < 0)
538 /* If the EC is busy calculating the hash, fidget until it's done. */
539 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
543 /* If the hash is valid, we're done. Otherwise, we have to kick it off
544 * again and wait for it to complete. Note that we explicitly assume
545 * that hashing zero bytes is always wrong, even though that would
546 * produce a valid hash value. */
547 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
550 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
551 __func__, hash->status, hash->size);
553 p.cmd = EC_VBOOT_HASH_START;
554 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
556 p.offset = hash_offset;
558 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
559 hash, sizeof(*hash)) < 0)
562 rv = cros_ec_wait_on_hash_done(dev, &p, hash);
565 if (hash->status != EC_VBOOT_HASH_STATUS_DONE) {
566 log_err("Hash did not complete, status=%d\n", hash->status);
570 debug("%s: hash done\n", __func__);
575 static int cros_ec_invalidate_hash(struct udevice *dev)
577 struct ec_params_vboot_hash p;
578 struct ec_response_vboot_hash *hash;
580 /* We don't have an explict command for the EC to discard its current
581 * hash value, so we'll just tell it to calculate one that we know is
582 * wrong (we claim that hashing zero bytes is always invalid).
584 p.cmd = EC_VBOOT_HASH_RECALC;
585 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
590 debug("%s:\n", __func__);
592 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
593 (uint8_t **)&hash, sizeof(*hash)) < 0)
596 /* No need to wait for it to finish */
600 int cros_ec_hello(struct udevice *dev, uint *handshakep)
602 struct ec_params_hello req;
603 struct ec_response_hello *resp;
605 req.in_data = 0x12345678;
606 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
607 (uint8_t **)&resp, sizeof(*resp)) < 0)
609 if (resp->out_data != req.in_data + 0x01020304) {
610 printf("Received invalid handshake %x\n", resp->out_data);
612 *handshakep = req.in_data;
619 int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
621 struct ec_params_reboot_ec p;
626 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
630 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
634 * EC reboot will take place immediately so delay to allow it
635 * to complete. Note that some reboot types (EC_REBOOT_COLD)
636 * will reboot the AP as well, in which case we won't actually
640 start = get_timer(0);
641 while (cros_ec_hello(dev, NULL)) {
642 if (get_timer(start) > 3000) {
643 log_err("EC did not return from reboot\n");
653 int cros_ec_interrupt_pending(struct udevice *dev)
655 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
657 /* no interrupt support : always poll */
658 if (!dm_gpio_is_valid(&cdev->ec_int))
661 return dm_gpio_get_value(&cdev->ec_int);
664 int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
666 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
667 sizeof(*info)) != sizeof(*info))
673 int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
675 struct ec_response_host_event_mask rsp;
678 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
681 else if (ret != sizeof(rsp))
689 int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
691 struct ec_params_host_event_mask req;
696 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
703 int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
705 struct ec_response_host_event_mask *resp;
708 * Use the B copy of the event flags, because the main copy is already
711 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
712 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
715 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
718 *events_ptr = resp->mask;
722 int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
724 struct ec_params_host_event_mask params;
726 params.mask = events;
729 * Use the B copy of the event flags, so it affects the data returned
730 * by cros_ec_get_host_events().
732 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
733 ¶ms, sizeof(params), NULL, 0) < 0)
739 int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
741 struct ec_response_flash_protect *resp)
743 struct ec_params_flash_protect params;
745 params.mask = set_mask;
746 params.flags = set_flags;
748 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
749 ¶ms, sizeof(params),
750 resp, sizeof(*resp)) != sizeof(*resp))
756 static int cros_ec_check_version(struct udevice *dev)
758 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
759 struct ec_params_hello req;
761 struct dm_cros_ec_ops *ops;
764 ops = dm_cros_ec_get_ops(dev);
765 if (ops->check_version) {
766 ret = ops->check_version(dev);
773 * There is a strange oddity here with the EC. We could just ignore
774 * the response, i.e. pass the last two parameters as NULL and 0.
775 * In this case we won't read back very many bytes from the EC.
776 * On the I2C bus the EC gets upset about this and will try to send
777 * the bytes anyway. This means that we will have to wait for that
778 * to complete before continuing with a new EC command.
780 * This problem is probably unique to the I2C bus.
782 * So for now, just read all the data anyway.
785 /* Try sending a version 3 packet */
786 cdev->protocol_version = 3;
788 ret = cros_ec_hello(dev, NULL);
789 if (!ret || ret == -ENOTSYNC)
792 /* Try sending a version 2 packet */
793 cdev->protocol_version = 2;
794 ret = cros_ec_hello(dev, NULL);
795 if (!ret || ret == -ENOTSYNC)
799 * Fail if we're still here, since the EC doesn't understand any
800 * protcol version we speak. Version 1 interface without command
801 * version is no longer supported, and we don't know about any new
804 cdev->protocol_version = 0;
805 printf("%s: ERROR: old EC interface not supported\n", __func__);
809 int cros_ec_test(struct udevice *dev)
814 ret = cros_ec_hello(dev, &out_data);
815 if (ret == -ENOTSYNC) {
816 printf("Received invalid handshake %x\n", out_data);
819 printf("ec_command_inptr() returned error\n");
826 int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
827 uint32_t *offset, uint32_t *size)
829 struct ec_params_flash_region_info p;
830 struct ec_response_flash_region_info *r;
834 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
835 EC_VER_FLASH_REGION_INFO,
836 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
837 if (ret != sizeof(*r))
848 int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
850 struct ec_params_flash_erase p;
854 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
859 * Write a single block to the flash
861 * Write a block of data to the EC flash. The size must not exceed the flash
862 * write block size which you can obtain from cros_ec_flash_write_burst_size().
864 * The offset starts at 0. You can obtain the region information from
865 * cros_ec_flash_offset() to find out where to write for a particular region.
867 * Attempting to write to the region where the EC is currently running from
868 * will result in an error.
870 * @param dev CROS-EC device
871 * @param data Pointer to data buffer to write
872 * @param offset Offset within flash to write to.
873 * @param size Number of bytes to write
874 * Return: 0 if ok, -1 on error
876 static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
877 uint32_t offset, uint32_t size)
879 struct ec_params_flash_write *p;
882 p = malloc(sizeof(*p) + size);
888 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
889 memcpy(p + 1, data, p->size);
891 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
892 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
900 * Return optimal flash write burst size
902 static int cros_ec_flash_write_burst_size(struct udevice *dev)
904 return EC_FLASH_WRITE_VER0_SIZE;
908 * Check if a block of data is erased (all 0xff)
910 * This function is useful when dealing with flash, for checking whether a
911 * data block is erased and thus does not need to be programmed.
913 * @param data Pointer to data to check (must be word-aligned)
914 * @param size Number of bytes to check (must be word-aligned)
915 * Return: 0 if erased, non-zero if any word is not erased
917 static int cros_ec_data_is_erased(const uint32_t *data, int size)
920 size /= sizeof(uint32_t);
921 for (; size > 0; size -= 4, data++)
929 * Read back flash parameters
931 * This function reads back parameters of the flash as reported by the EC
933 * @param dev Pointer to device
934 * @param info Pointer to output flash info struct
936 int cros_ec_read_flashinfo(struct udevice *dev,
937 struct ec_response_flash_info *info)
941 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
942 NULL, 0, info, sizeof(*info));
946 return ret < sizeof(*info) ? -1 : 0;
949 int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
950 uint32_t offset, uint32_t size)
952 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
953 uint32_t burst = cros_ec_flash_write_burst_size(dev);
961 * TODO: round up to the nearest multiple of write size. Can get away
962 * without that on link right now because its write size is 4 bytes.
965 for (off = offset; off < end; off += burst, data += burst) {
968 /* If the data is empty, there is no point in programming it */
969 todo = min(end - off, burst);
970 if (cdev->optimise_flash_write &&
971 cros_ec_data_is_erased((uint32_t *)data, todo))
974 ret = cros_ec_flash_write_block(dev, data, off, todo);
983 * Run verification on a slot
985 * @param me CrosEc instance
986 * @param region Region to run verification on
987 * Return: 0 if success or not applicable. Non-zero if verification failed.
989 int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
991 struct ec_params_efs_verify p;
994 log_info("EFS: EC is verifying updated image...\n");
997 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
999 log_info("EFS: Verification success\n");
1002 if (rv == -EC_RES_INVALID_COMMAND) {
1003 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
1006 log_info("EFS: Verification failed\n");
1012 * Read a single block from the flash
1014 * Read a block of data from the EC flash. The size must not exceed the flash
1015 * write block size which you can obtain from cros_ec_flash_write_burst_size().
1017 * The offset starts at 0. You can obtain the region information from
1018 * cros_ec_flash_offset() to find out where to read for a particular region.
1020 * @param dev CROS-EC device
1021 * @param data Pointer to data buffer to read into
1022 * @param offset Offset within flash to read from
1023 * @param size Number of bytes to read
1024 * Return: 0 if ok, -1 on error
1026 static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
1027 uint32_t offset, uint32_t size)
1029 struct ec_params_flash_read p;
1034 return ec_command(dev, EC_CMD_FLASH_READ, 0,
1035 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
1038 int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
1041 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1045 end = offset + size;
1046 for (off = offset; off < end; off += burst, data += burst) {
1047 ret = cros_ec_flash_read_block(dev, data, off,
1048 min(end - off, burst));
1056 int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
1059 uint32_t rw_offset, rw_size;
1062 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1065 if (image_size > (int)rw_size)
1068 /* Invalidate the existing hash, just in case the AP reboots
1069 * unexpectedly during the update. If that happened, the EC RW firmware
1070 * would be invalid, but the EC would still have the original hash.
1072 ret = cros_ec_invalidate_hash(dev);
1077 * Erase the entire RW section, so that the EC doesn't see any garbage
1078 * past the new image if it's smaller than the current image.
1080 * TODO: could optimize this to erase just the current image, since
1081 * presumably everything past that is 0xff's. But would still need to
1082 * round up to the nearest multiple of erase size.
1084 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1088 /* Write the image */
1089 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1096 int cros_ec_get_sku_id(struct udevice *dev)
1098 struct ec_sku_id_info *r;
1101 ret = ec_command_inptr(dev, EC_CMD_GET_SKU_ID, 0, NULL, 0,
1102 (uint8_t **)&r, sizeof(*r));
1103 if (ret != sizeof(*r)) {
1112 int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
1114 struct ec_params_vbnvcontext p;
1117 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1120 p.op = EC_VBNV_CONTEXT_OP_READ;
1122 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1123 &p, sizeof(uint32_t) + size, block, size);
1125 log_err("Expected %d bytes, got %d\n", size, len);
1132 int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
1134 struct ec_params_vbnvcontext p;
1137 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1139 p.op = EC_VBNV_CONTEXT_OP_WRITE;
1140 memcpy(p.block, block, size);
1142 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1143 &p, sizeof(uint32_t) + size, NULL, 0);
1150 int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1152 struct ec_params_battery_cutoff p;
1156 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1164 int cros_ec_set_pwm_duty(struct udevice *dev, uint8_t index, uint16_t duty)
1166 struct ec_params_pwm_set_duty p;
1170 p.pwm_type = EC_PWM_TYPE_GENERIC;
1173 ret = ec_command(dev, EC_CMD_PWM_SET_DUTY, 0, &p, sizeof(p),
1181 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
1183 struct ec_params_ldo_set params;
1185 params.index = index;
1186 params.state = state;
1188 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
1195 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
1197 struct ec_params_ldo_get params;
1198 struct ec_response_ldo_get *resp;
1200 params.index = index;
1202 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
1203 (uint8_t **)&resp, sizeof(*resp)) !=
1207 *state = resp->state;
1212 int cros_ec_register(struct udevice *dev)
1214 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1218 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1220 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1222 if (cros_ec_check_version(dev)) {
1223 debug("%s: Could not detect CROS-EC version\n", __func__);
1224 return -CROS_EC_ERR_CHECK_VERSION;
1227 if (cros_ec_read_id(dev, id, sizeof(id))) {
1228 debug("%s: Could not read KBC ID\n", __func__);
1229 return -CROS_EC_ERR_READ_ID;
1232 /* Remember this device for use by the cros_ec command */
1233 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1234 cdev->protocol_version, id);
1239 int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1241 ofnode flash_node, node;
1243 flash_node = dev_read_subnode(dev, "flash");
1244 if (!ofnode_valid(flash_node)) {
1245 debug("Failed to find flash node\n");
1249 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
1250 debug("Failed to decode flash node in chrome-ec\n");
1254 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1256 ofnode_for_each_subnode(node, flash_node) {
1257 const char *name = ofnode_get_name(node);
1258 enum ec_flash_region region;
1260 if (0 == strcmp(name, "ro")) {
1261 region = EC_FLASH_REGION_RO;
1262 } else if (0 == strcmp(name, "rw")) {
1263 region = EC_FLASH_REGION_ACTIVE;
1264 } else if (0 == strcmp(name, "wp-ro")) {
1265 region = EC_FLASH_REGION_WP_RO;
1267 debug("Unknown EC flash region name '%s'\n", name);
1271 if (ofnode_read_fmap_entry(node, &config->region[region])) {
1272 debug("Failed to decode flash region in chrome-ec'\n");
1280 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1284 struct ec_params_i2c_passthru p;
1285 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1288 struct ec_response_i2c_passthru r;
1289 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1291 struct ec_params_i2c_passthru *p = ¶ms.p;
1292 struct ec_response_i2c_passthru *r = &response.r;
1293 struct ec_params_i2c_passthru_msg *msg;
1294 uint8_t *pdata, *read_ptr = NULL;
1302 p->num_msgs = nmsgs;
1303 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1305 /* Create a message to write the register address and optional data */
1306 pdata = (uint8_t *)p + size;
1309 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1310 bool is_read = in->flags & I2C_M_RD;
1312 msg->addr_flags = in->addr;
1315 msg->addr_flags |= EC_I2C_FLAG_READ;
1316 read_len += in->len;
1318 if (sizeof(*r) + read_len > sizeof(response)) {
1319 puts("Read length too big for buffer\n");
1323 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1324 puts("Params too large for buffer\n");
1327 memcpy(pdata, in->buf, in->len);
1332 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1333 r, sizeof(*r) + read_len);
1337 /* Parse response */
1338 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1339 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1343 if (rv < sizeof(*r) + read_len) {
1344 puts("Truncated read response\n");
1348 /* We only support a single read message for each transfer */
1350 memcpy(read_ptr, r->data, read_len);
1355 int cros_ec_get_features(struct udevice *dev, u64 *featuresp)
1357 struct ec_response_get_features r;
1360 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
1361 if (rv != sizeof(r))
1363 *featuresp = r.flags[0] | (u64)r.flags[1] << 32;
1368 int cros_ec_check_feature(struct udevice *dev, uint feature)
1370 struct ec_response_get_features r;
1373 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, NULL, 0, &r, sizeof(r));
1374 if (rv != sizeof(r))
1377 if (feature >= 8 * sizeof(r.flags))
1380 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature) ? true :
1385 * Query the EC for specified mask indicating enabled events.
1386 * The EC maintains separate event masks for SMI, SCI and WAKE.
1388 static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1392 struct ec_params_host_event req;
1393 struct ec_response_host_event rsp;
1395 req.action = action;
1396 req.mask_type = mask;
1397 if (action != EC_HOST_EVENT_GET)
1401 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1404 if (action != EC_HOST_EVENT_GET)
1412 static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1413 uint action, uint64_t *value)
1416 struct ec_params_host_event_mask req;
1417 struct ec_response_host_event_mask rsp;
1419 if (hcmd == INVALID_HCMD)
1422 if (action != EC_HOST_EVENT_GET)
1423 req.mask = (uint32_t)*value;
1427 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1428 if (action != EC_HOST_EVENT_GET)
1436 bool cros_ec_is_uhepi_supported(struct udevice *dev)
1438 #define UHEPI_SUPPORTED 1
1439 #define UHEPI_NOT_SUPPORTED 2
1440 static int uhepi_support;
1442 if (!uhepi_support) {
1443 uhepi_support = cros_ec_check_feature(dev,
1444 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1445 UHEPI_NOT_SUPPORTED;
1446 log_debug("Chrome EC: UHEPI %s\n",
1447 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1450 return uhepi_support == UHEPI_SUPPORTED;
1453 static int cros_ec_get_mask(struct udevice *dev, uint type)
1457 if (cros_ec_is_uhepi_supported(dev)) {
1458 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1460 assert(type < ARRAY_SIZE(event_map));
1461 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1462 EC_HOST_EVENT_GET, &value);
1467 static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1469 if (cros_ec_is_uhepi_supported(dev))
1470 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1472 assert(type < ARRAY_SIZE(event_map));
1474 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1475 EC_HOST_EVENT_CLEAR, &mask);
1478 uint64_t cros_ec_get_events_b(struct udevice *dev)
1480 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1483 int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1485 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1487 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1490 int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1492 struct ec_params_charge_state p;
1493 struct ec_response_charge_state r;
1496 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1497 p.get_param.param = CS_PARAM_LIMIT_POWER;
1498 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1502 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1503 * LIMIT_POWER is not requested.
1505 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1506 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1510 if (ret != sizeof(r.get_param))
1513 *limit_powerp = r.get_param.value;
1517 int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1519 struct ec_params_config_power_button params;
1522 params.flags = flags;
1523 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1524 ¶ms, sizeof(params), NULL, 0);
1531 int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1536 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1541 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1544 int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1549 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1554 /* Set lid close event state in the EC SMI event mask */
1556 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1558 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1560 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1564 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1568 int cros_ec_vstore_supported(struct udevice *dev)
1570 return cros_ec_check_feature(dev, EC_FEATURE_VSTORE);
1573 int cros_ec_vstore_info(struct udevice *dev, u32 *lockedp)
1575 struct ec_response_vstore_info *resp;
1577 if (ec_command_inptr(dev, EC_CMD_VSTORE_INFO, 0, NULL, 0,
1578 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1582 *lockedp = resp->slot_locked;
1584 return resp->slot_count;
1588 * cros_ec_vstore_read - Read data from EC vstore slot
1590 * @slot: vstore slot to read from
1591 * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
1593 int cros_ec_vstore_read(struct udevice *dev, int slot, uint8_t *data)
1595 struct ec_params_vstore_read req;
1596 struct ec_response_vstore_read *resp;
1599 if (ec_command_inptr(dev, EC_CMD_VSTORE_READ, 0, &req, sizeof(req),
1600 (uint8_t **)&resp, sizeof(*resp)) != sizeof(*resp))
1603 if (!data || req.slot >= EC_VSTORE_SLOT_MAX)
1606 memcpy(data, resp->data, sizeof(resp->data));
1612 * cros_ec_vstore_write - Save data into EC vstore slot
1614 * @slot: vstore slot to write into
1615 * @data: data to write
1616 * @size: size of data in bytes
1618 * Maximum size of data is EC_VSTORE_SLOT_SIZE. It is the callers
1619 * responsibility to check the number of implemented slots by
1620 * querying the vstore info.
1622 int cros_ec_vstore_write(struct udevice *dev, int slot, const uint8_t *data,
1625 struct ec_params_vstore_write req;
1627 if (slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE)
1631 memcpy(req.data, data, size);
1633 if (ec_command(dev, EC_CMD_VSTORE_WRITE, 0, &req, sizeof(req), NULL, 0))
1639 int cros_ec_get_switches(struct udevice *dev)
1641 struct dm_cros_ec_ops *ops;
1644 ops = dm_cros_ec_get_ops(dev);
1645 if (!ops->get_switches)
1648 ret = ops->get_switches(dev);
1650 return log_msg_ret("get", ret);
1655 int cros_ec_read_batt_charge(struct udevice *dev, uint *chargep)
1657 struct ec_params_charge_state req;
1658 struct ec_response_charge_state resp;
1661 req.cmd = CHARGE_STATE_CMD_GET_STATE;
1662 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &req, sizeof(req),
1663 &resp, sizeof(resp));
1665 return log_msg_ret("read", ret);
1667 *chargep = resp.get_state.batt_state_of_charge;
1672 UCLASS_DRIVER(cros_ec) = {
1673 .id = UCLASS_CROS_EC,
1675 .per_device_auto = sizeof(struct cros_ec_dev),
1676 #if CONFIG_IS_ENABLED(OF_REAL)
1677 .post_bind = dm_scan_fdt_dev,
1679 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,