Commit 8dedbeac authored by Serge Semin's avatar Serge Semin Committed by Mark Brown

spi: dw: Refactor data IO procedure

The Tx and Rx data write/read procedure can be significantly simplified by
using Tx/Rx transfer lengths instead of the end pointers. By having the
Tx/Rx data leftover lengths (in the number of transfer words) we can get
rid of all subtraction and division operations utilized here and there in
the tx_max(), rx_max(), dw_writer() and dw_reader() methods. Such
modification will not only give us the more optimized IO procedures, but
will make the data IO methods much more readable than before.
Signed-off-by: default avatarSerge Semin <Sergey.Semin@baikalelectronics.ru>
Link: https://lore.kernel.org/r/20201007235511.4935-9-Sergey.Semin@baikalelectronics.ruSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent 3ff60c6b
...@@ -108,9 +108,8 @@ EXPORT_SYMBOL_GPL(dw_spi_set_cs); ...@@ -108,9 +108,8 @@ EXPORT_SYMBOL_GPL(dw_spi_set_cs);
/* Return the max entries we can fill into tx fifo */ /* Return the max entries we can fill into tx fifo */
static inline u32 tx_max(struct dw_spi *dws) static inline u32 tx_max(struct dw_spi *dws)
{ {
u32 tx_left, tx_room, rxtx_gap; u32 tx_room, rxtx_gap;
tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR); tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
/* /*
...@@ -121,18 +120,15 @@ static inline u32 tx_max(struct dw_spi *dws) ...@@ -121,18 +120,15 @@ static inline u32 tx_max(struct dw_spi *dws)
* shift registers. So a control from sw point of * shift registers. So a control from sw point of
* view is taken. * view is taken.
*/ */
rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx)) rxtx_gap = dws->fifo_len - (dws->rx_len - dws->tx_len);
/ dws->n_bytes;
return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap)); return min3((u32)dws->tx_len, tx_room, rxtx_gap);
} }
/* Return the max entries we should read out of rx fifo */ /* Return the max entries we should read out of rx fifo */
static inline u32 rx_max(struct dw_spi *dws) static inline u32 rx_max(struct dw_spi *dws)
{ {
u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes; return min_t(u32, dws->rx_len, dw_readl(dws, DW_SPI_RXFLR));
return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
} }
static void dw_writer(struct dw_spi *dws) static void dw_writer(struct dw_spi *dws)
...@@ -141,15 +137,16 @@ static void dw_writer(struct dw_spi *dws) ...@@ -141,15 +137,16 @@ static void dw_writer(struct dw_spi *dws)
u16 txw = 0; u16 txw = 0;
while (max--) { while (max--) {
/* Set the tx word if the transfer's original "tx" is not null */ if (dws->tx) {
if (dws->tx_end - dws->len) {
if (dws->n_bytes == 1) if (dws->n_bytes == 1)
txw = *(u8 *)(dws->tx); txw = *(u8 *)(dws->tx);
else else
txw = *(u16 *)(dws->tx); txw = *(u16 *)(dws->tx);
dws->tx += dws->n_bytes;
} }
dw_write_io_reg(dws, DW_SPI_DR, txw); dw_write_io_reg(dws, DW_SPI_DR, txw);
dws->tx += dws->n_bytes; --dws->tx_len;
} }
} }
...@@ -160,15 +157,16 @@ static void dw_reader(struct dw_spi *dws) ...@@ -160,15 +157,16 @@ static void dw_reader(struct dw_spi *dws)
while (max--) { while (max--) {
rxw = dw_read_io_reg(dws, DW_SPI_DR); rxw = dw_read_io_reg(dws, DW_SPI_DR);
/* Care rx only if the transfer's original "rx" is not null */ if (dws->rx) {
if (dws->rx_end - dws->len) {
if (dws->n_bytes == 1) if (dws->n_bytes == 1)
*(u8 *)(dws->rx) = rxw; *(u8 *)(dws->rx) = rxw;
else else
*(u16 *)(dws->rx) = rxw; *(u16 *)(dws->rx) = rxw;
}
dws->rx += dws->n_bytes; dws->rx += dws->n_bytes;
} }
--dws->rx_len;
}
} }
static void int_error_stop(struct dw_spi *dws, const char *msg) static void int_error_stop(struct dw_spi *dws, const char *msg)
...@@ -192,7 +190,7 @@ static irqreturn_t interrupt_transfer(struct dw_spi *dws) ...@@ -192,7 +190,7 @@ static irqreturn_t interrupt_transfer(struct dw_spi *dws)
} }
dw_reader(dws); dw_reader(dws);
if (dws->rx_end == dws->rx) { if (!dws->rx_len) {
spi_mask_intr(dws, 0xff); spi_mask_intr(dws, 0xff);
spi_finalize_current_transfer(dws->master); spi_finalize_current_transfer(dws->master);
return IRQ_HANDLED; return IRQ_HANDLED;
...@@ -320,12 +318,11 @@ static int dw_spi_transfer_one(struct spi_controller *master, ...@@ -320,12 +318,11 @@ static int dw_spi_transfer_one(struct spi_controller *master,
dws->dma_mapped = 0; dws->dma_mapped = 0;
dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE); dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
dws->tx = (void *)transfer->tx_buf; dws->tx = (void *)transfer->tx_buf;
dws->tx_end = dws->tx + transfer->len; dws->tx_len = transfer->len / dws->n_bytes;
dws->rx = transfer->rx_buf; dws->rx = transfer->rx_buf;
dws->rx_end = dws->rx + transfer->len; dws->rx_len = dws->tx_len;
dws->len = transfer->len;
/* Ensure dw->rx and dw->rx_end are visible */ /* Ensure the data above is visible for all CPUs */
smp_mb(); smp_mb();
spi_enable_chip(dws, 0); spi_enable_chip(dws, 0);
...@@ -352,7 +349,7 @@ static int dw_spi_transfer_one(struct spi_controller *master, ...@@ -352,7 +349,7 @@ static int dw_spi_transfer_one(struct spi_controller *master,
return ret; return ret;
} }
} else { } else {
txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes); txlevel = min_t(u16, dws->fifo_len / 2, dws->tx_len);
dw_writel(dws, DW_SPI_TXFTLR, txlevel); dw_writel(dws, DW_SPI_TXFTLR, txlevel);
/* Set the interrupt mask */ /* Set the interrupt mask */
......
...@@ -147,11 +147,10 @@ struct dw_spi { ...@@ -147,11 +147,10 @@ struct dw_spi {
void (*set_cs)(struct spi_device *spi, bool enable); void (*set_cs)(struct spi_device *spi, bool enable);
/* Current message transfer state info */ /* Current message transfer state info */
size_t len;
void *tx; void *tx;
void *tx_end; unsigned int tx_len;
void *rx; void *rx;
void *rx_end; unsigned int rx_len;
int dma_mapped; int dma_mapped;
u8 n_bytes; /* current is a 1/2 bytes op */ u8 n_bytes; /* current is a 1/2 bytes op */
irqreturn_t (*transfer_handler)(struct dw_spi *dws); irqreturn_t (*transfer_handler)(struct dw_spi *dws);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment