Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3760698pxt; Tue, 10 Aug 2021 10:41:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxSq6GWYNBI7U2dFKL86827PaK2JoWRDfiBD4mmZAr0qyZU/P7RVL2Ewj6BfmNU1oxwCsTC X-Received: by 2002:aa7:df96:: with SMTP id b22mr6206398edy.324.1628617282887; Tue, 10 Aug 2021 10:41:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628617282; cv=none; d=google.com; s=arc-20160816; b=C1e3Qw7NWw3k/XM4J/RH1ou+y4pEgQTjAFfL/h6e6pNZE3Lka0jz9WpknVAsUNhvC8 M6fB0Y8XgnXia26CLsVTAe0T72tebzm4a9vHpqoi4NQWFYr84Lu2+r+PuiqJbhUzw2st VRsIMgfWk3juoQ05LKokNMK91qHrKv1SebsRblnz4pDmtgLetxNQFlETev1rHWpgM2Vg raKGZqvqkx5jocIaCtNKvNoN0vUAnxMNcO9qKwkq95JIp6yequzEQy8Yt9HuEzxH5oHX o6ecD+fO0/Oy8sgF0b/38enCW3/ewaPagVMQmxGQAU1HnA1WEKuYwamB0VRdbhw0ZOu3 BjBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sPLda0Sah/jNKdQNI2y0OP8aVFddlfVpckyTXYLpZ7M=; b=cLFX2Z0xq3t8+fZsWZrbAQbaIhETIA/BdilwsfRaIc3R/1kAdnjHgMsuKh7aYqdT7Y adfZghfV3E+xuninRlel3fCr102nuON6NKNL3HHJSRM1HQMClw3amBmO13XrNMYxhHe9 vAS8GVfr4wS1gMrLeEkmLIEMbpQfgRPUX48L4M9pMXTOac0MAhRPHNJamBEFwYgJJmSh pztZ4GlgxMO/2mSat1S38dMeLrqvDntgAH417/XZtrFGpydvI3aDyrgEuaonH2y2c24x AiVCVN8Y79GW70kfDdMh7HyUH+sLzRbUDOqj8+EBbtbxbKja0o3YzLbybyt5QoFwOFzz R+hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BsV6UnxJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h23si8165906edj.326.2021.08.10.10.40.59; Tue, 10 Aug 2021 10:41:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BsV6UnxJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234295AbhHJRhK (ORCPT + 99 others); Tue, 10 Aug 2021 13:37:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:44922 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231202AbhHJRf0 (ORCPT ); Tue, 10 Aug 2021 13:35:26 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 93010610F7; Tue, 10 Aug 2021 17:35:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628616904; bh=/8qEP94GP047ql9b9/UYZEt16UIrJCJTHmLMrxxOQtM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BsV6UnxJf+/LTbBTFuYCQ0oS6D9lA+NyClDAjRCdXv59Fjd9omd0GV7w/MKcC4vcx tf706Lim1YnQbCCjHYgW8e/WwMmTLq0+A+npuWY4wgEbqpajhzsIQ7OtFDpG4+SiNe FhWqxNR7u0GQqfnwUrbXVX4EACZfr31HEZmbALDs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Marek Vasut , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Mark Brown , Sasha Levin Subject: [PATCH 5.4 16/85] spi: imx: mx51-ecspi: Fix low-speed CONFIGREG delay calculation Date: Tue, 10 Aug 2021 19:29:49 +0200 Message-Id: <20210810172948.751157893@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172948.192298392@linuxfoundation.org> References: <20210810172948.192298392@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marek Vasut [ Upstream commit 53ca18acbe645656132fb5a329833db711067e54 ] 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 Cc: Uwe Kleine-König Cc: Mark Brown Link: https://lore.kernel.org/r/20210726100102.5188-1-marex@denx.de Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- 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 14cebcda0ccc..474d5a7fa95e 100644 --- a/drivers/spi/spi-imx.c +++ b/drivers/spi/spi-imx.c @@ -497,7 +497,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); @@ -569,8 +571,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