Skip to contentSkip to conversation

clk: ath79: move the PLL clock driver to drivers/clk

PATCH Superseded Rosen Penev 2 participants 10 messages Nov 4 – Nov 10

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover clk: ath79: move the PLL clock driver to drivers/clk

The ath79 PLL clock code lives in arch/mips/ath79/clock.c and uses the arch headers and ath79_reset_base. This series makes it a normal clk driver in drivers/clk and then cleans it up.

Patches 1-2 remove what ties it to the arch code. The QCA956x timer interrupt workaround moves to plat_time_init(), and so does reading the reference clock rate from the bootstrap pins, for boards whose device tree leaves the rate out. The arch code also warns when a rate in the device tree disagrees with the pins.

Patch 3 moves the driver, with copies of the PLL register definitions it uses, and adds a MAINTAINERS entry from the qca,ath79-pll binding. It stays a CLK_OF_DECLARE driver, as the MIPS timer needs the CPU clock.

Patches 4-8 clean it up:
- 4: use GENMASK() and FIELD_GET() for the register fields (the generated code is unchanged)
- 5: convert to clk_hw. This also fixes the AR724x and AR933x clocks, whose parent was looked up by the name "ref", so they were orphans when the device tree's reference clock node has a different name.
- 6: fix the error handling: an unchecked ioremap(), clk_put() of an error pointer, and panics or BUG_ON() where an error return will do
- 7: one init function per compatible instead of re-matching it
- 8: share the fractional PLL calculation, using 64-bit maths. This fixes a 32-bit overflow of the QCA955x DDR PLL rate whenever its NFRAC is above 107 with a 40 MHz reference clock, or above 171 with 25 MHz.

The series touches both arch/mips and drivers/clk. Patches 3-8 depend on 1-2, so it would be easiest to take it all through one tree with an ack from the other maintainer.

Not addressed: the QCA956x PLL formula. It ignores the NFRAC_H field ("hfrac >> 13" of a 13-bit field is always 0) and divides NFRAC_L by 25 << 13 instead of 2^18. Mainline U-Boot has the same code, so it is no independent check. I left it unchanged until it can be checked against register values from a QCA956x board.

Tool use, per Documentation/process/generated-content.rst: this series was developed with Claude Code (Claude Opus 5.5). It wrote most of the code changes and changelogs in patches 3-8, following my review of the driver and my requests, and reviewed patches 1-2. It also wrote the user space harness used to test patch 8, and it fetched and compared the U-Boot sources for the QCA956x formula. I reviewed and tested all of it and take responsibility for it.

Rosen Penev (8):
MIPS: ath79: apply the QCA956x timer workaround in setup
MIPS: ath79: move the bootstrap reference clock to setup
clk: ath79: move the ath79 clock driver to drivers/clk
clk: ath79: use FIELD_GET() for the PLL register fields
clk: ath79: convert to clk_hw based registration
clk: ath79: fix the error handling
clk: ath79: give each compatible its own init function
clk: ath79: share the fractional PLL calculation

 MAINTAINERS              |   8 +
 arch/mips/ath79/Makefile |   2 +-
 arch/mips/ath79/clock.c  | 673 ---------------------------------------
 arch/mips/ath79/setup.c  | 100 ++++++
 drivers/clk/Kconfig      |  12 +
 drivers/clk/Makefile     |   1 +
 drivers/clk/clk-ath79.c  | 593 ++++++++++++++++++++++++++++++++++
 drivers/clk/clk-ath79.h  | 140 ++++++++
 8 files changed, 855 insertions(+), 674 deletions(-)
 delete mode 100644 arch/mips/ath79/clock.c
 create mode 100644 drivers/clk/clk-ath79.c
 create mode 100644 drivers/clk/clk-ath79.h

base-commit: d6bcfc94eb4da6e089c5473970cd96727292fd97

Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/8 MIPS: ath79: apply the QCA956x timer workaround in setup

qca956x_clocks_init() enables the MIPS timer interrupt in the misc interrupt controller, which QCA956x needs before the clocks are set up, or there are no jiffies. That has nothing to do with the clocks, and it ties the clock code to ath79_reset_base.

Do it in plat_time_init() right before of_clk_init() instead, which runs the clock setup. The order is the same. TP9343 boards use the QCA956x PLL and took this path too, so check for both.

---
 arch/mips/ath79/clock.c | 10 ----------
 arch/mips/ath79/setup.c | 13 +++++++++++++
 2 files changed, 13 insertions(+), 10 deletions(-)
arch/mips/ath79/clock.c +0 -10
index 050f6553f398..8c5eaa4ab6e0 100644
@@ -523,16 +523,6 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	u32 cpu_pll, ddr_pll;
 	u32 bootstrap;
 
-	/*
-	 * QCA956x timer init workaround has to be applied right before setting
-	 * up the clock. Else, there will be no jiffies
-	 */
-	u32 misc;
-
-	misc = ath79_reset_rr(AR71XX_RESET_REG_MISC_INT_ENABLE);
-	misc |= MISC_INT_MIPS_SI_TIMERINT_MASK;
-	ath79_reset_wr(AR71XX_RESET_REG_MISC_INT_ENABLE, misc);
-
 	bootstrap = ath79_reset_rr(QCA956X_RESET_REG_BOOTSTRAP);
 	if (bootstrap &	QCA956X_BOOTSTRAP_REF_CLK_40)
 		ref_rate = 40 * 1000 * 1000;
arch/mips/ath79/setup.c +13 -0
index 4e18cdcf65a0..d9e1cc77ad04 100644
@@ -241,6 +241,19 @@ void __init plat_time_init(void)
 	struct clk *clk;
 	unsigned long cpu_clk_rate;
 
+	/*
+	 * QCA956x needs the MIPS timer interrupt enabled in the misc
+	 * interrupt controller before the clocks are set up, or there are
+	 * no jiffies.
+	 */
+	if (soc_is_qca956x() || soc_is_tp9343()) {
+		u32 misc;
+
+		misc = ath79_reset_rr(AR71XX_RESET_REG_MISC_INT_ENABLE);
+		misc |= MISC_INT_MIPS_SI_TIMERINT_MASK;
+		ath79_reset_wr(AR71XX_RESET_REG_MISC_INT_ENABLE, misc);
+	}
+
 	of_clk_init(NULL);
 
 	np = of_get_cpu_node(0, NULL);
Signature · 1 line
2.56.0
#2 lore raw

PATCH 2/8 MIPS: ath79: move the bootstrap reference clock to setup

In reply to Rosen Penev

The qca,ath79-pll binding requires the reference clock as an input, and a rate in the device tree already took precedence over the 25/40 MHz that the clock code read from the bootstrap pins. Many AR933x boards leave the rate out, though, and rely on the pins.

Read the pins in plat_time_init() instead, and register the reference clock from them only when the device tree gives no rate. The clock code then takes the DT clock or this one through the clk API and no longer needs ath79_reset_base. When the device tree does give a rate, warn if it disagrees with the pins: a wrong rate leaves the CPU, DDR and AHB rates off, the UART baud rate included.

---
 arch/mips/ath79/clock.c | 71 ++++++++-------------------------
 arch/mips/ath79/setup.c | 87 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+), 54 deletions(-)
arch/mips/ath79/clock.c +17 -54
index 8c5eaa4ab6e0..85f747cf37a8 100644
@@ -25,9 +25,6 @@
 #include <asm/mach-ath79/ar71xx_regs.h>
 #include "common.h"
 
-#define AR71XX_BASE_FREQ	40000000
-#define AR724X_BASE_FREQ	40000000
-
 static struct clk *clks[ATH79_CLK_END];
 static struct clk_onecell_data clk_data = {
 	.clks = clks,
@@ -78,16 +75,12 @@ static struct clk * __init ath79_set_ff_clk(int type, const char *parent,
 	return clk;
 }
 
-static unsigned long __init ath79_setup_ref_clk(unsigned long rate)
+static unsigned long __init ath79_get_ref_rate(void)
 {
-	struct clk *clk = clks[ATH79_CLK_REF];
-
-	if (clk)
-		rate = clk_get_rate(clk);
-	else
-		clk = ath79_set_clk(ATH79_CLK_REF, rate);
+	if (!clks[ATH79_CLK_REF])
+		panic("ath79: no reference clock");
 
-	return rate;
+	return clk_get_rate(clks[ATH79_CLK_REF]);
 }
 
 static void __init ar71xx_clocks_init(void __iomem *pll_base)
@@ -100,7 +93,7 @@ static void __init ar71xx_clocks_init(void __iomem *pll_base)
 	u32 freq;
 	u32 div;
 
-	ref_rate = ath79_setup_ref_clk(AR71XX_BASE_FREQ);
+	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + AR71XX_PLL_REG_CPU_CONFIG);
 
@@ -126,7 +119,7 @@ static void __init ar724x_clocks_init(void __iomem *pll_base)
 	u32 mult, div, ddr_div, ahb_div;
 	u32 pll;
 
-	ath79_setup_ref_clk(AR71XX_BASE_FREQ);
+	ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + AR724X_PLL_REG_CPU_CONFIG);
 
