Skip to content

Commit 30eaf02

Browse files
wqyoungbebarino
authored andcommitted
clk: zynqmp: pll: rectify rate rounding in zynqmp_pll_round_rate
The function zynqmp_pll_round_rate is used to find a most appropriate PLL frequency which the hardware can generate according to the desired frequency. For example, if the desired frequency is 297MHz, considering the limited range from PS_PLL_VCO_MIN (1.5GHz) to PS_PLL_VCO_MAX (3.0GHz) of PLL, zynqmp_pll_round_rate should return 1.872GHz (297MHz * 5). There are two problems with the current code of zynqmp_pll_round_rate: 1) When the rate is below PS_PLL_VCO_MIN, it can't find a correct rate when the parameter "rate" is an integer multiple of *prate, in other words, if "f" is zero, zynqmp_pll_round_rate won't return a valid frequency which is from PS_PLL_VCO_MIN to PS_PLL_VCO_MAX. For example, *prate is 33MHz and the rate is 660MHz, zynqmp_pll_round_rate will not boost up rate and just return 660MHz, and this will cause clk_calc_new_rates failure since zynqmp_pll_round_rate returns an invalid rate out of its boundaries. 2) Even if the rate is higher than PS_PLL_VCO_MIN, there is still a risk that zynqmp_pll_round_rate returns an invalid rate because the function DIV_ROUND_CLOSEST makes some loss in the fractional part. If the parent clock *prate is 33333333Hz and we want to set the PLL rate to 1.5GHz, this function will return 1499999985Hz by using the formula below: value = *prate * DIV_ROUND_CLOSEST(rate, *prate)). This value is also invalid since it's slightly smaller than PS_PLL_VCO_MIN. because DIV_ROUND_CLOSEST makes some loss in the fractional part. Signed-off-by: Quanyang Wang <quanyang.wang@windriver.com> Link: https://lore.kernel.org/r/20220826142030.213805-1-quanyang.wang@windriver.com Reviewed-by: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com> Signed-off-by: Stephen Boyd <sboyd@kernel.org>
1 parent 8bdb15c commit 30eaf02

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

drivers/clk/zynqmp/pll.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,25 @@ static long zynqmp_pll_round_rate(struct clk_hw *hw, unsigned long rate,
102102
unsigned long *prate)
103103
{
104104
u32 fbdiv;
105-
long rate_div, f;
105+
u32 mult, div;
106106

107-
/* Enable the fractional mode if needed */
108-
rate_div = (rate * FRAC_DIV) / *prate;
109-
f = rate_div % FRAC_DIV;
110-
if (f) {
111-
if (rate > PS_PLL_VCO_MAX) {
112-
fbdiv = rate / PS_PLL_VCO_MAX;
113-
rate = rate / (fbdiv + 1);
114-
}
115-
if (rate < PS_PLL_VCO_MIN) {
116-
fbdiv = DIV_ROUND_UP(PS_PLL_VCO_MIN, rate);
117-
rate = rate * fbdiv;
118-
}
119-
return rate;
107+
/* Let rate fall inside the range PS_PLL_VCO_MIN ~ PS_PLL_VCO_MAX */
108+
if (rate > PS_PLL_VCO_MAX) {
109+
div = DIV_ROUND_UP(rate, PS_PLL_VCO_MAX);
110+
rate = rate / div;
111+
}
112+
if (rate < PS_PLL_VCO_MIN) {
113+
mult = DIV_ROUND_UP(PS_PLL_VCO_MIN, rate);
114+
rate = rate * mult;
120115
}
121116

122117
fbdiv = DIV_ROUND_CLOSEST(rate, *prate);
123-
fbdiv = clamp_t(u32, fbdiv, PLL_FBDIV_MIN, PLL_FBDIV_MAX);
124-
return *prate * fbdiv;
118+
if (fbdiv < PLL_FBDIV_MIN || fbdiv > PLL_FBDIV_MAX) {
119+
fbdiv = clamp_t(u32, fbdiv, PLL_FBDIV_MIN, PLL_FBDIV_MAX);
120+
rate = *prate * fbdiv;
121+
}
122+
123+
return rate;
125124
}
126125

127126
/**

0 commit comments

Comments
 (0)