Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp132771rwi; Wed, 12 Oct 2022 17:19:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5wuh0TciCtfvqxc9oSNEvXrs/PXJbanUmDU7219YNyWPngXsglmVTBfiIs0/wxvoF6xoCX X-Received: by 2002:a50:fe85:0:b0:458:5562:bf1e with SMTP id d5-20020a50fe85000000b004585562bf1emr29023492edt.167.1665620365632; Wed, 12 Oct 2022 17:19:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665620365; cv=none; d=google.com; s=arc-20160816; b=pXiAj6GDx3BrNUYZPK+Er7Rh/EZSJoLijxd1B1Q2ibLeEj/oh+RluZJmPvI1rerx8q cnzN0KTvDWF86e8Sb5D1tsILLLs4pT4EkV1jrGfO3MTQrmjORxqH48YPtoqJW8YboW4B 7XX49/Pzv6omtnhn7kJAfcQX4dbXFMr1TeaksKy3f8+RvyYbMAJL2dVv8jHqOjzPZyXP DMXkbT49xOkJupuOnj4jctUNdgXQc1kd4XC25s+7rV6kmC/oIzZUlI/5AC68RT3CUokp KPIdY0ZEWQ0M/QPwJokNkxnh092u2pInBpNXY8AUrKaMmzDxRX82eEMORCz+7+cVCMf5 NQ2A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8+G/2Du0XuxubuCfDN8v+J3rkn9TxxkYAp2SWkRg5gg=; b=Kv/eTbEiqWtCs640AdNVTNKi/U1BJpY518Zjm3h3LF/kJt3fSrEDpnP+JFkgk17EWa P0DH3pfjh6w0HhckMfrP6SRZLoKv5SX5ouWI2B90BzJJhXlkT5ww2yArhGxCcIzF1MtI TYm8EQY9NsdTYLZsgC6SpOQIxrClzqEgBy3pnY65n8EyjCOeZASiwxtkhCpuTx2nLWZF K1EK0VA/ev8ScPnfDhkBcM1e1nI4hIa4UIZHViCKVoivHsV0BBqJx6AgJXEQDpJyo63b NeCiChbpX6IB1ESdHIQeb3chehIxxQCxIpjUI6EXH4sXwroVI7D2bGJsIiN/FaxuPqI/ pxpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Cj/2JSt6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dp18-20020a170906c15200b0078d9db99a6bsi13206360ejc.444.2022.10.12.17.18.56; Wed, 12 Oct 2022 17:19:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Cj/2JSt6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229809AbiJMAQZ (ORCPT + 99 others); Wed, 12 Oct 2022 20:16:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229732AbiJMAQO (ORCPT ); Wed, 12 Oct 2022 20:16:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3480DFBCD0; Wed, 12 Oct 2022 17:16:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 7D3DA616B3; Thu, 13 Oct 2022 00:16:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6F92C433D7; Thu, 13 Oct 2022 00:16:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620171; bh=GvZod3RFAjidyXSUoKZ273KLjuujQzygOSc6mBdthnY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cj/2JSt6FLoSzHqztKPtdWX1zNT3zBuMK6K/lbnNOZbmxHMR3z9dq4afzkxmHBxsv VEynonPLoePKOb+cRiQugMZgucVXzY6ceLC2llLG3SUlXODoh+AXCBDJMf8AQFsBzg 7aQKbnwZbFYEPvQRshsFptUZuZoGknUG5dtf4ddjBkW6EQF1xfikiP8lyLAZ3wQYe/ AzjDOeBgc2tK3f8WOiM689cz40OY4/EAK07F2I+HTSUmLYTHbyaxuKPsAnpm695RHX snrVOwQVswbUEjFg5Gvvp4Hkrny/jFpOeF9ytHbGjUydq6ReE/Y/yrD7i6lOC+5PGb j42cC/0w2DxeA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Quanyang Wang , Shubhrajyoti Datta , Stephen Boyd , Sasha Levin , mturquette@baylibre.com, michal.simek@xilinx.com, m.tretter@pengutronix.de, linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH AUTOSEL 6.0 05/67] clk: zynqmp: pll: rectify rate rounding in zynqmp_pll_round_rate Date: Wed, 12 Oct 2022 20:14:46 -0400 Message-Id: <20221013001554.1892206-5-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013001554.1892206-1-sashal@kernel.org> References: <20221013001554.1892206-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Quanyang Wang [ Upstream commit 30eaf02149ecc3c5815e45d27187bf09e925071d ] The function zynqmp_pll_round_rate is used to find a most appropriate PLL frequency which the hardware can generate according to the desired frequency. For example, if the desired frequency is 297MHz, considering the limited range from PS_PLL_VCO_MIN (1.5GHz) to PS_PLL_VCO_MAX (3.0GHz) of PLL, zynqmp_pll_round_rate should return 1.872GHz (297MHz * 5). There are two problems with the current code of zynqmp_pll_round_rate: 1) When the rate is below PS_PLL_VCO_MIN, it can't find a correct rate when the parameter "rate" is an integer multiple of *prate, in other words, if "f" is zero, zynqmp_pll_round_rate won't return a valid frequency which is from PS_PLL_VCO_MIN to PS_PLL_VCO_MAX. For example, *prate is 33MHz and the rate is 660MHz, zynqmp_pll_round_rate will not boost up rate and just return 660MHz, and this will cause clk_calc_new_rates failure since zynqmp_pll_round_rate returns an invalid rate out of its boundaries. 2) Even if the rate is higher than PS_PLL_VCO_MIN, there is still a risk that zynqmp_pll_round_rate returns an invalid rate because the function DIV_ROUND_CLOSEST makes some loss in the fractional part. If the parent clock *prate is 33333333Hz and we want to set the PLL rate to 1.5GHz, this function will return 1499999985Hz by using the formula below: value = *prate * DIV_ROUND_CLOSEST(rate, *prate)). This value is also invalid since it's slightly smaller than PS_PLL_VCO_MIN. because DIV_ROUND_CLOSEST makes some loss in the fractional part. Signed-off-by: Quanyang Wang Link: https://lore.kernel.org/r/20220826142030.213805-1-quanyang.wang@windriver.com Reviewed-by: Shubhrajyoti Datta Signed-off-by: Stephen Boyd Signed-off-by: Sasha Levin --- drivers/clk/zynqmp/pll.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/drivers/clk/zynqmp/pll.c b/drivers/clk/zynqmp/pll.c index 91a6b4cc910e..0d3e1377b092 100644 --- a/drivers/clk/zynqmp/pll.c +++ b/drivers/clk/zynqmp/pll.c @@ -102,26 +102,25 @@ static long zynqmp_pll_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *prate) { u32 fbdiv; - long rate_div, f; + u32 mult, div; - /* Enable the fractional mode if needed */ - rate_div = (rate * FRAC_DIV) / *prate; - f = rate_div % FRAC_DIV; - if (f) { - if (rate > PS_PLL_VCO_MAX) { - fbdiv = rate / PS_PLL_VCO_MAX; - rate = rate / (fbdiv + 1); - } - if (rate < PS_PLL_VCO_MIN) { - fbdiv = DIV_ROUND_UP(PS_PLL_VCO_MIN, rate); - rate = rate * fbdiv; - } - return rate; + /* Let rate fall inside the range PS_PLL_VCO_MIN ~ PS_PLL_VCO_MAX */ + if (rate > PS_PLL_VCO_MAX) { + div = DIV_ROUND_UP(rate, PS_PLL_VCO_MAX); + rate = rate / div; + } + if (rate < PS_PLL_VCO_MIN) { + mult = DIV_ROUND_UP(PS_PLL_VCO_MIN, rate); + rate = rate * mult; } fbdiv = DIV_ROUND_CLOSEST(rate, *prate); - fbdiv = clamp_t(u32, fbdiv, PLL_FBDIV_MIN, PLL_FBDIV_MAX); - return *prate * fbdiv; + if (fbdiv < PLL_FBDIV_MIN || fbdiv > PLL_FBDIV_MAX) { + fbdiv = clamp_t(u32, fbdiv, PLL_FBDIV_MIN, PLL_FBDIV_MAX); + rate = *prate * fbdiv; + } + + return rate; } /** -- 2.35.1