diff mbox series

[3/4] wilc1000: Check for errors at end of DMA write

Message ID 20210224055135.1509200-3-davidm@egauge.net
State New
Headers show
Series [1/4] wilc1000: Make SPI transfers work at 48MHz | expand

Commit Message

David Mosberger-Tang Feb. 24, 2021, 5:51 a.m. UTC
After a DMA write to the WILC chip, check for and report any errors.

This is based on code from the wilc driver in the linux-at91
repository.

Signed-off-by: David Mosberger-Tang <davidm@egauge.net>
---
 drivers/net/wireless/microchip/wilc1000/spi.c | 50 ++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

Comments

Kalle Valo Feb. 25, 2021, 8:27 a.m. UTC | #1
David Mosberger-Tang <davidm@egauge.net> writes:

> After a DMA write to the WILC chip, check for and report any errors.

>

> This is based on code from the wilc driver in the linux-at91

> repository.

>

> Signed-off-by: David Mosberger-Tang <davidm@egauge.net>

> ---

>  drivers/net/wireless/microchip/wilc1000/spi.c | 50 ++++++++++++++++++-

>  1 file changed, 49 insertions(+), 1 deletion(-)

>

> diff --git a/drivers/net/wireless/microchip/wilc1000/spi.c b/drivers/net/wireless/microchip/wilc1000/spi.c

> index fca34d1999ec..b0e096a03a28 100644

> --- a/drivers/net/wireless/microchip/wilc1000/spi.c

> +++ b/drivers/net/wireless/microchip/wilc1000/spi.c

> @@ -750,6 +750,51 @@ static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)

>  	return 0;

>  }

>  

> +static int spi_data_rsp(struct wilc *wilc, u8 cmd)

> +{

> +	struct spi_device *spi = to_spi_device(wilc->dev);

> +	int result, i;

> +	u8 rsp[4];

> +

> +	/*

> +	 * The response to data packets is two bytes long.  For

> +	 * efficiency's sake, wilc_spi_write() wisely ignores the

> +	 * responses for all packets but the final one.  The downside

> +	 * of that optimization is that when the final data packet is

> +	 * short, we may receive (part of) the response to the

> +	 * second-to-last packet before the one for the final packet.

> +	 * To handle this, we always read 4 bytes and then search for

> +	 * the last byte that contains the "Response Start" code (0xc

> +	 * in the top 4 bits).  We then know that this byte is the

> +	 * first response byte of the final data packet.

> +	 */

> +	result = wilc_spi_rx(wilc, rsp, sizeof(rsp));

> +	if (result) {

> +		dev_err(&spi->dev, "Failed bus error...\n");

> +		return result;

> +	}

> +

> +	for (i = sizeof(rsp) - 2; i >= 0; --i)

> +		if ((rsp[i] & 0xf0) == 0xc0)

> +			break;


No magic numbers. Please create proper defines for these.

> +	if (i < 0) {

> +		dev_err(&spi->dev,

> +			"Data packet response missing (%02x %02x %02x %02x)\n",

> +			rsp[0], rsp[1], rsp[2], rsp[3]);

> +		return -1;

> +	}

> +

> +	/* rsp[i] is the last response start byte */

> +

> +	if (rsp[i] != 0xc3 || rsp[i + 1] != 0x00) {


Same here.

-- 
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
David Mosberger-Tang Feb. 25, 2021, 6:10 p.m. UTC | #2
On Thu, 2021-02-25 at 10:27 +0200, Kalle Valo wrote:
> David Mosberger-Tang <davidm@egauge.net> writes:

> > +	for (i = sizeof(rsp) - 2; i >= 0; --i)

> > +		if ((rsp[i] & 0xf0) == 0xc0)

> > +			break;

> 

> No magic numbers. Please create proper defines for these.]

>

> +	if (rsp[i] != 0xc3 || rsp[i + 1] != 0x00) {

> 

> Same here.


Good points.  I'll change those, thanks.

  --david
diff mbox series

Patch

diff --git a/drivers/net/wireless/microchip/wilc1000/spi.c b/drivers/net/wireless/microchip/wilc1000/spi.c
index fca34d1999ec..b0e096a03a28 100644
--- a/drivers/net/wireless/microchip/wilc1000/spi.c
+++ b/drivers/net/wireless/microchip/wilc1000/spi.c
@@ -750,6 +750,51 @@  static int wilc_spi_write_reg(struct wilc *wilc, u32 addr, u32 data)
 	return 0;
 }
 
+static int spi_data_rsp(struct wilc *wilc, u8 cmd)
+{
+	struct spi_device *spi = to_spi_device(wilc->dev);
+	int result, i;
+	u8 rsp[4];
+
+	/*
+	 * The response to data packets is two bytes long.  For
+	 * efficiency's sake, wilc_spi_write() wisely ignores the
+	 * responses for all packets but the final one.  The downside
+	 * of that optimization is that when the final data packet is
+	 * short, we may receive (part of) the response to the
+	 * second-to-last packet before the one for the final packet.
+	 * To handle this, we always read 4 bytes and then search for
+	 * the last byte that contains the "Response Start" code (0xc
+	 * in the top 4 bits).  We then know that this byte is the
+	 * first response byte of the final data packet.
+	 */
+	result = wilc_spi_rx(wilc, rsp, sizeof(rsp));
+	if (result) {
+		dev_err(&spi->dev, "Failed bus error...\n");
+		return result;
+	}
+
+	for (i = sizeof(rsp) - 2; i >= 0; --i)
+		if ((rsp[i] & 0xf0) == 0xc0)
+			break;
+
+	if (i < 0) {
+		dev_err(&spi->dev,
+			"Data packet response missing (%02x %02x %02x %02x)\n",
+			rsp[0], rsp[1], rsp[2], rsp[3]);
+		return -1;
+	}
+
+	/* rsp[i] is the last response start byte */
+
+	if (rsp[i] != 0xc3 || rsp[i + 1] != 0x00) {
+		dev_err(&spi->dev, "Data response error (%02x %02x)\n",
+			rsp[i], rsp[i + 1]);
+		return -1;
+	}
+	return 0;
+}
+
 static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
 {
 	struct spi_device *spi = to_spi_device(wilc->dev);
@@ -777,7 +822,10 @@  static int wilc_spi_write(struct wilc *wilc, u32 addr, u8 *buf, u32 size)
 		return result;
 	}
 
-	return 0;
+	/*
+	 * Data response
+	 */
+	return spi_data_rsp(wilc, CMD_DMA_EXT_WRITE);
 }
 
 /********************************************