Skip to contentSkip to conversation

clk: imx: pll14xx: fix recalc_rate for negative kdiv

PATCH Applied Kevin Groeneveld 3 participants 5 messages Dec 10, 2022 – Feb 18, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: imx: pll14xx: fix recalc_rate for negative kdiv

kdiv is a signed 16 bit value in the DEV_CTL1 register. Commit 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation") changed the kdiv variable from a short int to just int. When the value read from the DIV_CTL1 register is assigned directly to an int the sign of the value is lost resulting in incorrect results when the value is negative. Adding a s16 cast to the register value fixes the issue.

Fixes 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")
---
 drivers/clk/imx/clk-pll14xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/imx/clk-pll14xx.c +1 -1
index 1d0f79e9c346..d12194d17b10 100644
@@ -254,7 +254,7 @@ static unsigned long clk_pll14xx_recalc_rate(struct clk_hw *hw,
 
 	if (pll->type == PLL_1443X) {
 		pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
-		kdiv = FIELD_GET(KDIV_MASK, pll_div_ctl1);
+		kdiv = (s16)FIELD_GET(KDIV_MASK, pll_div_ctl1);
 	} else {
 		kdiv = 0;
 	}
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Quoting Kevin Groeneveld (2022-12-10 12:38:35)

Quoted 5 lines

kdiv is a signed 16 bit value in the DEV_CTL1 register. Commit 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation") changed the kdiv variable from a short int to just int. When the value read from the DIV_CTL1 register is assigned directly to an int the sign of the value is lost resulting in incorrect results when the value is negative. Adding

Can the field be negative?

Quoted 4 lines

a s16 cast to the register value fixes the issue.

Fixes 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")
#2 lore raw
Quoted 8 lines · Stephen Boyd

Quoting Kevin Groeneveld (2022-12-10 12:38:35)

Quoted 5 lines

kdiv is a signed 16 bit value in the DEV_CTL1 register. Commit 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation") changed the kdiv variable from a short int to just int. When the value read from the DIV_CTL1 register is assigned directly to an int the sign of the value is lost resulting in incorrect results when the value is negative. Adding

Can the field be negative?

Yes it can be. That is how I found the issue as I had a negative value in imx_pll14xx_rate_table which used to work but broke with the above mentioned commit.

The i.MX8MM reference manual states:

Quoted 4 lines

Formula for Fraction PLLOUT:
* FOUT=((m+k/65536) x FIN) /(p x 2s)
* Where, 1 ≤ p ≤ 63, 64 ≤ m ≤ 1023, 0 ≤ s ≤ 6, -32768 ≤ k ≤ 32767
* p, m, and s are unsigned integers. k is a two's complement integer.

The comments in the imx_pll14xx_calc_settings function also mention the same range for kdiv. And this function also uses negative values. For example:

Quoted 3 lines

/* Then see if we can get the desired rate by only adjusting kdiv (glitch free) */ rate_min = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MIN, prate); rate_max = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MAX, prate);

Where KDIV_MIN is defined as SHRT_MIN which is negative.

Kevin

Reviewed-by on v1 1/1
#3 lore raw
In reply to Kevin Groeneveld
Quoted 9 lines · Kevin Groeneveld

kdiv is a signed 16 bit value in the DEV_CTL1 register. Commit 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation") changed the kdiv variable from a short int to just int. When the value read from the DIV_CTL1 register is assigned directly to an int the sign of the value is lost resulting in incorrect results when the value is negative. Adding a s16 cast to the register value fixes the issue.

Fixes 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")

Stephen, can you pick this up through fixes?

Quoted 20 lines
---
 drivers/clk/imx/clk-pll14xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clk/imx/clk-pll14xx.c +1 -1
index 1d0f79e9c346..d12194d17b10 100644
@@ -254,7 +254,7 @@ static unsigned long clk_pll14xx_recalc_rate(struct clk_hw *hw,
 
 	if (pll->type == PLL_1443X) {
 		pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
-		kdiv = FIELD_GET(KDIV_MASK, pll_div_ctl1);
+		kdiv = (s16)FIELD_GET(KDIV_MASK, pll_div_ctl1);
 	} else {
 		kdiv = 0;
 	}
Signature · 1 line
2.17.1
maintainer Applied
#4 lore raw
In reply to Kevin Groeneveld

Quoting Kevin Groeneveld (2022-12-10 12:38:35)

Quoted 10 lines

kdiv is a signed 16 bit value in the DEV_CTL1 register. Commit 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation") changed the kdiv variable from a short int to just int. When the value read from the DIV_CTL1 register is assigned directly to an int the sign of the value is lost resulting in incorrect results when the value is negative. Adding a s16 cast to the register value fixes the issue.

Fixes 53990cf9d5b4 ("clk: imx: pll14xx: consolidate rate calculation")

---

Applied to clk-fixes