From patchwork Sun Aug 4 11:36:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Wahren X-Patchwork-Id: 13752608 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1C63DC3DA7F for ; Sun, 4 Aug 2024 11:37:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=o8wiZyuTf+7p/7c8oFkjGNg7jlgN2Y4Wy2pe5S8ALnk=; b=dou4ouhp7rkd4/lHDT1Ith5a4X 1gbH6hwV96lSMZghA9gTTcSKafcSmKFKFtZRUo/r/Msd0VODNcFatA/EreJ0Vq8XG+p8Ta/ZNByJh A6hnoWKGClwocXuAQF8LGp6uC1Cw0W+lw+zK9ZWSEleSbWmCdsHIrHawJByEMR+RsiIhhJ1/xD0SY x9IljnIFpiFH4GRNsU7vhP41T9KbxM4ikxzYvQeaCCtyLrCvaNSk/pQ54f4Yi7nQLqRo+Z+eo95jP U5c+7Bxcw3HYHa1+/VGYBgUaIAUPLrAGEv9HebHQQQm4ADZ9y8SYr/p7MMzLpWUJXGIt4qiErfd1J H0pd1JfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1saZXm-0000000DABi-2WYo; Sun, 04 Aug 2024 11:36:58 +0000 Received: from mout.gmx.net ([212.227.15.15]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1saZXF-0000000DA9t-3xVW for linux-arm-kernel@lists.infradead.org; Sun, 04 Aug 2024 11:36:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1722771377; x=1723376177; i=wahrenst@gmx.net; bh=o8wiZyuTf+7p/7c8oFkjGNg7jlgN2Y4Wy2pe5S8ALnk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:Message-Id: MIME-Version:Content-Transfer-Encoding:cc: content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=Gisk60Q1HhtUwBYr2a/n4ldu6Me2TBzpyivB1gjE+z5+ahV0TyX7f9gSBO6CCgGM 2hEJEigUVIjYVV8qeiGNdrxkIFv7Rt1KcJ35xcxOVARYZ8FEVkNBjiVU0sT+Lcrmr d/8onTdYorrpt+rh1jlvhAGtXrSXZWLKa7sBn1PNG8LI49R6Qkkdnbcm1aFlTMyfv lKUzwPPk/xDmDRXOGWqQzRqr+8MvU2S0y8QvVn1reKw5OiZBAkSl+vV2ipnqWMHeJ q8BMQM+AXoVmFF42+gXUxFMY24dTIqGSsIlOdUJiSZf+/zeGn9GzkWVZ0YtHl8AtA IdoRO+mWmWjTXj+09w== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from stefanw-SCHENKER ([37.4.248.43]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N8GQs-1sEcmv2tXc-013vNO; Sun, 04 Aug 2024 13:36:17 +0200 From: Stefan Wahren To: Mark Brown , Fabio Estevam , Gao Pan , Clark Wang Cc: imx@lists.linux.dev, kernel@pengutronix.de, linux-spi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Stefan Wahren Subject: [PATCH] spi: spi-fsl-lpspi: Fix scldiv calculation Date: Sun, 4 Aug 2024 13:36:11 +0200 Message-Id: <20240804113611.83613-1-wahrenst@gmx.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Provags-ID: V03:K1:niX0Js6Gc3sxUv1LEoco3KU9bIwI3f6AQv2+SLIljqq331yyAO4 hE3XEJBSC+yFsBPVH+I0Sl92+2Af0ovj3nGSCewbSOJ304vfiA3r1DlVYXHv0O6hWrFYX0r yPoMeGAEr3YGEtIxcDMuTSmsAfKFQaUgMCdtCWtyN/CRcNVr1WJjfAXxsRrGXmmHDcWq9jE WmMU+06dioxipOsnd/yxw== UI-OutboundReport: notjunk:1;M01:P0:9ulYULqnHBk=;B1BJ6ZR0sqUMWtNhJoVob21OZZJ QeGCYBaogUOH+8h8M+1XPE6fvQDOLxqhH82SYBVdn9NZ3AB7+Iimqrx1dJvstS4tfAMCxzhNN Qqmq7IQERP0P+e6YnPsCmlJSH4eclMVB5zZv7GK8PklGsotP/BKNJcG4jjBcNgIsixEd6SZK7 FNRxOwLQj9LoNOjQMEwtegjUjNJT8AqXpJSFw1jg+KbqxQeFNuVGfPVaS2lUnibXDKwEBRXdx worZJqvoNW5rdxHBFHzgQnegt8iJexz6awY2CqSa1Zhxjj9135YFu1s2CukJIH7KFKFXNqOOB GB8f5y9wjM79iPvpyLocjxsDDfkZA0jJY3JAlylCgZitE8uoC641vzeekCoKnBHztNDRTp5u0 2BXnzjZtZvjcO1SNjfkIfAkDScnrGr9HZZLdYw/daQG39GV0iHAYdJrTTt/ACuVd9aOkk3x7J wRz+Fr7QKsL0eLB6kaRYiqwSNcyr/CVBg3p+RBa+oDNmo+a0IPnaGHx00VhdanahR/NYHaeYU Lyp388n8Pcf4ijfTHrjzB9XCzAWwPNCKWvXX7Cck+H5OsnRuwEv3hL3vRigqG5zsHLVG03gnP uGp4G4iKZRmI9CX527LEVKwvrBpMTkUzvwJiGOYqryerLhZ+SjC9yXMzmiMDWXWn2QJGOMZeS +X0Lt0/69PBT1mv1vUQ6RlrPDHjtjxhW3aCjTYh2Lcyy/AJ+y86WH0tSdokQ1xXOFlor6PJUs Fm1zLKECpd/AvSlcuhAM1El3vvtBwMGMwH70yHhvR5A3SvI6iOpxncfOzBPWji6ckVE3luT2A d7JEybXiNnwQFKoBS+vr6Q3A== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240804_043626_295056_92E9D8CC X-CRM114-Status: GOOD ( 10.27 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The effective SPI clock frequency should never exceed speed_hz otherwise this might result in undefined behavior of the SPI device. Currently the scldiv calculation could violate this constraint. For the example parameters perclk_rate = 24 MHz and speed_hz = 7 MHz, the function fsl_lpspi_set_bitrate will determine perscale = 0 and scldiv = 1, which is a effective SPI clock of 8 MHz. So fix this by rounding up the quotient of perclk_rate and speed_hz. While this never change within the loop, we can pull this out. Fixes: 5314987de5e5 ("spi: imx: add lpspi bus driver") Signed-off-by: Stefan Wahren --- drivers/spi/spi-fsl-lpspi.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) -- 2.34.1 diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c index 32baa14dfd83..be261ac09df8 100644 --- a/drivers/spi/spi-fsl-lpspi.c +++ b/drivers/spi/spi-fsl-lpspi.c @@ -296,7 +296,7 @@ static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi) static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi) { struct lpspi_config config = fsl_lpspi->config; - unsigned int perclk_rate, scldiv; + unsigned int perclk_rate, scldiv, div; u8 prescale; perclk_rate = clk_get_rate(fsl_lpspi->clk_per); @@ -313,8 +313,10 @@ static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi) return -EINVAL; } + div = DIV_ROUND_UP(perclk_rate, config.speed_hz); + for (prescale = 0; prescale < 8; prescale++) { - scldiv = perclk_rate / config.speed_hz / (1 << prescale) - 2; + scldiv = div / (1 << prescale) - 2; if (scldiv < 256) { fsl_lpspi->config.prescale = prescale; break;