Skip to content

Commit 3373e90

Browse files
ADESTMbroonie
authored andcommitted
spi: stm32: fix fifo threshold level in case of short transfer
When transfer is shorter than half of the fifo, set the data packet size up to transfer size instead of up to half of the fifo. Check also that threshold is set at least to 1 data frame. Signed-off-by: Amelie Delaunay <[email protected]> Signed-off-by: Alain Volmat <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 135dd87 commit 3373e90

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

drivers/spi/spi-stm32.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -467,27 +467,37 @@ static int stm32_spi_prepare_mbr(struct stm32_spi *spi, u32 speed_hz,
467467
/**
468468
* stm32h7_spi_prepare_fthlv - Determine FIFO threshold level
469469
* @spi: pointer to the spi controller data structure
470+
* @xfer_len: length of the message to be transferred
470471
*/
471-
static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi)
472+
static u32 stm32h7_spi_prepare_fthlv(struct stm32_spi *spi, u32 xfer_len)
472473
{
473-
u32 fthlv, half_fifo;
474+
u32 fthlv, half_fifo, packet;
474475

475476
/* data packet should not exceed 1/2 of fifo space */
476477
half_fifo = (spi->fifo_size / 2);
477478

479+
/* data_packet should not exceed transfer length */
480+
if (half_fifo > xfer_len)
481+
packet = xfer_len;
482+
else
483+
packet = half_fifo;
484+
478485
if (spi->cur_bpw <= 8)
479-
fthlv = half_fifo;
486+
fthlv = packet;
480487
else if (spi->cur_bpw <= 16)
481-
fthlv = half_fifo / 2;
488+
fthlv = packet / 2;
482489
else
483-
fthlv = half_fifo / 4;
490+
fthlv = packet / 4;
484491

485492
/* align packet size with data registers access */
486493
if (spi->cur_bpw > 8)
487494
fthlv -= (fthlv % 2); /* multiple of 2 */
488495
else
489496
fthlv -= (fthlv % 4); /* multiple of 4 */
490497

498+
if (!fthlv)
499+
fthlv = 1;
500+
491501
return fthlv;
492502
}
493503

@@ -1393,7 +1403,7 @@ static void stm32h7_spi_set_bpw(struct stm32_spi *spi)
13931403
cfg1_setb |= (bpw << STM32H7_SPI_CFG1_DSIZE_SHIFT) &
13941404
STM32H7_SPI_CFG1_DSIZE;
13951405

1396-
spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi);
1406+
spi->cur_fthlv = stm32h7_spi_prepare_fthlv(spi, spi->cur_xferlen);
13971407
fthlv = spi->cur_fthlv - 1;
13981408

13991409
cfg1_clrb |= STM32H7_SPI_CFG1_FTHLV;
@@ -1588,6 +1598,8 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
15881598

15891599
spin_lock_irqsave(&spi->lock, flags);
15901600

1601+
spi->cur_xferlen = transfer->len;
1602+
15911603
if (spi->cur_bpw != transfer->bits_per_word) {
15921604
spi->cur_bpw = transfer->bits_per_word;
15931605
spi->cfg->set_bpw(spi);
@@ -1635,8 +1647,6 @@ static int stm32_spi_transfer_one_setup(struct stm32_spi *spi,
16351647
goto out;
16361648
}
16371649

1638-
spi->cur_xferlen = transfer->len;
1639-
16401650
dev_dbg(spi->dev, "transfer communication mode set to %d\n",
16411651
spi->cur_comm);
16421652
dev_dbg(spi->dev,

0 commit comments

Comments
 (0)