x86: Add a dtsi file for the pc speaker
[oweals/u-boot.git] / drivers / misc / cros_ec.c
index 1e5bcb0c568e1ea64965535ab782856bb9821ba3..565de040fe9c2c8841c18df913e2e4440bcdeb37 100644 (file)
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Chromium OS cros_ec driver
  *
  * Copyright (c) 2012 The Chromium OS Authors.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
@@ -14,6 +13,8 @@
  * is not reset.
  */
 
+#define LOG_CATEGORY UCLASS_CROS_EC
+
 #include <common.h>
 #include <command.h>
 #include <dm.h>
@@ -26,6 +27,7 @@
 #include <asm/io.h>
 #include <asm-generic/gpio.h>
 #include <dm/device-internal.h>
+#include <dm/of_extra.h>
 #include <dm/uclass-internal.h>
 
 #ifdef DEBUG_TRACE
@@ -41,10 +43,53 @@ enum {
        CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
 };
 
-DECLARE_GLOBAL_DATA_PTR;
+#define INVALID_HCMD 0xFF
 
-/* Note: depends on enum ec_current_image */
-static const char * const ec_current_image_name[] = {"unknown", "RO", "RW"};
+/*
+ * Map UHEPI masks to non UHEPI commands in order to support old EC FW
+ * which does not support UHEPI command.
+ */
+static const struct {
+       u8 set_cmd;
+       u8 clear_cmd;
+       u8 get_cmd;
+} event_map[] = {
+       [EC_HOST_EVENT_MAIN] = {
+               INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
+               INVALID_HCMD,
+       },
+       [EC_HOST_EVENT_B] = {
+               INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
+               EC_CMD_HOST_EVENT_GET_B,
+       },
+       [EC_HOST_EVENT_SCI_MASK] = {
+               EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
+               EC_CMD_HOST_EVENT_GET_SCI_MASK,
+       },
+       [EC_HOST_EVENT_SMI_MASK] = {
+               EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
+               EC_CMD_HOST_EVENT_GET_SMI_MASK,
+       },
+       [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
+               INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
+       },
+       [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
+               EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
+               EC_CMD_HOST_EVENT_GET_WAKE_MASK,
+       },
+       [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
+               EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
+               EC_CMD_HOST_EVENT_GET_WAKE_MASK,
+       },
+       [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
+               EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
+               EC_CMD_HOST_EVENT_GET_WAKE_MASK,
+       },
+       [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
+               EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
+               EC_CMD_HOST_EVENT_GET_WAKE_MASK,
+       },
+};
 
 void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
 {
@@ -88,15 +133,15 @@ int cros_ec_calc_checksum(const uint8_t *data, int size)
  * @param dout_len      Size of output data in bytes
  * @return packet size in bytes, or <0 if error.
  */
-static int create_proto3_request(struct cros_ec_dev *dev,
+static int create_proto3_request(struct cros_ec_dev *cdev,
                                 int cmd, int cmd_version,
                                 const void *dout, int dout_len)
 {
-       struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
+       struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
        int out_bytes = dout_len + sizeof(*rq);
 
        /* Fail if output size is too big */
-       if (out_bytes > (int)sizeof(dev->dout)) {
+       if (out_bytes > (int)sizeof(cdev->dout)) {
                debug("%s: Cannot send %d bytes\n", __func__, dout_len);
                return -EC_RES_REQUEST_TRUNCATED;
        }
@@ -113,9 +158,9 @@ static int create_proto3_request(struct cros_ec_dev *dev,
        memcpy(rq + 1, dout, dout_len);
 
        /* Write checksum field so the entire packet sums to 0 */
-       rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
+       rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
 
-       cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
+       cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
 
        /* Return size of request packet */
        return out_bytes;
@@ -128,12 +173,12 @@ static int create_proto3_request(struct cros_ec_dev *dev,
  * @param din_len       Maximum size of response in bytes
  * @return maximum expected number of bytes in response, or <0 if error.
  */
-static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
+static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
 {
        int in_bytes = din_len + sizeof(struct ec_host_response);
 
        /* Fail if input size is too big */
-       if (in_bytes > (int)sizeof(dev->din)) {
+       if (in_bytes > (int)sizeof(cdev->din)) {
                debug("%s: Cannot receive %d bytes\n", __func__, din_len);
                return -EC_RES_RESPONSE_TOO_BIG;
        }
@@ -202,7 +247,7 @@ static int handle_proto3_response(struct cros_ec_dev *dev,
        return rs->data_len;
 }
 
-static int send_command_proto3(struct cros_ec_dev *dev,
+static int send_command_proto3(struct cros_ec_dev *cdev,
                               int cmd, int cmd_version,
                               const void *dout, int dout_len,
                               uint8_t **dinp, int din_len)
@@ -212,26 +257,27 @@ static int send_command_proto3(struct cros_ec_dev *dev,
        int rv;
 
        /* Create request packet */
-       out_bytes = create_proto3_request(dev, cmd, cmd_version,
+       out_bytes = create_proto3_request(cdev, cmd, cmd_version,
                                          dout, dout_len);
        if (out_bytes < 0)
                return out_bytes;
 
        /* Prepare response buffer */
-       in_bytes = prepare_proto3_response_buffer(dev, din_len);
+       in_bytes = prepare_proto3_response_buffer(cdev, din_len);
        if (in_bytes < 0)
                return in_bytes;
 
-       ops = dm_cros_ec_get_ops(dev->dev);
-       rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
+       ops = dm_cros_ec_get_ops(cdev->dev);
+       rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
+                       -ENOSYS;
        if (rv < 0)
                return rv;
 
        /* Process the response */
-       return handle_proto3_response(dev, dinp, din_len);
+       return handle_proto3_response(cdev, dinp, din_len);
 }
 
-static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
+static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
                        const void *dout, int dout_len,
                        uint8_t **dinp, int din_len)
 {
@@ -267,15 +313,16 @@ static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  * @param din_len       Maximum size of response in bytes
  * @return number of bytes in response, or -ve on error
  */
-static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
-               int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
-               int din_len)
+static int ec_command_inptr(struct udevice *dev, uint8_t cmd,
+                           int cmd_version, const void *dout, int dout_len,
+                           uint8_t **dinp, int din_len)
 {
+       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
        uint8_t *din = NULL;
        int len;
 
-       len = send_command(dev, cmd, cmd_version, dout, dout_len,
-                               &din, din_len);
+       len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
+                          din_len);
 
        /* If the command doesn't complete, wait a while */
        if (len == -EC_RES_IN_PROGRESS) {
@@ -288,9 +335,9 @@ static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
                        int ret;
 
                        mdelay(50);     /* Insert some reasonable delay */
-                       ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
-                                       NULL, 0,
-                                       (uint8_t **)&resp, sizeof(*resp));
+                       ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
+                                          NULL, 0,
+                                          (uint8_t **)&resp, sizeof(*resp));
                        if (ret < 0)
                                return ret;
 
@@ -303,12 +350,11 @@ static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
 
                /* OK it completed, so read the status response */
                /* not sure why it was 0 for the last argument */
-               len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
-                               NULL, 0, &din, din_len);
+               len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
+                                  &din, din_len);
        }
 
-       debug("%s: len=%d, dinp=%p, *dinp=%p\n", __func__, len, dinp,
-             dinp ? *dinp : NULL);
+       debug("%s: len=%d, din=%p\n", __func__, len, din);
        if (dinp) {
                /* If we have any data to return, it must be 64bit-aligned */
                assert(len <= 0 || !((uintptr_t)din & 7));
@@ -334,7 +380,7 @@ static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
  * @param din_len       Maximum size of response in bytes
  * @return number of bytes in response, or -ve on error
  */
-static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
+static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
                      const void *dout, int dout_len,
                      void *din, int din_len)
 {
@@ -343,7 +389,7 @@ static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 
        assert((din_len == 0) || din);
        len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
-                       &in_buffer, din_len);
+                              &in_buffer, din_len);
        if (len > 0) {
                /*
                 * If we were asked to put it somewhere, do so, otherwise just
@@ -359,22 +405,24 @@ static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 
 int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
 {
-       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
-
-       if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
+       if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
                       sizeof(scan->data)) != sizeof(scan->data))
                return -1;
 
        return 0;
 }
 
-int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
+int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
 {
        struct ec_response_get_version *r;
+       int ret;
 
-       if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
-                       (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
+       ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
+                              (uint8_t **)&r, sizeof(*r));
+       if (ret != sizeof(*r)) {
+               log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
                return -1;
+       }
 
        if (maxlen > (int)sizeof(r->version_string_ro))
                maxlen = sizeof(r->version_string_ro);
@@ -387,6 +435,7 @@ int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
                memcpy(id, r->version_string_rw, maxlen);
                break;
        default:
+               log_err("Invalid EC image %d\n", r->current_image);
                return -1;
        }
 
@@ -394,8 +443,8 @@ int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
        return 0;
 }
 
-int cros_ec_read_version(struct cros_ec_dev *dev,
-                      struct ec_response_get_version **versionp)
+int cros_ec_read_version(struct udevice *dev,
+                        struct ec_response_get_version **versionp)
 {
        if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
                        (uint8_t **)versionp, sizeof(**versionp))
@@ -405,7 +454,7 @@ int cros_ec_read_version(struct cros_ec_dev *dev,
        return 0;
 }
 
-int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
+int cros_ec_read_build_info(struct udevice *dev, char **strp)
 {
        if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
                        (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
@@ -414,8 +463,8 @@ int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
        return 0;
 }
 
-int cros_ec_read_current_image(struct cros_ec_dev *dev,
-               enum ec_current_image *image)
+int cros_ec_read_current_image(struct udevice *dev,
+                              enum ec_current_image *image)
 {
        struct ec_response_get_version *r;
 
@@ -427,8 +476,8 @@ int cros_ec_read_current_image(struct cros_ec_dev *dev,
        return 0;
 }
 
-static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
-                                 struct ec_response_vboot_hash *hash)
+static int cros_ec_wait_on_hash_done(struct udevice *dev,
+                                    struct ec_response_vboot_hash *hash)
 {
        struct ec_params_vboot_hash p;
        ulong start;
@@ -450,14 +499,14 @@ static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
        return 0;
 }
 
-
-int cros_ec_read_hash(struct cros_ec_dev *dev,
-               struct ec_response_vboot_hash *hash)
+int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
+                     struct ec_response_vboot_hash *hash)
 {
        struct ec_params_vboot_hash p;
        int rv;
 
        p.cmd = EC_VBOOT_HASH_GET;
+       p.offset = hash_offset;
        if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
                       hash, sizeof(*hash)) < 0)
                return -1;
@@ -480,7 +529,7 @@ int cros_ec_read_hash(struct cros_ec_dev *dev,
        p.cmd = EC_VBOOT_HASH_START;
        p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
        p.nonce_size = 0;
-       p.offset = EC_VBOOT_HASH_OFFSET_RW;
+       p.offset = hash_offset;
 
        if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
                       hash, sizeof(*hash)) < 0)
@@ -495,7 +544,7 @@ int cros_ec_read_hash(struct cros_ec_dev *dev,
        return 0;
 }
 
-static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
+static int cros_ec_invalidate_hash(struct udevice *dev)
 {
        struct ec_params_vboot_hash p;
        struct ec_response_vboot_hash *hash;
@@ -520,8 +569,7 @@ static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
        return 0;
 }
 
-int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
-               uint8_t flags)
+int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
 {
        struct ec_params_reboot_ec p;
 
@@ -561,7 +609,7 @@ int cros_ec_interrupt_pending(struct udevice *dev)
        return dm_gpio_get_value(&cdev->ec_int);
 }
 
-int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
+int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
 {
        if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
                       sizeof(*info)) != sizeof(*info))
@@ -570,7 +618,37 @@ int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
        return 0;
 }
 
-int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
+int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
+{
+       struct ec_response_host_event_mask rsp;
+       int ret;
+
+       ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
+       if (ret < 0)
+               return ret;
+       else if (ret != sizeof(rsp))
+               return -EINVAL;
+
+       *mask = rsp.mask;
+
+       return 0;
+}
+
+int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
+{
+       struct ec_params_host_event_mask req;
+       int ret;
+
+       req.mask = mask;
+
+       ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
 {
        struct ec_response_host_event_mask *resp;
 
@@ -589,7 +667,7 @@ int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
        return 0;
 }
 
-int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
+int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
 {
        struct ec_params_host_event_mask params;
 
@@ -606,9 +684,9 @@ int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
        return 0;
 }
 
-int cros_ec_flash_protect(struct cros_ec_dev *dev,
-                      uint32_t set_mask, uint32_t set_flags,
-                      struct ec_response_flash_protect *resp)
+int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
+                         uint32_t set_flags,
+                         struct ec_response_flash_protect *resp)
 {
        struct ec_params_flash_protect params;
 
@@ -623,17 +701,29 @@ int cros_ec_flash_protect(struct cros_ec_dev *dev,
        return 0;
 }
 
-static int cros_ec_check_version(struct cros_ec_dev *dev)
+int cros_ec_entering_mode(struct udevice *dev, int mode)
 {
+       int rc;
+
+       rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
+                       NULL, 0);
+       if (rc)
+               return -1;
+       return 0;
+}
+
+static int cros_ec_check_version(struct udevice *dev)
+{
+       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
        struct ec_params_hello req;
        struct ec_response_hello *resp;
 
        struct dm_cros_ec_ops *ops;
        int ret;
 
-       ops = dm_cros_ec_get_ops(dev->dev);
+       ops = dm_cros_ec_get_ops(dev);
        if (ops->check_version) {
-               ret = ops->check_version(dev->dev);
+               ret = ops->check_version(dev);
                if (ret)
                        return ret;
        }
@@ -653,19 +743,17 @@ static int cros_ec_check_version(struct cros_ec_dev *dev)
         */
 
        /* Try sending a version 3 packet */
-       dev->protocol_version = 3;
+       cdev->protocol_version = 3;
        req.in_data = 0;
        if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
-                            (uint8_t **)&resp, sizeof(*resp)) > 0) {
+                            (uint8_t **)&resp, sizeof(*resp)) > 0)
                return 0;
-       }
 
        /* Try sending a version 2 packet */
-       dev->protocol_version = 2;
+       cdev->protocol_version = 2;
        if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
-                      (uint8_t **)&resp, sizeof(*resp)) > 0) {
+                            (uint8_t **)&resp, sizeof(*resp)) > 0)
                return 0;
