linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Fabio Estevam <festevam@gmail.com>
To: broonie@kernel.org
Cc: tudor.ambarus@microchip.com, linux-spi@vger.kernel.org,
	Fabio Estevam <festevam@gmail.com>
Subject: [PATCH] spi: Fix regression when the SPI controller does not pass max_speed_hz
Date: Mon, 28 Dec 2020 16:32:50 -0300	[thread overview]
Message-ID: <20201228193250.26808-1-festevam@gmail.com> (raw)

Since commit 9326e4f1e5dd ("spi: Limit the spi device max speed to
controller's max speed") the following regression is observed on an
imx6q-sabresd:

[    3.918284] spi_imx 2008000.spi: cannot set clock freq: 0 (base freq: 60000000)
[    3.925953] Division by zero in kernel.
[    3.929831] CPU: 1 PID: 1 Comm: swapper/0 Tainted: G        W         5.10.0-next-20201223 #276
[    3.938565] Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree)
[    3.945124] [<c0111a68>] (unwind_backtrace) from [<c010c068>] (show_stack+0x10/0x14)
[    3.952925] [<c010c068>] (show_stack) from [<c0e11540>] (dump_stack+0xe0/0x10c)
[    3.960279] [<c0e11540>] (dump_stack) from [<c05d471c>] (Ldiv0+0x8/0x10)
[    3.967026] [<c05d471c>] (Ldiv0) from [<c089b044>] (mx51_ecspi_prepare_transfer+0xfc/0x17c)

The spi-imx driver does not fill the max_speed_hz field, so we get:

spi->max_speed_hz = 20MHz
spi->controller->max_speed_hz = 0MHz

which will result in spi->max_speed_hz being 0, causing the division by
zero in the spi-imx driver.

Fix this problem, by checking if spi->controller->max_speed_hz is not
zero prior to assign it to spi->max_speed_hz.

Fixes: 9326e4f1e5dd ("spi: Limit the spi device max speed to controller's max speed")
Signed-off-by: Fabio Estevam <festevam@gmail.com>
---
 drivers/spi/spi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 51d7c004fbab..daa3d89484df 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -3379,8 +3379,9 @@ int spi_setup(struct spi_device *spi)
 		return status;
 
 	if (!spi->max_speed_hz ||
-	    spi->max_speed_hz > spi->controller->max_speed_hz)
-		spi->max_speed_hz = spi->controller->max_speed_hz;
+	    (spi->max_speed_hz > spi->controller->max_speed_hz))
+		if (spi->controller->max_speed_hz)
+			spi->max_speed_hz = spi->controller->max_speed_hz;
 
 	mutex_lock(&spi->controller->io_mutex);
 
-- 
2.17.1


                 reply	other threads:[~2020-12-28 23:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201228193250.26808-1-festevam@gmail.com \
    --to=festevam@gmail.com \
    --cc=broonie@kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=tudor.ambarus@microchip.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).