Skip to contentSkip to conversation

Add the System Timer Module counter

PATCH Superseded Daniel Lezcano 2 participants 8 messages Dec 17, 2025 – Dec 29, 2025

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add the System Timer Module counter

The NXP S32 family provides a System Timer Module (STM), a 32-bit free-running counter clocked from a peripheral clock. The STM includes a prescaler and one or more compare channels generating optional interrupts. When used as a generic hardware counter, only the main free-running counter is required, while the compare channels are typically unused.

On S32G2 devices, the STM is exposed as a simple counter block that can operate continuously and be shared across subsystems such as the Linux kernel, firmware components running on Cortex-M7 cores, or other co-processors. The counter can be read atomically and provides a stable timestamp source to correlate events occurring in different execution contexts.

The Linux kernel controls the STM through a memory-mapped interface, configuring the prescaler, enabling or disabling the counter, and accounting for wrap-arounds. Other subsystems access the counter in read-only mode, making it a shared timestamp reference across the platform.

This driver adds support for the STM when used as a counter on S32G2 platforms. The device is described in the device tree using the following compatible:

compatible = "nxp,s32g2-stm-cnt";

The driver exposes basic counter functionality: start, stop, reset, prescaler configuration, and overflow handling.

Changelog:
* v3
- Fixed compatible typo "nxp,s32g2-stm" to "nxp,s32g2-stm-cnt"

* v2
- Added Rob's tag
** kbuild
- Reordered alphabetically the headers
- Added bitfield.h header
- Use DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr()

Daniel Lezcano (3):
counters: Reorder the Makefile
dt-bindings: counter: Add NXP System Timer Module Counter
counter: Add STM based counter

 .../bindings/counter/nxp,s32g2-stm-cnt.yaml   |  64 +++
 drivers/counter/Kconfig                       |  10 +
 drivers/counter/Makefile                      |  21 +-
 drivers/counter/nxp-stm-cnt.c                 | 386 ++++++++++++++++++
 4 files changed, 472 insertions(+), 9 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/counter/nxp,s32g2-stm-cnt.yaml
 create mode 100644 drivers/counter/nxp-stm-cnt.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 counters: Reorder the Makefile

The next changes provide a new driver. For the sake of clarity, reorder the Makefile alphabetically.

No functional changes intended.

---
 drivers/counter/Makefile | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/counter/Makefile +11 -9
index fa3c1d08f706..40e644948e7a 100644
@@ -6,14 +6,16 @@
 obj-$(CONFIG_COUNTER) += counter.o
 counter-y := counter-core.o counter-sysfs.o counter-chrdev.o
 
-obj-$(CONFIG_I8254)		+= i8254.o
-obj-$(CONFIG_104_QUAD_8)	+= 104-quad-8.o
+obj-$(CONFIG_104_QUAD_8)		+= 104-quad-8.o
+obj-$(CONFIG_FTM_QUADDEC)		+= ftm-quaddec.o
+obj-$(CONFIG_I8254)			+= i8254.o
+obj-$(CONFIG_INTEL_QEP)			+= intel-qep.o
 obj-$(CONFIG_INTERRUPT_CNT)		+= interrupt-cnt.o
-obj-$(CONFIG_RZ_MTU3_CNT)	+= rz-mtu3-cnt.o
-obj-$(CONFIG_STM32_TIMER_CNT)	+= stm32-timer-cnt.o
-obj-$(CONFIG_STM32_LPTIMER_CNT)	+= stm32-lptimer-cnt.o
-obj-$(CONFIG_TI_EQEP)		+= ti-eqep.o
-obj-$(CONFIG_FTM_QUADDEC)	+= ftm-quaddec.o
 obj-$(CONFIG_MICROCHIP_TCB_CAPTURE)	+= microchip-tcb-capture.o
-obj-$(CONFIG_INTEL_QEP)		+= intel-qep.o
-obj-$(CONFIG_TI_ECAP_CAPTURE)	+= ti-ecap-capture.o
+obj-$(CONFIG_RZ_MTU3_CNT)		+= rz-mtu3-cnt.o
+obj-$(CONFIG_STM32_TIMER_CNT)		+= stm32-timer-cnt.o
+obj-$(CONFIG_STM32_LPTIMER_CNT)		+= stm32-lptimer-cnt.o
+obj-$(CONFIG_TI_ECAP_CAPTURE)		+= ti-ecap-capture.o
+obj-$(CONFIG_TI_EQEP)			+= ti-eqep.o
+
+
Signature · 1 line
2.43.0
#2 lore raw

PATCH v3 2/3 dt-bindings: counter: Add NXP System Timer Module Counter

In reply to Daniel Lezcano

Add the System Timer Module description found on the NXP s32 platform when it is used as a counter and the compatible for the s32g2 variant.

---
 .../bindings/counter/nxp,s32g2-stm-cnt.yaml   | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/counter/nxp,s32g2-stm-cnt.yaml
Documentation/devicetree/bindings/counter/nxp,s32g2-stm-cnt.yaml new file +64 -0
new file mode 100644
index 000000000000..4d42996f5ad3
@@ -0,0 +1,64 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/counter/nxp,s32g2-stm-cnt.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NXP System Timer Module (STM)
+
+maintainers:
+  - Daniel Lezcano <[email protected]>
+
+description:
+  The System Timer Module supports commonly required system and application
+  software timing functions. STM includes a 32-bit count-up timer and four
+  32-bit compare channels with a separate interrupt source for each channel.
+  The counter is driven by the STM module clock divided by an 8-bit prescale
+  value.
+
+properties:
+  compatible:
+    oneOf:
+      - const: nxp,s32g2-stm-cnt
+      - items:
+          - const: nxp,s32g3-stm-cnt
+          - const: nxp,s32g2-stm-cnt
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    items:
+      - description: Counter clock
+      - description: Module clock
+      - description: Register clock
+
+  clock-names:
+    items:
+      - const: counter
+      - const: module
+      - const: register
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - clock-names
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+    timer@4011c000 {
+        compatible = "nxp,s32g2-stm-cnt";
+        reg = <0x4011c000 0x3000>;
+        interrupts = <GIC_SPI 24 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clks 0x3b>, <&clks 0x3c>, <&clks 0x3c>;
+        clock-names = "counter", "module", "register";
+    };
Signature · 1 line
2.43.0
#3 lore raw

PATCH v3 3/3 counter: Add STM based counter

In reply to Daniel Lezcano

The NXP S32G2 automotive platform integrates four Cortex-A53 cores and three Cortex-M7 cores, along with a large number of timers and counters. These hardware blocks can be used as clocksources or clockevents, or as timestamp counters shared across the various subsystems running alongside the Linux kernel, such as firmware components. Their actual usage depends on the overall platform software design.

In a Linux-based system, the kernel controls the counter, which is a read-only shared resource for the other subsystems. One of its primary purposes is to act as a common timestamp source for messages or traces, allowing correlation of events occurring in different operating system contexts.

These changes introduce a basic counter driver that can start, stop, and reset the counter. It also handles overflow accounting and configures the prescaler value.

---
 drivers/counter/Kconfig       |  10 +
 drivers/counter/Makefile      |   1 +
 drivers/counter/nxp-stm-cnt.c | 386 ++++++++++++++++++++++++++++++++++
 3 files changed, 397 insertions(+)
 create mode 100644 drivers/counter/nxp-stm-cnt.c
drivers/counter/Kconfig +10 -0
index d30d22dfe577..bf5b281f194c 100644
@@ -90,6 +90,16 @@ config MICROCHIP_TCB_CAPTURE
 	  To compile this driver as a module, choose M here: the
 	  module will be called microchip-tcb-capture.
 
+config NXP_STM_CNT
+	tristate "NXP System Timer Module Counter driver"
+	depends on ARCH_S32 || COMPILE_TEST
+	help
+	  Select this option to enable the NXP System Timer Module
+	  Counter driver.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called nxp_stm_cnt.
+
 config RZ_MTU3_CNT
 	tristate "Renesas RZ/G2L MTU3a counter driver"
 	depends on RZ_MTU3
drivers/counter/Makefile +1 -0
index 40e644948e7a..196b3c216875 100644
@@ -12,6 +12,7 @@ obj-$(CONFIG_I8254)			+= i8254.o
 obj-$(CONFIG_INTEL_QEP)			+= intel-qep.o
 obj-$(CONFIG_INTERRUPT_CNT)		+= interrupt-cnt.o
 obj-$(CONFIG_MICROCHIP_TCB_CAPTURE)	+= microchip-tcb-capture.o
+obj-$(CONFIG_NXP_STM_CNT)		+= nxp-stm-cnt.o
 obj-$(CONFIG_RZ_MTU3_CNT)		+= rz-mtu3-cnt.o
 obj-$(CONFIG_STM32_TIMER_CNT)		+= stm32-timer-cnt.o
 obj-$(CONFIG_STM32_LPTIMER_CNT)		+= stm32-lptimer-cnt.o
drivers/counter/nxp-stm-cnt.c new file +386 -0

386 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v3 3/3] counter: Add STM based counter

Quoted 14 lines · Daniel Lezcano

The NXP S32G2 automotive platform integrates four Cortex-A53 cores and three Cortex-M7 cores, along with a large number of timers and counters. These hardware blocks can be used as clocksources or clockevents, or as timestamp counters shared across the various subsystems running alongside the Linux kernel, such as firmware components. Their actual usage depends on the overall platform software design.

In a Linux-based system, the kernel controls the counter, which is a read-only shared resource for the other subsystems. One of its primary purposes is to act as a common timestamp source for messages or traces, allowing correlation of events occurring in different operating system contexts.

Quoted 5 lines

These changes introduce a basic counter driver that can start, stop, and reset the counter. It also handles overflow accounting and configures the prescaler value.

Hi Daniel,

It sounds like you're trying to implement a clock for timestamping. Although the Generic Counter interface is flexible enough to shoehorn a a clock into its representation, I don't believe it's the right abstraction for this particular device. Perhaps reimplementing this driver under the Linux common clock framework would be a better approach to achieve what you want.

Regardless, if you do pursue a Counter driver you'll need to follow the Generic Counter paradigm[^1] and define at least three core components:
a Signal, a Synapse, and a Count. Resetting the Count is typically implemented by defining a struct counter_ops counter_write() callback[^2], while overflows are typically implemented by pushing COUNTER_EVENT_OVERFLOW Counter events[^3] that can be watched by userspace.

William Breathitt Gray

[^1] https://docs.kernel.org/driver-api/generic-counter.html#paradigm
[^2] https://docs.kernel.org/driver-api/generic-counter.html#c.counter_ops
[^3] https://docs.kernel.org/driver-api/generic-counter.html#counter-events

#5 lore raw

Re: [PATCH v3 3/3] counter: Add STM based counter

Hi William,

Quoted 19 lines · William Breathitt Gray
Quoted 18 lines · Daniel Lezcano

The NXP S32G2 automotive platform integrates four Cortex-A53 cores and three Cortex-M7 cores, along with a large number of timers and counters. These hardware blocks can be used as clocksources or clockevents, or as timestamp counters shared across the various subsystems running alongside the Linux kernel, such as firmware components. Their actual usage depends on the overall platform software design.

In a Linux-based system, the kernel controls the counter, which is a read-only shared resource for the other subsystems. One of its primary purposes is to act as a common timestamp source for messages or traces, allowing correlation of events occurring in different operating system contexts.

These changes introduce a basic counter driver that can start, stop, and reset the counter. It also handles overflow accounting and configures the prescaler value.

Quoted 5 lines
Quoted 1 line

Hi Daniel,

It sounds like you're trying to implement a clock for timestamping.

Well no, it is a counter which is used for timestamping. It is an automotive design.

Quoted 7 lines

Although the Generic Counter interface is flexible enough to shoehorn a a clock into its representation, I don't believe it's the right abstraction for this particular device.

Perhaps reimplementing this
driver under the Linux common clock framework would be a better approach to achieve what you want.

The common clock framework ? Sorry I may have misunderstood the CCF but how a counter exported and controlled by the userspace can be managed by the CCF. Can you elaborate ?

Quoted 7 lines

Regardless, if you do pursue a Counter driver you'll need to follow the Generic Counter paradigm[^1] and define at least three core components:
a Signal, a Synapse, and a Count. Resetting the Count is typically implemented by defining a struct counter_ops counter_write() callback[^2], while overflows are typically implemented by pushing COUNTER_EVENT_OVERFLOW Counter events[^3] that can be watched by userspace.

Yes, I think the Generic counter makes sense here for the goal to be achieved. Thanks for the pointers, I'll see how the counter fits with the paradigm.

-- Daniel

Quoted 5 lines

William Breathitt Gray

[^1] https://docs.kernel.org/driver-api/generic-counter.html#paradigm
[^2] https://docs.kernel.org/driver-api/generic-counter.html#c.counter_ops
[^3] https://docs.kernel.org/driver-api/generic-counter.html#counter-events

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
#6 lore raw

Re: [PATCH v3 3/3] counter: Add STM based counter

Quoted 23 lines · Daniel Lezcano

Hi William,

Quoted 19 lines · William Breathitt Gray
Quoted 18 lines · Daniel Lezcano

