From patchwork Mon Oct 24 11:32:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9155 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp457765wru; Mon, 24 Oct 2022 06:36:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4boMZA83Hc1ihJPtOPeiRYHJzopUSxm6J7UxV14fCOU7ZGYbWe/YCd4SwaGHhGcx9kAhof X-Received: by 2002:a17:906:5a6b:b0:7a0:b42d:f17a with SMTP id my43-20020a1709065a6b00b007a0b42df17amr10540748ejc.130.1666618589607; Mon, 24 Oct 2022 06:36:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666618589; cv=none; d=google.com; s=arc-20160816; b=IIY5JxM1dQ3/lSgPzq3iU1UtKlHDeUhUMbeGRpRP58N7wT5J4JErFZb/S8X1Lybamu V25nMtbzGuEVvFAerwrJ++oKYzQT6TwVYPHGY3gA+bj2sgApjNjmWhEAyvMtWqiGyBLY N921hsH66m8gTIhwCGwWtlGIlt2Lwb5Uputky/i7DqH5QvgwV4kv80Z6CJE/GpwXDGoJ D+FQv9B9Et46D9JJoP9FzWDGOwURoFzjE+DEg9W0627UYlvcd/6QZxi8S++78ZpTtyMS 1u7IjVEvjgPTZz8EqhpXcExsvWW6cOG67kZV9lnW3FDsfujgmVxRYLA4E+k4fDHOJInj je3Q== 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=nlT7iIWNxctPpkvJqbGorPPW0fFbU9yHyvOQqS4Gyt8=; b=Am3h+X8wKDjOgq0QUBrJw7Ic3t50BZGA/X3p4SiLyNb7a+7wY+jIYExLwcqOByRj37 imu3SzbdI/2933E2WePDEcCe++oiSFySSFNzgsbVWut/IRjRBTQK4zOKnuxd1k6AZB5u HRp0Ne5IwKhYiujCUNRsjWP7LO7/vZgEkwEgBdq0OYtB1O7kQsc0/1YRx9QoUSqm63F1 +VckGg59UQky3FtrX4gso35IqW97F8MZN52KP2OP6KaB+Xq/F/r18FIaPZPba+UqFgeu g4yuCqsAB4i22ALYMMmHyuOzirrzUBBsGrXgDjDbn0MUMrJbYCIqvgNFnRHAxzS7I7Tk z26A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vYQRJjFF; 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=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hq6-20020a1709073f0600b00787c0e9818csi32871961ejc.568.2022.10.24.06.35.26; Mon, 24 Oct 2022 06:36:29 -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=@linuxfoundation.org header.s=korg header.b=vYQRJjFF; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235929AbiJXNVf (ORCPT + 99 others); Mon, 24 Oct 2022 09:21:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236402AbiJXNUW (ORCPT ); Mon, 24 Oct 2022 09:20:22 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA61D86F92; Mon, 24 Oct 2022 05:28:51 -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 4CC38612E3; Mon, 24 Oct 2022 12:14:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C245C433D6; Mon, 24 Oct 2022 12:14:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613679; bh=Ku+s+zE2JUi5LkzFKWab//xnKW+xerFGSit2jWXOfwA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vYQRJjFF4O2Qjo9wdVctobCFU6ggwiNC4aWucvb7Vx2BDua8yknXEeYyIHTz3ue88 QWRdZtnPfElUQr47Be3YknlCSJM1Van1KoZrfmYWgzAdr5o7kKWPnNi26Bz8o36qOl skF7infBs9J2aABq1513ZWumbMhj5Uliqwo5UDKk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Quanyang Wang , Shubhrajyoti Datta , Stephen Boyd , Sasha Levin Subject: [PATCH 5.4 229/255] clk: zynqmp: pll: rectify rate rounding in zynqmp_pll_round_rate Date: Mon, 24 Oct 2022 13:32:19 +0200 Message-Id: <20221024113010.749178104@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113002.471093005@linuxfoundation.org> References: <20221024113002.471093005@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747576254227142402?= X-GMAIL-MSGID: =?utf-8?q?1747576254227142402?= 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 18fee827602a..3a2a694e5bf3 100644 --- a/drivers/clk/zynqmp/pll.c +++ b/drivers/clk/zynqmp/pll.c @@ -98,26 +98,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; } /**