]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/misc/cros_ec.c
cros_ec: spi: Add support for EC protocol version 3
[karo-tx-uboot.git] / drivers / misc / cros_ec.c
index 301e8ebbf57babe28ff1de3b218ceab7b11c0024..5682d39eca8910f3767e79aa267cbd6dc57c525b 100644 (file)
@@ -7,10 +7,11 @@
  */
 
 /*
- * The Matrix Keyboard Protocol driver handles talking to the keyboard
- * controller chip. Mostly this is for keyboard functions, but some other
- * things have slipped in, so we provide generic services to talk to the
- * KBC.
+ * This is the interface to the Chrome OS EC. It provides keyboard functions,
+ * power control and battery management. Quite a few other functions are
+ * provided to enable the EC software to be updated, talk to the EC's I2C bus
+ * and store a small amount of data in a memory which persists while the EC
+ * is not reset.
  */
 
 #include <common.h>
@@ -73,11 +74,179 @@ int cros_ec_calc_checksum(const uint8_t *data, int size)
        return csum & 0xff;
 }
 
+/**
+ * Create a request packet for protocol version 3.
+ *
+ * The packet is stored in the device's internal output buffer.
+ *
+ * @param dev          CROS-EC device
+ * @param cmd          Command to send (EC_CMD_...)
+ * @param cmd_version  Version of command to send (EC_VER_...)
+ * @param dout          Output data (may be NULL If dout_len=0)
+ * @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,
+                                int cmd, int cmd_version,
+                                const void *dout, int dout_len)
+{
+       struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
+       int out_bytes = dout_len + sizeof(*rq);
+
+       /* Fail if output size is too big */
+       if (out_bytes > (int)sizeof(dev->dout)) {
+               debug("%s: Cannot send %d bytes\n", __func__, dout_len);
+               return -EC_RES_REQUEST_TRUNCATED;
+       }
+
+       /* Fill in request packet */
+       rq->struct_version = EC_HOST_REQUEST_VERSION;
+       rq->checksum = 0;
+       rq->command = cmd;
+       rq->command_version = cmd_version;
+       rq->reserved = 0;
+       rq->data_len = dout_len;
+
+       /* Copy data after header */
+       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));
+
+       cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
+
+       /* Return size of request packet */
+       return out_bytes;
+}
+
+/**
+ * Prepare the device to receive a protocol version 3 response.
+ *
+ * @param dev          CROS-EC device
+ * @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)
+{
+       int in_bytes = din_len + sizeof(struct ec_host_response);
+
+       /* Fail if input size is too big */
+       if (in_bytes > (int)sizeof(dev->din)) {
+               debug("%s: Cannot receive %d bytes\n", __func__, din_len);
+               return -EC_RES_RESPONSE_TOO_BIG;
+       }
+
+       /* Return expected size of response packet */
+       return in_bytes;
+}
+
+/**
+ * Handle a protocol version 3 response packet.
+ *
+ * The packet must already be stored in the device's internal input buffer.
+ *
+ * @param dev          CROS-EC device
+ * @param dinp          Returns pointer to response data
+ * @param din_len       Maximum size of response in bytes
+ * @return number of bytes of response data, or <0 if error
+ */
+static int handle_proto3_response(struct cros_ec_dev *dev,
+                                 uint8_t **dinp, int din_len)
+{
+       struct ec_host_response *rs = (struct ec_host_response *)dev->din;
+       int in_bytes;
+       int csum;
+
+       cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
+
+       /* Check input data */
+       if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
+               debug("%s: EC response version mismatch\n", __func__);
+               return -EC_RES_INVALID_RESPONSE;
+       }
+
+       if (rs->reserved) {
+               debug("%s: EC response reserved != 0\n", __func__);
+               return -EC_RES_INVALID_RESPONSE;
+       }
+
+       if (rs->data_len > din_len) {
+               debug("%s: EC returned too much data\n", __func__);
+               return -EC_RES_RESPONSE_TOO_BIG;
+       }
+
+       cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
+
+       /* Update in_bytes to actual data size */
+       in_bytes = sizeof(*rs) + rs->data_len;
+
+       /* Verify checksum */
+       csum = cros_ec_calc_checksum(dev->din, in_bytes);
+       if (csum) {
+               debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
+                     csum);
+               return -EC_RES_INVALID_CHECKSUM;
+       }
+
+       /* Return error result, if any */
+       if (rs->result)
+               return -(int)rs->result;
+
+       /* If we're still here, set response data pointer and return length */
+       *dinp = (uint8_t *)(rs + 1);
+
+       return rs->data_len;
+}
+
+static int send_command_proto3(struct cros_ec_dev *dev,
+                              int cmd, int cmd_version,
+                              const void *dout, int dout_len,
+                              uint8_t **dinp, int din_len)
+{
+       int out_bytes, in_bytes;
+       int rv;
+
+       /* Create request packet */
+       out_bytes = create_proto3_request(dev, 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);
+       if (in_bytes < 0)
+               return in_bytes;
+
+       switch (dev->interface) {
+#ifdef CONFIG_CROS_EC_SPI
+       case CROS_EC_IF_SPI:
+               rv = cros_ec_spi_packet(dev, out_bytes, in_bytes);
+               break;
+#endif
+       case CROS_EC_IF_NONE:
+       /* TODO: support protocol 3 for LPC, I2C; for now fall through */
+       default:
+               debug("%s: Unsupported interface\n", __func__);
+               rv = -1;
+       }
+       if (rv < 0)
+               return rv;
+
+       /* Process the response */
+       return handle_proto3_response(dev, dinp, din_len);
+}
+
 static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
                        const void *dout, int dout_len,
                        uint8_t **dinp, int din_len)
 {
-       int ret;
+       int ret = -1;
+
+       /* Handle protocol version 3 support */
+       if (dev->protocol_version == 3) {
+               return send_command_proto3(dev, cmd, cmd_version,
+                                          dout, dout_len, dinp, din_len);
+       }
 
        switch (dev->interface) {
 #ifdef CONFIG_CROS_EC_SPI
@@ -132,10 +301,6 @@ static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
        uint8_t *din;
        int len;
 
-       if (cmd_version != 0 && !dev->cmd_version_is_supported) {
-               debug("%s: Command version >0 unsupported\n", __func__);
-               return -1;
-       }
        len = send_command(dev, cmd, cmd_version, dout, dout_len,
                                &din, din_len);
 
@@ -220,7 +385,7 @@ static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 
 int cros_ec_scan_keyboard(struct cros_ec_dev *dev, struct mbkp_keyscan *scan)
 {
-       if (ec_command(dev, EC_CMD_CROS_EC_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;
 
@@ -267,7 +432,7 @@ int cros_ec_read_version(struct cros_ec_dev *dev,
 int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
 {
        if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
-                       (uint8_t **)strp, EC_HOST_PARAM_SIZE) < 0)
+                       (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
                return -1;
 
        return 0;
@@ -336,7 +501,7 @@ int cros_ec_read_hash(struct cros_ec_dev *dev,
        debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
              __func__, hash->status, hash->size);
 
-       p.cmd = EC_VBOOT_HASH_RECALC;
+       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;
@@ -418,10 +583,10 @@ int cros_ec_interrupt_pending(struct cros_ec_dev *dev)
        return !gpio_get_value(dev->ec_int.gpio);
 }
 
-int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_cros_ec_info *info)
+int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
 {
-       if (ec_command(dev, EC_CMD_CROS_EC_INFO, 0, NULL, 0, info,
-                       sizeof(*info)) < sizeof(*info))
+       if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
+                      sizeof(*info)) < sizeof(*info))
                return -1;
 
        return 0;
@@ -504,23 +669,30 @@ static int cros_ec_check_version(struct cros_ec_dev *dev)
         *
         * So for now, just read all the data anyway.
         */
-       dev->cmd_version_is_supported = 1;
+
+       /* Try sending a version 3 packet */
+       dev->protocol_version = 3;
+       if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
+                            (uint8_t **)&resp, sizeof(*resp)) > 0) {
+               return 0;
+       }
+
+       /* Try sending a version 2 packet */
+       dev->protocol_version = 2;
        if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
                       (uint8_t **)&resp, sizeof(*resp)) > 0) {
-               /* It appears to understand new version commands */
-               dev->cmd_version_is_supported = 1;
-       } else {
-               dev->cmd_version_is_supported = 0;
-               if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req,
-                             sizeof(req), (uint8_t **)&resp,
-                             sizeof(*resp)) < 0) {
-                       debug("%s: Failed both old and new command style\n",
-                               __func__);
-                       return -1;
-               }
+               return 0;
        }
 
