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
26 #include <linux/errno.h>
28 #include <asm-generic/gpio.h>
29 #include <dm/device-internal.h>
30 #include <dm/of_extra.h>
31 #include <dm/uclass-internal.h>
34 #define debug_trace(fmt, b...) debug(fmt, #b)
36 #define debug_trace(fmt, b...)
40 /* Timeout waiting for a flash erase command to complete */
41 CROS_EC_CMD_TIMEOUT_MS
= 5000,
42 /* Timeout waiting for a synchronous hash to be recomputed */
43 CROS_EC_CMD_HASH_TIMEOUT_MS
= 2000,
46 #define INVALID_HCMD 0xFF
49 * Map UHEPI masks to non UHEPI commands in order to support old EC FW
50 * which does not support UHEPI command.
57 [EC_HOST_EVENT_MAIN
] = {
58 INVALID_HCMD
, EC_CMD_HOST_EVENT_CLEAR
,
62 INVALID_HCMD
, EC_CMD_HOST_EVENT_CLEAR_B
,
63 EC_CMD_HOST_EVENT_GET_B
,
65 [EC_HOST_EVENT_SCI_MASK
] = {
66 EC_CMD_HOST_EVENT_SET_SCI_MASK
, INVALID_HCMD
,
67 EC_CMD_HOST_EVENT_GET_SCI_MASK
,
69 [EC_HOST_EVENT_SMI_MASK
] = {
70 EC_CMD_HOST_EVENT_SET_SMI_MASK
, INVALID_HCMD
,
71 EC_CMD_HOST_EVENT_GET_SMI_MASK
,
73 [EC_HOST_EVENT_ALWAYS_REPORT_MASK
] = {
74 INVALID_HCMD
, INVALID_HCMD
, INVALID_HCMD
,
76 [EC_HOST_EVENT_ACTIVE_WAKE_MASK
] = {
77 EC_CMD_HOST_EVENT_SET_WAKE_MASK
, INVALID_HCMD
,
78 EC_CMD_HOST_EVENT_GET_WAKE_MASK
,
80 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX
] = {
81 EC_CMD_HOST_EVENT_SET_WAKE_MASK
, INVALID_HCMD
,
82 EC_CMD_HOST_EVENT_GET_WAKE_MASK
,
84 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3
] = {
85 EC_CMD_HOST_EVENT_SET_WAKE_MASK
, INVALID_HCMD
,
86 EC_CMD_HOST_EVENT_GET_WAKE_MASK
,
88 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5
] = {
89 EC_CMD_HOST_EVENT_SET_WAKE_MASK
, INVALID_HCMD
,
90 EC_CMD_HOST_EVENT_GET_WAKE_MASK
,
94 void cros_ec_dump_data(const char *name
, int cmd
, const uint8_t *data
, int len
)
101 printf("cmd=%#x: ", cmd
);
102 for (i
= 0; i
< len
; i
++)
103 printf("%02x ", data
[i
]);
109 * Calculate a simple 8-bit checksum of a data block
111 * @param data Data block to checksum
112 * @param size Size of data block in bytes
113 * @return checksum value (0 to 255)
115 int cros_ec_calc_checksum(const uint8_t *data
, int size
)
119 for (i
= csum
= 0; i
< size
; i
++)
125 * Create a request packet for protocol version 3.
127 * The packet is stored in the device's internal output buffer.
129 * @param dev CROS-EC device
130 * @param cmd Command to send (EC_CMD_...)
131 * @param cmd_version Version of command to send (EC_VER_...)
132 * @param dout Output data (may be NULL If dout_len=0)
133 * @param dout_len Size of output data in bytes
134 * @return packet size in bytes, or <0 if error.
136 static int create_proto3_request(struct cros_ec_dev
*cdev
,
137 int cmd
, int cmd_version
,
138 const void *dout
, int dout_len
)
140 struct ec_host_request
*rq
= (struct ec_host_request
*)cdev
->dout
;
141 int out_bytes
= dout_len
+ sizeof(*rq
);
143 /* Fail if output size is too big */
144 if (out_bytes
> (int)sizeof(cdev
->dout
)) {
145 debug("%s: Cannot send %d bytes\n", __func__
, dout_len
);
146 return -EC_RES_REQUEST_TRUNCATED
;
149 /* Fill in request packet */
150 rq
->struct_version
= EC_HOST_REQUEST_VERSION
;
153 rq
->command_version
= cmd_version
;
155 rq
->data_len
= dout_len
;
157 /* Copy data after header */
158 memcpy(rq
+ 1, dout
, dout_len
);
160 /* Write checksum field so the entire packet sums to 0 */
161 rq
->checksum
= (uint8_t)(-cros_ec_calc_checksum(cdev
->dout
, out_bytes
));
163 cros_ec_dump_data("out", cmd
, cdev
->dout
, out_bytes
);
165 /* Return size of request packet */
170 * Prepare the device to receive a protocol version 3 response.
172 * @param dev CROS-EC device
173 * @param din_len Maximum size of response in bytes
174 * @return maximum expected number of bytes in response, or <0 if error.
176 static int prepare_proto3_response_buffer(struct cros_ec_dev
*cdev
, int din_len
)
178 int in_bytes
= din_len
+ sizeof(struct ec_host_response
);
180 /* Fail if input size is too big */
181 if (in_bytes
> (int)sizeof(cdev
->din
)) {
182 debug("%s: Cannot receive %d bytes\n", __func__
, din_len
);
183 return -EC_RES_RESPONSE_TOO_BIG
;
186 /* Return expected size of response packet */
191 * Handle a protocol version 3 response packet.
193 * The packet must already be stored in the device's internal input buffer.
195 * @param dev CROS-EC device
196 * @param dinp Returns pointer to response data
197 * @param din_len Maximum size of response in bytes
198 * @return number of bytes of response data, or <0 if error. Note that error
199 * codes can be from errno.h or -ve EC_RES_INVALID_CHECKSUM values (and they
202 static int handle_proto3_response(struct cros_ec_dev
*dev
,
203 uint8_t **dinp
, int din_len
)
205 struct ec_host_response
*rs
= (struct ec_host_response
*)dev
->din
;
209 cros_ec_dump_data("in-header", -1, dev
->din
, sizeof(*rs
));
211 /* Check input data */
212 if (rs
->struct_version
!= EC_HOST_RESPONSE_VERSION
) {
213 debug("%s: EC response version mismatch\n", __func__
);
214 return -EC_RES_INVALID_RESPONSE
;
218 debug("%s: EC response reserved != 0\n", __func__
);
219 return -EC_RES_INVALID_RESPONSE
;
222 if (rs
->data_len
> din_len
) {
223 debug("%s: EC returned too much data\n", __func__
);
224 return -EC_RES_RESPONSE_TOO_BIG
;
227 cros_ec_dump_data("in-data", -1, dev
->din
+ sizeof(*rs
), rs
->data_len
);
229 /* Update in_bytes to actual data size */
230 in_bytes
= sizeof(*rs
) + rs
->data_len
;
232 /* Verify checksum */
233 csum
= cros_ec_calc_checksum(dev
->din
, in_bytes
);
235 debug("%s: EC response checksum invalid: 0x%02x\n", __func__
,
237 return -EC_RES_INVALID_CHECKSUM
;
240 /* Return error result, if any */
242 return -(int)rs
->result
;
244 /* If we're still here, set response data pointer and return length */
245 *dinp
= (uint8_t *)(rs
+ 1);
250 static int send_command_proto3(struct cros_ec_dev
*cdev
,
251 int cmd
, int cmd_version
,
252 const void *dout
, int dout_len
,
253 uint8_t **dinp
, int din_len
)
255 struct dm_cros_ec_ops
*ops
;
256 int out_bytes
, in_bytes
;
259 /* Create request packet */
260 out_bytes
= create_proto3_request(cdev
, cmd
, cmd_version
,
265 /* Prepare response buffer */
266 in_bytes
= prepare_proto3_response_buffer(cdev
, din_len
);
270 ops
= dm_cros_ec_get_ops(cdev
->dev
);
271 rv
= ops
->packet
? ops
->packet(cdev
->dev
, out_bytes
, in_bytes
) :
276 /* Process the response */
277 return handle_proto3_response(cdev
, dinp
, din_len
);
280 static int send_command(struct cros_ec_dev
*dev
, uint cmd
, int cmd_version
,
281 const void *dout
, int dout_len
,
282 uint8_t **dinp
, int din_len
)
284 struct dm_cros_ec_ops
*ops
;
287 /* Handle protocol version 3 support */
288 if (dev
->protocol_version
== 3) {
289 return send_command_proto3(dev
, cmd
, cmd_version
,
290 dout
, dout_len
, dinp
, din_len
);
293 ops
= dm_cros_ec_get_ops(dev
->dev
);
294 ret
= ops
->command(dev
->dev
, cmd
, cmd_version
,
295 (const uint8_t *)dout
, dout_len
, dinp
, din_len
);
301 * Send a command to the CROS-EC device and return the reply.
303 * The device's internal input/output buffers are used.
305 * @param dev CROS-EC device
306 * @param cmd Command to send (EC_CMD_...)
307 * @param cmd_version Version of command to send (EC_VER_...)
308 * @param dout Output data (may be NULL If dout_len=0)
309 * @param dout_len Size of output data in bytes
310 * @param dinp Response data (may be NULL If din_len=0).
311 * If not NULL, it will be updated to point to the data
312 * and will always be double word aligned (64-bits)
313 * @param din_len Maximum size of response in bytes
314 * @return number of bytes in response, or -ve on error
316 static int ec_command_inptr(struct udevice
*dev
, uint8_t cmd
,
317 int cmd_version
, const void *dout
, int dout_len
,
318 uint8_t **dinp
, int din_len
)
320 struct cros_ec_dev
*cdev
= dev_get_uclass_priv(dev
);
324 len
= send_command(cdev
, cmd
, cmd_version
, dout
, dout_len
, &din
,
327 /* If the command doesn't complete, wait a while */
328 if (len
== -EC_RES_IN_PROGRESS
) {
329 struct ec_response_get_comms_status
*resp
= NULL
;
332 /* Wait for command to complete */
333 start
= get_timer(0);
337 mdelay(50); /* Insert some reasonable delay */
338 ret
= send_command(cdev
, EC_CMD_GET_COMMS_STATUS
, 0,
340 (uint8_t **)&resp
, sizeof(*resp
));
344 if (get_timer(start
) > CROS_EC_CMD_TIMEOUT_MS
) {
345 debug("%s: Command %#02x timeout\n",
347 return -EC_RES_TIMEOUT
;
349 } while (resp
->flags
& EC_COMMS_STATUS_PROCESSING
);
351 /* OK it completed, so read the status response */
352 /* not sure why it was 0 for the last argument */
353 len
= send_command(cdev
, EC_CMD_RESEND_RESPONSE
, 0, NULL
, 0,
357 debug("%s: len=%d, din=%p\n", __func__
, len
, din
);
359 /* If we have any data to return, it must be 64bit-aligned */
360 assert(len
<= 0 || !((uintptr_t)din
& 7));
368 * Send a command to the CROS-EC device and return the reply.
370 * The device's internal input/output buffers are used.
372 * @param dev CROS-EC device
373 * @param cmd Command to send (EC_CMD_...)
374 * @param cmd_version Version of command to send (EC_VER_...)
375 * @param dout Output data (may be NULL If dout_len=0)
376 * @param dout_len Size of output data in bytes
377 * @param din Response data (may be NULL If din_len=0).
378 * It not NULL, it is a place for ec_command() to copy the
380 * @param din_len Maximum size of response in bytes
381 * @return number of bytes in response, or -ve on error
383 static int ec_command(struct udevice
*dev
, uint cmd
, int cmd_version
,
384 const void *dout
, int dout_len
,
385 void *din
, int din_len
)
390 assert((din_len
== 0) || din
);
391 len
= ec_command_inptr(dev
, cmd
, cmd_version
, dout
, dout_len
,
392 &in_buffer
, din_len
);
395 * If we were asked to put it somewhere, do so, otherwise just
396 * disregard the result.
398 if (din
&& in_buffer
) {
399 assert(len
<= din_len
);
400 memmove(din
, in_buffer
, len
);
406 int cros_ec_scan_keyboard(struct udevice
*dev
, struct mbkp_keyscan
*scan
)
408 if (ec_command(dev
, EC_CMD_MKBP_STATE
, 0, NULL
, 0, scan
,
409 sizeof(scan
->data
)) != sizeof(scan
->data
))
415 int cros_ec_read_id(struct udevice
*dev
, char *id
, int maxlen
)
417 struct ec_response_get_version
*r
;
420 ret
= ec_command_inptr(dev
, EC_CMD_GET_VERSION
, 0, NULL
, 0,
421 (uint8_t **)&r
, sizeof(*r
));
422 if (ret
!= sizeof(*r
)) {
423 log_err("Got rc %d, expected %u\n", ret
, (uint
)sizeof(*r
));
427 if (maxlen
> (int)sizeof(r
->version_string_ro
))
428 maxlen
= sizeof(r
->version_string_ro
);
430 switch (r
->current_image
) {
432 memcpy(id
, r
->version_string_ro
, maxlen
);
435 memcpy(id
, r
->version_string_rw
, maxlen
);
438 log_err("Invalid EC image %d\n", r
->current_image
);
442 id
[maxlen
- 1] = '\0';
446 int cros_ec_read_version(struct udevice
*dev
,
447 struct ec_response_get_version
**versionp
)
449 if (ec_command_inptr(dev
, EC_CMD_GET_VERSION
, 0, NULL
, 0,
450 (uint8_t **)versionp
, sizeof(**versionp
))
451 != sizeof(**versionp
))
457 int cros_ec_read_build_info(struct udevice
*dev
, char **strp
)
459 if (ec_command_inptr(dev
, EC_CMD_GET_BUILD_INFO
, 0, NULL
, 0,
460 (uint8_t **)strp
, EC_PROTO2_MAX_PARAM_SIZE
) < 0)
466 int cros_ec_read_current_image(struct udevice
*dev
,
467 enum ec_current_image
*image
)
469 struct ec_response_get_version
*r
;
471 if (ec_command_inptr(dev
, EC_CMD_GET_VERSION
, 0, NULL
, 0,
472 (uint8_t **)&r
, sizeof(*r
)) != sizeof(*r
))
475 *image
= r
->current_image
;
479 static int cros_ec_wait_on_hash_done(struct udevice
*dev
,
480 struct ec_response_vboot_hash
*hash
)
482 struct ec_params_vboot_hash p
;
485 start
= get_timer(0);
486 while (hash
->status
== EC_VBOOT_HASH_STATUS_BUSY
) {
487 mdelay(50); /* Insert some reasonable delay */
489 p
.cmd
= EC_VBOOT_HASH_GET
;
490 if (ec_command(dev
, EC_CMD_VBOOT_HASH
, 0, &p
, sizeof(p
),
491 hash
, sizeof(*hash
)) < 0)
494 if (get_timer(start
) > CROS_EC_CMD_HASH_TIMEOUT_MS
) {
495 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__
);
496 return -EC_RES_TIMEOUT
;
502 int cros_ec_read_hash(struct udevice
*dev
, uint hash_offset
,
503 struct ec_response_vboot_hash
*hash
)
505 struct ec_params_vboot_hash p
;
508 p
.cmd
= EC_VBOOT_HASH_GET
;
509 p
.offset
= hash_offset
;
510 if (ec_command(dev
, EC_CMD_VBOOT_HASH
, 0, &p
, sizeof(p
),
511 hash
, sizeof(*hash
)) < 0)
514 /* If the EC is busy calculating the hash, fidget until it's done. */
515 rv
= cros_ec_wait_on_hash_done(dev
, hash
);
519 /* If the hash is valid, we're done. Otherwise, we have to kick it off
520 * again and wait for it to complete. Note that we explicitly assume
521 * that hashing zero bytes is always wrong, even though that would
522 * produce a valid hash value. */
523 if (hash
->status
== EC_VBOOT_HASH_STATUS_DONE
&& hash
->size
)
526 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
527 __func__
, hash
->status
, hash
->size
);
529 p
.cmd
= EC_VBOOT_HASH_START
;
530 p
.hash_type
= EC_VBOOT_HASH_TYPE_SHA256
;
532 p
.offset
= hash_offset
;
534 if (ec_command(dev
, EC_CMD_VBOOT_HASH
, 0, &p
, sizeof(p
),
535 hash
, sizeof(*hash
)) < 0)
538 rv
= cros_ec_wait_on_hash_done(dev
, hash
);
542 debug("%s: hash done\n", __func__
);
547 static int cros_ec_invalidate_hash(struct udevice
*dev
)
549 struct ec_params_vboot_hash p
;
550 struct ec_response_vboot_hash
*hash
;
552 /* We don't have an explict command for the EC to discard its current
553 * hash value, so we'll just tell it to calculate one that we know is
554 * wrong (we claim that hashing zero bytes is always invalid).
556 p
.cmd
= EC_VBOOT_HASH_RECALC
;
557 p
.hash_type
= EC_VBOOT_HASH_TYPE_SHA256
;
562 debug("%s:\n", __func__
);
564 if (ec_command_inptr(dev
, EC_CMD_VBOOT_HASH
, 0, &p
, sizeof(p
),
565 (uint8_t **)&hash
, sizeof(*hash
)) < 0)
568 /* No need to wait for it to finish */
572 int cros_ec_reboot(struct udevice
*dev
, enum ec_reboot_cmd cmd
, uint8_t flags
)
574 struct ec_params_reboot_ec p
;
579 if (ec_command_inptr(dev
, EC_CMD_REBOOT_EC
, 0, &p
, sizeof(p
), NULL
, 0)
583 if (!(flags
& EC_REBOOT_FLAG_ON_AP_SHUTDOWN
)) {
585 * EC reboot will take place immediately so delay to allow it
586 * to complete. Note that some reboot types (EC_REBOOT_COLD)
587 * will reboot the AP as well, in which case we won't actually
591 * TODO(rspangler@chromium.org): Would be nice if we had a
592 * better way to determine when the reboot is complete. Could
593 * we poll a memory-mapped LPC value?
601 int cros_ec_interrupt_pending(struct udevice
*dev
)
603 struct cros_ec_dev
*cdev
= dev_get_uclass_priv(dev
);
605 /* no interrupt support : always poll */
606 if (!dm_gpio_is_valid(&cdev
->ec_int
))
609 return dm_gpio_get_value(&cdev
->ec_int
);
612 int cros_ec_info(struct udevice
*dev
, struct ec_response_mkbp_info
*info
)
614 if (ec_command(dev
, EC_CMD_MKBP_INFO
, 0, NULL
, 0, info
,
615 sizeof(*info
)) != sizeof(*info
))
621 int cros_ec_get_event_mask(struct udevice
*dev
, uint type
, uint32_t *mask
)
623 struct ec_response_host_event_mask rsp
;
626 ret
= ec_command(dev
, type
, 0, NULL
, 0, &rsp
, sizeof(rsp
));
629 else if (ret
!= sizeof(rsp
))
637 int cros_ec_set_event_mask(struct udevice
*dev
, uint type
, uint32_t mask
)
639 struct ec_params_host_event_mask req
;
644 ret
= ec_command(dev
, type
, 0, &req
, sizeof(req
), NULL
, 0);
651 int cros_ec_get_host_events(struct udevice
*dev
, uint32_t *events_ptr
)
653 struct ec_response_host_event_mask
*resp
;
656 * Use the B copy of the event flags, because the main copy is already
659 if (ec_command_inptr(dev
, EC_CMD_HOST_EVENT_GET_B
, 0, NULL
, 0,
660 (uint8_t **)&resp
, sizeof(*resp
)) < (int)sizeof(*resp
))
663 if (resp
->mask
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID
))
666 *events_ptr
= resp
->mask
;
670 int cros_ec_clear_host_events(struct udevice
*dev
, uint32_t events
)
672 struct ec_params_host_event_mask params
;
674 params
.mask
= events
;
677 * Use the B copy of the event flags, so it affects the data returned
678 * by cros_ec_get_host_events().
680 if (ec_command_inptr(dev
, EC_CMD_HOST_EVENT_CLEAR_B
, 0,
681 ¶ms
, sizeof(params
), NULL
, 0) < 0)
687 int cros_ec_flash_protect(struct udevice
*dev
, uint32_t set_mask
,
689 struct ec_response_flash_protect
*resp
)
691 struct ec_params_flash_protect params
;
693 params
.mask
= set_mask
;
694 params
.flags
= set_flags
;
696 if (ec_command(dev
, EC_CMD_FLASH_PROTECT
, EC_VER_FLASH_PROTECT
,
697 ¶ms
, sizeof(params
),
698 resp
, sizeof(*resp
)) != sizeof(*resp
))
704 int cros_ec_entering_mode(struct udevice
*dev
, int mode
)
708 rc
= ec_command(dev
, EC_CMD_ENTERING_MODE
, 0, &mode
, sizeof(mode
),
715 static int cros_ec_check_version(struct udevice
*dev
)
717 struct cros_ec_dev
*cdev
= dev_get_uclass_priv(dev
);
718 struct ec_params_hello req
;
719 struct ec_response_hello
*resp
;
721 struct dm_cros_ec_ops
*ops
;
724 ops
= dm_cros_ec_get_ops(dev
);
725 if (ops
->check_version
) {
726 ret
= ops
->check_version(dev
);
732 * TODO(sjg@chromium.org).
733 * There is a strange oddity here with the EC. We could just ignore
734 * the response, i.e. pass the last two parameters as NULL and 0.
735 * In this case we won't read back very many bytes from the EC.
736 * On the I2C bus the EC gets upset about this and will try to send
737 * the bytes anyway. This means that we will have to wait for that
738 * to complete before continuing with a new EC command.
740 * This problem is probably unique to the I2C bus.
742 * So for now, just read all the data anyway.
745 /* Try sending a version 3 packet */
746 cdev
->protocol_version
= 3;
748 if (ec_command_inptr(dev
, EC_CMD_HELLO
, 0, &req
, sizeof(req
),
749 (uint8_t **)&resp
, sizeof(*resp
)) > 0)
752 /* Try sending a version 2 packet */
753 cdev
->protocol_version
= 2;
754 if (ec_command_inptr(dev
, EC_CMD_HELLO
, 0, &req
, sizeof(req
),
755 (uint8_t **)&resp
, sizeof(*resp
)) > 0)
759 * Fail if we're still here, since the EC doesn't understand any
760 * protcol version we speak. Version 1 interface without command
761 * version is no longer supported, and we don't know about any new
764 cdev
->protocol_version
= 0;
765 printf("%s: ERROR: old EC interface not supported\n", __func__
);
769 int cros_ec_test(struct udevice
*dev
)
771 struct ec_params_hello req
;
772 struct ec_response_hello
*resp
;
774 req
.in_data
= 0x12345678;
775 if (ec_command_inptr(dev
, EC_CMD_HELLO
, 0, &req
, sizeof(req
),
776 (uint8_t **)&resp
, sizeof(*resp
)) < sizeof(*resp
)) {
777 printf("ec_command_inptr() returned error\n");
780 if (resp
->out_data
!= req
.in_data
+ 0x01020304) {
781 printf("Received invalid handshake %x\n", resp
->out_data
);
788 int cros_ec_flash_offset(struct udevice
*dev
, enum ec_flash_region region
,
789 uint32_t *offset
, uint32_t *size
)
791 struct ec_params_flash_region_info p
;
792 struct ec_response_flash_region_info
*r
;
796 ret
= ec_command_inptr(dev
, EC_CMD_FLASH_REGION_INFO
,
797 EC_VER_FLASH_REGION_INFO
,
798 &p
, sizeof(p
), (uint8_t **)&r
, sizeof(*r
));
799 if (ret
!= sizeof(*r
))
810 int cros_ec_flash_erase(struct udevice
*dev
, uint32_t offset
, uint32_t size
)
812 struct ec_params_flash_erase p
;
816 return ec_command_inptr(dev
, EC_CMD_FLASH_ERASE
, 0, &p
, sizeof(p
),
821 * Write a single block to the flash
823 * Write a block of data to the EC flash. The size must not exceed the flash
824 * write block size which you can obtain from cros_ec_flash_write_burst_size().
826 * The offset starts at 0. You can obtain the region information from
827 * cros_ec_flash_offset() to find out where to write for a particular region.
829 * Attempting to write to the region where the EC is currently running from
830 * will result in an error.
832 * @param dev CROS-EC device
833 * @param data Pointer to data buffer to write
834 * @param offset Offset within flash to write to.
835 * @param size Number of bytes to write
836 * @return 0 if ok, -1 on error
838 static int cros_ec_flash_write_block(struct udevice
*dev
, const uint8_t *data
,
839 uint32_t offset
, uint32_t size
)
841 struct ec_params_flash_write
*p
;
844 p
= malloc(sizeof(*p
) + size
);
850 assert(data
&& p
->size
<= EC_FLASH_WRITE_VER0_SIZE
);
851 memcpy(p
+ 1, data
, p
->size
);
853 ret
= ec_command_inptr(dev
, EC_CMD_FLASH_WRITE
, 0,
854 p
, sizeof(*p
) + size
, NULL
, 0) >= 0 ? 0 : -1;
862 * Return optimal flash write burst size
864 static int cros_ec_flash_write_burst_size(struct udevice
*dev
)
866 return EC_FLASH_WRITE_VER0_SIZE
;
870 * Check if a block of data is erased (all 0xff)
872 * This function is useful when dealing with flash, for checking whether a
873 * data block is erased and thus does not need to be programmed.
875 * @param data Pointer to data to check (must be word-aligned)
876 * @param size Number of bytes to check (must be word-aligned)
877 * @return 0 if erased, non-zero if any word is not erased
879 static int cros_ec_data_is_erased(const uint32_t *data
, int size
)
882 size
/= sizeof(uint32_t);
883 for (; size
> 0; size
-= 4, data
++)
891 * Read back flash parameters
893 * This function reads back parameters of the flash as reported by the EC
895 * @param dev Pointer to device
896 * @param info Pointer to output flash info struct
898 int cros_ec_read_flashinfo(struct udevice
*dev
,
899 struct ec_response_flash_info
*info
)
903 ret
= ec_command(dev
, EC_CMD_FLASH_INFO
, 0,
904 NULL
, 0, info
, sizeof(*info
));
908 return ret
< sizeof(*info
) ? -1 : 0;
911 int cros_ec_flash_write(struct udevice
*dev
, const uint8_t *data
,
912 uint32_t offset
, uint32_t size
)
914 struct cros_ec_dev
*cdev
= dev_get_uclass_priv(dev
);
915 uint32_t burst
= cros_ec_flash_write_burst_size(dev
);
923 * TODO: round up to the nearest multiple of write size. Can get away
924 * without that on link right now because its write size is 4 bytes.
927 for (off
= offset
; off
< end
; off
+= burst
, data
+= burst
) {
930 /* If the data is empty, there is no point in programming it */
931 todo
= min(end
- off
, burst
);
932 if (cdev
->optimise_flash_write
&&
933 cros_ec_data_is_erased((uint32_t *)data
, todo
))
936 ret
= cros_ec_flash_write_block(dev
, data
, off
, todo
);
945 * Run verification on a slot
947 * @param me CrosEc instance
948 * @param region Region to run verification on
949 * @return 0 if success or not applicable. Non-zero if verification failed.
951 int cros_ec_efs_verify(struct udevice
*dev
, enum ec_flash_region region
)
953 struct ec_params_efs_verify p
;
956 log_info("EFS: EC is verifying updated image...\n");
959 rv
= ec_command(dev
, EC_CMD_EFS_VERIFY
, 0, &p
, sizeof(p
), NULL
, 0);
961 log_info("EFS: Verification success\n");
964 if (rv
== -EC_RES_INVALID_COMMAND
) {
965 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
968 log_info("EFS: Verification failed\n");
974 * Read a single block from the flash
976 * Read a block of data from the EC flash. The size must not exceed the flash
977 * write block size which you can obtain from cros_ec_flash_write_burst_size().
979 * The offset starts at 0. You can obtain the region information from
980 * cros_ec_flash_offset() to find out where to read for a particular region.
982 * @param dev CROS-EC device
983 * @param data Pointer to data buffer to read into
984 * @param offset Offset within flash to read from
985 * @param size Number of bytes to read
986 * @return 0 if ok, -1 on error
988 static int cros_ec_flash_read_block(struct udevice
*dev
, uint8_t *data
,
989 uint32_t offset
, uint32_t size
)
991 struct ec_params_flash_read p
;
996 return ec_command(dev
, EC_CMD_FLASH_READ
, 0,
997 &p
, sizeof(p
), data
, size
) >= 0 ? 0 : -1;
1000 int cros_ec_flash_read(struct udevice
*dev
, uint8_t *data
, uint32_t offset
,
1003 uint32_t burst
= cros_ec_flash_write_burst_size(dev
);
1007 end
= offset
+ size
;
1008 for (off
= offset
; off
< end
; off
+= burst
, data
+= burst
) {
1009 ret
= cros_ec_flash_read_block(dev
, data
, off
,
1010 min(end
- off
, burst
));
1018 int cros_ec_flash_update_rw(struct udevice
*dev
, const uint8_t *image
,
1021 uint32_t rw_offset
, rw_size
;
1024 if (cros_ec_flash_offset(dev
, EC_FLASH_REGION_ACTIVE
, &rw_offset
,
1027 if (image_size
> (int)rw_size
)
1030 /* Invalidate the existing hash, just in case the AP reboots
1031 * unexpectedly during the update. If that happened, the EC RW firmware
1032 * would be invalid, but the EC would still have the original hash.
1034 ret
= cros_ec_invalidate_hash(dev
);
1039 * Erase the entire RW section, so that the EC doesn't see any garbage
1040 * past the new image if it's smaller than the current image.
1042 * TODO: could optimize this to erase just the current image, since
1043 * presumably everything past that is 0xff's. But would still need to
1044 * round up to the nearest multiple of erase size.
1046 ret
= cros_ec_flash_erase(dev
, rw_offset
, rw_size
);
1050 /* Write the image */
1051 ret
= cros_ec_flash_write(dev
, image
, rw_offset
, image_size
);
1058 int cros_ec_read_nvdata(struct udevice
*dev
, uint8_t *block
, int size
)
1060 struct ec_params_vbnvcontext p
;
1063 if (size
!= EC_VBNV_BLOCK_SIZE
&& size
!= EC_VBNV_BLOCK_SIZE_V2
)
1066 p
.op
= EC_VBNV_CONTEXT_OP_READ
;
1068 len
= ec_command(dev
, EC_CMD_VBNV_CONTEXT
, EC_VER_VBNV_CONTEXT
,
1069 &p
, sizeof(uint32_t) + size
, block
, size
);
1071 log_err("Expected %d bytes, got %d\n", size
, len
);
1078 int cros_ec_write_nvdata(struct udevice
*dev
, const uint8_t *block
, int size
)
1080 struct ec_params_vbnvcontext p
;
1083 if (size
!= EC_VBNV_BLOCK_SIZE
&& size
!= EC_VBNV_BLOCK_SIZE_V2
)
1085 p
.op
= EC_VBNV_CONTEXT_OP_WRITE
;
1086 memcpy(p
.block
, block
, size
);
1088 len
= ec_command_inptr(dev
, EC_CMD_VBNV_CONTEXT
, EC_VER_VBNV_CONTEXT
,
1089 &p
, sizeof(uint32_t) + size
, NULL
, 0);
1096 int cros_ec_battery_cutoff(struct udevice
*dev
, uint8_t flags
)
1098 struct ec_params_battery_cutoff p
;
1102 len
= ec_command(dev
, EC_CMD_BATTERY_CUT_OFF
, 1, &p
, sizeof(p
),
1110 int cros_ec_set_ldo(struct udevice
*dev
, uint8_t index
, uint8_t state
)
1112 struct ec_params_ldo_set params
;
1114 params
.index
= index
;
1115 params
.state
= state
;
1117 if (ec_command_inptr(dev
, EC_CMD_LDO_SET
, 0, ¶ms
, sizeof(params
),
1124 int cros_ec_get_ldo(struct udevice
*dev
, uint8_t index
, uint8_t *state
)
1126 struct ec_params_ldo_get params
;
1127 struct ec_response_ldo_get
*resp
;
1129 params
.index
= index
;
1131 if (ec_command_inptr(dev
, EC_CMD_LDO_GET
, 0, ¶ms
, sizeof(params
),
1132 (uint8_t **)&resp
, sizeof(*resp
)) !=
1136 *state
= resp
->state
;
1141 int cros_ec_register(struct udevice
*dev
)
1143 struct cros_ec_dev
*cdev
= dev_get_uclass_priv(dev
);
1147 gpio_request_by_name(dev
, "ec-interrupt", 0, &cdev
->ec_int
,
1149 cdev
->optimise_flash_write
= dev_read_bool(dev
, "optimise-flash-write");
1151 if (cros_ec_check_version(dev
)) {
1152 debug("%s: Could not detect CROS-EC version\n", __func__
);
1153 return -CROS_EC_ERR_CHECK_VERSION
;
1156 if (cros_ec_read_id(dev
, id
, sizeof(id
))) {
1157 debug("%s: Could not read KBC ID\n", __func__
);
1158 return -CROS_EC_ERR_READ_ID
;
1161 /* Remember this device for use by the cros_ec command */
1162 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1163 cdev
->protocol_version
, id
);
1168 int cros_ec_decode_ec_flash(struct udevice
*dev
, struct fdt_cros_ec
*config
)
1170 ofnode flash_node
, node
;
1172 flash_node
= dev_read_subnode(dev
, "flash");
1173 if (!ofnode_valid(flash_node
)) {
1174 debug("Failed to find flash node\n");
1178 if (ofnode_read_fmap_entry(flash_node
, &config
->flash
)) {
1179 debug("Failed to decode flash node in chrome-ec\n");
1183 config
->flash_erase_value
= ofnode_read_s32_default(flash_node
,
1185 ofnode_for_each_subnode(node
, flash_node
) {
1186 const char *name
= ofnode_get_name(node
);
1187 enum ec_flash_region region
;
1189 if (0 == strcmp(name
, "ro")) {
1190 region
= EC_FLASH_REGION_RO
;
1191 } else if (0 == strcmp(name
, "rw")) {
1192 region
= EC_FLASH_REGION_ACTIVE
;
1193 } else if (0 == strcmp(name
, "wp-ro")) {
1194 region
= EC_FLASH_REGION_WP_RO
;
1196 debug("Unknown EC flash region name '%s'\n", name
);
1200 if (ofnode_read_fmap_entry(node
, &config
->region
[region
])) {
1201 debug("Failed to decode flash region in chrome-ec'\n");
1209 int cros_ec_i2c_tunnel(struct udevice
*dev
, int port
, struct i2c_msg
*in
,
1213 struct ec_params_i2c_passthru p
;
1214 uint8_t outbuf
[EC_PROTO2_MAX_PARAM_SIZE
];
1217 struct ec_response_i2c_passthru r
;
1218 uint8_t inbuf
[EC_PROTO2_MAX_PARAM_SIZE
];
1220 struct ec_params_i2c_passthru
*p
= ¶ms
.p
;
1221 struct ec_response_i2c_passthru
*r
= &response
.r
;
1222 struct ec_params_i2c_passthru_msg
*msg
;
1223 uint8_t *pdata
, *read_ptr
= NULL
;
1231 p
->num_msgs
= nmsgs
;
1232 size
= sizeof(*p
) + p
->num_msgs
* sizeof(*msg
);
1234 /* Create a message to write the register address and optional data */
1235 pdata
= (uint8_t *)p
+ size
;
1238 for (i
= 0, msg
= p
->msg
; i
< nmsgs
; i
++, msg
++, in
++) {
1239 bool is_read
= in
->flags
& I2C_M_RD
;
1241 msg
->addr_flags
= in
->addr
;
1244 msg
->addr_flags
|= EC_I2C_FLAG_READ
;
1245 read_len
+= in
->len
;
1247 if (sizeof(*r
) + read_len
> sizeof(response
)) {
1248 puts("Read length too big for buffer\n");
1252 if (pdata
- (uint8_t *)p
+ in
->len
> sizeof(params
)) {
1253 puts("Params too large for buffer\n");
1256 memcpy(pdata
, in
->buf
, in
->len
);
1261 rv
= ec_command(dev
, EC_CMD_I2C_PASSTHRU
, 0, p
, pdata
- (uint8_t *)p
,
1262 r
, sizeof(*r
) + read_len
);
1266 /* Parse response */
1267 if (r
->i2c_status
& EC_I2C_STATUS_ERROR
) {
1268 printf("Transfer failed with status=0x%x\n", r
->i2c_status
);
1272 if (rv
< sizeof(*r
) + read_len
) {
1273 puts("Truncated read response\n");
1277 /* We only support a single read message for each transfer */
1279 memcpy(read_ptr
, r
->data
, read_len
);
1284 int cros_ec_check_feature(struct udevice
*dev
, int feature
)
1286 struct ec_response_get_features r
;
1289 rv
= ec_command(dev
, EC_CMD_GET_FEATURES
, 0, &r
, sizeof(r
), NULL
, 0);
1293 if (feature
>= 8 * sizeof(r
.flags
))
1296 return r
.flags
[feature
/ 32] & EC_FEATURE_MASK_0(feature
);
1300 * Query the EC for specified mask indicating enabled events.
1301 * The EC maintains separate event masks for SMI, SCI and WAKE.
1303 static int cros_ec_uhepi_cmd(struct udevice
*dev
, uint mask
, uint action
,
1307 struct ec_params_host_event req
;
1308 struct ec_response_host_event rsp
;
1310 req
.action
= action
;
1311 req
.mask_type
= mask
;
1312 if (action
!= EC_HOST_EVENT_GET
)
1316 ret
= ec_command(dev
, EC_CMD_HOST_EVENT
, 0, &req
, sizeof(req
), &rsp
,
1319 if (action
!= EC_HOST_EVENT_GET
)
1327 static int cros_ec_handle_non_uhepi_cmd(struct udevice
*dev
, uint hcmd
,
1328 uint action
, uint64_t *value
)
1331 struct ec_params_host_event_mask req
;
1332 struct ec_response_host_event_mask rsp
;
1334 if (hcmd
== INVALID_HCMD
)
1337 if (action
!= EC_HOST_EVENT_GET
)
1338 req
.mask
= (uint32_t)*value
;
1342 ret
= ec_command(dev
, hcmd
, 0, &req
, sizeof(req
), &rsp
, sizeof(rsp
));
1343 if (action
!= EC_HOST_EVENT_GET
)
1351 bool cros_ec_is_uhepi_supported(struct udevice
*dev
)
1353 #define UHEPI_SUPPORTED 1
1354 #define UHEPI_NOT_SUPPORTED 2
1355 static int uhepi_support
;
1357 if (!uhepi_support
) {
1358 uhepi_support
= cros_ec_check_feature(dev
,
1359 EC_FEATURE_UNIFIED_WAKE_MASKS
) > 0 ? UHEPI_SUPPORTED
:
1360 UHEPI_NOT_SUPPORTED
;
1361 log_debug("Chrome EC: UHEPI %s\n",
1362 uhepi_support
== UHEPI_SUPPORTED
? "supported" :
1365 return uhepi_support
== UHEPI_SUPPORTED
;
1368 static int cros_ec_get_mask(struct udevice
*dev
, uint type
)
1372 if (cros_ec_is_uhepi_supported(dev
)) {
1373 cros_ec_uhepi_cmd(dev
, type
, EC_HOST_EVENT_GET
, &value
);
1375 assert(type
< ARRAY_SIZE(event_map
));
1376 cros_ec_handle_non_uhepi_cmd(dev
, event_map
[type
].get_cmd
,
1377 EC_HOST_EVENT_GET
, &value
);
1382 static int cros_ec_clear_mask(struct udevice
*dev
, uint type
, u64 mask
)
1384 if (cros_ec_is_uhepi_supported(dev
))
1385 return cros_ec_uhepi_cmd(dev
, type
, EC_HOST_EVENT_CLEAR
, &mask
);
1387 assert(type
< ARRAY_SIZE(event_map
));
1389 return cros_ec_handle_non_uhepi_cmd(dev
, event_map
[type
].clear_cmd
,
1390 EC_HOST_EVENT_CLEAR
, &mask
);
1393 uint64_t cros_ec_get_events_b(struct udevice
*dev
)
1395 return cros_ec_get_mask(dev
, EC_HOST_EVENT_B
);
1398 int cros_ec_clear_events_b(struct udevice
*dev
, uint64_t mask
)
1400 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask
);
1402 return cros_ec_clear_mask(dev
, EC_HOST_EVENT_B
, mask
);
1405 int cros_ec_read_limit_power(struct udevice
*dev
, int *limit_powerp
)
1407 struct ec_params_charge_state p
;
1408 struct ec_response_charge_state r
;
1411 p
.cmd
= CHARGE_STATE_CMD_GET_PARAM
;
1412 p
.get_param
.param
= CS_PARAM_LIMIT_POWER
;
1413 ret
= ec_command(dev
, EC_CMD_CHARGE_STATE
, 0, &p
, sizeof(p
),
1417 * If our EC doesn't support the LIMIT_POWER parameter, assume that
1418 * LIMIT_POWER is not requested.
1420 if (ret
== -EC_RES_INVALID_PARAM
|| ret
== -EC_RES_INVALID_COMMAND
) {
1421 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1425 if (ret
!= sizeof(r
.get_param
))
1428 *limit_powerp
= r
.get_param
.value
;
1432 int cros_ec_config_powerbtn(struct udevice
*dev
, uint32_t flags
)
1434 struct ec_params_config_power_button params
;
1437 params
.flags
= flags
;
1438 ret
= ec_command(dev
, EC_CMD_CONFIG_POWER_BUTTON
, 0,
1439 ¶ms
, sizeof(params
), NULL
, 0);
1446 int cros_ec_get_lid_shutdown_mask(struct udevice
*dev
)
1451 ret
= cros_ec_get_event_mask(dev
, EC_CMD_HOST_EVENT_GET_SMI_MASK
,
1456 return !!(mask
& EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED
));
1459 int cros_ec_set_lid_shutdown_mask(struct udevice
*dev
, int enable
)
1464 ret
= cros_ec_get_event_mask(dev
, EC_CMD_HOST_EVENT_GET_SMI_MASK
,
1469 /* Set lid close event state in the EC SMI event mask */
1471 mask
|= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED
);
1473 mask
&= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED
);
1475 ret
= cros_ec_set_event_mask(dev
, EC_CMD_HOST_EVENT_SET_SMI_MASK
, mask
);
1479 printf("EC: %sabled lid close event\n", enable
? "en" : "dis");
1483 UCLASS_DRIVER(cros_ec
) = {
1484 .id
= UCLASS_CROS_EC
,
1486 .per_device_auto_alloc_size
= sizeof(struct cros_ec_dev
),
1487 .post_bind
= dm_scan_fdt_dev
,
1488 .flags
= DM_UC_FLAG_ALLOC_PRIV_DMA
,