Skip to contentSkip to conversation

mfd: cros ec: spi: Don't send first message too soon

PATCH In review Jon Hunter 4 participants 8 messages Nov 14, 2017 – Nov 29, 2017

Subsystems (4)
lore
Share & tools
lore

Jon Hunter

author maintainer
#0 lore raw

PATCH v2 1/2 mfd: cros ec: spi: Don't send first message too soon

On the Tegra124 Nyan-Big chromebook the very first SPI message sent to the EC is failing.

The Tegra SPI driver configures the SPI chip-selects to be active-high by default (and always has for many years). The EC SPI requires an active-low chip-select and so the Tegra chip-select is reconfigured to be active-low when the EC SPI driver calls spi_setup(). The problem is that if the first SPI message to the EC is sent too soon after reconfiguring the SPI chip-select, it fails.

The EC SPI driver prevents back-to-back SPI messages being sent too soon by keeping track of the time the last transfer was sent via the variable 'last_transfer_ns'. To prevent the very first transfer being sent too soon, initialise the 'last_transfer_ns' variable after calling spi_setup() and before sending the first SPI message.

---
Changes since V1:
- Added stable-tag and Brian's reviewed-by.

Looks like this issue has been around for several Linux releases now and it just depends on timing if this issue is seen or not and so there is no specific commit this fixes. However, would be good to include for v4.15.

 drivers/mfd/cros_ec_spi.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mfd/cros_ec_spi.c +1 -0
index c9714072e224..a14196e95e9b 100644
@@ -667,6 +667,7 @@ static int cros_ec_spi_probe(struct spi_device *spi)
 			   sizeof(struct ec_response_get_protocol_info);
 	ec_dev->dout_size = sizeof(struct ec_host_request);
 
+	ec_spi->last_transfer_ns = ktime_get_ns();
 
 	err = cros_ec_register(ec_dev);
 	if (err) {
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v2 2/2 mfd: cros ec: spi: Simplify delay handling between SPI messages

The EC SPI driver prevents SPI transfers being to rapidly by keeping track of the time the last transfer was issued via the 'last_transfer_ns' variable. Previously, if the 'last_transfer_ns' variable was zero, this indicated that no previous transfer had been sent and that no delay was needed. However, the EC SPI driver has been updated to always initialise the 'last_transfer_ns' variable during probe and therefore, it is no longer necessary to test if it is zero. Remove the code that checks if this variable is zero.

---
Changes since V1:
- Added Brian's reviewed-by.

 drivers/mfd/cros_ec_spi.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
drivers/mfd/cros_ec_spi.c +9 -16
index a14196e95e9b..7849d781c6bb 100644
@@ -72,8 +72,7 @@
  * struct cros_ec_spi - information about a SPI-connected EC
  *
  * @spi: SPI device we are connected to
- * @last_transfer_ns: time that we last finished a transfer, or 0 if there
- *	if no record
+ * @last_transfer_ns: time that we last finished a transfer.
  * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that
  *      is sent when we want to turn on CS at the start of a transaction.
  * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
@@ -378,18 +377,15 @@ static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
 	u8 *rx_buf;
 	u8 sum;
 	int ret = 0, final_ret;
+	unsigned long delay;
 
 	len = cros_ec_prepare_tx(ec_dev, ec_msg);
 	dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
 
 	/* If it's too soon to do another transaction, wait */
-	if (ec_spi->last_transfer_ns) {
-		unsigned long delay;	/* The delay completed so far */
-
-		delay = ktime_get_ns() - ec_spi->last_transfer_ns;
-		if (delay < EC_SPI_RECOVERY_TIME_NS)
-			ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
-	}
+	delay = ktime_get_ns() - ec_spi->last_transfer_ns;
+	if (delay < EC_SPI_RECOVERY_TIME_NS)
+		ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
 
 	rx_buf = kzalloc(len, GFP_KERNEL);
 	if (!rx_buf)
@@ -510,18 +506,15 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
 	u8 *rx_buf;
 	int sum;
 	int ret = 0, final_ret;
+	unsigned long delay;
 
 	len = cros_ec_prepare_tx(ec_dev, ec_msg);
 	dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
 
 	/* If it's too soon to do another transaction, wait */
-	if (ec_spi->last_transfer_ns) {
-		unsigned long delay;	/* The delay completed so far */
-
-		delay = ktime_get_ns() - ec_spi->last_transfer_ns;
-		if (delay < EC_SPI_RECOVERY_TIME_NS)
-			ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
-	}
+	delay = ktime_get_ns() - ec_spi->last_transfer_ns;
+	if (delay < EC_SPI_RECOVERY_TIME_NS)
+		ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
 
 	rx_buf = kzalloc(len, GFP_KERNEL);
 	if (!rx_buf)
Signature · 1 line
2.7.4
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH V2 2/2] mfd: cros ec: spi: Simplify delay handling between SPI messages

Hi,

Quoted 9 lines · Jon Hunter

The EC SPI driver prevents SPI transfers being to rapidly by keeping track of the time the last transfer was issued via the 'last_transfer_ns' variable. Previously, if the 'last_transfer_ns' variable was zero, this indicated that no previous transfer had been sent and that no delay was needed. However, the EC SPI driver has been updated to always initialise the 'last_transfer_ns' variable during probe and therefore, it is no longer necessary to test if it is zero. Remove the code that checks if this variable is zero.

Quoted 8 lines

---
Changes since V1:
- Added Brian's reviewed-by.

 drivers/mfd/cros_ec_spi.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
Acked-by on v2 2/2
#3 lore raw

Re: [PATCH V2 2/2] mfd: cros ec: spi: Simplify delay handling between SPI messages

In reply to Jon Hunter

Hi Jon,

Quoted 11 lines · Jon Hunter

The EC SPI driver prevents SPI transfers being to rapidly by keeping track of the time the last transfer was issued via the 'last_transfer_ns' variable. Previously, if the 'last_transfer_ns' variable was zero, this indicated that no previous transfer had been sent and that no delay was needed. However, the EC SPI driver has been updated to always initialise the 'last_transfer_ns' variable during probe and therefore, it is no longer necessary to test if it is zero. Remove the code that checks if this variable is zero.

Signature · 7 lines
Benson Leung
Staff Software Engineer
Chrome OS Kernel
Google Inc.
[email protected]
Chromium OS Project
[email protected]
Reviewed-by on v2 1/2
#4 lore raw
In reply to Jon Hunter

Hi,

Quoted 24 lines · Jon Hunter

On the Tegra124 Nyan-Big chromebook the very first SPI message sent to the EC is failing.

The Tegra SPI driver configures the SPI chip-selects to be active-high by default (and always has for many years). The EC SPI requires an active-low chip-select and so the Tegra chip-select is reconfigured to be active-low when the EC SPI driver calls spi_setup(). The problem is that if the first SPI message to the EC is sent too soon after reconfiguring the SPI chip-select, it fails.

The EC SPI driver prevents back-to-back SPI messages being sent too soon by keeping track of the time the last transfer was sent via the variable 'last_transfer_ns'. To prevent the very first transfer being sent too soon, initialise the 'last_transfer_ns' variable after calling spi_setup() and before sending the first SPI message.

---
Changes since V1:
- Added stable-tag and Brian's reviewed-by.

Quoted 7 lines

Looks like this issue has been around for several Linux releases now and it just depends on timing if this issue is seen or not and so there is no specific commit this fixes. However, would be good to include for v4.15.

 drivers/mfd/cros_ec_spi.c | 1 +
 1 file changed, 1 insertion(+)
Acked-by on v2 1/2
#5 lore raw
In reply to Jon Hunter

Hi Jon,

Quoted 16 lines · Jon Hunter

On the Tegra124 Nyan-Big chromebook the very first SPI message sent to the EC is failing.

The Tegra SPI driver configures the SPI chip-selects to be active-high by default (and always has for many years). The EC SPI requires an active-low chip-select and so the Tegra chip-select is reconfigured to be active-low when the EC SPI driver calls spi_setup(). The problem is that if the first SPI message to the EC is sent too soon after reconfiguring the SPI chip-select, it fails.

The EC SPI driver prevents back-to-back SPI messages being sent too soon by keeping track of the time the last transfer was sent via the variable 'last_transfer_ns'. To prevent the very first transfer being sent too soon, initialise the 'last_transfer_ns' variable after calling spi_setup() and before sending the first SPI message.

Quoted 4 lines
Signature · 7 lines
Benson Leung
Staff Software Engineer
Chrome OS Kernel
Google Inc.
[email protected]
Chromium OS Project
[email protected]
author maintainer
#6 lore raw
In reply to Jon Hunter

Hi Lee,

Quoted 37 lines · Jon Hunter

On the Tegra124 Nyan-Big chromebook the very first SPI message sent to the EC is failing.

The Tegra SPI driver configures the SPI chip-selects to be active-high by default (and always has for many years). The EC SPI requires an active-low chip-select and so the Tegra chip-select is reconfigured to be active-low when the EC SPI driver calls spi_setup(). The problem is that if the first SPI message to the EC is sent too soon after reconfiguring the SPI chip-select, it fails.

The EC SPI driver prevents back-to-back SPI messages being sent too soon by keeping track of the time the last transfer was sent via the variable 'last_transfer_ns'. To prevent the very first transfer being sent too soon, initialise the 'last_transfer_ns' variable after calling spi_setup() and before sending the first SPI message.

---
Changes since V1:
- Added stable-tag and Brian's reviewed-by.

Looks like this issue has been around for several Linux releases now and it just depends on timing if this issue is seen or not and so there is no specific commit this fixes. However, would be good to include for v4.15.

 drivers/mfd/cros_ec_spi.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mfd/cros_ec_spi.c +0 -0
index c9714072e224..a14196e95e9b 100644

drivers/mfd/cros_ec_spi.c static int cros_ec_spi_probe(struct spi_device *spi) · line 673

Quoted 7 lines
drivers/mfd/cros_ec_spi.c +1 -0
@@ -667,6 +667,7 @@ static int cros_ec_spi_probe(struct spi_device *spi)
 			   sizeof(struct ec_response_get_protocol_info);
 	ec_dev->dout_size = sizeof(struct ec_host_request);
 
+	ec_spi->last_transfer_ns = ktime_get_ns();
 
 	err = cros_ec_register(ec_dev);
 	if (err) {

Can you queue this as a fix for v4.15-rc1?

Cheers
Jon

Signature · 1 line
nvpublic
#7 lore raw
Quoted 41 lines · Jon Hunter
Quoted 40 lines · Jon Hunter

On the Tegra124 Nyan-Big chromebook the very first SPI message sent to the EC is failing.

The Tegra SPI driver configures the SPI chip-selects to be active-high by default (and always has for many years). The EC SPI requires an active-low chip-select and so the Tegra chip-select is reconfigured to be active-low when the EC SPI driver calls spi_setup(). The problem is that if the first SPI message to the EC is sent too soon after reconfiguring the SPI chip-select, it fails.

The EC SPI driver prevents back-to-back SPI messages being sent too soon by keeping track of the time the last transfer was sent via the variable 'last_transfer_ns'. To prevent the very first transfer being sent too soon, initialise the 'last_transfer_ns' variable after calling spi_setup() and before sending the first SPI message.

---
Changes since V1:
- Added stable-tag and Brian's reviewed-by.

Looks like this issue has been around for several Linux releases now and it just depends on timing if this issue is seen or not and so there is no specific commit this fixes. However, would be good to include for v4.15.

 drivers/mfd/cros_ec_spi.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mfd/cros_ec_spi.c +0 -0
index c9714072e224..a14196e95e9b 100644
@@ -667,6 +667,7 @@ static int cros_ec_spi_probe(struct spi_device *spi)
 			   sizeof(struct ec_response_get_protocol_info);
 	ec_dev->dout_size = sizeof(struct ec_host_request);
 
Quoted 6 lines

drivers/mfd/cros_ec_spi.c static int cros_ec_spi_probe(struct spi_device *spi) · line 673

Quoted 4 lines
drivers/mfd/cros_ec_spi.c +1 -0
+	ec_spi->last_transfer_ns = ktime_get_ns();
 
 	err = cros_ec_register(ec_dev);
 	if (err) {

Can you queue this as a fix for v4.15-rc1?

No, but I will submit it for one of the v4.15-rcs.

Signature · 4 lines
Lee Jones
Linaro STMicroelectronics Landing Team Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog