OSDN Git Service

clk: sunxi-ng: Use u64 for calculation of NM rate
authorJernej Skrabec <jernej.skrabec@siol.net>
Sun, 4 Nov 2018 18:26:40 +0000 (19:26 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 9 Jan 2019 16:38:44 +0000 (17:38 +0100)
commit 65b6657672388b72822e0367f06d41c1e3ffb5bb upstream.

Allwinner H6 SoC has multiplier N range between 1 and 254. Since parent
rate is 24MHz, intermediate result when calculating final rate easily
overflows 32 bit variable.

Because of that, introduce function for calculating clock rate which
uses 64 bit variable for intermediate result.

Fixes: 6174a1e24b0d ("clk: sunxi-ng: Add N-M-factor clock support")
Fixes: ee28648cb2b4 ("clk: sunxi-ng: Remove the use of rational computations")

CC: <stable@vger.kernel.org>
Signed-off-by: Jernej Skrabec <jernej.skrabec@siol.net>
Signed-off-by: Maxime Ripard <maxime.ripard@bootlin.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/clk/sunxi-ng/ccu_nm.c

index 4e20733..9e3944f 100644 (file)
@@ -19,6 +19,17 @@ struct _ccu_nm {
        unsigned long   m, min_m, max_m;
 };
 
+static unsigned long ccu_nm_calc_rate(unsigned long parent,
+                                     unsigned long n, unsigned long m)
+{
+       u64 rate = parent;
+
+       rate *= n;
+       do_div(rate, m);
+
+       return rate;
+}
+
 static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
                             struct _ccu_nm *nm)
 {
@@ -28,7 +39,8 @@ static void ccu_nm_find_best(unsigned long parent, unsigned long rate,
 
        for (_n = nm->min_n; _n <= nm->max_n; _n++) {
                for (_m = nm->min_m; _m <= nm->max_m; _m++) {
-                       unsigned long tmp_rate = parent * _n  / _m;
+                       unsigned long tmp_rate = ccu_nm_calc_rate(parent,
+                                                                 _n, _m);
 
                        if (tmp_rate > rate)
                                continue;
@@ -100,7 +112,7 @@ static unsigned long ccu_nm_recalc_rate(struct clk_hw *hw,
        if (ccu_sdm_helper_is_enabled(&nm->common, &nm->sdm))
                rate = ccu_sdm_helper_read_rate(&nm->common, &nm->sdm, m, n);
        else
-               rate = parent_rate * n / m;
+               rate = ccu_nm_calc_rate(parent_rate, n, m);
 
        if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
                rate /= nm->fixed_post_div;
@@ -142,7 +154,7 @@ static long ccu_nm_round_rate(struct clk_hw *hw, unsigned long rate,
        _nm.max_m = nm->m.max ?: 1 << nm->m.width;
 
        ccu_nm_find_best(*parent_rate, rate, &_nm);
-       rate = *parent_rate * _nm.n / _nm.m;
+       rate = ccu_nm_calc_rate(*parent_rate, _nm.n, _nm.m);
 
        if (nm->common.features & CCU_FEATURE_FIXED_POSTDIV)
                rate /= nm->fixed_post_div;