mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marcin Niestroj <m.niestroj@grinn-global.com>
To: barebox@lists.infradead.org
Cc: Marcin Niestroj <m.niestroj@grinn-global.com>
Subject: [PATCH 2/3] clk: divider: Fix best div calculation for power-of-two and table dividers
Date: Fri, 28 Jun 2019 17:33:49 +0200	[thread overview]
Message-ID: <20190628153350.16250-2-m.niestroj@grinn-global.com> (raw)
In-Reply-To: <20190628153350.16250-1-m.niestroj@grinn-global.com>

commit dd23c2cd38da2c64af381b19795d2c4f115e8ecb Linux upstream.

  clk: divider: Fix best div calculation for power-of-two and table dividers

  The divider returned by clk_divider_bestdiv() is likely to be invalid in case
  of power-of-two and table dividers when CLK_SET_RATE_PARENT flag isn't set.

  Fixes boot on STiH416 platform.

  Signed-off-by: Maxime Coquelin <maxime.coquelin@st.com>
  Signed-off-by: Mike Turquette <mturquette@linaro.org>
  [mturquette@linaro.org: trivial merge conflict & updated changelog]

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
---
 drivers/clk/clk-divider.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c
index 84a5322cf..d705729af 100644
--- a/drivers/clk/clk-divider.c
+++ b/drivers/clk/clk-divider.c
@@ -141,6 +141,37 @@ static bool _is_valid_div(struct clk_divider *divider, unsigned int div)
 	return true;
 }
 
+static int _round_up_table(const struct clk_div_table *table, int div)
+{
+	const struct clk_div_table *clkt;
+	int up = _get_table_maxdiv(table);
+
+	for (clkt = table; clkt->div; clkt++) {
+		if (clkt->div == div)
+			return clkt->div;
+		else if (clkt->div < div)
+			continue;
+
+		if ((clkt->div - div) < (up - div))
+			up = clkt->div;
+	}
+
+	return up;
+}
+
+static int _div_round_up(struct clk_divider *divider,
+		unsigned long parent_rate, unsigned long rate)
+{
+	int div = DIV_ROUND_UP(parent_rate, rate);
+
+	if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
+		div = __roundup_pow_of_two(div);
+	if (divider->table)
+		div = _round_up_table(divider->table, div);
+
+	return div;
+}
+
 static int clk_divider_bestdiv(struct clk *clk, unsigned long rate,
 		unsigned long *best_parent_rate)
 {
@@ -156,7 +187,7 @@ static int clk_divider_bestdiv(struct clk *clk, unsigned long rate,
 
 	if (!(clk->flags & CLK_SET_RATE_PARENT)) {
 		parent_rate = *best_parent_rate;
-		bestdiv = DIV_ROUND_UP(parent_rate, rate);
+		bestdiv = _div_round_up(divider, parent_rate, rate);
 		bestdiv = bestdiv == 0 ? 1 : bestdiv;
 		bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
 		return bestdiv;
@@ -230,6 +261,9 @@ static int clk_divider_set_rate(struct clk *clk, unsigned long rate,
 		div = DIV_ROUND_UP(parent_rate, rate);
 	}
 
+	if (!_is_valid_div(divider->table, div, divider->flags))
+		return -EINVAL;
+
 	value = _get_val(divider, div);
 
 	if (value > div_mask(divider))
-- 
2.21.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2019-06-28 15:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-28 15:33 [PATCH 1/3] clk: divider: Remove unused variable in clk_divider_recalc_rate Marcin Niestroj
2019-06-28 15:33 ` Marcin Niestroj [this message]
2019-06-28 15:33 ` [PATCH 3/3] clk: divider: Make generic for usage elsewhere Marcin Niestroj
2019-07-02  6:15 ` [PATCH 1/3] clk: divider: Remove unused variable in clk_divider_recalc_rate Sascha Hauer

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=20190628153350.16250-2-m.niestroj@grinn-global.com \
    --to=m.niestroj@grinn-global.com \
    --cc=barebox@lists.infradead.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