clk: axi-clkgen: Correctly handle nocount bit in recalc_rate()
PATCH 1/2 clk: axi-clkgen: Correctly handle nocount bit in recalc_rate()
If the nocount bit is set the divider is bypassed and the settings for the divider count should be ignored and a divider value of 1 should be assumed. Handle this correctly in the driver recalc_rate() callback.
While the driver sets up the part so that the read back dividers values yield the correct result the power-on reset settings of the part might not reflect this and hence calling e.g. clk_get_rate() without prior calls to clk_set_rate() will yield the wrong result.
--- drivers/clk/clk-axi-clkgen.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-)
drivers/clk/clk-axi-clkgen.c +24 -5
@@ -40,6 +40,10 @@#define MMCM_REG_FILTER1 0x4e#define MMCM_REG_FILTER2 0x4f +#define MMCM_CLKOUT_NOCOUNT BIT(6) + +#define MMCM_CLK_DIV_NOCOUNT BIT(12) +struct axi_clkgen {void __iomem *base;struct clk_hw clk_hw;
@@ -315,12 +319,27 @@ static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,unsigned int reg;unsigned long long tmp; -axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLKOUT0_1 , ®); - dout = (reg &0x3f ) + ((reg >>6 ) &0x3f ); +axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLKOUT0_2 , ®); +if (reg &MMCM_CLKOUT_NOCOUNT ) { + dout =1 ; + }else { +axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLKOUT0_1 , ®); + dout = (reg &0x3f ) + ((reg >>6 ) &0x3f ); + } +axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLK_DIV , ®); - d = (reg &0x3f ) + ((reg >>6 ) &0x3f ); -axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLK_FB1 , ®); - m = (reg &0x3f ) + ((reg >>6 ) &0x3f ); +if (reg &MMCM_CLK_DIV_NOCOUNT ) + d =1 ; +else + d = (reg &0x3f ) + ((reg >>6 ) &0x3f ); + +axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLK_FB2 , ®); +if (reg &MMCM_CLKOUT_NOCOUNT ) { + m =1 ; + }else { +axi_clkgen_mmcm_read (axi_clkgen,MMCM_REG_CLK_FB1 , ®); + m = (reg &0x3f ) + ((reg >>6 ) &0x3f ); + }if (d ==0 || dout ==0 )return 0 ;
Signature · 1 line
2.11.0
PATCH 2/2 clk: axi-clkgen: Round closest in round_rate() and recalc_rate()
To minimize the rounding error round to the closest integer when calculating the result in the recalc_rate() and set_rate() callbacks.
Also in order to improve precision multiply first and then divide.
--- drivers/clk/clk-axi-clkgen.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/clk/clk-axi-clkgen.c +7 -3
@@ -302,13 +302,17 @@ static long axi_clkgen_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate) {unsigned int d, m, dout; +unsigned long long tmp;axi_clkgen_calc_params (*parent_rate, rate, &d, &m, &dout);if (d ==0 || dout ==0 || m ==0 )return -EINVAL ; -return *parent_rate / d * m / dout; + tmp = (unsigned long long )*parent_rate * m; + tmp =DIV_ROUND_CLOSEST_ULL (tmp, dout * d); + + return min_t(unsigned long long, tmp, LONG_MAX); } static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,
@@ -344,8 +348,8 @@ static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,if (d ==0 || dout ==0 )return 0 ; - tmp = (unsigned long long )(parent_rate / d) * m; -do_div (tmp, dout); + tmp = (unsigned long long )parent_rate * m; + tmp =DIV_ROUND_CLOSEST_ULL (tmp, dout * d); return min_t(unsigned long long, tmp, ULONG_MAX); }
Signature · 1 line
2.11.0
Stephen Boyd
AppliedQuoted 11 lines · Lars-Peter Clausen
If the nocount bit is set the divider is bypassed and the settings for the divider count should be ignored and a divider value of 1 should be assumed. Handle this correctly in the driver
recalc_rate()callback.While the driver sets up the part so that the read back dividers values yield the correct result the power-on reset settings of the part might not reflect this and hence calling e.g.
clk_get_rate()without prior calls toclk_set_rate()will yield the wrong result.Signed-off-by Lars-Peter Clausen <[email protected]>---
Applied to clk-next
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
Stephen Boyd
AppliedRe: [PATCH 2/2] clk: axi-clkgen: Round closest in round_rate() and recalc_rate()
In reply to Lars-Peter ClausenQuoted 7 lines · Lars-Peter Clausen
To minimize the rounding error round to the closest integer when calculating the result in the
recalc_rate()andset_rate()callbacks.Also in order to improve precision multiply first and then divide.
Signed-off-by Lars-Peter Clausen <[email protected]>---
Applied to clk-next
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project