Skip to contentSkip to conversation

spi: soft_spi: Add support for SPI_3WIRE

PATCH In review Hironori KIKUCHI 2 participants 3 messages Jan 31, 2025 – Feb 15, 2025 SPI

lore
Share & tools
lore
#0 lore raw

v2 cover spi: soft_spi: Add support for SPI_3WIRE

Add support for SPI_3WIRE on soft_spi driver.

v2:
- Remove the wrong patch
- Add GPIOD_PULL_UP to prevent the bus from floating

Hironori KIKUCHI (1):
spi: soft_spi: Add support for SPI_3WIRE

 drivers/spi/soft_spi.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 1/1 spi: soft_spi: Add support for SPI_3WIRE

When 3-wire mode is claimed on the bus, use the MOSI (output) pin to receive data. In this mode, since the transfer can only be either TX or RX, return -EINVAL if both are required at the same time.

---
 drivers/spi/soft_spi.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/spi/soft_spi.c +16 -3
index a8ec2f4f7b..50bd7be564 100644
@@ -124,8 +124,19 @@ static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
 	u8		*rxd = din;
 	int		cpha = !!(priv->mode & SPI_CPHA);
 	int		cidle = !!(priv->mode & SPI_CPOL);
+	int		txrx = plat->flags;
 	unsigned int	j;
 
+	if (priv->mode & SPI_3WIRE) {
+		if (txd && rxd)
+			return -EINVAL;
+
+		txrx = txd ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
+		dm_gpio_set_dir_flags(&plat->mosi,
+				      txd ? GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE :
+					    GPIOD_IS_IN | GPIOD_PULL_UP);
+	}
+
 	debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
 	      dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
 	      bitlen);
@@ -160,7 +171,7 @@ static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
 		 */
 		if (cpha)
 			soft_spi_scl(dev, !cidle);
-		if ((plat->flags & SPI_MASTER_NO_TX) == 0)
+		if ((txrx & SPI_MASTER_NO_TX) == 0)
 			soft_spi_sda(dev, !!(tmpdout & 0x80));
 		udelay(plat->spi_delay_us);
 
@@ -174,8 +185,10 @@ static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
 		else
 			soft_spi_scl(dev, cidle);
 		tmpdin	<<= 1;
-		if ((plat->flags & SPI_MASTER_NO_RX) == 0)
-			tmpdin	|= dm_gpio_get_value(&plat->miso);
+		if ((txrx & SPI_MASTER_NO_RX) == 0)
+			tmpdin |= dm_gpio_get_value((priv->mode & SPI_3WIRE) ?
+							    &plat->mosi :
+							    &plat->miso);
 		tmpdout	<<= 1;
 		udelay(plat->spi_delay_us);
 
Signature · 1 line
2.48.1
#2 lore raw
In reply to Hironori KIKUCHI
Quoted 10 lines · Hironori KIKUCHI

Add support for SPI_3WIRE on soft_spi driver.

v2:
- Remove the wrong patch
- Add GPIOD_PULL_UP to prevent the bus from floating

Hironori KIKUCHI (1):
spi: soft_spi: Add support for SPI_3WIRE

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom