Skip to content

Commit 869f2c9

Browse files
Jon Linbroonie
authored andcommitted
spi: rockchip: Stop spi slave dma receiver when cs inactive
The spi which's version is higher than ver 2 will automatically enable this feature. If the length of master transmission is uncertain, the RK spi slave is better to automatically stop after cs inactive instead of waiting for xfer_completion forever. Signed-off-by: Jon Lin <jon.lin@rock-chips.com> Link: https://lore.kernel.org/r/20220216014028.8123-4-jon.lin@rock-chips.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 8080876 commit 869f2c9

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

drivers/spi/spi-rockchip.c

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@
133133
#define INT_TF_OVERFLOW (1 << 1)
134134
#define INT_RF_UNDERFLOW (1 << 2)
135135
#define INT_RF_OVERFLOW (1 << 3)
136-
#define INT_RF_FULL (1 << 4)
136+
#define INT_RF_FULL (1 << 4)
137+
#define INT_CS_INACTIVE (1 << 6)
137138

138139
/* Bit fields in ICR, 4bit */
139140
#define ICR_MASK 0x0f
@@ -194,6 +195,8 @@ struct rockchip_spi {
194195
bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];
195196

196197
bool slave_abort;
198+
bool cs_inactive; /* spi slave tansmition stop when cs inactive */
199+
struct spi_transfer *xfer; /* Store xfer temporarily */
197200
};
198201

199202
static inline void spi_enable_chip(struct rockchip_spi *rs, bool enable)
@@ -343,28 +346,42 @@ static irqreturn_t rockchip_spi_isr(int irq, void *dev_id)
343346
struct spi_controller *ctlr = dev_id;
344347
struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
345348

349+
/* When int_cs_inactive comes, spi slave abort */
350+
if (rs->cs_inactive && readl_relaxed(rs->regs + ROCKCHIP_SPI_IMR) & INT_CS_INACTIVE) {
351+
ctlr->slave_abort(ctlr);
352+
writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
353+
writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
354+
355+
return IRQ_HANDLED;
356+
}
357+
346358
if (rs->tx_left)
347359
rockchip_spi_pio_writer(rs);
348360

349361
rockchip_spi_pio_reader(rs);
350362
if (!rs->rx_left) {
351363
spi_enable_chip(rs, false);
352364
writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
365+
writel_relaxed(0xffffffff, rs->regs + ROCKCHIP_SPI_ICR);
353366
spi_finalize_current_transfer(ctlr);
354367
}
355368

356369
return IRQ_HANDLED;
357370
}
358371

359372
static int rockchip_spi_prepare_irq(struct rockchip_spi *rs,
360-
struct spi_transfer *xfer)
373+
struct spi_controller *ctlr,
374+
struct spi_transfer *xfer)
361375
{
362376
rs->tx = xfer->tx_buf;
363377
rs->rx = xfer->rx_buf;
364378
rs->tx_left = rs->tx ? xfer->len / rs->n_bytes : 0;
365379
rs->rx_left = xfer->len / rs->n_bytes;
366380

367-
writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
381+
if (rs->cs_inactive)
382+
writel_relaxed(INT_RF_FULL | INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
383+
else
384+
writel_relaxed(INT_RF_FULL, rs->regs + ROCKCHIP_SPI_IMR);
368385
spi_enable_chip(rs, true);
369386

370387
if (rs->tx_left)
@@ -383,6 +400,9 @@ static void rockchip_spi_dma_rxcb(void *data)
383400
if (state & TXDMA && !rs->slave_abort)
384401
return;
385402

403+
if (rs->cs_inactive)
404+
writel_relaxed(0, rs->regs + ROCKCHIP_SPI_IMR);
405+
386406
spi_enable_chip(rs, false);
387407
spi_finalize_current_transfer(ctlr);
388408
}
@@ -423,14 +443,16 @@ static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
423443

424444
atomic_set(&rs->state, 0);
425445

446+
rs->tx = xfer->tx_buf;
447+
rs->rx = xfer->rx_buf;
448+
426449
rxdesc = NULL;
427450
if (xfer->rx_buf) {
428451
struct dma_slave_config rxconf = {
429452
.direction = DMA_DEV_TO_MEM,
430453
.src_addr = rs->dma_addr_rx,
431454
.src_addr_width = rs->n_bytes,
432-
.src_maxburst = rockchip_spi_calc_burst_size(xfer->len /
433-
rs->n_bytes),
455+
.src_maxburst = rockchip_spi_calc_burst_size(xfer->len / rs->n_bytes),
434456
};
435457

436458
dmaengine_slave_config(ctlr->dma_rx, &rxconf);
@@ -474,10 +496,13 @@ static int rockchip_spi_prepare_dma(struct rockchip_spi *rs,
474496
/* rx must be started before tx due to spi instinct */
475497
if (rxdesc) {
476498
atomic_or(RXDMA, &rs->state);
477-
dmaengine_submit(rxdesc);
499+
ctlr->dma_rx->cookie = dmaengine_submit(rxdesc);
478500
dma_async_issue_pending(ctlr->dma_rx);
479501
}
480502

503+
if (rs->cs_inactive)
504+
writel_relaxed(INT_CS_INACTIVE, rs->regs + ROCKCHIP_SPI_IMR);
505+
481506
spi_enable_chip(rs, true);
482507

483508
if (txdesc) {
@@ -584,7 +609,42 @@ static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
584609
static int rockchip_spi_slave_abort(struct spi_controller *ctlr)
585610
{
586611
struct rockchip_spi *rs = spi_controller_get_devdata(ctlr);
612+
u32 rx_fifo_left;
613+
struct dma_tx_state state;
614+
enum dma_status status;
615+
616+
/* Get current dma rx point */
617+
if (atomic_read(&rs->state) & RXDMA) {
618+
dmaengine_pause(ctlr->dma_rx);
619+
status = dmaengine_tx_status(ctlr->dma_rx, ctlr->dma_rx->cookie, &state);
620+
if (status == DMA_ERROR) {
621+
rs->rx = rs->xfer->rx_buf;
622+
rs->xfer->len = 0;
623+
rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
624+
for (; rx_fifo_left; rx_fifo_left--)
625+
readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
626+
goto out;
627+
} else {
628+
rs->rx += rs->xfer->len - rs->n_bytes * state.residue;
629+
}
630+
}
587631

632+
/* Get the valid data left in rx fifo and set rs->xfer->len real rx size */
633+
if (rs->rx) {
634+
rx_fifo_left = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
635+
for (; rx_fifo_left; rx_fifo_left--) {
636+
u32 rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
637+
638+
if (rs->n_bytes == 1)
639+
*(u8 *)rs->rx = (u8)rxw;
640+
else
641+
*(u16 *)rs->rx = (u16)rxw;
642+
rs->rx += rs->n_bytes;
643+
}
644+
rs->xfer->len = (unsigned int)(rs->rx - rs->xfer->rx_buf);
645+
}
646+
647+
out:
588648
if (atomic_read(&rs->state) & RXDMA)
589649
dmaengine_terminate_sync(ctlr->dma_rx);
590650
if (atomic_read(&rs->state) & TXDMA)
@@ -626,7 +686,7 @@ static int rockchip_spi_transfer_one(
626686
}
627687

628688
rs->n_bytes = xfer->bits_per_word <= 8 ? 1 : 2;
629-
689+
rs->xfer = xfer;
630690
use_dma = ctlr->can_dma ? ctlr->can_dma(ctlr, spi, xfer) : false;
631691

632692
ret = rockchip_spi_config(rs, spi, xfer, use_dma, ctlr->slave);
@@ -636,7 +696,7 @@ static int rockchip_spi_transfer_one(
636696
if (use_dma)
637697
return rockchip_spi_prepare_dma(rs, ctlr, xfer);
638698

639-
return rockchip_spi_prepare_irq(rs, xfer);
699+
return rockchip_spi_prepare_irq(rs, ctlr, xfer);
640700
}
641701

642702
static bool rockchip_spi_can_dma(struct spi_controller *ctlr,
@@ -815,8 +875,13 @@ static int rockchip_spi_probe(struct platform_device *pdev)
815875
switch (readl_relaxed(rs->regs + ROCKCHIP_SPI_VERSION)) {
816876
case ROCKCHIP_SPI_VER2_TYPE2:
817877
ctlr->mode_bits |= SPI_CS_HIGH;
878+
if (ctlr->can_dma && slave_mode)
879+
rs->cs_inactive = true;
880+
else
881+
rs->cs_inactive = false;
818882
break;
819883
default:
884+
rs->cs_inactive = false;
820885
break;
821886
}
822887

0 commit comments

Comments
 (0)