linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/6] spi: pxa2xx: add slave mode support
@ 2018-11-13 10:22 Lubomir Rintel
  2018-11-13 10:22 ` [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property Lubomir Rintel
                   ` (5 more replies)
  0 siblings, 6 replies; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel

Hello,

this patch set adds slave mode support to pxa2xx.

The objective is that it will be able to support the OLPC XO 1.75 embedded
controller that is a SPI master talking to a MMP2 SOC with an extra
"ready" signal for handshaking.

The patches have been submitted previously along with DT support for the
controller which has already been accepted, hence the "v3". These are
the remaining ones. Reviewed/Ack-ed by tags have been collected. Other
changes to the pataches are described in the respective messages.

The EC driver itself is being reviewed in a separate patch set.
 
Verified on a OLPC XO 1.75 machine.

Thanks to everyone who kindly provided feedback to make the patch set
better.

Thank you,
Lubo
   



^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property
  2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
@ 2018-11-13 10:22 ` Lubomir Rintel
  2018-11-13 10:28   ` Geert Uytterhoeven
  2018-11-13 10:22 ` [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished Lubomir Rintel
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, Lubomir Rintel

This is used to indicate that the chip attached to this controller is a SPI
master.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Reviewed-by: Rob Herring <robh@kernel.org>
Acked-by: Pavel Machek <pavel@ucw.cz>

---
Changes since v2:
- Updated the subject line to conform with subsystem customs

 Documentation/devicetree/bindings/spi/spi-pxa2xx.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt b/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
index 0335a9bd2e8a..89b2832283e3 100644
--- a/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
+++ b/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
@@ -11,6 +11,7 @@ Required properties:
 Optional properties:
 - cs-gpios: list of GPIO chip selects. See the SPI bus bindings,
   Documentation/devicetree/bindings/spi/spi-bus.txt
+- spi-slave: Empty property indicating the SPI controller is used in slave mode.
 
 Child nodes represent devices on the SPI bus
   See ../spi/spi-bus.txt
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished
  2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
  2018-11-13 10:22 ` [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property Lubomir Rintel
@ 2018-11-13 10:22 ` Lubomir Rintel
  2018-11-13 10:33   ` Geert Uytterhoeven
  2018-11-13 18:35   ` Applied "spi: Deal with slaves that return from transfer_one() unfinished" to the spi tree Mark Brown
  2018-11-13 10:22 ` [PATCH v3 3/6] spi: pxa2xx: Add slave mode support Lubomir Rintel
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, Lubomir Rintel

Some drivers, such as spi-pxa2xx return from the transfer_one callback
immediately, idicating that the transfer will be finished asynchronously.

Normally, spi_transfer_one_message() synchronously waits for the
transfer to finish with wait_for_completion_timeout(). For slaves, we
don't want the transaction to time out as it can complete in a long time
in future. Use wait_for_completion_interruptible() instead.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Acked-by: Pavel Machek <pavel@ucw.cz>

---
Changed since v2:
- Corrected the spi_transfer_wait() return value handling to avoid early
  bail out without the necessary cleanup (thanks Geert Uytterhoeven)

 drivers/spi/spi.c | 62 +++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 23 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 6ca59406b0b7..498d3b9bf3ae 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1037,6 +1037,42 @@ static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
 	return __spi_map_msg(ctlr, msg);
 }
 
+static int spi_transfer_wait(struct spi_controller *ctlr,
+			     struct spi_message *msg,
+			     struct spi_transfer *xfer)
+{
+	struct spi_statistics *statm = &ctlr->statistics;
+	struct spi_statistics *stats = &msg->spi->statistics;
+	unsigned long long ms = 1;
+
+	if (spi_controller_is_slave(ctlr)) {
+		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
+			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
+			return -EINTR;
+		}
+	} else {
+		ms = 8LL * 1000LL * xfer->len;
+		do_div(ms, xfer->speed_hz);
+		ms += ms + 200; /* some tolerance */
+
+		if (ms > UINT_MAX)
+			ms = UINT_MAX;
+
+		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
+						 msecs_to_jiffies(ms));
+
+		if (ms == 0) {
+			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
+			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
+			dev_err(&msg->spi->dev,
+				"SPI transfer timed out\n");
+			return -ETIMEDOUT;
+		}
+	}
+
+	return 0;
+}
+
 /*
  * spi_transfer_one_message - Default implementation of transfer_one_message()
  *
@@ -1050,7 +1086,6 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
 	struct spi_transfer *xfer;
 	bool keep_cs = false;
 	int ret = 0;
-	unsigned long long ms = 1;
 	struct spi_statistics *statm = &ctlr->statistics;
 	struct spi_statistics *stats = &msg->spi->statistics;
 
@@ -1079,28 +1114,9 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
 				goto out;
 			}
 
-			if (ret > 0) {
-				ret = 0;
-				ms = 8LL * 1000LL * xfer->len;
-				do_div(ms, xfer->speed_hz);
-				ms += ms + 200; /* some tolerance */
-
-				if (ms > UINT_MAX)
-					ms = UINT_MAX;
-
-				ms = wait_for_completion_timeout(&ctlr->xfer_completion,
-								 msecs_to_jiffies(ms));
-			}
-
-			if (ms == 0) {
-				SPI_STATISTICS_INCREMENT_FIELD(statm,
-							       timedout);
-				SPI_STATISTICS_INCREMENT_FIELD(stats,
-							       timedout);
-				dev_err(&msg->spi->dev,
-					"SPI transfer timed out\n");
-				msg->status = -ETIMEDOUT;
-			}
+			ret = spi_transfer_wait(ctlr, msg, xfer);
+			if (ret < 0)
+				msg->status = ret;
 		} else {
 			if (xfer->len)
 				dev_err(&msg->spi->dev,
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v3 3/6] spi: pxa2xx: Add slave mode support
  2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
  2018-11-13 10:22 ` [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property Lubomir Rintel
  2018-11-13 10:22 ` [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished Lubomir Rintel
@ 2018-11-13 10:22 ` Lubomir Rintel
  2018-11-13 10:22 ` [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal Lubomir Rintel
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, Lubomir Rintel

Tested on an OLPC XO-1.75 machine, where the Embedded Controller happens
to be a SPI master.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Acked-by: Pavel Machek <pavel@ucw.cz>
---
 drivers/spi/spi-pxa2xx.c       | 81 +++++++++++++++++++++++++++++++---
 include/linux/spi/pxa2xx_spi.h |  1 +
 2 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index 612cc49db28f..54ae77f1227c 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -626,6 +626,11 @@ static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
 		return IRQ_HANDLED;
 	}
 
+	if (irq_status & SSSR_TUR) {
+		int_error_stop(drv_data, "interrupt_transfer: fifo underrun");
+		return IRQ_HANDLED;
+	}
+
 	if (irq_status & SSSR_TINT) {
 		pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
 		if (drv_data->read(drv_data)) {
@@ -1073,6 +1078,11 @@ static int pxa2xx_spi_transfer_one(struct spi_controller *master,
 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
 	}
 
+	if (spi_controller_is_slave(master)) {
+		while (drv_data->write(drv_data))
+			;
+	}
+
 	/*
 	 * Release the data by enabling service requests and interrupts,
 	 * without changing any mode bits
@@ -1082,6 +1092,27 @@ static int pxa2xx_spi_transfer_one(struct spi_controller *master,
 	return 1;
 }
 
+static int pxa2xx_spi_slave_abort(struct spi_master *master)
+{
+	struct driver_data *drv_data = spi_controller_get_devdata(master);
+
+	/* Stop and reset SSP */
+	write_SSSR_CS(drv_data, drv_data->clear_sr);
+	reset_sccr1(drv_data);
+	if (!pxa25x_ssp_comp(drv_data))
+		pxa2xx_spi_write(drv_data, SSTO, 0);
+	pxa2xx_spi_flush(drv_data);
+	pxa2xx_spi_write(drv_data, SSCR0,
+			 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
+
+	dev_dbg(&drv_data->pdev->dev, "transfer aborted\n");
+
+	drv_data->master->cur_msg->status = -EINTR;
+	spi_finalize_current_transfer(drv_data->master);
+
+	return 0;
+}
+
 static void pxa2xx_spi_handle_err(struct spi_controller *master,
 				 struct spi_message *msg)
 {
@@ -1209,9 +1240,14 @@ static int setup(struct spi_device *spi)
 		rx_thres = config->rx_threshold;
 		break;
 	default:
-		tx_thres = TX_THRESH_DFLT;
 		tx_hi_thres = 0;
-		rx_thres = RX_THRESH_DFLT;
+		if (spi_controller_is_slave(drv_data->master)) {
+			tx_thres = 1;
+			rx_thres = 2;
+		} else {
+			tx_thres = TX_THRESH_DFLT;
+			rx_thres = RX_THRESH_DFLT;
+		}
 		break;
 	}
 
@@ -1255,6 +1291,12 @@ static int setup(struct spi_device *spi)
 		if (chip_info->enable_loopback)
 			chip->cr1 = SSCR1_LBM;
 	}
+	if (spi_controller_is_slave(drv_data->master)) {
+		chip->cr1 |= SSCR1_SCFR;
+		chip->cr1 |= SSCR1_SCLKDIR;
+		chip->cr1 |= SSCR1_SFRMDIR;
+		chip->cr1 |= SSCR1_SPH;
+	}
 
 	chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
 	chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
@@ -1494,6 +1536,13 @@ pxa2xx_spi_init_pdata(struct platform_device *pdev)
 	}
 #endif
 
+#if CONFIG_OF
+	if (of_id) {
+		pdata->is_slave = of_property_read_bool(pdev->dev.of_node,
+								"spi-slave");
+	}
+#endif
+
 	ssp->clk = devm_clk_get(&pdev->dev, NULL);
 	ssp->irq = platform_get_irq(pdev, 0);
 	ssp->type = type;
@@ -1559,7 +1608,11 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	master = spi_alloc_master(dev, sizeof(struct driver_data));
+	if (platform_info->is_slave)
+		master = spi_alloc_slave(dev, sizeof(struct driver_data));
+	else
+		master = spi_alloc_master(dev, sizeof(struct driver_data));
+
 	if (!master) {
 		dev_err(&pdev->dev, "cannot alloc spi_master\n");
 		pxa_ssp_free(ssp);
@@ -1581,6 +1634,7 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 	master->setup = setup;
 	master->set_cs = pxa2xx_spi_set_cs;
 	master->transfer_one = pxa2xx_spi_transfer_one;
+	master->slave_abort = pxa2xx_spi_slave_abort;
 	master->handle_err = pxa2xx_spi_handle_err;
 	master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
 	master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
@@ -1610,7 +1664,8 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 		drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
 		drv_data->dma_cr1 = DEFAULT_DMA_CR1;
 		drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
-		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
+		drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS
+						| SSSR_ROR | SSSR_TUR;
 	}
 
 	status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
@@ -1658,10 +1713,22 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
 		break;
 	default:
-		tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
-		      SSCR1_TxTresh(TX_THRESH_DFLT);
+
+		if (spi_controller_is_slave(master)) {
+			tmp = SSCR1_SCFR |
+			      SSCR1_SCLKDIR |
+			      SSCR1_SFRMDIR |
+			      SSCR1_RxTresh(2) |
+			      SSCR1_TxTresh(1) |
+			      SSCR1_SPH;
+		} else {
+			tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
+			      SSCR1_TxTresh(TX_THRESH_DFLT);
+		}
 		pxa2xx_spi_write(drv_data, SSCR1, tmp);
-		tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
+		tmp = SSCR0_Motorola | SSCR0_DataSize(8);
+		if (!spi_controller_is_slave(master))
+			tmp |= SSCR0_SCR(2);
 		pxa2xx_spi_write(drv_data, SSCR0, tmp);
 		break;
 	}
diff --git a/include/linux/spi/pxa2xx_spi.h b/include/linux/spi/pxa2xx_spi.h
index 9ec4c147abbc..b0674e330ef6 100644
--- a/include/linux/spi/pxa2xx_spi.h
+++ b/include/linux/spi/pxa2xx_spi.h
@@ -25,6 +25,7 @@ struct dma_chan;
 struct pxa2xx_spi_master {
 	u16 num_chipselect;
 	u8 enable_dma;
+	bool is_slave;
 
 	/* DMA engine specific config */
 	bool (*dma_filter)(struct dma_chan *chan, void *param);
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal
  2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
                   ` (2 preceding siblings ...)
  2018-11-13 10:22 ` [PATCH v3 3/6] spi: pxa2xx: Add slave mode support Lubomir Rintel
@ 2018-11-13 10:22 ` Lubomir Rintel
  2018-11-13 10:29   ` Geert Uytterhoeven
  2018-11-13 18:34   ` Applied "spi: pxa2xx: dt-bindings: Add ready GPIO signal" to the spi tree Mark Brown
  2018-11-13 10:22 ` [PATCH v3 5/6] spi: pxa2xx: Add ready signal Lubomir Rintel
  2018-11-13 10:22 ` [PATCH v3 6/6] spi: pxa2xx: Deal with the leftover garbage in TXFIFO Lubomir Rintel
  5 siblings, 2 replies; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, Lubomir Rintel

This this is used to let the SPI master know that our FIFO is filled and
we're ready to service a transfer. Only useful in slave mode.

A signal like this is used by an embedded controller on a OLPC XO 1.75
machine, that happens to be a SPI master.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Acked-by: Pavel Machek <pavel@ucw.cz>

---
Changes since v2:
- s/ready-gpio/ready-gpios/
- Updated the subject line to conform with subsystem customs

 Documentation/devicetree/bindings/spi/spi-pxa2xx.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt b/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
index 89b2832283e3..e30e0c2a4bce 100644
--- a/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
+++ b/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
@@ -12,6 +12,8 @@ Optional properties:
 - cs-gpios: list of GPIO chip selects. See the SPI bus bindings,
   Documentation/devicetree/bindings/spi/spi-bus.txt
 - spi-slave: Empty property indicating the SPI controller is used in slave mode.
+- ready-gpios: GPIO used to signal a SPI master that the FIFO is filled
+  and we're ready to service a transfer. Only useful in slave mode.
 
 Child nodes represent devices on the SPI bus
   See ../spi/spi-bus.txt
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v3 5/6] spi: pxa2xx: Add ready signal
  2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
                   ` (3 preceding siblings ...)
  2018-11-13 10:22 ` [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal Lubomir Rintel
@ 2018-11-13 10:22 ` Lubomir Rintel
  2018-11-13 10:31   ` Geert Uytterhoeven
  2018-11-13 18:34   ` Applied "spi: pxa2xx: Add ready signal" to the spi tree Mark Brown
  2018-11-13 10:22 ` [PATCH v3 6/6] spi: pxa2xx: Deal with the leftover garbage in TXFIFO Lubomir Rintel
  5 siblings, 2 replies; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, Lubomir Rintel

Strobe a GPIO line when the slave TX FIFO is filled. This is how the
Embedded Controller on an OLPC XO-1.75 machine, that happens to be a SPI
master, learns that it can initiate a transaction.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Tested-by: Pavel Machek <pavel@ucw.cz>

---
Changes since v2
- Avoid an useless delay if there's no ready GPIO
- Remove useless (int)PTR_ERR(...) casts from pxa2xx_spi_probe()
  (thanks to Geert Uytterhoeven)

 drivers/spi/spi-pxa2xx.c | 16 +++++++++++++++-
 drivers/spi/spi-pxa2xx.h |  3 +++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index 54ae77f1227c..7e5aab0af501 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -1081,6 +1081,11 @@ static int pxa2xx_spi_transfer_one(struct spi_controller *master,
 	if (spi_controller_is_slave(master)) {
 		while (drv_data->write(drv_data))
 			;
+		if (drv_data->gpiod_ready) {
+			gpiod_set_value(drv_data->gpiod_ready, 1);
+			udelay(1);
+			gpiod_set_value(drv_data->gpiod_ready, 0);
+		}
 	}
 
 	/*
@@ -1778,7 +1783,7 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 				if (PTR_ERR(gpiod) == -ENOENT)
 					continue;
 
-				status = (int)PTR_ERR(gpiod);
+				status = PTR_ERR(gpiod);
 				goto out_error_clock_enabled;
 			} else {
 				drv_data->cs_gpiods[i] = gpiod;
@@ -1786,6 +1791,15 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 		}
 	}
 
+	if (platform_info->is_slave) {
+		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
+						"ready", GPIOD_OUT_LOW);
+		if (IS_ERR(drv_data->gpiod_ready)) {
+			status = PTR_ERR(drv_data->gpiod_ready);
+			goto out_error_clock_enabled;
+		}
+	}
+
 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
 	pm_runtime_use_autosuspend(&pdev->dev);
 	pm_runtime_set_active(&pdev->dev);
diff --git a/drivers/spi/spi-pxa2xx.h b/drivers/spi/spi-pxa2xx.h
index 513c53aaeab2..4e324da66ef7 100644
--- a/drivers/spi/spi-pxa2xx.h
+++ b/drivers/spi/spi-pxa2xx.h
@@ -64,6 +64,9 @@ struct driver_data {
 
 	/* GPIOs for chip selects */
 	struct gpio_desc **cs_gpiods;
+
+	/* Optional slave FIFO ready signal */
+	struct gpio_desc *gpiod_ready;
 };
 
 struct chip_data {
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v3 6/6] spi: pxa2xx: Deal with the leftover garbage in TXFIFO
  2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
                   ` (4 preceding siblings ...)
  2018-11-13 10:22 ` [PATCH v3 5/6] spi: pxa2xx: Add ready signal Lubomir Rintel
@ 2018-11-13 10:22 ` Lubomir Rintel
  5 siblings, 0 replies; 14+ messages in thread
From: Lubomir Rintel @ 2018-11-13 10:22 UTC (permalink / raw)
  To: Mark Brown, Geert Uytterhoeven
  Cc: James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, Lubomir Rintel

There doesn't seem to be a way to empty TXFIFO on MMP2. The datasheet is
super-secret and the method described in Armada 16x manual won't work:

  "The TXFIFO and RXFIFO are cleared to 0b0 when the SSPx port is reset or
  disabled (by writing a 0b0 to the <Synchronous Serial Port Enable> field
  in the SSP Control Register 0)."

  # devmem 0xd4037008           # read SSSR
  0x0000F204
  # devmem 0xd4037000 32 0x07   # SSE off in SSCR0
  # devmem 0xd4037000 32 0x87   # SSE on
  # devmem 0xd4037008
  0x0000F204
         ^ TXFIFO level is still 2. Sigh.

The OLPC 1.75 boot firmware leaves two bytes in the TXFIFO. Those are
basically throwaway bytes used in response to the messages from the EC.
The OLPC kernel copes with this by power-cycling the hardware. Perhaps
the firmware should do this instead.

Other than that, there's not much we can do other than complain loudly
until the garbage gets drained and discard the actual data... For the
OLPC EC this will work just fine and pushing more data to TXFIFO would
break further transactions.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Acked-by: Pavel Machek <pavel@ucw.cz>

---
Changes since v1:
- Fixed a commit message typo (spotted by James Cameron)

 drivers/spi/spi-pxa2xx.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index 7e5aab0af501..29e6025f104c 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -1078,6 +1078,20 @@ static int pxa2xx_spi_transfer_one(struct spi_controller *master,
 			pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
 	}
 
+	if (drv_data->ssp_type == MMP2_SSP) {
+		u8 tx_level = (pxa2xx_spi_read(drv_data, SSSR)
+					& SSSR_TFL_MASK) >> 8;
+
+		if (tx_level) {
+			/* On MMP2, flipping SSE doesn't to empty TXFIFO. */
+			dev_warn(&spi->dev, "%d bytes of garbage in TXFIFO!\n",
+								tx_level);
+			if (tx_level > transfer->len)
+				tx_level = transfer->len;
+			drv_data->tx += tx_level;
+		}
+	}
+
 	if (spi_controller_is_slave(master)) {
 		while (drv_data->write(drv_data))
 			;
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property
  2018-11-13 10:22 ` [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property Lubomir Rintel
@ 2018-11-13 10:28   ` Geert Uytterhoeven
  0 siblings, 0 replies; 14+ messages in thread
From: Geert Uytterhoeven @ 2018-11-13 10:28 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Mark Brown, Geert Uytterhoeven, quozl, Rob Herring, Mark Rutland,
	Eric Miao, Haojian Zhuang, Daniel Mack, Robert Jarzmik,
	linux-spi,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
	Linux Kernel Mailing List, Linux ARM

On Tue, Nov 13, 2018 at 11:23 AM Lubomir Rintel <lkundrak@v3.sk> wrote:
> This is used to indicate that the chip attached to this controller is a SPI
> master.
>
> Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
> Reviewed-by: Rob Herring <robh@kernel.org>
> Acked-by: Pavel Machek <pavel@ucw.cz>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal
  2018-11-13 10:22 ` [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal Lubomir Rintel
@ 2018-11-13 10:29   ` Geert Uytterhoeven
  2018-11-13 18:34   ` Applied "spi: pxa2xx: dt-bindings: Add ready GPIO signal" to the spi tree Mark Brown
  1 sibling, 0 replies; 14+ messages in thread
From: Geert Uytterhoeven @ 2018-11-13 10:29 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Mark Brown, Geert Uytterhoeven, quozl, Rob Herring, Mark Rutland,
	Eric Miao, Haojian Zhuang, Daniel Mack, Robert Jarzmik,
	linux-spi,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
	Linux Kernel Mailing List, Linux ARM

On Tue, Nov 13, 2018 at 11:23 AM Lubomir Rintel <lkundrak@v3.sk> wrote:
> This this is used to let the SPI master know that our FIFO is filled and
> we're ready to service a transfer. Only useful in slave mode.
>
> A signal like this is used by an embedded controller on a OLPC XO 1.75
> machine, that happens to be a SPI master.
>
> Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
> Acked-by: Pavel Machek <pavel@ucw.cz>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v3 5/6] spi: pxa2xx: Add ready signal
  2018-11-13 10:22 ` [PATCH v3 5/6] spi: pxa2xx: Add ready signal Lubomir Rintel
@ 2018-11-13 10:31   ` Geert Uytterhoeven
  2018-11-13 18:34   ` Applied "spi: pxa2xx: Add ready signal" to the spi tree Mark Brown
  1 sibling, 0 replies; 14+ messages in thread
From: Geert Uytterhoeven @ 2018-11-13 10:31 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Mark Brown, Geert Uytterhoeven, quozl, Rob Herring, Mark Rutland,
	Eric Miao, Haojian Zhuang, Daniel Mack, Robert Jarzmik,
	linux-spi,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
	Linux Kernel Mailing List, Linux ARM

On Tue, Nov 13, 2018 at 11:23 AM Lubomir Rintel <lkundrak@v3.sk> wrote:
> Strobe a GPIO line when the slave TX FIFO is filled. This is how the
> Embedded Controller on an OLPC XO-1.75 machine, that happens to be a SPI
> master, learns that it can initiate a transaction.
>
> Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
> Tested-by: Pavel Machek <pavel@ucw.cz>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished
  2018-11-13 10:22 ` [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished Lubomir Rintel
@ 2018-11-13 10:33   ` Geert Uytterhoeven
  2018-11-13 18:35   ` Applied "spi: Deal with slaves that return from transfer_one() unfinished" to the spi tree Mark Brown
  1 sibling, 0 replies; 14+ messages in thread
From: Geert Uytterhoeven @ 2018-11-13 10:33 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Mark Brown, Geert Uytterhoeven, quozl, Rob Herring, Mark Rutland,
	Eric Miao, Haojian Zhuang, Daniel Mack, Robert Jarzmik,
	linux-spi,
	open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS,
	Linux Kernel Mailing List, Linux ARM

On Tue, Nov 13, 2018 at 11:23 AM Lubomir Rintel <lkundrak@v3.sk> wrote:
> Some drivers, such as spi-pxa2xx return from the transfer_one callback
> immediately, idicating that the transfer will be finished asynchronously.
>
> Normally, spi_transfer_one_message() synchronously waits for the
> transfer to finish with wait_for_completion_timeout(). For slaves, we
> don't want the transaction to time out as it can complete in a long time
> in future. Use wait_for_completion_interruptible() instead.
>
> Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
> Acked-by: Pavel Machek <pavel@ucw.cz>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Applied "spi: pxa2xx: Add ready signal" to the spi tree
  2018-11-13 10:22 ` [PATCH v3 5/6] spi: pxa2xx: Add ready signal Lubomir Rintel
  2018-11-13 10:31   ` Geert Uytterhoeven
@ 2018-11-13 18:34   ` Mark Brown
  1 sibling, 0 replies; 14+ messages in thread
From: Mark Brown @ 2018-11-13 18:34 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Pavel Machek, Mark Brown, Mark Brown, Geert Uytterhoeven,
	James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, linux-spi

The patch

   spi: pxa2xx: Add ready signal

has been applied to the spi tree at

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git 

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.  

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

From 77d33897c68d56797a3201e06bdf10e2094a96d6 Mon Sep 17 00:00:00 2001
From: Lubomir Rintel <lkundrak@v3.sk>
Date: Tue, 13 Nov 2018 11:22:27 +0100
Subject: [PATCH] spi: pxa2xx: Add ready signal

Strobe a GPIO line when the slave TX FIFO is filled. This is how the
Embedded Controller on an OLPC XO-1.75 machine, that happens to be a SPI
master, learns that it can initiate a transaction.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Tested-by: Pavel Machek <pavel@ucw.cz>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/spi/spi-pxa2xx.c | 16 +++++++++++++++-
 drivers/spi/spi-pxa2xx.h |  3 +++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-pxa2xx.c b/drivers/spi/spi-pxa2xx.c
index a057c3be7e3b..69b221e34b2d 100644
--- a/drivers/spi/spi-pxa2xx.c
+++ b/drivers/spi/spi-pxa2xx.c
@@ -1081,6 +1081,11 @@ static int pxa2xx_spi_transfer_one(struct spi_controller *master,
 	if (spi_controller_is_slave(master)) {
 		while (drv_data->write(drv_data))
 			;
+		if (drv_data->gpiod_ready) {
+			gpiod_set_value(drv_data->gpiod_ready, 1);
+			udelay(1);
+			gpiod_set_value(drv_data->gpiod_ready, 0);
+		}
 	}
 
 	/*
@@ -1778,7 +1783,7 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 				if (PTR_ERR(gpiod) == -ENOENT)
 					continue;
 
-				status = (int)PTR_ERR(gpiod);
+				status = PTR_ERR(gpiod);
 				goto out_error_clock_enabled;
 			} else {
 				drv_data->cs_gpiods[i] = gpiod;
@@ -1786,6 +1791,15 @@ static int pxa2xx_spi_probe(struct platform_device *pdev)
 		}
 	}
 
+	if (platform_info->is_slave) {
+		drv_data->gpiod_ready = devm_gpiod_get_optional(dev,
+						"ready", GPIOD_OUT_LOW);
+		if (IS_ERR(drv_data->gpiod_ready)) {
+			status = PTR_ERR(drv_data->gpiod_ready);
+			goto out_error_clock_enabled;
+		}
+	}
+
 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
 	pm_runtime_use_autosuspend(&pdev->dev);
 	pm_runtime_set_active(&pdev->dev);
diff --git a/drivers/spi/spi-pxa2xx.h b/drivers/spi/spi-pxa2xx.h
index 513c53aaeab2..4e324da66ef7 100644
--- a/drivers/spi/spi-pxa2xx.h
+++ b/drivers/spi/spi-pxa2xx.h
@@ -64,6 +64,9 @@ struct driver_data {
 
 	/* GPIOs for chip selects */
 	struct gpio_desc **cs_gpiods;
+
+	/* Optional slave FIFO ready signal */
+	struct gpio_desc *gpiod_ready;
 };
 
 struct chip_data {
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Applied "spi: pxa2xx: dt-bindings: Add ready GPIO signal" to the spi tree
  2018-11-13 10:22 ` [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal Lubomir Rintel
  2018-11-13 10:29   ` Geert Uytterhoeven
@ 2018-11-13 18:34   ` Mark Brown
  1 sibling, 0 replies; 14+ messages in thread
From: Mark Brown @ 2018-11-13 18:34 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Pavel Machek, Mark Brown, Mark Brown, Geert Uytterhoeven,
	James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, linux-spi

The patch

   spi: pxa2xx: dt-bindings: Add ready GPIO signal

has been applied to the spi tree at

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git 

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.  

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

From 8ae13d0b0d4bb4af99bec8c50152f0c8f5cbcc06 Mon Sep 17 00:00:00 2001
From: Lubomir Rintel <lkundrak@v3.sk>
Date: Tue, 13 Nov 2018 11:22:26 +0100
Subject: [PATCH] spi: pxa2xx: dt-bindings: Add ready GPIO signal

This this is used to let the SPI master know that our FIFO is filled and
we're ready to service a transfer. Only useful in slave mode.

A signal like this is used by an embedded controller on a OLPC XO 1.75
machine, that happens to be a SPI master.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Acked-by: Pavel Machek <pavel@ucw.cz>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 Documentation/devicetree/bindings/spi/spi-pxa2xx.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt b/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
index 89b2832283e3..e30e0c2a4bce 100644
--- a/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
+++ b/Documentation/devicetree/bindings/spi/spi-pxa2xx.txt
@@ -12,6 +12,8 @@ Optional properties:
 - cs-gpios: list of GPIO chip selects. See the SPI bus bindings,
   Documentation/devicetree/bindings/spi/spi-bus.txt
 - spi-slave: Empty property indicating the SPI controller is used in slave mode.
+- ready-gpios: GPIO used to signal a SPI master that the FIFO is filled
+  and we're ready to service a transfer. Only useful in slave mode.
 
 Child nodes represent devices on the SPI bus
   See ../spi/spi-bus.txt
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Applied "spi: Deal with slaves that return from transfer_one() unfinished" to the spi tree
  2018-11-13 10:22 ` [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished Lubomir Rintel
  2018-11-13 10:33   ` Geert Uytterhoeven
@ 2018-11-13 18:35   ` Mark Brown
  1 sibling, 0 replies; 14+ messages in thread
From: Mark Brown @ 2018-11-13 18:35 UTC (permalink / raw)
  To: Lubomir Rintel
  Cc: Pavel Machek, Mark Brown, Mark Brown, Geert Uytterhoeven,
	James Cameron, Rob Herring, Mark Rutland, Eric Miao,
	Haojian Zhuang, Daniel Mack, Robert Jarzmik, linux-spi,
	devicetree, linux-kernel, linux-arm-kernel, linux-spi

The patch

   spi: Deal with slaves that return from transfer_one() unfinished

has been applied to the spi tree at

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git 

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.  

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark

From 810923f3bf06c11022b7bb370dd3130a956a222e Mon Sep 17 00:00:00 2001
From: Lubomir Rintel <lkundrak@v3.sk>
Date: Tue, 13 Nov 2018 11:22:24 +0100
Subject: [PATCH] spi: Deal with slaves that return from transfer_one()
 unfinished

Some drivers, such as spi-pxa2xx return from the transfer_one callback
immediately, idicating that the transfer will be finished asynchronously.

Normally, spi_transfer_one_message() synchronously waits for the
transfer to finish with wait_for_completion_timeout(). For slaves, we
don't want the transaction to time out as it can complete in a long time
in future. Use wait_for_completion_interruptible() instead.

Signed-off-by: Lubomir Rintel <lkundrak@v3.sk>
Acked-by: Pavel Machek <pavel@ucw.cz>
Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
Signed-off-by: Mark Brown <broonie@kernel.org>
---
 drivers/spi/spi.c | 62 +++++++++++++++++++++++++++++------------------
 1 file changed, 39 insertions(+), 23 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 6ca59406b0b7..498d3b9bf3ae 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1037,6 +1037,42 @@ static int spi_map_msg(struct spi_controller *ctlr, struct spi_message *msg)
 	return __spi_map_msg(ctlr, msg);
 }
 
+static int spi_transfer_wait(struct spi_controller *ctlr,
+			     struct spi_message *msg,
+			     struct spi_transfer *xfer)
+{
+	struct spi_statistics *statm = &ctlr->statistics;
+	struct spi_statistics *stats = &msg->spi->statistics;
+	unsigned long long ms = 1;
+
+	if (spi_controller_is_slave(ctlr)) {
+		if (wait_for_completion_interruptible(&ctlr->xfer_completion)) {
+			dev_dbg(&msg->spi->dev, "SPI transfer interrupted\n");
+			return -EINTR;
+		}
+	} else {
+		ms = 8LL * 1000LL * xfer->len;
+		do_div(ms, xfer->speed_hz);
+		ms += ms + 200; /* some tolerance */
+
+		if (ms > UINT_MAX)
+			ms = UINT_MAX;
+
+		ms = wait_for_completion_timeout(&ctlr->xfer_completion,
+						 msecs_to_jiffies(ms));
+
+		if (ms == 0) {
+			SPI_STATISTICS_INCREMENT_FIELD(statm, timedout);
+			SPI_STATISTICS_INCREMENT_FIELD(stats, timedout);
+			dev_err(&msg->spi->dev,
+				"SPI transfer timed out\n");
+			return -ETIMEDOUT;
+		}
+	}
+
+	return 0;
+}
+
 /*
  * spi_transfer_one_message - Default implementation of transfer_one_message()
  *
@@ -1050,7 +1086,6 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
 	struct spi_transfer *xfer;
 	bool keep_cs = false;
 	int ret = 0;
-	unsigned long long ms = 1;
 	struct spi_statistics *statm = &ctlr->statistics;
 	struct spi_statistics *stats = &msg->spi->statistics;
 
@@ -1079,28 +1114,9 @@ static int spi_transfer_one_message(struct spi_controller *ctlr,
 				goto out;
 			}
 
-			if (ret > 0) {
-				ret = 0;
-				ms = 8LL * 1000LL * xfer->len;
-				do_div(ms, xfer->speed_hz);
-				ms += ms + 200; /* some tolerance */
-
-				if (ms > UINT_MAX)
-					ms = UINT_MAX;
-
-				ms = wait_for_completion_timeout(&ctlr->xfer_completion,
-								 msecs_to_jiffies(ms));
-			}
-
-			if (ms == 0) {
-				SPI_STATISTICS_INCREMENT_FIELD(statm,
-							       timedout);
-				SPI_STATISTICS_INCREMENT_FIELD(stats,
-							       timedout);
-				dev_err(&msg->spi->dev,
-					"SPI transfer timed out\n");
-				msg->status = -ETIMEDOUT;
-			}
+			ret = spi_transfer_wait(ctlr, msg, xfer);
+			if (ret < 0)
+				msg->status = ret;
 		} else {
 			if (xfer->len)
 				dev_err(&msg->spi->dev,
-- 
2.19.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2018-11-13 18:36 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-13 10:22 [PATCH v3 0/6] spi: pxa2xx: add slave mode support Lubomir Rintel
2018-11-13 10:22 ` [PATCH v3 1/6] spi: pxa2xx: dt-bindings: Add spi-slave property Lubomir Rintel
2018-11-13 10:28   ` Geert Uytterhoeven
2018-11-13 10:22 ` [PATCH v3 2/6] spi: Deal with slaves that return from transfer_one() unfinished Lubomir Rintel
2018-11-13 10:33   ` Geert Uytterhoeven
2018-11-13 18:35   ` Applied "spi: Deal with slaves that return from transfer_one() unfinished" to the spi tree Mark Brown
2018-11-13 10:22 ` [PATCH v3 3/6] spi: pxa2xx: Add slave mode support Lubomir Rintel
2018-11-13 10:22 ` [PATCH v3 4/6] spi: pxa2xx: dt-bindings: Add ready GPIO signal Lubomir Rintel
2018-11-13 10:29   ` Geert Uytterhoeven
2018-11-13 18:34   ` Applied "spi: pxa2xx: dt-bindings: Add ready GPIO signal" to the spi tree Mark Brown
2018-11-13 10:22 ` [PATCH v3 5/6] spi: pxa2xx: Add ready signal Lubomir Rintel
2018-11-13 10:31   ` Geert Uytterhoeven
2018-11-13 18:34   ` Applied "spi: pxa2xx: Add ready signal" to the spi tree Mark Brown
2018-11-13 10:22 ` [PATCH v3 6/6] spi: pxa2xx: Deal with the leftover garbage in TXFIFO Lubomir Rintel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).