public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH] spi: oc_tiny_spi: Refactor to simplify spi_xfer implementation
@ 2014-01-10  8:08 Axel Lin
  2014-03-02 18:43 ` Jagan Teki
  0 siblings, 1 reply; 2+ messages in thread
From: Axel Lin @ 2014-01-10  8:08 UTC (permalink / raw)
  To: u-boot

Currently we have similar code for (txp && rxp), (txp && !rxp), (!rxp & txp),
and (!txp && !rxp) cases. This patch refactors the code a bit to avoid
duplicate similar code.

Signed-off-by: Axel Lin <axel.lin@ingics.com>
---
Hi Thomas,
This path is similar to the patch I sent for spi-oc-tiny.c linux driver.
I'd appreciate if you can review and test this patch.
Regards,
Axel
 drivers/spi/oc_tiny_spi.c | 85 ++++++-----------------------------------------
 1 file changed, 11 insertions(+), 74 deletions(-)

diff --git a/drivers/spi/oc_tiny_spi.c b/drivers/spi/oc_tiny_spi.c
index 4de5d00..a8c1dfd 100644
--- a/drivers/spi/oc_tiny_spi.c
+++ b/drivers/spi/oc_tiny_spi.c
@@ -158,85 +158,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 		spi_cs_activate(slave);
 
 	/* we need to tighten the transfer loop */
-	if (txp && rxp) {
-		writeb(*txp++, &regs->txdata);
-		if (bytes > 1) {
-			writeb(*txp++, &regs->txdata);
-			for (i = 2; i < bytes; i++) {
-				u8 rx, tx = *txp++;
-				while (!(readb(&regs->status) &
-					 TINY_SPI_STATUS_TXR))
+	writeb(txp ? *txp++ : CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
+	for (i = 1; i < bytes; i++) {
+		writeb(txp ? *txp++ : CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
+
+		if (rxp || (i != bytes - 1)) {
+			while (!(readb(&regs->status) & TINY_SPI_STATUS_TXR))
 					;
-				rx = readb(&regs->txdata);
-				writeb(tx, &regs->txdata);
-				*rxp++ = rx;
-			}
-			while (!(readb(&regs->status) &
-				 TINY_SPI_STATUS_TXR))
-				;
-			*rxp++ = readb(&regs->txdata);
 		}
-		while (!(readb(&regs->status) &
-			 TINY_SPI_STATUS_TXE))
-			;
-		*rxp++ = readb(&regs->rxdata);
-	} else if (rxp) {
-		writeb(CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
-		if (bytes > 1) {
-			writeb(CONFIG_TINY_SPI_IDLE_VAL,
-			       &regs->txdata);
-			for (i = 2; i < bytes; i++) {
-				u8 rx;
-				while (!(readb(&regs->status) &
-					 TINY_SPI_STATUS_TXR))
-					;
-				rx = readb(&regs->txdata);
-				writeb(CONFIG_TINY_SPI_IDLE_VAL,
-				       &regs->txdata);
-				*rxp++ = rx;
-			}
-			while (!(readb(&regs->status) &
-				 TINY_SPI_STATUS_TXR))
-				;
+
+		if (rxp)
 			*rxp++ = readb(&regs->txdata);
-		}
-		while (!(readb(&regs->status) &
-			 TINY_SPI_STATUS_TXE))
+	}
+	while (!(readb(&regs->status) & TINY_SPI_STATUS_TXE))
 			;
+	if (rxp)
 		*rxp++ = readb(&regs->rxdata);
-	} else if (txp) {
-		writeb(*txp++, &regs->txdata);
-		if (bytes > 1) {
-			writeb(*txp++, &regs->txdata);
-			for (i = 2; i < bytes; i++) {
-				u8 tx = *txp++;
-				while (!(readb(&regs->status) &
-					 TINY_SPI_STATUS_TXR))
-					;
-				writeb(tx, &regs->txdata);
-			}
-		}
-		while (!(readb(&regs->status) &
-			 TINY_SPI_STATUS_TXE))
-			;
-	} else {
-		writeb(CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
-		if (bytes > 1) {
-			writeb(CONFIG_TINY_SPI_IDLE_VAL,
-			       &regs->txdata);
-			for (i = 2; i < bytes; i++) {
-				while (!(readb(&regs->status) &
-					 TINY_SPI_STATUS_TXR))
-					;
-				writeb(CONFIG_TINY_SPI_IDLE_VAL,
-				       &regs->txdata);
-			}
-		}
-		while (!(readb(&regs->status) &
-			 TINY_SPI_STATUS_TXE))
-			;
-	}
-
  done:
 	if (flags & SPI_XFER_END)
 		spi_cs_deactivate(slave);
-- 
1.8.1.2

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

* [U-Boot] [PATCH] spi: oc_tiny_spi: Refactor to simplify spi_xfer implementation
  2014-01-10  8:08 [U-Boot] [PATCH] spi: oc_tiny_spi: Refactor to simplify spi_xfer implementation Axel Lin
@ 2014-03-02 18:43 ` Jagan Teki
  0 siblings, 0 replies; 2+ messages in thread
From: Jagan Teki @ 2014-03-02 18:43 UTC (permalink / raw)
  To: u-boot

Hi Axel,

Any testing on this patch?

