Merge tag 'dm-pull-5jan21' of git://git.denx.de/u-boot-dm into next
[platform/kernel/u-boot.git] / drivers / misc / cros_ec_sandbox.c
index df41e82..9fd6cc2 100644 (file)
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Chromium OS cros_ec driver - sandbox emulation
  *
  * Copyright (c) 2013 The Chromium OS Authors.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <ec_commands.h>
 #include <errno.h>
 #include <hash.h>
-#include <malloc.h>
+#include <log.h>
 #include <os.h>
 #include <u-boot/sha256.h>
 #include <spi.h>
+#include <asm/malloc.h>
 #include <asm/state.h>
 #include <asm/sdl.h>
 #include <linux/input.h>
@@ -51,8 +51,6 @@
  * the EC image in with U-Boot (Vic has demonstrated a prototype for this).
  */
 
-DECLARE_GLOBAL_DATA_PTR;
-
 #define KEYBOARD_ROWS  8
 #define KEYBOARD_COLS  13
 
@@ -77,7 +75,7 @@ struct ec_keymatrix_entry {
  * @recovery_req: Keyboard recovery requested
  */
 struct ec_state {
-       uint8_t vbnv_context[EC_VBNV_BLOCK_SIZE];
+       u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2];
        struct fdt_cros_ec ec_config;
        uint8_t *flash_data;
        int flash_data_len;
@@ -118,7 +116,7 @@ static int cros_ec_read_state(const void *blob, int node)
        prop = fdt_getprop(blob, node, "flash-data", &len);
        if (prop) {
                ec->flash_data_len = len;
-               ec->flash_data = os_malloc(len);
+               ec->flash_data = malloc(len);
                if (!ec->flash_data)
                        return -ENOMEM;
                memcpy(ec->flash_data, prop, len);
@@ -188,18 +186,16 @@ static int get_image_used(struct ec_state *ec, struct fmap_entry *entry)
  * RR=Row CC=Column KKKK=Key Code
  *
  * @param ec   Current emulated EC state
- * @param blob Device tree blob containing keyscan information
  * @param node Keyboard node of device tree containing keyscan information
  * @return 0 if ok, -1 on error
  */
-static int keyscan_read_fdt_matrix(struct ec_state *ec, const void *blob,
-                                  int node)
+static int keyscan_read_fdt_matrix(struct ec_state *ec, ofnode node)
 {
        const u32 *cell;
        int upto;
        int len;
 
-       cell = fdt_getprop(blob, node, "linux,keymap", &len);
+       cell = ofnode_get_property(node, "linux,keymap", &len);
        ec->matrix_count = len / 4;
        ec->matrix = calloc(ec->matrix_count, sizeof(*ec->matrix));
        if (!ec->matrix) {
@@ -318,13 +314,15 @@ static int process_cmd(struct ec_state *ec,
 
                switch (req->op) {
                case EC_VBNV_CONTEXT_OP_READ:
+                       /* TODO(sjg@chromium.org): Support full-size context */
                        memcpy(resp->block, ec->vbnv_context,
-                              sizeof(resp->block));
-                       len = sizeof(*resp);
+                              EC_VBNV_BLOCK_SIZE);
+                       len = 16;
                        break;
                case EC_VBNV_CONTEXT_OP_WRITE:
-                       memcpy(ec->vbnv_context, resp->block,
-                              sizeof(resp->block));
+                       /* TODO(sjg@chromium.org): Support full-size context */
+                       memcpy(ec->vbnv_context, req->block,
+                              EC_VBNV_BLOCK_SIZE);
                        len = 0;
                        break;
                default:
@@ -370,7 +368,7 @@ static int process_cmd(struct ec_state *ec,
                struct fmap_entry *entry;
                int ret, size;
 
-               entry = &ec->ec_config.region[EC_FLASH_REGION_RW];
+               entry = &ec->ec_config.region[EC_FLASH_REGION_ACTIVE];
 
                switch (req->cmd) {
                case EC_VBOOT_HASH_RECALC:
@@ -425,7 +423,7 @@ static int process_cmd(struct ec_state *ec,
 
                switch (req->region) {
                case EC_FLASH_REGION_RO:
-               case EC_FLASH_REGION_RW:
+               case EC_FLASH_REGION_ACTIVE:
                case EC_FLASH_REGION_WP_RO:
                        entry = &ec->ec_config.region[req->region];
                        resp->offset = entry->offset;
@@ -459,6 +457,17 @@ static int process_cmd(struct ec_state *ec,
        case EC_CMD_MKBP_STATE:
                len = cros_ec_keyscan(ec, resp_data);
                break;
+       case EC_CMD_ENTERING_MODE:
+               len = 0;
+               break;
+       case EC_CMD_GET_NEXT_EVENT: {
+               struct ec_response_get_next_event *resp = resp_data;
+
+               resp->event_type = EC_MKBP_EVENT_KEY_MATRIX;
+               cros_ec_keyscan(ec, resp->data.key_matrix);
+               len = sizeof(*resp);
+               break;
+       }
        default:
                printf("   ** Unknown EC command %#02x\n", req_hdr->command);
                return -1;
@@ -493,9 +502,9 @@ int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
        return in_bytes;
 }
 
-void cros_ec_check_keyboard(struct cros_ec_dev *dev)
+void cros_ec_check_keyboard(struct udevice *dev)
 {
-       struct ec_state *ec = dev_get_priv(dev->dev);
+       struct ec_state *ec = dev_get_priv(dev);
        ulong start;
 
        printf("Press keys for EC to detect on reset (ESC=recovery)...");
@@ -511,21 +520,31 @@ void cros_ec_check_keyboard(struct cros_ec_dev *dev)
 
 int cros_ec_probe(struct udevice *dev)
 {
-       struct ec_state *ec = dev->priv;
-       struct cros_ec_dev *cdev = dev->uclass_priv;
-       const void *blob = gd->fdt_blob;
-       int node;
+       struct ec_state *ec = dev_get_priv(dev);
+       struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
+       struct udevice *keyb_dev;
+       ofnode node;
        int err;
 
        memcpy(ec, &s_state, sizeof(*ec));
-       err = cros_ec_decode_ec_flash(blob, dev->of_offset, &ec->ec_config);
-       if (err)
+       err = cros_ec_decode_ec_flash(dev, &ec->ec_config);
+       if (err) {
+               debug("%s: Cannot device EC flash\n", __func__);
                return err;
+       }
 
-       node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
-       if (node < 0) {
+       node = ofnode_null();
+       for (device_find_first_child(dev, &keyb_dev);
+            keyb_dev;
+            device_find_next_child(&keyb_dev)) {
+               if (device_get_uclass_id(keyb_dev) == UCLASS_KEYBOARD) {
+                       node = dev_ofnode(keyb_dev);
+                       break;
+               }
+       }
+       if (!ofnode_valid(node)) {
                debug("%s: No cros_ec keyboard found\n", __func__);
-       } else if (keyscan_read_fdt_matrix(ec, blob, node)) {
+       } else if (keyscan_read_fdt_matrix(ec, node)) {
                debug("%s: Could not read key matrix\n", __func__);
                return -1;
        }
@@ -535,14 +554,14 @@ int cros_ec_probe(struct udevice *dev)
            ec->flash_data_len != ec->ec_config.flash.length) {
                printf("EC data length is %x, expected %x, discarding data\n",
                       ec->flash_data_len, ec->ec_config.flash.length);
-               os_free(ec->flash_data);
+               free(ec->flash_data);
                ec->flash_data = NULL;
        }
 
        /* Otherwise allocate the memory */
        if (!ec->flash_data) {
                ec->flash_data_len = ec->ec_config.flash.length;
-               ec->flash_data = os_malloc(ec->flash_data_len);
+               ec->flash_data = malloc(ec->flash_data_len);
                if (!ec->flash_data)
                        return -ENOMEM;
        }
@@ -561,11 +580,11 @@ static const struct udevice_id cros_ec_ids[] = {
        { }
 };
 
-U_BOOT_DRIVER(cros_ec_sandbox) = {
-       .name           = "cros_ec_sandbox",
+U_BOOT_DRIVER(google_cros_ec_sandbox) = {
+       .name           = "google_cros_ec_sandbox",
        .id             = UCLASS_CROS_EC,
        .of_match       = cros_ec_ids,
        .probe          = cros_ec_probe,
-       .priv_auto_alloc_size = sizeof(struct ec_state),
+       .priv_auto      = sizeof(struct ec_state),
        .ops            = &cros_ec_ops,
 };