Skip to contentSkip to conversation

SPI: Enable SPI_PREAMBLE Mode

PATCH Superseded Rajeshwari Shinde 4 participants 10 messages Mar 22, 2013 – May 21, 2013

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover SPI: Enable SPI_PREAMBLE Mode

This patch set enables PREAMBLE Mode for EXYNOS SPI.

Rajeshwari Shinde (2):
SPI: Add support for preamble bytes
EXYNOS: SPI: Support SPI_PREAMBLE mode

 drivers/spi/exynos_spi.c |   62 ++++++++++++++++++++++++++++++++++++++++------
 include/spi.h            |    5 +++
 2 files changed, 59 insertions(+), 8 deletions(-)
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH 1/2 SPI: Add support for preamble bytes

A SPI slave may take time to react to a request. For SPI flash devices this time is defined as one bit time, or a whole byte for 'fast read' mode.

If the SPI slave is another CPU, then the time it takes to react may vary. It is convenient to allow the slave device to tag the start of the actual reply so that the host can determine when this 'preamble' finishes and the actual message starts.

Add a preamble flag to the available SPI flags. If supported by the driver then it will ignore any received bytes before the preamble on each transaction. This ensures that reliable communication with the slave is possible.

---
 include/spi.h |    5 +++++
 1 files changed, 5 insertions(+), 0 deletions(-)
include/spi.h +5 -0
index 60e85db..5351c59 100644
@@ -37,11 +37,16 @@
 #define	SPI_LSB_FIRST	0x08			/* per-word bits-on-wire */
 #define	SPI_3WIRE	0x10			/* SI/SO signals shared */
 #define	SPI_LOOP	0x20			/* loopback mode */
+#define	SPI_SLAVE	0x40			/* slave mode */
+#define	SPI_PREAMBLE	0x80			/* Skip preamble bytes */
 
 /* SPI transfer flags */
 #define SPI_XFER_BEGIN	0x01			/* Assert CS before transfer */
 #define SPI_XFER_END	0x02			/* Deassert CS after transfer */
 
+/* Header byte that marks the start of the message */
+#define SPI_PREAMBLE_END_BYTE	0xec
+
 /*-----------------------------------------------------------------------
  * Representation of a SPI slave, i.e. what we're communicating with.
  *
Signature · 1 line
1.7.4.4
#2 lore raw

PATCH 1/2 SPI: Add support for preamble bytes

Quoted 9 lines · Rajeshwari Shinde

A SPI slave may take time to react to a request. For SPI flash devices this time is defined as one bit time, or a whole byte for 'fast read' mode.

If the SPI slave is another CPU, then the time it takes to react may vary. It is convenient to allow the slave device to tag the start of the actual reply so that the host can determine when this 'preamble' finishes and the actual message starts.

Quoted 7 lines

Add a preamble flag to the available SPI flags. If supported by the driver then it will ignore any received bytes before the preamble on each transaction. This ensures that reliable communication with the slave is possible.

#3 lore raw

PATCH 2/2 EXYNOS: SPI: Support SPI_PREAMBLE mode

In reply to Rajeshwari Shinde

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

---
 drivers/spi/exynos_spi.c |   62 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 54 insertions(+), 8 deletions(-)
drivers/spi/exynos_spi.c +54 -8
index be60ada..09e88d5 100644
@@ -51,6 +51,7 @@ struct exynos_spi_slave {
 	unsigned int mode;
 	enum periph_id periph_id;	/* Peripheral ID for this device */
 	unsigned int fifo_size;
+	int skip_preamble;
 };
 
 static struct spi_bus *spi_get_bus(unsigned dev_index)
@@ -107,6 +108,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
 	else
 		spi_slave->fifo_size = 256;
 
+	spi_slave->skip_preamble = 0;
+
 	spi_slave->freq = bus->frequency;
 	if (max_hz)
 		spi_slave->freq = min(max_hz, spi_slave->freq);
@@ -219,17 +222,23 @@ static void spi_request_bytes(struct exynos_spi *regs, int count)
 	writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
