The QCA953x CPU PLL can dither its fractional part: when bit 31 of the CPU_DIT_FRAC register (PLL block offset 0x48) is set, the hardware sweeps the fraction between the MIN and MAX fields of that register. Mainline U-Boot enables it for QCA953x (MIN 0, MAX 63, so the CPU clock sweeps 650 to 674.6 MHz with a 25 MHz reference). qca953x_clocks_init() reads the fraction once from CPU_PLL_CONFIG, so with dithering enabled it gets a random sample of the sweep. The CPU clock, the MIPS clocksource and therefore the system time are then off by up to the dithering range, differently on every boot. Measured on a QCA9531 board booted by mainline U-Boot, comparing /proc/uptime with an external clock: the kernel reported 652.3, 653.1 and 673.0 MHz on three boots, and the system time drifted by +16100, +12000 and -15600 ppm respectively, while the real mean frequency was 661 to 663 MHz every time, i.e. the mean of the sweep (662.3 MHz). When dithering is enabled, use the mean (MIN + MAX) / 2 of the fraction. Without dithering nothing changes. Tested on the same board, with U-Boot dithering between MIN 30 and MAX 63: the kernel now reports 643.164 MHz, the mean of the sweep, on every boot. Note that offset 0x48 is also defined as QCA953X_PLL_ETH_SGMII_CONTROL_REG, which has no user. The problem was analysed and this patch written with the help of an AI coding assistant (Claude, Anthropic), from register dumps and the drift measurements above; all measurements and tests were run on hardware. Fixes: af2d1b521bfb ("MIPS: ath79: add support for QCA953x QCA956x TP9343") Cc: stable@vger.kernel.org Signed-off-by: Nicolas Thibert --- arch/mips/ath79/clock.c | 19 +++++++++++++++++-- .../mips/include/asm/mach-ath79/ar71xx_regs.h | 7 +++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/arch/mips/ath79/clock.c b/arch/mips/ath79/clock.c index 050f6553f398..1ae8b21ef04d 100644 --- a/arch/mips/ath79/clock.c +++ b/arch/mips/ath79/clock.c @@ -355,7 +355,7 @@ static void __init qca953x_clocks_init(void __iomem *pll_base) unsigned long ahb_rate; u32 pll, out_div, ref_div, nint, frac, clk_ctrl, postdiv; u32 cpu_pll, ddr_pll; - u32 bootstrap; + u32 bootstrap, dith; bootstrap = ath79_reset_rr(QCA953X_RESET_REG_BOOTSTRAP); if (bootstrap & QCA953X_BOOTSTRAP_REF_CLK_40) @@ -376,7 +376,22 @@ static void __init qca953x_clocks_init(void __iomem *pll_base) QCA953X_PLL_CPU_CONFIG_NFRAC_MASK; cpu_pll = nint * ref_rate / ref_div; - cpu_pll += frac * (ref_rate >> 6) / ref_div; + + /* + * With dithering enabled, the hardware sweeps the fraction between + * MIN and MAX: the one read above is just a sample. Use the mean. + */ + dith = __raw_readl(pll_base + QCA953X_PLL_CPU_DIT_FRAC_REG); + if (dith & QCA953X_PLL_DIT_FRAC_EN) { + u32 fmin = (dith >> QCA953X_PLL_CPU_DIT_FRAC_MIN_SHIFT) & + QCA953X_PLL_CPU_DIT_FRAC_MIN_MASK; + u32 fmax = (dith >> QCA953X_PLL_CPU_DIT_FRAC_MAX_SHIFT) & + QCA953X_PLL_CPU_DIT_FRAC_MAX_MASK; + + cpu_pll += (fmin + fmax) * (ref_rate >> 6) / (2 * ref_div); + } else { + cpu_pll += frac * (ref_rate >> 6) / ref_div; + } cpu_pll /= (1 << out_div); pll = __raw_readl(pll_base + QCA953X_PLL_DDR_CONFIG_REG); diff --git a/arch/mips/include/asm/mach-ath79/ar71xx_regs.h b/arch/mips/include/asm/mach-ath79/ar71xx_regs.h index 5f837060724e..5691e8d6ad07 100644 --- a/arch/mips/include/asm/mach-ath79/ar71xx_regs.h +++ b/arch/mips/include/asm/mach-ath79/ar71xx_regs.h @@ -355,6 +355,7 @@ #define QCA953X_PLL_SWITCH_CLOCK_CONTROL_REG 0x24 #define QCA953X_PLL_ETH_XMII_CONTROL_REG 0x2c #define QCA953X_PLL_ETH_SGMII_CONTROL_REG 0x48 +#define QCA953X_PLL_CPU_DIT_FRAC_REG 0x48 #define QCA953X_PLL_CPU_CONFIG_NFRAC_SHIFT 0 #define QCA953X_PLL_CPU_CONFIG_NFRAC_MASK 0x3f @@ -365,6 +366,12 @@ #define QCA953X_PLL_CPU_CONFIG_OUTDIV_SHIFT 19 #define QCA953X_PLL_CPU_CONFIG_OUTDIV_MASK 0x7 +#define QCA953X_PLL_CPU_DIT_FRAC_MAX_SHIFT 0 +#define QCA953X_PLL_CPU_DIT_FRAC_MAX_MASK 0x3f +#define QCA953X_PLL_CPU_DIT_FRAC_MIN_SHIFT 6 +#define QCA953X_PLL_CPU_DIT_FRAC_MIN_MASK 0x3f +#define QCA953X_PLL_DIT_FRAC_EN BIT(31) + #define QCA953X_PLL_DDR_CONFIG_NFRAC_SHIFT 0 #define QCA953X_PLL_DDR_CONFIG_NFRAC_MASK 0x3ff #define QCA953X_PLL_DDR_CONFIG_NINT_SHIFT 10 -- 2.34.1