From patchwork Mon Nov 9 04:25:50 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Boichat X-Patchwork-Id: 7580491 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 563329F1C2 for ; Mon, 9 Nov 2015 04:26:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 56A7320462 for ; Mon, 9 Nov 2015 04:26:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2D727203E6 for ; Mon, 9 Nov 2015 04:26:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753121AbbKIEZ7 (ORCPT ); Sun, 8 Nov 2015 23:25:59 -0500 Received: from mail-pa0-f47.google.com ([209.85.220.47]:36448 "EHLO mail-pa0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752609AbbKIEZz (ORCPT ); Sun, 8 Nov 2015 23:25:55 -0500 Received: by pacdm15 with SMTP id dm15so161370361pac.3 for ; Sun, 08 Nov 2015 20:25:55 -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; bh=hres9YjBYUego9d2WZULrcoyK0p+8IZCiBmg4sngMsE=; b=HXNM1/7T6iQHYa2Od+zBWrkHum2g9zKuD7XH6owM96iudhVSUI42jFPuIQ5c+srbJj 26p5rBBBRXX957GYK4Z6jFZ4djJgcEGiIJjEpyjXR286kMRpCXAGqKtbhyAkSF/wUnjI 33A3rTBlz4lNYovmOmZ2QlyNcbZbfSvzqiVzQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=hres9YjBYUego9d2WZULrcoyK0p+8IZCiBmg4sngMsE=; b=UHXKf67FbbKYxeB5o8x2wQsEeNHGfXF3YnycC4rINhccKKLxAEVcVSaRiQGeFZYZBP A4LubJ8PpwU09AWbTHwC62RK/kDA+yAyeAULLdp/ennQkfL6JmkYm+XS3VUtRGtgLuSv QB+7xygq1HoMpXXm0+IE53S3eVJffeC9hYpjcThjo4U13QPGmxDPnstNEKwM9LTa4mYF bGtNUXuJ22iGajTLllDKD785oBM8ZlheNldgg6LW+lA8/kDSFjoHI8ut24R/AQwIw8/L DAu4O0GH5xtDXhMmJ4nWEnBfPb9rZcrg+y7N5iCeoDMN4mBnYE05Wy/W9TYGpYenRjfZ H2qw== X-Gm-Message-State: ALoCoQkmKGW30FrvUJOTt+DuUlBj3NgggiE+67NdvBn4uEEqpY8lt7nfQCf54jvYw8veItuV965h X-Received: by 10.68.195.129 with SMTP id ie1mr36684699pbc.100.1447043155219; Sun, 08 Nov 2015 20:25:55 -0800 (PST) Received: from drinkcat.tpe.corp.google.com ([172.30.210.53]) by smtp.gmail.com with ESMTPSA id j12sm1811433pbq.55.2015.11.08.20.25.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 08 Nov 2015 20:25:54 -0800 (PST) From: Nicolas Boichat To: Lee Jones Cc: linux-kernel@vger.kernel.org, Javier Martinez Canillas , Olof Johansson , dianders@chromium.org, rspangler@chromium.org, gwendal@chromium.org, linux-spi@vger.kernel.org, Mark Brown Subject: [PATCH v2] mfd: cros ec: Lock the SPI bus while holding chipselect Date: Mon, 9 Nov 2015 12:25:50 +0800 Message-Id: <1447043150-4806-1-git-send-email-drinkcat@chromium.org> X-Mailer: git-send-email 2.6.0.rc2.230.g3dd15c0 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP cros_ec_cmd_xfer_spi and cros_ec_pkt_xfer_spi generally work like this: - Pull CS down (active), wait a bit, then send a command - Wait for response (multiple requests) - Wait a while, pull CS up (inactive) These operations, individually, lock the SPI bus, but there is nothing preventing the SPI framework from interleaving messages intended for other devices as the bus is unlocked in between. This is a problem as the EC expects CS to be held low for the whole duration. Solution: Lock the SPI bus during the whole transaction, to make sure that no other messages can be interleaved. Signed-off-by: Nicolas Boichat --- v2: Move bus_unlock earlier in the functions. Applies on top on linux-next/master (20151109) drivers/mfd/cros_ec_spi.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/drivers/mfd/cros_ec_spi.c b/drivers/mfd/cros_ec_spi.c index 6a0f6ec..d538284 100644 --- a/drivers/mfd/cros_ec_spi.c +++ b/drivers/mfd/cros_ec_spi.c @@ -97,6 +97,11 @@ static void debug_packet(struct device *dev, const char *name, u8 *ptr, #endif } +/** + * terminate_request - turn off CS + * + * Requires spi bus to be locked. + */ static int terminate_request(struct cros_ec_device *ec_dev) { struct cros_ec_spi *ec_spi = ec_dev->priv; @@ -113,7 +118,7 @@ static int terminate_request(struct cros_ec_device *ec_dev) trans.delay_usecs = ec_spi->end_of_msg_delay; spi_message_add_tail(&trans, &msg); - ret = spi_sync(ec_spi->spi, &msg); + ret = spi_sync_locked(ec_spi->spi, &msg); /* Reset end-of-response timer */ ec_spi->last_transfer_ns = ktime_get_ns(); @@ -130,6 +135,8 @@ static int terminate_request(struct cros_ec_device *ec_dev) * receive_n_bytes - receive n bytes from the EC. * * Assumes buf is a pointer into the ec_dev->din buffer + * + * Requires spi bus to be locked. */ static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) { @@ -147,7 +154,7 @@ static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) spi_message_init(&msg); spi_message_add_tail(&trans, &msg); - ret = spi_sync(ec_spi->spi, &msg); + ret = spi_sync_locked(ec_spi->spi, &msg); if (ret < 0) dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret); @@ -163,6 +170,8 @@ static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n) * * The received data is placed into ec_dev->din. * + * Requires spi bus to be locked. + * * @ec_dev: ChromeOS EC device * @need_len: Number of message bytes we need to read */ @@ -272,6 +281,8 @@ static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev, * * The received data is placed into ec_dev->din. * + * Requires spi bus to be locked. + * * @ec_dev: ChromeOS EC device * @need_len: Number of message bytes we need to read */ @@ -391,10 +402,10 @@ static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, } rx_buf = kzalloc(len, GFP_KERNEL); - if (!rx_buf) { - ret = -ENOMEM; - goto exit; - } + if (!rx_buf) + return -ENOMEM; + + spi_bus_lock(ec_spi->spi->master); /* * Leave a gap between CS assertion and clocking of data to allow the @@ -414,7 +425,7 @@ static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, trans.len = len; trans.cs_change = 1; spi_message_add_tail(&trans, &msg); - ret = spi_sync(ec_spi->spi, &msg); + ret = spi_sync_locked(ec_spi->spi, &msg); /* Get the response */ if (!ret) { @@ -440,6 +451,9 @@ static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev, } final_ret = terminate_request(ec_dev); + + spi_bus_unlock(ec_spi->spi->master); + if (!ret) ret = final_ret; if (ret < 0) @@ -520,10 +534,10 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, } rx_buf = kzalloc(len, GFP_KERNEL); - if (!rx_buf) { - ret = -ENOMEM; - goto exit; - } + if (!rx_buf) + return -ENOMEM; + + spi_bus_lock(ec_spi->spi->master); /* Transmit phase - send our message */ debug_packet(ec_dev->dev, "out", ec_dev->dout, len); @@ -534,7 +548,7 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, trans.cs_change = 1; spi_message_init(&msg); spi_message_add_tail(&trans, &msg); - ret = spi_sync(ec_spi->spi, &msg); + ret = spi_sync_locked(ec_spi->spi, &msg); /* Get the response */ if (!ret) { @@ -560,6 +574,9 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev, } final_ret = terminate_request(ec_dev); + + spi_bus_unlock(ec_spi->spi->master); + if (!ret) ret = final_ret; if (ret < 0)