Message ID | 20230912045157.177966-10-claudiu.beznea.uj@bp.renesas.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp394763vqx; Tue, 12 Sep 2023 06:09:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtd1IDdqpmAoBqRH92BHD7xTQouWjqnCxI2ofFRw72INfW9S2ixlKsHcaa5D82w3B7TmsT X-Received: by 2002:a17:902:a504:b0:1b8:76fc:5bf6 with SMTP id s4-20020a170902a50400b001b876fc5bf6mr11693650plq.43.1694524190065; Tue, 12 Sep 2023 06:09:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694524190; cv=none; d=google.com; s=arc-20160816; b=AW91/MBFe+OC/VtQq3kTLR7kkEqmury3v0ztsnNHcp3cKTxJpPKND/dhZSkiTVIeNh PoeCqaN46ky8QwKIcUvX3Lh18O0XKeNXgZ3rv82h8BhPqtLeNr1Rfys76ZyDpaSODzT6 SC4ujfIa7DRkBVlAaOo/9KSdeud9bntKzfnW4TqABwfadJ989/RKxMLKLj2WHy7gd9m9 N24hSiw1jImVTxmqLR92UhcrYrc1fL6s1OUFfWh/GPNd3VK6u2iUkS7dtsWXJw56RVYD Kqw4bvKPIibdK7MQwEoiyYVH0g5nvxSseJcNbgaB0iy5GEAeWLkAWJAc4HVnK+zNbWUS C23w== 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=hoCpw81yKfLo4VgDXItzmRw2QIwsF6c48cElOMC6EIw=; fh=7xbNZwTym+CKkW9darKiAIrlxwrHvdD9/+5rIv88u2Q=; b=z/otkFn4yDvYnqwLXIEjYHSPYuE0T42FL5ttUyLUMiPlXkBL1yhyQE3OVR4PUb1QZ2 uGyy2r6b23EmJwbql3uyIXyhp+hn79zUr7cA8MKh57x9jH4yJ/W9LzbErof8lliXHIGL WvT0xmryyy1ZHNOT4pQ+gWJQCt5YJlo5rreZ3HMuhSba0tGwZYASrdb1segbJqdwj34t 37T4OEGkqx3uMfcQ5o5E/ESKXwDUbWj1iCtgq6CvsD/FTWlDFq7K6XMSVBBE8dT5J9+6 xA9Cij9Y2TS6upWHrcce59R0YPiw48gmg2pZ3RAGZ1A4Gzu94vw6HJhhPJr8QOP/02uY DzpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tuxon.dev header.s=google header.b=efNOZzaz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id o13-20020a170902d4cd00b001c0dab08decsi2215915plg.499.2023.09.12.06.09.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:09:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@tuxon.dev header.s=google header.b=efNOZzaz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 6F9DA80C4791; Mon, 11 Sep 2023 21:54:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229793AbjILExt (ORCPT <rfc822;ruipengqi7@gmail.com> + 40 others); Tue, 12 Sep 2023 00:53:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230022AbjILEws (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 12 Sep 2023 00:52:48 -0400 Received: from mail-ed1-x530.google.com (mail-ed1-x530.google.com [IPv6:2a00:1450:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C73A3E75 for <linux-kernel@vger.kernel.org>; Mon, 11 Sep 2023 21:52:36 -0700 (PDT) Received: by mail-ed1-x530.google.com with SMTP id 4fb4d7f45d1cf-529fb04a234so6790134a12.3 for <linux-kernel@vger.kernel.org>; Mon, 11 Sep 2023 21:52:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tuxon.dev; s=google; t=1694494355; x=1695099155; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hoCpw81yKfLo4VgDXItzmRw2QIwsF6c48cElOMC6EIw=; b=efNOZzaz2mrdrYrVz2KZBe32hb1eSXlS2tFMQ4NMoahGA3XAxUHEviSgw/HMyGy0/B CEZNceT+KnwaHQFSGL3o5Ng8YedDWmHdMIo5DZiWau3V8csdEwc6aqSwS3uXRGY3No9q DOaKOTQezTL3Wmx+HByicQ+A+hGxccvel3g4l2KSQKDqPRG+5qgRHZPuxarKLWOFPJTp M9Zxw4YoxrConb/40UaNZpceX7tpnj/hiRC09kSjDxvTM9ftODKFuIVre6j0h5BNAH7f 0Dwe0+S6xrcrAosN+C6CfKzpZ/WEkBTQlh9IRRamQ0wCrzENLWoCwW9PtWEbychZXVEq YBwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694494355; x=1695099155; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hoCpw81yKfLo4VgDXItzmRw2QIwsF6c48cElOMC6EIw=; b=qfkHIKVBDm2asYzn9nRhkwPWvgMUM1GMb8fyWuEbzls3cz47BJD9YtdNe4Vjp4+aQL eBcXWWtod/wVVgMn/D3RELbGSVuNynZm2z2JKuXJtYKLvE46p0MHHQXb/dfM+JvAJUhT q0t1koBTJLHY70eNgMdGCidVC1VXXi71jB+8DvvqlhxSPnGpUIFMgeaPMugqpT+2Z/Bj Zb9w83JLMennDnciIFuPhALkgL5dXglHcio0qiyJuiT7Fij5YwMEb8NDT/R5ozVcEEpT hfX/UJIdPnGLc+j4xuOxwNv13LkQNypVR95o1DNzzM/cIzI3/dlpMBJ3cJ+874Ge+zzf QPOg== X-Gm-Message-State: AOJu0YzEyvyBxiovEJSvp2qEc5ev2S4U14FzY10iiUq/fJSxRUGw7/61 aw9/NYAsIccbPutUw9ofUDU3xQ== X-Received: by 2002:aa7:c48b:0:b0:522:1bdd:d41a with SMTP id m11-20020aa7c48b000000b005221bddd41amr9906905edq.4.1694494355421; Mon, 11 Sep 2023 21:52:35 -0700 (PDT) Received: from claudiu-X670E-Pro-RS.. ([82.78.167.145]) by smtp.gmail.com with ESMTPSA id f21-20020a05640214d500b0051e22660835sm5422415edx.46.2023.09.11.21.52.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Sep 2023 21:52:35 -0700 (PDT) From: Claudiu <claudiu.beznea@tuxon.dev> X-Google-Original-From: Claudiu <claudiu.beznea.uj@bp.renesas.com> To: geert+renesas@glider.be, mturquette@baylibre.com, sboyd@kernel.org, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, conor+dt@kernel.org, ulf.hansson@linaro.org, linus.walleij@linaro.org, gregkh@linuxfoundation.org, jirislaby@kernel.org, magnus.damm@gmail.com, catalin.marinas@arm.com, will@kernel.org, prabhakar.mahadev-lad.rj@bp.renesas.com, biju.das.jz@bp.renesas.com, quic_bjorande@quicinc.com, arnd@arndb.de, konrad.dybcio@linaro.org, neil.armstrong@linaro.org, nfraprado@collabora.com, rafal@milecki.pl, wsa+renesas@sang-engineering.com Cc: linux-renesas-soc@vger.kernel.org, linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mmc@vger.kernel.org, linux-gpio@vger.kernel.org, linux-serial@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> Subject: [PATCH 09/37] clk: renesas: rzg2l: fix computation formula Date: Tue, 12 Sep 2023 07:51:29 +0300 Message-Id: <20230912045157.177966-10-claudiu.beznea.uj@bp.renesas.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230912045157.177966-1-claudiu.beznea.uj@bp.renesas.com> References: <20230912045157.177966-1-claudiu.beznea.uj@bp.renesas.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 11 Sep 2023 21:54:18 -0700 (PDT) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776837396788012974 X-GMAIL-MSGID: 1776837396788012974 |
Series |
Add new Renesas RZ/G3S SoC and RZ/G3S SMARC EVK
|
|
Commit Message
claudiu beznea
Sept. 12, 2023, 4:51 a.m. UTC
From: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> According to hardware manual of RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf) the computation formula for PLL rate is as follows: Fout = ((m + k/65536) * Fin) / (p * 2^s) and k has values in range [-32768, 32767]. Dividing k by 65536 with integer variables leads all the time to zero. Thus we may have slight differences b/w what has been set vs. what is displayed. Thus, get rid of this and decompose the formula before dividing k by 65536. Fixes: ef3c613ccd68a ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> --- drivers/clk/renesas/rzg2l-cpg.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
Comments
Hi Claudiu, On Tue, Sep 12, 2023 at 6:52 AM Claudiu <claudiu.beznea@tuxon.dev> wrote: > From: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> > > According to hardware manual of RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf) > the computation formula for PLL rate is as follows: > > Fout = ((m + k/65536) * Fin) / (p * 2^s) > > and k has values in range [-32768, 32767]. Dividing k by 65536 with > integer variables leads all the time to zero. Thus we may have slight > differences b/w what has been set vs. what is displayed. Thus, > get rid of this and decompose the formula before dividing k by 65536. > > Fixes: ef3c613ccd68a ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") > Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> Thanks for your patch! > --- a/drivers/clk/renesas/rzg2l-cpg.c > +++ b/drivers/clk/renesas/rzg2l-cpg.c > @@ -696,18 +696,22 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, > struct pll_clk *pll_clk = to_pll(hw); > struct rzg2l_cpg_priv *priv = pll_clk->priv; > unsigned int val1, val2; > - unsigned int mult = 1; > - unsigned int div = 1; > + unsigned int div; > + u64 rate; > + s16 kdiv; > > if (pll_clk->type != CLK_TYPE_SAM_PLL) > return parent_rate; > > val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); > val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); > - mult = MDIV(val1) + KDIV(val1) / 65536; > + kdiv = KDIV(val1); > div = PDIV(val1) << SDIV(val2); > > - return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div); > + rate = (u64)MDIV(val1) * parent_rate; > + rate += ((long long)parent_rate * kdiv) / 65536; As the division is a binary shift, you can use the mul_u64_u32_shr() helper, and incorporate the sdiv shift at the same time: rate += mul_u64_u32_shr(parent_rate, KDIV(val1), 16 + SDIV(val2)); You can save a multiplication by premultiplying mdiv by 65536: rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16)) + KDIV(val1), 16 + SDIV(val2)); > + > + return DIV_ROUND_CLOSEST_ULL(rate, div); return DIV_ROUND_CLOSEST_ULL(rate, PDIV(val1)); > } > > static const struct clk_ops rzg2l_cpg_pll_ops = { Gr{oetje,eeting}s, Geert
Hi, Geert, On 14.09.2023 15:55, Geert Uytterhoeven wrote: > Hi Claudiu, > > On Tue, Sep 12, 2023 at 6:52 AM Claudiu <claudiu.beznea@tuxon.dev> wrote: >> From: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> >> >> According to hardware manual of RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf) >> the computation formula for PLL rate is as follows: >> >> Fout = ((m + k/65536) * Fin) / (p * 2^s) >> >> and k has values in range [-32768, 32767]. Dividing k by 65536 with >> integer variables leads all the time to zero. Thus we may have slight >> differences b/w what has been set vs. what is displayed. Thus, >> get rid of this and decompose the formula before dividing k by 65536. >> >> Fixes: ef3c613ccd68a ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") >> Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> > > Thanks for your patch! > >> --- a/drivers/clk/renesas/rzg2l-cpg.c >> +++ b/drivers/clk/renesas/rzg2l-cpg.c >> @@ -696,18 +696,22 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, >> struct pll_clk *pll_clk = to_pll(hw); >> struct rzg2l_cpg_priv *priv = pll_clk->priv; >> unsigned int val1, val2; >> - unsigned int mult = 1; >> - unsigned int div = 1; >> + unsigned int div; >> + u64 rate; >> + s16 kdiv; >> >> if (pll_clk->type != CLK_TYPE_SAM_PLL) >> return parent_rate; >> >> val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); >> val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); >> - mult = MDIV(val1) + KDIV(val1) / 65536; >> + kdiv = KDIV(val1); >> div = PDIV(val1) << SDIV(val2); >> >> - return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div); >> + rate = (u64)MDIV(val1) * parent_rate; >> + rate += ((long long)parent_rate * kdiv) / 65536; > > As the division is a binary shift, you can use the mul_u64_u32_shr() helper, > and incorporate the sdiv shift at the same time: > > rate += mul_u64_u32_shr(parent_rate, KDIV(val1), 16 + SDIV(val2)); > > You can save a multiplication by premultiplying mdiv by 65536: > > rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16)) + KDIV(val1), > 16 + SDIV(val2)); Looking again at this: KDIV (aka DIV_K) could have negative values thus mul_u64_u32_shr() cannot be used here. > >> + >> + return DIV_ROUND_CLOSEST_ULL(rate, div); > > return DIV_ROUND_CLOSEST_ULL(rate, PDIV(val1)); > >> } >> >> static const struct clk_ops rzg2l_cpg_pll_ops = { > > Gr{oetje,eeting}s, > > Geert >
Hi Claudiu, On Tue, Sep 26, 2023 at 1:47 PM claudiu beznea <claudiu.beznea@tuxon.dev> wrote: > On 14.09.2023 15:55, Geert Uytterhoeven wrote: > > On Tue, Sep 12, 2023 at 6:52 AM Claudiu <claudiu.beznea@tuxon.dev> wrote: > >> From: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> > >> > >> According to hardware manual of RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf) > >> the computation formula for PLL rate is as follows: > >> > >> Fout = ((m + k/65536) * Fin) / (p * 2^s) > >> > >> and k has values in range [-32768, 32767]. Dividing k by 65536 with > >> integer variables leads all the time to zero. Thus we may have slight > >> differences b/w what has been set vs. what is displayed. Thus, > >> get rid of this and decompose the formula before dividing k by 65536. > >> > >> Fixes: ef3c613ccd68a ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") > >> Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> > > > > Thanks for your patch! > > > >> --- a/drivers/clk/renesas/rzg2l-cpg.c > >> +++ b/drivers/clk/renesas/rzg2l-cpg.c > >> @@ -696,18 +696,22 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, > >> struct pll_clk *pll_clk = to_pll(hw); > >> struct rzg2l_cpg_priv *priv = pll_clk->priv; > >> unsigned int val1, val2; > >> - unsigned int mult = 1; > >> - unsigned int div = 1; > >> + unsigned int div; > >> + u64 rate; > >> + s16 kdiv; > >> > >> if (pll_clk->type != CLK_TYPE_SAM_PLL) > >> return parent_rate; > >> > >> val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); > >> val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); > >> - mult = MDIV(val1) + KDIV(val1) / 65536; > >> + kdiv = KDIV(val1); > >> div = PDIV(val1) << SDIV(val2); > >> > >> - return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div); > >> + rate = (u64)MDIV(val1) * parent_rate; > >> + rate += ((long long)parent_rate * kdiv) / 65536; > > > > As the division is a binary shift, you can use the mul_u64_u32_shr() helper, > > and incorporate the sdiv shift at the same time: > > > > rate += mul_u64_u32_shr(parent_rate, KDIV(val1), 16 + SDIV(val2)); [1]^ > > > > You can save a multiplication by premultiplying mdiv by 65536: > > > > rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16)) + KDIV(val1), > > 16 + SDIV(val2)); [2]^ > > Looking again at this: KDIV (aka DIV_K) could have negative values thus > mul_u64_u32_shr() cannot be used here. That means you can indeed not use [1]. But you can still use [2], as MDIV() must be in the range 64..533[3], so "(MDIV(val1) << 16)) + (s16)KDIV(val1)" is always positive. Note that you do need the cast to s16 (which I had missed before), or the intermediate variable kdiv of type s16 (like in your patch). [3] As the current PLL driver is read-only, there is no calculation or validation of the PLL parameters. > >> + > >> + return DIV_ROUND_CLOSEST_ULL(rate, div); > > > > return DIV_ROUND_CLOSEST_ULL(rate, PDIV(val1)); Gr{oetje,eeting}s, Geert
Hi Claudiu, On Tue, Sep 26, 2023 at 4:44 PM Geert Uytterhoeven <geert@linux-m68k.org> wrote: > On Tue, Sep 26, 2023 at 1:47 PM claudiu beznea <claudiu.beznea@tuxon.dev> wrote: > > On 14.09.2023 15:55, Geert Uytterhoeven wrote: > > > On Tue, Sep 12, 2023 at 6:52 AM Claudiu <claudiu.beznea@tuxon.dev> wrote: > > >> From: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> > > >> > > >> According to hardware manual of RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf) > > >> the computation formula for PLL rate is as follows: > > >> > > >> Fout = ((m + k/65536) * Fin) / (p * 2^s) > > >> > > >> and k has values in range [-32768, 32767]. Dividing k by 65536 with > > >> integer variables leads all the time to zero. Thus we may have slight > > >> differences b/w what has been set vs. what is displayed. Thus, > > >> get rid of this and decompose the formula before dividing k by 65536. > > >> > > >> Fixes: ef3c613ccd68a ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") > > >> Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> > > > > > > Thanks for your patch! > > > > > >> --- a/drivers/clk/renesas/rzg2l-cpg.c > > >> +++ b/drivers/clk/renesas/rzg2l-cpg.c > > >> @@ -696,18 +696,22 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, > > >> struct pll_clk *pll_clk = to_pll(hw); > > >> struct rzg2l_cpg_priv *priv = pll_clk->priv; > > >> unsigned int val1, val2; > > >> - unsigned int mult = 1; > > >> - unsigned int div = 1; > > >> + unsigned int div; > > >> + u64 rate; > > >> + s16 kdiv; > > >> > > >> if (pll_clk->type != CLK_TYPE_SAM_PLL) > > >> return parent_rate; > > >> > > >> val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); > > >> val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); > > >> - mult = MDIV(val1) + KDIV(val1) / 65536; > > >> + kdiv = KDIV(val1); > > >> div = PDIV(val1) << SDIV(val2); > > >> > > >> - return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div); > > >> + rate = (u64)MDIV(val1) * parent_rate; > > >> + rate += ((long long)parent_rate * kdiv) / 65536; > > > > > > As the division is a binary shift, you can use the mul_u64_u32_shr() helper, > > > and incorporate the sdiv shift at the same time: > > > > > > rate += mul_u64_u32_shr(parent_rate, KDIV(val1), 16 + SDIV(val2)); > > [1]^ > > > > > > > You can save a multiplication by premultiplying mdiv by 65536: > > > > > > rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16)) + KDIV(val1), > > > 16 + SDIV(val2)); > > [2]^ > > > > > Looking again at this: KDIV (aka DIV_K) could have negative values thus > > mul_u64_u32_shr() cannot be used here. > > That means you can indeed not use [1]. > > But you can still use [2], as MDIV() must be in the range 64..533[3], > so "(MDIV(val1) << 16)) + (s16)KDIV(val1)" is always positive. > Note that you do need the cast to s16 (which I had missed before), or > the intermediate variable kdiv of type s16 (like in your patch). Or include the cast to a signed type in the definition of KDIV(). Gr{oetje,eeting}s, Geert
Hi, Geert, On 27.09.2023 11:00, Geert Uytterhoeven wrote: > Hi Claudiu, > > On Tue, Sep 26, 2023 at 4:44 PM Geert Uytterhoeven <geert@linux-m68k.org> wrote: >> On Tue, Sep 26, 2023 at 1:47 PM claudiu beznea <claudiu.beznea@tuxon.dev> wrote: >>> On 14.09.2023 15:55, Geert Uytterhoeven wrote: >>>> On Tue, Sep 12, 2023 at 6:52 AM Claudiu <claudiu.beznea@tuxon.dev> wrote: >>>>> From: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> >>>>> >>>>> According to hardware manual of RZ/G2L (r01uh0914ej0130-rzg2l-rzg2lc.pdf) >>>>> the computation formula for PLL rate is as follows: >>>>> >>>>> Fout = ((m + k/65536) * Fin) / (p * 2^s) >>>>> >>>>> and k has values in range [-32768, 32767]. Dividing k by 65536 with >>>>> integer variables leads all the time to zero. Thus we may have slight >>>>> differences b/w what has been set vs. what is displayed. Thus, >>>>> get rid of this and decompose the formula before dividing k by 65536. >>>>> >>>>> Fixes: ef3c613ccd68a ("clk: renesas: Add CPG core wrapper for RZ/G2L SoC") >>>>> Signed-off-by: Claudiu Beznea <claudiu.beznea.uj@bp.renesas.com> >>>> >>>> Thanks for your patch! >>>> >>>>> --- a/drivers/clk/renesas/rzg2l-cpg.c >>>>> +++ b/drivers/clk/renesas/rzg2l-cpg.c >>>>> @@ -696,18 +696,22 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, >>>>> struct pll_clk *pll_clk = to_pll(hw); >>>>> struct rzg2l_cpg_priv *priv = pll_clk->priv; >>>>> unsigned int val1, val2; >>>>> - unsigned int mult = 1; >>>>> - unsigned int div = 1; >>>>> + unsigned int div; >>>>> + u64 rate; >>>>> + s16 kdiv; >>>>> >>>>> if (pll_clk->type != CLK_TYPE_SAM_PLL) >>>>> return parent_rate; >>>>> >>>>> val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); >>>>> val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); >>>>> - mult = MDIV(val1) + KDIV(val1) / 65536; >>>>> + kdiv = KDIV(val1); >>>>> div = PDIV(val1) << SDIV(val2); >>>>> >>>>> - return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div); >>>>> + rate = (u64)MDIV(val1) * parent_rate; >>>>> + rate += ((long long)parent_rate * kdiv) / 65536; >>>> >>>> As the division is a binary shift, you can use the mul_u64_u32_shr() helper, >>>> and incorporate the sdiv shift at the same time: >>>> >>>> rate += mul_u64_u32_shr(parent_rate, KDIV(val1), 16 + SDIV(val2)); >> >> [1]^ >> >>>> >>>> You can save a multiplication by premultiplying mdiv by 65536: >>>> >>>> rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16)) + KDIV(val1), >>>> 16 + SDIV(val2)); >> >> [2]^ >> >>> >>> Looking again at this: KDIV (aka DIV_K) could have negative values thus >>> mul_u64_u32_shr() cannot be used here. >> >> That means you can indeed not use [1]. You're right. Thanks for the input! >> >> But you can still use [2], as MDIV() must be in the range 64..533[3], >> so "(MDIV(val1) << 16)) + (s16)KDIV(val1)" is always positive. >> Note that you do need the cast to s16 (which I had missed before), or >> the intermediate variable kdiv of type s16 (like in your patch). > > Or include the cast to a signed type in the definition of KDIV(). > > Gr{oetje,eeting}s, > > Geert >
diff --git a/drivers/clk/renesas/rzg2l-cpg.c b/drivers/clk/renesas/rzg2l-cpg.c index d0d086d6dc51..b391c9548421 100644 --- a/drivers/clk/renesas/rzg2l-cpg.c +++ b/drivers/clk/renesas/rzg2l-cpg.c @@ -696,18 +696,22 @@ static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, struct pll_clk *pll_clk = to_pll(hw); struct rzg2l_cpg_priv *priv = pll_clk->priv; unsigned int val1, val2; - unsigned int mult = 1; - unsigned int div = 1; + unsigned int div; + u64 rate; + s16 kdiv; if (pll_clk->type != CLK_TYPE_SAM_PLL) return parent_rate; val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); - mult = MDIV(val1) + KDIV(val1) / 65536; + kdiv = KDIV(val1); div = PDIV(val1) << SDIV(val2); - return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, div); + rate = (u64)MDIV(val1) * parent_rate; + rate += ((long long)parent_rate * kdiv) / 65536; + + return DIV_ROUND_CLOSEST_ULL(rate, div); } static const struct clk_ops rzg2l_cpg_pll_ops = {