From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EC59606A32C for ; Fri, 4 Dec 2026 04:22:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=GaWYie4Z; dkim=fail reason="signature verification failed" (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=Ht7oyv9M DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:From:Cc:Message-Id:In-Reply-To: References:Date:To:Mime-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=k1rl29Ns3i1t9VCYcklEVv7azL6z1ngscA4Yo36NewM=; b=GaWYie4ZW/6UNxbGaj6itBoqII ZQxr0dkzaSVPfZA8vbTg6E67+G0+vNSdTWUbQg0UGo1+bCqZpGpa2LWJ6ZRCwgKiZ6ubBJX0/vFVl +HN/RRwLSidXnk2tpixJ5Wm88LjHStkjt2X/cESmVRq+nA8Do20ip2Ln9xd/piEwfgNeR7G2Ek1QP 9GJarYW23sMmhOjSyOyRvsqZwCNVG9zJOLeM694ByMMghFp0CnJ8o3uYdTjd+EzJPAx8gjzGCp/JI bOvPGMjzm5aVsJlsa0luDVxlprv9Rn0+qXJg9lRshASZELBp+ac+ueu2VjEC3JhoS5chYjU66i3wq kayZGmCw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZKoQ-0000000GbxO-4AYZ; Fri, 04 Dec 2026 04:22:23 +0000 Received: from va-2-35.ptr.blmpb.com ([209.127.231.35]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZKnm-0000000Gbev-020c for linux-riscv@lists.infradead.org; Fri, 04 Dec 2026 04:22:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=deepcomputing-io.20200927.dkim.feishu.cn; t=1796358097; 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=Ht7oyv9M7iibQ+ZrUqCUGkzPTLPMaYfpGx/G/IhGhWImyQEeNzx1fIQp81IMvYzBfkfQcB JWh3u1bVsoMwkqvkiWrQcxmSlAKv+QxhAR/qjCGc8VPOe5fFkcu4YlBE5/ZJzgA3ZqYGE2 fggSH3T5ra+VvUAU0aHcscZYVlMF7FajY9Z8K5+EeNFr2QrG7fW4ywiO1RLGO7IEmxNotr ON+jJwURzECA8YyucHusXB9QVIE6dfWMave70YG6p5hm0t6FfnXP9+noRJnHL1A5BA1Iz7 b7v/geSr+iWgJG5np0wzLrgsoBfuh6jju/ozBnfkatW+8boIu0sDgWzgO1V84g== X-Archived-By: Mime-Version: 1.0 Received: from ubuntu.. ([183.23.101.24]) by smtp.feishu.cn with ESMTPS; Fri, 04 Dec 2026 12:21:35 +0800 To: Date: Fri, 4 Dec 2026 12:21:07 +0800 References: <20261204042112.2985112-1-yang.wang@deepcomputing.io> X-Original-From: Wang Yang In-Reply-To: <20261204042112.2985112-1-yang.wang@deepcomputing.io> Message-Id: <20261204042112.2985112-10-yang.wang@deepcomputing.io> X-Lms-Return-Path: Cc: "Benson Leung" , "Tzung-Bi Shih" , "Yixun Lan" , "Troy Mitchell" , "Guenter Roeck" , , , From: "Wang Yang" Subject: [PATCH v2 09/13] platform/chrome: Add SpacemiT K3 eSPI EC transport X-Mailer: git-send-email 2.43.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261203_202159_165863_5A5E5036 X-CRM114-Status: GOOD ( 27.33 ) X-Spam-Score: -1.9 (-) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv