All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nuno Sá" <nuno.sa@analog.com>
To: <broonie@kernel.org>, <eric@anholt.net>, <wahrenst@gmx.net>,
	<f.fainelli@gmail.com>, <rjui@broadcom.com>,
	<sbranden@broadcom.com>
Cc: <bcm-kernel-feedback-list@broadcom.com>,
	<linux-spi@vger.kernel.org>,
	<linux-rpi-kernel@lists.infradead.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH] spi: spi-bcm2835.c: Fix 3-wire mode
Date: Fri, 28 Jun 2019 14:30:23 +0200	[thread overview]
Message-ID: <20190628123023.4696-1-nuno.sa@analog.com> (raw)

As stated in
https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md,
one of rx or tx buffer's must be null. However, if DMA is enabled, the
driver sets the SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX on the
controller flags. Hence, the spi core will provide dummy buffers even if
one of the buffers was set to null by the device driver. Thus, the
communication with the 3-wire device fails.

This patch uses the prepare_message callback to look for the device mode
and sets/clears the SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX on a
per spi message basis. It also assumes that DMA is not supported on
half-duplex devices.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
---
 drivers/spi/spi-bcm2835.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c
index 6f243a90c844..8993a15a4684 100644
--- a/drivers/spi/spi-bcm2835.c
+++ b/drivers/spi/spi-bcm2835.c
@@ -674,6 +674,10 @@ static bool bcm2835_spi_can_dma(struct spi_controller *ctlr,
 	if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
 		return false;
 
+	/* Do not DMA on half-duplex devices */
+	if (spi->mode & SPI_3WIRE)
+		return false;
+
 	/* return OK */
 	return true;
 }
@@ -902,6 +906,15 @@ static int bcm2835_spi_prepare_message(struct spi_controller *ctlr,
 		cs |= BCM2835_SPI_CS_CPOL;
 	if (spi->mode & SPI_CPHA)
 		cs |= BCM2835_SPI_CS_CPHA;
+	/*
+	 * For half-duplex devices, one of tx or rx buffers must be null
+	 * for one spi transfer. Hence, we need to clear the spi controller
+	 * flags so that we don't get dummy buffers...
+	 */
+	if (spi->mode & SPI_3WIRE)
+		master->flags &= ~(SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
+	else if (master->can_dma)
+		master->flags |= (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
 
 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
 
-- 
2.22.0


WARNING: multiple messages have this Message-ID (diff)
From: "Nuno Sá" <nuno.sa@analog.com>
To: <broonie@kernel.org>, <eric@anholt.net>, <wahrenst@gmx.net>,
	<f.fainelli@gmail.com>, <rjui@broadcom.com>,
	<sbranden@broadcom.com>
Cc: linux-kernel@vger.kernel.org,
	bcm-kernel-feedback-list@broadcom.com,
	linux-rpi-kernel@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org, linux-spi@vger.kernel.org
Subject: [PATCH] spi: spi-bcm2835.c: Fix 3-wire mode
Date: Fri, 28 Jun 2019 14:30:23 +0200	[thread overview]
Message-ID: <20190628123023.4696-1-nuno.sa@analog.com> (raw)

As stated in
https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md,
one of rx or tx buffer's must be null. However, if DMA is enabled, the
driver sets the SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX on the
controller flags. Hence, the spi core will provide dummy buffers even if
one of the buffers was set to null by the device driver. Thus, the
communication with the 3-wire device fails.

This patch uses the prepare_message callback to look for the device mode
and sets/clears the SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX on a
per spi message basis. It also assumes that DMA is not supported on
half-duplex devices.

Signed-off-by: Nuno Sá <nuno.sa@analog.com>
---
 drivers/spi/spi-bcm2835.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c
index 6f243a90c844..8993a15a4684 100644
--- a/drivers/spi/spi-bcm2835.c
+++ b/drivers/spi/spi-bcm2835.c
@@ -674,6 +674,10 @@ static bool bcm2835_spi_can_dma(struct spi_controller *ctlr,
 	if (tfr->len < BCM2835_SPI_DMA_MIN_LENGTH)
 		return false;
 
+	/* Do not DMA on half-duplex devices */
+	if (spi->mode & SPI_3WIRE)
+		return false;
+
 	/* return OK */
 	return true;
 }
@@ -902,6 +906,15 @@ static int bcm2835_spi_prepare_message(struct spi_controller *ctlr,
 		cs |= BCM2835_SPI_CS_CPOL;
 	if (spi->mode & SPI_CPHA)
 		cs |= BCM2835_SPI_CS_CPHA;
+	/*
+	 * For half-duplex devices, one of tx or rx buffers must be null
+	 * for one spi transfer. Hence, we need to clear the spi controller
+	 * flags so that we don't get dummy buffers...
+	 */
+	if (spi->mode & SPI_3WIRE)
+		master->flags &= ~(SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
+	else if (master->can_dma)
+		master->flags |= (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
 
 	bcm2835_wr(bs, BCM2835_SPI_CS, cs);
 
-- 
2.22.0


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

             reply	other threads:[~2019-06-28 15:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-28 12:30 Nuno Sá [this message]
2019-06-28 12:30 ` [PATCH] spi: spi-bcm2835.c: Fix 3-wire mode Nuno Sá
2019-06-28 15:23 ` Stefan Wahren
2019-06-28 15:23   ` Stefan Wahren
2019-06-28 19:00   ` Lukas Wunner
2019-07-01  7:24     ` Sa, Nuno
2019-07-01  7:24       ` Sa, Nuno
2019-07-01 11:55       ` Lukas Wunner
2019-07-01 11:55         ` Lukas Wunner
2019-07-01 14:21         ` Sa, Nuno
2019-07-01 14:21           ` Sa, Nuno
2019-07-03 10:43           ` Lukas Wunner
2019-07-03 10:43             ` Lukas Wunner

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=20190628123023.4696-1-nuno.sa@analog.com \
    --to=nuno.sa@analog.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=broonie@kernel.org \
    --cc=eric@anholt.net \
    --cc=f.fainelli@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=rjui@broadcom.com \
    --cc=sbranden@broadcom.com \
    --cc=wahrenst@gmx.net \
    /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.