Skip to contentSkip to conversation

Nuvoton WPCM450 clock and reset driver

PATCH Superseded Jonathan Neuschäfer 1 participant 7 messages May 2, 2024

Subsystems (3)
lore
Share & tools
lore

Jonathan Neuschäfer

author maintainer
#0 lore raw

v12 cover Nuvoton WPCM450 clock and reset driver

This series adds support for the clock and reset controller in the Nuvoton WPCM450 SoC. This means that the clock rates for peripherals will be calculated automatically based on the clock tree as it was preconfigured by the bootloader. The 24 MHz dummy clock, that is currently in the devicetree, is no longer needed. Somewhat unfortunately, this also means that there is a breaking change once the devicetree starts relying on the clock driver, but I find it acceptable in this case, because WPCM450 is still at a somewhat early stage.

v12:
- Convert to platform driver, but use fixed-factor-clock for timer (a necessary workaround because npcm7xx-timer needs its clock earlier than a platform driver can provide it)
- Various driver improvements suggested or inspired by Stephen Boyd
- New patches:
- clk: Introduce devm_clk_hw_register_divider_table_parent_data()
- clk: provider: Address documentation pitfall in struct clk_parent_data

v11:
- Link: https://lore.kernel.org/r/[email protected]
- Improved description in "ARM: dts: wpcm450: Remove clock-output-names from reference clock node"
- some minor format differences due to switching to B4

v10:
- A small tweak (using selected instead of extending an already-long default line) in Kconfig, for better robustness

v9:
- Various improvements to the driver
- No longer use global clock names (and the clock-output-names property) to refer to the reference clock, but instead rely on a phandle reference

v8:
- https://lore.kernel.org/lkml/[email protected]/
- Use %pe throughout the driver

v7:
- Simplified the error handling, by largely removing resource deallocation, which:
- was already incomplete
- would only happen in a case when the system is in pretty bad state because the clock driver didn't initialize correctly (in other words, the clock driver isn't optional enough that complex error handling really pays off)

v6:
- Dropped all patches except the clock binding and the clock driver, because they have mostly been merged
- Minor correction to how RESET_SIMPLE is selected

v5:
- Dropped patch 2 (watchdog: npcm: Enable clock if provided), which was since merged upstream
- Added patch 2 (clocksource: timer-npcm7xx: Enable timer 1 clock before use) again, because I wasn't able to find it in linux-next
- Switched the driver to using struct clk_parent_data
- Rebased on 6.1-rc3

v4:
- Leave WDT clock running during after restart handler
- Fix reset controller initialization
- Dropped patch 2/7 (clocksource: timer-npcm7xx: Enable timer 1 clock before use), as it was applied by Daniel Lezcano

v3:
- https://lore.kernel.org/lkml/[email protected]/
- Changed "refclk" string to "ref"
- Fixed some dead code in the driver
- Added clk_prepare_enable call to the watchdog restart handler
- Added a few review tags

v2:
- https://lore.kernel.org/lkml/[email protected]/
- various small improvements

v1:
- https://lore.kernel.org/lkml/[email protected]/

---
---
Jonathan Neuschäfer (6):
dt-bindings: clock: Add Nuvoton WPCM450 clock/reset controller
clk: Introduce devm_clk_hw_register_divider_table_parent_data()
clk: provider: Address documentation pitfall in struct clk_parent_data
clk: wpcm450: Add Nuvoton WPCM450 clock/reset controller driver
ARM: dts: wpcm450: Remove clock-output-names from reference clock node
ARM: dts: wpcm450: Switch clocks to clock controller

 .../bindings/clock/nuvoton,wpcm450-clk.yaml        |  65 +++
 arch/arm/boot/dts/nuvoton/nuvoton-wpcm450.dtsi     |  33 +-
 drivers/clk/Makefile                               |   2 +-
 drivers/clk/nuvoton/Kconfig                        |  10 +-
 drivers/clk/nuvoton/Makefile                       |   1 +
 drivers/clk/nuvoton/clk-wpcm450.c                  | 455 +++++++++++++++++++++
 include/dt-bindings/clock/nuvoton,wpcm450-clk.h    |  67 +++
 include/linux/clk-provider.h                       |  26 +-
 8 files changed, 643 insertions(+), 16 deletions(-)