The NXP S32G2 automotive platform integrates four Cortex-A53 cores and three Cortex-M7 cores, along with a large number of timers and counters. These hardware blocks can be used as clocksources or clockevents, or as timestamp counters shared across the various subsystems running alongside the Linux kernel, such as firmware components. Their actual usage depends on the overall platform software design.

In a Linux-based system, the kernel controls the counter, which is a read-only shared resource for the other subsystems. One of its primary purposes is to act as a common timestamp source for messages or traces, allowing correlation of events occurring in different operating system contexts.

These changes introduce a basic counter driver that can start, stop, and reset the counter. It also handles overflow accounting and configures the prescaler value.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Hi Daniel,

It sounds like you're trying to implement a clock for timestamping.

Well no, it is a counter which is used for timestamping. It is an automotive design.

I'm sorry, I misunderstood your device earlier. We'll continue with the Counter driver implementation in that case.

Quoted 4 lines
Quoted 4 lines

Regardless, if you do pursue a Counter driver you'll need to follow the Generic Counter paradigm[^1] and define at least three core components:
a Signal, a Synapse, and a Count. Resetting the Count is typically implemented by defining a struct counter_ops counter_write()

Oops, I should have written count_write() there; when a user sets the Count back to 0, it can be considered a reset.

Quoted 9 lines
Quoted 3 lines

callback[^2], while overflows are typically implemented by pushing COUNTER_EVENT_OVERFLOW Counter events[^3] that can be watched by userspace.

Yes, I think the Generic counter makes sense here for the goal to be achieved. Thanks for the pointers, I'll see how the counter fits with the paradigm.

-- Daniel

I suspect you'll define a Signal after the peripheral clock input to the counter device block; if it's possible to read the instataneous level of this signal, define a signal_read() callback for it.

Your Synapse action is dependent on the edge sensitivity (i.e. rising, falling, or both edges) of your counter device; e.g. a rising edge configuration corresponds to a COUNTER_SYNAPSE_ACTION_RISING_EDGE action in action_read(). If your counter only ever increases, then you can report a COUNTER_FUNCTION_INCREASE function in the function_read() callback.

Finally, the component names should be intuitive so give the Count a more intuitive name than "stm-cnt". The same idea applies to the name you give the Signal.

William Breathitt Gray

#7 lore raw
In reply to Daniel Lezcano

Hi William,

just a heads up about these changes.

Thanks

-- Daniel

Quoted 53 lines · Daniel Lezcano

The NXP S32 family provides a System Timer Module (STM), a 32-bit free-running counter clocked from a peripheral clock. The STM includes a prescaler and one or more compare channels generating optional interrupts. When used as a generic hardware counter, only the main free-running counter is required, while the compare channels are typically unused.

On S32G2 devices, the STM is exposed as a simple counter block that can operate continuously and be shared across subsystems such as the Linux kernel, firmware components running on Cortex-M7 cores, or other co-processors. The counter can be read atomically and provides a stable timestamp source to correlate events occurring in different execution contexts.

The Linux kernel controls the STM through a memory-mapped interface, configuring the prescaler, enabling or disabling the counter, and accounting for wrap-arounds. Other subsystems access the counter in read-only mode, making it a shared timestamp reference across the platform.

This driver adds support for the STM when used as a counter on S32G2 platforms. The device is described in the device tree using the following compatible:

compatible = "nxp,s32g2-stm-cnt";

The driver exposes basic counter functionality: start, stop, reset, prescaler configuration, and overflow handling.

Changelog:
* v3
- Fixed compatible typo "nxp,s32g2-stm" to "nxp,s32g2-stm-cnt"

* v2
- Added Rob's tag
** kbuild
- Reordered alphabetically the headers
- Added bitfield.h header
- Use DEFINE_SIMPLE_DEV_PM_OPS() and pm_sleep_ptr()

Daniel Lezcano (3):
counters: Reorder the Makefile
dt-bindings: counter: Add NXP System Timer Module Counter
counter: Add STM based counter

  .../bindings/counter/nxp,s32g2-stm-cnt.yaml   |  64 +++
  drivers/counter/Kconfig                       |  10 +
  drivers/counter/Makefile                      |  21 +-
  drivers/counter/nxp-stm-cnt.c                 | 386 ++++++++++++++++++
  4 files changed, 472 insertions(+), 9 deletions(-)

create mode 100644 Documentation/devicetree/bindings/counter/nxp,s32g2-stm-cnt.yaml create mode 100644 drivers/counter/nxp-stm-cnt.c

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog