linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation
@ 2021-07-26 10:01 Marek Vasut
  2021-07-26 21:34 ` Mark Brown
  2021-07-27  6:28 ` Uwe Kleine-König
  0 siblings, 2 replies; 3+ messages in thread
From: Marek Vasut @ 2021-07-26 10:01 UTC (permalink / raw)
  To: linux-spi; +Cc: Marek Vasut, Uwe Kleine-König, Mark Brown

The spi_imx->spi_bus_clk may be uninitialized and thus also zero in
mx51_ecspi_prepare_message(), which would lead to division by zero
in kernel. Since bitbang .setup_transfer callback which initializes
the spi_imx->spi_bus_clk is called after bitbang prepare_message
callback, iterate over all the transfers in spi_message, find the
one with lowest bus frequency, and use that bus frequency for the
delay calculation.

Note that it is not possible to move this CONFIGREG delay back into
the .setup_transfer callback, because that is invoked too late, after
the GPIO chipselects were already configured.

Fixes: 135cbd378eab ("spi: imx: mx51-ecspi: Reinstate low-speed CONFIGREG delay")
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Cc: Mark Brown <broonie@kernel.org>
---
V2: Add comment regarding all zero-frequency transfers
---
 drivers/spi/spi-imx.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c
index 593b63be73de8..cb9f7261c4385 100644
--- a/drivers/spi/spi-imx.c
+++ b/drivers/spi/spi-imx.c
@@ -505,7 +505,9 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
 				      struct spi_message *msg)
 {
 	struct spi_device *spi = msg->spi;
+	struct spi_transfer *xfer;
 	u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
+	u32 min_speed_hz = ~0U;
 	u32 testreg, delay;
 	u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
 
@@ -577,8 +579,20 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
 	 * be asserted before the SCLK polarity changes, which would disrupt
 	 * the SPI communication as the device on the other end would consider
 	 * the change of SCLK polarity as a clock tick already.
+	 *
+	 * Because spi_imx->spi_bus_clk is only set in bitbang prepare_message
+	 * callback, iterate over all the transfers in spi_message, find the
+	 * one with lowest bus frequency, and use that bus frequency for the
+	 * delay calculation. In case all transfers have speed_hz == 0, then
+	 * min_speed_hz is ~0 and the resulting delay is zero.
 	 */
-	delay = (2 * 1000000) / spi_imx->spi_bus_clk;
+	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
+		if (!xfer->speed_hz)
+			continue;
+		min_speed_hz = min(xfer->speed_hz, min_speed_hz);
+	}
+
+	delay = (2 * 1000000) / min_speed_hz;
 	if (likely(delay < 10))	/* SCLK is faster than 100 kHz */
 		udelay(delay);
 	else			/* SCLK is _very_ slow */
-- 
2.30.2


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

* Re: [PATCH V2] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation
  2021-07-26 10:01 [PATCH V2] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation Marek Vasut
@ 2021-07-26 21:34 ` Mark Brown
  2021-07-27  6:28 ` Uwe Kleine-König
  1 sibling, 0 replies; 3+ messages in thread
From: Mark Brown @ 2021-07-26 21:34 UTC (permalink / raw)
  To: Marek Vasut, linux-spi; +Cc: Mark Brown, Uwe Kleine-König

On Mon, 26 Jul 2021 12:01:02 +0200, Marek Vasut wrote:
> The spi_imx->spi_bus_clk may be uninitialized and thus also zero in
> mx51_ecspi_prepare_message(), which would lead to division by zero
> in kernel. Since bitbang .setup_transfer callback which initializes
> the spi_imx->spi_bus_clk is called after bitbang prepare_message
> callback, iterate over all the transfers in spi_message, find the
> one with lowest bus frequency, and use that bus frequency for the
> delay calculation.
> 
> [...]

Applied to

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

Thanks!

[1/1] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation
      commit: 53ca18acbe645656132fb5a329833db711067e54

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

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

* Re: [PATCH V2] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation
  2021-07-26 10:01 [PATCH V2] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation Marek Vasut
  2021-07-26 21:34 ` Mark Brown
@ 2021-07-27  6:28 ` Uwe Kleine-König
  1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2021-07-27  6:28 UTC (permalink / raw)
  To: Marek Vasut; +Cc: linux-spi, Mark Brown

[-- Attachment #1: Type: text/plain, Size: 1988 bytes --]

Hello,

On Mon, Jul 26, 2021 at 12:01:02PM +0200, Marek Vasut wrote:
>  drivers/spi/spi-imx.c | 16 +++++++++++++++-
>  1 file changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c
> index 593b63be73de8..cb9f7261c4385 100644
> --- a/drivers/spi/spi-imx.c
> +++ b/drivers/spi/spi-imx.c
> @@ -505,7 +505,9 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
>  				      struct spi_message *msg)
>  {
>  	struct spi_device *spi = msg->spi;
> +	struct spi_transfer *xfer;
>  	u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
> +	u32 min_speed_hz = ~0U;
>  	u32 testreg, delay;
>  	u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
>  
> @@ -577,8 +579,20 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
>  	 * be asserted before the SCLK polarity changes, which would disrupt
>  	 * the SPI communication as the device on the other end would consider
>  	 * the change of SCLK polarity as a clock tick already.
> +	 *
> +	 * Because spi_imx->spi_bus_clk is only set in bitbang prepare_message
> +	 * callback, iterate over all the transfers in spi_message, find the
> +	 * one with lowest bus frequency, and use that bus frequency for the
> +	 * delay calculation. In case all transfers have speed_hz == 0, then
> +	 * min_speed_hz is ~0 and the resulting delay is zero.
>  	 */
> -	delay = (2 * 1000000) / spi_imx->spi_bus_clk;
> +	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
> +		if (!xfer->speed_hz)
> +			continue;
> +		min_speed_hz = min(xfer->speed_hz, min_speed_hz);
> +	}
> +
> +	delay = (2 * 1000000) / min_speed_hz;

It might already be too late as Mark already send out his applied mail,
but still:

Acked-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2021-07-27  6:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-26 10:01 [PATCH V2] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation Marek Vasut
2021-07-26 21:34 ` Mark Brown
2021-07-27  6:28 ` Uwe Kleine-König

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).