Skip to contentSkip to conversation

Add SpacemiT K1/K3 SoC timer support

PATCH New Troy Mitchell 2 participants 7 messages Nov 25

Subsystems (4)
lore
Share & tools
lore

Troy Mitchell

author reviewer
#0 lore raw

v3 cover Add SpacemiT K1/K3 SoC timer support

The SpacemiT K1 and K3 SoCs have APB timers that run independently of the CPU cores. Each timer block has three 32-bit up counters, and each counter has three match comparators and its own interrupt. A counter can be clocked from the functional clock or from the 32.768 kHz clock, and can run in free-running or periodic mode.

This series uses counter 0 as a oneshot clockevent. It is registered for tick broadcast, so it can wake up CPUs whose local RISC-V timer stops in idle.

K3 uses the same register interface as K1 and falls back to the K1 compatible. Both SoCs run the timer from a 1 MHz functional clock.

---
Changes in v3:
- Drop the unnecessary RESET_CONTROLLER dependency (Philipp)
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Use named constants with comments for the counter sync latency and
the minimum clock rate (Iker)
- Link to v1: https://patch.msgid.link/[email protected]

To: Troy Mitchell <[email protected]>
To: Daniel Lezcano <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Rob Herring <[email protected]>
To: Krzysztof Kozlowski <[email protected]>
To: Conor Dooley <[email protected]>
To: Yixun Lan <[email protected]>
To: Philipp Zabel <[email protected]>
To: Paul Walmsley <[email protected]>
To: Palmer Dabbelt <[email protected]>
To: Albert Ou <[email protected]>
To: Alexandre Ghiti <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Troy Mitchell (5):
dt-bindings: timer: Add SpacemiT K1 timer clocksource/drivers/spacemit: Add K1 timer support
riscv: dts: spacemit: Add the K1 APB timer
riscv: dts: spacemit: Add the K3 APB timer
MAINTAINERS: Add entry for SpacemiT timer

 .../bindings/timer/spacemit,k1-timer.yaml          |  71 +++++
 MAINTAINERS                                        |   6 +
 arch/riscv/boot/dts/spacemit/k1.dtsi               |  12 +
 arch/riscv/boot/dts/spacemit/k3.dtsi               |  12 +
 drivers/clocksource/Kconfig                        |  11 +
 drivers/clocksource/Makefile                       |   1 +
 drivers/clocksource/timer-spacemit.c               | 317 +++++++++++++++++++++
 7 files changed, 430 insertions(+)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261019-spacemit-soc-timer-cadf3088d286

Best regards,

Signature · 1 line
Troy Mitchell <[email protected]>
author reviewer
#1 lore raw

PATCH v3 1/5 dt-bindings: timer: Add SpacemiT K1 timer

The SpacemiT timer block provides three 32-bit counters, each with three match comparators and an interrupt output. Describe its register range, interrupts, functional and bus clocks, and reset.

K3 uses the same register interface, so describe its timer with a SoC-specific compatible followed by the K1 fallback.

---
 .../bindings/timer/spacemit,k1-timer.yaml          | 71 ++++++++++++++++++++++
 1 file changed, 71 insertions(+)
Documentation/devicetree/bindings/timer/spacemit,k1-timer.yaml new file +71 -0
new file mode 100644
index 000000000000..b71785b8aa94
@@ -0,0 +1,71 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/timer/spacemit,k1-timer.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: SpacemiT K1 timer
+
+maintainers:
+  - Troy Mitchell <[email protected]>
+
+description:
+  The timer block contains three 32-bit up counters, each with three match
+  comparators and an interrupt output. Each counter can use the functional
+  clock or the internal 32.768 kHz clock and supports free-running and
+  periodic operation.
+
+properties:
+  compatible:
+    oneOf:
+      - const: spacemit,k1-timer
+      - items:
+          - const: spacemit,k3-timer
+          - const: spacemit,k1-timer
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    minItems: 1
+    items:
+      - description: Counter 0 interrupt
+      - description: Counter 1 interrupt
+      - description: Counter 2 interrupt
+
+  clocks:
+    items:
+      - description: Functional clock
+      - description: APB bus clock
+
+  clock-names:
+    items:
+      - const: func
+      - const: bus
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+  - resets
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>
+
+    timer@d4014000 {
+        compatible = "spacemit,k1-timer";
+        reg = <0xd4014000 0x100>;
+        interrupts = <23>;
+        clocks = <&syscon_apbc CLK_TIMERS1>,
+                 <&syscon_apbc CLK_TIMERS1_BUS>;
+        clock-names = "func", "bus";
+        resets = <&syscon_apbc RESET_TIMERS1>;
+    };
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v3 1/5] dt-bindings: timer: Add SpacemiT K1 timer

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dt-bindings: timer: interrupts listed before clocks in spacemit,k1-timer.yaml --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dt-bindings: timer: interrupts listed before clocks in spacemit,k1-timer.yaml
--

