Skip to contentSkip to conversation

can: mcp251x: fix support for half duplex SPI host controllers

PATCH In review Marc Kleine-Budde 3 participants 6 messages Mar 30, 2021 – Mar 31, 2021 CAN network drivers

lore
Share & tools
lore

Marc Kleine-Budde

author maintainer
#0 lore raw

PATCH 1/1 can: mcp251x: fix support for half duplex SPI host controllers

Some SPI host controllers do not support full-duplex SPI transfers.

The function mcp251x_spi_trans() does a full duplex transfer. It is used in several places in the driver, where a TX half duplex transfer is sufficient.

To fix support for half duplex SPI host controllers, this patch introduces a new function mcp251x_spi_write() and changes all callers that do a TX half duplex transfer to use mcp251x_spi_write().

Fixes e0e25001d088 ("can: mcp251x: add support for half duplex controllers")
Cc Tim Harvey <[email protected]>
---
 drivers/net/can/spi/mcp251x.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/net/can/spi/mcp251x.c +18 -6
index 80ab1593ca31..492f1bcb0516 100644
@@ -314,6 +314,18 @@ static int mcp251x_spi_trans(struct spi_device *spi, int len)
 	return ret;
 }
 
+static int mcp251x_spi_write(struct spi_device *spi, int len)
+{
+	struct mcp251x_priv *priv = spi_get_drvdata(spi);
+	int ret;
+
+	ret = spi_write(spi, priv->spi_tx_buf, len);
+	if (ret)
+		dev_err(&spi->dev, "spi write failed: ret = %d\n", ret);
+
+	return ret;
+}
+
 static u8 mcp251x_read_reg(struct spi_device *spi, u8 reg)
 {
 	struct mcp251x_priv *priv = spi_get_drvdata(spi);
@@ -361,7 +373,7 @@ static void mcp251x_write_reg(struct spi_device *spi, u8 reg, u8 val)
 	priv->spi_tx_buf[1] = reg;
 	priv->spi_tx_buf[2] = val;
 
-	mcp251x_spi_trans(spi, 3);
+	mcp251x_spi_write(spi, 3);
 }
 
 static void mcp251x_write_2regs(struct spi_device *spi, u8 reg, u8 v1, u8 v2)
@@ -373,7 +385,7 @@ static void mcp251x_write_2regs(struct spi_device *spi, u8 reg, u8 v1, u8 v2)
 	priv->spi_tx_buf[2] = v1;
 	priv->spi_tx_buf[3] = v2;
 
-	mcp251x_spi_trans(spi, 4);
+	mcp251x_spi_write(spi, 4);
 }
 
 static void mcp251x_write_bits(struct spi_device *spi, u8 reg,
@@ -386,7 +398,7 @@ static void mcp251x_write_bits(struct spi_device *spi, u8 reg,
 	priv->spi_tx_buf[2] = mask;
 	priv->spi_tx_buf[3] = val;
 
-	mcp251x_spi_trans(spi, 4);
+	mcp251x_spi_write(spi, 4);
 }
 
 static u8 mcp251x_read_stat(struct spi_device *spi)
@@ -618,7 +630,7 @@ static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf,
 					  buf[i]);
 	} else {
 		memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len);
-		mcp251x_spi_trans(spi, TXBDAT_OFF + len);
+		mcp251x_spi_write(spi, TXBDAT_OFF + len);
 	}
 }
 
@@ -650,7 +662,7 @@ static void mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame,
 
 	/* use INSTRUCTION_RTS, to avoid "repeated frame problem" */
 	priv->spi_tx_buf[0] = INSTRUCTION_RTS(1 << tx_buf_idx);
-	mcp251x_spi_trans(priv->spi, 1);
+	mcp251x_spi_write(priv->spi, 1);
 }
 
 static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf,
@@ -888,7 +900,7 @@ static int mcp251x_hw_reset(struct spi_device *spi)
 	mdelay(MCP251X_OST_DELAY_MS);
 
 	priv->spi_tx_buf[0] = INSTRUCTION_RESET;
-	ret = mcp251x_spi_trans(spi, 1);
+	ret = mcp251x_spi_write(spi, 1);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.30.2
#1 lore raw
Quoted 11 lines · Marc Kleine-Budde

Some SPI host controllers do not support full-duplex SPI transfers.

The function mcp251x_spi_trans() does a full duplex transfer. It is used in several places in the driver, where a TX half duplex transfer is sufficient.

To fix support for half duplex SPI host controllers, this patch introduces a new function mcp251x_spi_write() and changes all callers that do a TX half duplex transfer to use mcp251x_spi_write().

Quoted 5 lines
Fixes e0e25001d088 ("can: mcp251x: add support for half duplex controllers")
Cc Tim Harvey <[email protected]>

---

Mi Marc and Gerhard,

So was the issue being resolved here that there was another SPI host controller that wasn't advertising that it was half duplex only or was something else wrong with e0e25001d088 ("can: mcp251x: add support for half duplex controllers")?

Best regards,

Tim

author maintainer
#2 lore raw
Quoted 8 lines · Tim Harvey
Quoted 7 lines · Marc Kleine-Budde

Some SPI host controllers do not support full-duplex SPI transfers.

The function mcp251x_spi_trans() does a full duplex transfer. It is used in several places in the driver, where a TX half duplex transfer is sufficient.

Quoted 6 lines
Quoted 3 lines

To fix support for half duplex SPI host controllers, this patch introduces a new function mcp251x_spi_write() and changes all callers that do a TX half duplex transfer to use mcp251x_spi_write().

So was the issue being resolved here that there was another SPI host controller that wasn't advertising that it was half duplex only

I don't know which SPI host controller Gerhard uses, but I assume it has half duplex set, as the driver probe fails with:

| [ 112.226164] mcp251x spi0.1: spi transfer failed: ret = -22

The -22 is returned by the SPI framework if you have a half duplex controller and a transfer with both TX and RX buffer set. This is the case in the mcp251x_spi_trans() function.

Quoted 2 lines

or was something else wrong with e0e25001d088 ("can: mcp251x: add support for half duplex controllers")?

Your patch only converted the SPI read path to use half duplex transfers. My patch also converts the SPI write path.

If your half duplex controller works without that patch, the controller driver doesn't advertise correctly that it is half duplex only. If the hardware is indeed half duplex only, better send a patch that sets the half duplex flag. If the hardware support full duplex, but the driver somehow doesn't implement it correctly, so that it implements half duplex only you should at least drop a note on the SPI mailing list.

Can you test this patch and give me a Tested-by?

regards,
Marc

--

Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
#3 lore raw

Am 2021-03-31 09:14, schrieb Marc Kleine-Budde:

Quoted 16 lines · Tim Harvey
Quoted 8 lines · Marc Kleine-Budde
Quoted 7 lines

Some SPI host controllers do not support full-duplex SPI transfers.

The function mcp251x_spi_trans() does a full duplex transfer. It is used in several places in the driver, where a TX half duplex transfer is sufficient.

Quoted 6 lines
Quoted 3 lines

To fix support for half duplex SPI host controllers, this patch introduces a new function mcp251x_spi_write() and changes all callers that do a TX half duplex transfer to use mcp251x_spi_write().

So was the issue being resolved here that there was another SPI host controller that wasn't advertising that it was half duplex only

Quoted 8 lines

I don't know which SPI host controller Gerhard uses, but I assume it has half duplex set, as the driver probe fails with:

| [ 112.226164] mcp251x spi0.1: spi transfer failed: ret = -22

The -22 is returned by the SPI framework if you have a half duplex controller and a transfer with both TX and RX buffer set. This is the case in the mcp251x_spi_trans() function.

I'm using an Onion Omega2 board with MT7688 CPU. It uses MediaTek MT7621 SPI controller driver - spi-mt7621.c (5.10.26) with sets the flag to:

        master->flags = SPI_CONTROLLER_HALF_DUPLEX;

All Mediatek/Ralink SPI controller are only half duplex AFAIK.

Quoted 6 lines
Quoted 3 lines

or was something else wrong with e0e25001d088 ("can: mcp251x: add support for half duplex controllers")?

Your patch only converted the SPI read path to use half duplex transfers. My patch also converts the SPI write path.

Without Marcs latest patch the MCP2515 didn't initialized correctly and showed the error mentioned above (spi transfer failed).

Regards

