Skip to content

Commit e0205d6

Browse files
miquelraynalbroonie
authored andcommitted
spi: atmel: Prevent false timeouts on long transfers
A slow SPI bus clocks at ~20MHz, which means it would transfer about 2500 bytes per second with a single data line. Big transfers, like when dealing with flashes can easily reach a few MiB. The current DMA timeout is set to 1 second, which means any working transfer of about 4MiB will always be cancelled. With the above derivations, on a slow bus, we can assume every byte will take at most 0.4ms. Said otherwise, we could add 4ms to the 1-second timeout delay every 10kiB. On a 4MiB transfer, it would bring the timeout delay up to 2.6s which still seems rather acceptable for a timeout. The consequence of this is that long transfers might be allowed, which hence requires the need to interrupt the transfer if wanted by the user. We can hence switch to the _interruptible variant of wait_for_completion. This leads to a little bit more handling to also handle the interrupted case but looks really acceptable overall. While at it, we drop the useless, noisy and redundant WARN_ON() call. Signed-off-by: Miquel Raynal <[email protected]> Acked-by: Ryan Wanner <[email protected]> Link: https://lore.kernel.org/r/Message-Id: <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent d8e4ebf commit e0205d6

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

drivers/spi/spi-atmel.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@
233233
*/
234234
#define DMA_MIN_BYTES 16
235235

236-
#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
236+
#define SPI_DMA_MIN_TIMEOUT (msecs_to_jiffies(1000))
237+
#define SPI_DMA_TIMEOUT_PER_10K (msecs_to_jiffies(4))
237238

238239
#define AUTOSUSPEND_TIMEOUT 2000
239240

@@ -1279,7 +1280,8 @@ static int atmel_spi_one_transfer(struct spi_controller *host,
12791280
struct atmel_spi_device *asd;
12801281
int timeout;
12811282
int ret;
1282-
unsigned long dma_timeout;
1283+
unsigned int dma_timeout;
1284+
long ret_timeout;
12831285

12841286
as = spi_controller_get_devdata(host);
12851287

@@ -1333,11 +1335,13 @@ static int atmel_spi_one_transfer(struct spi_controller *host,
13331335
atmel_spi_unlock(as);
13341336
}
13351337

1336-
dma_timeout = wait_for_completion_timeout(&as->xfer_completion,
1337-
SPI_DMA_TIMEOUT);
1338-
if (WARN_ON(dma_timeout == 0)) {
1339-
dev_err(&spi->dev, "spi transfer timeout\n");
1340-
as->done_status = -EIO;
1338+
dma_timeout = msecs_to_jiffies(spi_controller_xfer_timeout(host, xfer));
1339+
ret_timeout = wait_for_completion_interruptible_timeout(&as->xfer_completion,
1340+
dma_timeout);
1341+
if (ret_timeout <= 0) {
1342+
dev_err(&spi->dev, "spi transfer %s\n",
1343+
!ret_timeout ? "timeout" : "canceled");
1344+
as->done_status = ret_timeout < 0 ? ret_timeout : -EIO;
13411345
}
13421346

13431347
if (as->done_status)

0 commit comments

Comments
 (0)