@@ -143,7 +136,6 @@ static void __init ar724x_clocks_init(void __iomem *pll_base)
 
 static void __init ar933x_clocks_init(void __iomem *pll_base)
 {
-	unsigned long ref_rate;
 	u32 clock_ctrl;
 	u32 ref_div;
 	u32 ninit_mul;
@@ -152,15 +144,8 @@ static void __init ar933x_clocks_init(void __iomem *pll_base)
 	u32 cpu_div;
 	u32 ddr_div;
 	u32 ahb_div;
-	u32 t;
 
-	t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP);
-	if (t & AR933X_BOOTSTRAP_REF_CLK_40)
-		ref_rate = (40 * 1000 * 1000);
-	else
-		ref_rate = (25 * 1000 * 1000);
-
-	ath79_setup_ref_clk(ref_rate);
+	ath79_get_ref_rate();
 
 	clock_ctrl = __raw_readl(pll_base + AR933X_PLL_CLOCK_CTRL_REG);
 	if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) {
@@ -237,18 +222,11 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 	unsigned long ahb_rate;
 	u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv;
 	u32 cpu_pll, ddr_pll;
-	u32 bootstrap;
 	void __iomem *dpll_base;
 
 	dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE);
 
-	bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP);
-	if (bootstrap & AR934X_BOOTSTRAP_REF_CLK_40)
-		ref_rate = 40 * 1000 * 1000;
-	else
-		ref_rate = 25 * 1000 * 1000;
-
-	ref_rate = ath79_setup_ref_clk(ref_rate);
+	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL2_REG);
 	if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) {
@@ -355,15 +333,8 @@ 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;
-
-	bootstrap = ath79_reset_rr(QCA953X_RESET_REG_BOOTSTRAP);
-	if (bootstrap &	QCA953X_BOOTSTRAP_REF_CLK_40)
-		ref_rate = 40 * 1000 * 1000;
-	else
-		ref_rate = 25 * 1000 * 1000;
 
-	ref_rate = ath79_setup_ref_clk(ref_rate);
+	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + QCA953X_PLL_CPU_CONFIG_REG);
 	out_div = (pll >> QCA953X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
@@ -438,15 +409,8 @@ static void __init qca955x_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;
 
-	bootstrap = ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP);
-	if (bootstrap &	QCA955X_BOOTSTRAP_REF_CLK_40)
-		ref_rate = 40 * 1000 * 1000;
-	else
-		ref_rate = 25 * 1000 * 1000;
-
-	ref_rate = ath79_setup_ref_clk(ref_rate);
+	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + QCA955X_PLL_CPU_CONFIG_REG);
 	out_div = (pll >> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
@@ -521,15 +485,8 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	unsigned long ahb_rate;
 	u32 pll, out_div, ref_div, nint, hfrac, lfrac, clk_ctrl, postdiv;
 	u32 cpu_pll, ddr_pll;
-	u32 bootstrap;
-
-	bootstrap = ath79_reset_rr(QCA956X_RESET_REG_BOOTSTRAP);
-	if (bootstrap &	QCA956X_BOOTSTRAP_REF_CLK_40)
-		ref_rate = 40 * 1000 * 1000;
-	else
-		ref_rate = 25 * 1000 * 1000;
 
-	ref_rate = ath79_setup_ref_clk(ref_rate);
+	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + QCA956X_PLL_CPU_CONFIG_REG);
 	out_div = (pll >> QCA956X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
@@ -610,7 +567,13 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	struct clk *ref_clk;
 	void __iomem *pll_base;
 
+	/*
+	 * The board's crystal. Without a rate in the device tree, the arch
+	 * code registers it from the bootstrap pins.
+	 */
 	ref_clk = of_clk_get(np, 0);
+	if (IS_ERR(ref_clk))
+		ref_clk = clk_get_sys(NULL, "ref");
 	if (!IS_ERR(ref_clk))
 		clks[ATH79_CLK_REF] = ref_clk;
 
arch/mips/ath79/setup.c +87 -0

105 lines not on this page · raw message

Signature · 1 line
2.56.0
#3 lore raw

PATCH 3/8 clk: ath79: move the ath79 clock driver to drivers/clk

In reply to Rosen Penev

The ath79 PLL clock code only uses the clk API and its own PLL and SRIF registers now, so move it from arch/mips/ath79 to drivers/clk, like the Ralink clocks before it. Copy the register definitions it uses from asm/mach-ath79/ar71xx_regs.h into the driver, so it no longer needs the mach headers, and drop the unused ath79.h and common.h.

The driver stays a CLK_OF_DECLARE one, as the MIPS timer needs the CPU clock from plat_time_init(). COMMON_CLK_ATH79 defaults to ATH79.

---
 MAINTAINERS                                   |   8 +
 arch/mips/ath79/Makefile                      |   2 +-
 drivers/clk/Kconfig                           |  12 ++
 drivers/clk/Makefile                          |   1 +
 .../ath79/clock.c => drivers/clk/clk-ath79.c  |   6 +-
 drivers/clk/clk-ath79.h                       | 194 ++++++++++++++++++
 6 files changed, 218 insertions(+), 5 deletions(-)
 rename arch/mips/ath79/clock.c => drivers/clk/clk-ath79.c (99%)
 create mode 100644 drivers/clk/clk-ath79.h
MAINTAINERS +8 -0
index 46c416cadacd..d1d11bb49875 100644
@@ -4352,6 +4352,14 @@ S:	Supported
 F:	Documentation/devicetree/bindings/rtc/andestech,atcrtc100.yaml
 F:	drivers/rtc/rtc-atcrtc100.c
 
+ATHEROS 71XX/9XXX CLOCK DRIVER
+M:	Alban Bedel <[email protected]>
+M:	Antony Pavlov <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/clock/qca,ath79-pll.yaml
+F:	drivers/clk/clk-ath79.*
+F:	include/dt-bindings/clock/ath79-clk.h
+
 ATHEROS 71XX/9XXX GPIO DRIVER
 M:	Alban Bedel <[email protected]>
 S:	Maintained
arch/mips/ath79/Makefile +1 -1
index 0fb3aaf42149..5df55b5e1c15 100644
@@ -6,6 +6,6 @@
 # Copyright (C) 2008 Imre Kaloz <[email protected]>
 #
 
-obj-y	:= prom.o setup.o common.o clock.o
+obj-y	:= prom.o setup.o common.o
 
 obj-$(CONFIG_EARLY_PRINTK)		+= early_printk.o
drivers/clk/Kconfig +12 -0
index 37a8e35d4178..9a3ca7d31c06 100644
@@ -66,6 +66,18 @@ config COMMON_CLK_APPLE_NCO
 	  found on Apple SoCs such as t8103 (M1). The blocks are typically
 	  generators of audio clocks.
 
+config COMMON_CLK_ATH79
+	bool "Clock driver for Atheros/Qualcomm ath79 SoCs" if COMPILE_TEST
+	depends on ATH79 || COMPILE_TEST
+	depends on OF
+	default ATH79
+	help
+	  Support for the PLL controller of the Atheros/Qualcomm ath79
+	  family of MIPS SoCs: AR71xx, AR724x, AR913x, AR933x, AR934x,
+	  QCA953x, QCA955x and QCA956x. It provides the CPU, DDR, AHB and
+	  MDIO clocks, derived from the board's reference clock. The clocks
+	  are set up early, as the MIPS timer needs the CPU clock.
+
 config COMMON_CLK_MAX77686
 	tristate "Clock driver for Maxim 77620/77686/77802 MFD"
 	depends on MFD_MAX77686 || MFD_MAX77620 || COMPILE_TEST
drivers/clk/Makefile +1 -0
index 52d138d4ffc8..589d2ef9dc89 100644
@@ -58,6 +58,7 @@ endif
 # please keep this section sorted lexicographically by file path name
 obj-$(CONFIG_COMMON_CLK_APPLE_NCO)  	+= clk-apple-nco.o
 obj-$(CONFIG_MACH_ASM9260)		+= clk-asm9260.o
+obj-$(CONFIG_COMMON_CLK_ATH79)		+= clk-ath79.o
 obj-$(CONFIG_COMMON_CLK_AXI_CLKGEN)	+= clk-axi-clkgen.o
 obj-$(CONFIG_ARCH_AXXIA)		+= clk-axm5516.o
 obj-$(CONFIG_COMMON_CLK_BD718XX)	+= clk-bd718x7.o
drivers/clk/clk-ath79.c renamed from arch/mips/ath79/clock.c +2 -4
similarity index 99%
rename from arch/mips/ath79/clock.c
rename to drivers/clk/clk-ath79.c
index 85f747cf37a8..c5da404c9c48 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- *  Atheros AR71XX/AR724X/AR913X common routines
+ *  Atheros/Qualcomm ath79 PLL clocks
  *
  *  Copyright (C) 2010-2011 Jaiganesh Narayanan <[email protected]>
  *  Copyright (C) 2011 Gabor Juhos <[email protected]>
@@ -21,9 +21,7 @@
 
 #include <asm/div64.h>
 
-#include <asm/mach-ath79/ath79.h>
-#include <asm/mach-ath79/ar71xx_regs.h>
-#include "common.h"
+#include "clk-ath79.h"
 
 static struct clk *clks[ATH79_CLK_END];
 static struct clk_onecell_data clk_data = {
drivers/clk/clk-ath79.h new file +194 -0

194 lines not on this page · raw message

Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH 3/8] clk: ath79: move the ath79 clock driver to drivers/clk

Hi Rosen,

Quoted 10 lines · Rosen Penev

The ath79 PLL clock code only uses the clk API and its own PLL and SRIF registers now, so move it from arch/mips/ath79 to drivers/clk, like the Ralink clocks before it. Copy the register definitions it uses from asm/mach-ath79/ar71xx_regs.h into the driver, so it no longer needs the mach headers, and drop the unused ath79.h and common.h.

The driver stays a CLK_OF_DECLARE one, as the MIPS timer needs the CPU clock from plat_time_init(). COMMON_CLK_ATH79 defaults to ATH79.

This patch itself looks straight forward, however looking at the original driver it needs a lot of work to be cleaned up. To make it easier to review, I suggest that you clean up the driver all in one go, and post it as a brand new version to drivers/clk.

Also for the CLK_OF_DECLARE, we are trying to limit new drivers that use this. Please have just the timer clock(s) declared with CLK_OF_DECLARE, and have all of the others as a standard platform driver.

Brian

#5 lore raw

PATCH 4/8 clk: ath79: use FIELD_GET() for the PLL register fields

In reply to Rosen Penev

Describe the PLL and SRIF register fields with GENMASK() instead of separate _SHIFT and _MASK pairs, and extract them with FIELD_GET() instead of open-coded shifts and masks.

No functional change: the generated code is the same, apart from a register allocation swap in ar933x_clocks_init().

---
 drivers/clk/clk-ath79.c | 199 +++++++++++---------------------
 drivers/clk/clk-ath79.h | 246 ++++++++++++++++------------------------
 2 files changed, 165 insertions(+), 280 deletions(-)
drivers/clk/clk-ath79.c +69 -130
index c5da404c9c48..8279d58be318 100644
@@ -8,6 +8,7 @@
  *  Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP
  */
 
+#include <linux/bitfield.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/io.h>
@@ -95,16 +96,16 @@ static void __init ar71xx_clocks_init(void __iomem *pll_base)
 
 	pll = __raw_readl(pll_base + AR71XX_PLL_REG_CPU_CONFIG);
 
-	div = ((pll >> AR71XX_PLL_FB_SHIFT) & AR71XX_PLL_FB_MASK) + 1;
+	div = FIELD_GET(AR71XX_PLL_FB, pll) + 1;
 	freq = div * ref_rate;
 
-	div = ((pll >> AR71XX_CPU_DIV_SHIFT) & AR71XX_CPU_DIV_MASK) + 1;
+	div = FIELD_GET(AR71XX_CPU_DIV, pll) + 1;
 	cpu_rate = freq / div;
 
-	div = ((pll >> AR71XX_DDR_DIV_SHIFT) & AR71XX_DDR_DIV_MASK) + 1;
+	div = FIELD_GET(AR71XX_DDR_DIV, pll) + 1;
 	ddr_rate = freq / div;
 
-	div = (((pll >> AR71XX_AHB_DIV_SHIFT) & AR71XX_AHB_DIV_MASK) + 1) * 2;
+	div = (FIELD_GET(AR71XX_AHB_DIV, pll) + 1) * 2;
 	ahb_rate = cpu_rate / div;
 
 	ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
@@ -121,11 +122,11 @@ static void __init ar724x_clocks_init(void __iomem *pll_base)
 
 	pll = __raw_readl(pll_base + AR724X_PLL_REG_CPU_CONFIG);
 
-	mult = ((pll >> AR724X_PLL_FB_SHIFT) & AR724X_PLL_FB_MASK);
-	div = ((pll >> AR724X_PLL_REF_DIV_SHIFT) & AR724X_PLL_REF_DIV_MASK) * 2;
+	mult = FIELD_GET(AR724X_PLL_FB, pll);
+	div = FIELD_GET(AR724X_PLL_REF_DIV, pll) * 2;
 
-	ddr_div = ((pll >> AR724X_DDR_DIV_SHIFT) & AR724X_DDR_DIV_MASK) + 1;
-	ahb_div = (((pll >> AR724X_AHB_DIV_SHIFT) & AR724X_AHB_DIV_MASK) + 1) * 2;
+	ddr_div = FIELD_GET(AR724X_DDR_DIV, pll) + 1;
+	ahb_div = (FIELD_GET(AR724X_AHB_DIV, pll) + 1) * 2;
 
 	ath79_set_ff_clk(ATH79_CLK_CPU, "ref", mult, div);
 	ath79_set_ff_clk(ATH79_CLK_DDR, "ref", mult, div * ddr_div);
@@ -160,28 +161,18 @@ static void __init ar933x_clocks_init(void __iomem *pll_base)
 
 		cpu_config = __raw_readl(pll_base + AR933X_PLL_CPU_CONFIG_REG);
 
-		t = (cpu_config >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
-		    AR933X_PLL_CPU_CONFIG_REFDIV_MASK;
-		ref_div = t;
+		ref_div = FIELD_GET(AR933X_PLL_CPU_CONFIG_REFDIV, cpu_config);
+		ninit_mul = FIELD_GET(AR933X_PLL_CPU_CONFIG_NINT, cpu_config);
 
-		ninit_mul = (cpu_config >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT) &
-		    AR933X_PLL_CPU_CONFIG_NINT_MASK;
-
-		t = (cpu_config >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
-		    AR933X_PLL_CPU_CONFIG_OUTDIV_MASK;
+		t = FIELD_GET(AR933X_PLL_CPU_CONFIG_OUTDIV, cpu_config);
 		if (t == 0)
 			t = 1;
 
 		out_div = (1 << t);
 
-		cpu_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT) &
-		     AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK) + 1;
-
-		ddr_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) &
-		      AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1;
-
-		ahb_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) &
-		     AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1;
+		cpu_div = FIELD_GET(AR933X_PLL_CLOCK_CTRL_CPU_DIV, clock_ctrl) + 1;
+		ddr_div = FIELD_GET(AR933X_PLL_CLOCK_CTRL_DDR_DIV, clock_ctrl) + 1;
+		ahb_div = FIELD_GET(AR933X_PLL_CLOCK_CTRL_AHB_DIV, clock_ctrl) + 1;
 	}
 
 	ath79_set_ff_clk(ATH79_CLK_CPU, "ref", ninit_mul,
@@ -228,25 +219,18 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 
 	pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL2_REG);
 	if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) {
-		out_div = (pll >> AR934X_SRIF_DPLL2_OUTDIV_SHIFT) &
-			  AR934X_SRIF_DPLL2_OUTDIV_MASK;
+		out_div = FIELD_GET(AR934X_SRIF_DPLL2_OUTDIV, pll);
 		pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL1_REG);
-		nint = (pll >> AR934X_SRIF_DPLL1_NINT_SHIFT) &
-		       AR934X_SRIF_DPLL1_NINT_MASK;
-		nfrac = pll & AR934X_SRIF_DPLL1_NFRAC_MASK;
-		ref_div = (pll >> AR934X_SRIF_DPLL1_REFDIV_SHIFT) &
-			  AR934X_SRIF_DPLL1_REFDIV_MASK;
+		nint = FIELD_GET(AR934X_SRIF_DPLL1_NINT, pll);
+		nfrac = FIELD_GET(AR934X_SRIF_DPLL1_NFRAC, pll);
+		ref_div = FIELD_GET(AR934X_SRIF_DPLL1_REFDIV, pll);
 		frac = 1 << 18;
 	} else {
 		pll = __raw_readl(pll_base + AR934X_PLL_CPU_CONFIG_REG);
-		out_div = (pll >> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
-			AR934X_PLL_CPU_CONFIG_OUTDIV_MASK;
-		ref_div = (pll >> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
-			  AR934X_PLL_CPU_CONFIG_REFDIV_MASK;
-		nint = (pll >> AR934X_PLL_CPU_CONFIG_NINT_SHIFT) &
-		       AR934X_PLL_CPU_CONFIG_NINT_MASK;
-		nfrac = (pll >> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
-			AR934X_PLL_CPU_CONFIG_NFRAC_MASK;
+		out_div = FIELD_GET(AR934X_PLL_CPU_CONFIG_OUTDIV, pll);
+		ref_div = FIELD_GET(AR934X_PLL_CPU_CONFIG_REFDIV, pll);
+		nint = FIELD_GET(AR934X_PLL_CPU_CONFIG_NINT, pll);
+		nfrac = FIELD_GET(AR934X_PLL_CPU_CONFIG_NFRAC, pll);
 		frac = 1 << 6;
 	}
 
@@ -255,25 +239,18 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 
 	pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL2_REG);
 	if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) {
-		out_div = (pll >> AR934X_SRIF_DPLL2_OUTDIV_SHIFT) &
-			  AR934X_SRIF_DPLL2_OUTDIV_MASK;
+		out_div = FIELD_GET(AR934X_SRIF_DPLL2_OUTDIV, pll);
 		pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL1_REG);
-		nint = (pll >> AR934X_SRIF_DPLL1_NINT_SHIFT) &
-		       AR934X_SRIF_DPLL1_NINT_MASK;
-		nfrac = pll & AR934X_SRIF_DPLL1_NFRAC_MASK;
-		ref_div = (pll >> AR934X_SRIF_DPLL1_REFDIV_SHIFT) &
-			  AR934X_SRIF_DPLL1_REFDIV_MASK;
+		nint = FIELD_GET(AR934X_SRIF_DPLL1_NINT, pll);
+		nfrac = FIELD_GET(AR934X_SRIF_DPLL1_NFRAC, pll);
+		ref_div = FIELD_GET(AR934X_SRIF_DPLL1_REFDIV, pll);
 		frac = 1 << 18;
 	} else {
 		pll = __raw_readl(pll_base + AR934X_PLL_DDR_CONFIG_REG);
-		out_div = (pll >> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
-			  AR934X_PLL_DDR_CONFIG_OUTDIV_MASK;
-		ref_div = (pll >> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
-			   AR934X_PLL_DDR_CONFIG_REFDIV_MASK;
-		nint = (pll >> AR934X_PLL_DDR_CONFIG_NINT_SHIFT) &
-		       AR934X_PLL_DDR_CONFIG_NINT_MASK;
-		nfrac = (pll >> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
-			AR934X_PLL_DDR_CONFIG_NFRAC_MASK;
+		out_div = FIELD_GET(AR934X_PLL_DDR_CONFIG_OUTDIV, pll);
+		ref_div = FIELD_GET(AR934X_PLL_DDR_CONFIG_REFDIV, pll);
+		nint = FIELD_GET(AR934X_PLL_DDR_CONFIG_NINT, pll);
+		nfrac = FIELD_GET(AR934X_PLL_DDR_CONFIG_NFRAC, pll);
 		frac = 1 << 10;
 	}
 
@@ -282,8 +259,7 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 
 	clk_ctrl = __raw_readl(pll_base + AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
 
-	postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT) &
-		  AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK;
+	postdiv = FIELD_GET(AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS)
 		cpu_rate = ref_rate;
@@ -292,8 +268,7 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 	else
 		cpu_rate = ddr_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT) &
-		  AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK;
+	postdiv = FIELD_GET(AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS)
 		ddr_rate = ref_rate;
@@ -302,8 +277,7 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 	else
 		ddr_rate = cpu_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT) &
-		  AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK;
+	postdiv = FIELD_GET(AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS)
 		ahb_rate = ref_rate;
@@ -335,28 +309,20 @@ static void __init qca953x_clocks_init(void __iomem *pll_base)
 	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + QCA953X_PLL_CPU_CONFIG_REG);
-	out_div = (pll >> QCA953X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
-		  QCA953X_PLL_CPU_CONFIG_OUTDIV_MASK;
-	ref_div = (pll >> QCA953X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
-		  QCA953X_PLL_CPU_CONFIG_REFDIV_MASK;
-	nint = (pll >> QCA953X_PLL_CPU_CONFIG_NINT_SHIFT) &
-	       QCA953X_PLL_CPU_CONFIG_NINT_MASK;
-	frac = (pll >> QCA953X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
-	       QCA953X_PLL_CPU_CONFIG_NFRAC_MASK;
+	out_div = FIELD_GET(QCA953X_PLL_CPU_CONFIG_OUTDIV, pll);
+	ref_div = FIELD_GET(QCA953X_PLL_CPU_CONFIG_REFDIV, pll);
+	nint = FIELD_GET(QCA953X_PLL_CPU_CONFIG_NINT, pll);
+	frac = FIELD_GET(QCA953X_PLL_CPU_CONFIG_NFRAC, pll);
 
 	cpu_pll = nint * ref_rate / ref_div;
 	cpu_pll += frac * (ref_rate >> 6) / ref_div;
 	cpu_pll /= (1 << out_div);
 
 	pll = __raw_readl(pll_base + QCA953X_PLL_DDR_CONFIG_REG);
-	out_div = (pll >> QCA953X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
-		  QCA953X_PLL_DDR_CONFIG_OUTDIV_MASK;
-	ref_div = (pll >> QCA953X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
-		  QCA953X_PLL_DDR_CONFIG_REFDIV_MASK;
-	nint = (pll >> QCA953X_PLL_DDR_CONFIG_NINT_SHIFT) &
-	       QCA953X_PLL_DDR_CONFIG_NINT_MASK;
-	frac = (pll >> QCA953X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
-	       QCA953X_PLL_DDR_CONFIG_NFRAC_MASK;
+	out_div = FIELD_GET(QCA953X_PLL_DDR_CONFIG_OUTDIV, pll);
+	ref_div = FIELD_GET(QCA953X_PLL_DDR_CONFIG_REFDIV, pll);
+	nint = FIELD_GET(QCA953X_PLL_DDR_CONFIG_NINT, pll);
+	frac = FIELD_GET(QCA953X_PLL_DDR_CONFIG_NFRAC, pll);
 
 	ddr_pll = nint * ref_rate / ref_div;
 	ddr_pll += frac * (ref_rate >> 6) / (ref_div << 4);
@@ -364,8 +330,7 @@ static void __init qca953x_clocks_init(void __iomem *pll_base)
 
 	clk_ctrl = __raw_readl(pll_base + QCA953X_PLL_CLK_CTRL_REG);
 
-	postdiv = (clk_ctrl >> QCA953X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
-		  QCA953X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA953X_PLL_CLK_CTRL_CPU_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA953X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
 		cpu_rate = ref_rate;
@@ -374,8 +339,7 @@ static void __init qca953x_clocks_init(void __iomem *pll_base)
 	else
 		cpu_rate = ddr_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> QCA953X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
-		  QCA953X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA953X_PLL_CLK_CTRL_DDR_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA953X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
 		ddr_rate = ref_rate;
@@ -384,8 +348,7 @@ static void __init qca953x_clocks_init(void __iomem *pll_base)
 	else
 		ddr_rate = cpu_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> QCA953X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
-		  QCA953X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA953X_PLL_CLK_CTRL_AHB_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA953X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
 		ahb_rate = ref_rate;
@@ -411,28 +374,20 @@ static void __init qca955x_clocks_init(void __iomem *pll_base)
 	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + QCA955X_PLL_CPU_CONFIG_REG);
-	out_div = (pll >> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
-		  QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK;
-	ref_div = (pll >> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
-		  QCA955X_PLL_CPU_CONFIG_REFDIV_MASK;
-	nint = (pll >> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT) &
-	       QCA955X_PLL_CPU_CONFIG_NINT_MASK;
-	frac = (pll >> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
-	       QCA955X_PLL_CPU_CONFIG_NFRAC_MASK;
+	out_div = FIELD_GET(QCA955X_PLL_CPU_CONFIG_OUTDIV, pll);
+	ref_div = FIELD_GET(QCA955X_PLL_CPU_CONFIG_REFDIV, pll);
+	nint = FIELD_GET(QCA955X_PLL_CPU_CONFIG_NINT, pll);
+	frac = FIELD_GET(QCA955X_PLL_CPU_CONFIG_NFRAC, pll);
 
 	cpu_pll = nint * ref_rate / ref_div;
 	cpu_pll += frac * ref_rate / (ref_div * (1 << 6));
 	cpu_pll /= (1 << out_div);
 
 	pll = __raw_readl(pll_base + QCA955X_PLL_DDR_CONFIG_REG);
-	out_div = (pll >> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
-		  QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK;
-	ref_div = (pll >> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
-		  QCA955X_PLL_DDR_CONFIG_REFDIV_MASK;
-	nint = (pll >> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT) &
-	       QCA955X_PLL_DDR_CONFIG_NINT_MASK;
-	frac = (pll >> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
-	       QCA955X_PLL_DDR_CONFIG_NFRAC_MASK;
+	out_div = FIELD_GET(QCA955X_PLL_DDR_CONFIG_OUTDIV, pll);
+	ref_div = FIELD_GET(QCA955X_PLL_DDR_CONFIG_REFDIV, pll);
+	nint = FIELD_GET(QCA955X_PLL_DDR_CONFIG_NINT, pll);
+	frac = FIELD_GET(QCA955X_PLL_DDR_CONFIG_NFRAC, pll);
 
 	ddr_pll = nint * ref_rate / ref_div;
 	ddr_pll += frac * ref_rate / (ref_div * (1 << 10));
@@ -440,8 +395,7 @@ static void __init qca955x_clocks_init(void __iomem *pll_base)
 
 	clk_ctrl = __raw_readl(pll_base + QCA955X_PLL_CLK_CTRL_REG);
 
-	postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
-		  QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA955X_PLL_CLK_CTRL_CPU_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
 		cpu_rate = ref_rate;
@@ -450,8 +404,7 @@ static void __init qca955x_clocks_init(void __iomem *pll_base)
 	else
 		cpu_rate = cpu_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
-		  QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA955X_PLL_CLK_CTRL_DDR_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
 		ddr_rate = ref_rate;
@@ -460,8 +413,7 @@ static void __init qca955x_clocks_init(void __iomem *pll_base)
 	else
 		ddr_rate = ddr_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
-		  QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA955X_PLL_CLK_CTRL_AHB_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
 		ahb_rate = ref_rate;
@@ -487,18 +439,13 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	ref_rate = ath79_get_ref_rate();
 
 	pll = __raw_readl(pll_base + QCA956X_PLL_CPU_CONFIG_REG);
-	out_div = (pll >> QCA956X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
-		  QCA956X_PLL_CPU_CONFIG_OUTDIV_MASK;
-	ref_div = (pll >> QCA956X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
-		  QCA956X_PLL_CPU_CONFIG_REFDIV_MASK;
+	out_div = FIELD_GET(QCA956X_PLL_CPU_CONFIG_OUTDIV, pll);
+	ref_div = FIELD_GET(QCA956X_PLL_CPU_CONFIG_REFDIV, pll);
 
 	pll = __raw_readl(pll_base + QCA956X_PLL_CPU_CONFIG1_REG);
-	nint = (pll >> QCA956X_PLL_CPU_CONFIG1_NINT_SHIFT) &
-	       QCA956X_PLL_CPU_CONFIG1_NINT_MASK;
-	hfrac = (pll >> QCA956X_PLL_CPU_CONFIG1_NFRAC_H_SHIFT) &
-	       QCA956X_PLL_CPU_CONFIG1_NFRAC_H_MASK;
-	lfrac = (pll >> QCA956X_PLL_CPU_CONFIG1_NFRAC_L_SHIFT) &
-	       QCA956X_PLL_CPU_CONFIG1_NFRAC_L_MASK;
+	nint = FIELD_GET(QCA956X_PLL_CPU_CONFIG1_NINT, pll);
+	hfrac = FIELD_GET(QCA956X_PLL_CPU_CONFIG1_NFRAC_H, pll);
+	lfrac = FIELD_GET(QCA956X_PLL_CPU_CONFIG1_NFRAC_L, pll);
 
 	cpu_pll = nint * ref_rate / ref_div;
 	cpu_pll += (lfrac * ref_rate) / ((ref_div * 25) << 13);
@@ -506,17 +453,12 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	cpu_pll /= (1 << out_div);
 
 	pll = __raw_readl(pll_base + QCA956X_PLL_DDR_CONFIG_REG);
-	out_div = (pll >> QCA956X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
-		  QCA956X_PLL_DDR_CONFIG_OUTDIV_MASK;
-	ref_div = (pll >> QCA956X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
-		  QCA956X_PLL_DDR_CONFIG_REFDIV_MASK;
+	out_div = FIELD_GET(QCA956X_PLL_DDR_CONFIG_OUTDIV, pll);
+	ref_div = FIELD_GET(QCA956X_PLL_DDR_CONFIG_REFDIV, pll);
 	pll = __raw_readl(pll_base + QCA956X_PLL_DDR_CONFIG1_REG);
-	nint = (pll >> QCA956X_PLL_DDR_CONFIG1_NINT_SHIFT) &
-	       QCA956X_PLL_DDR_CONFIG1_NINT_MASK;
-	hfrac = (pll >> QCA956X_PLL_DDR_CONFIG1_NFRAC_H_SHIFT) &
-	       QCA956X_PLL_DDR_CONFIG1_NFRAC_H_MASK;
-	lfrac = (pll >> QCA956X_PLL_DDR_CONFIG1_NFRAC_L_SHIFT) &
-	       QCA956X_PLL_DDR_CONFIG1_NFRAC_L_MASK;
+	nint = FIELD_GET(QCA956X_PLL_DDR_CONFIG1_NINT, pll);
+	hfrac = FIELD_GET(QCA956X_PLL_DDR_CONFIG1_NFRAC_H, pll);
+	lfrac = FIELD_GET(QCA956X_PLL_DDR_CONFIG1_NFRAC_L, pll);
 
 	ddr_pll = nint * ref_rate / ref_div;
 	ddr_pll += (lfrac * ref_rate) / ((ref_div * 25) << 13);
@@ -525,8 +467,7 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 
 	clk_ctrl = __raw_readl(pll_base + QCA956X_PLL_CLK_CTRL_REG);
 
-	postdiv = (clk_ctrl >> QCA956X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
-		  QCA956X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA956X_PLL_CLK_CTRL_CPU_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA956X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
 		cpu_rate = ref_rate;
@@ -535,8 +476,7 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	else
 		cpu_rate = cpu_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> QCA956X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
-		  QCA956X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA956X_PLL_CLK_CTRL_DDR_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA956X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
 		ddr_rate = ref_rate;
@@ -545,8 +485,7 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	else
 		ddr_rate = ddr_pll / (postdiv + 1);
 
-	postdiv = (clk_ctrl >> QCA956X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
-		  QCA956X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
+	postdiv = FIELD_GET(QCA956X_PLL_CLK_CTRL_AHB_POST_DIV, clk_ctrl);
 
 	if (clk_ctrl & QCA956X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
 		ahb_rate = ref_rate;
drivers/clk/clk-ath79.h +96 -150

282 lines not on this page · raw message

Signature · 1 line
2.56.0
#6 lore raw

PATCH 5/8 clk: ath79: convert to clk_hw based registration

In reply to Rosen Penev

Register the clocks with the clk_hw API and provide them with of_clk_add_hw_provider() and of_clk_hw_onecell_get(), instead of the struct clk based clk_register_*() and of_clk_src_onecell_get().

The AR724x and AR933x CPU, DDR and AHB clocks are fixed factors of the reference clock, but named their parent "ref". That only works when the reference clock is registered under that name, as the fallback from the bootstrap pins is, or a device tree fixed clock whose node happens to be called "ref". With any other node name, the clocks were orphans and ran at 0 Hz. Give them the reference clock's clk_hw as the parent instead.

---
 drivers/clk/clk-ath79.c | 68 ++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 32 deletions(-)
drivers/clk/clk-ath79.c +36 -32
index 8279d58be318..3afda29377b5 100644
@@ -18,17 +18,14 @@
 #include <linux/clk-provider.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
+#include <linux/slab.h>
 #include <dt-bindings/clock/ath79-clk.h>
 
 #include <asm/div64.h>
 
 #include "clk-ath79.h"
 
-static struct clk *clks[ATH79_CLK_END];
-static struct clk_onecell_data clk_data = {
-	.clks = clks,
-	.clk_num = ARRAY_SIZE(clks),
-};
+static struct clk_hw_onecell_data *clk_data;
 
 static const char * const clk_names[ATH79_CLK_END] = {
 	[ATH79_CLK_CPU] = "cpu",
@@ -44,42 +41,44 @@ static const char * __init ath79_clk_name(int type)
 	return clk_names[type];
 }
 
-static void __init __ath79_set_clk(int type, const char *name, struct clk *clk)
+static void __init __ath79_set_clk(int type, const char *name,
+				   struct clk_hw *hw)
 {
-	if (IS_ERR(clk))
+	if (IS_ERR(hw))
 		panic("failed to allocate %s clock structure", clk_names[type]);
 
-	clks[type] = clk;
-	clk_register_clkdev(clk, name, NULL);
+	clk_data->hws[type] = hw;
+	clk_hw_register_clkdev(hw, name, NULL);
 }
 
-static struct clk * __init ath79_set_clk(int type, unsigned long rate)
+static void __init ath79_set_clk(int type, unsigned long rate)
 {
 	const char *name = ath79_clk_name(type);
-	struct clk *clk;
+	struct clk_hw *hw;
 
-	clk = clk_register_fixed_rate(NULL, name, NULL, 0, rate);
-	__ath79_set_clk(type, name, clk);
-	return clk;
+	hw = clk_hw_register_fixed_rate(NULL, name, NULL, 0, rate);
+	__ath79_set_clk(type, name, hw);
 }
 
-static struct clk * __init ath79_set_ff_clk(int type, const char *parent,
-					    unsigned int mult, unsigned int div)
+/* a clock derived from the reference clock by a fixed factor */
+static void __init ath79_set_ff_clk(int type, unsigned int mult,
+				    unsigned int div)
 {
 	const char *name = ath79_clk_name(type);
-	struct clk *clk;
+	struct clk_hw *hw;
 
-	clk = clk_register_fixed_factor(NULL, name, parent, 0, mult, div);
-	__ath79_set_clk(type, name, clk);
-	return clk;
+	hw = clk_hw_register_fixed_factor_parent_hw(NULL, name,
+						    clk_data->hws[ATH79_CLK_REF],
+						    0, mult, div);
+	__ath79_set_clk(type, name, hw);
 }
 
 static unsigned long __init ath79_get_ref_rate(void)
 {
-	if (!clks[ATH79_CLK_REF])
+	if (!clk_data->hws[ATH79_CLK_REF])
 		panic("ath79: no reference clock");
 
-	return clk_get_rate(clks[ATH79_CLK_REF]);
+	return clk_hw_get_rate(clk_data->hws[ATH79_CLK_REF]);
 }
 
 static void __init ar71xx_clocks_init(void __iomem *pll_base)
@@ -128,9 +127,9 @@ static void __init ar724x_clocks_init(void __iomem *pll_base)
 	ddr_div = FIELD_GET(AR724X_DDR_DIV, pll) + 1;
 	ahb_div = (FIELD_GET(AR724X_AHB_DIV, pll) + 1) * 2;
 
-	ath79_set_ff_clk(ATH79_CLK_CPU, "ref", mult, div);
-	ath79_set_ff_clk(ATH79_CLK_DDR, "ref", mult, div * ddr_div);
-	ath79_set_ff_clk(ATH79_CLK_AHB, "ref", mult, div * ahb_div);
+	ath79_set_ff_clk(ATH79_CLK_CPU, mult, div);
+	ath79_set_ff_clk(ATH79_CLK_DDR, mult, div * ddr_div);
+	ath79_set_ff_clk(ATH79_CLK_AHB, mult, div * ahb_div);
 }
 
 static void __init ar933x_clocks_init(void __iomem *pll_base)
@@ -175,11 +174,11 @@ static void __init ar933x_clocks_init(void __iomem *pll_base)
 		ahb_div = FIELD_GET(AR933X_PLL_CLOCK_CTRL_AHB_DIV, clock_ctrl) + 1;
 	}
 
-	ath79_set_ff_clk(ATH79_CLK_CPU, "ref", ninit_mul,
+	ath79_set_ff_clk(ATH79_CLK_CPU, ninit_mul,
 			 ref_div * out_div * cpu_div);
-	ath79_set_ff_clk(ATH79_CLK_DDR, "ref", ninit_mul,
+	ath79_set_ff_clk(ATH79_CLK_DDR, ninit_mul,
 			 ref_div * out_div * ddr_div);
-	ath79_set_ff_clk(ATH79_CLK_AHB, "ref", ninit_mul,
+	ath79_set_ff_clk(ATH79_CLK_AHB, ninit_mul,
 			 ref_div * out_div * ahb_div);
 }
 
@@ -504,6 +503,11 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	struct clk *ref_clk;
 	void __iomem *pll_base;
 
+	clk_data = kzalloc_flex(*clk_data, hws, ATH79_CLK_END);
+	if (!clk_data)
+		panic("ath79: can't allocate the clock data");
+	clk_data->num = ATH79_CLK_END;
+
 	/*
 	 * The board's crystal. Without a rate in the device tree, the arch
 	 * code registers it from the bootstrap pins.
@@ -512,7 +516,7 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	if (IS_ERR(ref_clk))
 		ref_clk = clk_get_sys(NULL, "ref");
 	if (!IS_ERR(ref_clk))
-		clks[ATH79_CLK_REF] = ref_clk;
+		clk_data->hws[ATH79_CLK_REF] = __clk_get_hw(ref_clk);
 
 	pll_base = of_iomap(np, 0);
 	if (!pll_base) {
@@ -536,10 +540,10 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	else if (of_device_is_compatible(np, "qca,qca9560-pll"))
 		qca956x_clocks_init(pll_base);
 
-	if (!clks[ATH79_CLK_MDIO])
-		clks[ATH79_CLK_MDIO] = clks[ATH79_CLK_REF];
+	if (!clk_data->hws[ATH79_CLK_MDIO])
+		clk_data->hws[ATH79_CLK_MDIO] = clk_data->hws[ATH79_CLK_REF];
 
-	if (of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data)) {
+	if (of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data)) {
 		pr_err("%pOF: could not register clk provider\n", np);
 		goto err_iounmap;
 	}
Signature · 1 line
2.56.0
#7 lore raw

PATCH 6/8 clk: ath79: fix the error handling

In reply to Rosen Penev

Report errors instead of crashing, and clean up after them:

- Check the ioremap() of the AR934x SRIF block, which was used without a check.
- Without a reference clock, ath79_clocks_init_dt() called clk_put() on an error pointer, which warns, and left the stale pointer in the clock table. Bail out early instead.
- Return registration errors from the SoC init functions instead of panicking, log them, and free the clock data.
- Drop the BUG_ON() on the clock index, which is always a constant.

The PLL registers aren't needed once the rates are known, so unmap them after the SoC init in all cases, and unmap the SRIF block as soon as the DPLLs are read. Registering the CPU, DDR and AHB clocks together also removes some repetition.

If the clocks can't be set up, plat_time_init() still reports that it can't get the CPU clock.

---
 drivers/clk/clk-ath79.c | 179 ++++++++++++++++++++++------------------
 1 file changed, 100 insertions(+), 79 deletions(-)
drivers/clk/clk-ath79.c +100 -79
index 3afda29377b5..768c9e410b59 100644
@@ -35,53 +35,71 @@ static const char * const clk_names[ATH79_CLK_END] = {
 	[ATH79_CLK_MDIO] = "mdio",
 };
 
-static const char * __init ath79_clk_name(int type)
+static int __init ath79_add_clk(int type, struct clk_hw *hw)
 {
-	BUG_ON(type >= ARRAY_SIZE(clk_names) || !clk_names[type]);
-	return clk_names[type];
+	if (IS_ERR(hw))
+		return PTR_ERR(hw);
+
+	clk_data->hws[type] = hw;
+	clk_hw_register_clkdev(hw, clk_names[type], NULL);
+	return 0;
 }
 
-static void __init __ath79_set_clk(int type, const char *name,
-				   struct clk_hw *hw)
+static int __init ath79_set_clk(int type, unsigned long rate)
 {
-	if (IS_ERR(hw))
-		panic("failed to allocate %s clock structure", clk_names[type]);
+	return ath79_add_clk(type, clk_hw_register_fixed_rate(NULL,
+					clk_names[type], NULL, 0, rate));
+}
 
-	clk_data->hws[type] = hw;
-	clk_hw_register_clkdev(hw, name, NULL);
+/* a clock derived from the reference clock by a fixed factor */
+static int __init ath79_set_ff_clk(int type, unsigned int mult,
+				   unsigned int div)
+{
+	return ath79_add_clk(type, clk_hw_register_fixed_factor_parent_hw(NULL,
+					clk_names[type],
+					clk_data->hws[ATH79_CLK_REF],
+					0, mult, div));
 }
 
-static void __init ath79_set_clk(int type, unsigned long rate)
+static int __init ath79_set_clks(unsigned long cpu_rate,
+				 unsigned long ddr_rate,
+				 unsigned long ahb_rate)
 {
-	const char *name = ath79_clk_name(type);
-	struct clk_hw *hw;
+	int ret;
+
+	ret = ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
+	if (ret)
+		return ret;
 
-	hw = clk_hw_register_fixed_rate(NULL, name, NULL, 0, rate);
-	__ath79_set_clk(type, name, hw);
+	ret = ath79_set_clk(ATH79_CLK_DDR, ddr_rate);
+	if (ret)
+		return ret;
+
+	return ath79_set_clk(ATH79_CLK_AHB, ahb_rate);
 }
 
-/* a clock derived from the reference clock by a fixed factor */
-static void __init ath79_set_ff_clk(int type, unsigned int mult,
-				    unsigned int div)
+static int __init ath79_set_ff_clks(unsigned int mult, unsigned int cpu_div,
+				    unsigned int ddr_div, unsigned int ahb_div)
 {
-	const char *name = ath79_clk_name(type);
-	struct clk_hw *hw;
+	int ret;
 
-	hw = clk_hw_register_fixed_factor_parent_hw(NULL, name,
-						    clk_data->hws[ATH79_CLK_REF],
-						    0, mult, div);
-	__ath79_set_clk(type, name, hw);
+	ret = ath79_set_ff_clk(ATH79_CLK_CPU, mult, cpu_div);
+	if (ret)
+		return ret;
+
+	ret = ath79_set_ff_clk(ATH79_CLK_DDR, mult, ddr_div);
+	if (ret)
+		return ret;
+
+	return ath79_set_ff_clk(ATH79_CLK_AHB, mult, ahb_div);
 }
 
 static unsigned long __init ath79_get_ref_rate(void)
 {
-	if (!clk_data->hws[ATH79_CLK_REF])
-		panic("ath79: no reference clock");
-
 	return clk_hw_get_rate(clk_data->hws[ATH79_CLK_REF]);
 }
 
-static void __init ar71xx_clocks_init(void __iomem *pll_base)
+static int __init ar71xx_clocks_init(void __iomem *pll_base)
 {
 	unsigned long ref_rate;
 	unsigned long cpu_rate;
@@ -107,18 +125,14 @@ static void __init ar71xx_clocks_init(void __iomem *pll_base)
 	div = (FIELD_GET(AR71XX_AHB_DIV, pll) + 1) * 2;
 	ahb_rate = cpu_rate / div;
 
-	ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
-	ath79_set_clk(ATH79_CLK_DDR, ddr_rate);
-	ath79_set_clk(ATH79_CLK_AHB, ahb_rate);
+	return ath79_set_clks(cpu_rate, ddr_rate, ahb_rate);
 }
 
-static void __init ar724x_clocks_init(void __iomem *pll_base)
+static int __init ar724x_clocks_init(void __iomem *pll_base)
 {
 	u32 mult, div, ddr_div, ahb_div;
 	u32 pll;
 
-	ath79_get_ref_rate();
-
 	pll = __raw_readl(pll_base + AR724X_PLL_REG_CPU_CONFIG);
 
 	mult = FIELD_GET(AR724X_PLL_FB, pll);
@@ -127,12 +141,10 @@ static void __init ar724x_clocks_init(void __iomem *pll_base)
 	ddr_div = FIELD_GET(AR724X_DDR_DIV, pll) + 1;
 	ahb_div = (FIELD_GET(AR724X_AHB_DIV, pll) + 1) * 2;
 
-	ath79_set_ff_clk(ATH79_CLK_CPU, mult, div);
-	ath79_set_ff_clk(ATH79_CLK_DDR, mult, div * ddr_div);
-	ath79_set_ff_clk(ATH79_CLK_AHB, mult, div * ahb_div);
+	return ath79_set_ff_clks(mult, div, div * ddr_div, div * ahb_div);
 }
 
-static void __init ar933x_clocks_init(void __iomem *pll_base)
+static int __init ar933x_clocks_init(void __iomem *pll_base)
 {
 	u32 clock_ctrl;
 	u32 ref_div;
@@ -143,8 +155,6 @@ static void __init ar933x_clocks_init(void __iomem *pll_base)
 	u32 ddr_div;
 	u32 ahb_div;
 
-	ath79_get_ref_rate();
-
 	clock_ctrl = __raw_readl(pll_base + AR933X_PLL_CLOCK_CTRL_REG);
 	if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) {
 		ref_div = 1;
@@ -174,12 +184,9 @@ static void __init ar933x_clocks_init(void __iomem *pll_base)
 		ahb_div = FIELD_GET(AR933X_PLL_CLOCK_CTRL_AHB_DIV, clock_ctrl) + 1;
 	}
 
-	ath79_set_ff_clk(ATH79_CLK_CPU, ninit_mul,
-			 ref_div * out_div * cpu_div);
-	ath79_set_ff_clk(ATH79_CLK_DDR, ninit_mul,
-			 ref_div * out_div * ddr_div);
-	ath79_set_ff_clk(ATH79_CLK_AHB, ninit_mul,
-			 ref_div * out_div * ahb_div);
+	return ath79_set_ff_clks(ninit_mul, ref_div * out_div * cpu_div,
+				 ref_div * out_div * ddr_div,
+				 ref_div * out_div * ahb_div);
 }
 
 static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac,
@@ -202,7 +209,7 @@ static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac,
 	return ret;
 }
 
-static void __init ar934x_clocks_init(void __iomem *pll_base)
+static int __init ar934x_clocks_init(void __iomem *pll_base)
 {
 	unsigned long ref_rate;
 	unsigned long cpu_rate;
@@ -211,8 +218,11 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 	u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv;
 	u32 cpu_pll, ddr_pll;
 	void __iomem *dpll_base;
+	int ret;
 
 	dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE);
+	if (!dpll_base)
+		return -ENOMEM;
 
 	ref_rate = ath79_get_ref_rate();
 
@@ -256,6 +266,8 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 	ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
 				      nfrac, frac, out_div);
 
+	iounmap(dpll_base);
+
 	clk_ctrl = __raw_readl(pll_base + AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
 
 	postdiv = FIELD_GET(AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV, clk_ctrl);
@@ -285,18 +297,18 @@ static void __init ar934x_clocks_init(void __iomem *pll_base)
 	else
 		ahb_rate = cpu_pll / (postdiv + 1);
 
-	ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
-	ath79_set_clk(ATH79_CLK_DDR, ddr_rate);
-	ath79_set_clk(ATH79_CLK_AHB, ahb_rate);
+	ret = ath79_set_clks(cpu_rate, ddr_rate, ahb_rate);
+	if (ret)
+		return ret;
 
 	clk_ctrl = __raw_readl(pll_base + AR934X_PLL_SWITCH_CLOCK_CONTROL_REG);
 	if (clk_ctrl & AR934X_PLL_SWITCH_CLOCK_CONTROL_MDIO_CLK_SEL)
-		ath79_set_clk(ATH79_CLK_MDIO, 100 * 1000 * 1000);
+		return ath79_set_clk(ATH79_CLK_MDIO, 100 * 1000 * 1000);
 
-	iounmap(dpll_base);
+	return 0;
 }
 
-static void __init qca953x_clocks_init(void __iomem *pll_base)
+static int __init qca953x_clocks_init(void __iomem *pll_base)
 {
 	unsigned long ref_rate;
 	unsigned long cpu_rate;
@@ -356,12 +368,10 @@ static void __init qca953x_clocks_init(void __iomem *pll_base)
 	else
 		ahb_rate = cpu_pll / (postdiv + 1);
 
-	ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
-	ath79_set_clk(ATH79_CLK_DDR, ddr_rate);
-	ath79_set_clk(ATH79_CLK_AHB, ahb_rate);
+	return ath79_set_clks(cpu_rate, ddr_rate, ahb_rate);
 }
 
-static void __init qca955x_clocks_init(void __iomem *pll_base)
+static int __init qca955x_clocks_init(void __iomem *pll_base)
 {
 	unsigned long ref_rate;
 	unsigned long cpu_rate;
@@ -421,12 +431,10 @@ static void __init qca955x_clocks_init(void __iomem *pll_base)
 	else
 		ahb_rate = cpu_pll / (postdiv + 1);
 
-	ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
-	ath79_set_clk(ATH79_CLK_DDR, ddr_rate);
-	ath79_set_clk(ATH79_CLK_AHB, ahb_rate);
+	return ath79_set_clks(cpu_rate, ddr_rate, ahb_rate);
 }
 
-static void __init qca956x_clocks_init(void __iomem *pll_base)
+static int __init qca956x_clocks_init(void __iomem *pll_base)
 {
 	unsigned long ref_rate;
 	unsigned long cpu_rate;
@@ -493,19 +501,18 @@ static void __init qca956x_clocks_init(void __iomem *pll_base)
 	else
 		ahb_rate = cpu_pll / (postdiv + 1);
 
-	ath79_set_clk(ATH79_CLK_CPU, cpu_rate);
-	ath79_set_clk(ATH79_CLK_DDR, ddr_rate);
-	ath79_set_clk(ATH79_CLK_AHB, ahb_rate);
+	return ath79_set_clks(cpu_rate, ddr_rate, ahb_rate);
 }
 
 static void __init ath79_clocks_init_dt(struct device_node *np)
 {
 	struct clk *ref_clk;
 	void __iomem *pll_base;
+	int ret = -ENODEV;
 
 	clk_data = kzalloc_flex(*clk_data, hws, ATH79_CLK_END);
 	if (!clk_data)
-		panic("ath79: can't allocate the clock data");
+		return;
 	clk_data->num = ATH79_CLK_END;
 
 	/*
@@ -515,8 +522,11 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	ref_clk = of_clk_get(np, 0);
 	if (IS_ERR(ref_clk))
 		ref_clk = clk_get_sys(NULL, "ref");
-	if (!IS_ERR(ref_clk))
-		clk_data->hws[ATH79_CLK_REF] = __clk_get_hw(ref_clk);
+	if (IS_ERR(ref_clk)) {
+		pr_err("%pOF: no reference clock\n", np);
+		goto err_free;
+	}
+	clk_data->hws[ATH79_CLK_REF] = __clk_get_hw(ref_clk);
 
 	pll_base = of_iomap(np, 0);
 	if (!pll_base) {
@@ -525,36 +535,47 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	}
 
 	if (of_device_is_compatible(np, "qca,ar7100-pll"))
-		ar71xx_clocks_init(pll_base);
+		ret = ar71xx_clocks_init(pll_base);
 	else if (of_device_is_compatible(np, "qca,ar7240-pll") ||
 		 of_device_is_compatible(np, "qca,ar9130-pll"))
-		ar724x_clocks_init(pll_base);
+		ret = ar724x_clocks_init(pll_base);
 	else if (of_device_is_compatible(np, "qca,ar9330-pll"))
-		ar933x_clocks_init(pll_base);
+		ret = ar933x_clocks_init(pll_base);
 	else if (of_device_is_compatible(np, "qca,ar9340-pll"))
-		ar934x_clocks_init(pll_base);
+		ret = ar934x_clocks_init(pll_base);
 	else if (of_device_is_compatible(np, "qca,qca9530-pll"))
-		qca953x_clocks_init(pll_base);
+		ret = qca953x_clocks_init(pll_base);
 	else if (of_device_is_compatible(np, "qca,qca9550-pll"))
-		qca955x_clocks_init(pll_base);
+		ret = qca955x_clocks_init(pll_base);
 	else if (of_device_is_compatible(np, "qca,qca9560-pll"))
-		qca956x_clocks_init(pll_base);
+		ret = qca956x_clocks_init(pll_base);
+
+	/* the rates are fixed, so the registers aren't needed any more */
+	iounmap(pll_base);
+
+	if (ret) {
+		pr_err("%pOF: failed to register the clocks: %pe\n", np,
+		       ERR_PTR(ret));
+		goto err_clk;
+	}
 
 	if (!clk_data->hws[ATH79_CLK_MDIO])
 		clk_data->hws[ATH79_CLK_MDIO] = clk_data->hws[ATH79_CLK_REF];
 
-	if (of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data)) {
-		pr_err("%pOF: could not register clk provider\n", np);
-		goto err_iounmap;
+	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
+	if (ret) {
+		pr_err("%pOF: could not register clk provider: %pe\n", np,
+		       ERR_PTR(ret));
+		goto err_clk;
 	}
 
 	return;
 
-err_iounmap:
-	iounmap(pll_base);
-
 err_clk:
 	clk_put(ref_clk);
+err_free:
+	kfree(clk_data);
+	clk_data = NULL;
 }
 
 CLK_OF_DECLARE(ar7100_clk, "qca,ar7100-pll", ath79_clocks_init_dt);
Signature · 1 line
2.56.0
#8 lore raw

PATCH 7/8 clk: ath79: give each compatible its own init function

In reply to Rosen Penev

Every compatible already has its own CLK_OF_DECLARE(), yet they all shared one init function, which then matched the compatible again to pick the SoC code. Have each CLK_OF_DECLARE() call a small wrapper that passes the SoC init function to the common code instead.

---
 drivers/clk/clk-ath79.c | 72 +++++++++++++++++++++++++++--------------
 1 file changed, 47 insertions(+), 25 deletions(-)
drivers/clk/clk-ath79.c +47 -25
index 768c9e410b59..8086b4f6378c 100644
@@ -504,11 +504,12 @@ static int __init qca956x_clocks_init(void __iomem *pll_base)
 	return ath79_set_clks(cpu_rate, ddr_rate, ahb_rate);
 }
 
-static void __init ath79_clocks_init_dt(struct device_node *np)
+static void __init ath79_clocks_init_dt(struct device_node *np,
+					int (*soc_init)(void __iomem *pll_base))
 {
 	struct clk *ref_clk;
 	void __iomem *pll_base;
-	int ret = -ENODEV;
+	int ret;
 
 	clk_data = kzalloc_flex(*clk_data, hws, ATH79_CLK_END);
 	if (!clk_data)
@@ -534,21 +535,7 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 		goto err_clk;
 	}
 
-	if (of_device_is_compatible(np, "qca,ar7100-pll"))
-		ret = ar71xx_clocks_init(pll_base);
-	else if (of_device_is_compatible(np, "qca,ar7240-pll") ||
-		 of_device_is_compatible(np, "qca,ar9130-pll"))
-		ret = ar724x_clocks_init(pll_base);
-	else if (of_device_is_compatible(np, "qca,ar9330-pll"))
-		ret = ar933x_clocks_init(pll_base);
-	else if (of_device_is_compatible(np, "qca,ar9340-pll"))
-		ret = ar934x_clocks_init(pll_base);
-	else if (of_device_is_compatible(np, "qca,qca9530-pll"))
-		ret = qca953x_clocks_init(pll_base);
-	else if (of_device_is_compatible(np, "qca,qca9550-pll"))
-		ret = qca955x_clocks_init(pll_base);
-	else if (of_device_is_compatible(np, "qca,qca9560-pll"))
-		ret = qca956x_clocks_init(pll_base);
+	ret = soc_init(pll_base);
 
 	/* the rates are fixed, so the registers aren't needed any more */
 	iounmap(pll_base);
@@ -578,11 +565,46 @@ static void __init ath79_clocks_init_dt(struct device_node *np)
 	clk_data = NULL;
 }
 
-CLK_OF_DECLARE(ar7100_clk, "qca,ar7100-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar7240_clk, "qca,ar7240-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar9130_clk, "qca,ar9130-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar9330_clk, "qca,ar9330-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar9340_clk, "qca,ar9340-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar9530_clk, "qca,qca9530-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar9550_clk, "qca,qca9550-pll", ath79_clocks_init_dt);
-CLK_OF_DECLARE(ar9560_clk, "qca,qca9560-pll", ath79_clocks_init_dt);
+static void __init ar71xx_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, ar71xx_clocks_init);
+}
+
+static void __init ar724x_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, ar724x_clocks_init);
+}
+
+static void __init ar933x_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, ar933x_clocks_init);
+}
+
+static void __init ar934x_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, ar934x_clocks_init);
+}
+
+static void __init qca953x_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, qca953x_clocks_init);
+}
+
+static void __init qca955x_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, qca955x_clocks_init);
+}
+
+static void __init qca956x_clocks_init_dt(struct device_node *np)
+{
+	ath79_clocks_init_dt(np, qca956x_clocks_init);
+}
+
+CLK_OF_DECLARE(ar7100_clk, "qca,ar7100-pll", ar71xx_clocks_init_dt);
+CLK_OF_DECLARE(ar7240_clk, "qca,ar7240-pll", ar724x_clocks_init_dt);
+CLK_OF_DECLARE(ar9130_clk, "qca,ar9130-pll", ar724x_clocks_init_dt);
+CLK_OF_DECLARE(ar9330_clk, "qca,ar9330-pll", ar933x_clocks_init_dt);
+CLK_OF_DECLARE(ar9340_clk, "qca,ar9340-pll", ar934x_clocks_init_dt);
+CLK_OF_DECLARE(ar9530_clk, "qca,qca9530-pll", qca953x_clocks_init_dt);
+CLK_OF_DECLARE(ar9550_clk, "qca,qca9550-pll", qca955x_clocks_init_dt);
+CLK_OF_DECLARE(ar9560_clk, "qca,qca9560-pll", qca956x_clocks_init_dt);
Signature · 1 line
2.56.0
#9 lore raw

PATCH 8/8 clk: ath79: share the fractional PLL calculation

In reply to Rosen Penev

AR934x, QCA953x and QCA955x compute their CPU and DDR PLL rates with the same formula, ref / ref_div * (nint + nfrac / frac) / 2^out_div, written three different ways. Use one helper for all of them, with 64-bit intermediates.

That fixes the QCA955x DDR PLL rate, whose nfrac * ref product could overflow 32 bits: with a 40 MHz reference clock it did for any nfrac above 107, and with 25 MHz for any above 171. The other rates are unchanged, as checked against the old code with random register values.

---
 drivers/clk/clk-ath79.c | 47 +++++++++++++----------------------------
 1 file changed, 15 insertions(+), 32 deletions(-)
drivers/clk/clk-ath79.c +15 -32
index 8086b4f6378c..f9a8c9b9aa24 100644
@@ -10,6 +10,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/init.h>
 #include <linux/io.h>
 #include <linux/err.h>
@@ -21,7 +22,6 @@
 #include <linux/slab.h>
 #include <dt-bindings/clock/ath79-clk.h>
 
-#include <asm/div64.h>
 
 #include "clk-ath79.h"
 
@@ -189,24 +189,17 @@ static int __init ar933x_clocks_init(void __iomem *pll_base)
 				 ref_div * out_div * ahb_div);
 }
 