On Fri, Jan 10, 2014 at 1:38 PM, Axel Lin <axel.lin@ingics.com> wrote:
> Currently we have similar code for (txp && rxp), (txp && !rxp), (!rxp & txp),
> and (!txp && !rxp) cases. This patch refactors the code a bit to avoid
> duplicate similar code.
>
> Signed-off-by: Axel Lin <axel.lin@ingics.com>
> ---
> Hi Thomas,
> This path is similar to the patch I sent for spi-oc-tiny.c linux driver.
> I'd appreciate if you can review and test this patch.
> Regards,
> Axel
>  drivers/spi/oc_tiny_spi.c | 85 ++++++-----------------------------------------
>  1 file changed, 11 insertions(+), 74 deletions(-)
>
> diff --git a/drivers/spi/oc_tiny_spi.c b/drivers/spi/oc_tiny_spi.c
> index 4de5d00..a8c1dfd 100644
> --- a/drivers/spi/oc_tiny_spi.c
> +++ b/drivers/spi/oc_tiny_spi.c
> @@ -158,85 +158,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
>                 spi_cs_activate(slave);
>
>         /* we need to tighten the transfer loop */
> -       if (txp && rxp) {
> -               writeb(*txp++, &regs->txdata);
> -               if (bytes > 1) {
> -                       writeb(*txp++, &regs->txdata);
> -                       for (i = 2; i < bytes; i++) {
> -                               u8 rx, tx = *txp++;
> -                               while (!(readb(&regs->status) &
> -                                        TINY_SPI_STATUS_TXR))
> +       writeb(txp ? *txp++ : CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
> +       for (i = 1; i < bytes; i++) {
> +               writeb(txp ? *txp++ : CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
> +
> +               if (rxp || (i != bytes - 1)) {
> +                       while (!(readb(&regs->status) & TINY_SPI_STATUS_TXR))
>                                         ;
> -                               rx = readb(&regs->txdata);
> -                               writeb(tx, &regs->txdata);
> -                               *rxp++ = rx;
> -                       }
> -                       while (!(readb(&regs->status) &
> -                                TINY_SPI_STATUS_TXR))
> -                               ;
> -                       *rxp++ = readb(&regs->txdata);
>                 }
> -               while (!(readb(&regs->status) &
> -                        TINY_SPI_STATUS_TXE))
> -                       ;
> -               *rxp++ = readb(&regs->rxdata);
> -       } else if (rxp) {
> -               writeb(CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
> -               if (bytes > 1) {
> -                       writeb(CONFIG_TINY_SPI_IDLE_VAL,
> -                              &regs->txdata);
> -                       for (i = 2; i < bytes; i++) {
> -                               u8 rx;
> -                               while (!(readb(&regs->status) &
> -                                        TINY_SPI_STATUS_TXR))
> -                                       ;
> -                               rx = readb(&regs->txdata);
> -                               writeb(CONFIG_TINY_SPI_IDLE_VAL,
> -                                      &regs->txdata);
> -                               *rxp++ = rx;
> -                       }
> -                       while (!(readb(&regs->status) &
> -                                TINY_SPI_STATUS_TXR))
> -                               ;
> +
> +               if (rxp)
>                         *rxp++ = readb(&regs->txdata);
> -               }
> -               while (!(readb(&regs->status) &
> -                        TINY_SPI_STATUS_TXE))
> +       }
> +       while (!(readb(&regs->status) & TINY_SPI_STATUS_TXE))
>                         ;
> +       if (rxp)
>                 *rxp++ = readb(&regs->rxdata);
> -       } else if (txp) {
> -               writeb(*txp++, &regs->txdata);
> -               if (bytes > 1) {
> -                       writeb(*txp++, &regs->txdata);
> -                       for (i = 2; i < bytes; i++) {
> -                               u8 tx = *txp++;
> -                               while (!(readb(&regs->status) &
> -                                        TINY_SPI_STATUS_TXR))
> -                                       ;
> -                               writeb(tx, &regs->txdata);
> -                       }
> -               }
> -               while (!(readb(&regs->status) &
> -                        TINY_SPI_STATUS_TXE))
> -                       ;
> -       } else {
> -               writeb(CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
> -               if (bytes > 1) {
> -                       writeb(CONFIG_TINY_SPI_IDLE_VAL,
> -                              &regs->txdata);
> -                       for (i = 2; i < bytes; i++) {
> -                               while (!(readb(&regs->status) &
> -                                        TINY_SPI_STATUS_TXR))
> -                                       ;
> -                               writeb(CONFIG_TINY_SPI_IDLE_VAL,
> -                                      &regs->txdata);
> -                       }
> -               }
> -               while (!(readb(&regs->status) &
> -                        TINY_SPI_STATUS_TXE))
> -                       ;
> -       }
> -
>   done:
>         if (flags & SPI_XFER_END)
>                 spi_cs_deactivate(slave);
> --
> 1.8.1.2
>
>
>
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot

thanks!
-- 
Jagan.

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

end of thread, other threads:[~2014-03-02 18:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-10  8:08 [U-Boot] [PATCH] spi: oc_tiny_spi: Refactor to simplify spi_xfer implementation Axel Lin
2014-03-02 18:43 ` Jagan Teki

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