-       return 0;
+       /*
+        * Fail if we're still here, since the EC doesn't understand any
+        * protcol version we speak.  Version 1 interface without command
+        * version is no longer supported, and we don't know about any new
+        * protocol versions.
+        */
+       dev->protocol_version = 0;
+       printf("%s: ERROR: old EC interface not supported\n", __func__);
+       return -1;
 }
 
 int cros_ec_test(struct cros_ec_dev *dev)
@@ -599,8 +771,8 @@ static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
 
        p.offset = offset;
        p.size = size;
-       assert(data && p.size <= sizeof(p.data));
-       memcpy(p.data, data, p.size);
+       assert(data && p.size <= EC_FLASH_WRITE_VER0_SIZE);
+       memcpy(&p + 1, data, p.size);
 
        return ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
                          &p, sizeof(p), NULL, 0) >= 0 ? 0 : -1;
@@ -611,8 +783,7 @@ static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
  */
 static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
 {
-       struct ec_params_flash_write p;
-       return sizeof(p.data);
+       return EC_FLASH_WRITE_VER0_SIZE;
 }
 
 /**
@@ -813,7 +984,8 @@ int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
 }
 
 /**
- * Decode MBKP details from the device tree and allocate a suitable device.
+ * Decode EC interface details from the device tree and allocate a suitable
+ * device.
  *
  * @param blob         Device tree blob
  * @param node         Node to decode from
@@ -941,7 +1113,6 @@ int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
        return 0;
 }
 
-#ifdef CONFIG_CMD_CROS_EC
 int cros_ec_decode_region(int argc, char * const argv[])
 {
        if (argc > 0) {
@@ -958,6 +1129,58 @@ int cros_ec_decode_region(int argc, char * const argv[])
        return -1;
 }
 
+int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
+{
+       int flash_node, node;
+
+       node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
+       if (node < 0) {
+               debug("Failed to find chrome-ec node'\n");
+               return -1;
+       }
+
+       flash_node = fdt_subnode_offset(blob, node, "flash");
+       if (flash_node < 0) {
+               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");
+               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);
+               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;
+               } else if (0 == strcmp(name, "wp-ro")) {
+                       region = EC_FLASH_REGION_WP_RO;
+               } else {
+                       debug("Unknown EC flash region name '%s'\n", name);
+                       return -1;
+               }
+
+               if (fdtdec_read_fmap_entry(blob, node, "reg",
+                                          &config->region[region])) {
+                       debug("Failed to decode flash region in chrome-ec'\n");
+                       return -1;
+               }
+       }
+
+       return 0;
+}
+
+#ifdef CONFIG_CMD_CROS_EC
+
 /**
  * Perform a flash read or write command
  *
@@ -1044,7 +1267,7 @@ static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                }
                printf("%s\n", id);
        } else if (0 == strcmp("info", cmd)) {
-               struct ec_response_cros_ec_info info;
+               struct ec_response_mkbp_info info;
 
                if (cros_ec_info(dev, &info)) {
                        debug("%s: Could not read KBC info\n", __func__);