Skip to content

Commit 26d6084

Browse files
committed
Merge tag 'spi-fix-v6.7-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
Pull spi fixes from Mark Brown: "A few bigger things here, the main one being that there were changes to the atmel driver in this cycle which made it possible to kill transfers being used for filesystem I/O which turned out to be very disruptive, the series of patches here undoes that and hardens things up further. There's also a few smaller driver specific changes, the main one being to revert a change that duplicted delays" * tag 'spi-fix-v6.7-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi: spi: atmel: Fix clock issue when using devices with different polarities spi: spi-imx: correctly configure burst length when using dma spi: cadence: revert "Add SPI transfer delays" spi: atmel: Prevent spi transfers from being killed spi: atmel: Drop unused defines spi: atmel: Do not cancel a transfer upon any signal
2 parents ceb6a6f + fc70d64 commit 26d6084

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

drivers/spi/spi-atmel.c

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/gpio/consumer.h>
2323
#include <linux/pinctrl/consumer.h>
2424
#include <linux/pm_runtime.h>
25+
#include <linux/iopoll.h>
2526
#include <trace/events/spi.h>
2627

2728
/* SPI register offsets */
@@ -233,9 +234,6 @@
233234
*/
234235
#define DMA_MIN_BYTES 16
235236

236-
#define SPI_DMA_MIN_TIMEOUT (msecs_to_jiffies(1000))
237-
#define SPI_DMA_TIMEOUT_PER_10K (msecs_to_jiffies(4))
238-
239237
#define AUTOSUSPEND_TIMEOUT 2000
240238

241239
struct atmel_spi_caps {
@@ -279,6 +277,7 @@ struct atmel_spi {
279277
bool keep_cs;
280278

281279
u32 fifo_size;
280+
bool last_polarity;
282281
u8 native_cs_free;
283282
u8 native_cs_for_gpio;
284283
};
@@ -291,6 +290,22 @@ struct atmel_spi_device {
291290
#define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */
292291
#define INVALID_DMA_ADDRESS 0xffffffff
293292

293+
/*
294+
* This frequency can be anything supported by the controller, but to avoid
295+
* unnecessary delay, the highest possible frequency is chosen.
296+
*
297+
* This frequency is the highest possible which is not interfering with other
298+
* chip select registers (see Note for Serial Clock Bit Rate configuration in
299+
* Atmel-11121F-ATARM-SAMA5D3-Series-Datasheet_02-Feb-16, page 1283)
300+
*/
301+
#define DUMMY_MSG_FREQUENCY 0x02
302+
/*
303+
* 8 bits is the minimum data the controller is capable of sending.
304+
*
305+
* This message can be anything as it should not be treated by any SPI device.
306+
*/
307+
#define DUMMY_MSG 0xAA
308+
294309
/*
295310
* Version 2 of the SPI controller has
296311
* - CR.LASTXFER
@@ -304,6 +319,43 @@ static bool atmel_spi_is_v2(struct atmel_spi *as)
304319
return as->caps.is_spi2;
305320
}
306321

322+
/*
323+
* Send a dummy message.
324+
*
325+
* This is sometimes needed when using a CS GPIO to force clock transition when
326+
* switching between devices with different polarities.
327+
*/
328+
static void atmel_spi_send_dummy(struct atmel_spi *as, struct spi_device *spi, int chip_select)
329+
{
330+
u32 status;
331+
u32 csr;
332+
333+
/*
334+
* Set a clock frequency to allow sending message on SPI bus.
335+
* The frequency here can be anything, but is needed for
336+
* the controller to send the data.
337+
*/
338+
csr = spi_readl(as, CSR0 + 4 * chip_select);
339+
csr = SPI_BFINS(SCBR, DUMMY_MSG_FREQUENCY, csr);
340+
spi_writel(as, CSR0 + 4 * chip_select, csr);
341+
342+
/*
343+
* Read all data coming from SPI bus, needed to be able to send
344+
* the message.
345+
*/
346+
spi_readl(as, RDR);
347+
while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
348+
spi_readl(as, RDR);
349+
cpu_relax();
350+
}
351+
352+
spi_writel(as, TDR, DUMMY_MSG);
353+
354+
readl_poll_timeout_atomic(as->regs + SPI_SR, status,
355+
(status & SPI_BIT(TXEMPTY)), 1, 1000);
356+
}
357+
358+
307359
/*
308360
* Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
309361
* they assume that spi slave device state will not change on deselect, so
@@ -320,11 +372,17 @@ static bool atmel_spi_is_v2(struct atmel_spi *as)
320372
* Master on Chip Select 0.") No workaround exists for that ... so for
321373
* nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
322374
* and (c) will trigger that first erratum in some cases.
375+
*
376+
* When changing the clock polarity, the SPI controller waits for the next
377+
* transmission to enforce the default clock state. This may be an issue when
378+
* using a GPIO as Chip Select: the clock level is applied only when the first
379+
* packet is sent, once the CS has already been asserted. The workaround is to
380+
* avoid this by sending a first (dummy) message before toggling the CS state.
323381
*/
324-
325382
static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
326383
{
327384
struct atmel_spi_device *asd = spi->controller_state;
385+
bool new_polarity;
328386
int chip_select;
329387
u32 mr;
330388

@@ -353,6 +411,25 @@ static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
353411
}
354412

355413
mr = spi_readl(as, MR);
414+
415+
/*
416+
* Ensures the clock polarity is valid before we actually
417+
* assert the CS to avoid spurious clock edges to be
418+
* processed by the spi devices.
419+
*/
420+
if (spi_get_csgpiod(spi, 0)) {
421+
new_polarity = (asd->csr & SPI_BIT(CPOL)) != 0;
422+
if (new_polarity != as->last_polarity) {
423+
/*
424+
* Need to disable the GPIO before sending the dummy
425+
* message because it is already set by the spi core.
426+
*/
427+
gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 0);
428+
atmel_spi_send_dummy(as, spi, chip_select);
429+
as->last_polarity = new_polarity;
430+
gpiod_set_value_cansleep(spi_get_csgpiod(spi, 0), 1);
431+
}
432+
}
356433
} else {
357434
u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
358435
int i;
@@ -1336,12 +1413,10 @@ static int atmel_spi_one_transfer(struct spi_controller *host,
13361413
}
13371414

13381415
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;
1416+
ret_timeout = wait_for_completion_timeout(&as->xfer_completion, dma_timeout);
1417+
if (!ret_timeout) {
1418+
dev_err(&spi->dev, "spi transfer timeout\n");
1419+
as->done_status = -EIO;
13451420
}
13461421

13471422
if (as->done_status)

drivers/spi/spi-cadence.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ static int cdns_transfer_one(struct spi_controller *ctlr,
451451
udelay(10);
452452

453453
cdns_spi_process_fifo(xspi, xspi->tx_fifo_depth, 0);
454-
spi_transfer_delay_exec(transfer);
455454

456455
cdns_spi_write(xspi, CDNS_SPI_IER, CDNS_SPI_IXR_DEFAULT);
457456
return transfer->len;

drivers/spi/spi-imx.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,18 @@ static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
659659
ctrl |= (spi_imx->target_burst * 8 - 1)
660660
<< MX51_ECSPI_CTRL_BL_OFFSET;
661661
else {
662-
if (spi_imx->count >= 512)
663-
ctrl |= 0xFFF << MX51_ECSPI_CTRL_BL_OFFSET;
664-
else
665-
ctrl |= (spi_imx->count * spi_imx->bits_per_word - 1)
662+
if (spi_imx->usedma) {
663+
ctrl |= (spi_imx->bits_per_word *
664+
spi_imx_bytes_per_word(spi_imx->bits_per_word) - 1)
666665
<< MX51_ECSPI_CTRL_BL_OFFSET;
666+
} else {
667+
if (spi_imx->count >= MX51_ECSPI_CTRL_MAX_BURST)
668+
ctrl |= (MX51_ECSPI_CTRL_MAX_BURST - 1)
669+
<< MX51_ECSPI_CTRL_BL_OFFSET;
670+
else
671+
ctrl |= (spi_imx->count * spi_imx->bits_per_word - 1)
672+
<< MX51_ECSPI_CTRL_BL_OFFSET;
673+
}
667674
}
668675

669676
/* set clock speed */

0 commit comments

Comments
 (0)