-       }
 
        /*
         * Fail if we're still here, since the EC doesn't understand any
@@ -673,12 +761,12 @@ static int cros_ec_check_version(struct cros_ec_dev *dev)
         * version is no longer supported, and we don't know about any new
         * protocol versions.
         */
-       dev->protocol_version = 0;
+       cdev->protocol_version = 0;
        printf("%s: ERROR: old EC interface not supported\n", __func__);
        return -1;
 }
 
-int cros_ec_test(struct cros_ec_dev *dev)
+int cros_ec_test(struct udevice *dev)
 {
        struct ec_params_hello req;
        struct ec_response_hello *resp;
@@ -697,7 +785,7 @@ int cros_ec_test(struct cros_ec_dev *dev)
        return 0;
 }
 
-int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
+int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
                      uint32_t *offset, uint32_t *size)
 {
        struct ec_params_flash_region_info p;
@@ -719,7 +807,7 @@ int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
        return 0;
 }
 
-int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
+int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
 {
        struct ec_params_flash_erase p;
 
@@ -747,8 +835,8 @@ int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
  * @param size         Number of bytes to write
  * @return 0 if ok, -1 on error
  */
-static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
-               const uint8_t *data, uint32_t offset, uint32_t size)
+static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
+                                    uint32_t offset, uint32_t size)
 {
        struct ec_params_flash_write *p;
        int ret;
@@ -773,7 +861,7 @@ static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
 /**
  * Return optimal flash write burst size
  */
-static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
+static int cros_ec_flash_write_burst_size(struct udevice *dev)
 {
        return EC_FLASH_WRITE_VER0_SIZE;
 }
@@ -807,8 +895,8 @@ static int cros_ec_data_is_erased(const uint32_t *data, int size)
  * @param dev  Pointer to device
  * @param info Pointer to output flash info struct
  */
-int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
-                         struct ec_response_flash_info *info)
+int cros_ec_read_flashinfo(struct udevice *dev,
+                          struct ec_response_flash_info *info)
 {
        int ret;
 
@@ -820,13 +908,17 @@ int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
        return ret < sizeof(*info) ? -1 : 0;
 }
 
-int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
-                    uint32_t offset, uint32_t size)
+int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
+                       uint32_t offset, uint32_t size)
 {
+       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
        uint32_t burst = cros_ec_flash_write_burst_size(dev);
        uint32_t end, off;
        int ret;
 
+       if (!burst)
+               return -EINVAL;
+
        /*
         * TODO: round up to the nearest multiple of write size.  Can get away
         * without that on link right now because its write size is 4 bytes.
@@ -837,8 +929,8 @@ int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
 
                /* If the data is empty, there is no point in programming it */
                todo = min(end - off, burst);
-               if (dev->optimise_flash_write &&
-                               cros_ec_data_is_erased((uint32_t *)data, todo))
+               if (cdev->optimise_flash_write &&
+                   cros_ec_data_is_erased((uint32_t *)data, todo))
                        continue;
 
                ret = cros_ec_flash_write_block(dev, data, off, todo);
@@ -849,6 +941,35 @@ int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
        return 0;
 }
 
+/**
+ * Run verification on a slot
+ *
+ * @param me     CrosEc instance
+ * @param region Region to run verification on
+ * @return 0 if success or not applicable. Non-zero if verification failed.
+ */
+int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
+{
+       struct ec_params_efs_verify p;
+       int rv;
+
+       log_info("EFS: EC is verifying updated image...\n");
+       p.region = region;
+
+       rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
+       if (rv >= 0) {
+               log_info("EFS: Verification success\n");
+               return 0;
+       }
+       if (rv == -EC_RES_INVALID_COMMAND) {
+               log_info("EFS: EC doesn't support EFS_VERIFY command\n");
+               return 0;
+       }
+       log_info("EFS: Verification failed\n");
+
+       return rv;
+}
+
 /**
  * Read a single block from the flash
  *
@@ -864,8 +985,8 @@ int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
  * @param size         Number of bytes to read
  * @return 0 if ok, -1 on error
  */
-static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
-                                uint32_t offset, uint32_t size)
+static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
+                                   uint32_t offset, uint32_t size)
 {
        struct ec_params_flash_read p;
 
@@ -876,8 +997,8 @@ static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
                          &p, sizeof(p), data, size) >= 0 ? 0 : -1;
 }
 
-int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
-                   uint32_t size)
+int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
+                      uint32_t size)
 {
        uint32_t burst = cros_ec_flash_write_burst_size(dev);
        uint32_t end, off;
@@ -894,13 +1015,14 @@ int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
        return 0;
 }
 
-int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
-                        const uint8_t *image, int image_size)
+int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
+                           int image_size)
 {
        uint32_t rw_offset, rw_size;
        int ret;
 
-       if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
+       if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
+               &rw_size))
                return -1;
        if (image_size > (int)rw_size)
                return -1;
@@ -933,46 +1055,66 @@ int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
        return 0;
 }
 
-int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
+int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
 {
        struct ec_params_vbnvcontext p;
        int len;
 
+       if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
+               return -EINVAL;
+
        p.op = EC_VBNV_CONTEXT_OP_READ;
 
        len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
-                       &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
-       if (len < EC_VBNV_BLOCK_SIZE)
-               return -1;
+                        &p, sizeof(uint32_t) + size, block, size);
+       if (len != size) {
+               log_err("Expected %d bytes, got %d\n", size, len);
+               return -EIO;
+       }
 
        return 0;
 }
 
-int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
+int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
 {
        struct ec_params_vbnvcontext p;
        int len;
 
+       if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
+               return -EINVAL;
        p.op = EC_VBNV_CONTEXT_OP_WRITE;
-       memcpy(p.block, block, sizeof(p.block));
+       memcpy(p.block, block, size);
 
        len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
-                       &p, sizeof(p), NULL, 0);
+                       &p, sizeof(uint32_t) + size, NULL, 0);
        if (len < 0)
                return -1;
 
        return 0;
 }
 
+int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
+{
+       struct ec_params_battery_cutoff p;
+       int len;
+
+       p.flags = flags;
+       len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
+                        NULL, 0);
+
+       if (len < 0)
+               return -1;
+       return 0;
+}
+
 int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
 {
-       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
        struct ec_params_ldo_set params;
 
        params.index = index;
        params.state = state;
 
-       if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
+       if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, &params, sizeof(params),
                             NULL, 0))
                return -1;
 
@@ -981,13 +1123,12 @@ int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
 
 int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
 {
-       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
        struct ec_params_ldo_get params;
        struct ec_response_ldo_get *resp;
 
        params.index = index;
 
-       if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
+       if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, &params, sizeof(params),
                             (uint8_t **)&resp, sizeof(*resp)) !=
                             sizeof(*resp))
                return -1;
