The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Wang Yang" <yang.wang@deepcomputing.io>
To: "Yixun Lan" <dlan@kernel.org>, "Benson Leung" <bleung@chromium.org>
Cc: "Troy Mitchell" <troy.mitchell@linux.spacemit.com>,
	 "Tzung-Bi Shih" <tzungbi@kernel.org>,
	 "Guenter Roeck" <groeck@chromium.org>,
	"Rob Herring" <robh@kernel.org>,
	 "Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	 "Conor Dooley" <conor+dt@kernel.org>,
	"Paul Walmsley" <pjw@kernel.org>,
	 "Palmer Dabbelt" <palmer@dabbelt.com>,
	 "Albert Ou" <aou@eecs.berkeley.edu>,
	"Alexandre Ghiti" <alex@ghiti.fr>,
	 "Sandie Cao" <sandie.cao@deepcomputing.io>,
	<devicetree@vger.kernel.org>,  <chrome-platform@lists.linux.dev>,
	<linux-riscv@lists.infradead.org>,  <spacemit@lists.linux.dev>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH 5/9] platform/chrome: Add SpacemiT K3 eSPI EC transport
Date: Wed, 18 Nov 2026 16:57:38 +0800	[thread overview]
Message-ID: <20261118085742.1817536-6-yang.wang@deepcomputing.io> (raw)
In-Reply-To: <20261118085742.1817536-1-yang.wang@deepcomputing.io>

Add the K3-specific EC transport as a separate driver. Use a vendor-
scoped Kconfig symbol, module name and compatible. The register mapping
and recovery API are tied to the SpacemiT K3 controller, so this does
not claim to be a generic eSPI EC transport.

Preserve the original independent eSPI command and event logic. Existing
EC core, LPC, protocol and keyboard implementations remain unchanged.

Signed-off-by: Wang Yang <yang.wang@deepcomputing.io>
---
 drivers/platform/chrome/Kconfig               |  11 +
 drivers/platform/chrome/Makefile              |   1 +
 drivers/platform/chrome/cros_ec_spacemit_k3.c | 739 ++++++++++++++++++
 3 files changed, 751 insertions(+)
 create mode 100644 drivers/platform/chrome/cros_ec_spacemit_k3.c

diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kconfig
index 6beda4655..10933f7af 100644
--- a/drivers/platform/chrome/Kconfig
+++ b/drivers/platform/chrome/Kconfig
@@ -154,6 +154,17 @@ config CROS_EC_LPC
 	  To compile this driver as a module, choose M here: the
 	  module will be called cros_ec_lpcs.
 
+config CROS_EC_SPACEMIT_K3
+	tristate "SpacemiT K3 ChromeOS EC over eSPI"
+	depends on CROS_EC && SPACEMIT_ESPI_BUS
+	help
+	  Support a ChromeOS EC connected through the SpacemiT K3 eSPI
+	  peripheral shared-memory window. The transport supports host
+	  command protocol versions 2 and 3 and EC event interrupts.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called cros_ec_spacemit_k3.
+
 config CROS_EC_PROTO
 	tristate
 	help
diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile
index b981a1bb5..e3357a6bc 100644
--- a/drivers/platform/chrome/Makefile
+++ b/drivers/platform/chrome/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_CROS_EC_I2C)		+= cros_ec_i2c.o
 obj-$(CONFIG_CROS_EC_ISHTP)		+= cros_ec_ishtp.o
 obj-$(CONFIG_CROS_TYPEC_SWITCH)		+= cros_typec_switch.o
 obj-$(CONFIG_CROS_EC_RPMSG)		+= cros_ec_rpmsg.o
+obj-$(CONFIG_CROS_EC_SPACEMIT_K3)		+= cros_ec_spacemit_k3.o
 obj-$(CONFIG_CROS_EC_SPI)		+= cros_ec_spi.o
 obj-$(CONFIG_CROS_EC_UART)		+= cros_ec_uart.o
 cros_ec_lpcs-objs			:= cros_ec_lpc.o cros_ec_lpc_mec.o