---
base-commit: 4cece764965020c22cff7665b18a012006359095
change-id: 20240330-wpcm-clk-222a37f59cfb

Best regards,
--
Jonathan Neuschäfer <[email protected]>

author maintainer
#1 lore raw

PATCH v12 1/6 dt-bindings: clock: Add Nuvoton WPCM450 clock/reset controller

The Nuvoton WPCM450 SoC has a combined clock and reset controller. Add a devicetree binding for it, as well as definitions for the bit numbers used by it.

---
v10-v11:
- no changes

v9:
- Remove clock-output-names in example, because it's now unnecessary due to driver improvements

v5-v8:
- no changes

v4:
- https://lore.kernel.org/lkml/[email protected]/
- Add R-b tag

v3:
- Change clock-output-names and clock-names from "refclk" to "ref", suggested by Krzysztof Kozlowski

v2:
- https://lore.kernel.org/lkml/[email protected]/
- Various improvements, suggested by Krzysztof Kozlowski

v1:
- https://lore.kernel.org/lkml/[email protected]/

---
 .../bindings/clock/nuvoton,wpcm450-clk.yaml        | 65 +++++++++++++++++++++
 include/dt-bindings/clock/nuvoton,wpcm450-clk.h    | 67 ++++++++++++++++++++++
 2 files changed, 132 insertions(+)
Documentation/devicetree/bindings/clock/nuvoton,wpcm450-clk.yaml new file +65 -0
new file mode 100644
index 00000000000000..93521cf68a040f
@@ -0,0 +1,65 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/clock/nuvoton,wpcm450-clk.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton WPCM450 clock controller
+
+maintainers:
+  - Jonathan Neuschäfer <[email protected]>
+
+description:
+  The clock controller of the Nuvoton WPCM450 SoC supplies clocks and resets to
+  the rest of the chip.
+
+properties:
+  compatible:
+    const: nuvoton,wpcm450-clk
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: Reference clock oscillator (should be 48 MHz)
+
+  clock-names:
+    items:
+      - const: ref
+
+  '#clock-cells':
+    const: 1
+
+  '#reset-cells':
+    const: 1
+
+additionalProperties: false
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - '#clock-cells'
+
+examples:
+  - |
+    #include <dt-bindings/clock/nuvoton,wpcm450-clk.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    refclk: clock-48mhz {
+      /* 48 MHz reference oscillator */
+      compatible = "fixed-clock";
+      clock-frequency = <48000000>;
+      #clock-cells = <0>;
+    };
+
+    clk: clock-controller@b0000200 {
+      reg = <0xb0000200 0x100>;
+      compatible = "nuvoton,wpcm450-clk";
+      clocks = <&refclk>;
+      clock-names = "ref";
+      #clock-cells = <1>;
+      #reset-cells = <1>;
+    };
include/dt-bindings/clock/nuvoton,wpcm450-clk.h new file +67 -0

67 lines not on this page · raw message

--
2.43.0

author maintainer
#2 lore raw

PATCH v12 2/6 clk: Introduce devm_clk_hw_register_divider_table_parent_data()

In reply to Jonathan Neuschäfer

Introduce the devm pendant to clk_hw_register_divider_table_parent_data.

---
 include/linux/clk-provider.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
include/linux/clk-provider.h +22 -0
index 4a537260f6557d..5d537d0776a11f 100644
@@ -932,6 +932,28 @@ struct clk *clk_register_divider_table(struct device *dev, const char *name,
 				       (width), (clk_divider_flags), (table), \
 				       (lock))
 
+/**
+ * devm_clk_hw_register_divider_table_parent_data - register a table based divider clock
+ * with the clock framework (devres variant)
+ * @dev: device registering this clock
+ * @name: name of this clock
+ * @parent_data: parent clk data
+ * @flags: framework-specific flags
+ * @reg: register address to adjust divider
+ * @shift: number of bits to shift the bitfield
+ * @width: width of the bitfield
+ * @clk_divider_flags: divider-specific flags for this clock
+ * @table: array of divider/value pairs ending with a div set to 0
+ * @lock: shared register lock for this clock
+ */
+#define devm_clk_hw_register_divider_table_parent_data(dev, name, parent_data, flags,   \
+						       reg, shift, width,               \
+						       clk_divider_flags, table, lock)  \
+	__devm_clk_hw_register_divider((dev), NULL, (name), NULL,                       \
+				       NULL, (parent_data), (flags), (reg), (shift),    \
+				       (width), (clk_divider_flags), (table),           \
+				       (lock))
+
 void clk_unregister_divider(struct clk *clk);
 void clk_hw_unregister_divider(struct clk_hw *hw);
 

