From: Lee Jones <lee.jones@linaro.org>
To: Nicolas Boichat <drinkcat@chromium.org>
Cc: linux-kernel@vger.kernel.org,
Javier Martinez Canillas <javier@osg.samsung.com>,
Olof Johansson <olof@lixom.net>,
dianders@chromium.org, rspangler@chromium.org,
gwendal@chromium.org, linux-spi@vger.kernel.org,
Mark Brown <broonie@kernel.org>
Subject: Re: [PATCH v3] mfd: cros ec: Lock the SPI bus while holding chipselect
Date: Wed, 25 Nov 2015 08:12:19 +0000 [thread overview]
Message-ID: <20151125081219.GL12874@x1> (raw)
In-Reply-To: <1448430667-4213-1-git-send-email-drinkcat@chromium.org>
On Wed, 25 Nov 2015, Nicolas Boichat wrote:
> 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 <drinkcat@chromium.org>
> ---
>
> 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(-)
Acked-by: Lee Jones <lee.jones@linaro.org>
> 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)
--
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
next prev parent reply other threads:[~2015-11-25 8:12 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-25 5:51 [PATCH v3] mfd: cros ec: Lock the SPI bus while holding chipselect Nicolas Boichat
2015-11-25 5:51 ` Nicolas Boichat
2015-11-25 8:12 ` Lee Jones [this message]
2015-11-25 22:37 ` Gwendal Grignou
2015-11-25 22:37 ` Gwendal Grignou
[not found] ` <1448430667-4213-1-git-send-email-drinkcat-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
2015-11-26 9:14 ` Lee Jones
2015-11-26 9:14 ` Lee Jones
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151125081219.GL12874@x1 \
--to=lee.jones@linaro.org \
--cc=broonie@kernel.org \
--cc=dianders@chromium.org \
--cc=drinkcat@chromium.org \
--cc=gwendal@chromium.org \
--cc=javier@osg.samsung.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=olof@lixom.net \
--cc=rspangler@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.