From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.127.231.36 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796358104; b=j8XivB26pcZ6Kn6fMbPjMd2gR0MtwE2bxNxb1ZBUXMXKDKo1lOYoBw7Dtoz3uM0hU0zL aac3ejF87nONochWaNZM2pxt+cJSHHywmay0EJOOngTBHJqizxlN7yM1xvwTBuTnuFP1w /oG0Afpd0MA9sr8hd00VAFnTNGN9gjNridxL3PT+AXBYGhsvpqPYplH6oLG0B/agptqbM 9X4FF0vezfc28XiKoGxxveWq/8HdgYKe9EUArCyVSm4OOq9gekXtsyjG5ivvZlM0pSbq7 4yblyPtju5kNB84j0IM5SZcFAnZbSOmzOQusAmbIR0zov3C+o4jGGCH44ki509kI0mw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796358104; h=DMARC-Filter:DKIM-Signature:Subject:Message-Id:Received: X-Lms-Return-Path:To:From:References:Cc:Mime-Version: Content-Transfer-Encoding:Content-Type:X-Mailer:In-Reply-To:Date: X-Original-From; bh=cvF6JRxa/8dti0w4/mkekkbKdYzVg43T7kxq1vZ9EmA=; b=vHC4yxhkROpnP7kJ+8mDhYLW+GVGTRnlumL9PR3VdNg2sOhaXKcF4AwBe4s6LfIUUhCQ nDxnoDQCE7bPETWGmUK1xdXC2Ot4kFoJZGgmqAg5u1RmM/Rftm+A4Ww2aqZ9DvlOxlnDM 2/55JNUodfsDuGPJ+sg0kH+kTLLWDhXtCy0I50d6DgJJsHOu45OprhU7F6Mamhr4aJUA5 eq4yT1/4ZTgTGGoTTdxtK2S3LM2OqdtzRgCTjyQT3FxVGei4lrIaihmCTjL1enEYzYmQm u8bNBgazRqFjmoJb9lnyYryU0UTEA4oJJXmnDNDRCAnH1gL0lh0J+SIgRky/hYybCtw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=deepcomputing.io; spf=pass smtp.mailfrom=deepcomputing.io; dkim=pass header.d=deepcomputing-io.20200927.dkim.feishu.cn header.i=@deepcomputing-io.20200927.dkim.feishu.cn header.a=rsa-sha256 header.s=s1 header.b="f3oh/3gV"; arc=none smtp.remote-ip=209.127.231.36 Received: from va-2-36.ptr.blmpb.com (va-2-36.ptr.blmpb.com [209.127.231.36]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE64C606A6A0 for ; Fri, 4 Dec 2026 04:21:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AE64C606A6A0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=deepcomputing.io Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=deepcomputing.io Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=deepcomputing-io.20200927.dkim.feishu.cn header.i=@deepcomputing-io.20200927.dkim.feishu.cn header.a=rsa-sha256 header.s=s1 header.b=f3oh/3gV DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=deepcomputing-io.20200927.dkim.feishu.cn; t=1796358098; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=cvF6JRxa/8dti0w4/mkekkbKdYzVg43T7kxq1vZ9EmA=; b=f3oh/3gV7qjvE6lz/XhmmNAx0JakQPrkUbiuSUlbP6yi1eJCoUKmUGUiQrpMbcroEHvt/0 FXPmfuao3uSF9PC+FPTcrJlY76ZM4ZtaDRxRh0Ct3OqGk1YORKEX7KfjbosASLhBmEdLtJ S66994wbyZAkvnp6MgI2lEJI18UYcZ/8UygE2DrR8r/S3Zrj9cxCvLkBUQD2Nad3/MZvuR 9iSPZL1yKDTwdf1eXtS0oXZyrm0PL/JzzjsE9uUwd6wMGOvFNw5RddzHrslCiRoME22095 UXWRiCTpwvUaGXzGith/zlRBbpGKX6+XQbEjAAIxf6EvBCe/A+MI9/ZsxfWRSQ== Subject: [PATCH v2 09/13] platform/chrome: Add SpacemiT K3 eSPI EC transport Message-Id: <20261204042112.2985112-10-yang.wang@deepcomputing.io> Received: from ubuntu.. ([183.23.101.24]) by smtp.feishu.cn with ESMTPS; Fri, 04 Dec 2026 12:21:35 +0800 X-Lms-Return-Path: To: From: "Wang Yang" References: <20261204042112.2985112-1-yang.wang@deepcomputing.io> Cc: "Benson Leung" , "Tzung-Bi Shih" , "Yixun Lan" , "Troy Mitchell" , "Guenter Roeck" , , , X-Mailing-List: chrome-platform@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=UTF-8 X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204042112.2985112-1-yang.wang@deepcomputing.io> Date: Fri, 4 Dec 2026 12:21:07 +0800 X-Original-From: Wang Yang Map the K3 non-linear window and reuse the common LPC-style protocol helpers. Keep K3 readiness, five-second timeout and link recovery in the vendor backend. Recover a timed-out link without replaying a command that may already have executed. Let the EC core request the firmware-configured event IRQ. Use module cros_ec_espi_spacemit, CONFIG_CROS_EC_SPACEMIT_K3 and compatible spacemit,k3-cros-ec. This is a K3-specific interface. Require the firmware event IRQ before accessing the EC. Serialize host commands and direct memory reads with link recovery. Retry transient negotiation failures, then wait for the EC ID on the established link. Give the already negotiated link time to publish its ID before resetting it at probe. Recover synchronously during protocol negotiation. Once the core installs the dedicated GPIO IRQ action, defer link recovery to a workqueue so the GPIO bank thread can service other pins. Prevent queued recovery from outliving a failed probe. Reject window accesses while recovery is active. Check request and response sizes against the K3 packet window. Check a pending link recovery before enabling the event IRQ on resume. Prepared with OpenAI Codex assistance. Signed-off-by: Wang Yang --- drivers/platform/chrome/Kconfig | 12 + drivers/platform/chrome/Makefile | 1 + .../platform/chrome/cros_ec_espi_spacemit.c | 568 ++++++++++++++++++ 3 files changed, 581 insertions(+) create mode 100644 drivers/platform/chrome/cros_ec_espi_spacemit.c diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kconfig index 2e4fec3820f5..11a8320b202d 100644 --- a/drivers/platform/chrome/Kconfig +++ b/drivers/platform/chrome/Kconfig @@ -160,6 +160,18 @@ 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 + select CROS_EC_LPC_PROTO + 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_espi_spacemit. + config CROS_EC_PROTO tristate help diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile index 00d3d9fba1e0..a0731d359104 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_espi_spacemit.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_espi_spacemit.c b/drivers/platform/chrome/cros_ec_espi_spacemit.c new file mode 100644 index 000000000000..cb5c37d1a24d --- /dev/null +++ b/drivers/platform/chrome/cros_ec_espi_spacemit.c @@ -0,0 +1,568 @@ +// 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cros_ec.h" +#include "cros_ec_lpc_proto.h" + +#define DRV_NAME "cros_ec_espi_spacemit" + +/* 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. + * + * 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-0x013: Host arguments (4 bytes; 0x014-0x017 reserved) + * 0x018-0x113: Protocol v2 parameters (up to 252 bytes) + * 0x100-0x1FF: Packet data area (256 bytes for protocol v3) + * 0x200-0x2FF: Memory map region (256 bytes) + * + * Protocol v2 parameters and protocol v3 packets are used separately. + */ + +/* 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 */ +#define ESPI_HOST_PARAM_SIZE EC_PROTO2_MAX_PARAM_SIZE + +/** + * struct cros_ec_espi - eSPI device-specific data + * @espi_shared_mem_base: Base address of eSPI shared memory region + * @invalid_status_polls: Consecutive absent-endpoint status reads + * @needs_recovery: Restore the link before the next window access. + * @ec: EC whose peripheral window is being recovered. + * @recovery_work: Restore the link outside the GPIO interrupt thread. + * @io_mutex: Serialize window accesses and changes to the recovery state. + */ +struct cros_ec_espi { + void __iomem *espi_shared_mem_base; + unsigned int invalid_status_polls; + bool needs_recovery; + struct cros_ec_device *ec; + struct work_struct recovery_work; + /* Serialize commands, direct reads and recovery state transitions. */ + struct mutex io_mutex; +}; + +/* Validate an entire transfer before touching the shared window. */ +static int lpc_to_espi_offset(struct cros_ec_device *ec, u32 lpc_addr, + unsigned int length) +{ + u32 start, size, offset; + + if (lpc_addr == EC_LPC_ADDR_HOST_CMD || lpc_addr == EC_LPC_ADDR_HOST_DATA) { + if (length != 1) + return -EMSGSIZE; + return lpc_addr == EC_LPC_ADDR_HOST_CMD ? + ESPI_HOST_CMD_OFFSET : ESPI_HOST_DATA_OFFSET; + } + + if (lpc_addr >= EC_LPC_ADDR_MEMMAP && + lpc_addr < EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE) { + start = EC_LPC_ADDR_MEMMAP; + size = EC_MEMMAP_SIZE; + offset = ESPI_MEMMAP_OFFSET; + } else if (ec->proto_version > 2 && + lpc_addr >= EC_LPC_ADDR_HOST_PACKET && + lpc_addr < EC_LPC_ADDR_HOST_PACKET + EC_LPC_HOST_PACKET_SIZE) { + start = EC_LPC_ADDR_HOST_PACKET; + size = EC_LPC_HOST_PACKET_SIZE; + offset = ESPI_HOST_PACKET_OFFSET; + } else if (ec->proto_version <= 2 && + lpc_addr >= EC_LPC_ADDR_HOST_ARGS && + lpc_addr < EC_LPC_ADDR_HOST_PARAM) { + start = EC_LPC_ADDR_HOST_ARGS; + size = EC_LPC_ADDR_HOST_PARAM - EC_LPC_ADDR_HOST_ARGS; + offset = ESPI_HOST_ARGS_OFFSET; + } else if (ec->proto_version <= 2 && + lpc_addr >= EC_LPC_ADDR_HOST_PARAM && + lpc_addr < EC_LPC_ADDR_HOST_PARAM + ESPI_HOST_PARAM_SIZE) { + start = EC_LPC_ADDR_HOST_PARAM; + size = ESPI_HOST_PARAM_SIZE; + offset = ESPI_HOST_PARAM_OFFSET; + } else { + return -EINVAL; + } + + if (length > size - (lpc_addr - start)) + return -EMSGSIZE; + + return offset + lpc_addr - start; +} + +/* 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; + int espi_offset; + u8 sum = 0; + int i; + + espi_offset = lpc_to_espi_offset(ec, offset, length); + if (espi_offset < 0) + return espi_offset; + + for (i = 0; i < length; ++i) { + addr = ec_espi->espi_shared_mem_base + espi_offset + i; + + /* eSPI shared memory: 1-byte access */ + dest[i] = readb(addr); + sum += dest[i]; + } + + if (offset == EC_LPC_ADDR_HOST_CMD) { + if (*dest == ESPI_INVALID_STATUS_VALUE) + ec_espi->invalid_status_polls++; + else + ec_espi->invalid_status_polls = 0; + } + + if (offset == EC_LPC_ADDR_HOST_CMD && + ec_espi->invalid_status_polls >= ESPI_INVALID_STATUS_MAX_POLLS) + return -ETIMEDOUT; + + /* 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; + int espi_offset; + u8 sum = 0; + int i; + + espi_offset = lpc_to_espi_offset(ec, offset, length); + if (espi_offset < 0) + return espi_offset; + + for (i = 0; i < length; ++i) { + addr = ec_espi->espi_shared_mem_base + espi_offset + i; + + /* eSPI shared memory: 1-byte access */ + writeb(msg[i], addr); + sum += msg[i]; + } + + /* Return checksum of all bytes written */ + return sum; +} + +static const struct cros_ec_lpc_io cros_ec_espi_io = { + .read = cros_ec_espi_read_bytes, + .write = cros_ec_espi_write_bytes, + .response_timeout_ms = ESPI_CMD_TIMEOUT_MS, + .timeout_error = -ETIMEDOUT, +}; + +static int cros_ec_pkt_xfer_espi_once(struct cros_ec_device *ec, + struct cros_ec_command *msg) +{ + struct cros_ec_espi *ec_espi = ec->priv; + + if (msg->outsize > EC_LPC_HOST_PACKET_SIZE - sizeof(struct ec_host_request) || + msg->insize > EC_LPC_HOST_PACKET_SIZE - sizeof(struct ec_host_response)) + return -EMSGSIZE; + + ec_espi->invalid_status_polls = 0; + usleep_range(500, 1000); + + return cros_ec_lpc_pkt_xfer(ec, &cros_ec_espi_io, msg); +} + +static int cros_ec_cmd_xfer_espi_once(struct cros_ec_device *ec, + struct cros_ec_command *msg) +{ + struct cros_ec_espi *ec_espi = ec->priv; + + if (msg->outsize > ESPI_HOST_PARAM_SIZE || msg->insize > ESPI_HOST_PARAM_SIZE) + return -EMSGSIZE; + + ec_espi->invalid_status_polls = 0; + usleep_range(500, 1000); + + return cros_ec_lpc_cmd_xfer(ec, &cros_ec_espi_io, msg); +} + +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_wait_for_id(struct cros_ec_device *ec, + unsigned long deadline) +{ + u8 id[2]; + int ret; + + do { + ret = cros_ec_espi_read_id(ec, id); + if (ret < 0) + return ret; + if (cros_ec_espi_valid_id(id)) { + /* The ID appears before the command window is ready. */ + msleep(100); + return 0; + } + usleep_range(ESPI_READY_POLL_INTERVAL_US, + ESPI_READY_POLL_INTERVAL_US + 10000); + } while (time_before(jiffies, deadline)); + + return -ETIMEDOUT; +} + +static int cros_ec_espi_recover_controller(struct cros_ec_device *ec, + unsigned long deadline) +{ + unsigned long interval = msecs_to_jiffies(ESPI_RECOVERY_RETRY_INTERVAL_MS); + int ret; + + do { + ret = spacemit_k3_espi_recover(ec->dev->parent); + if (!ret) + return cros_ec_espi_wait_for_id(ec, deadline); + if (ret != -ETIMEDOUT && ret != -EBUSY && ret != -EIO) + return ret; + if (time_after_eq(jiffies + interval, deadline)) + break; + msleep(ESPI_RECOVERY_RETRY_INTERVAL_MS); + } while (time_before(jiffies, deadline)); + + return ret; +} + +/* The parent may already have negotiated the link and mapped the window. */ +static int cros_ec_espi_check_link(struct cros_ec_device *ec) +{ + unsigned long deadline = jiffies + msecs_to_jiffies(ESPI_READY_TIMEOUT_MS); + int ret; + + ret = cros_ec_espi_wait_for_id(ec, jiffies + + msecs_to_jiffies(ESPI_RECOVERY_RETRY_INTERVAL_MS)); + if (ret == -ETIMEDOUT) + ret = cros_ec_espi_recover_controller(ec, deadline); + + return ret; +} + +static void cros_ec_espi_recovery_work(struct work_struct *work) +{ + struct cros_ec_espi *ec_espi = container_of(work, struct cros_ec_espi, + recovery_work); + unsigned long deadline = jiffies + msecs_to_jiffies(ESPI_READY_TIMEOUT_MS); + int ret; + + mutex_lock(&ec_espi->io_mutex); + if (!ec_espi->needs_recovery) { + mutex_unlock(&ec_espi->io_mutex); + return; + } + mutex_unlock(&ec_espi->io_mutex); + + /* While recovery owns the window, new callers return -EHOSTDOWN. */ + ret = cros_ec_espi_recover_controller(ec_espi->ec, deadline); + if (ret) { + dev_warn_ratelimited(ec_espi->ec->dev, "link recovery failed: %d\n", ret); + return; + } + + mutex_lock(&ec_espi->io_mutex); + ec_espi->needs_recovery = false; + mutex_unlock(&ec_espi->io_mutex); +} + +/* Called with io_mutex held. */ +static int cros_ec_espi_ensure_ready(struct cros_ec_device *ec) +{ + struct cros_ec_espi *ec_espi = ec->priv; + unsigned long deadline; + int ret; + + if (!ec_espi->needs_recovery) + return 0; + + /* + * The core negotiates the protocol before requesting our dedicated + * IRQ. Once its action is installed, transfers may already run in + * the GPIO interrupt thread, even before cros_ec_register() returns. + */ + if (irq_has_action(ec->irq)) { + queue_work(system_freezable_wq, &ec_espi->recovery_work); + return -EHOSTDOWN; + } + + /* The initial protocol negotiation needs a working link immediately. */ + deadline = jiffies + msecs_to_jiffies(ESPI_READY_TIMEOUT_MS); + ret = cros_ec_espi_recover_controller(ec, deadline); + ec_espi->needs_recovery = !!ret; + return ret; +} + +static int cros_ec_espi_xfer(struct cros_ec_device *ec, + struct cros_ec_command *msg, + int (*xfer_once)(struct cros_ec_device *ec, + struct cros_ec_command *msg)) +{ + struct cros_ec_espi *ec_espi = ec->priv; + int ret; + + mutex_lock(&ec_espi->io_mutex); + ret = cros_ec_espi_ensure_ready(ec); + if (ret) + goto unlock; + + ret = xfer_once(ec, msg); + /* + * A lost completion does not mean that the EC did not execute the + * command. Repair the link without replaying a potentially destructive + * operation, such as an I2C passthrough write. + */ + if (ret == -ETIMEDOUT) { + ec_espi->needs_recovery = true; + cros_ec_espi_ensure_ready(ec); + } +unlock: + mutex_unlock(&ec_espi->io_mutex); + + return ret; +} + +static int cros_ec_pkt_xfer_espi(struct cros_ec_device *ec, + struct cros_ec_command *msg) +{ + return cros_ec_espi_xfer(ec, msg, cros_ec_pkt_xfer_espi_once); +} + +static int cros_ec_cmd_xfer_espi(struct cros_ec_device *ec, + struct cros_ec_command *msg) +{ + return cros_ec_espi_xfer(ec, msg, cros_ec_cmd_xfer_espi_once); +} + +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 ret; + + mutex_lock(&ec_espi->io_mutex); + ret = cros_ec_espi_ensure_ready(ec); + if (!ret) + ret = cros_ec_lpc_readmem(ec, &cros_ec_espi_io, EC_LPC_ADDR_MEMMAP, + offset, bytes, dest); + mutex_unlock(&ec_espi->io_mutex); + + return ret; +} + +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; + int irq, ret; + + irq = platform_get_irq(pdev, 0); + if (irq < 0) + return irq; + + 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); + + mutex_init(&ec_espi->io_mutex); + + ec = cros_ec_device_alloc(dev); + if (!ec) + return -ENOMEM; + + ec->phys_name = dev_name(dev); + ec->irq = irq; + /* Use the IRQ trigger type specified by the board's device tree. */ + ec->irqflags = IRQF_ONESHOT; + 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); + ec_espi->ec = ec; + INIT_WORK(&ec_espi->recovery_work, cros_ec_espi_recovery_work); + + ret = cros_ec_espi_check_link(ec); + if (ret) + return dev_err_probe(dev, ret, "EC is not ready\n"); + + if (of_property_read_bool(dev->of_node, "wakeup-source")) { + ret = devm_device_init_wakeup(dev); + if (ret) + return ret; + } + + ret = cros_ec_register(ec); + if (ret) { + /* An IRQ during registration may already have queued recovery. */ + disable_work_sync(&ec_espi->recovery_work); + return dev_err_probe(dev, ret, "failed to register EC\n"); + } + + return 0; +} + +static void cros_ec_espi_remove(struct platform_device *pdev) +{ + struct cros_ec_device *ec = platform_get_drvdata(pdev); + struct cros_ec_espi *ec_espi = ec->priv; + + disable_work_sync(&ec_espi->recovery_work); + 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); + struct cros_ec_espi *ec_espi = ec_dev->priv; + + disable_work_sync(&ec_espi->recovery_work); + 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); + struct cros_ec_espi *ec_espi = ec_dev->priv; + int ret = 0; + + /* Recovery work is still frozen when the core sends its resume event. */ + mutex_lock(&ec_espi->io_mutex); + if (ec_espi->needs_recovery) { + ret = cros_ec_espi_check_link(ec_dev); + ec_espi->needs_recovery = !!ret; + } + enable_work(&ec_espi->recovery_work); + mutex_unlock(&ec_espi->io_mutex); + + /* Balance suspend_late() even if the link needs another recovery. */ + cros_ec_resume_early(ec_dev); + return ret; +} +#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 transport for the ChromeOS EC protocol"); +MODULE_ALIAS("platform:" DRV_NAME); -- 2.43.0