Skip to contentSkip to conversation

hw/ssi: imx_spi: Fix various bugs in the imx_spi model

PATCH Superseded Bin Meng 2 participants 12 messages Jan 9, 2021 – Jan 10, 2021 Arm i.MX and Layerscape

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

v3 cover hw/ssi: imx_spi: Fix various bugs in the imx_spi model

From: Bin Meng <[email protected]>

This series fixes a bunch of bugs in current implementation of the imx spi controller, including the following issues:

- chip select signal was not lower down when spi controller is disabled
- remove imx_spi_update_irq() in imx_spi_reset()
- transfer incorrect data when the burst length is larger than 32 bit
- spi controller tx and rx fifo endianness is incorrect

Tested with upstream U-Boot v2020.10 (polling mode) and VxWorks 7 (interrupt mode).

Changes in v3:
- Move the chip selects disable out of imx_spi_reset()
- new patch: remove imx_spi_update_irq() in imx_spi_reset()
- new patch: log unimplemented burst length
- Simplify the tx fifo endianness handling

Changes in v2:
- Fix the "Fixes" tag in the commit message
- Use ternary operator as Philippe suggested

Bin Meng (5):
hw/ssi: imx_spi: Use a macro for number of chip selects supported hw/ssi: imx_spi: Remove imx_spi_update_irq() in imx_spi_reset() hw/ssi: imx_spi: Log unimplemented burst length hw/ssi: imx_spi: Correct the burst length > 32 bit transfer logic hw/ssi: imx_spi: Correct tx and rx fifo endianness

Xuzhou Cheng (1):
hw/ssi: imx_spi: Disable chip selects when controller is disabled

 include/hw/ssi/imx_spi.h |  5 ++++-
 hw/ssi/imx_spi.c         | 42 ++++++++++++++++++++++++++++------------
 2 files changed, 34 insertions(+), 13 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/6 hw/ssi: imx_spi: Use a macro for number of chip selects supported

From: Bin Meng <[email protected]>

Avoid using a magic number (4) everywhere for the number of chip selects supported.

---

(no changes since v1)

 include/hw/ssi/imx_spi.h | 5 ++++-
 hw/ssi/imx_spi.c         | 4 ++--
 2 files changed, 6 insertions(+), 3 deletions(-)
include/hw/ssi/imx_spi.h +4 -1
index b82b17f364..eeaf49bbac 100644
@@ -77,6 +77,9 @@
 
 #define EXTRACT(value, name) extract32(value, name##_SHIFT, name##_LENGTH)
 
+/* number of chip selects supported */
+#define ECSPI_NUM_CS 4
+
 #define TYPE_IMX_SPI "imx.spi"
 OBJECT_DECLARE_SIMPLE_TYPE(IMXSPIState, IMX_SPI)
 
@@ -89,7 +92,7 @@ struct IMXSPIState {
 
     qemu_irq irq;
 
-    qemu_irq cs_lines[4];
+    qemu_irq cs_lines[ECSPI_NUM_CS];
 
     SSIBus *bus;
 
hw/ssi/imx_spi.c +2 -2
index d8885ae454..e605049a21 100644
@@ -361,7 +361,7 @@ static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value,
 
             /* We are in master mode */
 
-            for (i = 0; i < 4; i++) {
+            for (i = 0; i < ECSPI_NUM_CS; i++) {
                 qemu_set_irq(s->cs_lines[i],
                              i == imx_spi_selected_channel(s) ? 0 : 1);
             }
@@ -424,7 +424,7 @@ static void imx_spi_realize(DeviceState *dev, Error **errp)
     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
 
-    for (i = 0; i < 4; ++i) {
+    for (i = 0; i < ECSPI_NUM_CS; ++i) {
         sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->cs_lines[i]);
     }
 
Signature · 1 line
2.25.1
Reviewed-by on v3 1/6
#2 lore raw

Re: [PATCH v3 1/6] hw/ssi: imx_spi: Use a macro for number of chip selects supported

Quoted 7 lines · Bin Meng

From: Bin Meng <[email protected]>

Avoid using a magic number (4) everywhere for the number of chip selects supported.

Quoted 7 lines

---

(no changes since v1)

 include/hw/ssi/imx_spi.h | 5 ++++-
 hw/ssi/imx_spi.c         | 4 ++--
 2 files changed, 6 insertions(+), 3 deletions(-)
author
#3 lore raw

PATCH v3 2/6 hw/ssi: imx_spi: Disable chip selects when controller is disabled

In reply to Bin Meng

From: Xuzhou Cheng <[email protected]>

When a write to ECSPI_CONREG register to disable the SPI controller, imx_spi_reset() is called to reset the controller, but chip select lines should have been disabled, otherwise the state machine of any devices (e.g.: SPI flashes) connected to the SPI master is stuck to its last state and responds incorrectly to any follow-up commands.

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

---

Changes in v3:
- Move the chip selects disable out of imx_spi_reset()

Changes in v2:
- Fix the "Fixes" tag in the commit message

 hw/ssi/imx_spi.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/ssi/imx_spi.c +5 -0
index e605049a21..8d429e703f 100644
@@ -353,6 +353,11 @@ static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value,
         if (!imx_spi_is_enabled(s)) {
             /* device is disabled, so this is a reset */
             imx_spi_reset(DEVICE(s));
+
+            for (int i = 0; i < ECSPI_NUM_CS; i++) {
+                qemu_set_irq(s->cs_lines[i], 1);
+            }
+
             return;
         }
 
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH v3 2/6] hw/ssi: imx_spi: Disable chip selects when controller is disabled

Hi,

Quoted 28 lines · Bin Meng

From: Xuzhou Cheng <[email protected]>

When a write to ECSPI_CONREG register to disable the SPI controller, imx_spi_reset() is called to reset the controller, but chip select lines should have been disabled, otherwise the state machine of any devices (e.g.: SPI flashes) connected to the SPI master is stuck to its last state and responds incorrectly to any follow-up commands.

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

---

Changes in v3:
- Move the chip selects disable out of imx_spi_reset()

Changes in v2:
- Fix the "Fixes" tag in the commit message

 hw/ssi/imx_spi.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/ssi/imx_spi.c +0 -0
index e605049a21..8d429e703f 100644

hw/ssi/imx_spi.c static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value, · line 359

Quoted 7 lines
hw/ssi/imx_spi.c +4 -0
@@ -353,6 +353,11 @@ static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value,
         if (!imx_spi_is_enabled(s)) {
             /* device is disabled, so this is a reset */
             imx_spi_reset(DEVICE(s));
+
+            for (int i = 0; i < ECSPI_NUM_CS; i++) {
+                qemu_set_irq(s->cs_lines[i], 1);
+            }

Shouldn't this be done in imx_spi_reset()?

author
#5 lore raw

PATCH v3 3/6 hw/ssi: imx_spi: Remove imx_spi_update_irq() in imx_spi_reset()

In reply to Bin Meng

From: Bin Meng <[email protected]>

Usually the approach is that the device on the other end of the line is going to reset its state anyway, so there's no need to actively signal an irq line change during the reset hook.

Move imx_spi_update_irq() out of imx_spi_reset(), along with the disabling of chip selects, to a new function imx_spi_soft_reset() that is called when the controller is disabled.

---

Changes in v3:
- new patch: remove imx_spi_update_irq() in imx_spi_reset()

 hw/ssi/imx_spi.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
hw/ssi/imx_spi.c +14 -7
index 8d429e703f..880939f595 100644
@@ -241,9 +241,20 @@ static void imx_spi_reset(DeviceState *dev)
     imx_spi_rxfifo_reset(s);
     imx_spi_txfifo_reset(s);
 
+    s->burst_length = 0;
+}
+
+static void imx_spi_soft_reset(IMXSPIState *s)
+{
+    int i;
+
+    imx_spi_reset(DEVICE(s));
+
     imx_spi_update_irq(s);
 
-    s->burst_length = 0;
+    for (i = 0; i < ECSPI_NUM_CS; i++) {
+        qemu_set_irq(s->cs_lines[i], 1);
+    }
 }
 
 static uint64_t imx_spi_read(void *opaque, hwaddr offset, unsigned size)
@@ -351,12 +362,8 @@ static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value,
         s->regs[ECSPI_CONREG] = value;
 
         if (!imx_spi_is_enabled(s)) {
-            /* device is disabled, so this is a reset */
-            imx_spi_reset(DEVICE(s));
-
-            for (int i = 0; i < ECSPI_NUM_CS; i++) {
-                qemu_set_irq(s->cs_lines[i], 1);
-            }
+            /* device is disabled, so this is a soft reset */
+            imx_spi_soft_reset(s);
 
             return;
         }
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v3 3/6] hw/ssi: imx_spi: Remove imx_spi_update_irq() in imx_spi_reset()

Quoted 9 lines · Bin Meng

From: Bin Meng <[email protected]>

Usually the approach is that the device on the other end of the line is going to reset its state anyway, so there's no need to actively signal an irq line change during the reset hook.

Move imx_spi_update_irq() out of imx_spi_reset(), along with the disabling of chip selects, to a new function imx_spi_soft_reset() that is called when the controller is disabled.

Now I read this patch, forget my comment on previous patch.

Quoted 17 lines

---

Changes in v3:
- new patch: remove imx_spi_update_irq() in imx_spi_reset()

 hw/ssi/imx_spi.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
hw/ssi/imx_spi.c +0 -0
index 8d429e703f..880939f595 100644
@@ -241,9 +241,20 @@ static void imx_spi_reset(DeviceState *dev)
     imx_spi_rxfifo_reset(s);

hw/ssi/imx_spi.c static void imx_spi_reset(DeviceState *dev) · line 251

Quoted 10 lines
hw/ssi/imx_spi.c +8 -0
     imx_spi_txfifo_reset(s);
 
+    s->burst_length = 0;
+}
+
+static void imx_spi_soft_reset(IMXSPIState *s)
+{
+    int i;
+
+    imx_spi_reset(DEVICE(s));

Hmm usually hard reset include soft reset.

hw/ssi/imx_spi.c static void imx_spi_reset(DeviceState *dev) · line 256

Quoted 6 lines
hw/ssi/imx_spi.c +3 -1
+
     imx_spi_update_irq(s);
 
-    s->burst_length = 0;
+    for (i = 0; i < ECSPI_NUM_CS; i++) {
+        qemu_set_irq(s->cs_lines[i], 1);

Isn't this part of the hard reset?

Quoted 6 lines
hw/ssi/imx_spi.c +1 -0
+    }
 }
 
 static uint64_t imx_spi_read(void *opaque, hwaddr offset, unsigned size)
@@ -351,12 +362,8 @@ static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value,
         s->regs[ECSPI_CONREG] = value;

hw/ssi/imx_spi.c static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value, · line 366

Quoted 10 lines
hw/ssi/imx_spi.c +2 -6
 
         if (!imx_spi_is_enabled(s)) {
-            /* device is disabled, so this is a reset */
-            imx_spi_reset(DEVICE(s));
-
-            for (int i = 0; i < ECSPI_NUM_CS; i++) {
-                qemu_set_irq(s->cs_lines[i], 1);
-            }
+            /* device is disabled, so this is a soft reset */
+            imx_spi_soft_reset(s);

Maybe you can restructure patches 2/3, first introduce imx_spi_soft_reset() - this patch - then fix ECSPI_CONREG
- the previous patch -.

Quoted 4 lines
hw/ssi/imx_spi.c +0 -0
             return;
         }
 
author
#7 lore raw

Re: [PATCH v3 3/6] hw/ssi: imx_spi: Remove imx_spi_update_irq() in imx_spi_reset()

Hi Philippe,

Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 9 lines · Bin Meng

From: Bin Meng <[email protected]>

Usually the approach is that the device on the other end of the line is going to reset its state anyway, so there's no need to actively signal an irq line change during the reset hook.

Move imx_spi_update_irq() out of imx_spi_reset(), along with the disabling of chip selects, to a new function imx_spi_soft_reset() that is called when the controller is disabled.

Now I read this patch, forget my comment on previous patch.

Quoted 19 lines

---

Changes in v3:
- new patch: remove imx_spi_update_irq() in imx_spi_reset()

 hw/ssi/imx_spi.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
hw/ssi/imx_spi.c +0 -0
index 8d429e703f..880939f595 100644
@@ -241,9 +241,20 @@ static void imx_spi_reset(DeviceState *dev)
     imx_spi_rxfifo_reset(s);
     imx_spi_txfifo_reset(s);
 
Quoted 10 lines

hw/ssi/imx_spi.c static void imx_spi_reset(DeviceState *dev) · line 251

Quoted 8 lines
hw/ssi/imx_spi.c +8 -0
+    s->burst_length = 0;
+}
+
+static void imx_spi_soft_reset(IMXSPIState *s)
+{
+    int i;
+
+    imx_spi_reset(DEVICE(s));

Hmm usually hard reset include soft reset.

That's my understanding as well.

Quoted 10 lines

hw/ssi/imx_spi.c static void imx_spi_reset(DeviceState *dev) · line 256

Quoted 7 lines
hw/ssi/imx_spi.c +3 -1
+
     imx_spi_update_irq(s);
 
-    s->burst_length = 0;
+    for (i = 0; i < ECSPI_NUM_CS; i++) {
+        qemu_set_irq(s->cs_lines[i], 1);

Isn't this part of the hard reset?

I think we can rename the name to imx_spi_hard_reset() to avoid such confusion.

Quoted 7 lines
Quoted 7 lines
hw/ssi/imx_spi.c +1 -0
+    }
 }
 
 static uint64_t imx_spi_read(void *opaque, hwaddr offset, unsigned size)
@@ -351,12 +362,8 @@ static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value,
         s->regs[ECSPI_CONREG] = value;
 
Quoted 13 lines

hw/ssi/imx_spi.c static void imx_spi_write(void *opaque, hwaddr offset, uint64_t value, · line 366

Quoted 9 lines
hw/ssi/imx_spi.c +2 -6
         if (!imx_spi_is_enabled(s)) {
-            /* device is disabled, so this is a reset */
-            imx_spi_reset(DEVICE(s));
-
-            for (int i = 0; i < ECSPI_NUM_CS; i++) {
-                qemu_set_irq(s->cs_lines[i], 1);
-            }
+            /* device is disabled, so this is a soft reset */
+            imx_spi_soft_reset(s);

Maybe you can restructure patches 2/3, first introduce imx_spi_soft_reset() - this patch - then fix ECSPI_CONREG
- the previous patch -.

Sure.

Regards,
Bin

author
#8 lore raw

PATCH v3 4/6 hw/ssi: imx_spi: Log unimplemented burst length

In reply to Bin Meng

From: Bin Meng <[email protected]>

Current implementation of the imx spi controller expects the burst length to be multiple of 8, which is the most normal use case.

In case the burst length is not what we expect, log it to give user a chance to notice it.

---

Changes in v3:
- new patch: log unimplemented burst length

 hw/ssi/imx_spi.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
hw/ssi/imx_spi.c +10 -1
index 880939f595..609d4b658e 100644
@@ -128,7 +128,16 @@ static uint8_t imx_spi_selected_channel(IMXSPIState *s)
 
 static uint32_t imx_spi_burst_length(IMXSPIState *s)
 {
-    return EXTRACT(s->regs[ECSPI_CONREG], ECSPI_CONREG_BURST_LENGTH) + 1;
+    uint32_t burst;
+
+    burst = EXTRACT(s->regs[ECSPI_CONREG], ECSPI_CONREG_BURST_LENGTH) + 1;
+    if (burst % 8) {
+        qemu_log_mask(LOG_UNIMP,
+                      "[%s]%s: burst length not multiple of 8!\n",
+                      TYPE_IMX_SPI, __func__);
+    }
+
+    return burst;
 }
 
 static bool imx_spi_is_enabled(IMXSPIState *s)
Signature · 1 line
2.25.1
Reviewed-by on v3 4/6
#9 lore raw

Re: [PATCH v3 4/6] hw/ssi: imx_spi: Log unimplemented burst length

Quoted 4 lines · Bin Meng

From: Bin Meng <[email protected]>

Current implementation of the imx spi controller expects the burst length to be multiple of 8, which is the most normal use case.

s/normal/common/?

Quoted 20 lines

In case the burst length is not what we expect, log it to give user a chance to notice it.

---

Changes in v3:
- new patch: log unimplemented burst length

 hw/ssi/imx_spi.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
hw/ssi/imx_spi.c +0 -0
index 880939f595..609d4b658e 100644
@@ -128,7 +128,16 @@ static uint8_t imx_spi_selected_channel(IMXSPIState *s)
 

hw/ssi/imx_spi.c static uint8_t imx_spi_selected_channel(IMXSPIState *s) · line 137

Quoted 10 lines
hw/ssi/imx_spi.c +7 -1
 static uint32_t imx_spi_burst_length(IMXSPIState *s)
 {
-    return EXTRACT(s->regs[ECSPI_CONREG], ECSPI_CONREG_BURST_LENGTH) + 1;
+    uint32_t burst;
+
+    burst = EXTRACT(s->regs[ECSPI_CONREG], ECSPI_CONREG_BURST_LENGTH) + 1;
+    if (burst % 8) {
+        qemu_log_mask(LOG_UNIMP,
+                      "[%s]%s: burst length not multiple of 8!\n",
+                      TYPE_IMX_SPI, __func__);

Please log the burst length value in the log message.

With it:

Quoted 7 lines
hw/ssi/imx_spi.c +3 -0
+    }
+
+    return burst;
 }
 
 static bool imx_spi_is_enabled(IMXSPIState *s)
author
#10 lore raw

PATCH v3 5/6 hw/ssi: imx_spi: Correct the burst length > 32 bit transfer logic

In reply to 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")

---

(no changes since v2)

Changes in v2:
- Use ternary operator as Philippe suggested

 hw/ssi/imx_spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ssi/imx_spi.c +1 -1
index 609d4b658e..68a32b689e 100644
@@ -187,7 +187,7 @@ 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) ? : 32;
 
         rx = 0;
 
Signature · 1 line
2.25.1
author
#11 lore raw

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

In reply to 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.

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")

---

Changes in v3:
- Simplify the tx fifo endianness handling

 hw/ssi/imx_spi.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
hw/ssi/imx_spi.c +2 -5
index 68a32b689e..a81242e860 100644
@@ -171,7 +171,6 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
 
     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);
@@ -192,7 +191,7 @@ static void imx_spi_flush_txfifo(IMXSPIState *s)
         rx = 0;
 
         while (tx_burst > 0) {
-            uint8_t byte = tx & 0xff;
+            uint8_t byte = tx >> (tx_burst - 8);
 
             DPRINTF("writing 0x%02x\n", (uint32_t)byte);
 
@@ -201,13 +200,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.25.1