From patchwork Sat Sep 11 17:20:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1526779 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=GpaxnzsS; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H6KJQ1mGxz9sVw for ; Sun, 12 Sep 2021 03:20:35 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AF6318381F; Sat, 11 Sep 2021 19:20:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="GpaxnzsS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2940B83837; Sat, 11 Sep 2021 19:20:19 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-qv1-xf33.google.com (mail-qv1-xf33.google.com [IPv6:2607:f8b0:4864:20::f33]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E28088381F for ; Sat, 11 Sep 2021 19:20:14 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qv1-xf33.google.com with SMTP id s16so3393448qvt.13 for ; Sat, 11 Sep 2021 10:20:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=T4TmOnV0K1K9iwaEWDu3vQwDCbNbijYHv5aaH/3PvdI=; b=GpaxnzsSwxMahaQb1O6hM43LrnvyQZLqFj9AbuMJungdaDNtcTeM/S1Ys0rH5v72px TfvR0Sy5bDNfFh045XUM0rNpbY0zYv0v4DZXI+ldcCKn6+DvpYUFkWLs3AypjcWgrlGL 7HauhoYy59dZ3ALL/NjIy/1b8APwsRH8DG97vkfHmoJED/xhCEKSRQLns1/at44ht+Ge gee3+jjM6f+Ezhjd4erm0RVuV6fcAkDpqyErnkbbNrhg6zWBit1q2IXNZvEnmGPZWDg+ rSfFEwHKMJLgQVi5ystgNlFKLuZUtUMHM16KdTLk8aiagPWj+dkT3pSlVSMBCVT5uZno /07w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=T4TmOnV0K1K9iwaEWDu3vQwDCbNbijYHv5aaH/3PvdI=; b=v/WU5pmTL2jDRdl7TS1tsUnCHfHyrw0N01xLzLByG8rHGEku2clPP1pqbHwBKrrEt4 67KlTsEzP1P71VE82/JXmLcME8xHnO9yXzU5vQ06UCdPPGU4WDBfdOVYJnmyWtTjNs7P kCqh1N6w4OBkJ20ckT1AJoPqXnB5r3F/VKLbdImiuScmwmBo6IttN0HYV53O/2J1tck7 jAkzjqMK3SI8AbDg/zLko8j07YrW/ds2kbrIv/JndN+/ulfCOQDtA/ckCiNBcZLDQven ELtfoo7si1guQ6lb9xM0Mok/aLBanrXzaI/m01YMdsFiByRXSpRk91V6q5v07STvUHm8 cw9w== X-Gm-Message-State: AOAM530gTduZ412JIuo1qYP7NqsguGwf1Ehe2SShQt2glzM1MTyfgzSA WwsEp8R0DOkkG0lzWXPs20HFNX8seJQ= X-Google-Smtp-Source: ABdhPJwfzh2DrhtrjgytrTDgglb8wI3ks8CYA33pfCYFShe/5uL7xblhGKyTQLR+cqTLCTUHYbYTEQ== X-Received: by 2002:a05:6214:3f1:: with SMTP id cf17mr2840911qvb.57.1631380813532; Sat, 11 Sep 2021 10:20:13 -0700 (PDT) Received: from godwin.fios-router.home (pool-108-45-127-224.washdc.fios.verizon.net. [108.45.127.224]) by smtp.gmail.com with ESMTPSA id d12sm1295669qtq.61.2021.09.11.10.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Sep 2021 10:20:12 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Leo Liang Cc: Bin Meng , Damien Le Moal , Lukasz Majewski , Sean Anderson , Coverity Scan Subject: [PATCH v2 1/4] clk: k210: Fix checking if ulongs are less than 0 Date: Sat, 11 Sep 2021 13:20:00 -0400 Message-Id: <20210911172003.899301-1-seanga2@gmail.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean The PLL functions take ulong arguments for rate, but still check if that rate is negative (which is never true). The correct way to handle this is to use IS_ERR_VALUE (like is already done in k210_clk_set_rate). While we're at it, we can move the error checking up into the caller of the pll set/get rate functions. This also protects our other calculations from using bogus values for rate. Fixes: 609bd60b94 ("clk: k210: Rewrite to remove CCF") Reported-by: Coverity Scan Signed-off-by: Sean Anderson Reviewed-by: Leo Yu-Chi Liang --- Changes in v2: - Reworked patch to use IS_ERR_VALUE instead of changing arguments to long drivers/clk/clk_kendryte.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/clk/clk_kendryte.c b/drivers/clk/clk_kendryte.c index 3148756968..2caa21aec9 100644 --- a/drivers/clk/clk_kendryte.c +++ b/drivers/clk/clk_kendryte.c @@ -849,9 +849,6 @@ static ulong k210_pll_set_rate(struct k210_clk_priv *priv, int id, ulong rate, u32 reg; ulong calc_rate; - if (rate_in < 0) - return rate_in; - err = k210_pll_calc_config(rate, rate_in, &config); if (err) return err; @@ -895,7 +892,7 @@ static ulong k210_pll_get_rate(struct k210_clk_priv *priv, int id, u64 r, f, od; u32 reg = readl(priv->base + k210_plls[id].off); - if (rate_in < 0 || (reg & K210_PLL_BYPASS)) + if (reg & K210_PLL_BYPASS) return rate_in; if (!(reg & K210_PLL_PWRD)) @@ -1029,6 +1026,8 @@ static ulong do_k210_clk_get_rate(struct k210_clk_priv *priv, int id) parent = k210_clk_get_parent(priv, id); parent_rate = do_k210_clk_get_rate(priv, parent); + if (IS_ERR_VALUE(parent_rate)) + return parent_rate; if (k210_clks[id].flags & K210_CLKF_PLL) return k210_pll_get_rate(priv, k210_clks[id].pll, parent_rate); @@ -1099,6 +1098,8 @@ static ulong k210_clk_set_rate(struct clk *clk, unsigned long rate) parent = k210_clk_get_parent(priv, clk->id); rate_in = do_k210_clk_get_rate(priv, parent); + if (IS_ERR_VALUE(rate_in)) + return rate_in; log_debug("id=%ld rate=%lu rate_in=%lu\n", clk->id, rate, rate_in); From patchwork Sat Sep 11 17:20:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1526780 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=QWHZ6Z5s; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H6KJZ4Z4Dz9sVw for ; Sun, 12 Sep 2021 03:20:46 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 562D58382C; Sat, 11 Sep 2021 19:20:31 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="QWHZ6Z5s"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4E8AD83831; Sat, 11 Sep 2021 19:20:28 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-qv1-xf2d.google.com (mail-qv1-xf2d.google.com [IPv6:2607:f8b0:4864:20::f2d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D3DD083830 for ; Sat, 11 Sep 2021 19:20:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qv1-xf2d.google.com with SMTP id w8so3436084qvt.0 for ; Sat, 11 Sep 2021 10:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=g9gFNk0XwB9UcMRXGcFxmIjs9p/aA9OW2a9fHvdZJ9A=; b=QWHZ6Z5ssYMcKy4c+19Qk5TuMwh+dLepp8fks8QYI/EW7Kpxn/Wkw5thdyWYbWCtFG s66bBz1ACO+hQNhiH1Hsxg3KHAQSANseLwrB1kydYToJuYC/3WBEB7KReQGqk7FAKCbe i0pEZ9ZeOF/auHQSmz4SSTsZnfWgqiwv6xj6VWzHCIwAapV+qAWDirL7rik/e1+BfTyh LX7BiNAfFSvyA45wMWqMNjx1O9jfDKhbhVYqXyze3aFxwRKDu9UNoGMUI1vyRT19Bei5 a1c/ADzuih1sZJmYhor9QcMr3KXCFLYeClDyLqED6F2dkNORDpqgbfyebaBIBt+PN4/E WV7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=g9gFNk0XwB9UcMRXGcFxmIjs9p/aA9OW2a9fHvdZJ9A=; b=nO8sIg4W9Kk21cwgKYkTgol5g1MN/Kz1UfGMLCbYsRQiC6r3mmvgS+i1t9AQI1rzjp DKt4jATWRQZA7YAYjr/Df1vQOwLdWof1T6QZgB//fRyPqgY+PZxbdrGN36yp2YXyb/Rf Y0UKFPqoreMzG7C88qgLbFFasx/Pmn1evdjXI7var24v4mYTDhi4J8oEzGfvFKOMxc6L JhHftRcV7Z/02E9pnnHAG+OKa5MosMZkgQiu00CNHU5XOwy73SyKkADJG0AMwnR/Cavo qKFAVTZwxL/wvhcxl7yQ3CH+qlAsG78yn6u2ICIdwlvprVAxazC7TURdiv+TAW96APFz b0Lw== X-Gm-Message-State: AOAM531QMZUB+L+XP2V1wj9gBzBXlWtATmfgKtI072lp785puahGdtVm 2t5Fif4GESKo6+6GOhvKriRyXY4Bt20= X-Google-Smtp-Source: ABdhPJw860w4xyFPLR6s1Wj94g7h7b9ZzCnqY2dSzLTcJn3pp57DITGyt6eHtqGtJ5D8M3Zu5Yb+Ag== X-Received: by 2002:a0c:f38b:: with SMTP id i11mr3004057qvk.42.1631380814281; Sat, 11 Sep 2021 10:20:14 -0700 (PDT) Received: from godwin.fios-router.home (pool-108-45-127-224.washdc.fios.verizon.net. [108.45.127.224]) by smtp.gmail.com with ESMTPSA id d12sm1295669qtq.61.2021.09.11.10.20.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Sep 2021 10:20:13 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Leo Liang Cc: Bin Meng , Damien Le Moal , Lukasz Majewski , Sean Anderson Subject: [PATCH v2 2/4] k210: clk: Refactor out_of_spec tests Date: Sat, 11 Sep 2021 13:20:01 -0400 Message-Id: <20210911172003.899301-2-seanga2@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210911172003.899301-1-seanga2@gmail.com> References: <20210911172003.899301-1-seanga2@gmail.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Everything here sits in a while (true) loop. However, this introduces a couple of layers of indentation. We can simplify the code by introducing a single goto instead of using continue/break. This will also make adding loops in the next patch easier. Signed-off-by: Sean Anderson Reviewed-by: Leo Yu-Chi Liang --- (no changes since v1) drivers/clk/clk_kendryte.c | 105 ++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/drivers/clk/clk_kendryte.c b/drivers/clk/clk_kendryte.c index 2caa21aec9..69691c4a04 100644 --- a/drivers/clk/clk_kendryte.c +++ b/drivers/clk/clk_kendryte.c @@ -709,6 +709,10 @@ TEST_STATIC int k210_pll_calc_config(u32 rate, u32 rate_in, * Whether we swapped r and od while enforcing frequency limits */ bool swapped = false; + /* + * Whether the intermediate frequencies are out-of-spec + */ + bool out_of_spec; u64 last_od = od; u64 last_r = r; @@ -767,76 +771,71 @@ TEST_STATIC int k210_pll_calc_config(u32 rate, u32 rate_in, * aren't in spec, try swapping r and od. If everything is * in-spec, calculate the relative error. */ - while (true) { +again: + out_of_spec = false; + if (r > max_r) { + out_of_spec = true; + } else { /* - * Whether the intermediate frequencies are out-of-spec + * There is no way to only divide once; we need + * to examine the frequency with and without the + * effect of od. */ - bool out_of_spec = false; + u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r); - if (r > max_r) { + if (vco > 1750000000 || vco < 340000000) out_of_spec = true; - } else { - /* - * There is no way to only divide once; we need - * to examine the frequency with and without the - * effect of od. - */ - u64 vco = DIV_ROUND_CLOSEST_ULL(rate_in * f, r); + } - if (vco > 1750000000 || vco < 340000000) - out_of_spec = true; + if (out_of_spec) { + u64 new_r, new_od; + + if (!swapped) { + u64 tmp = r; + + r = od; + od = tmp; + swapped = true; + goto again; } - if (out_of_spec) { - if (!swapped) { - u64 tmp = r; - - r = od; - od = tmp; - swapped = true; - continue; - } else { - /* - * Try looking ahead to see if there are - * additional factors for the same - * product. - */ - if (i + 1 < ARRAY_SIZE(factors)) { - u64 new_r, new_od; - - i++; - new_r = UNPACK_R(factors[i]); - new_od = UNPACK_OD(factors[i]); - if (r * od == new_r * new_od) { - r = new_r; - od = new_od; - swapped = false; - continue; - } - i--; - } - break; + /* + * Try looking ahead to see if there are additional + * factors for the same product. + */ + if (i + 1 < ARRAY_SIZE(factors)) { + i++; + new_r = UNPACK_R(factors[i]); + new_od = UNPACK_OD(factors[i]); + if (r * od == new_r * new_od) { + r = new_r; + od = new_od; + swapped = false; + goto again; } + i--; } - error = DIV_ROUND_CLOSEST_ULL(f * inv_ratio, r * od); - /* The lower 16 bits are spurious */ - error = abs((error - BIT(32))) >> 16; + /* We ran out of things to try */ + continue; + } - if (error < best_error) { - best->r = r; - best->f = f; - best->od = od; - best_error = error; - } - break; + error = DIV_ROUND_CLOSEST_ULL(f * inv_ratio, r * od); + /* The lower 16 bits are spurious */ + error = abs((error - BIT(32))) >> 16; + + if (error < best_error) { + best->r = r; + best->f = f; + best->od = od; + best_error = error; } } while (f < 64 && i + 1 < ARRAY_SIZE(factors) && error != 0); + log_debug("best error %lld\n", best_error); if (best_error == S64_MAX) return -EINVAL; - log_debug("best error %lld\n", best_error); return 0; } From patchwork Sat Sep 11 17:20:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1526781 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=lQKmODp8; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H6KJt01xJz9sVw for ; Sun, 12 Sep 2021 03:21:01 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2554483831; Sat, 11 Sep 2021 19:20:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lQKmODp8"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 82C878382A; Sat, 11 Sep 2021 19:20:26 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-qv1-xf29.google.com (mail-qv1-xf29.google.com [IPv6:2607:f8b0:4864:20::f29]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C421883831 for ; Sat, 11 Sep 2021 19:20:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qv1-xf29.google.com with SMTP id z12so3402699qvx.5 for ; Sat, 11 Sep 2021 10:20:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KgiWTQ7cbDPGQPLTV7oZFOErpRuJMXfweEwAZi7kcEA=; b=lQKmODp8TEEYY4gTZOqbbLOVh4miAyeSMw+IkU/EOBKnCQZu15UTAmD8txjDNeIm2J 0ruK8T0bM8j6DbRLt175ndkSDYkXZvnfrZ8LvpAE0Wv1ybsWJeMkluIwTNBKY/kVm0dP NgELctHot9OhsdcNi5bYk30KZ4nzBiGqIheP6wg+EuMipSUVGK2EzBhz/grHzmoM89KZ z0GWAzCAQpiOeLHxz7KFvddURcPjaIeCv8cpuY0B2FlxXLKyLc/Uvr7WHUkUPNgpy67b dPQQAZdjBoMyN02h6rJ24xp9zGbn00ykK/Wa3EQHZdug7J7Ft8+g1GCkyJarzgXVxZOn kGcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KgiWTQ7cbDPGQPLTV7oZFOErpRuJMXfweEwAZi7kcEA=; b=ZfdXNuKGVMNM2kHh5oaYAhJZmzaGBjLv9kf7Xk1vbgx8QfPd1Ak6qoq6fFDSYEWLAn LG13dHk5ecHl0BO6bxJyP8TYogmUJKI5VXYfHcf016VYLKZVmxZ/8mV/N0y4IUv7Tgch gAvSC4zP8zduFGM9ABNR53y1uN9ElwtsfzowUDHVgGsKbyXc7KxADdS+brP/oEqiHmMQ dSq2mW5hCSYxYu/PGCds4HCYgD7hmeFe7sSb+Ofgy7wcwV8i+eVXIIBGBSZnq1CjVvJ8 qV24p0pbLWDRXUvOHzaByQS8azrhhHTMZ5m+WIzYUbsgXwPPNoM70haURXt8O1xSgT9X gtmA== X-Gm-Message-State: AOAM533opoNLFfugrxeeG3RvgpMKYLW5hi6x/98S3JQd/zuI3SJWb+Oq PV66AaPBZiIfoSB5RHJbeo6NwYpIfRo= X-Google-Smtp-Source: ABdhPJyl3EkBzKsQ4Yfo13TBuFaZ68XnLu+MxlA2SBFHim0erDa/tZ1DNnt+iZUY9Q3u42Il50hmqA== X-Received: by 2002:a05:6214:3ea:: with SMTP id cf10mr3016749qvb.53.1631380815073; Sat, 11 Sep 2021 10:20:15 -0700 (PDT) Received: from godwin.fios-router.home (pool-108-45-127-224.washdc.fios.verizon.net. [108.45.127.224]) by smtp.gmail.com with ESMTPSA id d12sm1295669qtq.61.2021.09.11.10.20.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Sep 2021 10:20:14 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Leo Liang Cc: Bin Meng , Damien Le Moal , Lukasz Majewski , Sean Anderson , Simon Glass Subject: [PATCH v2 3/4] test: dm: k210: Reduce duplication in test cases Date: Sat, 11 Sep 2021 13:20:02 -0400 Message-Id: <20210911172003.899301-3-seanga2@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210911172003.899301-1-seanga2@gmail.com> References: <20210911172003.899301-1-seanga2@gmail.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Having to copy-paste the same 3 lines makes adding new test cases error-prone. Use a macro. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass Reviewed-by: Leo Yu-Chi Liang --- (no changes since v1) test/dm/k210_pll.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/test/dm/k210_pll.c b/test/dm/k210_pll.c index 54764f269c..5574ac96fa 100644 --- a/test/dm/k210_pll.c +++ b/test/dm/k210_pll.c @@ -69,27 +69,21 @@ static int dm_test_k210_pll(struct unit_test_state *uts) &theirs)); ut_asserteq(-EINVAL, k210_pll_calc_config(1500000000, 20000000, &theirs)); + ut_asserteq(-EINVAL, k210_pll_calc_config(1750000000, 13300000, + &theirs)); /* Verify we get the same output with brute-force */ - ut_assertok(dm_test_k210_pll_calc_config(390000000, 26000000, &ours)); - ut_assertok(k210_pll_calc_config(390000000, 26000000, &theirs)); - ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); +#define compare(rate, rate_in) do { \ + ut_assertok(dm_test_k210_pll_calc_config(rate, rate_in, &ours)); \ + ut_assertok(k210_pll_calc_config(rate, rate_in, &theirs)); \ + ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); \ +} while (0) - ut_assertok(dm_test_k210_pll_calc_config(26000000, 390000000, &ours)); - ut_assertok(k210_pll_calc_config(26000000, 390000000, &theirs)); - ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); - - ut_assertok(dm_test_k210_pll_calc_config(400000000, 26000000, &ours)); - ut_assertok(k210_pll_calc_config(400000000, 26000000, &theirs)); - ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); - - ut_assertok(dm_test_k210_pll_calc_config(27000000, 26000000, &ours)); - ut_assertok(k210_pll_calc_config(27000000, 26000000, &theirs)); - ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); - - ut_assertok(dm_test_k210_pll_calc_config(26000000, 27000000, &ours)); - ut_assertok(k210_pll_calc_config(26000000, 27000000, &theirs)); - ut_assertok(dm_test_k210_pll_compare(&ours, &theirs)); + compare(390000000, 26000000); + compare(26000000, 390000000); + compare(400000000, 26000000); + compare(27000000, 26000000); + compare(26000000, 27000000); return 0; } From patchwork Sat Sep 11 17:20:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1526782 X-Patchwork-Delegate: uboot@andestech.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=CaOknn5d; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H6KK06K87z9sVw for ; Sun, 12 Sep 2021 03:21:08 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4F4EB83838; Sat, 11 Sep 2021 19:20:46 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="CaOknn5d"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 24B118382E; Sat, 11 Sep 2021 19:20:34 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6E0AC83835 for ; Sat, 11 Sep 2021 19:20:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qv1-xf2f.google.com with SMTP id w8so3436118qvt.0 for ; Sat, 11 Sep 2021 10:20:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SmR49mkuHexYgD16TKvu0pvgPOXm3FnLkfDB8Wjw8bE=; b=CaOknn5dI7eyF38ilK2l2Yz8nC/RZyno+NfPBrmFuoFFh9Zm7pq4SK2N7hptJM6BUQ R+XlDaLmzRW40kQaUW9lCKYJ/GAOoCHthSyNMZZGn06tl00N8UvFMecnNjfyQJwzN+b7 /rXktufm70kQqZ/uFa49UyUNm3Z9O/+UnYJS9AvAvFECyfKCLy9+6P+qUTauPEX12spa IEu2zV9lVeMUDZXvgJpO6co4tHVkgYalw3eS2xjO+nz6qkTbMfJrySx2Z3Pfa/8yuXJz 5h4UsIvkNyLbtYSFAWwQfdndyqD4URpljsuSmbTAsAodH2ZwWd8isHmBpSaWnTcqurV8 UGDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SmR49mkuHexYgD16TKvu0pvgPOXm3FnLkfDB8Wjw8bE=; b=OhnwdLsXEeETECKBbWy81zizzWuPpB8KQW/ZlDwi1eqRWJ+pdItM/PmdgrusUsF62j OpC+nzRy21k5HmCFt0kbzZ6EAs4iRsnPg+8PUOv3w8PR6W7UAT1usZOVmOa91LVK0Qvl LHUjfazh6HXFIABKdzq2HUCYcNYnEiObyzN4DFhGDe8+0wMx2+awARMwOGxZLvBqbgCm QA3F3MxbzX4Me2UkfnHGpeXlV5sUpU1Ak5dxrVCBp5awwKU7kKE37Pyg8QJf3/jNkmtV lizrEn3B+/aTXRVUxmN6GOEbsBQ0F5v37cssU2XRBa14nPFsFQzDBD4w/PdyTlHobsR6 uS2g== X-Gm-Message-State: AOAM5310fTZIwKPil6M/usC6jWU+PbeL+oZtI6lmkPn7SlWaENbpb1i6 s+RSjNDZ33WCyRR+YTOIUGjKWadzJcM= X-Google-Smtp-Source: ABdhPJxUnOHFCcja6t7RysPLqZCFjFIJmSSIoPGge7KJyqgHAzcm7wTC2pVnAyb6TPMA2mJcCWuOdQ== X-Received: by 2002:a0c:a88a:: with SMTP id x10mr2953872qva.66.1631380816119; Sat, 11 Sep 2021 10:20:16 -0700 (PDT) Received: from godwin.fios-router.home (pool-108-45-127-224.washdc.fios.verizon.net. [108.45.127.224]) by smtp.gmail.com with ESMTPSA id d12sm1295669qtq.61.2021.09.11.10.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Sep 2021 10:20:15 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de, Leo Liang Cc: Bin Meng , Damien Le Moal , Lukasz Majewski , Sean Anderson , Simon Glass Subject: [PATCH v2 4/4] clk: k210: Try harder to get the best config Date: Sat, 11 Sep 2021 13:20:03 -0400 Message-Id: <20210911172003.899301-4-seanga2@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210911172003.899301-1-seanga2@gmail.com> References: <20210911172003.899301-1-seanga2@gmail.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean In some cases, the best config cannot be used because the VCO would be out-of-spec. In these cases, we may need to try a worse combination of r/od in order to find the best representable config. This also adds a few test cases to catch this and other (possible) unlikely errors. Signed-off-by: Sean Anderson Reviewed-by: Simon Glass --- (no changes since v1) drivers/clk/clk_kendryte.c | 24 ++++++++++++++++++++++++ test/dm/k210_pll.c | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/drivers/clk/clk_kendryte.c b/drivers/clk/clk_kendryte.c index 69691c4a04..97efda5b6f 100644 --- a/drivers/clk/clk_kendryte.c +++ b/drivers/clk/clk_kendryte.c @@ -816,6 +816,30 @@ again: i--; } + /* + * Try looking back to see if there is a worse ratio + * that we could try anyway + */ + while (i > 0) { + i--; + new_r = UNPACK_R(factors[i]); + new_od = UNPACK_OD(factors[i]); + /* + * Don't loop over factors for the same product + * to avoid getting stuck because of the above + * clause + */ + if (r * od != new_r * new_od) { + if (new_r * new_od > last_r * last_od) { + r = new_r; + od = new_od; + swapped = false; + goto again; + } + break; + } + } + /* We ran out of things to try */ continue; } diff --git a/test/dm/k210_pll.c b/test/dm/k210_pll.c index 5574ac96fa..f55379f336 100644 --- a/test/dm/k210_pll.c +++ b/test/dm/k210_pll.c @@ -84,6 +84,10 @@ static int dm_test_k210_pll(struct unit_test_state *uts) compare(400000000, 26000000); compare(27000000, 26000000); compare(26000000, 27000000); + compare(13300000 * 64, 13300000); + compare(21250000, 21250000 * 70); + compare(21250000, 1750000000); + compare(1750000000, 1750000000); return 0; }