diff --git a/drivers/platform/chrome/cros_ec_spacemit_k3.c b/drivers/platform/chrome/cros_ec_spacemit_k3.c
new file mode 100644
index 000000000..b3010e2bf
--- /dev/null
+++ b/drivers/platform/chrome/cros_ec_spacemit_k3.c
@@ -0,0 +1,739 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * ChromeOS EC interface for the SpacemiT K3 eSPI controller
+ *
+ * Copyright (c) 2012 The Chromium OS Authors.
+ * Copyright (c) 2024 SpacemiT, Inc.
+ *
+ * This driver provides the SpacemiT K3 eSPI window for ChromeOS EC communication.
+ * It converts LPC I/O port accesses to eSPI shared memory accesses while maintaining
+ * compatibility with the existing ChromeOS EC protocol stack.
+ */
+
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_data/cros_ec_commands.h>
+#include <linux/platform_data/cros_ec_proto.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/spacemit-k3-espi.h>
+#include <linux/suspend.h>
+
+#include "cros_ec.h"
+
+#define DRV_NAME "cros_ec_spacemit_k3"
+
+/* eSPI shared memory access timeout */
+#define ESPI_CMD_TIMEOUT_MS 5000
+/*
+ * After an EC reboot the transport may recover well before the EC firmware
+ * finishes repopulating the shared memory window. Allow a longer grace period
+ * here so the first post-reboot command does not immediately fall into another
+ * full timeout cycle.
+ */
+#define ESPI_READY_TIMEOUT_MS 20000
+#define ESPI_READY_POLL_INTERVAL_US 20000
+#define ESPI_RECOVERY_RETRY_INTERVAL_MS 5000
+#define ESPI_INVALID_STATUS_VALUE 0xff
+#define ESPI_INVALID_STATUS_MAX_POLLS 5
+
+/*
+ * eSPI Shared Memory Address Mapping for EC Communication
+ *
+ * This driver converts LPC I/O port accesses to eSPI shared memory accesses.
+ * The original LPC addresses are mapped to offsets within the eSPI shared memory region.
+ *
+ * Address Mapping Strategy:
+ * - LPC addresses are mapped to shared memory offsets
+ * - The mapping preserves the relative layout and functionality of the original LPC interface
+ * - All accesses go through espi_shared_mem_base + offset
+ *
+ * Memory Layout in eSPI Shared Memory:
+ * 0x000: Host command register (EC_LPC_ADDR_HOST_CMD -> 0x000)
+ * 0x001: Host data register (EC_LPC_ADDR_HOST_DATA -> 0x001)
+ * 0x010-0x017: Host arguments (8 bytes)
+ * 0x018-0x0FF: Host parameters (up to 232 bytes)
+ * 0x100-0x1FF: Packet data area (256 bytes for protocol v3)
+ * 0x200-0x2FF: Memory map region (256 bytes)
+ */
+
+/* eSPI shared memory offsets */
+#define ESPI_HOST_CMD_OFFSET 0x000 /* EC_LPC_ADDR_HOST_CMD -> 0x000 */
+#define ESPI_HOST_DATA_OFFSET 0x001 /* EC_LPC_ADDR_HOST_DATA -> 0x001 */
+#define ESPI_HOST_ARGS_OFFSET 0x010 /* EC_LPC_ADDR_HOST_ARGS -> 0x010 */
+#define ESPI_HOST_PARAM_OFFSET 0x018 /* EC_LPC_ADDR_HOST_PARAM -> 0x018 */
+#define ESPI_HOST_PACKET_OFFSET 0x100 /* EC_LPC_ADDR_HOST_PACKET -> 0x100 */
+#define ESPI_MEMMAP_OFFSET 0x200 /* EC_LPC_ADDR_MEMMAP -> 0x200 */
+
+/**
+ * struct cros_ec_espi - eSPI device-specific data
+ * @espi_shared_mem_base: Base address of eSPI shared memory region
+ * @mmio_memory_base: The first I/O port addressing EC mapped memory
+ * @io_mutex: Mutex to protect eSPI shared memory access
+ */
+struct cros_ec_espi {
+	void __iomem *espi_shared_mem_base;
+	u16 mmio_memory_base;
+	/* Serialize shared-window accesses and link recovery. */
+	struct mutex io_mutex;
+};
+
+/* Convert LPC address to eSPI shared memory offset */
+static inline u32 lpc_to_espi_offset(struct cros_ec_device *ec, u32 lpc_addr)
+{
+	switch (lpc_addr & 0xFF00) {
+	case 0x200: /* HOST_CMD/DATA region */
+		if (lpc_addr == EC_LPC_ADDR_HOST_CMD)
+			return ESPI_HOST_CMD_OFFSET;
+		else if (lpc_addr == EC_LPC_ADDR_HOST_DATA)
+			return ESPI_HOST_DATA_OFFSET;
+		break;
+	case 0x800: /* HOST_ARGS/PARAM/PACKET region */
+		/* Protocols 2 and 3 use the same LPC addresses. */
+		if (ec->proto_version > 2)
+			return ESPI_HOST_PACKET_OFFSET +
+			       (lpc_addr - EC_LPC_ADDR_HOST_PACKET);
+		else if (lpc_addr >= EC_LPC_ADDR_HOST_ARGS &&
+			 lpc_addr < EC_LPC_ADDR_HOST_PARAM)
+			return ESPI_HOST_ARGS_OFFSET +
+			       (lpc_addr - EC_LPC_ADDR_HOST_ARGS);
+		else if (lpc_addr >= EC_LPC_ADDR_HOST_PARAM)
+			return ESPI_HOST_PARAM_OFFSET +
+			       (lpc_addr - EC_LPC_ADDR_HOST_PARAM);
+		break;
+	case 0x900: /* MEMMAP region */
+		return ESPI_MEMMAP_OFFSET + (lpc_addr - EC_LPC_ADDR_MEMMAP);
+	}
+
+	/* Default mapping for unknown addresses */
+	pr_warn_once("Unknown LPC address 0x%x, using direct offset\n",
+		     lpc_addr);
+	return lpc_addr & 0xFF;
+}
+
+/* Return the checksum of the bytes read from the peripheral window. */
+static int cros_ec_espi_read_bytes(struct cros_ec_device *ec,
+				   unsigned int offset, unsigned int length,
+				   u8 *dest)
+{
+	struct cros_ec_espi *ec_espi = ec->priv;
+	void __iomem *addr;
+	u32 espi_offset;
+	u8 sum = 0;
+	int i;
+
+	mutex_lock(&ec_espi->io_mutex);
+
+	for (i = 0; i < length; ++i) {
+		espi_offset = lpc_to_espi_offset(ec, offset + i);
+		addr = ec_espi->espi_shared_mem_base + espi_offset;
+
+		/* eSPI shared memory: 1-byte access */
+		dest[i] = readb(addr);
+		sum += dest[i];
+	}
+
+	mutex_unlock(&ec_espi->io_mutex);
+
+	/* Return checksum of all bytes read */
+	return sum;
+}
+
+/* Return the checksum of the bytes written to the peripheral window. */
+static int cros_ec_espi_write_bytes(struct cros_ec_device *ec,
+				    unsigned int offset, unsigned int length,
+				    const u8 *msg)
+{
+	struct cros_ec_espi *ec_espi = ec->priv;
+	void __iomem *addr;
+	u32 espi_offset;
+	u8 sum = 0;
+	int i;
+
+	mutex_lock(&ec_espi->io_mutex);
+
+	for (i = 0; i < length; ++i) {
+		espi_offset = lpc_to_espi_offset(ec, offset + i);
+		addr = ec_espi->espi_shared_mem_base + espi_offset;
+
+		/* eSPI shared memory: 1-byte access */
+		writeb(msg[i], addr);
+		sum += msg[i];
+	}
+
+	mutex_unlock(&ec_espi->io_mutex);
+
+	/* Return checksum of all bytes written */
+	return sum;
+}
+
+static int ec_response_timed_out(struct cros_ec_device *ec)
+{
+	unsigned long timeout = jiffies + msecs_to_jiffies(ESPI_CMD_TIMEOUT_MS);
+	unsigned int invalid_status_polls = 0;
+	u8 data;
+	int ret;
+
+	usleep_range(200, 300);
+	do {
+		ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_CMD, 1, &data);
+		if (ret < 0)
+			return ret;
+
+		if (!(data & EC_LPC_STATUS_BUSY_MASK))
+			return 0;
+
+		/*
+		 * While the EC is rebooting the shared window reads back as 0xff.
+		 * Escalate to controller recovery quickly instead of burning the
+		 * whole command timeout on an absent endpoint.
+		 */
+		if (data == ESPI_INVALID_STATUS_VALUE) {
+			if (++invalid_status_polls >= ESPI_INVALID_STATUS_MAX_POLLS)
+				return 1;
+		} else {
+			invalid_status_polls = 0;
+		}
+
+		usleep_range(100, 200);
+	} while (time_before(jiffies, timeout));
+
+	return 1;
+}
+
+static bool cros_ec_espi_valid_id(const u8 *buf)
+{
+	return buf[0] == 'E' && buf[1] == 'C';
+}
+
+static int cros_ec_espi_read_id(struct cros_ec_device *ec, u8 *buf)
+{
+	return cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
+}
+
+static int cros_ec_espi_recover_bus(struct cros_ec_device *ec)
+{
+	struct cros_ec_espi *ec_espi = ec->priv;
+	int ret;
+
+	mutex_lock(&ec_espi->io_mutex);
+	ret = spacemit_k3_espi_recover(ec->dev->parent);
+	mutex_unlock(&ec_espi->io_mutex);
+
+	return ret;
+}
+
+static int cros_ec_espi_wait_ready(struct cros_ec_device *ec, const char *reason)
+{
+	struct device *dev = ec->dev;
+	unsigned long timeout = jiffies + msecs_to_jiffies(ESPI_READY_TIMEOUT_MS);
+	unsigned long next_recover =
+		jiffies + msecs_to_jiffies(ESPI_RECOVERY_RETRY_INTERVAL_MS);
+	u8 buf[2] = {};
+	int ret = 0;
+
+	do {
+		ret = cros_ec_espi_read_id(ec, buf);
+		if (ret >= 0 && cros_ec_espi_valid_id(buf))
+			return 0;
+
+		/*
+		 * The first recover may happen while the EC is still updating and
+		 * leave the controller in a master-only state. Retry link recovery
+		 * periodically while waiting so we can renegotiate as soon as the
+		 * EC starts responding again.
+		 */
+		if (time_after_eq(jiffies, next_recover)) {
+			ret = cros_ec_espi_recover_bus(ec);
+			if (ret)
+				dev_dbg(dev, "background eSPI recovery failed: %d\n", ret);
+			next_recover = jiffies +
+				msecs_to_jiffies(ESPI_RECOVERY_RETRY_INTERVAL_MS);
+			continue;
+		}
+
+		usleep_range(ESPI_READY_POLL_INTERVAL_US,
+			     ESPI_READY_POLL_INTERVAL_US + 10000);
+	} while (time_before(jiffies, timeout));
+
+	if (ret < 0)
+		dev_warn(dev, "EC not ready after %s: %d\n", reason, ret);
+	else
+		dev_warn(dev, "EC not ready after %s (id: 0x%02x 0x%02x)\n",
+			 reason, buf[0], buf[1]);
+
+	return ret < 0 ? ret : -ETIMEDOUT;
+}
+
+static int cros_ec_espi_recover_controller(struct cros_ec_device *ec,
+					   const char *reason)
+{
+	struct device *dev = ec->dev;
+	int ret;
+
+	if (!dev->parent)
+		return -ENODEV;
+
+	dev_warn(dev, "attempting eSPI controller recovery after %s\n", reason);
+	ret = cros_ec_espi_recover_bus(ec);
+	if (ret)
+		dev_err(dev, "eSPI controller recovery failed: %d\n", ret);
+
+	if (ret)
+		return ret;
+
+	return cros_ec_espi_wait_ready(ec, reason);
+}
+
+static int cros_ec_pkt_xfer_espi_once(struct cros_ec_device *ec,
+				      struct cros_ec_command *msg)
+{
+	struct ec_host_response response;
+	u8 sum;
+	int ret = 0;
+	u8 *dout;
+
+	/*
+	 * Give EC some time between commands to avoid overwhelming it.
+	 * This prevents timing issues when multiple commands are sent rapidly.
+	 */
+	usleep_range(500, 1000);
+
+	ret = cros_ec_prepare_tx(ec, msg);
+	if (ret < 0) {
+		dev_err(ec->dev, "cros_ec_prepare_tx failed: %d\n", ret);
+		goto done;
+	}
+
+	/* Write buffer */
+	ret = cros_ec_espi_write_bytes(ec, EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
+	if (ret < 0) {
+		dev_err(ec->dev, "Write to HOST_PACKET failed: %d\n", ret);
+		goto done;
+	}
+
+	/* Here we go */
+	sum = EC_COMMAND_PROTOCOL_3;
+	ret = cros_ec_espi_write_bytes(ec, EC_LPC_ADDR_HOST_CMD, 1, &sum);
+	if (ret < 0) {
+		dev_err(ec->dev, "Write to HOST_CMD failed: %d\n", ret);
+		goto done;
+	}
+
+	ret = ec_response_timed_out(ec);
+	if (ret < 0) {
+		dev_err(ec->dev, "ec_response_timed_out failed: %d\n", ret);
+		goto done;
+	}
+	if (ret) {
+		dev_warn(ec->dev, "EC response timed out\n");
+		ret = -ETIMEDOUT;
+		goto done;
+	}
+
+	/* Check result */
+	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_DATA, 1, &sum);
+	if (ret < 0) {
+		dev_err(ec->dev, "Read from HOST_DATA failed: %d\n", ret);
+		goto done;
+	}
+
+	msg->result = sum;
+	ret = cros_ec_check_result(ec, msg);
+	if (ret) {
+		dev_err(ec->dev, "cros_ec_check_result failed: %d\n", ret);
+		goto done;
+	}
+
+	/* Read back response */
+	dout = (u8 *)&response;
+	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_PACKET, sizeof(response),
+				      dout);
+	if (ret < 0) {
+		dev_err(ec->dev, "Read response header failed: %d\n", ret);
+		goto done;
+	}
+	sum = ret;
+
+	msg->result = response.result;
+
+	if (response.data_len > msg->insize) {
+		dev_err(ec->dev, "packet too long (%d bytes, expected %d)",
+			response.data_len, msg->insize);
+		ret = -EMSGSIZE;
+		goto done;
+	}
+
+	/* Read response and process checksum */
+	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_PACKET + sizeof(response),
+				      response.data_len, msg->data);
+	if (ret < 0) {
+		dev_err(ec->dev, "Read response data failed: %d\n", ret);
+		goto done;
+	}
+	sum += ret;
+
+	if (sum) {
+		dev_err(ec->dev, "bad packet checksum %02x\n",
+			response.checksum);
+		ret = -EBADMSG;
+		goto done;
+	}
+
+	/* Return actual amount of data received */
+	ret = response.data_len;
+done:
+	return ret;
+}
+
+static int cros_ec_espi_xfer_retry(struct cros_ec_device *ec,
+				   struct cros_ec_command *msg,
+				   int (*xfer_once)(struct cros_ec_device *ec,
+						    struct cros_ec_command *msg),
+				   const char *name)
+{
+	int ret;
+
+	ret = xfer_once(ec, msg);
+	if (ret != -ETIMEDOUT)
+		return ret;
+
+	if (cros_ec_espi_recover_controller(ec, name))
+		return ret;
+
+	dev_dbg(ec->dev, "eSPI controller recovered, retrying %s\n", name);
+	return xfer_once(ec, msg);
+}
+
+static int cros_ec_pkt_xfer_espi(struct cros_ec_device *ec,
+				 struct cros_ec_command *msg)
+{
+	return cros_ec_espi_xfer_retry(ec, msg, cros_ec_pkt_xfer_espi_once,
+				       "packet transfer timeout");
+}
+
+static int cros_ec_cmd_xfer_espi_once(struct cros_ec_device *ec,
+				      struct cros_ec_command *msg)
+{
+	struct ec_lpc_host_args args;
+	u8 sum;
+	int ret = 0;
+
+	if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
+	    msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
+		dev_err(ec->dev, "invalid buffer sizes (out %d, in %d)\n",
+			msg->outsize, msg->insize);
+		return -EINVAL;
+	}
+
+	/*
+	 * Give EC some time between commands to avoid overwhelming it.
+	 * This prevents timing issues when multiple commands are sent rapidly.
+	 */
+	usleep_range(500, 1000);
+
+	/* Now actually send the command to the EC and get the result */
+	args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
+	args.command_version = msg->version;
+	args.data_size = msg->outsize;
+
+	/* Initialize checksum */
+	sum = msg->command + args.flags + args.command_version + args.data_size;
+
+	/* Copy data and update checksum */
+	ret = cros_ec_espi_write_bytes(ec, EC_LPC_ADDR_HOST_PARAM, msg->outsize,
+				       msg->data);
+	if (ret < 0) {
+		dev_err(ec->dev, "Write to HOST_PARAM failed: %d\n", ret);
+		goto done;
+	}
+	sum += ret;
+
+	/* Finalize checksum and write args */
+	args.checksum = sum;
+	ret = cros_ec_espi_write_bytes(ec, EC_LPC_ADDR_HOST_ARGS, sizeof(args),
+				       (u8 *)&args);
+	if (ret < 0) {
+		dev_err(ec->dev, "Write to HOST_ARGS failed: %d\n", ret);
+		goto done;
+	}
+
+	/* Here we go */
+	sum = msg->command;
+	ret = cros_ec_espi_write_bytes(ec, EC_LPC_ADDR_HOST_CMD, 1, &sum);
+	if (ret < 0) {
+		dev_err(ec->dev, "Write to HOST_CMD failed: %d\n", ret);
+		goto done;
+	}
+
+	ret = ec_response_timed_out(ec);
+	if (ret < 0) {
+		dev_err(ec->dev, "ec_response_timed_out failed: %d\n", ret);
+		goto done;
+	}
+	if (ret) {
+		dev_warn(ec->dev, "EC response timed out\n");
+		ret = -ETIMEDOUT;
+		goto done;
+	}
+
+	/* Check result */
+	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_DATA, 1, &sum);
+	if (ret < 0) {
+		dev_err(ec->dev, "Read from HOST_DATA failed: %d\n", ret);
+		goto done;
+	}
+
+	msg->result = sum;
+	ret = cros_ec_check_result(ec, msg);
+	if (ret) {
+		dev_err(ec->dev, "cros_ec_check_result failed: %d\n", ret);
+		goto done;
+	}
+
+	/* Read back args */
+	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_ARGS, sizeof(args),
+				      (u8 *)&args);
+	if (ret < 0) {
+		dev_err(ec->dev, "Read HOST_ARGS failed: %d\n", ret);
+		goto done;
+	}
+
+	if (args.data_size > msg->insize) {
+		dev_err(ec->dev, "packet too long (%d bytes, expected %d)",
+			args.data_size, msg->insize);
+		ret = -ENOSPC;
+		goto done;
+	}
+
+	/* Start calculating response checksum */
+	sum = msg->command + args.flags + args.command_version + args.data_size;
+
+	/* Read response and update checksum */
+	ret = cros_ec_espi_read_bytes(ec, EC_LPC_ADDR_HOST_PARAM, args.data_size,
+				      msg->data);
+	if (ret < 0) {
+		dev_err(ec->dev, "Read response data failed: %d\n", ret);
+		goto done;
+	}
+	sum += ret;
+
+	/* Verify checksum */
+	if (args.checksum != sum) {
+		dev_err(ec->dev,
+			"bad packet checksum, expected %02x, got %02x\n",
+			args.checksum, sum);
+		ret = -EBADMSG;
+		goto done;
+	}
+
+	/* Return actual amount of data received */
+	ret = args.data_size;
+done:
+	return ret;
+}
+
+static int cros_ec_cmd_xfer_espi(struct cros_ec_device *ec,
+				 struct cros_ec_command *msg)
+{
+	return cros_ec_espi_xfer_retry(ec, msg, cros_ec_cmd_xfer_espi_once,
+				       "command transfer timeout");
+}
+
+/* Returns num bytes read, or negative on error. Doesn't need locking. */
+static int cros_ec_espi_readmem(struct cros_ec_device *ec, unsigned int offset,
+				unsigned int bytes, void *dest)
+{
+	struct cros_ec_espi *ec_espi = ec->priv;
+	int i = offset;
+	char *s = dest;
+	int cnt = 0;
+	int ret;
+
+	if (offset >= EC_MEMMAP_SIZE || bytes > EC_MEMMAP_SIZE - offset)
+		return -EINVAL;
+
+	/* fixed length */
+	if (bytes) {
+		ret = cros_ec_espi_read_bytes(ec, ec_espi->mmio_memory_base + offset,
+					      bytes, s);
+		if (ret < 0)
+			return ret;
+		return bytes;
+	}
+
+	/* string */
+	for (; i < EC_MEMMAP_SIZE; i++, s++) {
+		ret = cros_ec_espi_read_bytes(ec, ec_espi->mmio_memory_base + i, 1,
+					      s);
+		if (ret < 0)
+			return ret;
+		cnt++;
+		if (!*s)
+			break;
+	}
+
+	return cnt;
+}
+
+static irqreturn_t cros_ec_espi_irq_handler(int irq, void *data)
+{
+	struct cros_ec_device *ec_dev = data;
+
+	ec_dev->last_event_time = cros_ec_get_time_ns();
+
+	return IRQ_WAKE_THREAD;
+}
+
+static int cros_ec_espi_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct cros_ec_device *ec;
+	struct cros_ec_espi *ec_espi;
+	struct resource *res;
+	u8 id[2];
+	int irq, ret;
+
+	ec_espi = devm_kzalloc(dev, sizeof(*ec_espi), GFP_KERNEL);
+	if (!ec_espi)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res || resource_size(res) < 0x300)
+		return -EINVAL;
+	ec_espi->espi_shared_mem_base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(ec_espi->espi_shared_mem_base))
+		return PTR_ERR(ec_espi->espi_shared_mem_base);
+
+	ec_espi->mmio_memory_base = EC_LPC_ADDR_MEMMAP;
+	mutex_init(&ec_espi->io_mutex);
+
+	ec = cros_ec_device_alloc(dev);
+	if (!ec)
+		return -ENOMEM;
+
+	ec->phys_name = dev_name(dev);
+	ec->cmd_xfer = cros_ec_cmd_xfer_espi;
+	ec->pkt_xfer = cros_ec_pkt_xfer_espi;
+	ec->cmd_readmem = cros_ec_espi_readmem;
+	ec->priv = ec_espi;
+	platform_set_drvdata(pdev, ec);
+
+	ret = cros_ec_espi_read_id(ec, id);
+	if (ret < 0 || !cros_ec_espi_valid_id(id)) {
+		ret = cros_ec_espi_recover_controller(ec, "EC detection");
+		if (ret)
+			return dev_err_probe(dev, ret, "EC is not ready\n");
+	}
+
+	/* The EC still initializes its command window after publishing its ID. */
+	msleep(100);
+
+	irq = platform_get_irq_optional(pdev, 0);
+	if (irq < 0 && irq != -ENXIO)
+		return irq;
+
+	/* Keep IRQ ownership here; the core requests a fixed low-level IRQ. */
+	ret = cros_ec_register(ec);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to register EC\n");
+
+	if (irq > 0) {
+		ec->irq = irq;
+		ret = devm_request_threaded_irq(dev, irq, cros_ec_espi_irq_handler,
+						cros_ec_irq_thread, IRQF_ONESHOT,
+						"chromeos-ec", ec);
+		if (ret) {
+			cros_ec_unregister(ec);
+			return dev_err_probe(dev, ret, "failed to request EC IRQ\n");
+		}
+
+		/* Drain events queued before the edge-triggered IRQ was ready. */
+		if (ec->mkbp_event_supported)
+			cros_ec_irq_thread(irq, ec);
+	}
+
+	return 0;
+}
+
+static void cros_ec_espi_remove(struct platform_device *pdev)
+{
+	struct cros_ec_device *ec = platform_get_drvdata(pdev);
+
+	if (ec->irq > 0)
+		devm_free_irq(&pdev->dev, ec->irq, ec);
+	cros_ec_unregister(ec);
+}
+
+static const struct of_device_id cros_ec_espi_of_match[] = {
+	{ .compatible = "spacemit,k3-cros-ec" },
+	{}
+};
+MODULE_DEVICE_TABLE(of, cros_ec_espi_of_match);
+
+#ifdef CONFIG_PM_SLEEP
+static int cros_ec_espi_prepare(struct device *dev)
+{
+	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
+
+	return cros_ec_suspend_prepare(ec_dev);
+}
+
+static void cros_ec_espi_complete(struct device *dev)
+{
+	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
+
+	cros_ec_resume_complete(ec_dev);
+}
+
+static int cros_ec_espi_suspend_late(struct device *dev)
+{
+	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
+
+	return cros_ec_suspend_late(ec_dev);
+}
+
+static int cros_ec_espi_resume_early(struct device *dev)
+{
+	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
+
+	return cros_ec_resume_early(ec_dev);
+}
+#endif
+
+static const struct dev_pm_ops cros_ec_espi_pm_ops = {
+#ifdef CONFIG_PM_SLEEP
+	.prepare = cros_ec_espi_prepare,
+	.complete = cros_ec_espi_complete,
+#endif
+	SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_espi_suspend_late,
+				     cros_ec_espi_resume_early)
+};
+
+static struct platform_driver cros_ec_espi_driver = {
+	.driver = {
+		.name = DRV_NAME,
+		.of_match_table = cros_ec_espi_of_match,
+		.pm = &cros_ec_espi_pm_ops,
+		/*
+		 * Child devices may probe before us, and they racily
+		 * check our drvdata pointer. Force synchronous probe until
+		 * those races are resolved.
+		 */
+		.probe_type = PROBE_FORCE_SYNCHRONOUS,
+	},
+	.probe = cros_ec_espi_probe,
+	.remove = cros_ec_espi_remove,
+};
+
+module_platform_driver(cros_ec_espi_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("SpacemiT K3 eSPI ChromeOS EC transport");
+MODULE_ALIAS("platform:" DRV_NAME);
-- 
2.43.0

  parent reply	other threads:[~2026-11-18  8:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:57 [PATCH 0/9] Add SpacemiT K3 FML13V05 EC keyboard support Wang Yang
2026-11-18  8:57 ` [PATCH 1/9] dt-bindings: bus: Add SpacemiT K3 eSPI controller Wang Yang
2026-11-18  8:57 ` [PATCH 2/9] dt-bindings: embedded-controller: Add SpacemiT K3 EC window Wang Yang
2026-11-18  8:57 ` [PATCH 3/9] bus: Add SpacemiT K3 eSPI controller support Wang Yang
2026-11-18  8:57 ` [PATCH 4/9] platform/chrome: Allow ChromeOS platform support on RISC-V Wang Yang
2026-11-18  8:57 ` Wang Yang [this message]
2026-11-18  8:57 ` [PATCH 6/9] riscv: dts: spacemit: Add K3 eSPI pin group Wang Yang
2026-11-18  8:57 ` [PATCH 7/9] riscv: dts: spacemit: Describe the K3 eSPI controller Wang Yang
2026-11-18  8:57 ` [PATCH 8/9] riscv: dts: spacemit: Enable the FML13V05 EC keyboard Wang Yang
2026-11-18  8:57 ` [PATCH 9/9] riscv: defconfig: Enable the FML13V05 EC keyboard modules Wang Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118085742.1817536-6-yang.wang@deepcomputing.io \
    --to=yang.wang@deepcomputing.io \
    --cc=alex@ghiti.fr \
    --cc=aou@eecs.berkeley.edu \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlan@kernel.org \
    --cc=groeck@chromium.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=robh@kernel.org \
    --cc=sandie.cao@deepcomputing.io \
    --cc=spacemit@lists.linux.dev \
    --cc=troy.mitchell@linux.spacemit.com \
    --cc=tzungbi@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox