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

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.ru


Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent 3ff60c6b
Loading
Loading
Loading
Loading
+17 −20
Original line number Diff line number Diff line
@@ -108,9 +108,8 @@ EXPORT_SYMBOL_GPL(dw_spi_set_cs);
/* Return the max entries we can fill into tx fifo */
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);

	/*
@@ -121,18 +120,15 @@ static inline u32 tx_max(struct dw_spi *dws)
	 * shift registers. So a control from sw point of
	 * view is taken.
	 */
	rxtx_gap =  ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
			/ dws->n_bytes;
	rxtx_gap = dws->fifo_len - (dws->rx_len - dws->tx_len);

	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 */
static inline u32 rx_max(struct dw_spi *dws)
{
	u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;

	return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
	return min_t(u32, dws->rx_len, dw_readl(dws, DW_SPI_RXFLR));
}

static void dw_writer(struct dw_spi *dws)
@@ -141,15 +137,16 @@ static void dw_writer(struct dw_spi *dws)
	u16 txw = 0;

	while (max--) {
		/* Set the tx word if the transfer's original "tx" is not null */
		if (dws->tx_end - dws->len) {
		if (dws->tx) {
			if (dws->n_bytes == 1)
				txw = *(u8 *)(dws->tx);
			else
				txw = *(u16 *)(dws->tx);

			dws->tx += dws->n_bytes;
		}
		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)

	while (max--) {
		rxw = dw_read_io_reg(dws, DW_SPI_DR);
		/* Care rx only if the transfer's original "rx" is not null */
		if (dws->rx_end - dws->len) {
		if (dws->rx) {
			if (dws->n_bytes == 1)
				*(u8 *)(dws->rx) = rxw;
			else
				*(u16 *)(dws->rx) = rxw;
		}

			dws->rx += dws->n_bytes;
		}
		--dws->rx_len;
	}
}

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)
	}

	dw_reader(dws);
	if (dws->rx_end == dws->rx) {
	if (!dws->rx_len) {
		spi_mask_intr(dws, 0xff);
		spi_finalize_current_transfer(dws->master);
		return IRQ_HANDLED;
@@ -320,12 +318,11 @@ static int dw_spi_transfer_one(struct spi_controller *master,
	dws->dma_mapped = 0;
	dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
	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_end = dws->rx + transfer->len;
	dws->len = transfer->len;
	dws->rx_len = dws->tx_len;

	/* Ensure dw->rx and dw->rx_end are visible */
	/* Ensure the data above is visible for all CPUs */
	smp_mb();

	spi_enable_chip(dws, 0);
@@ -352,7 +349,7 @@ static int dw_spi_transfer_one(struct spi_controller *master,
			return ret;
		}
	} 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);

		/* Set the interrupt mask */
+2 −3
Original line number Diff line number Diff line
@@ -147,11 +147,10 @@ struct dw_spi {
	void (*set_cs)(struct spi_device *spi, bool enable);

	/* Current message transfer state info */
	size_t			len;
	void			*tx;
	void			*tx_end;
	unsigned int		tx_len;
	void			*rx;
	void			*rx_end;
	unsigned int		rx_len;
	int			dma_mapped;
	u8			n_bytes;	/* current is a 1/2 bytes op */
	irqreturn_t		(*transfer_handler)(struct dw_spi *dws);