From patchwork Sat Oct 22 07:18:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7170 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1088481wrr; Sat, 22 Oct 2022 00:40:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4db1zsfCOH1mhIbxkT2cSPgGv2Cb4U6pc8HJAFsrZRyfeWiUSmQozZQUkaNYacJuHK5L1s X-Received: by 2002:a17:90b:2741:b0:20a:ebc3:6513 with SMTP id qi1-20020a17090b274100b0020aebc36513mr27350665pjb.29.1666424410637; Sat, 22 Oct 2022 00:40:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666424410; cv=none; d=google.com; s=arc-20160816; b=Q43aKhlczNjklFtdkw9XDnMYS9BiPBzLWqsL01Qer+To9/6L/j/lzuQf1hw6luh18x 1IzhDraPgEbO77LWqAtTng7jMOG7X2nVa3Ir8Dd8VkK+zzhluP4OA1uMzxn/uZgXR5L1 kGTCQcg68vMfX/bWbFdQ9TqcTlwwqIU5qo9KSwlBBFvmjxRWuC+i/BGsuA89Z5MMVpU2 hmpPPaR4PgozNzVNWrcJBb2auWQaoOeYgVMHtcs/GhiFSjMl6h86/3kAZ8brncu57K3K nrHdkP2xQ6iBubhpI2ykUA24ObcJk/x+aY5NX6VFkYmTYVa2qiVznJJ53Jt50UlbsfQB 2I1A== 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=rQcYVprb7lq0eUVaMLISSGXF3Wck4rzGh+PlwoQIhYY=; b=g0H2GzQRY7+D7IhGd7oXK7gKDLH/dqTlHcmLFzpgmlUM+yeaSYFkVyWdt62HE/jJm/ g26gv30FGR4pA4G7tx8uWfAcN6ONbXUiqEQINVJEubytIRyeQ2SKLjDqYIfugS+m2DC0 BSp/oNYMerBByEmbCXPmya+eHPRfwieF2i8hravVoy4NHgvl/ofPiskewLEjrfyLfhOQ iH0HY+r4jjFQJLnuVN5bNLOTEileqQBuKcGI++g3E9n03B5Q88syNP9T8Td/zE6wRyU6 TrLiBsWoc/kXSLjxFcyLSvHgV9DKPba/bBLqKEg4RYZL3OGmNjDx9V+KEFYMcxnqamed STUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Le+LrxNU; 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 d23-20020a634f17000000b0043a1129e476si27402865pgb.340.2022.10.22.00.39.56; Sat, 22 Oct 2022 00:40:10 -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=Le+LrxNU; 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 S230486AbiJVHjg (ORCPT + 99 others); Sat, 22 Oct 2022 03:39:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230467AbiJVHiw (ORCPT ); Sat, 22 Oct 2022 03:38:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FB8229B898; Sat, 22 Oct 2022 00:36:14 -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 ams.source.kernel.org (Postfix) with ESMTPS id 563E7B82DEF; Sat, 22 Oct 2022 07:36:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C364CC433D6; Sat, 22 Oct 2022 07:36:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424171; bh=OQ5+raYgpwxt5gHw9gmhOZpaGz9bNoTbg+XUHADCeTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Le+LrxNUJTuB7DYqHVoUBEvw8PvcRN+E5kOt/r+UHfi04MP6BeHeTWHpZBYHacL0x jO3TE/q39IekijDSPnvCn8tRk4ORICWusytVSAcNY+2iS2o+aTkedrmLOcN859BnFD WW3jbZHTQZUNIo3yrHTj+iaoo2Rv7TkPgNfQ955Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Biju Das , Geert Uytterhoeven , Wolfram Sang , Ulf Hansson Subject: [PATCH 5.19 032/717] mmc: renesas_sdhi: Fix rounding errors Date: Sat, 22 Oct 2022 09:18:31 +0200 Message-Id: <20221022072420.811562401@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 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?1747372643319867758?= X-GMAIL-MSGID: =?utf-8?q?1747372643319867758?= From: Biju Das commit f0c00454bf78975925eccc9737faaa4d4951edbf upstream. Due to clk rounding errors on RZ/G2L platforms, it selects a clock source with a lower clock rate compared to a higher one. For eg: The rounding error (533333333 Hz / 4 * 4 = 533333332 Hz < 5333333 33 Hz) selects a clk source of 400 MHz instead of 533.333333 MHz. This patch fixes this issue by adding a margin of (1/1024) higher to the clock rate. Signed-off-by: Biju Das Reviewed-by: Geert Uytterhoeven Tested-by: Geert Uytterhoeven Reviewed-by: Wolfram Sang Tested-by: Wolfram Sang Fixes: bb6d3fa98a41 ("clk: renesas: rcar-gen3: Switch to new SD clock handling") Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/20220928110755.849275-1-biju.das.jz@bp.renesas.com Signed-off-by: Ulf Hansson Signed-off-by: Greg Kroah-Hartman --- drivers/mmc/host/renesas_sdhi_core.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) --- a/drivers/mmc/host/renesas_sdhi_core.c +++ b/drivers/mmc/host/renesas_sdhi_core.c @@ -128,6 +128,7 @@ static unsigned int renesas_sdhi_clk_upd struct clk *ref_clk = priv->clk; unsigned int freq, diff, best_freq = 0, diff_min = ~0; unsigned int new_clock, clkh_shift = 0; + unsigned int new_upper_limit; int i; /* @@ -153,13 +154,20 @@ static unsigned int renesas_sdhi_clk_upd * greater than, new_clock. As we can divide by 1 << i for * any i in [0, 9] we want the input clock to be as close as * possible, but no greater than, new_clock << i. + * + * Add an upper limit of 1/1024 rate higher to the clock rate to fix + * clk rate jumping to lower rate due to rounding error (eg: RZ/G2L has + * 3 clk sources 533.333333 MHz, 400 MHz and 266.666666 MHz. The request + * for 533.333333 MHz will selects a slower 400 MHz due to rounding + * error (533333333 Hz / 4 * 4 = 533333332 Hz < 533333333 Hz)). */ for (i = min(9, ilog2(UINT_MAX / new_clock)); i >= 0; i--) { freq = clk_round_rate(ref_clk, new_clock << i); - if (freq > (new_clock << i)) { + new_upper_limit = (new_clock << i) + ((new_clock << i) >> 10); + if (freq > new_upper_limit) { /* Too fast; look for a slightly slower option */ freq = clk_round_rate(ref_clk, (new_clock << i) / 4 * 3); - if (freq > (new_clock << i)) + if (freq > new_upper_limit) continue; } @@ -181,6 +189,7 @@ static unsigned int renesas_sdhi_clk_upd static void renesas_sdhi_set_clock(struct tmio_mmc_host *host, unsigned int new_clock) { + unsigned int clk_margin; u32 clk = 0, clock; sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~CLK_CTL_SCLKEN & @@ -194,7 +203,13 @@ static void renesas_sdhi_set_clock(struc host->mmc->actual_clock = renesas_sdhi_clk_update(host, new_clock); clock = host->mmc->actual_clock / 512; - for (clk = 0x80000080; new_clock >= (clock << 1); clk >>= 1) + /* + * Add a margin of 1/1024 rate higher to the clock rate in order + * to avoid clk variable setting a value of 0 due to the margin + * provided for actual_clock in renesas_sdhi_clk_update(). + */ + clk_margin = new_clock >> 10; + for (clk = 0x80000080; new_clock + clk_margin >= (clock << 1); clk >>= 1) clock <<= 1; /* 1/1 clock is option */