-static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac,
-				      u32 frac, u32 out_div)
+/* a fractional PLL: ref / ref_div * (nint + nfrac / frac) / 2^out_div */
+static unsigned long __init ath79_pll_rate(unsigned long ref, u32 ref_div,
+					   u32 nint, u32 nfrac, u32 frac,
+					   u32 out_div)
 {
-	u64 t;
-	u32 ret;
+	unsigned long rate;
 
-	t = ref;
-	t *= nint;
-	do_div(t, ref_div);
-	ret = t;
+	rate = div_u64((u64)ref * nint, ref_div);
+	rate += div_u64((u64)ref * nfrac, ref_div * frac);
 
-	t = ref;
-	t *= nfrac;
-	do_div(t, ref_div * frac);
-	ret += t;
-
-	ret /= (1 << out_div);
-	return ret;
+	return rate >> out_div;
 }
 
 static int __init ar934x_clocks_init(void __iomem *pll_base)
@@ -243,8 +236,7 @@ static int __init ar934x_clocks_init(void __iomem *pll_base)
 		frac = 1 << 6;
 	}
 
-	cpu_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
-				      nfrac, frac, out_div);
+	cpu_pll = ath79_pll_rate(ref_rate, ref_div, nint, nfrac, frac, out_div);
 
 	pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL2_REG);
 	if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) {
@@ -263,8 +255,7 @@ static int __init ar934x_clocks_init(void __iomem *pll_base)
 		frac = 1 << 10;
 	}
 
-	ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
-				      nfrac, frac, out_div);
+	ddr_pll = ath79_pll_rate(ref_rate, ref_div, nint, nfrac, frac, out_div);
 
 	iounmap(dpll_base);
 
@@ -325,9 +316,7 @@ static int __init qca953x_clocks_init(void __iomem *pll_base)
 	nint = FIELD_GET(QCA953X_PLL_CPU_CONFIG_NINT, pll);
 	frac = FIELD_GET(QCA953X_PLL_CPU_CONFIG_NFRAC, pll);
 
-	cpu_pll = nint * ref_rate / ref_div;
-	cpu_pll += frac * (ref_rate >> 6) / ref_div;
-	cpu_pll /= (1 << out_div);
+	cpu_pll = ath79_pll_rate(ref_rate, ref_div, nint, frac, 1 << 6, out_div);
 
 	pll = __raw_readl(pll_base + QCA953X_PLL_DDR_CONFIG_REG);
 	out_div = FIELD_GET(QCA953X_PLL_DDR_CONFIG_OUTDIV, pll);
@@ -335,9 +324,7 @@ static int __init qca953x_clocks_init(void __iomem *pll_base)
 	nint = FIELD_GET(QCA953X_PLL_DDR_CONFIG_NINT, pll);
 	frac = FIELD_GET(QCA953X_PLL_DDR_CONFIG_NFRAC, pll);
 
-	ddr_pll = nint * ref_rate / ref_div;
-	ddr_pll += frac * (ref_rate >> 6) / (ref_div << 4);
-	ddr_pll /= (1 << out_div);
+	ddr_pll = ath79_pll_rate(ref_rate, ref_div, nint, frac, 1 << 10, out_div);
 
 	clk_ctrl = __raw_readl(pll_base + QCA953X_PLL_CLK_CTRL_REG);
 
@@ -388,9 +375,7 @@ static int __init qca955x_clocks_init(void __iomem *pll_base)
 	nint = FIELD_GET(QCA955X_PLL_CPU_CONFIG_NINT, pll);
 	frac = FIELD_GET(QCA955X_PLL_CPU_CONFIG_NFRAC, pll);
 
-	cpu_pll = nint * ref_rate / ref_div;
-	cpu_pll += frac * ref_rate / (ref_div * (1 << 6));
-	cpu_pll /= (1 << out_div);
+	cpu_pll = ath79_pll_rate(ref_rate, ref_div, nint, frac, 1 << 6, out_div);
 
 	pll = __raw_readl(pll_base + QCA955X_PLL_DDR_CONFIG_REG);
 	out_div = FIELD_GET(QCA955X_PLL_DDR_CONFIG_OUTDIV, pll);
@@ -398,9 +383,7 @@ static int __init qca955x_clocks_init(void __iomem *pll_base)
 	nint = FIELD_GET(QCA955X_PLL_DDR_CONFIG_NINT, pll);
 	frac = FIELD_GET(QCA955X_PLL_DDR_CONFIG_NFRAC, pll);
 
-	ddr_pll = nint * ref_rate / ref_div;
-	ddr_pll += frac * ref_rate / (ref_div * (1 << 10));
-	ddr_pll /= (1 << out_div);
+	ddr_pll = ath79_pll_rate(ref_rate, ref_div, nint, frac, 1 << 10, out_div);
 
 	clk_ctrl = __raw_readl(pll_base + QCA955X_PLL_CLK_CTRL_REG);
 
Signature · 1 line
2.56.0