@@ -1000,22 +1141,19 @@ int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
 int cros_ec_register(struct udevice *dev)
 {
        struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
-       const void *blob = gd->fdt_blob;
-       int node = dev->of_offset;
        char id[MSG_BYTES];
 
        cdev->dev = dev;
        gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
                             GPIOD_IS_IN);
-       cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
-                                                    "optimise-flash-write");
+       cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
 
-       if (cros_ec_check_version(cdev)) {
+       if (cros_ec_check_version(dev)) {
                debug("%s: Could not detect CROS-EC version\n", __func__);
                return -CROS_EC_ERR_CHECK_VERSION;
        }
 
-       if (cros_ec_read_id(cdev, id, sizeof(id))) {
+       if (cros_ec_read_id(dev, id, sizeof(id))) {
                debug("%s: Could not read KBC ID\n", __func__);
                return -CROS_EC_ERR_READ_ID;
        }
@@ -1027,50 +1165,31 @@ int cros_ec_register(struct udevice *dev)
        return 0;
 }
 
-int cros_ec_decode_region(int argc, char * const argv[])
+int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
 {
-       if (argc > 0) {
-               if (0 == strcmp(*argv, "rw"))
-                       return EC_FLASH_REGION_RW;
-               else if (0 == strcmp(*argv, "ro"))
-                       return EC_FLASH_REGION_RO;
-
-               debug("%s: Invalid region '%s'\n", __func__, *argv);
-       } else {
-               debug("%s: Missing region parameter\n", __func__);
-       }
-
-       return -1;
-}
+       ofnode flash_node, node;
 
-int cros_ec_decode_ec_flash(const void *blob, int node,
-                           struct fdt_cros_ec *config)
-{
-       int flash_node;
-
-       flash_node = fdt_subnode_offset(blob, node, "flash");
-       if (flash_node < 0) {
+       flash_node = dev_read_subnode(dev, "flash");
+       if (!ofnode_valid(flash_node)) {
                debug("Failed to find flash node\n");
                return -1;
        }
 
-       if (fdtdec_read_fmap_entry(blob, flash_node, "flash",
-                                  &config->flash)) {
-               debug("Failed to decode flash node in chrome-ec'\n");
+       if (ofnode_read_fmap_entry(flash_node,  &config->flash)) {
+               debug("Failed to decode flash node in chrome-ec\n");
                return -1;
        }
 
-       config->flash_erase_value = fdtdec_get_int(blob, flash_node,
-                                                   "erase-value", -1);
-       for (node = fdt_first_subnode(blob, flash_node); node >= 0;
-            node = fdt_next_subnode(blob, node)) {
-               const char *name = fdt_get_name(blob, node, NULL);
+       config->flash_erase_value = ofnode_read_s32_default(flash_node,
+                                                           "erase-value", -1);
+       ofnode_for_each_subnode(node, flash_node) {
+               const char *name = ofnode_get_name(node);
                enum ec_flash_region region;
 
                if (0 == strcmp(name, "ro")) {
                        region = EC_FLASH_REGION_RO;
                } else if (0 == strcmp(name, "rw")) {
-                       region = EC_FLASH_REGION_RW;
+                       region = EC_FLASH_REGION_ACTIVE;
                } else if (0 == strcmp(name, "wp-ro")) {
                        region = EC_FLASH_REGION_WP_RO;
                } else {
@@ -1078,8 +1197,7 @@ int cros_ec_decode_ec_flash(const void *blob, int node,
                        return -1;
                }
 
-               if (fdtdec_read_fmap_entry(blob, node, "reg",
-                                          &config->region[region])) {
+               if (ofnode_read_fmap_entry(node, &config->region[region])) {
                        debug("Failed to decode flash region in chrome-ec'\n");
                        return -1;
                }
@@ -1091,7 +1209,6 @@ int cros_ec_decode_ec_flash(const void *blob, int node,
 int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
                       int nmsgs)
 {
-       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
        union {
                struct ec_params_i2c_passthru p;
                uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
@@ -1141,7 +1258,7 @@ int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
                }
        }
 
-       rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
+       rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
                        r, sizeof(*r) + read_len);
        if (rv < 0)
                return rv;
@@ -1164,357 +1281,209 @@ int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
        return 0;
 }
 
-#ifdef CONFIG_CMD_CROS_EC
+int cros_ec_check_feature(struct udevice *dev, int feature)
+{
+       struct ec_response_get_features r;
+       int rv;
 
-/**
- * Perform a flash read or write command
- *
- * @param dev          CROS-EC device to read/write
- * @param is_write     1 do to a write, 0 to do a read
- * @param argc         Number of arguments
- * @param argv         Arguments (2 is region, 3 is address)
- * @return 0 for ok, 1 for a usage error or -ve for ec command error
- *     (negative EC_RES_...)
+       rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
+       if (rv)
+               return rv;
+
+       if (feature >= 8 * sizeof(r.flags))
+               return -1;
+
+       return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
+}
+
+/*
+ * Query the EC for specified mask indicating enabled events.
+ * The EC maintains separate event masks for SMI, SCI and WAKE.
  */
-static int do_read_write(struct cros_ec_dev *dev, int is_write, int argc,
-                        char * const argv[])
+static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
+                            uint64_t *value)
 {
-       uint32_t offset, size = -1U, region_size;
-       unsigned long addr;
-       char *endp;
-       int region;
        int ret;
+       struct ec_params_host_event req;
+       struct ec_response_host_event rsp;
+
+       req.action = action;
+       req.mask_type = mask;
+       if (action != EC_HOST_EVENT_GET)
+               req.value = *value;
+       else
+               *value = 0;
+       ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
+                        sizeof(rsp));
+
+       if (action != EC_HOST_EVENT_GET)
+               return ret;
+       if (ret == 0)
+               *value = rsp.value;
 
-       region = cros_ec_decode_region(argc - 2, argv + 2);
-       if (region == -1)
-               return 1;
-       if (argc < 4)
-               return 1;
-       addr = simple_strtoul(argv[3], &endp, 16);
-       if (*argv[3] == 0 || *endp != 0)
-               return 1;
-       if (argc > 4) {
-               size = simple_strtoul(argv[4], &endp, 16);
-               if (*argv[4] == 0 || *endp != 0)
-                       return 1;
-       }
+       return ret;
+}
+
+static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
+                                       uint action, uint64_t *value)
+{
+       int ret = -1;
+       struct ec_params_host_event_mask req;
+       struct ec_response_host_event_mask rsp;
 
-       ret = cros_ec_flash_offset(dev, region, &offset, &region_size);
-       if (ret) {
-               debug("%s: Could not read region info\n", __func__);
+       if (hcmd == INVALID_HCMD)
                return ret;
-       }
-       if (size == -1U)
-               size = region_size;
-
-       ret = is_write ?
-               cros_ec_flash_write(dev, (uint8_t *)addr, offset, size) :
-               cros_ec_flash_read(dev, (uint8_t *)addr, offset, size);
-       if (ret) {
-               debug("%s: Could not %s region\n", __func__,
-                     is_write ? "write" : "read");
+
+       if (action != EC_HOST_EVENT_GET)
+               req.mask = (uint32_t)*value;
+       else
+               *value = 0;
+
+       ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
+       if (action != EC_HOST_EVENT_GET)
                return ret;
-       }
+       if (ret == 0)
+               *value = rsp.mask;
 
-       return 0;
+       return ret;
 }
 
-static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+bool cros_ec_is_uhepi_supported(struct udevice *dev)
 {
-       struct cros_ec_dev *dev;
-       struct udevice *udev;
-       const char *cmd;
-       int ret = 0;
-
-       if (argc < 2)
-               return CMD_RET_USAGE;
-
-       cmd = argv[1];
-       if (0 == strcmp("init", cmd)) {
-               /* Remove any existing device */
-               ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
-               if (!ret)
-                       device_remove(udev);
-               ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
-               if (ret) {
-                       printf("Could not init cros_ec device (err %d)\n", ret);
-                       return 1;
-               }
-               return 0;
+#define UHEPI_SUPPORTED 1
+#define UHEPI_NOT_SUPPORTED 2
+       static int uhepi_support;
+
+       if (!uhepi_support) {
+               uhepi_support = cros_ec_check_feature(dev,
+                       EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
+                       UHEPI_NOT_SUPPORTED;
+               log_debug("Chrome EC: UHEPI %s\n",
+                         uhepi_support == UHEPI_SUPPORTED ? "supported" :
+                         "not supported");
        }
+       return uhepi_support == UHEPI_SUPPORTED;
+}
+
+static int cros_ec_get_mask(struct udevice *dev, uint type)
+{
+       u64 value = 0;
 
-       ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
-       if (ret) {
-               printf("Cannot get cros-ec device (err=%d)\n", ret);
-               return 1;
+       if (cros_ec_is_uhepi_supported(dev)) {
+               cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
+       } else {
+               assert(type < ARRAY_SIZE(event_map));
+               cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
+                                            EC_HOST_EVENT_GET, &value);
        }
-       dev = dev_get_uclass_priv(udev);
-       if (0 == strcmp("id", cmd)) {
-               char id[MSG_BYTES];
+       return value;
+}
 
-               if (cros_ec_read_id(dev, id, sizeof(id))) {
-                       debug("%s: Could not read KBC ID\n", __func__);
-                       return 1;
-               }
-               printf("%s\n", id);
-       } else if (0 == strcmp("info", cmd)) {
-               struct ec_response_mkbp_info info;
+static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
+{
+       if (cros_ec_is_uhepi_supported(dev))
+               return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
 
-               if (cros_ec_info(dev, &info)) {
-                       debug("%s: Could not read KBC info\n", __func__);
-                       return 1;
-               }
-               printf("rows     = %u\n", info.rows);
-               printf("cols     = %u\n", info.cols);
-               printf("switches = %#x\n", info.switches);
-       } else if (0 == strcmp("curimage", cmd)) {
-               enum ec_current_image image;
-
-               if (cros_ec_read_current_image(dev, &image)) {
-                       debug("%s: Could not read KBC image\n", __func__);
-                       return 1;
-               }
-               printf("%d\n", image);
-       } else if (0 == strcmp("hash", cmd)) {
-               struct ec_response_vboot_hash hash;
-               int i;
-
-               if (cros_ec_read_hash(dev, &hash)) {
-                       debug("%s: Could not read KBC hash\n", __func__);
-                       return 1;
-               }
+       assert(type < ARRAY_SIZE(event_map));
 
-               if (hash.hash_type == EC_VBOOT_HASH_TYPE_SHA256)
-                       printf("type:    SHA-256\n");
-               else
-                       printf("type:    %d\n", hash.hash_type);
-
-               printf("offset:  0x%08x\n", hash.offset);
-               printf("size:    0x%08x\n", hash.size);
-
-               printf("digest:  ");
-               for (i = 0; i < hash.digest_size; i++)
-                       printf("%02x", hash.hash_digest[i]);
-               printf("\n");
-       } else if (0 == strcmp("reboot", cmd)) {
-               int region;
-               enum ec_reboot_cmd cmd;
-
-               if (argc >= 3 && !strcmp(argv[2], "cold"))
-                       cmd = EC_REBOOT_COLD;
-               else {
-                       region = cros_ec_decode_region(argc - 2, argv + 2);
-                       if (region == EC_FLASH_REGION_RO)
-                               cmd = EC_REBOOT_JUMP_RO;
-                       else if (region == EC_FLASH_REGION_RW)
-                               cmd = EC_REBOOT_JUMP_RW;
-                       else
-                               return CMD_RET_USAGE;
-               }
+       return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
+                                           EC_HOST_EVENT_CLEAR, &mask);
+}
 
-               if (cros_ec_reboot(dev, cmd, 0)) {
-                       debug("%s: Could not reboot KBC\n", __func__);
-                       return 1;
-               }
-       } else if (0 == strcmp("events", cmd)) {
-               uint32_t events;
+uint64_t cros_ec_get_events_b(struct udevice *dev)
+{
+       return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
+}
 
-               if (cros_ec_get_host_events(dev, &events)) {
-                       debug("%s: Could not read host events\n", __func__);
-                       return 1;
-               }
-               printf("0x%08x\n", events);
-       } else if (0 == strcmp("clrevents", cmd)) {
-               uint32_t events = 0x7fffffff;
+int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
+{
+       log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
 
-               if (argc >= 3)
-                       events = simple_strtol(argv[2], NULL, 0);
+       return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
+}
 
-               if (cros_ec_clear_host_events(dev, events)) {
-                       debug("%s: Could not clear host events\n", __func__);
-                       return 1;
-               }
-       } else if (0 == strcmp("read", cmd)) {
-               ret = do_read_write(dev, 0, argc, argv);
-               if (ret > 0)
-                       return CMD_RET_USAGE;
-       } else if (0 == strcmp("write", cmd)) {
-               ret = do_read_write(dev, 1, argc, argv);
-               if (ret > 0)
-                       return CMD_RET_USAGE;
-       } else if (0 == strcmp("erase", cmd)) {
-               int region = cros_ec_decode_region(argc - 2, argv + 2);
-               uint32_t offset, size;
-
-               if (region == -1)
-                       return CMD_RET_USAGE;
-               if (cros_ec_flash_offset(dev, region, &offset, &size)) {
-                       debug("%s: Could not read region info\n", __func__);
-                       ret = -1;
-               } else {
-                       ret = cros_ec_flash_erase(dev, offset, size);
-                       if (ret) {
-                               debug("%s: Could not erase region\n",
-                                     __func__);
-                       }
-               }
-       } else if (0 == strcmp("regioninfo", cmd)) {
-               int region = cros_ec_decode_region(argc - 2, argv + 2);
-               uint32_t offset, size;
-
-               if (region == -1)
-                       return CMD_RET_USAGE;
-               ret = cros_ec_flash_offset(dev, region, &offset, &size);
-               if (ret) {
-                       debug("%s: Could not read region info\n", __func__);
-               } else {
-                       printf("Region: %s\n", region == EC_FLASH_REGION_RO ?
-                                       "RO" : "RW");
-                       printf("Offset: %x\n", offset);
-                       printf("Size:   %x\n", size);
-               }
-       } else if (0 == strcmp("flashinfo", cmd)) {
-               struct ec_response_flash_info p;
-
-               ret = cros_ec_read_flashinfo(dev, &p);
-               if (!ret) {
-                       printf("Flash size:         %u\n", p.flash_size);
-                       printf("Write block size:   %u\n", p.write_block_size);
-                       printf("Erase block size:   %u\n", p.erase_block_size);
-               }
-       } else if (0 == strcmp("vbnvcontext", cmd)) {
-               uint8_t block[EC_VBNV_BLOCK_SIZE];
-               char buf[3];
-               int i, len;
-               unsigned long result;
-
-               if (argc <= 2) {
-                       ret = cros_ec_read_vbnvcontext(dev, block);
-                       if (!ret) {
-                               printf("vbnv_block: ");
-                               for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++)
-                                       printf("%02x", block[i]);
-                               putc('\n');
-                       }
-               } else {
-                       /*
-                        * TODO(clchiou): Move this to a utility function as
-                        * cmd_spi might want to call it.
-                        */
-                       memset(block, 0, EC_VBNV_BLOCK_SIZE);
-                       len = strlen(argv[2]);
-                       buf[2] = '\0';
-                       for (i = 0; i < EC_VBNV_BLOCK_SIZE; i++) {
-                               if (i * 2 >= len)
-                                       break;
-                               buf[0] = argv[2][i * 2];
-                               if (i * 2 + 1 >= len)
-                                       buf[1] = '0';
-                               else
-                                       buf[1] = argv[2][i * 2 + 1];
-                               strict_strtoul(buf, 16, &result);
-                               block[i] = result;
-                       }
-                       ret = cros_ec_write_vbnvcontext(dev, block);
-               }
-               if (ret) {
-                       debug("%s: Could not %s VbNvContext\n", __func__,
-                                       argc <= 2 ?  "read" : "write");
-               }
-       } else if (0 == strcmp("test", cmd)) {
-               int result = cros_ec_test(dev);
-
-               if (result)
-                       printf("Test failed with error %d\n", result);
-               else
-                       puts("Test passed\n");
-       } else if (0 == strcmp("version", cmd)) {
-               struct ec_response_get_version *p;
-               char *build_string;
-
-               ret = cros_ec_read_version(dev, &p);
-               if (!ret) {
-                       /* Print versions */
-                       printf("RO version:    %1.*s\n",
-                              (int)sizeof(p->version_string_ro),
-                              p->version_string_ro);
-                       printf("RW version:    %1.*s\n",
-                              (int)sizeof(p->version_string_rw),
-                              p->version_string_rw);
-                       printf("Firmware copy: %s\n",
-                               (p->current_image <
-                                       ARRAY_SIZE(ec_current_image_name) ?
-                               ec_current_image_name[p->current_image] :
-                               "?"));
-                       ret = cros_ec_read_build_info(dev, &build_string);
-                       if (!ret)
-                               printf("Build info:    %s\n", build_string);
-               }
-       } else if (0 == strcmp("ldo", cmd)) {
-               uint8_t index, state;
-               char *endp;
-
-               if (argc < 3)
-                       return CMD_RET_USAGE;
-               index = simple_strtoul(argv[2], &endp, 10);
-               if (*argv[2] == 0 || *endp != 0)
-                       return CMD_RET_USAGE;
-               if (argc > 3) {
-                       state = simple_strtoul(argv[3], &endp, 10);
-                       if (*argv[3] == 0 || *endp != 0)
-                               return CMD_RET_USAGE;
-                       ret = cros_ec_set_ldo(udev, index, state);
-               } else {
-                       ret = cros_ec_get_ldo(udev, index, &state);
-                       if (!ret) {
-                               printf("LDO%d: %s\n", index,
-                                       state == EC_LDO_STATE_ON ?
-                                       "on" : "off");
-                       }
-               }
+int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
+{
+       struct ec_params_charge_state p;
+       struct ec_response_charge_state r;
+       int ret;
 
-               if (ret) {
-                       debug("%s: Could not access LDO%d\n", __func__, index);
-                       return ret;
-               }
-       } else {
-               return CMD_RET_USAGE;
-       }
+       p.cmd = CHARGE_STATE_CMD_GET_PARAM;
+       p.get_param.param = CS_PARAM_LIMIT_POWER;
+       ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
+                        &r, sizeof(r));
 
-       if (ret < 0) {
-               printf("Error: CROS-EC command failed (error %d)\n", ret);
-               ret = 1;
+       /*
+        * If our EC doesn't support the LIMIT_POWER parameter, assume that
+        * LIMIT_POWER is not requested.
+        */
+       if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
+               log_warning("PARAM_LIMIT_POWER not supported by EC\n");
+               return -ENOSYS;
        }
 
-       return ret;
+       if (ret != sizeof(r.get_param))
+               return -EINVAL;
+
+       *limit_powerp = r.get_param.value;
+       return 0;
 }
 
-U_BOOT_CMD(
-       crosec, 6,      1,      do_cros_ec,
-       "CROS-EC utility command",
-       "init                Re-init CROS-EC (done on startup automatically)\n"
-       "crosec id                  Read CROS-EC ID\n"
-       "crosec info                Read CROS-EC info\n"
-       "crosec curimage            Read CROS-EC current image\n"
-       "crosec hash                Read CROS-EC hash\n"
-       "crosec reboot [rw | ro | cold]  Reboot CROS-EC\n"
-       "crosec events              Read CROS-EC host events\n"
-       "crosec clrevents [mask]    Clear CROS-EC host events\n"
-       "crosec regioninfo <ro|rw>  Read image info\n"
-       "crosec flashinfo           Read flash info\n"
-       "crosec erase <ro|rw>       Erase EC image\n"
-       "crosec read <ro|rw> <addr> [<size>]   Read EC image\n"
-       "crosec write <ro|rw> <addr> [<size>]  Write EC image\n"
-       "crosec vbnvcontext [hexstring]        Read [write] VbNvContext from EC\n"
-       "crosec ldo <idx> [<state>] Switch/Read LDO state\n"
-       "crosec test                run tests on cros_ec\n"
-       "crosec version             Read CROS-EC version"
-);
-#endif
+int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
+{
+       struct ec_params_config_power_button params;
+       int ret;
+
+       params.flags = flags;
+       ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
+                        &params, sizeof(params), NULL, 0);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
+{
+       u32 mask;
+       int ret;
+
+       ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
+                                    &mask);
+       if (ret < 0)
+               return ret;
+
+       return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
+}
+
+int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
+{
+       u32 mask;
+       int ret;
+
+       ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
+                                    &mask);
+       if (ret < 0)
+               return ret;
+
+       /* Set lid close event state in the EC SMI event mask */
+       if (enable)
+               mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
+       else
+               mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
+
+       ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
+       if (ret < 0)
+               return ret;
+
+       printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
+       return 0;
+}
 
 UCLASS_DRIVER(cros_ec) = {
        .id             = UCLASS_CROS_EC,
        .name           = "cros_ec",
        .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
        .post_bind      = dm_scan_fdt_dev,
+       .flags          = DM_UC_FLAG_ALLOC_PRIV_DMA,
 };