-static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
-			void **dinp, void const **doutp)
+static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+			void **dinp, void const **doutp, unsigned long flags)
 {
 	struct exynos_spi *regs = spi_slave->regs;
 	uchar *rxp = *dinp;
 	const uchar *txp = *doutp;
 	int rx_lvl, tx_lvl;
 	uint out_bytes, in_bytes;
+	int toread, preamable_count = 0;
+	unsigned start = get_timer(0);
+	int stopping;
 
 	out_bytes = in_bytes = todo;
 
+	stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
+					!(spi_slave->mode & SPI_SLAVE);
+
 	/*
 	 * If there's something to send, do a software reset and set a
 	 * transaction size.
@@ -240,6 +249,7 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 	 * Bytes are transmitted/received in pairs. Wait to receive all the
 	 * data because then transmission will be done as well.
 	 */
+	toread = in_bytes;
 	while (in_bytes) {
 		int temp;
 
@@ -252,13 +262,41 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 		}
 		if (rx_lvl > 0 && in_bytes) {
 			temp = readl(&regs->rx_data);
-			if (rxp)
+			if (!rxp && !stopping) {
+				in_bytes--;
+			} else if (spi_slave->skip_preamble) {
+				if (temp == SPI_PREAMBLE_END_BYTE) {
+					spi_slave->skip_preamble = 0;
+					stopping = 0;
+				}
+			} else {
 				*rxp++ = temp;
-			in_bytes--;
+				in_bytes--;
+			}
+			toread--;
+		}
+		/*
+		 * We have run out of input data, but haven't read enough
+		 * bytes after the preamble yet. Read some more, and make
+		 * sure that we transmit dummy bytes too, to keep things
+		 * going.
+		 */
+		else if (in_bytes && !toread) {
+			assert(!out_bytes);
+			toread = out_bytes = in_bytes;
+			txp = NULL;
+			spi_request_bytes(regs, toread);
+		}
+		if (spi_slave->skip_preamble && get_timer(start) > 100) {
+			printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
+			       in_bytes, out_bytes);
+			printf("count = %d\n", preamable_count);
+			return -1;
 		}
 	}
 	*dinp = rxp;
 	*doutp = txp;
+	return 0;
 }
 
 /**
@@ -278,6 +316,7 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
 	int upto, todo;
 	int bytelen;
+	int ret = 0;
 
 	/* spi core configured to do 8 bit transfers */
 	if (bitlen % 8) {
@@ -291,16 +330,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 
 	/* Exynos SPI limits each transfer to 65535 bytes */
 	bytelen =  bitlen / 8;
-	for (upto = 0; upto < bytelen; upto += todo) {
+	for (upto = 0; !ret && upto < bytelen; upto += todo) {
 		todo = min(bytelen - upto, (1 << 16) - 1);
-		spi_rx_tx(spi_slave, todo, &din, &dout);
+		ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
 	}
 
 	/* Stop the transaction, if necessary. */
-	if ((flags & SPI_XFER_END))
+	if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
 		spi_cs_deactivate(slave);
+		if (spi_slave->skip_preamble) {
+			assert(!spi_slave->skip_preamble);
+			debug("Failed to complete premable transaction\n");
+			ret = -1;
+		}
+	}
 
-	return 0;
+	return ret;
 }
 
 /**
@@ -327,6 +372,7 @@ void spi_cs_activate(struct spi_slave *slave)
 
 	clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
 	debug("Activate CS, bus %d\n", spi_slave->slave.bus);
+	spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
 }
 
 /**
Signature · 1 line
1.7.4.4
#4 lore raw

PATCH 2/2 EXYNOS: SPI: Support SPI_PREAMBLE mode

Quoted 40 lines · Rajeshwari Shinde

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

---
 drivers/spi/exynos_spi.c |   62 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 54 insertions(+), 8 deletions(-)
drivers/spi/exynos_spi.c +3 -1
index be60ada..09e88d5 100644
@@ -51,6 +51,7 @@ struct exynos_spi_slave {
        unsigned int mode;
        enum periph_id periph_id;       /* Peripheral ID for this device */
        unsigned int fifo_size;
+       int skip_preamble;
 };
 
 static struct spi_bus *spi_get_bus(unsigned dev_index)
@@ -107,6 +108,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
        else
                spi_slave->fifo_size = 256;
 
+       spi_slave->skip_preamble = 0;
+
        spi_slave->freq = bus->frequency;
        if (max_hz)
                spi_slave->freq = min(max_hz, spi_slave->freq);
@@ -219,17 +222,23 @@ static void spi_request_bytes(struct exynos_spi *regs, int count)
        writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
-static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,

drivers/spi/exynos_spi.c static void spi_request_bytes(struct exynos_spi *regs, int count) · line 233

Quoted 10 lines
drivers/spi/exynos_spi.c +3 -1
-                       void **dinp, void const **doutp)
+static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+                       void **dinp, void const **doutp, unsigned long flags)
 {
        struct exynos_spi *regs = spi_slave->regs;
        uchar *rxp = *dinp;
        const uchar *txp = *doutp;
        int rx_lvl, tx_lvl;
        uint out_bytes, in_bytes;
+       int toread, preamable_count = 0;

preamable_count: the name is misspelled, and the variable is never modified.

Quoted 99 lines
drivers/spi/exynos_spi.c +47 -6
+       unsigned start = get_timer(0);
+       int stopping;
 
        out_bytes = in_bytes = todo;
 
+       stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
+                                       !(spi_slave->mode & SPI_SLAVE);
+
        /*
         * If there's something to send, do a software reset and set a
         * transaction size.
@@ -240,6 +249,7 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
         * Bytes are transmitted/received in pairs. Wait to receive all the
         * data because then transmission will be done as well.
         */
+       toread = in_bytes;
        while (in_bytes) {
                int temp;
 
@@ -252,13 +262,41 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
                }
                if (rx_lvl > 0 && in_bytes) {
                        temp = readl(&regs->rx_data);
-                       if (rxp)
+                       if (!rxp && !stopping) {
+                               in_bytes--;
+                       } else if (spi_slave->skip_preamble) {
+                               if (temp == SPI_PREAMBLE_END_BYTE) {
+                                       spi_slave->skip_preamble = 0;
+                                       stopping = 0;
+                               }
+                       } else {
                                *rxp++ = temp;
-                       in_bytes--;
+                               in_bytes--;
+                       }
+                       toread--;
+               }
+               /*
+                * We have run out of input data, but haven't read enough
+                * bytes after the preamble yet. Read some more, and make
+                * sure that we transmit dummy bytes too, to keep things
+                * going.
+                */
+               else if (in_bytes && !toread) {
+                       assert(!out_bytes);
+                       toread = out_bytes = in_bytes;
+                       txp = NULL;
+                       spi_request_bytes(regs, toread);
+               }
+               if (spi_slave->skip_preamble && get_timer(start) > 100) {
+                       printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
+                              in_bytes, out_bytes);
+                       printf("count = %d\n", preamable_count);
+                       return -1;
                }
        }
        *dinp = rxp;
        *doutp = txp;
+       return 0;
 }
 
 /**
@@ -278,6 +316,7 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
        struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
        int upto, todo;
        int bytelen;
+       int ret = 0;
 
        /* spi core configured to do 8 bit transfers */
        if (bitlen % 8) {
@@ -291,16 +330,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 
        /* Exynos SPI limits each transfer to 65535 bytes */
        bytelen =  bitlen / 8;
-       for (upto = 0; upto < bytelen; upto += todo) {
+       for (upto = 0; !ret && upto < bytelen; upto += todo) {
                todo = min(bytelen - upto, (1 << 16) - 1);
-               spi_rx_tx(spi_slave, todo, &din, &dout);
+               ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
        }
 
        /* Stop the transaction, if necessary. */
-       if ((flags & SPI_XFER_END))
+       if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
                spi_cs_deactivate(slave);
+               if (spi_slave->skip_preamble) {
+                       assert(!spi_slave->skip_preamble);
+                       debug("Failed to complete premable transaction\n");
+                       ret = -1;
+               }
+       }
 
-       return 0;
+       return ret;
 }
 
 /**

drivers/spi/exynos_spi.c void spi_cs_activate(struct spi_slave *slave) · line 375

Quoted 4 lines
drivers/spi/exynos_spi.c +1 -0
@@ -327,6 +372,7 @@ void spi_cs_activate(struct spi_slave *slave)
 
        clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
        debug("Activate CS, bus %d\n", spi_slave->slave.bus);
+       spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;

who sets this bit in the 'mode' field?

Quoted 10 lines
drivers/spi/exynos_spi.c +0 -1
 }
 
 /**
--

1.7.4.4

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

#5 lore raw

PATCH 2/2 EXYNOS: SPI: Support SPI_PREAMBLE mode

Hi Vadim,

Quoted 152 lines · Vadim Bendebury
Quoted 40 lines · Rajeshwari Shinde

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

---
 drivers/spi/exynos_spi.c |   62 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 54 insertions(+), 8 deletions(-)
drivers/spi/exynos_spi.c +3 -1
index be60ada..09e88d5 100644
@@ -51,6 +51,7 @@ struct exynos_spi_slave {
        unsigned int mode;
        enum periph_id periph_id;       /* Peripheral ID for this device */
        unsigned int fifo_size;
+       int skip_preamble;
 };
 
 static struct spi_bus *spi_get_bus(unsigned dev_index)
@@ -107,6 +108,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
        else
                spi_slave->fifo_size = 256;
 
+       spi_slave->skip_preamble = 0;
+
        spi_slave->freq = bus->frequency;
        if (max_hz)
                spi_slave->freq = min(max_hz, spi_slave->freq);
@@ -219,17 +222,23 @@ static void spi_request_bytes(struct exynos_spi *regs, int count)
        writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
-static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,

drivers/spi/exynos_spi.c static void spi_request_bytes(struct exynos_spi *regs, int count) · line 233

Quoted 10 lines
drivers/spi/exynos_spi.c +3 -1
-                       void **dinp, void const **doutp)
+static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+                       void **dinp, void const **doutp, unsigned long flags)
 {
        struct exynos_spi *regs = spi_slave->regs;
        uchar *rxp = *dinp;
        const uchar *txp = *doutp;
        int rx_lvl, tx_lvl;
        uint out_bytes, in_bytes;
+       int toread, preamable_count = 0;

preamable_count: the name is misspelled, and the variable is never modified.

Quoted 97 lines
drivers/spi/exynos_spi.c +47 -6
+       unsigned start = get_timer(0);
+       int stopping;
 
        out_bytes = in_bytes = todo;
 
+       stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
+                                       !(spi_slave->mode & SPI_SLAVE);
+
        /*
         * If there's something to send, do a software reset and set a
         * transaction size.
@@ -240,6 +249,7 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
         * Bytes are transmitted/received in pairs. Wait to receive all the
         * data because then transmission will be done as well.
         */
+       toread = in_bytes;
        while (in_bytes) {
                int temp;
 
@@ -252,13 +262,41 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
                }
                if (rx_lvl > 0 && in_bytes) {
                        temp = readl(&regs->rx_data);
-                       if (rxp)
+                       if (!rxp && !stopping) {
+                               in_bytes--;
+                       } else if (spi_slave->skip_preamble) {
+                               if (temp == SPI_PREAMBLE_END_BYTE) {
+                                       spi_slave->skip_preamble = 0;
+                                       stopping = 0;
+                               }
+                       } else {
                                *rxp++ = temp;
-                       in_bytes--;
+                               in_bytes--;
+                       }
+                       toread--;
+               }
+               /*
+                * We have run out of input data, but haven't read enough
+                * bytes after the preamble yet. Read some more, and make
+                * sure that we transmit dummy bytes too, to keep things
+                * going.
+                */
+               else if (in_bytes && !toread) {
+                       assert(!out_bytes);
+                       toread = out_bytes = in_bytes;
+                       txp = NULL;
+                       spi_request_bytes(regs, toread);
+               }
+               if (spi_slave->skip_preamble && get_timer(start) > 100) {
+                       printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
+                              in_bytes, out_bytes);
+                       printf("count = %d\n", preamable_count);
+                       return -1;
                }
        }
        *dinp = rxp;
        *doutp = txp;
+       return 0;
 }
 
 /**
@@ -278,6 +316,7 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
        struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
        int upto, todo;
        int bytelen;
+       int ret = 0;
 
        /* spi core configured to do 8 bit transfers */
        if (bitlen % 8) {
@@ -291,16 +330,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 
        /* Exynos SPI limits each transfer to 65535 bytes */
        bytelen =  bitlen / 8;
-       for (upto = 0; upto < bytelen; upto += todo) {
+       for (upto = 0; !ret && upto < bytelen; upto += todo) {
                todo = min(bytelen - upto, (1 << 16) - 1);
-               spi_rx_tx(spi_slave, todo, &din, &dout);
+               ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
        }
 
        /* Stop the transaction, if necessary. */
-       if ((flags & SPI_XFER_END))
+       if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
                spi_cs_deactivate(slave);
+               if (spi_slave->skip_preamble) {
+                       assert(!spi_slave->skip_preamble);
+                       debug("Failed to complete premable transaction\n");
+                       ret = -1;
+               }
+       }
 
-       return 0;
+       return ret;
 }
 
Quoted 8 lines

drivers/spi/exynos_spi.c void spi_cs_activate(struct spi_slave *slave) · line 375

Quoted 6 lines
drivers/spi/exynos_spi.c +1 -0
 /**
@@ -327,6 +372,7 @@ void spi_cs_activate(struct spi_slave *slave)
 
        clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
        debug("Activate CS, bus %d\n", spi_slave->slave.bus);
+       spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;

who sets this bit in the 'mode' field?

This is set by whoever creates the slave - in the case of snow it is cros_ec.

Regards,
Simon

#6 lore raw

PATCH 2/2 EXYNOS: SPI: Support SPI_PREAMBLE mode

In reply to Vadim Bendebury

Hi,

Quoted 41 lines · Vadim Bendebury
Quoted 39 lines · Rajeshwari Shinde

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

---
 drivers/spi/exynos_spi.c |   62 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 54 insertions(+), 8 deletions(-)
drivers/spi/exynos_spi.c +3 -0
index be60ada..09e88d5 100644
@@ -51,6 +51,7 @@ struct exynos_spi_slave {
        unsigned int mode;
        enum periph_id periph_id;       /* Peripheral ID for this device */
        unsigned int fifo_size;
+       int skip_preamble;
 };
 
 static struct spi_bus *spi_get_bus(unsigned dev_index)
@@ -107,6 +108,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
        else
                spi_slave->fifo_size = 256;
 
+       spi_slave->skip_preamble = 0;
+
        spi_slave->freq = bus->frequency;
        if (max_hz)
                spi_slave->freq = min(max_hz, spi_slave->freq);
@@ -219,17 +222,23 @@ static void spi_request_bytes(struct exynos_spi *regs, int count)
        writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
Quoted 13 lines

drivers/spi/exynos_spi.c static void spi_request_bytes(struct exynos_spi *regs, int count) · line 233

Quoted 11 lines
drivers/spi/exynos_spi.c +3 -2
-static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
-                       void **dinp, void const **doutp)
+static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+                       void **dinp, void const **doutp, unsigned long flags)
 {
        struct exynos_spi *regs = spi_slave->regs;
        uchar *rxp = *dinp;
        const uchar *txp = *doutp;
        int rx_lvl, tx_lvl;
        uint out_bytes, in_bytes;
+       int toread, preamable_count = 0;

preamable_count: the name is misspelled, and the variable is never modified.

Yes, it is supposed to count the number of bytes before the preamble, in the event of an error, but it isn't really needed. I will resend the patch with it removed.

Regards,
Simon

#7 lore raw

PATCH v2 1/1 EXYNOS: SPI: Support SPI_PREAMBLE mode

In reply to Rajeshwari Shinde

From: Rajeshwari Shinde <[email protected]>

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

---
Changes in v2:
- Remove preamable_count variable which is not really needed
- Fix checkpatch warning (multiple assignments)

 drivers/spi/exynos_spi.c | 62 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 54 insertions(+), 8 deletions(-)
drivers/spi/exynos_spi.c +54 -8
index 607e1cd..b7b2ea5 100644
@@ -51,6 +51,7 @@ struct exynos_spi_slave {
 	unsigned int mode;
 	enum periph_id periph_id;	/* Peripheral ID for this device */
 	unsigned int fifo_size;
+	int skip_preamble;
 };
 
 static struct spi_bus *spi_get_bus(unsigned dev_index)
@@ -105,6 +106,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
 	else
 		spi_slave->fifo_size = 256;
 
+	spi_slave->skip_preamble = 0;
+
 	spi_slave->freq = bus->frequency;
 	if (max_hz)
 		spi_slave->freq = min(max_hz, spi_slave->freq);
@@ -217,17 +220,23 @@ static void spi_request_bytes(struct exynos_spi *regs, int count)
 	writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
-static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
-			void **dinp, void const **doutp)
+static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+			void **dinp, void const **doutp, unsigned long flags)
 {
 	struct exynos_spi *regs = spi_slave->regs;
 	uchar *rxp = *dinp;
 	const uchar *txp = *doutp;
 	int rx_lvl, tx_lvl;
 	uint out_bytes, in_bytes;
+	int toread;
+	unsigned start = get_timer(0);
+	int stopping;
 
 	out_bytes = in_bytes = todo;
 
+	stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
+					!(spi_slave->mode & SPI_SLAVE);
+
 	/*
 	 * If there's something to send, do a software reset and set a
 	 * transaction size.
@@ -238,6 +247,7 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 	 * Bytes are transmitted/received in pairs. Wait to receive all the
 	 * data because then transmission will be done as well.
 	 */
+	toread = in_bytes;
 	while (in_bytes) {
 		int temp;
 
@@ -250,13 +260,41 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 		}
 		if (rx_lvl > 0 && in_bytes) {
 			temp = readl(&regs->rx_data);
-			if (rxp)
+			if (!rxp && !stopping) {
+				in_bytes--;
+			} else if (spi_slave->skip_preamble) {
+				if (temp == SPI_PREAMBLE_END_BYTE) {
+					spi_slave->skip_preamble = 0;
+					stopping = 0;
+				}
+			} else {
 				*rxp++ = temp;
-			in_bytes--;
+				in_bytes--;
+			}
+			toread--;
+		}
+		/*
+		 * We have run out of input data, but haven't read enough
+		 * bytes after the preamble yet. Read some more, and make
+		 * sure that we transmit dummy bytes too, to keep things
+		 * going.
+		 */
+		else if (in_bytes && !toread) {
+			assert(!out_bytes);
+			out_bytes = in_bytes;
+			toread = in_bytes;
+			txp = NULL;
+			spi_request_bytes(regs, toread);
+		}
+		if (spi_slave->skip_preamble && get_timer(start) > 100) {
+			printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
+			       in_bytes, out_bytes);
+			return -1;
 		}
 	}
 	*dinp = rxp;
 	*doutp = txp;