Gerd

Tested-by
#4 lore raw
In reply to Marc Kleine-Budde
Quoted 32 lines · Marc Kleine-Budde
Quoted 8 lines · Tim Harvey
Quoted 7 lines · Marc Kleine-Budde

Some SPI host controllers do not support full-duplex SPI transfers.

The function mcp251x_spi_trans() does a full duplex transfer. It is used in several places in the driver, where a TX half duplex transfer is sufficient.

Quoted 6 lines
Quoted 3 lines

To fix support for half duplex SPI host controllers, this patch introduces a new function mcp251x_spi_write() and changes all callers that do a TX half duplex transfer to use mcp251x_spi_write().

So was the issue being resolved here that there was another SPI host controller that wasn't advertising that it was half duplex only

I don't know which SPI host controller Gerhard uses, but I assume it has half duplex set, as the driver probe fails with:

| [ 112.226164] mcp251x spi0.1: spi transfer failed: ret = -22

The -22 is returned by the SPI framework if you have a half duplex controller and a transfer with both TX and RX buffer set. This is the case in the mcp251x_spi_trans() function.

Quoted 2 lines

or was something else wrong with e0e25001d088 ("can: mcp251x: add support for half duplex controllers")?

Your patch only converted the SPI read path to use half duplex transfers. My patch also converts the SPI write path.

Quoted 6 lines

If your half duplex controller works without that patch, the controller driver doesn't advertise correctly that it is half duplex only. If the hardware is indeed half duplex only, better send a patch that sets the half duplex flag. If the hardware support full duplex, but the driver somehow doesn't implement it correctly, so that it implements half duplex only you should at least drop a note on the SPI mailing list.

Marc,

Thanks for the explanation!

I was surprised as the 5.4 kernel I use with the CN803x OcteonTX using drivers/spi/spi-cavium-thunderx.c works fine but as you say it is because the host controller does not advertise half duplex only in that kernel. I did mainlin in e8510d43f219 ("spi: spi-cavium-thunderx:
flag controller as half duplex") which appears in 5.9.

Quoted 3 lines

Can you test this patch and give me a Tested-by?

I did verify that with this patch 5.12-rc5 initializes the mcp251x on the CN803x OcteonTx and without it we fail.

Tested on a GW6404 board with an OcteonTX SoC and MCP25625

By the way, I believe you were discussing at one point the possibility of adding something in the spi core that would be able to implement half duplex transactions for drivers written for full duplex communication. Is that something on your list or even possible?

regards,

Tim

author maintainer
#5 lore raw
Quoted 9 lines · Tim Harvey
Quoted 9 lines

Your patch only converted the SPI read path to use half duplex transfers. My patch also converts the SPI write path.

If your half duplex controller works without that patch, the controller driver doesn't advertise correctly that it is half duplex only. If the hardware is indeed half duplex only, better send a patch that sets the half duplex flag. If the hardware support full duplex, but the driver somehow doesn't implement it correctly, so that it implements half duplex only you should at least drop a note on the SPI mailing list.

[...]

Quoted 7 lines

Thanks for the explanation!

I was surprised as the 5.4 kernel I use with the CN803x OcteonTX using drivers/spi/spi-cavium-thunderx.c works fine but as you say it is because the host controller does not advertise half duplex only in that kernel. I did mainlin in e8510d43f219 ("spi: spi-cavium-thunderx:
flag controller as half duplex") which appears in 5.9.

Thanks for mainlining the half duplex flag and clarifying the issue here, I was to lazy to look up which controller you're using and if the patch was mainlined.

Quoted 9 lines
Quoted 2 lines

Can you test this patch and give me a Tested-by?

I did verify that with this patch 5.12-rc5 initializes the mcp251x on the CN803x OcteonTx and without it we fail.

Tested on a GW6404 board with an OcteonTX SoC and MCP25625

Thanks a lot!

Quoted 4 lines

By the way, I believe you were discussing at one point the possibility of adding something in the spi core that would be able to implement half duplex transactions for drivers written for full duplex communication. Is that something on your list or even possible?

Yes I had this discussion, but the only thing the SPI core can do is to aggregate several half duplex transfers into one full duplex one. This requires a lot of memory handling/copying and I decided to not work on this.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |