Add SpacemiT K1/K3 SoC timer support
Subsystems (4)
Troy Mitchell
reviewerv2 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 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]
---
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]>
Troy Mitchell
reviewerPATCH v2 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
@@ -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
Troy Mitchell
reviewerPATCH v2 2/5 clocksource/drivers/spacemit: Add K1 timer support
In reply to Troy MitchellSpacemiT 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
@@ -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 && RESET_CONTROLLER && 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
@@ -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
Re: [PATCH v2 2/5] clocksource/drivers/spacemit: Add K1 timer support
Quoted 28 lines · 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.
Signed-off-by Troy Mitchell <[email protected]>--- drivers/clocksource/Kconfig | 11 ++ drivers/clocksource/Makefile | 1 + drivers/clocksource/timer-spacemit.c | 317 +++++++++++++++++++++++++++++++++++ 3 files changed, 329 insertions(+)drivers/clocksource/Kconfig +0
-0
drivers/clocksource/Kconfig config RZN1_TIMER · line 169
Quoted 7 lines
drivers/clocksource/Kconfig +4
-0@@ -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 && RESET_CONTROLLER && HAS_IOMEM
Drop the unnecessary RESET_CONTROLLER dependency, the reset controller API has stubs for consumers.
regards
Philipp
Troy Mitchell
reviewerRe: [PATCH v2 2/5] clocksource/drivers/spacemit: Add K1 timer support
Quoted 4 lines · Philipp Zabel
drivers/clocksource/Kconfig config RZN1_TIMER · line 169
Quoted 1 line
drivers/clocksource/Kconfig +1
-0+ depends on OF && COMMON_CLK && RESET_CONTROLLER && HAS_IOMEMDrop the unnecessary
RESET_CONTROLLERdependency, the reset controller API has stubs for consumers.
Will drop it in v3, thanks.
Signature · 1 line
Troy Mitchell
Troy Mitchell
reviewerPATCH v2 3/5 riscv: dts: spacemit: Add the K1 APB timer
In reply to Troy MitchellDescribe 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
@@ -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 · 7 lines
2.56.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Troy Mitchell
reviewerPATCH v2 4/5 riscv: dts: spacemit: Add the K3 APB timer
In reply to Troy MitchellDescribe 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
@@ -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
Troy Mitchell
reviewerPATCH v2 5/5 MAINTAINERS: Add entry for SpacemiT timer
In reply to Troy MitchellAdd a maintainer entry for the SpacemiT K1/K3 timer binding and clocksource driver.
--- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
@@ -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 · 7 lines
2.56.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv