mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] i.MX: SPI: Respect SPI_LSB_FIRST flag in mode settings
@ 2014-12-31 15:10 Andrey Smirnov
  2015-01-05  9:31 ` Sascha Hauer
  0 siblings, 1 reply; 3+ messages in thread
From: Andrey Smirnov @ 2014-12-31 15:10 UTC (permalink / raw)
  To: barebox; +Cc: Andrey Smirnov

Add code to support SPI transfers that have data shifted out least
significant bit first. This is useful in many cases, but specifically
it is needed for drivers/firmware/altera_serial.c to work on i.MX
platform.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 drivers/spi/imx_spi.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)

diff --git a/drivers/spi/imx_spi.c b/drivers/spi/imx_spi.c
index 3146339..efbbb08 100644
--- a/drivers/spi/imx_spi.c
+++ b/drivers/spi/imx_spi.c
@@ -137,6 +137,25 @@ static int imx_spi_setup(struct spi_device *spi)
 	return 0;
 }
 
+static unsigned int imx_spi_maybe_reverse_bits(struct spi_device *spi, unsigned int word)
+{
+	unsigned int result = word;
+
+	if (spi->mode & SPI_LSB_FIRST) {
+		size_t bits_left = spi->bits_per_word - 1;
+
+		for (word >>= 1; word; word >>= 1) {
+			result <<= 1;
+			result |= word & 1;
+			bits_left--;
+		}
+
+		result <<= bits_left;
+	}
+
+	return result;
+}
+
 static unsigned int cspi_0_0_xchg_single(struct imx_spi *imx, unsigned int data)
 {
 	void __iomem *base = imx->regs;
@@ -386,6 +405,7 @@ static void imx_spi_do_transfer(struct spi_device *spi, struct spi_transfer *t)
 {
 	struct imx_spi *imx = container_of(spi->master, struct imx_spi, master);
 	unsigned i;
+	u32 tx_val;
 
 	if (spi->bits_per_word <= 8) {
 		const u8	*tx_buf = t->tx_buf;
@@ -393,9 +413,15 @@ static void imx_spi_do_transfer(struct spi_device *spi, struct spi_transfer *t)
 		u8		rx_val;
 
 		for (i = 0; i < t->len; i++) {
-			rx_val = imx->xchg_single(imx, tx_buf ? tx_buf[i] : 0);
+			if (tx_buf)
+				tx_val = imx_spi_maybe_reverse_bits(spi, tx_buf[i]);
+			else
+				tx_val = 0;
+
+			rx_val = imx->xchg_single(imx, tx_val);
+
 			if (rx_buf)
-				rx_buf[i] = rx_val;
+				rx_buf[i] = (u8)imx_spi_maybe_reverse_bits(spi, rx_val);
 		}
 	} else if (spi->bits_per_word <= 16) {
 		const u16	*tx_buf = t->tx_buf;
@@ -403,9 +429,15 @@ static void imx_spi_do_transfer(struct spi_device *spi, struct spi_transfer *t)
 		u16		rx_val;
 
 		for (i = 0; i < t->len >> 1; i++) {
-			rx_val = imx->xchg_single(imx, tx_buf ? tx_buf[i] : 0);
+			if (tx_buf)
+				tx_val = imx_spi_maybe_reverse_bits(spi, tx_buf[i]);
+			else
+				tx_val = 0;
+
+			rx_val = imx->xchg_single(imx, tx_val);
+
 			if (rx_buf)
-				rx_buf[i] = rx_val;
+				rx_buf[i] = (u16)imx_spi_maybe_reverse_bits(spi, rx_val);
 		}
 	} else if (spi->bits_per_word <= 32) {
 		const u32	*tx_buf = t->tx_buf;
@@ -413,9 +445,15 @@ static void imx_spi_do_transfer(struct spi_device *spi, struct spi_transfer *t)
 		u32		rx_val;
 
 		for (i = 0; i < t->len >> 2; i++) {
-			rx_val = imx->xchg_single(imx, tx_buf ? tx_buf[i] : 0);
+			if (tx_buf)
+				tx_val = imx_spi_maybe_reverse_bits(spi, tx_buf[i]);
+			else
+				tx_val = 0;
+
+			rx_val = imx->xchg_single(imx, tx_val);
+
 			if (rx_buf)
-				rx_buf[i] = rx_val;
+				rx_buf[i] = imx_spi_maybe_reverse_bits(spi, rx_val);
 		}
 	}
 }
-- 
2.1.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] i.MX: SPI: Respect SPI_LSB_FIRST flag in mode settings
  2014-12-31 15:10 [PATCH] i.MX: SPI: Respect SPI_LSB_FIRST flag in mode settings Andrey Smirnov
@ 2015-01-05  9:31 ` Sascha Hauer
  2015-01-06 20:13   ` Andrey Smirnov
  0 siblings, 1 reply; 3+ messages in thread
From: Sascha Hauer @ 2015-01-05  9:31 UTC (permalink / raw)
  To: Andrey Smirnov; +Cc: barebox

Hi Andrey,

On Wed, Dec 31, 2014 at 07:10:55AM -0800, Andrey Smirnov wrote:
> Add code to support SPI transfers that have data shifted out least
> significant bit first. This is useful in many cases, but specifically
> it is needed for drivers/firmware/altera_serial.c to work on i.MX
> platform.
> 
> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
> ---
>  drivers/spi/imx_spi.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 44 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/spi/imx_spi.c b/drivers/spi/imx_spi.c
> index 3146339..efbbb08 100644
> --- a/drivers/spi/imx_spi.c
> +++ b/drivers/spi/imx_spi.c
> @@ -137,6 +137,25 @@ static int imx_spi_setup(struct spi_device *spi)
>  	return 0;
>  }
>  
> +static unsigned int imx_spi_maybe_reverse_bits(struct spi_device *spi, unsigned int word)
> +{
> +	unsigned int result = word;
> +
> +	if (spi->mode & SPI_LSB_FIRST) {
> +		size_t bits_left = spi->bits_per_word - 1;
> +
> +		for (word >>= 1; word; word >>= 1) {
> +			result <<= 1;
> +			result |= word & 1;
> +			bits_left--;
> +		}
> +
> +		result <<= bits_left;
> +	}
> +
> +	return result;
> +}
> +
>  static unsigned int cspi_0_0_xchg_single(struct imx_spi *imx, unsigned int data)
>  {
>  	void __iomem *base = imx->regs;
> @@ -386,6 +405,7 @@ static void imx_spi_do_transfer(struct spi_device *spi, struct spi_transfer *t)
>  {
>  	struct imx_spi *imx = container_of(spi->master, struct imx_spi, master);
>  	unsigned i;
> +	u32 tx_val;
>  
>  	if (spi->bits_per_word <= 8) {
>  		const u8	*tx_buf = t->tx_buf;
> @@ -393,9 +413,15 @@ static void imx_spi_do_transfer(struct spi_device *spi, struct spi_transfer *t)
>  		u8		rx_val;
>  
>  		for (i = 0; i < t->len; i++) {
> -			rx_val = imx->xchg_single(imx, tx_buf ? tx_buf[i] : 0);
> +			if (tx_buf)
> +				tx_val = imx_spi_maybe_reverse_bits(spi, tx_buf[i]);
> +			else
> +				tx_val = 0;
> +
> +			rx_val = imx->xchg_single(imx, tx_val);
> +

Better introduce a imx_xchg_single which does the necessary bit
reversing and calls imx->xchg_single. This avoids duplicating the code
for all three bits_per_word settings.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] i.MX: SPI: Respect SPI_LSB_FIRST flag in mode settings
  2015-01-05  9:31 ` Sascha Hauer
@ 2015-01-06 20:13   ` Andrey Smirnov
  0 siblings, 0 replies; 3+ messages in thread
From: Andrey Smirnov @ 2015-01-06 20:13 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

> Better introduce a imx_xchg_single which does the necessary bit
> reversing and calls imx->xchg_single. This avoids duplicating the code
> for all three bits_per_word settings.
>

I like this idea. Will do for the next version of the patch.

Thanks,
Andrey

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-01-06 20:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-31 15:10 [PATCH] i.MX: SPI: Respect SPI_LSB_FIRST flag in mode settings Andrey Smirnov
2015-01-05  9:31 ` Sascha Hauer
2015-01-06 20:13   ` Andrey Smirnov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox