Skip to contentSkip to conversation

hw/ssi: imx_spi: Correct the burst length > 32 bit transfer logic

PATCH In review Bin Meng 3 participants 8 messages Dec 17, 2020 – Jan 9, 2021 Arm i.MX and Layerscape

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/2 hw/ssi: imx_spi: Correct the burst length > 32 bit transfer logic

From: Bin Meng <[email protected]>

For the ECSPIx_CONREG register BURST_LENGTH field, the manual says:

0x020 A SPI burst contains the 1 LSB in first word and all 32 bits in second word. 0x021 A SPI burst contains the 2 LSB in first word and all 32 bits in second word.

Current logic uses either s->burst_length or 32, whichever smaller, to determine how many bits it should read from the tx fifo each time. For example, for a 48 bit burst length, current logic transfers the first 32 bit from the first word in the tx fifo, followed by a 16 bit from the second word in the tx fifo, which is wrong. The correct logic should be: transfer the first 16 bit from the first word in the tx fifo, followed by a 32 bit from the second word in the tx fifo.

With this change, SPI flash can be successfully probed by U-Boot on imx6 sabrelite board.

=> sf probe
SF: Detected sst25vf016b with page size 256 Bytes, erase size 4 KiB, total 2 MiB

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/ssi/imx_spi.c +4 -1
index 85c172e..509fb9f 100644
@@ -178,7 +178,10 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 
         DPRINTF("data tx:0x%08x\n", tx);
 
-        tx_burst = MIN(s->burst_length, 32);
+        tx_burst = s->burst_length % 32;
+        if (tx_burst == 0) {
+            tx_burst = 32;
+        }
 
         rx = 0;
 
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 2/2 hw/ssi: imx_spi: Correct tx and rx fifo endianness

From: Bin Meng <[email protected]>

The endianness of data exchange between tx and rx fifo is incorrect. Earlier bytes are supposed to show up on MSB and later bytes on LSB,
ie: in big endian. The manual does not explicitly say this, but the U-Boot and Linux driver codes have a swap on the data transferred to tx fifo and from rx fifo.

With this change, U-Boot read from / write to SPI flash tests pass.

