Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3079244rwa; Mon, 22 Aug 2022 21:00:38 -0700 (PDT) X-Google-Smtp-Source: AA6agR5ec0tkbLzgI+g+vczHrGVcArhL6at/4oWrTvuZ4Nc5buvTELI0pTYGDSjp/jgi0mxxRulB X-Received: by 2002:a05:6a00:17aa:b0:537:27e:625b with SMTP id s42-20020a056a0017aa00b00537027e625bmr813909pfg.12.1661227237900; Mon, 22 Aug 2022 21:00:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661227237; cv=none; d=google.com; s=arc-20160816; b=ybMYTBUbK8jFCs7X/rPGd44pTfRC2j11an0JObpZQ98yOrC0tlLI4AvxIzwgir9e7P q5L6fQ9G3zUof2DhZwUBRUf2Jp9/kOiH8KZwkr0FqomM8gN82oVDKXddORJmLQWUschh KaL4UQycZWVDx7y/1DmaD5VWJrJP1fxWKMTxT+bcQz/w5QBPS3v4HoxZrdfhItS3JaBb HH90YsyXFU0yNbdsxZMKQ67J4ZVlTUR9G8qOrVCGZgFw6XFQbRNl4ruYaE044E5Q888C gBlLiMwkJXOAvDQeXVEhS92PKovpKTSuw0dpPerf9PsHJ4UhoHRUcLTvNQHe/kRMmton YyvQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=gm8nv1A1HcUSnQ4KZ2XBswRy9fFO4PeMLiDQLhSlOoo=; b=jvByuIWAFm1Isn8+5CojHaMfSuNs2nmPTT7DTG8QGkS5FTFAlDs8gRPQfDuyat/wq+ 9Kh7YTrW+Ual9eONlwpDt04f4lk1Eo4ynTerFcGXMDaZntLlH8Z5I4agSJbuyvW0Qf2V F36wsgIN1dHosOvmCUn8tr6de90d3yPiunhwEciwbfql1BNlNeoskZLk8hVGjEPz+byF OE1TWa3AwgUEloCdVHIyZE7o2aC7KRb9wW3m3qAOcPyIFiz5+2YZ1CyE2YzGUQwMqLuF w22Zcxb3Y0vohf+ldJ52cR7x41p/Un7xzfoyN8rc7SHGAyx2k0XVOBqsRCYamDpUrgEv drrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=IQjqzc+K; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 5-20020a631445000000b00419b2750266si15184747pgu.845.2022.08.22.21.00.26; Mon, 22 Aug 2022 21:00:37 -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=@gmail.com header.s=20210112 header.b=IQjqzc+K; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240097AbiHWDeW (ORCPT + 99 others); Mon, 22 Aug 2022 23:34:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234556AbiHWDeU (ORCPT ); Mon, 22 Aug 2022 23:34:20 -0400 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7D4932AA3; Mon, 22 Aug 2022 20:34:17 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id g16so9406364qkl.11; Mon, 22 Aug 2022 20:34:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=gm8nv1A1HcUSnQ4KZ2XBswRy9fFO4PeMLiDQLhSlOoo=; b=IQjqzc+KSmruImmSPXbpoKrGOJlGjBg8YJEsxTyXvk3j4YkbpGJ65ekZNg/B+EIEnE 4jSMwRwB29H7E1OdhN9XvNW3jsWsSo/rhSK14FPXynq/A4AzKC1QtuvV8wHfR2x+YG9V tdjHltSvvJiJwvaFxMDIXeKPlySSy80pvXDOSWfNJe9vcesfLUmfIc/yuIWFS1+SbGA3 jCJ+BTX1c6MARZqWX7wUOmiEhptNibKbhUz4Qwdk3nf/Njv+MI/OeVs3cuvF5Uqi9o3e ++YE/tCjSOlMst1NwOIo2+yc5y74ORyUqGKW2b0K5UXO8329kBvLKh9WMdm+/90SbreM 1oqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=gm8nv1A1HcUSnQ4KZ2XBswRy9fFO4PeMLiDQLhSlOoo=; b=JIHFu9dOm0GK8WOBpR2jpd4x8dRLZQnanuGjhYwkkKozM2n0BRYyxFGWA1tnErNT36 mS03weIXcF2WiP6g6nkj2JEz1isO1MqCOZ/VCmHPNUW7aMw7avzfEEQhK9MGc8vTRaI3 ch1NZdFilBxwAyah6E704S1B5wZdfU0cdBZ4g1HZacf+g3yMDrNHs6obYuL5o1FYCphS qIU7VfwXyZHRbeTpc9DaF3gvuQM/k5ngrddjZgG7fHtC3d0pbcpvbhkysRU5OqOLoKlB 3ixuUoiTKXsAsDrnVB4CZga5fTWoF1nNIdPZIpcJtpI22ypq/qFTJ/939WLhoIM6YiDO /sxA== X-Gm-Message-State: ACgBeo1QTFh3LGkQytgu6MCYUdinTs5aMlQ0HfXrP4VR9aZCFKdT5ibd EOUUip5I+VZBGtwasQ1b1DJHhwefkrs= X-Received: by 2002:a37:2ec6:0:b0:6bb:2ee3:4519 with SMTP id u189-20020a372ec6000000b006bb2ee34519mr14762140qkh.178.1661225656614; Mon, 22 Aug 2022 20:34:16 -0700 (PDT) Received: from localhost (pool-173-73-95-180.washdc.fios.verizon.net. [173.73.95.180]) by smtp.gmail.com with UTF8SMTPSA id j20-20020a05620a411400b006b9a24dc9d7sm12084640qko.7.2022.08.22.20.34.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 22 Aug 2022 20:34:16 -0700 (PDT) From: Sean Anderson To: linux-mips@vger.kernel.org, linux-clk@vger.kernel.org Cc: Yang Ling , linux-kernel@vger.kernel.org, Kelvin Cheung , Stephen Boyd , Du Huanpeng , Sean Anderson Subject: [RESEND PATCH] clk: ls1c: Fix PLL rate calculation Date: Mon, 22 Aug 2022 23:34:14 -0400 Message-Id: <20220823033414.198525-1-seanga2@gmail.com> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 While reviewing Dhu's patch adding ls1c300 clock support to U-Boot [1], I noticed the following calculation, which is copied from drivers/clk/loongson1/clk-loongson1c.c: ulong ls1c300_pll_get_rate(struct clk *clk) { unsigned int mult; long long parent_rate; void *base; unsigned int val; parent_rate = clk_get_parent_rate(clk); base = (void *)clk->data; val = readl(base + START_FREQ); mult = FIELD_GET(FRAC_N, val) + FIELD_GET(M_PLL, val); return (mult * parent_rate) / 4; } I would like to examine the use of M_PLL and FRAC_N to calculate the multiplier for the PLL. The datasheet has the following to say: START_FREQ 位 缺省值 描述 ========== ===== =========== ==================================== FRAC_N 23:16 0 PLL 倍频系数的小数部分 由 PLL 倍频系数的整数部分 M_PLL 15:8 NAND_D[3:0] (理论可以达到 255,建议不要超过 100) 配置 which according to google translate means START_FREQ Bits Default Description ========== ===== ============= ================================================ FRAC_N 23:16 0 Fractional part of the PLL multiplication factor Depends on Integer part of PLL multiplication factor M_PLL 15:8 NAND_D[3:0] (Theoretically it can reach 255, [but] it is configuration recommended not to exceed 100) So just based on this description, I would expect that the formula to be something like rate = parent * (256 * M_PLL + FRAC_N) / 256 / 4 However, the datasheet also gives the following formula: rate = parent * (M_PLL + FRAC_N) / 4 which is what the Linux driver has implemented. I find this very unusual. First, the datasheet specifically says that these fields are the integer and fractional parts of the multiplier. Second, I think such a construct does not easily map to traditional PLL building blocks. Implementing this formula in hardware would likely require an adder, just to then set the threshold of a clock divider. I think it is much more likely that the first formula is correct. The author of the datasheet may think of a multiplier of (say) 3.14 as M_PLL = 3 FRAC_N = 0.14 which together sum to the correct multiplier, even though the actual value stored in FRAC_N would be 36. I suspect that this has slipped by unnoticed because when FRAC_N is 0, there is no difference in the formulae. The following patch is untested, but I suspect it will fix this issue. I would appreciate if anyone with access to the hardware could measure the output of the PLL (or one of its derived clocks) and determine the correct formula. [1] https://lore.kernel.org/u-boot/20220418204519.19991-1-dhu@hodcarrier.org/T/#u Fixes: b4626a7f4892 ("CLK: Add Loongson1C clock support") Signed-off-by: Sean Anderson --- drivers/clk/loongson1/clk-loongson1c.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/clk/loongson1/clk-loongson1c.c b/drivers/clk/loongson1/clk-loongson1c.c index 1ebf740380ef..2aa839b05d6b 100644 --- a/drivers/clk/loongson1/clk-loongson1c.c +++ b/drivers/clk/loongson1/clk-loongson1c.c @@ -21,9 +21,9 @@ static unsigned long ls1x_pll_recalc_rate(struct clk_hw *hw, u32 pll, rate; pll = __raw_readl(LS1X_CLK_PLL_FREQ); - rate = ((pll >> 8) & 0xff) + ((pll >> 16) & 0xff); + rate = (pll & 0xff00) + ((pll >> 16) & 0xff); rate *= OSC; - rate >>= 2; + rate >>= 10; return rate; } -- 2.37.1