Commit e09d0d22 authored by Steve Wilkins's avatar Steve Wilkins Committed by ZhangPeng
Browse files

spi: microchip-core: defer asserting chip select until just before write to TX FIFO

stable inclusion
from stable-v6.6.44
commit 1dc6d9fd4828fdbb8585ce2567e1ad02d3a64448
bugzilla: https://gitee.com/openeuler/kernel/issues/IAHMJO

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=1dc6d9fd4828fdbb8585ce2567e1ad02d3a64448



--------------------------------

[ Upstream commit 22fd98c107c792e35db7abe45298bc3a29bf4723 ]

Setting up many of the registers for a new SPI transfer requires the
SPI controller to be disabled after set_cs() has been called to assert
the chip select line. However, disabling the controller results in the
SCLK and MOSI output pins being tristate, which can cause clock
transitions to be seen by a slave device whilst SS is active. To fix
this, the CS is only set to inactive inline, whilst setting it active
is deferred until all registers are set up and the any controller
disables have been completed.

Fixes: 9ac8d176 ("spi: add support for microchip fpga spi controllers")
Signed-off-by: default avatarSteve Wilkins <steve.wilkins@raymarine.com>
Signed-off-by: default avatarConor Dooley <conor.dooley@microchip.com>
Link: https://patch.msgid.link/20240715-sanitizer-recant-dd96b7a97048@wendy


Signed-off-by: default avatarMark Brown <broonie@kernel.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarZhangPeng <zhangpeng362@huawei.com>
parent 228c8f0d
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ struct mchp_corespi {
	u8 *rx_buf;
	u32 clk_gen; /* divider for spi output clock generated by the controller */
	u32 clk_mode;
	u32 pending_slave_select;
	int irq;
	int tx_len;
	int rx_len;
@@ -249,7 +250,17 @@ static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
	reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
	reg &= ~BIT(spi_get_chipselect(spi, 0));
	reg |= !disable << spi_get_chipselect(spi, 0);
	corespi->pending_slave_select = reg;

	/*
	 * Only deassert chip select immediately. Writing to some registers
	 * requires the controller to be disabled, which results in the
	 * output pins being tristated and can cause the SCLK and MOSI lines
	 * to transition. Therefore asserting the chip select is deferred
	 * until just before writing to the TX FIFO, to ensure the device
	 * doesn't see any spurious clock transitions whilst CS is enabled.
	 */
	if (((spi->mode & SPI_CS_HIGH) == 0) == disable)
		mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
}

@@ -266,6 +277,7 @@ static int mchp_corespi_setup(struct spi_device *spi)
	if (spi->mode & SPI_CS_HIGH) {
		reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
		reg |= BIT(spi_get_chipselect(spi, 0));
		corespi->pending_slave_select = reg;
		mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
	}
	return 0;
@@ -307,7 +319,8 @@ static void mchp_corespi_init(struct spi_master *master, struct mchp_corespi *sp
	 * select is relinquished to the hardware. SSELOUT is enabled too so we
	 * can deal with active high slaves.
	 */
	mchp_corespi_write(spi, REG_SLAVE_SELECT, SSELOUT | SSEL_DIRECT);
	spi->pending_slave_select = SSELOUT | SSEL_DIRECT;
	mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select);

	control = mchp_corespi_read(spi, REG_CONTROL);

@@ -476,6 +489,8 @@ static int mchp_corespi_transfer_one(struct spi_master *master,
	mchp_corespi_set_xfer_size(spi, (spi->tx_len > FIFO_DEPTH)
				   ? FIFO_DEPTH : spi->tx_len);

	mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select);

	while (spi->tx_len)
		mchp_corespi_write_fifo(spi);