+	return 0;
 }
 
 /**
@@ -276,6 +314,7 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
 	int upto, todo;
 	int bytelen;
+	int ret = 0;
 
 	/* spi core configured to do 8 bit transfers */
 	if (bitlen % 8) {
@@ -289,16 +328,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 
 	/* Exynos SPI limits each transfer to 65535 bytes */
 	bytelen =  bitlen / 8;
-	for (upto = 0; upto < bytelen; upto += todo) {
+	for (upto = 0; !ret && upto < bytelen; upto += todo) {
 		todo = min(bytelen - upto, (1 << 16) - 1);
-		spi_rx_tx(spi_slave, todo, &din, &dout);
+		ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
 	}
 
 	/* Stop the transaction, if necessary. */
-	if ((flags & SPI_XFER_END))
+	if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
 		spi_cs_deactivate(slave);
+		if (spi_slave->skip_preamble) {
+			assert(!spi_slave->skip_preamble);
+			debug("Failed to complete premable transaction\n");
+			ret = -1;
+		}
+	}
 
-	return 0;
+	return ret;
 }
 
 /**
@@ -325,6 +370,7 @@ void spi_cs_activate(struct spi_slave *slave)
 
 	clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
 	debug("Activate CS, bus %d\n", spi_slave->slave.bus);
+	spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
 }
 
 /**
Signature · 1 line
1.8.2.1
#8 lore raw

PATCH v2 1/1 EXYNOS: SPI: Support SPI_PREAMBLE mode

Quoted 10 lines · Simon Glass

From: Rajeshwari Shinde <[email protected]>

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

Quoted 6 lines

---
Changes in v2:
- Remove preamable_count variable which is not really needed
- Fix checkpatch warning (multiple assignments)

This v2 patch fixes the nits reported.

#9 lore raw

PATCH v2 1/1 EXYNOS: SPI: Support SPI_PREAMBLE mode

In reply to Simon Glass

Dear Rajeshwari Shinde,

Quoted 68 lines · Simon Glass

From: Rajeshwari Shinde <[email protected]>

Support interfaces with a preamble before each received message.

We handle this when the client has requested a SPI_XFER_END, meaning that we must close of the transaction. In this case we read until we see the preamble (or a timeout occurs), skipping all data before and including the preamble. The client will receive only data bytes after the preamble.

---
Changes in v2:
- Remove preamable_count variable which is not really needed
- Fix checkpatch warning (multiple assignments)

 drivers/spi/exynos_spi.c | 62 +++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 54 insertions(+), 8 deletions(-)
drivers/spi/exynos_spi.c +11 -2
index 607e1cd..b7b2ea5 100644
@@ -51,6 +51,7 @@ struct exynos_spi_slave {
 	unsigned int mode;
 	enum periph_id periph_id;	/* Peripheral ID for this device */
 	unsigned int fifo_size;
+	int skip_preamble;
 };
 
 static struct spi_bus *spi_get_bus(unsigned dev_index)
@@ -105,6 +106,8 @@ struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
 	else
 		spi_slave->fifo_size = 256;
 
+	spi_slave->skip_preamble = 0;
+
 	spi_slave->freq = bus->frequency;
 	if (max_hz)
 		spi_slave->freq = min(max_hz, spi_slave->freq);
@@ -217,17 +220,23 @@ static void spi_request_bytes(struct exynos_spi *regs, int count)
 	writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
