spi: stm32: OSPI and QSPI throughput optimization
cover spi: stm32: OSPI and QSPI throughput optimization
Update OSPI and QSPI drivers to optimize indirect read/write accesses to increase throughput.
---
Patrice Chotard (3):
spi: stm32-ospi: Increase read throughput in indirect mode
spi: stm32-qspi: Increase read throughput in indirect mode
spi: stm32-qspi: Optimize FIFO accesses using u16 or u32
drivers/spi/stm32_ospi.c | 4 ++- drivers/spi/stm32_qspi.c | 63 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 11 deletions(-)
---
base-commit: aff0f4d30ec6d7968578fc05fc6df6b5700d9913
change-id: 20251215-next-e0b3ed280f98
Best regards,
Signature · 1 line
Patrice Chotard <[email protected]>
PATCH 1/3 spi: stm32-ospi: Increase read throughput in indirect mode
Schedule() is called every u8/u16 or u32 read accesses which is overkill. Move schedule() from stm32_ospi_read_fifo() to stm32_ospi_tx_poll() and call schedule() only every 1MB chunk of data.
Test performed by reading 64MB on sNOR on stm32mp257f-ev1 board:
before after ratio
Read : 10.6MB/s 14.2MB/s +34%
--- drivers/spi/stm32_ospi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/stm32_ospi.c +3 -1
@@ -142,7 +142,6 @@ static void stm32_ospi_read_fifo(void *val, phys_addr_t addr, u8 len)case sizeof (u8): *((u8 *)val) =readb_relaxed (addr); }; -schedule (); }static void stm32_ospi_write_fifo (void *val,phys_addr_t addr,u8 len)
@@ -200,6 +199,9 @@ int stm32_ospi_tx_poll(struct udevice *dev, void *buf, u32 len, bool read)fifo (buf, regs_base +OSPI_DR , step); len -= step; buf += step; + +if (!(len %SZ_1M )) +schedule (); }return 0 ;
Signature · 1 line
2.43.0
Re: [PATCH 1/3] spi: stm32-ospi: Increase read throughput in indirect mode
Hi,
Quoted 28 lines · Patrice Chotard
Schedule()is called every u8/u16 or u32 read accesses which is overkill. Moveschedule()fromstm32_ospi_read_fifo()tostm32_ospi_tx_poll()and callschedule()only every 1MB chunk of data.Test performed by reading 64MB on sNOR on stm32mp257f-ev1 board:
before after ratio
Read : 10.6MB/s 14.2MB/s +34%Signed-off-by Patrice Chotard <[email protected]>--- drivers/spi/stm32_ospi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/spi/stm32_ospi.c +0
-1@@ -142,7 +142,6 @@ static void stm32_ospi_read_fifo(void *val, phys_addr_t addr, u8 len)case sizeof (u8): *((u8 *)val) =readb_relaxed (addr); }; -schedule (); }static void stm32_ospi_write_fifo (void *val,phys_addr_t addr,u8 len)
drivers/spi/stm32_ospi.c int stm32_ospi_tx_poll(struct udevice *dev, void *buf, u32 len, bool read) · line 207
Quoted 9 lines
drivers/spi/stm32_ospi.c +3
-0@@ -200,6 +199,9 @@ int stm32_ospi_tx_poll(struct udevice *dev, void *buf, u32 len, bool read)fifo (buf, regs_base +OSPI_DR , step); len -= step; buf += step; + +if (!(len %SZ_1M )) +schedule (); }return 0 ;
Thanks
Patrick
PATCH 2/3 spi: stm32-qspi: Increase read throughput in indirect mode
In reply to Patrice ChotardWhen WATCHDOG_RESET() was replaced by schedule() in commit 29caf9305b6f ("cyclic: Use schedule() instead of WATCHDOG_RESET()") we not only reset the watchdog but also call the cyclic infrastructure which takes time and has impact on read accesses performances.
Move schedule() from _stm32_qspi_read_fifo() to _stm32_qspi_poll() and call schedule() only every 1MB chunk of data.
Test performed by reading 64MB on sNOR on stm32mp157c-ev1 board:
before after ratio
Read : 201 KB/s 520KB/s +258%
--- drivers/spi/stm32_qspi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/stm32_qspi.c +3 -1
@@ -165,7 +165,6 @@ static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,static void _stm32_qspi_read_fifo (u8 *val,void __iomem *addr) { *val =readb (addr); -schedule (); }static void _stm32_qspi_write_fifo (u8 *val,void __iomem *addr)
@@ -200,6 +199,9 @@ static int _stm32_qspi_poll(struct stm32_qspi_priv *priv, }fifo (buf++, &priv->regs->dr); + +if (!(len %SZ_1M )) +schedule (); }return 0 ;
Signature · 1 line
2.43.0
Re: [PATCH 2/3] spi: stm32-qspi: Increase read throughput in indirect mode
Hi,
Quoted 32 lines · Patrice Chotard
When
WATCHDOG_RESET()was replaced byschedule()in commit29caf9305b6f("cyclic: Useschedule()instead ofWATCHDOG_RESET()") we not only reset the watchdog but also call the cyclic infrastructure which takes time and has impact on read accesses performances.Move
schedule()from_stm32_qspi_read_fifo()to_stm32_qspi_poll()and callschedule()only every 1MB chunk of data.Test performed by reading 64MB on sNOR on stm32mp157c-ev1 board:
before after ratio
Read : 201 KB/s 520KB/s +258%Signed-off-by Patrice Chotard <[email protected]>--- drivers/spi/stm32_qspi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/spi/stm32_qspi.c +0
-1@@ -165,7 +165,6 @@ static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,static void _stm32_qspi_read_fifo (u8 *val,void __iomem *addr) { *val =readb (addr); -schedule (); }static void _stm32_qspi_write_fifo (u8 *val,void __iomem *addr)
drivers/spi/stm32_qspi.c static int _stm32_qspi_poll(struct stm32_qspi_priv *priv, · line 207
Quoted 9 lines
drivers/spi/stm32_qspi.c +3
-0@@ -200,6 +199,9 @@ static int _stm32_qspi_poll(struct stm32_qspi_priv *priv, }fifo (buf++, &priv->regs->dr); + +if (!(len %SZ_1M )) +schedule (); }return 0 ;
Thanks
Patrick
PATCH 3/3 spi: stm32-qspi: Optimize FIFO accesses using u16 or u32
In reply to Patrice ChotardFIFO accesses uses u8 only for read/write.
In order to optimize throughput, add u16 or u32 read/write accesses when possible.
Set FIFO threshold level value accordingly.
Test performed by writing and reading 64MB on sNOR on stm32mp157c-ev1 board:
before after ratio
Write : 428 KB/s 719 KB/s +68% Read : 520 KB/s 3200 KB/s +615%
--- drivers/spi/stm32_qspi.c | 59 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-)
drivers/spi/stm32_qspi.c +50 -9
@@ -50,6 +50,7 @@ struct stm32_qspi_regs {#define STM32_QSPI_CR_SSHIFT BIT(4)#define STM32_QSPI_CR_DFM BIT(6)#define STM32_QSPI_CR_FSEL BIT(7) +#define STM32_QSPI_CR_FTHRES_MASK GENMASK(4, 0)#define STM32_QSPI_CR_FTHRES_SHIFT 8#define STM32_QSPI_CR_TEIE BIT(16)#define STM32_QSPI_CR_TCIE BIT(17)
@@ -162,23 +163,42 @@ static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,return ret; } -static void _stm32_qspi_read_fifo (u8 *val,void __iomem *addr) +static void _stm32_qspi_read_fifo (void *val,void __iomem *addr,u8 len) { - *val =readb (addr); +switch (len) { +case sizeof (u32): + *((u32 *)val) =readl_relaxed (addr); +break ; +case sizeof (u16): + *((u16 *)val) =readw_relaxed (addr); +break ; +case sizeof (u8): + *((u8 *)val) =readb_relaxed (addr); + }; } -static void _stm32_qspi_write_fifo (u8 *val,void __iomem *addr) +static void _stm32_qspi_write_fifo (void *val,void __iomem *addr,u8 len) { -writeb (*val, addr); +switch (len) { +case sizeof (u32): +writel_relaxed (*((u32 *)val), addr); +break ; +case sizeof (u16): +writew_relaxed (*((u16 *)val), addr); +break ; +case sizeof (u8): +writeb_relaxed (*((u8 *)val), addr); + }; }static int _stm32_qspi_poll (struct stm32_qspi_priv *priv,const struct spi_mem_op *op) { -void (*fifo)(u8 *val,void __iomem *addr); +void (*fifo)(void *val,void __iomem *addr,u8 len);u32 len = op->data.nbytes, sr; -u8 *buf; +void *buf;int ret; +u8 step =0 ;if (op->data.dir ==SPI_MEM_DATA_IN ) { fifo = _stm32_qspi_read_fifo;
@@ -186,10 +206,10 @@ static int _stm32_qspi_poll(struct stm32_qspi_priv *priv, }else { fifo = _stm32_qspi_write_fifo; - buf = (u8 *)op->data.buf.out; + buf = (void *)op->data.buf.out; } -while (len--) { +while (len) { ret =readl_poll_timeout (&priv->regs->sr, sr, sr &STM32_QSPI_SR_FTF ,STM32_QSPI_FIFO_TIMEOUT_US );
@@ -198,7 +218,23 @@ static int _stm32_qspi_poll(struct stm32_qspi_priv *priv,return ret; } -fifo (buf++, &priv->regs->dr); +if (!IS_ALIGNED ((uintptr_t )buf,sizeof (u32))) { +if (!IS_ALIGNED ((uintptr_t )buf,sizeof (u16))) + step =sizeof (u8); +else + step =min ((u32 )len, (u32 )sizeof (u16)); + } +/* Buf is aligned */ +else if (len >=sizeof (u32)) + step =sizeof (u32); +else if (len >=sizeof (u16)) + step =sizeof (u16); +else if (len) + step =sizeof (u8); + +fifo (buf, &priv->regs->dr, step); + len -= step; + buf += step;if (!(len %SZ_1M ))schedule ();
@@ -383,6 +419,11 @@ static int stm32_qspi_probe(struct udevice *bus) priv->cs_used =-1 ; +clrsetbits_le32 (&priv->regs->cr, +STM32_QSPI_CR_FTHRES_MASK << +STM32_QSPI_CR_FTHRES_SHIFT , +3 <<STM32_QSPI_CR_FTHRES_SHIFT ); +setbits_le32 (&priv->regs->cr,STM32_QSPI_CR_SSHIFT );/* Set dcr fsize to max address */
Signature · 1 line
2.43.0
Re: [PATCH 3/3] spi: stm32-qspi: Optimize FIFO accesses using u16 or u32
Hi,
Quoted 8 lines · Patrice Chotard
FIFO accesses uses u8 only for read/write.
In order to optimize throughput, add u16 or u32 read/write accesses when possible.
Set FIFO threshold level value accordingly.Test performed by writing and reading 64MB on sNOR on stm32mp157c-ev1 board:
Quoted 9 lines
before after ratio
Write : 428 KB/s 719 KB/s +68% Read : 520 KB/s 3200 KB/s +615%Signed-off-by Patrice Chotard <[email protected]>--- drivers/spi/stm32_qspi.c | 59 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-)
Quoted 10 lines · Patrice Chotard
Update OSPI and QSPI drivers to optimize indirect read/write accesses to increase throughput.
Signed-off-by Patrice Chotard <[email protected]>---
Patrice Chotard (3):
spi: stm32-ospi: Increase read throughput in indirect mode
spi: stm32-qspi: Increase read throughput in indirect mode
spi: stm32-qspi: Optimize FIFO accesses using u16 or u32
Quoted 8 lines
drivers/spi/stm32_ospi.c | 4 ++- drivers/spi/stm32_qspi.c | 63 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 56 insertions(+), 11 deletions(-)---
base-commit:aff0f4d30ec6d7968578fc05fc6df6b5700d9913
change-id: 20251215-next-e0b3ed280f98Best regards,
For the whole series
Applied to u-boot-stm32/master
Thanks
Patrice