=> sf test 1ff000 1000
SPI flash test:
0 erase: 0 ticks, 4096000 KiB/s 32768.000 Mbps
1 check: 3 ticks, 1333 KiB/s 10.664 Mbps
2 write: 235 ticks, 17 KiB/s 0.136 Mbps
3 read: 2 ticks, 2000 KiB/s 16.000 Mbps
Test passed
0 erase: 0 ticks, 4096000 KiB/s 32768.000 Mbps
1 check: 3 ticks, 1333 KiB/s 10.664 Mbps
2 write: 235 ticks, 17 KiB/s 0.136 Mbps
3 read: 2 ticks, 2000 KiB/s 16.000 Mbps

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/ssi/imx_spi.c +12 -4
index 509fb9f..71f0902 100644
@@ -156,13 +156,14 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 {
     uint32_t tx;
     uint32_t rx;
+    uint32_t data;
+    uint8_t byte;
 
     DPRINTF("Begin: TX Fifo Size = %d, RX Fifo Size = %d\n",
             fifo32_num_used(&s->tx_fifo), fifo32_num_used(&s->rx_fifo));
 
     while (!fifo32_is_empty(&s->tx_fifo)) {
         int tx_burst = 0;
-        int index = 0;
 
         if (s->burst_length <= 0) {
             s->burst_length = imx_spi_burst_length(s);
@@ -183,10 +184,18 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
             tx_burst = 32;
         }
 
+        data = 0;
+        for (int i = 0; i < tx_burst / 8; i++) {
+            byte = tx & 0xff;
+            tx = tx >> 8;
+            data = (data << 8) | byte;
+        }
+        tx = data;
+
         rx = 0;
 
         while (tx_burst > 0) {
-            uint8_t byte = tx & 0xff;
+            byte = tx & 0xff;
 
             DPRINTF("writing 0x%02x\n", (uint32_t)byte);
 
@@ -196,12 +205,11 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
             DPRINTF("0x%02x read\n", (uint32_t)byte);
 
             tx = tx >> 8;
-            rx |= (byte << (index * 8));
+            rx = (rx << 8) | byte;
 
             /* Remove 8 bits from the actual burst */
             tx_burst -= 8;
             s->burst_length -= 8;
-            index++;
         }
 
         DPRINTF("data rx:0x%08x\n", rx);
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/2] hw/ssi: imx_spi: Correct tx and rx fifo endianness

Quoted 8 lines · Bin Meng

From: Bin Meng <[email protected]>

The endianness of data exchange between tx and rx fifo is incorrect. Earlier bytes are supposed to show up on MSB and later bytes on LSB,
ie: in big endian. The manual does not explicitly say this, but the U-Boot and Linux driver codes have a swap on the data transferred to tx fifo and from rx fifo.

To check my understanding, if we have a burst length of 16 bits, say, when we do the fifo32_pop() of a 32 bit word, where in that word and which way round are the 2 bytes we are going to transfer ?

Quoted 44 lines

With this change, U-Boot read from / write to SPI flash tests pass.

=> sf test 1ff000 1000
SPI flash test:
0 erase: 0 ticks, 4096000 KiB/s 32768.000 Mbps
1 check: 3 ticks, 1333 KiB/s 10.664 Mbps
2 write: 235 ticks, 17 KiB/s 0.136 Mbps
3 read: 2 ticks, 2000 KiB/s 16.000 Mbps
Test passed
0 erase: 0 ticks, 4096000 KiB/s 32768.000 Mbps
1 check: 3 ticks, 1333 KiB/s 10.664 Mbps
2 write: 235 ticks, 17 KiB/s 0.136 Mbps
3 read: 2 ticks, 2000 KiB/s 16.000 Mbps

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/ssi/imx_spi.c +2 -1
index 509fb9f..71f0902 100644
@@ -156,13 +156,14 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 {
     uint32_t tx;
     uint32_t rx;
+    uint32_t data;
+    uint8_t byte;
 
     DPRINTF("Begin: TX Fifo Size = %d, RX Fifo Size = %d\n",
             fifo32_num_used(&s->tx_fifo), fifo32_num_used(&s->rx_fifo));
 
     while (!fifo32_is_empty(&s->tx_fifo)) {
         int tx_burst = 0;
-        int index = 0;
 
         if (s->burst_length <= 0) {
             s->burst_length = imx_spi_burst_length(s);
@@ -183,10 +184,18 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
             tx_burst = 32;

hw/ssi/imx_spi.c static void imx_spi_flush_txfifo(IMXSPIState *s) · line 194

Quoted 10 lines
hw/ssi/imx_spi.c +8 -0
         }
 
+        data = 0;
+        for (int i = 0; i < tx_burst / 8; i++) {
+            byte = tx & 0xff;
+            tx = tx >> 8;
+            data = (data << 8) | byte;
+        }
+        tx = data;
+

Why carefully reverse the order of bytes in the word and then take a byte at a time from the bottom of the word in the loop below, when you could change the loop to take bytes from the top of the word instead ?

Quoted 23 lines
hw/ssi/imx_spi.c +2 -4
         rx = 0;
 
         while (tx_burst > 0) {
-            uint8_t byte = tx & 0xff;
+            byte = tx & 0xff;
 
             DPRINTF("writing 0x%02x\n", (uint32_t)byte);
 
@@ -196,12 +205,11 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
             DPRINTF("0x%02x read\n", (uint32_t)byte);
 
             tx = tx >> 8;
-            rx |= (byte << (index * 8));
+            rx = (rx << 8) | byte;
 
             /* Remove 8 bits from the actual burst */
             tx_burst -= 8;
             s->burst_length -= 8;
-            index++;
         }
 
         DPRINTF("data rx:0x%08x\n", rx);
--
Quoted 1 line

2.7.4

thanks
-- PMM

author
#3 lore raw

Re: [PATCH 2/2] hw/ssi: imx_spi: Correct tx and rx fifo endianness

Hi Peter,

Quoted 11 lines · Peter Maydell
Quoted 8 lines · Bin Meng

From: Bin Meng <[email protected]>

The endianness of data exchange between tx and rx fifo is incorrect. Earlier bytes are supposed to show up on MSB and later bytes on LSB,
ie: in big endian. The manual does not explicitly say this, but the U-Boot and Linux driver codes have a swap on the data transferred to tx fifo and from rx fifo.

Quoted 3 lines

To check my understanding, if we have a burst length of 16 bits, say, when we do the fifo32_pop() of a 32 bit word, where in that word and which way round are the 2 bytes we are going to transfer ?

Say the fifo was written with a value of 0x00001234 when the burst length is 16 bits, 0x12 will be transferred first then followed by 0x34.

Quoted 47 lines
Quoted 47 lines

With this change, U-Boot read from / write to SPI flash tests pass.

=> sf test 1ff000 1000
SPI flash test:
0 erase: 0 ticks, 4096000 KiB/s 32768.000 Mbps
1 check: 3 ticks, 1333 KiB/s 10.664 Mbps
2 write: 235 ticks, 17 KiB/s 0.136 Mbps
3 read: 2 ticks, 2000 KiB/s 16.000 Mbps
Test passed
0 erase: 0 ticks, 4096000 KiB/s 32768.000 Mbps
1 check: 3 ticks, 1333 KiB/s 10.664 Mbps
2 write: 235 ticks, 17 KiB/s 0.136 Mbps
3 read: 2 ticks, 2000 KiB/s 16.000 Mbps

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
hw/ssi/imx_spi.c +2 -1
index 509fb9f..71f0902 100644
@@ -156,13 +156,14 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 {
     uint32_t tx;
     uint32_t rx;
+    uint32_t data;
+    uint8_t byte;
 
     DPRINTF("Begin: TX Fifo Size = %d, RX Fifo Size = %d\n",
             fifo32_num_used(&s->tx_fifo), fifo32_num_used(&s->rx_fifo));
 
     while (!fifo32_is_empty(&s->tx_fifo)) {
         int tx_burst = 0;
-        int index = 0;
 
         if (s->burst_length <= 0) {
             s->burst_length = imx_spi_burst_length(s);
@@ -183,10 +184,18 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
             tx_burst = 32;
         }
 
Quoted 13 lines

hw/ssi/imx_spi.c static void imx_spi_flush_txfifo(IMXSPIState *s) · line 194

Quoted 8 lines
hw/ssi/imx_spi.c +8 -0
+        data = 0;
+        for (int i = 0; i < tx_burst / 8; i++) {
+            byte = tx & 0xff;
+            tx = tx >> 8;
+            data = (data << 8) | byte;
+        }
+        tx = data;
+

Why carefully reverse the order of bytes in the word and then take a byte at a time from the bottom of the word in the loop below, when you could change the loop to take bytes from the top of the word instead ?

Ah, yes, this can be rewritten to simplify a little.

Quoted 16 lines
Quoted 16 lines
hw/ssi/imx_spi.c +2 -2
         rx = 0;
 
         while (tx_burst > 0) {
-            uint8_t byte = tx & 0xff;
+            byte = tx & 0xff;
 
             DPRINTF("writing 0x%02x\n", (uint32_t)byte);
 
@@ -196,12 +205,11 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
             DPRINTF("0x%02x read\n", (uint32_t)byte);
 
             tx = tx >> 8;
-            rx |= (byte << (index * 8));
+            rx = (rx << 8) | byte;
 
Quoted 8 lines

hw/ssi/imx_spi.c static void imx_spi_flush_txfifo(IMXSPIState *s)

Quoted 8 lines
hw/ssi/imx_spi.c +0 -2
             /* Remove 8 bits from the actual burst */
             tx_burst -= 8;
             s->burst_length -= 8;
-            index++;
         }
 
         DPRINTF("data rx:0x%08x\n", rx);
--

Regards,
Bin

author
#4 lore raw
In reply to Bin Meng
Quoted 22 lines · Bin Meng

From: Bin Meng <[email protected]>

For the ECSPIx_CONREG register BURST_LENGTH field, the manual says:

0x020 A SPI burst contains the 1 LSB in first word and all 32 bits in second word. 0x021 A SPI burst contains the 2 LSB in first word and all 32 bits in second word.

Current logic uses either s->burst_length or 32, whichever smaller, to determine how many bits it should read from the tx fifo each time. For example, for a 48 bit burst length, current logic transfers the first 32 bit from the first word in the tx fifo, followed by a 16 bit from the second word in the tx fifo, which is wrong. The correct logic should be: transfer the first 16 bit from the first word in the tx fifo, followed by a 32 bit from the second word in the tx fifo.

With this change, SPI flash can be successfully probed by U-Boot on imx6 sabrelite board.

=> sf probe
SF: Detected sst25vf016b with page size 256 Bytes, erase size 4 KiB, total 2 MiB

Quoted 7 lines
Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Ping?

author
#5 lore raw
Quoted 28 lines · Bin Meng
Quoted 26 lines · Bin Meng

From: Bin Meng <[email protected]>

For the ECSPIx_CONREG register BURST_LENGTH field, the manual says:

0x020 A SPI burst contains the 1 LSB in first word and all 32 bits in second word. 0x021 A SPI burst contains the 2 LSB in first word and all 32 bits in second word.

Current logic uses either s->burst_length or 32, whichever smaller, to determine how many bits it should read from the tx fifo each time. For example, for a 48 bit burst length, current logic transfers the first 32 bit from the first word in the tx fifo, followed by a 16 bit from the second word in the tx fifo, which is wrong. The correct logic should be: transfer the first 16 bit from the first word in the tx fifo, followed by a 32 bit from the second word in the tx fifo.

With this change, SPI flash can be successfully probed by U-Boot on imx6 sabrelite board.

=> sf probe
SF: Detected sst25vf016b with page size 256 Bytes, erase size 4 KiB, total 2 MiB

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

Quoted 5 lines
Quoted 3 lines
 hw/ssi/imx_spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Ping?

Ping?

Reviewed-by on v1 1/2
#6 lore raw
In reply to Bin Meng
Quoted 33 lines · Bin Meng

From: Bin Meng <[email protected]>

For the ECSPIx_CONREG register BURST_LENGTH field, the manual says:

0x020 A SPI burst contains the 1 LSB in first word and all 32 bits in second word. 0x021 A SPI burst contains the 2 LSB in first word and all 32 bits in second word.

Current logic uses either s->burst_length or 32, whichever smaller, to determine how many bits it should read from the tx fifo each time. For example, for a 48 bit burst length, current logic transfers the first 32 bit from the first word in the tx fifo, followed by a 16 bit from the second word in the tx fifo, which is wrong. The correct logic should be: transfer the first 16 bit from the first word in the tx fifo, followed by a 32 bit from the second word in the tx fifo.

With this change, SPI flash can be successfully probed by U-Boot on imx6 sabrelite board.

=> sf probe
SF: Detected sst25vf016b with page size 256 Bytes, erase size 4 KiB, total 2 MiB

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/ssi/imx_spi.c +0 -0
index 85c172e..509fb9f 100644

hw/ssi/imx_spi.c static void imx_spi_flush_txfifo(IMXSPIState *s) · line 184

Quoted 8 lines
hw/ssi/imx_spi.c +4 -1
@@ -178,7 +178,10 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 
         DPRINTF("data tx:0x%08x\n", tx);
 
-        tx_burst = MIN(s->burst_length, 32);
+        tx_burst = s->burst_length % 32;
+        if (tx_burst == 0) {
+            tx_burst = 32;
+        }

Or alternatively using ternary operator:

           tx_burst = (s->burst_length % 32) ?: 32;
Quoted 4 lines
hw/ssi/imx_spi.c +0 -0
         rx = 0;
 
 
author
#7 lore raw
Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 33 lines · Bin Meng

From: Bin Meng <[email protected]>

For the ECSPIx_CONREG register BURST_LENGTH field, the manual says:

0x020 A SPI burst contains the 1 LSB in first word and all 32 bits in second word. 0x021 A SPI burst contains the 2 LSB in first word and all 32 bits in second word.

Current logic uses either s->burst_length or 32, whichever smaller, to determine how many bits it should read from the tx fifo each time. For example, for a 48 bit burst length, current logic transfers the first 32 bit from the first word in the tx fifo, followed by a 16 bit from the second word in the tx fifo, which is wrong. The correct logic should be: transfer the first 16 bit from the first word in the tx fifo, followed by a 32 bit from the second word in the tx fifo.

With this change, SPI flash can be successfully probed by U-Boot on imx6 sabrelite board.

=> sf probe
SF: Detected sst25vf016b with page size 256 Bytes, erase size 4 KiB, total 2 MiB

Fixes c906a3a01582 ("i.MX: Add the Freescale SPI Controller")

---

 hw/ssi/imx_spi.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/ssi/imx_spi.c +0 -0
index 85c172e..509fb9f 100644

hw/ssi/imx_spi.c static void imx_spi_flush_txfifo(IMXSPIState *s) · line 184

Quoted 8 lines
hw/ssi/imx_spi.c +4 -1
@@ -178,7 +178,10 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 
         DPRINTF("data tx:0x%08x\n", tx);
 
-        tx_burst = MIN(s->burst_length, 32);
+        tx_burst = s->burst_length % 32;
+        if (tx_burst == 0) {
+            tx_burst = 32;
+        }

Or alternatively using ternary operator:

Quoted 1 line
           tx_burst = (s->burst_length % 32) ?: 32;

Updated this in v2 series:
http://patchwork.ozlabs.org/project/qemu-devel/list/?series=222931

Quoted 3 lines

Regards,
Bin