mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xukai Wang <kingxukai@zohomail.com>
To: David Carlier <devnexen@gmail.com>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>
Cc: Brian Masney <bmasney@redhat.com>,
	Conor Dooley <conor.dooley@microchip.com>,
	Troy Mitchell <TroyMitchell988@gmail.com>,
	linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] clk: canaan: Clear rate fields before reprogramming dividers
Date: Thu, 18 Jun 2026 16:18:51 +0800	[thread overview]
Message-ID: <2eda17d5-298a-421e-9460-ef32a65e50d2@zohomail.com> (raw)
In-Reply-To: <20260618045030.12581-1-devnexen@gmail.com>


On 2026/6/18 12:50, David Carlier wrote:
> The rate set_rate helpers perform a read-modify-write on the divider
> and multiplier registers but only ever OR the new value in, without
> first masking off the existing field. The first write after reset lands
> on a zeroed field and looks correct, but any later reprogramming leaves
> the old bits set: the field becomes the bitwise OR of the previous and
> new encodings, corrupting the divider or multiplier.
>
> Mask off each field before writing the new value so reprogramming a
> clock to a different rate produces the intended register contents.
>
> Fixes: a7b7c7c6c016 ("clk: canaan: Add clock driver for Canaan K230")
> Signed-off-by: David Carlier <devnexen@gmail.com>
> ---
>  drivers/clk/clk-k230.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/drivers/clk/clk-k230.c b/drivers/clk/clk-k230.c
> index cfc437038e4e..f34a3e6d3bca 100644
> --- a/drivers/clk/clk-k230.c
> +++ b/drivers/clk/clk-k230.c
> @@ -2227,6 +2227,7 @@ static int k230_clk_set_rate_mul(struct clk_hw *hw, unsigned long rate,
>  	guard(spinlock)(rate_self->lock);
>  
>  	mul_reg = readl(rate_self->reg + clk->mul_reg_off);
> +	mul_reg &= ~(rate_self->mul_mask << rate_self->mul_shift);
>  	mul_reg |= ((mul - 1) & rate_self->mul_mask) << (rate_self->mul_shift);
>  	mul_reg |= BIT(rate_self->write_enable_bit);
>  	writel(mul_reg, rate_self->reg + clk->mul_reg_off);
> @@ -2257,6 +2258,7 @@ static int k230_clk_set_rate_div(struct clk_hw *hw, unsigned long rate,
>  	guard(spinlock)(rate_self->lock);
>  
>  	div_reg = readl(rate_self->reg + clk->div_reg_off);
> +	div_reg &= ~(rate_self->div_mask << rate_self->div_shift);
>  	div_reg |= ((div - 1) & rate_self->div_mask) << (rate_self->div_shift);
>  	div_reg |= BIT(rate_self->write_enable_bit);
>  	writel(div_reg, rate_self->reg + clk->div_reg_off);
> @@ -2287,11 +2289,13 @@ static int k230_clk_set_rate_mul_div(struct clk_hw *hw, unsigned long rate,
>  	guard(spinlock)(rate_self->lock);
>  
>  	div_reg = readl(rate_self->reg + clk->div_reg_off);
> +	div_reg &= ~(rate_self->div_mask << rate_self->div_shift);
>  	div_reg |= ((div - 1) & rate_self->div_mask) << (rate_self->div_shift);
>  	div_reg |= BIT(rate_self->write_enable_bit);
>  	writel(div_reg, rate_self->reg + clk->div_reg_off);
>  
>  	mul_reg = readl(rate_self->reg + clk->mul_reg_off);
> +	mul_reg &= ~(rate_self->mul_mask << rate_self->mul_shift);
>  	mul_reg |= ((mul - 1) & rate_self->mul_mask) << (rate_self->mul_shift);
>  	mul_reg |= BIT(rate_self->write_enable_bit);
>  	writel(mul_reg, rate_self->reg + clk->mul_reg_off);

Good catch. This fix is correct and necessary -- without masking the old
bits first, reprogramming the clock rate would leave stale bits in the
register and result in incorrect divider/multiplier values.

All set_rate paths are properly covered.

Acked-by: Xukai Wang <kingxukai@zohomail.com>

-- 
Thanks,
Xukai


  reply	other threads:[~2026-06-18  8:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-18  4:50 David Carlier
2026-06-18  8:18 ` Xukai Wang [this message]
2026-06-18 16:55 ` Brian Masney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2eda17d5-298a-421e-9460-ef32a65e50d2@zohomail.com \
    --to=kingxukai@zohomail.com \
    --cc=TroyMitchell988@gmail.com \
    --cc=bmasney@redhat.com \
    --cc=conor.dooley@microchip.com \
    --cc=devnexen@gmail.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=sboyd@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®