--
2.43.0

author maintainer
#3 lore raw

PATCH v12 3/6 clk: provider: Address documentation pitfall in struct clk_parent_data

In reply to Jonathan Neuschäfer

For some reason, I understood the description of .index to mean an index into the list of clocks defined by the provider itself. This is not the case, and it caused me much confusion.

Let's be a bit more explicit for those who read the documentation after me.

---
 include/linux/clk-provider.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/clk-provider.h +3 -1
index 5d537d0776a11f..88bdada390c772 100644
@@ -273,7 +273,9 @@ struct clk_ops {
  * @hw: parent clk_hw pointer (used for clk providers with internal clks)
  * @fw_name: parent name local to provider registering clk
  * @name: globally unique parent name (used as a fallback)
- * @index: parent index local to provider registering clk (if @fw_name absent)
+ * @index: parent index local to provider registering clk (if @fw_name absent).
+ *         Note that this is not an index into the provider's own clocks but
+ *         into its list of parents!
  */
 struct clk_parent_data {
 	const struct clk_hw	*hw;

--
2.43.0

author maintainer
#4 lore raw

PATCH v12 4/6 clk: wpcm450: Add Nuvoton WPCM450 clock/reset controller driver

In reply to Jonathan Neuschäfer

This driver implements the following features w.r.t. the clock and reset controller in the WPCM450 SoC:

- It calculates the rates for all clocks managed by the clock controller
- It leaves the clock tree mostly unchanged, except that it enables/ disables clock gates based on usage.
- It exposes the reset lines managed by the controller using the Generic Reset Controller subsystem
- A few clocks are marked as critical because they don't have consumers in the common clock framework:
- CPU clock
- timer clocks, because the timer-npcm7xx driver (in its current shape) can't depend on a platform driver (clk-wpcm450), but the timer clocks must not be disabled

NOTE: If the driver and the corresponding devicetree node are present,
the driver will disable "unused" clocks. This is problem until the clock relations are properly declared in the devicetree (in a later patch). Until then, the clk_ignore_unused kernel parameter can be used as a workaround.

---

v12:
- Switch ref clock inputs to .fw_name = "ref"
- Use .hw to refer to internal clocks
- Remove unnecessary pr_fmt definition
- Convert to a platform driver, to avoid use of .name; use devm_ API
- Mark timer0/1 clocks as critical
- Update copyright year to 2024
- Split reset controller initialization into a separate function
- Change MODULE_LICENSE to "GPL" according to commit bf7fbeeae6db ("module: Cure the MODULE_LICENSE "GPL" vs. "GPL v2" bogosity")

v11:
- no changes

v10:
- select RESET_{CONTROLLER,SIMPLE} from CLK_WPCM450 instead of messing with the 'default' statement

v9:
- Apply comments made by Stephen Boyd
- Move to drivers/clk/nuvoton/ directory
- Update SPDX license identifier from GPL-2.0 to GPL-2.0-only
- Rename clk_np variable to np
- Use of_clk_hw_register
- Refer to clock parents by .fw_name

v8:
- https://lore.kernel.org/lkml/[email protected]/
- Use %pe format specifier throughout the driver, as suggested by Philipp Zabel
- Add Joel's R-b

v7:
- https://lore.kernel.org/lkml/[email protected]/
- Simplify error handling by not deallocating resources

v6:
- Enable RESET_SIMPLE based on ARCH_WPCM450, not ARCH_NPCM, as suggested by Tomer Maimon

v5:
- https://lore.kernel.org/lkml/[email protected]/
- Switch to using clk_parent_data

v4:
- Fix reset controller initialization

v3:
- Change reference clock name from "refclk" to "ref"
- Remove unused variable in return path of wpcm450_clk_register_pll
- Remove unused divisor tables

v2:
- no changes

---
 drivers/clk/Makefile              |   2 +-
 drivers/clk/nuvoton/Kconfig       |  10 +-
 drivers/clk/nuvoton/Makefile      |   1 +
 drivers/clk/nuvoton/clk-wpcm450.c | 455 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 466 insertions(+), 2 deletions(-)
drivers/clk/Makefile +1 -1
index 14fa8d4ecc1fbe..cdeb2ecf3a8e99 100644
@@ -107,7 +107,7 @@ endif
 obj-y					+= mstar/
 obj-y					+= mvebu/
 obj-$(CONFIG_ARCH_MXS)			+= mxs/
-obj-$(CONFIG_ARCH_MA35)			+= nuvoton/
+obj-y					+= nuvoton/
 obj-$(CONFIG_COMMON_CLK_NXP)		+= nxp/
 obj-$(CONFIG_COMMON_CLK_PISTACHIO)	+= pistachio/
 obj-$(CONFIG_COMMON_CLK_PXA)		+= pxa/
drivers/clk/nuvoton/Kconfig +9 -1
index fe4b7f62f46704..908881654b2e91 100644
@@ -3,7 +3,7 @@
 
 config COMMON_CLK_NUVOTON
 	bool "Nuvoton clock controller common support"
-	depends on ARCH_MA35 || COMPILE_TEST
+	depends on ARCH_MA35 || ARCH_NPCM || COMPILE_TEST
 	default y
 	help
 	  Say y here to enable common clock controller for Nuvoton platforms.
@@ -16,4 +16,12 @@ config CLK_MA35D1
 	help
 	  Build the clock controller driver for MA35D1 SoC.
 
+config CLK_WPCM450
+	bool "Nuvoton WPCM450 clock/reset controller support"
+	default y
+	select RESET_CONTROLLER
+	select RESET_SIMPLE
+	help
+	  Build the clock and reset controller driver for the WPCM450 SoC.
+
 endif
drivers/clk/nuvoton/Makefile +1 -0
index c3c59dd9f2aaab..b130f0d3889ca0 100644
@@ -2,3 +2,4 @@
 obj-$(CONFIG_CLK_MA35D1) += clk-ma35d1.o
 obj-$(CONFIG_CLK_MA35D1) += clk-ma35d1-divider.o
 obj-$(CONFIG_CLK_MA35D1) += clk-ma35d1-pll.o
+obj-$(CONFIG_CLK_WPCM450) += clk-wpcm450.o
drivers/clk/nuvoton/clk-wpcm450.c new file +455 -0

455 lines not on this page · raw message

--
2.43.0

author maintainer
#5 lore raw

PATCH v12 5/6 ARM: dts: wpcm450: Remove clock-output-names from reference clock node

In reply to Jonathan Neuschäfer

A previous version of the as-yet unmerged clk-wpcm450 driver[1] used the output name, but in the current iteration it doesn't rely on it anymore. Thus it can be removed from the devicetree.

Preserve the "reference" naming aspect in the node name instead.

[1]: Added in "clk: wpcm450: Add Nuvoton WPCM450 clock/reset controller driver"

Fixes 362e8be2ec04a6 ("ARM: dts: wpcm450: Add clock controller node")

---

v12:
- rename node to clock-ref, because it isn't necessarily 48MHz, just by board design recommendation

v11:
- Specify since when clock-output-names is unnecessary

v10:
- no changes

v9:
- New patch

---
 arch/arm/boot/dts/nuvoton/nuvoton-wpcm450.dtsi | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm/boot/dts/nuvoton/nuvoton-wpcm450.dtsi +1 -2
index 6e1f0f164cb4f5..ff153858801ccf 100644
@@ -37,10 +37,9 @@ clk24m: clock-24mhz {
 		#clock-cells = <0>;
 	};
 
-	refclk: clock-48mhz {
+	refclk: clock-ref {
 		/* 48 MHz reference oscillator */
 		compatible = "fixed-clock";
-		clock-output-names = "ref";
 		clock-frequency = <48000000>;
 		#clock-cells = <0>;
 	};

--
2.43.0

author maintainer
#6 lore raw

PATCH v12 6/6 ARM: dts: wpcm450: Switch clocks to clock controller

In reply to Jonathan Neuschäfer

This change is incompatible with older kernels because it requires the clock controller driver, but I think that's acceptable because WPCM450 support is generally still in an early phase.

---

It's probably best to delay merging of this patch until after the driver is merged; I'm including it here for review, and in case someone wants to set up a shared branch between the clock and devicetree parts.

v12:
- work around timer-npcm7xx driver issue by providing timer clock separately

v11:
- no changes

v10:
- Reintroducing this patch as part of the clock/reset controller series

---
 arch/arm/boot/dts/nuvoton/nuvoton-wpcm450.dtsi | 32 ++++++++++++++++----------
 1 file changed, 20 insertions(+), 12 deletions(-)
arch/arm/boot/dts/nuvoton/nuvoton-wpcm450.dtsi +20 -12
index ff153858801ccf..daf4d399ecab4c 100644
@@ -2,6 +2,7 @@
 // Copyright 2021 Jonathan Neuschäfer
 
 #include <dt-bindings/interrupt-controller/irq.h>
+#include <dt-bindings/clock/nuvoton,wpcm450-clk.h>
 
 / {
 	compatible = "nuvoton,wpcm450";
@@ -30,13 +31,6 @@ cpu@0 {
 		};
 	};
 
-	clk24m: clock-24mhz {
-		/* 24 MHz dummy clock */
-		compatible = "fixed-clock";
-		clock-frequency = <24000000>;
-		#clock-cells = <0>;
-	};
-
 	refclk: clock-ref {
 		/* 48 MHz reference oscillator */
 		compatible = "fixed-clock";
@@ -44,6 +38,19 @@ refclk: clock-ref {
 		#clock-cells = <0>;
 	};
 
+	refclk_div2: clock-refdiv2 {
+		/*
+		 * reference oscillator divided by 2, as a workaround because
+		 * the npcm7xx-timer driver needs its clock earlier than the
+		 * clk-wpcm450 driver (as a platform driver) can provide it.
+		 */
+		compatible = "fixed-factor-clock";
+		clocks = <&refclk>;
+		#clock-cells = <0>;
+		clock-mult = <1>;
+		clock-div = <2>;
+	};
+
 	soc {
 		compatible = "simple-bus";
 		#address-cells = <1>;
@@ -70,7 +77,7 @@ serial0: serial@b8000000 {
 			reg = <0xb8000000 0x20>;
 			reg-shift = <2>;
 			interrupts = <7 IRQ_TYPE_LEVEL_HIGH>;
-			clocks = <&clk24m>;
+			clocks = <&clk WPCM450_CLK_UART0>;
 			pinctrl-names = "default";
 			pinctrl-0 = <&bsp_pins>;
 			status = "disabled";
@@ -81,7 +88,7 @@ serial1: serial@b8000100 {
 			reg = <0xb8000100 0x20>;
 			reg-shift = <2>;
 			interrupts = <8 IRQ_TYPE_LEVEL_HIGH>;
-			clocks = <&clk24m>;
+			clocks = <&clk WPCM450_CLK_UART1>;
 			status = "disabled";
 		};
 
@@ -89,14 +96,15 @@ timer0: timer@b8001000 {
 			compatible = "nuvoton,wpcm450-timer";
 			interrupts = <12 IRQ_TYPE_LEVEL_HIGH>;
 			reg = <0xb8001000 0x1c>;
-			clocks = <&clk24m>;
+			clocks = <&refclk_div2>,
+				 <&refclk_div2>;
 		};
 
 		watchdog0: watchdog@b800101c {
 			compatible = "nuvoton,wpcm450-wdt";
 			interrupts = <1 IRQ_TYPE_LEVEL_HIGH>;
 			reg = <0xb800101c 0x4>;
-			clocks = <&clk24m>;
+			clocks = <&clk WPCM450_CLK_WDT>;
 		};
 
 		aic: interrupt-controller@b8002000 {
@@ -480,7 +488,7 @@ fiu: spi-controller@c8000000 {
 			#size-cells = <0>;
 			reg = <0xc8000000 0x1000>, <0xc0000000 0x4000000>;
 			reg-names = "control", "memory";
-			clocks = <&clk 0>;
+			clocks = <&clk WPCM450_CLK_FIU>;
 			nuvoton,shm = <&shm>;
 			status = "disabled";
 		};

--
2.43.0