Skip to content

Commit a957499

Browse files
vladimirolteanbroonie
authored andcommitted
spi: spi-fsl-dspi: Fix bits-per-word acceleration in DMA mode
In DMA mode, dspi_setup_accel does not get called, which results in the dspi->oper_word_size variable (which is used by dspi_dma_xfer) to not be initialized properly. Because oper_word_size is zero, a few calculations end up being incorrect, and the DMA transfer eventually times out instead of sending anything on the wire. Set up native transfers (or 8-on-16 acceleration) using dspi_setup_accel for DMA mode too. Also take the opportunity and simplify the DMA buffer handling a little bit. Fixes: 6c1c26e ("spi: spi-fsl-dspi: Accelerate transfers using larger word size if possible") Signed-off-by: Vladimir Oltean <[email protected]> Tested-by: Michael Walle <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 671ffde commit a957499

File tree

1 file changed

+32
-54
lines changed

1 file changed

+32
-54
lines changed

drivers/spi/spi-fsl-dspi.c

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ struct fsl_dspi_devtype_data {
119119
enum dspi_trans_mode trans_mode;
120120
u8 max_clock_factor;
121121
int fifo_size;
122-
int dma_bufsize;
123122
};
124123

125124
enum {
@@ -138,7 +137,6 @@ static const struct fsl_dspi_devtype_data devtype_data[] = {
138137
[VF610] = {
139138
.trans_mode = DSPI_DMA_MODE,
140139
.max_clock_factor = 2,
141-
.dma_bufsize = 4096,
142140
.fifo_size = 4,
143141
},
144142
[LS1021A] = {
@@ -167,19 +165,16 @@ static const struct fsl_dspi_devtype_data devtype_data[] = {
167165
},
168166
[LS2080A] = {
169167
.trans_mode = DSPI_DMA_MODE,
170-
.dma_bufsize = 8,
171168
.max_clock_factor = 8,
172169
.fifo_size = 4,
173170
},
174171
[LS2085A] = {
175172
.trans_mode = DSPI_DMA_MODE,
176-
.dma_bufsize = 8,
177173
.max_clock_factor = 8,
178174
.fifo_size = 4,
179175
},
180176
[LX2160A] = {
181177
.trans_mode = DSPI_DMA_MODE,
182-
.dma_bufsize = 8,
183178
.max_clock_factor = 8,
184179
.fifo_size = 4,
185180
},
@@ -191,9 +186,6 @@ static const struct fsl_dspi_devtype_data devtype_data[] = {
191186
};
192187

193188
struct fsl_dspi_dma {
194-
/* Length of transfer in words of dspi->fifo_size */
195-
u32 curr_xfer_len;
196-
197189
u32 *tx_dma_buf;
198190
struct dma_chan *chan_tx;
199191
dma_addr_t tx_dma_phys;
@@ -352,7 +344,7 @@ static void dspi_rx_dma_callback(void *arg)
352344
int i;
353345

354346
if (dspi->rx) {
355-
for (i = 0; i < dma->curr_xfer_len; i++)
347+
for (i = 0; i < dspi->words_in_flight; i++)
356348
dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
357349
}
358350

@@ -366,12 +358,12 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
366358
int time_left;
367359
int i;
368360

369-
for (i = 0; i < dma->curr_xfer_len; i++)
361+
for (i = 0; i < dspi->words_in_flight; i++)
370362
dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
371363

372364
dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
373365
dma->tx_dma_phys,
374-
dma->curr_xfer_len *
366+
dspi->words_in_flight *
375367
DMA_SLAVE_BUSWIDTH_4_BYTES,
376368
DMA_MEM_TO_DEV,
377369
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
@@ -389,7 +381,7 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
389381

390382
dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
391383
dma->rx_dma_phys,
392-
dma->curr_xfer_len *
384+
dspi->words_in_flight *
393385
DMA_SLAVE_BUSWIDTH_4_BYTES,
394386
DMA_DEV_TO_MEM,
395387
DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
@@ -437,46 +429,42 @@ static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
437429
return 0;
438430
}
439431

432+
static void dspi_setup_accel(struct fsl_dspi *dspi);
433+
440434
static int dspi_dma_xfer(struct fsl_dspi *dspi)
441435
{
442436
struct spi_message *message = dspi->cur_msg;
443437
struct device *dev = &dspi->pdev->dev;
444-
struct fsl_dspi_dma *dma = dspi->dma;
445-
int curr_remaining_bytes;
446-
int bytes_per_buffer;
447438
int ret = 0;
448439

449-
curr_remaining_bytes = dspi->len;
450-
bytes_per_buffer = dspi->devtype_data->dma_bufsize /
451-
dspi->devtype_data->fifo_size;
452-
while (curr_remaining_bytes) {
453-
/* Check if current transfer fits the DMA buffer */
454-
dma->curr_xfer_len = curr_remaining_bytes /
455-
dspi->oper_word_size;
456-
if (dma->curr_xfer_len > bytes_per_buffer)
457-
dma->curr_xfer_len = bytes_per_buffer;
440+
/*
441+
* dspi->len gets decremented by dspi_pop_tx_pushr in
442+
* dspi_next_xfer_dma_submit
443+
*/
444+
while (dspi->len) {
445+
/* Figure out operational bits-per-word for this chunk */
446+
dspi_setup_accel(dspi);
447+
448+
dspi->words_in_flight = dspi->len / dspi->oper_word_size;
449+
if (dspi->words_in_flight > dspi->devtype_data->fifo_size)
450+
dspi->words_in_flight = dspi->devtype_data->fifo_size;
451+
452+
message->actual_length += dspi->words_in_flight *
453+
dspi->oper_word_size;
458454

459455
ret = dspi_next_xfer_dma_submit(dspi);
460456
if (ret) {
461457
dev_err(dev, "DMA transfer failed\n");
462-
goto exit;
463-
464-
} else {
465-
const int len = dma->curr_xfer_len *
466-
dspi->oper_word_size;
467-
curr_remaining_bytes -= len;
468-
message->actual_length += len;
469-
if (curr_remaining_bytes < 0)
470-
curr_remaining_bytes = 0;
458+
break;
471459
}
472460
}
473461

474-
exit:
475462
return ret;
476463
}
477464

478465
static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
479466
{
467+
int dma_bufsize = dspi->devtype_data->fifo_size * 2;
480468
struct device *dev = &dspi->pdev->dev;
481469
struct dma_slave_config cfg;
482470
struct fsl_dspi_dma *dma;
@@ -501,16 +489,16 @@ static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
501489
}
502490

503491
dma->tx_dma_buf = dma_alloc_coherent(dma->chan_tx->device->dev,
504-
dspi->devtype_data->dma_bufsize,
505-
&dma->tx_dma_phys, GFP_KERNEL);
492+
dma_bufsize, &dma->tx_dma_phys,
493+
GFP_KERNEL);
506494
if (!dma->tx_dma_buf) {
507495
ret = -ENOMEM;
508496
goto err_tx_dma_buf;
509497
}
510498

511499
dma->rx_dma_buf = dma_alloc_coherent(dma->chan_rx->device->dev,
512-
dspi->devtype_data->dma_bufsize,
513-
&dma->rx_dma_phys, GFP_KERNEL);
500+
dma_bufsize, &dma->rx_dma_phys,
501+
GFP_KERNEL);
514502
if (!dma->rx_dma_buf) {
515503
ret = -ENOMEM;
516504
goto err_rx_dma_buf;
@@ -547,12 +535,10 @@ static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
547535

548536
err_slave_config:
549537
dma_free_coherent(dma->chan_rx->device->dev,
550-
dspi->devtype_data->dma_bufsize,
551-
dma->rx_dma_buf, dma->rx_dma_phys);
538+
dma_bufsize, dma->rx_dma_buf, dma->rx_dma_phys);
552539
err_rx_dma_buf:
553540
dma_free_coherent(dma->chan_tx->device->dev,
554-
dspi->devtype_data->dma_bufsize,
555-
dma->tx_dma_buf, dma->tx_dma_phys);
541+
dma_bufsize, dma->tx_dma_buf, dma->tx_dma_phys);
556542
err_tx_dma_buf:
557543
dma_release_channel(dma->chan_tx);
558544
err_tx_channel:
@@ -566,22 +552,21 @@ static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
566552

567553
static void dspi_release_dma(struct fsl_dspi *dspi)
568554
{
555+
int dma_bufsize = dspi->devtype_data->fifo_size * 2;
569556
struct fsl_dspi_dma *dma = dspi->dma;
570557

571558
if (!dma)
572559
return;
573560

574561
if (dma->chan_tx) {
575562
dma_unmap_single(dma->chan_tx->device->dev, dma->tx_dma_phys,
576-
dspi->devtype_data->dma_bufsize,
577-
DMA_TO_DEVICE);
563+
dma_bufsize, DMA_TO_DEVICE);
578564
dma_release_channel(dma->chan_tx);
579565
}
580566

581567
if (dma->chan_rx) {
582568
dma_unmap_single(dma->chan_rx->device->dev, dma->rx_dma_phys,
583-
dspi->devtype_data->dma_bufsize,
584-
DMA_FROM_DEVICE);
569+
dma_bufsize, DMA_FROM_DEVICE);
585570
dma_release_channel(dma->chan_rx);
586571
}
587572
}
@@ -833,7 +818,7 @@ static void dspi_setup_accel(struct fsl_dspi *dspi)
833818
dspi->oper_word_size = DIV_ROUND_UP(dspi->oper_bits_per_word, 8);
834819

835820
/*
836-
* Update CTAR here (code is common for both EOQ and XSPI modes).
821+
* Update CTAR here (code is common for EOQ, XSPI and DMA modes).
837822
* We will update CTARE in the portion specific to XSPI, when we
838823
* also know the preload value (DTCP).
839824
*/
@@ -960,13 +945,6 @@ static int dspi_transfer_one_message(struct spi_controller *ctlr,
960945
regmap_update_bits(dspi->regmap, SPI_MCR,
961946
SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
962947
SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
963-
/*
964-
* Static CTAR setup for modes that don't dynamically adjust it
965-
* via dspi_setup_accel (aka for DMA)
966-
*/
967-
regmap_write(dspi->regmap, SPI_CTAR(0),
968-
dspi->cur_chip->ctar_val |
969-
SPI_FRAME_BITS(transfer->bits_per_word));
970948

971949
spi_take_timestamp_pre(dspi->ctlr, dspi->cur_transfer,
972950
dspi->progress, !dspi->irq);

0 commit comments

Comments
 (0)