From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBE311684AC; Thu, 15 Aug 2024 14:00:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723730453; cv=none; b=hTCAi/3pX3JT5ZRekWgqta/inGjizovaYLJCTr3wOAl++0ZhGy2On+H2FacK331dGAvA0PkrI92yBcppMbtjgvyaYj+iobb9qzBP2kb5ORx6y2AhZDQZJyMcfPFdHg6dY6XHjupS9XfDVqqu8nW6Cd2NWWFdPAgMgNK34NRrFSs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723730453; c=relaxed/simple; bh=49pb3yPGXAIw6albdFBaSoQJy9+HMQV2Q/KAv8b3xq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DrXkUMn6KwR6OAYd3ibxi33tX4n5aSuSWNna1TWS0gtXooHtFBnJFYD1uIDccjvcFgBuNzPeZR44z/INvM/DevnW6G1GZtPggTkuxnM6VAUKgIdRGP9FUrjCicvMO8DHx5o1yrGmrOV5deoiF/BaHZljDhiW6TEp4+QCt+d7cR8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=AUi6N94l; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="AUi6N94l" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 197D7C32786; Thu, 15 Aug 2024 14:00:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1723730452; bh=49pb3yPGXAIw6albdFBaSoQJy9+HMQV2Q/KAv8b3xq8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AUi6N94l9msY8p38mno96RqGUjo1Llv7Ixf+Ui5Yz5C2wIFNJrt69amZ7TpnPsxW6 HTZX+fFX/JXXRJRLHWb1/gWL3UGf5TbjU2US3cZQzsEt/ludm1iHoYQTV/zj6vNiif 0H4AiimDSIfClNSudE9R6EkDgnsxLgkfb7ayZ0C8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Stefan Wahren , Mark Brown , Sasha Levin Subject: [PATCH 5.15 413/484] spi: spi-fsl-lpspi: Fix scldiv calculation Date: Thu, 15 Aug 2024 15:24:31 +0200 Message-ID: <20240815131957.407370556@linuxfoundation.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240815131941.255804951@linuxfoundation.org> References: <20240815131941.255804951@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Stefan Wahren [ Upstream commit 730bbfaf7d4890bd99e637db7767dc68cfeb24e7 ] 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 Link: https://patch.msgid.link/20240804113611.83613-1-wahrenst@gmx.net Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- drivers/spi/spi-fsl-lpspi.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/spi/spi-fsl-lpspi.c b/drivers/spi/spi-fsl-lpspi.c index c5ff6e8c45be0..c21d7959dcd23 100644 --- a/drivers/spi/spi-fsl-lpspi.c +++ b/drivers/spi/spi-fsl-lpspi.c @@ -297,7 +297,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); @@ -308,8 +308,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; -- 2.43.0