rockchip: Miscellaneous PLL handling fixes
Subsystems (3)
v4 cover rockchip: Miscellaneous PLL handling fixes
This series fixes a few issues found in Rockchip PLL handling while working on a cleanup and refactoring series for RK35xx clock drivers.
The 'special pll handling' should ideally be moved into clk_rk3588 driver, something that can be done in a future follow-up series.
Similarly, any refactoring has been held back for a future follow-up series.
Changes in v4:
- Collect r-b tag
- Drop uneccacery type casting
- Update commit message
- Rebase on top of u-boot-rockchip/WIP/20261110
Changes in v3:
- Split the series into smaller series
Changes in v2:
- Add patch to fix automatic PLL fractional calculation
- Update commit messages
- Add fixes tag
- Collect r-b tags
Jonas Karlman (6):
clk: rockchip: pll: Fix fractional PLL rate calculation
clk: rockchip: pll: Fix automatic PLL fractional parameter calculation
clk: rockchip: pll: Always write the dsmpd flag
clk: rockchip: pll: Always write the k parameter
clk: rockchip: pll: Use PLL_FIXED_MODE flag on RK3588/RK3576 PPLL
clk: rockchip: pll: Limit special rk3588_pll handling to RK3588
arch/arm/include/asm/arch-rockchip/clock.h | 2 +- drivers/clk/rockchip/clk_pll.c | 73 +++++++++++----------- drivers/clk/rockchip/clk_rk3576.c | 3 +- drivers/clk/rockchip/clk_rk3588.c | 3 +- 4 files changed, 42 insertions(+), 39 deletions(-)
Signature · 1 line
2.56.1
PATCH v4 1/6 clk: rockchip: pll: Fix fractional PLL rate calculation
The fractional part of the rk3036-style PLL rate is divided by postdiv1 twice instead of by both postdiv1 and postdiv2. The integer part also loses precision by calculating in MHz before converting to Hz.
Calculate both parts in Hz using u64 arithmetic and apply post-dividers on the combined integer and fractional parts. This also closely matches the Linux implementation.
---
Changes in v4:
- Collect r-b tag
Changes in v3:
- No change
Changes in v2:
- Change implementation to closer match Linux implementation
- Update commit message
- Add fixes tag
--- drivers/clk/rockchip/clk_pll.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/clk/rockchip/clk_pll.c +6 -5
@@ -371,7 +371,7 @@ static ulong rk3036_pll_get_rate(struct rockchip_pll_clock *pll, {u32 refdiv, fbdiv, postdiv1, postdiv2, dsmpd, frac;u32 con =0 , shift, mask; -ulong rate; +u64 rate;int mode; con =readl (base + pll->mode_offset);
@@ -403,17 +403,18 @@ static ulong rk3036_pll_get_rate(struct rockchip_pll_clock *pll, con =readl (base + pll->con_offset +0x8 ); frac = (con &RK3036_PLLCON2_FRAC_MASK ) >>RK3036_PLLCON2_FRAC_SHIFT ; - rate = (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) *1000000 ; + rate =OSC_HZ * (u64 )fbdiv; +do_div (rate, refdiv);if (dsmpd ==0 ) {u64 frac_rate =OSC_HZ * (u64 )frac;do_div (frac_rate, refdiv); frac_rate >>=24 ; -do_div (frac_rate, postdiv1); -do_div (frac_rate, postdiv1); rate += frac_rate; } -return rate; +do_div (rate, postdiv1); +do_div (rate, postdiv2); +return (ulong )rate;case RKCLK_PLL_MODE_DEEP :default :return 32768 ;
Signature · 1 line
2.56.1
PATCH v4 2/6 clk: rockchip: pll: Fix automatic PLL fractional parameter calculation
In reply to Jonas KarlmanRequesting a PLL fractional rate missing from the pll table, e.g. 786432000 hz, may result in use of wrong fractional parameter.
With extra debug logging something like following could be printed:
frac div,fin_hz = 24000000,fout_hz = 786432000 frac get postdiv1 = 1, postdiv2 = 2, foutvco = 1572864000 frac get refdiv = 2, fbdiv = 131
f_frac = d2f00
fin_64 = b71b00
frac_64 = 0
frac = 0
Use u64 arithmetic for fractional parameter calculation to prevent truncation when automatically configuring fractional PLLs. With the typical use of a 24 MHz oscillator, u32 arithmetic is enough for the denominator value.
After this following values would be used for the 786432000 hz rate:
f_frac = d2f00
frac_64 = d2f00000000 (before do_div)
frac_64 = 126e97 (after do_div)
frac = 126e97
---
Changes in v4:
- Drop uneccacery type casting
- Update commit message
Changes in v3:
- No change
Changes in v2:
- New patch
--- drivers/clk/rockchip/clk_pll.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/clk/rockchip/clk_pll.c +3 -5
@@ -114,7 +114,7 @@ rockchip_pll_clk_set_by_auto(ulong fin_hz,struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table;/* FIXME set postdiv1/2 always 1*/ u32 foutvco = fout_hz; -ulong fin_64, frac_64; +u64 frac_64;u32 f_frac, postdiv1, postdiv2;ulong clk_gcd =0 ;
@@ -155,10 +155,8 @@ rockchip_pll_clk_set_by_auto(ulong fin_hz, rate_table->frac =0 ; f_frac = (foutvco %MHZ ); - fin_64 = fin_hz; - fin_64 = fin_64 / rate_table->refdiv; - frac_64 = f_frac <<24 ; - frac_64 = frac_64 / fin_64; + frac_64 = (u64 )f_frac <<24 ; +do_div (frac_64, fin_hz / rate_table->refdiv); rate_table->frac = frac_64;if (rate_table->frac >0 ) rate_table->dsmpd =0 ;
Signature · 1 line
2.56.1
Re: [PATCH v4 2/6] clk: rockchip: pll: Fix automatic PLL fractional parameter calculation
Hi Jonas,
Quoted 20 lines · Jonas Karlman
Requesting a PLL fractional rate missing from the pll table, e.g. 786432000 hz, may result in use of wrong fractional parameter.
With extra debug logging something like following could be printed:
frac div,fin_hz = 24000000,fout_hz = 786432000 frac get postdiv1 = 1, postdiv2 = 2, foutvco = 1572864000 frac get refdiv = 2, fbdiv = 131 f_frac = d2f00 fin_64 = b71b00 frac_64 = 0 frac = 0Use u64 arithmetic for fractional parameter calculation to prevent truncation when automatically configuring fractional PLLs. With the typical use of a 24 MHz oscillator, u32 arithmetic is enough for the denominator value.
After this following values would be used for the 786432000 hz rate:
Quoted 7 lines
f_frac = d2f00 frac_64 = d2f00000000 (before do_div) frac_64 = 126e97 (after do_div) frac = 126e97Fixes bbda2ed5840c ("rockchip: clk: pll: add common pll setting funcs")Signed-off-by Jonas Karlman <[email protected]>
Thanks!
Quentin
PATCH v4 3/6 clk: rockchip: pll: Always write the dsmpd flag
In reply to Jonas KarlmanThe dsmpd flag controls whether an rk3036-style PLL uses fractional mode. When changing from a fractional rate to an integer rate, the dsmpd field is left cleared, keeping fractional mode active.
Always write dsmpd to the register so integer rates disable fractional mode. This also matches how Linux configures these PLLs.
---
Changes in v4:
- No change
Changes in v3:
- No change
Changes in v2:
- Update commit message
- Add fixes tag
- Collect r-b tag
--- drivers/clk/rockchip/clk_pll.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/clk/rockchip/clk_pll.c +4 -5
@@ -329,13 +329,12 @@ static int rk3036_pll_set_rate(struct rockchip_pll_clock *pll, rate->fbdiv);rk_clrsetreg (base + pll->con_offset +0x4 , (RK3036_PLLCON1_POSTDIV2_MASK | -RK3036_PLLCON1_REFDIV_MASK ), +RK3036_PLLCON1_REFDIV_MASK | +RK3036_PLLCON1_DSMPD_MASK ), (rate->postdiv2 <<RK3036_PLLCON1_POSTDIV2_SHIFT | - rate->refdiv <<RK3036_PLLCON1_REFDIV_SHIFT )); + rate->refdiv <<RK3036_PLLCON1_REFDIV_SHIFT | + rate->dsmpd <<RK3036_PLLCON1_DSMPD_SHIFT ));if (!rate->dsmpd) { -rk_clrsetreg (base + pll->con_offset +0x4 , -RK3036_PLLCON1_DSMPD_MASK , - rate->dsmpd <<RK3036_PLLCON1_DSMPD_SHIFT ); writel((readl(base + pll->con_offset + 0x8) & (~RK3036_PLLCON2_FRAC_MASK)) | (rate->frac << RK3036_PLLCON2_FRAC_SHIFT),
Signature · 1 line
2.56.1
PATCH v4 4/6 clk: rockchip: pll: Always write the k parameter
In reply to Jonas KarlmanThe k parameter supplies the fractional part of an rk3588-style PLL rate. When changing from a fractional rate to an integer rate, the k value is left unchanged, leaving the old fractional part active.
Always write k to the register, including zero, so integer rates clear any prior fractional part. This also matches how Linux configures these PLLs.
---
Changes in v4:
- No change
Changes in v3:
- No change
Changes in v2:
- Update commit message
- Add fixes tag
- Collect r-b tag
--- drivers/clk/rockchip/clk_pll.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/clk/rockchip/clk_pll.c +3 -5
@@ -488,11 +488,9 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll, RK3588_PLLCON1_S_MASK), (rate->p <<RK3588_PLLCON1_P_SHIFT | rate->s << RK3588_PLLCON1_S_SHIFT)); -if (rate->k) { -rk_clrsetreg (base + pll->con_offset +RK3588_PLLCON (2 ), -RK3588_PLLCON2_K_MASK , - rate->k <<RK3588_PLLCON2_K_SHIFT ); - } +rk_clrsetreg (base + pll->con_offset +RK3588_PLLCON (2 ), +RK3588_PLLCON2_K_MASK , + rate->k <<RK3588_PLLCON2_K_SHIFT );/* Power up */ rk_clrreg (base + pll->con_offset +RK3588_PLLCON (1 ),RK3588_PLLCON1_PWRDOWN );
Signature · 1 line
2.56.1
PATCH v4 5/6 clk: rockchip: pll: Use PLL_FIXED_MODE flag on RK3588/RK3576 PPLL
In reply to Jonas KarlmanThe PPLL on RK3588 and RK3576 has no PLL mode register and cannot switch between slow, deep slow or normal mode.
Extend ROCKCHIP_PLL_FIXED_MODE support to the pll_rk3588 type and mark both PPLLs with this flag. Skip mode register writes when changing rates and treat the PLLs as operating in normal mode when reading rates.
---
Changes in v4:
- Rebase on top of u-boot-rockchip/WIP/20261110
Changes in v3:
- No change
Changes in v2:
- Update comment in clock.h header
- Update commit message
- Collect r-b tag
--- arch/arm/include/asm/arch-rockchip/clock.h | 2 +- drivers/clk/rockchip/clk_pll.c | 21 +++++++++++++-------- drivers/clk/rockchip/clk_rk3576.c | 3 ++- drivers/clk/rockchip/clk_rk3588.c | 3 ++- 4 files changed, 18 insertions(+), 11 deletions(-)
arch/arm/include/asm/arch-rockchip/clock.h +1 -1
@@ -19,7 +19,7 @@ struct udevice;* PLL flags */ #define ROCKCHIP_PLL_SYNC_RATE BIT(0) -/* normal mode only. now only for pll_rk3036, pll_rk3328 type */ +/* normal mode only. */ #define ROCKCHIP_PLL_FIXED_MODE BIT(1)enum {
drivers/clk/rockchip/clk_pll.c +13 -8
@@ -460,9 +460,11 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll,if (pll_id ==3 )rk_clrsetreg (base +0x84c ,0x1 <<1 ,0x1 <<1 ); -rk_clrsetreg (base + pll->mode_offset, - pll->mode_mask << pll->mode_shift, -RKCLK_PLL_MODE_SLOW << pll->mode_shift); +if (!(pll->pll_flags &ROCKCHIP_PLL_FIXED_MODE )) { +rk_clrsetreg (base + pll->mode_offset, + pll->mode_mask << pll->mode_shift, +RKCLK_PLL_MODE_SLOW << pll->mode_shift); + }if (pll_id ==0 ) rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0), pll->mode_mask << 6,
@@ -502,8 +504,11 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll,debug ("%s: wait pll lock, pll_id=%ld\n" , __func__, pll_id); } -rk_clrsetreg (base + pll->mode_offset, pll->mode_mask << pll->mode_shift, -RKCLK_PLL_MODE_NORMAL << pll->mode_shift); +if (!(pll->pll_flags &ROCKCHIP_PLL_FIXED_MODE )) { +rk_clrsetreg (base + pll->mode_offset, + pll->mode_mask << pll->mode_shift, +RKCLK_PLL_MODE_NORMAL << pll->mode_shift); + }if (pll_id ==0 ) { rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0), pll->mode_mask << 6,
@@ -564,10 +569,10 @@ static ulong rk3588_pll_get_rate(struct rockchip_pll_clock *pll, con =readl (base + pll->mode_offset); shift = pll->mode_shift; -if (pll_id ==8 ) - mode =RKCLK_PLL_MODE_NORMAL ; -else +if (!(pll->pll_flags &ROCKCHIP_PLL_FIXED_MODE )) mode = (con & (pll->mode_mask << shift)) >> shift; +else + mode =RKCLK_PLL_MODE_NORMAL ;switch (mode) {case RKCLK_PLL_MODE_SLOW :return OSC_HZ ;
drivers/clk/rockchip/clk_rk3576.c +2 -1
@@ -56,7 +56,8 @@ static struct rockchip_pll_clock rk3576_pll_clks[] = { [GPLL ] =PLL (pll_rk3588,PLL_GPLL ,RK3576_PLL_CON (112 ), RK3576_MODE_CON0, 2, 15, 0, rk3576_24m_pll_rates), [PPLL ] =PLL (pll_rk3588,PLL_PPLL ,RK3576_PHP_PLL_CON (128 ), - RK3576_MODE_CON0, 10, 15, 0, rk3576_24m_pll_rates), +RK3576_MODE_CON0 ,10 ,15 ,ROCKCHIP_PLL_FIXED_MODE , + rk3576_24m_pll_rates), };#ifdef CONFIG_SPL_BUILD
drivers/clk/rockchip/clk_rk3588.c +2 -1
@@ -60,7 +60,8 @@ static struct rockchip_pll_clock rk3588_pll_clks[] = { [NPLL ] =PLL (pll_rk3588,PLL_NPLL ,RK3588_PLL_CON (120 ), RK3588_MODE_CON0, 0, 15, 0, rk3588_pll_rates), [PPLL ] =PLL (pll_rk3588,PLL_PPLL ,RK3588_PHP_PLL_CON (128 ), -RK3588_MODE_CON0 ,10 ,15 ,0 , rk3588_pll_rates), +RK3588_MODE_CON0 ,10 ,15 ,ROCKCHIP_PLL_FIXED_MODE , + rk3588_pll_rates), #ifdef CONFIG_XPL_BUILD/* * The SPLL is part of the SBUSCRU, not the main CRU and as
Signature · 1 line
2.56.1
PATCH v4 6/6 clk: rockchip: pll: Limit special rk3588_pll handling to RK3588
In reply to Jonas KarlmanThe rk3588_pll_set_rate() function contains special handling intended only for PLLs on RK3588 that are also applied to PLLs on RK3576.
This special handling should ideally be moved from the common pll funcs into the clk_rk3588 driver. Add IS_ENABLED() guards as an initial step to limit this special handling to PLLs on RK3588 targets.
---
Changes in v4:
- No change
Changes in v3:
- No change
Changes in v2:
- Add fixes tag
- Collect r-b tag
--- drivers/clk/rockchip/clk_pll.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/clk/rockchip/clk_pll.c +8 -8
@@ -457,7 +457,7 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll,* When power on or changing PLL setting, * we must force PLL into slow mode to ensure output stable clock. */ -if (pll_id ==3 ) +if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==3 )rk_clrsetreg (base +0x84c ,0x1 <<1 ,0x1 <<1 );if (!(pll->pll_flags &ROCKCHIP_PLL_FIXED_MODE )) {
@@ -465,15 +465,15 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll, pll->mode_mask << pll->mode_shift, RKCLK_PLL_MODE_SLOW << pll->mode_shift); } -if (pll_id ==0 ) +if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==0 )rk_clrsetreg (base +RK3588_B0PLL_CLKSEL_CON (0 ), pll->mode_mask <<6 ,RKCLK_PLL_MODE_SLOW <<6 ); -else if (pll_id ==1 ) +else if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==1 )rk_clrsetreg (base +RK3588_B1PLL_CLKSEL_CON (0 ), pll->mode_mask <<6 ,RKCLK_PLL_MODE_SLOW <<6 ); -else if (pll_id ==2 ) +else if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==2 )rk_clrsetreg (base +RK3588_LPLL_CLKSEL_CON (5 ), pll->mode_mask <<14 ,RKCLK_PLL_MODE_SLOW <<14 );
@@ -509,7 +509,7 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll, pll->mode_mask << pll->mode_shift, RKCLK_PLL_MODE_NORMAL << pll->mode_shift); } -if (pll_id ==0 ) { +if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==0 ) {rk_clrsetreg (base +RK3588_B0PLL_CLKSEL_CON (0 ), pll->mode_mask <<6 ,2 <<6 );
@@ -519,7 +519,7 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll,rk_clrsetreg (base +RK3588_B0PLL_CLKSEL_CON (1 ),RK3588_CORE_DIV_MASK <<RK3588_CORE_B13_DIV_SHIFT ,0 <<RK3588_CORE_B13_DIV_SHIFT ); - }else if (pll_id ==1 ) { + }else if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==1 ) {rk_clrsetreg (base +RK3588_B1PLL_CLKSEL_CON (0 ), pll->mode_mask <<6 ,2 <<6 );
@@ -529,7 +529,7 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll,rk_clrsetreg (base +RK3588_B1PLL_CLKSEL_CON (1 ),RK3588_CORE_DIV_MASK <<RK3588_CORE_B13_DIV_SHIFT ,0 <<RK3588_CORE_B13_DIV_SHIFT ); - }else if (pll_id ==2 ) { + }else if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==2 ) {rk_clrsetreg (base +RK3588_LPLL_CLKSEL_CON (5 ), pll->mode_mask <<14 ,2 <<14 );
@@ -547,7 +547,7 @@ static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll, 0 << RK3588_CORE_L02_DIV_SHIFT); } -if (pll_id ==3 ) +if (IS_ENABLED (CONFIG_ROCKCHIP_RK3588 ) && pll_id ==3 )rk_clrsetreg (base +0x84c ,0x1 <<1 ,0 ); debug("PLL at %p: con0=%x con1= %x con2= %x mode= %x\n",
Signature · 1 line
2.56.1
Quoted 10 lines · Jonas Karlman
This series fixes a few issues found in Rockchip PLL handling while working on a cleanup and refactoring series for RK35xx clock drivers.
The 'special pll handling' should ideally be moved into
clk_rk3588driver, something that can be done in a future follow-up series.Similarly, any refactoring has been held back for a future follow-up series.
[...]
Applied to https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip (u-boot-rockchip-2027.04), thanks!
[1/6] clk: rockchip: pll: Fix fractional PLL rate calculation
https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/63fd00abafbc2eb70234142e175da0de3ed9781c [2/6] clk: rockchip: pll: Fix automatic PLL fractional parameter calculation https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/ea665bde5f020e33cb2768e6e0cbcf241d9f7085 [3/6] clk: rockchip: pll: Always write the dsmpd flag https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/1a50ae37147a39e4cb349a278b6483f802d5604b [4/6] clk: rockchip: pll: Always write the k parameter https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/070ecf61ab41dd45961e758f00b04e8365ca77ec [5/6] clk: rockchip: pll: Use PLL_FIXED_MODE flag on RK3588/RK3576 PPLL https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/464ab6457bafce42bbca156ddfdf35c5eeac4a5f [6/6] clk: rockchip: pll: Limit special rk3588_pll handling to RK3588 https://git.u-boot-project.org/u-boot/custodians/u-boot-rockchip/-/commit/5335515b2b53ad13e1c80637a552afb1dcd50b5b
Best regards,
Signature · 1 line
Quentin Schulz <[email protected]>