commit 16e785f5241b621458eff707f48ab8e02a62ce8d
Author: Troy Mitchell <[email protected]>

dt-bindings: timer: Add SpacemiT K1 timer

This commit adds a Device Tree binding schema for the SpacemiT K1 and K3 timer block.

Quoted 6 lines
Documentation/devicetree/bindings/timer/spacemit,k1-timer.yaml new file +0 -0
new file mode 100644
index 0000000000000..b71785b8aa947
@@ -0,0 +1,71 @@

[ ... ]

Quoted 44 lines
Documentation/devicetree/bindings/timer/spacemit,k1-timer.yaml new file +44 -0
+properties:
+  compatible:
+    oneOf:
+      - const: spacemit,k1-timer
+      - items:
+          - const: spacemit,k3-timer
+          - const: spacemit,k1-timer
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    minItems: 1
+    items:
+      - description: Counter 0 interrupt
+      - description: Counter 1 interrupt
+      - description: Counter 2 interrupt
+
+  clocks:
+    items:
+      - description: Functional clock
+      - description: APB bus clock
+
+  clock-names:
+    items:
+      - const: func
+      - const: bus
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+  - resets
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/clock/spacemit,k1-syscon.h>

Documentation/devicetree/bindings/timer/spacemit,k1-timer.yaml line 71

Quoted 10 lines
Documentation/devicetree/bindings/timer/spacemit,k1-timer.yaml new file +10 -0
+
+    timer@d4014000 {
+        compatible = "spacemit,k1-timer";
+        reg = <0xd4014000 0x100>;
+        interrupts = <23>;
+        clocks = <&syscon_apbc CLK_TIMERS1>,
+                 <&syscon_apbc CLK_TIMERS1_BUS>;
+        clock-names = "func", "bus";
+        resets = <&syscon_apbc RESET_TIMERS1>;
+    };

[Severity: Low]
This isn't a bug, but in DT binding schemas and example nodes, clocks and clock-names come before interrupts (following the standard compatible, reg, clocks, clock-names, interrupts order) in properties, required, and examples.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author reviewer
#3 lore raw

PATCH v3 2/5 clocksource/drivers/spacemit: Add K1 timer support

In reply to Troy Mitchell

SpacemiT K1 and K3 have APB timers that can provide a shared clockevent alongside the per-CPU RISC-V timer. Use counter 0 and its first match comparator as a oneshot device available to the tick broadcast framework.

Counter enable changes cross clock domains. Wait for the synchronized counter value to stop changing before reprogramming the match register, and check interrupt acknowledgment through the status register rather than reading the write-only clear register. Bound all synchronization waits and propagate programming failures to the clockevent core.

Obtain the counter rate from the functional clock and protect it against rate changes. Keep the clocks enabled for the clockevent lifetime and restore counter configuration through the tick resume callback without sleeping clock operations. Use a platform driver so clock and reset providers can probe before the timer is registered.

---
 drivers/clocksource/Kconfig          |  11 ++
 drivers/clocksource/Makefile         |   1 +
 drivers/clocksource/timer-spacemit.c | 317 +++++++++++++++++++++++++++++++++++
 3 files changed, 329 insertions(+)
drivers/clocksource/Kconfig +11 -0
index da83483296d5..e14bb40f9a31 100644
@@ -163,6 +163,17 @@ config RZN1_TIMER
 	    - 2 32-bit timers
 	  Two timers blocks are available in RZ/N1 SoCs.
 
+config SPACEMIT_TIMER
+	bool "SpacemiT K1/K3 timer"
+	depends on ARCH_SPACEMIT || COMPILE_TEST
+	depends on OF && COMMON_CLK && HAS_IOMEM
+	default y if ARCH_SPACEMIT
+	help
+	  Enable the SoC timer clockevent device on SpacemiT K1 and K3.
+	  It provides a shared oneshot timer for tick broadcast, alongside
+	  the per-CPU RISC-V timer. The RISC-V clocksource continues to
+	  provide timekeeping.
+
 config SUN4I_TIMER
 	bool "Sun4i timer driver" if COMPILE_TEST
 	depends on HAS_IOMEM
drivers/clocksource/Makefile +1 -0
index 464ec6690ab6..dadf9bc77dee 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_BCM2835_TIMER)	+= bcm2835_timer.o
 obj-$(CONFIG_CLPS711X_TIMER)	+= clps711x-timer.o
 obj-$(CONFIG_MXS_TIMER)		+= mxs_timer.o
 obj-$(CONFIG_CLKSRC_PXA)	+= timer-pxa.o
+obj-$(CONFIG_SPACEMIT_TIMER)	+= timer-spacemit.o
 obj-$(CONFIG_SUN4I_TIMER)	+= timer-sun4i.o
 obj-$(CONFIG_SUN5I_HSTIMER)	+= timer-sun5i.o
 obj-$(CONFIG_MESON6_TIMER)	+= timer-meson6.o
drivers/clocksource/timer-spacemit.c new file +317 -0

317 lines not on this page · raw message

Signature · 1 line
2.56.0
author reviewer
#4 lore raw

PATCH v3 3/5 riscv: dts: spacemit: Add the K1 APB timer

In reply to Troy Mitchell

Describe the first APB timer block and its counter 0 interrupt so it can provide a shared clockevent. Connect its functional and bus clocks and reset, and select the oscillator-derived 1 MHz input for a stable counter rate independent of the PLLs.

---
 arch/riscv/boot/dts/spacemit/k1.dtsi | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/riscv/boot/dts/spacemit/k1.dtsi +12 -0
index 542bfc0d19f5..c3db90f6bf14 100644
@@ -583,6 +583,18 @@ i2c5: i2c@d4013800 {
 			status = "disabled";
 		};
 
+		timer0: timer@d4014000 {
+			compatible = "spacemit,k1-timer";
+			reg = <0x0 0xd4014000 0x0 0x100>;
+			interrupts = <23>;
+			clocks = <&syscon_apbc CLK_TIMERS1>,
+				 <&syscon_apbc CLK_TIMERS1_BUS>;
+			clock-names = "func", "bus";
+			assigned-clocks = <&syscon_apbc CLK_TIMERS1>;
+			assigned-clock-rates = <1000000>;
+			resets = <&syscon_apbc RESET_TIMERS1>;
+		};
+
 		syscon_apbc: system-controller@d4015000 {
 			compatible = "spacemit,k1-syscon-apbc";
 			reg = <0x0 0xd4015000 0x0 0x1000>;
Signature · 1 line
2.56.0
author reviewer
#5 lore raw

PATCH v3 4/5 riscv: dts: spacemit: Add the K3 APB timer

In reply to Troy Mitchell

Describe the APB timer at 0xd4016000 and its counter 0 interrupt so it can provide a shared clockevent. Select the oscillator-derived 1 MHz input and connect the functional clock, bus clock and reset.

The register interface is compatible with K1, so use the K1 fallback following the K3 compatible.

---
 arch/riscv/boot/dts/spacemit/k3.dtsi | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/riscv/boot/dts/spacemit/k3.dtsi +12 -0
index e89d2cc5ca8c..cee313232c11 100644
@@ -777,6 +777,18 @@ syscon_apbc: system-controller@d4015000 {
 			#reset-cells = <1>;
 		};
 
+		timer1: timer@d4016000 {
+			compatible = "spacemit,k3-timer", "spacemit,k1-timer";
+			reg = <0x0 0xd4016000 0x0 0x100>;
+			interrupts = <26 IRQ_TYPE_LEVEL_HIGH>;
+			clocks = <&syscon_apbc CLK_APBC_TIMERS1>,
+				 <&syscon_apbc CLK_APBC_TIMERS1_BUS>;
+			clock-names = "func", "bus";
+			assigned-clocks = <&syscon_apbc CLK_APBC_TIMERS1>;
+			assigned-clock-rates = <1000000>;
+			resets = <&syscon_apbc RESET_APBC_TIMERS1>;
+		};
+
 		uart0: serial@d4017000 {
 			compatible = "spacemit,k3-uart", "intel,xscale-uart";
 			reg = <0x0 0xd4017000 0x0 0x100>;
Signature · 1 line
2.56.0
author reviewer
#6 lore raw

PATCH v3 5/5 MAINTAINERS: Add entry for SpacemiT timer

In reply to Troy Mitchell

Add a maintainer entry for the SpacemiT K1/K3 timer binding and clocksource driver.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 93abbb315065..e607e9cb07a0 100644
@@ -26372,6 +26372,12 @@ S:	Maintained
 F:	drivers/reset/spacemit/
 F:	include/dt-bindings/reset/spacemit,k3-resets.h
 
+SPACEMIT TIMER DRIVER
+M:	Troy Mitchell <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/timer/spacemit,k1-timer.yaml
+F:	drivers/clocksource/timer-spacemit.c
+
 SPANISH DOCUMENTATION
 M:	Carlos Bilbao <[email protected]>
 R:	Avadhut Naik <[email protected]>
Signature · 1 line
2.56.0