-static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
-			void **dinp, void const **doutp)
+static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+			void **dinp, void const **doutp, unsigned long flags)
 {
 	struct exynos_spi *regs = spi_slave->regs;
 	uchar *rxp = *dinp;
 	const uchar *txp = *doutp;
 	int rx_lvl, tx_lvl;
 	uint out_bytes, in_bytes;
+	int toread;
+	unsigned start = get_timer(0);
+	int stopping;
 
 	out_bytes = in_bytes = todo;
 
+	stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
+					!(spi_slave->mode & SPI_SLAVE);
+
 	/*
 	 * If there's something to send, do a software reset and set a
 	 * transaction size.

drivers/spi/exynos_spi.c static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, · line 250

Quoted 4 lines
drivers/spi/exynos_spi.c +1 -0
@@ -238,6 +247,7 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 	 * Bytes are transmitted/received in pairs. Wait to receive all the
 	 * data because then transmission will be done as well.
 	 */
+	toread = in_bytes;

please add blank line here.

drivers/spi/exynos_spi.c static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, · line 261

Quoted 6 lines
drivers/spi/exynos_spi.c +0 -0
 	while (in_bytes) {
 		int temp;
 
@@ -250,13 +260,41 @@ static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 		}
 		if (rx_lvl > 0 && in_bytes) {

I think, in_bytes is always true. It's a redundant checking.

Quoted 6 lines
drivers/spi/exynos_spi.c +4 -1
 			temp = readl(&regs->rx_data);
-			if (rxp)
+			if (!rxp && !stopping) {
+				in_bytes--;
+			} else if (spi_slave->skip_preamble) {
+				if (temp == SPI_PREAMBLE_END_BYTE) {

drivers/spi/exynos_spi.c static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, · line 275

Quoted 10 lines
drivers/spi/exynos_spi.c +8 -1
+					spi_slave->skip_preamble = 0;
+					stopping = 0;
+				}
+			} else {
 				*rxp++ = temp;
-			in_bytes--;
+				in_bytes--;
+			}
+			toread--;
+		}

I think, this if statement is not logical.
The checking condition is differ, so it looks confused.

How about this?

+ temp = readl(&regs->rx_data);
+ if (spi_slave->skip_preamble) {
+ if (temp == SPI_PREAMBLE_END_BYTE) {
+ spi_slave->skip_preamble = 0;
+ stopping = 0;
+ }
+ } else {
+ if (rxp || stopping)
+ *rxp++ = temp;
+ in_bytes--;
+ }
+
+ toread--;

drivers/spi/exynos_spi.c static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, · line 281

Quoted 6 lines
drivers/spi/exynos_spi.c +6 -0
+		/*
+		 * We have run out of input data, but haven't read enough
+		 * bytes after the preamble yet. Read some more, and make
+		 * sure that we transmit dummy bytes too, to keep things
+		 * going.
+		 */

This comment is what for?
please move this comment into the brace.

drivers/spi/exynos_spi.c static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, · line 282

Quoted 1 line
drivers/spi/exynos_spi.c +1 -0
+		else if (in_bytes && !toread) {

in_bytes.. ditto.

And please fix brace.
} else if (

Quoted 4 lines
drivers/spi/exynos_spi.c +4 -0
+			assert(!out_bytes);
+			out_bytes = in_bytes;
+			toread = in_bytes;
+			txp = NULL;

drivers/spi/exynos_spi.c static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo, · line 296

Quoted 10 lines
drivers/spi/exynos_spi.c +6 -0
+			spi_request_bytes(regs, toread);
+		}
+		if (spi_slave->skip_preamble && get_timer(start) > 100) {
+			printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
+			       in_bytes, out_bytes);
+			return -1;
 		}
 	}
 	*dinp = rxp;
 	*doutp = txp;

please add blank line here.

Quoted 13 lines
drivers/spi/exynos_spi.c +2 -0
+	return 0;
 }
 
 /**
@@ -276,6 +314,7 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
 	int upto, todo;
 	int bytelen;
+	int ret = 0;
 
 	/* spi core configured to do 8 bit transfers */
 	if (bitlen % 8) {

drivers/spi/exynos_spi.c int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, · line 333

Quoted 8 lines
drivers/spi/exynos_spi.c +2 -2
@@ -289,16 +328,22 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 
 	/* Exynos SPI limits each transfer to 65535 bytes */
 	bytelen =  bitlen / 8;
-	for (upto = 0; upto < bytelen; upto += todo) {
+	for (upto = 0; !ret && upto < bytelen; upto += todo) {
 		todo = min(bytelen - upto, (1 << 16) - 1);
-		spi_rx_tx(spi_slave, todo, &din, &dout);
+		ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);

It looks good.
But I think, it's better to checking the error explicitly.

if (!ret)

	break;
Quoted 20 lines
drivers/spi/exynos_spi.c +8 -2
 	}
 
 	/* Stop the transaction, if necessary. */
-	if ((flags & SPI_XFER_END))
+	if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
 		spi_cs_deactivate(slave);
+		if (spi_slave->skip_preamble) {
+			assert(!spi_slave->skip_preamble);
+			debug("Failed to complete premable transaction\n");
+			ret = -1;
+		}
+	}
 
-	return 0;
+	return ret;
 }
 
 /**

drivers/spi/exynos_spi.c void spi_cs_activate(struct spi_slave *slave) · line 376

Quoted 7 lines
drivers/spi/exynos_spi.c +1 -0
@@ -325,6 +370,7 @@ void spi_cs_activate(struct spi_slave *slave)
 
 	clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
 	debug("Activate CS, bus %d\n", spi_slave->slave.bus);
+	spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
 }
 
 /**

Thanks,
Minkyu Kang.