From patchwork Wed Nov 25 05:51:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Boichat X-Patchwork-Id: 7695471 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C2C7FBF90C for ; Wed, 25 Nov 2015 05:51:57 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BBDCB208A2 for ; Wed, 25 Nov 2015 05:51:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B8BB92085D for ; Wed, 25 Nov 2015 05:51:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932577AbbKYFvy (ORCPT ); Wed, 25 Nov 2015 00:51:54 -0500 Received: from mail-pa0-f48.google.com ([209.85.220.48]:33728 "EHLO mail-pa0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932574AbbKYFvu (ORCPT ); Wed, 25 Nov 2015 00:51:50 -0500 Received: by pabfh17 with SMTP id fh17so47776720pab.0 for ; Tue, 24 Nov 2015 21:51:49 -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=j7Me5zeVwDTUvitvdScAK95DAh2gRE1VBZOUWzETA78=; b=PBqPKJ+iqE9z/5pAjHmhbVx/+qlPhBqlAcPQ2soCuqprARtAsEOWoLH6iS7CRRvp0T Ays85F/C1dp1rq4ZN0jsRKaz7r76453BdieUsEnfccEKjPytAvJ9x0SC+XvyYR5GvAxy vJzOWr2RcgftQBOkn7/TMZxJAeWq3MuBwaTwk= 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=j7Me5zeVwDTUvitvdScAK95DAh2gRE1VBZOUWzETA78=; b=Hv1Z9ljvR49oYjrpq+5dSe7v6U6Lusq8dC98JnQqDHno593Ow5I9dSjsCOJdMZY//I WyqNKx82Acslw/HU+eek1iDdB2gRzmB56FjOr9OxIomxrY2oEvTrzCv5JammKYam8Bq0 a9noxSIaIx8V7MvGECPIteeTxn4htcrKdhd2a5W73rt6OYYivQG/eEyMImpiHsPDEauM kAnOuU7TDT5xx8xmi4Q1d8vjN6OYNrqdKMoM5OHNKTgp7mzOLC9JxE1oQtZxaGziQF9G rTQiRlPmnnNFSdKvzguCBlG7QcgJWTip3BIrB+een/kS8LzBnmJWw5xxXzYRias9fABr TrSA== X-Gm-Message-State: ALoCoQnwVABZxFq3m250v+6u68I8Qm+GP0CVSlCPIbVNr5pn5ggq2uf7gt2WEztyoCPifbPsR0fh X-Received: by 10.66.164.234 with SMTP id yt10mr25867039pab.11.1448430709682; Tue, 24 Nov 2015 21:51:49 -0800 (PST) Received: from drinkcat.tpe.corp.google.com ([172.30.210.53]) by smtp.gmail.com with ESMTPSA id fk8sm18446472pab.33.2015.11.24.21.51.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 24 Nov 2015 21:51:49 -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 v3] mfd: cros ec: Lock the SPI bus while holding chipselect Date: Wed, 25 Nov 2015 13:51:07 +0800 Message-Id: <1448430667-4213-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=-7.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,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 Acked-by: Lee Jones Reviewed-by: Gwendal Grignou --- v2: Move bus_unlock earlier in the functions. v3: Remove comments. Applies on top on linux-next/master (20151124) drivers/mfd/cros_ec_spi.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/drivers/mfd/cros_ec_spi.c b/drivers/mfd/cros_ec_spi.c index 6a0f6ec..d6af52d 100644 --- a/drivers/mfd/cros_ec_spi.c +++ b/drivers/mfd/cros_ec_spi.c @@ -113,7 +113,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(); @@ -147,7 +147,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); @@ -391,10 +391,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 +414,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 +440,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 +523,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 +537,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 +563,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)