Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4344008imu; Mon, 7 Jan 2019 21:29:31 -0800 (PST) X-Google-Smtp-Source: ALg8bN5DohMdybxO/lipjATXVPc3+1zu4jQFRUTJDMkLIe6WFiufROdoL6Suhhqcc91vA5QjbsWF X-Received: by 2002:a17:902:9691:: with SMTP id n17mr406322plp.9.1546925371505; Mon, 07 Jan 2019 21:29:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546925371; cv=none; d=google.com; s=arc-20160816; b=fvHOgRdJk943e1jFsZLumhAOfhq6r7xZ2Dy5QvJjOfVct2P2a6Z7eRo3DN7mjS3qyB 2AHE9bgckXRHlPBAymFmDJuYfFjkoDjtrrO44gwdplHUK1U8NLpBWvM4V8W4fj+hwHib d6/kRgWxRDiCZyuTGrSEvE/Kkngs07Ls5FkJwdA8cssFz3e4U1uTY9bOw75bYPkk9Sq3 9v3MSzzzCvudRFbaVLkNP2tkQ2uE9C+RPkI+Z2Y6ipktDM+d2T+K3XaqdMYSkpZFL8Pr LVhnzM+np1/SFah9V2lsWg82KX9ip/jqLuefVaxhOZWpD5R1iFR+hO0Lq2mfNcQe3zuR S2Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=Lref0HVEoFjGXZ/N04W2nPFJxgeSeaU7c9/oCO39rUw=; b=Q9VplLyNdOpmaJv+Wv6/Wu7IFut/G2UBCwqUuZB86/56u4QuKkeaYU31CvSZPUKKsK rFqhLmlUvqK5T3Zp5RSfrBwFP8dlu+sAD2tuNsQ+xGuZK2djyuYv44ua9g15qcSnrIaX tKQa768DQ5ylbU5c5Pb7pGdjzj4nNPMgKhFJ4enwCeoADjMsgBbe2P9bimn53C8/ajix QR3DdjL90Gz1d0wGJAPoSMIJzgAYA5DJfbIsZ9BInfg6rmOn2n15tp1FO9AzLglnPapr ptnKvH1fqZKRc6KBcuGuJ8LfM6ESzf9cLX5alEbBM9jTS49pY6EPsKkaUrJe/NzkyEy2 eing== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@chromium.org header.s=google header.b=IQ4AwHUW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j34si59862152pgj.557.2019.01.07.21.29.16; Mon, 07 Jan 2019 21:29:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@chromium.org header.s=google header.b=IQ4AwHUW; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728007AbfAHF0m (ORCPT + 99 others); Tue, 8 Jan 2019 00:26:42 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:35136 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727693AbfAHF0l (ORCPT ); Tue, 8 Jan 2019 00:26:41 -0500 Received: by mail-pg1-f196.google.com with SMTP id s198so1207352pgs.2 for ; Mon, 07 Jan 2019 21:26:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Lref0HVEoFjGXZ/N04W2nPFJxgeSeaU7c9/oCO39rUw=; b=IQ4AwHUWxlICG4xsDxk7PuhpCKmWznIMKIv6ZbM5gDoygXpLClnchQ92JRXMengraE RS3t4YocPj41hfzCh4DGb4qNAZPCRKh2dzcfmWaCi3xAI89kUrvYdgxPuW3xtBWBu9PH 5+WYhc9ZOFdJ0+cxJg7GejCl2Wvdvz4Y5QQ2o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Lref0HVEoFjGXZ/N04W2nPFJxgeSeaU7c9/oCO39rUw=; b=nKvmgMY9/RIzdwZ5jgbYW3oajrHr006v0bf3Cj9aOHQfLvMjC4XVseXLQK+n16u2s3 cNEQSc8e0G0BDMmcrVlSuQF4pwy/RfXkBeVVnk33Oq4JikDvtgEFeUaliFXgFDEcw9NK iF1Z53IsBGpkbJZHaE2x8Fyrf22Ey7WDQXCOa7YbuiRh6jhmYohmgqFea7mz92p6nS4m gkgeyfUQqa51cvXv+p9KGLh9q6+mxw6BB0szgLxAKjvcQj9sLw1eXCfMOYVlWWL/Qfya 5hjWxLybKMzu/xXdBCWxqiQOIbwqNR+5ImuFTLPk/kJBEbeddd1676gHJYM3ZAeJopmQ Mm/Q== X-Gm-Message-State: AJcUukf2cGMFZ6Omuh2ynfC5EFUd51jSpcvXmoELGWXbbLarZI6Y7yeX cdyTljWxYzm/DmI27N8syTlKNg== X-Received: by 2002:a62:db41:: with SMTP id f62mr377018pfg.123.1546925200574; Mon, 07 Jan 2019 21:26:40 -0800 (PST) Received: from pihsun-z840.tpe.corp.google.com ([2401:fa00:1:10:7889:7a43:f899:134c]) by smtp.googlemail.com with ESMTPSA id 64sm88106874pff.101.2019.01.07.21.26.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Jan 2019 21:26:39 -0800 (PST) From: Pi-Hsun Shih Cc: Pi-Hsun Shih , Nicolas Boichat , Enric Balletbo Serra , Guenter Roeck , Benson Leung , Enric Balletbo i Serra , linux-kernel@vger.kernel.org (open list) Subject: [RFC v2 5/6] mfd: add EC host command support using rpmsg. Date: Tue, 8 Jan 2019 13:26:05 +0800 Message-Id: <20190108052606.59426-6-pihsun@chromium.org> X-Mailer: git-send-email 2.20.1.97.g81188d93c3-goog In-Reply-To: <20190108052606.59426-1-pihsun@chromium.org> References: <20190108052606.59426-1-pihsun@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add EC host command support through rpmsg. Cc: Enric Balletbo Serra Cc: Guenter Roeck Signed-off-by: Pi-Hsun Shih --- Changes from v1: - Code format fix based on feedback for cros_ec_rpmsg.c. - Extract feature detection for SCP into separate patch (Patch 6). --- drivers/platform/chrome/Kconfig | 8 ++ drivers/platform/chrome/Makefile | 1 + drivers/platform/chrome/cros_ec_rpmsg.c | 159 ++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 drivers/platform/chrome/cros_ec_rpmsg.c diff --git a/drivers/platform/chrome/Kconfig b/drivers/platform/chrome/Kconfig index 16b1615958aa2d..e3f63f3d67711b 100644 --- a/drivers/platform/chrome/Kconfig +++ b/drivers/platform/chrome/Kconfig @@ -72,6 +72,14 @@ config CROS_EC_SPI response time cannot be guaranteed, we support ignoring 'pre-amble' bytes before the response actually starts. +config CROS_EC_RPMSG + tristate "ChromeOS Embedded Controller (rpmsg)" + depends on MFD_CROS_EC && RPMSG && OF + help + If you say Y here, you get support for talking to the ChromeOS EC + through rpmsg. This uses a simple byte-level protocol with a + checksum. + config CROS_EC_LPC tristate "ChromeOS Embedded Controller (LPC)" depends on MFD_CROS_EC && ACPI && (X86 || COMPILE_TEST) diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile index cd591bf872bbe9..3e3190af2b50f4 100644 --- a/drivers/platform/chrome/Makefile +++ b/drivers/platform/chrome/Makefile @@ -8,6 +8,7 @@ cros_ec_ctl-objs := cros_ec_sysfs.o cros_ec_lightbar.o \ obj-$(CONFIG_CROS_EC_CTL) += cros_ec_ctl.o obj-$(CONFIG_CROS_EC_I2C) += cros_ec_i2c.o obj-$(CONFIG_CROS_EC_SPI) += cros_ec_spi.o +obj-$(CONFIG_CROS_EC_RPMSG) += cros_ec_rpmsg.o cros_ec_lpcs-objs := cros_ec_lpc.o cros_ec_lpc_reg.o cros_ec_lpcs-$(CONFIG_CROS_EC_LPC_MEC) += cros_ec_lpc_mec.o obj-$(CONFIG_CROS_EC_LPC) += cros_ec_lpcs.o diff --git a/drivers/platform/chrome/cros_ec_rpmsg.c b/drivers/platform/chrome/cros_ec_rpmsg.c new file mode 100644 index 00000000000000..92c967b4db4862 --- /dev/null +++ b/drivers/platform/chrome/cros_ec_rpmsg.c @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Copyright 2018 Google LLC. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply + * + * This is only used for old EC proto version, and is not supported for this + * driver. + * + * @ec_dev: ChromeOS EC device + * @ec_msg: Message to transfer + */ +static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev, + struct cros_ec_command *ec_msg) +{ + return -EINVAL; +} + +/** + * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply + * + * @ec_dev: ChromeOS EC device + * @ec_msg: Message to transfer + */ +static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev, + struct cros_ec_command *ec_msg) +{ + struct ec_host_response *response; + struct rpmsg_device *rpdev = ec_dev->priv; + int len; + u8 sum; + int ret; + int i; + + ec_msg->result = 0; + len = cros_ec_prepare_tx(ec_dev, ec_msg); + dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); + + /* + * TODO: This currently relies on that mtk_rpmsg send actually blocks + * until ack. Should do the wait here instead. + */ + ret = rpmsg_send(rpdev->ept, ec_dev->dout, len); + if (ret) { + dev_err(ec_dev->dev, "rpmsg send failed\n"); + return ret; + } + + /* check response error code */ + response = (struct ec_host_response *)ec_dev->din; + ec_msg->result = response->result; + + ret = cros_ec_check_result(ec_dev, ec_msg); + if (ret) + goto exit; + + if (response->data_len > ec_msg->insize) { + dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", + response->data_len, ec_msg->insize); + ret = -EMSGSIZE; + goto exit; + } + + /* copy response packet payload and compute checksum */ + memcpy(ec_msg->data, ec_dev->din + sizeof(*response), + response->data_len); + + sum = 0; + for (i = 0; i < sizeof(*response) + response->data_len; i++) + sum += ec_dev->din[i]; + + if (sum) { + dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n", + sum); + ret = -EBADMSG; + goto exit; + } + + ret = response->data_len; +exit: + if (ec_msg->command == EC_CMD_REBOOT_EC) + msleep(EC_REBOOT_DELAY_MS); + + return ret; +} + +static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data, + int len, void *priv, u32 src) +{ + struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); + + if (len > ec_dev->din_size) { + dev_warn(ec_dev->dev, + "ipi received length %d > din_size, truncating", len); + len = ec_dev->din_size; + } + + memcpy(ec_dev->din, data, len); + + return 0; +} + +static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) +{ + struct device *dev = &rpdev->dev; + struct cros_ec_device *ec_dev; + int ret; + + ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); + if (!ec_dev) + return -ENOMEM; + + ec_dev->dev = dev; + ec_dev->priv = rpdev; + ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg; + ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg; + ec_dev->phys_name = dev_name(&rpdev->dev); + ec_dev->din_size = sizeof(struct ec_host_response) + + sizeof(struct ec_response_get_protocol_info); + ec_dev->dout_size = sizeof(struct ec_host_request); + dev_set_drvdata(dev, ec_dev); + + ret = cros_ec_register(ec_dev); + if (ret) { + dev_err(dev, "cannot register EC\n"); + return ret; + } + + return 0; +} + +static const struct rpmsg_device_id cros_ec_rpmsg_device_id[] = { + { .name = "cros-ec-rpmsg", }, + { } +}; +MODULE_DEVICE_TABLE(rpmsg, cros_ec_rpmsg_device_id); + +static struct rpmsg_driver cros_ec_driver_rpmsg = { + .drv.name = KBUILD_MODNAME, + .id_table = cros_ec_rpmsg_device_id, + .probe = cros_ec_rpmsg_probe, + .callback = cros_ec_rpmsg_callback, +}; + +module_rpmsg_driver(cros_ec_driver_rpmsg); + +MODULE_LICENSE("GPL v2"); +MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)"); -- 2.20.1.97.g81188d93c3-goog