Commit graph zephyr/drivers/timer
Author SHA1 Message Date
Nicolas Pitre
9da75b3eab drivers: timer: mark sloppy idle as experimental
CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE lets the kernel skip the wakeups that
keep uptime accurate while no timeout is pending. No in-tree board
selects it and no test enables it, so the drivers implementing it have
never run. Several stop their time base outright, which freezes
k_cycle_get_32() under a running thread and can hang k_busy_wait(), and
some of those cannot start it again at all.

Mark the option experimental until those drivers have been audited.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-08-12 19:27:28 -04:00
Keith Short
0f521cd8df soc: ite: Add deep sleep time measurement support for ITE ECs
Track the time spent in deep doze / sleep states on ITE EC SoCs
(IT8xxx2, IT51xxx) by capturing the 32.768 kHz free-run timer timestamp
before entering deep sleep and accumulating elapsed cycles upon waking.

Add ite_ec_clock_get_sleep_ticks() to convert total deep-sleep cycles
to kernel ticks.

Signed-off-by: Keith Short <keithshort@google.com>
Assisted-by: Antigravity:gemini-1.5-pro
2026-08-12 13:00:41 -04:00
Keith Short
f968be4a80 soc: ite: it8xxx2: Rename ite_ec_timer_block_idle
Rename ite_ec_timer_block_idle() to ite_it8xxx2_timer_block_idle() to
reflect that this DMA clock synchronization idle check is specific to
the IT8XXX2 SoC family. Remove the unnecessary header guard around its
prototype in soc_timer.h.

Signed-off-by: Keith Short <keithshort@google.com>
Assisted-by: Antigravity:gemini-1.5-pro
2026-08-12 13:00:41 -04:00
Holt Sun
efa8637ac7 soc: nxp: keep mcux lptmr as an explicit per-SoC default
The MCUX LPTMR system-timer driver previously defaulted to enabled
whenever the /chosen/zephyr,system-timer node was compatible with
nxp,lptmr. On SoCs whose devicetree points zephyr,system-timer at an
LPTMR instance (e.g. frdm_mcxw71/mcxw716c), this forced every build,
including non-power-management builds, to select the 32.768 kHz LPTMR
and disable the Cortex-M SysTick. Tests that rely on the high-resolution
SysTick clock (tests/kernel/timer/timer_api) then failed because the
1024 tick/s LPTMR does not provide the required timing resolution.

Restore the explicit per-SoC policy: the generic driver no longer
defaults MCUX_LPTMR_TIMER on, and each SoC opts in on its own terms.
MCXW7xx, KE1xF and MCXC enable the LPTMR only when power management is
enabled (default y if PM); i.MX95 and i.MX952 M7 enable it only when the
LPTMR is not claimed by the counter driver (default y if
!COUNTER_MCUX_LPTMR). This keeps SysTick as the system timer for plain
builds while preserving the low-power LPTMR configuration for PM builds.

Update the 4.5 migration guide accordingly.

Fixes #115491

Signed-off-by: Holt Sun <holt.sun@nxp.com>
2026-08-11 20:54:32 -04:00
Markus Grafen
6f7587a6b3 drivers: timer: cortex_m_systick: fix LPM tick accounting overflow
When CONFIG_SYSTEM_TIMER_LPM_COMPANION_COUNTER is used to keep time across
a low-power state, sys_clock_idle_exit() reconstructs the elapsed time
and announces it to the kernel. Two intermediate values were computed
in 32 bits, which overflows once a single low-power sleep exceeds 2^32
core cycles (~134 s at 32 MHz):

  - dcycles (the unannounced cycle delta) was uint32_t. Truncation
  makes the announced tick count far too small, so k_sleep() never
  completes and the MCU appears to never wake from STOP.

  - announced_cycles += dticks * CYC_PER_TICK evaluated the product in
  32 bits before adding to the 64-bit accumulator. Truncation under-counts
  announced_cycles by a multiple of 2^32, so each subsequent sleep's
  dcycles is too large and kernel time runs progressively fast.

Widen dcycles to 64 bits and cast the multiply to cycle_t so both
operations are performed in 64 bits. Only affects builds with a 64-bit
cycle counter (CONFIG_CORTEX_M_SYSTICK_64BIT_CYCLE_COUNTER) using the
counter companion; verified on STM32L151 (32 MHz core, LSE RTC companion)
with 5- and 9-minute STOP intervals.

Signed-off-by: Markus Grafen <markus.grafen@medicassist.de>
Assisted-by: Claude:claude-opus-4.8
2026-08-07 11:17:03 -04:00
Michal Frankiewicz
8b92d9b807 drivers: clock_control: split clock control into individual clocks.
Split clock control into individual clocks:
HFCLK, XO, LFCLK, HFCLK192M, XO24M, HFCLKAUDIO

Signed-off-by: Michal Frankiewicz <michal.frankiewicz@nordicsemi.no>
2026-08-06 14:30:12 +02:00
Michal Frankiewicz
bca199e0bd drivers: clock_control: Added common functions for separated clocks.
Added common functions for separated clocks.

Signed-off-by: Michal Frankiewicz <michal.frankiewicz@nordicsemi.no>
2026-08-06 14:30:12 +02:00
Elie Carrot
d789ccc18b drivers: timer: add missing Kconfig dependency
Add SOC_SERIES_QINGKE_V3C to the timer driver's Kconfig dependencies

Signed-off-by: Elie Carrot <elie.carrot@smile.fr>
2026-08-03 15:00:06 -04:00
Nicolas Pitre
21c38193ee drivers: timer: cortex_m_systick: derive min timeout from cycle rate
MIN_DELAY is the floor of the SysTick LOAD register: the closest-in
timeout the driver will program. It must exceed the longest the SysTick
interrupt can stay masked, otherwise the 24-bit counter can wrap more than
once between elapsed() reads and lose a LOAD's worth of cycles. That is a
wall-clock property, but the default was a fixed cycle count:

	MAX(1024, CYC_PER_TICK/16)

A fixed cycle count is a different wall-clock time on every clock. On a
32 kHz SysTick the 1024-cycle floor is ~31 ms (31 ticks), so the driver
could never schedule a timeout shorter than 31 ticks and every such board
had to override zephyr,min-timeout-cycles by hand.

Compute it instead as a fixed wall-clock budget scaled to the actual
frequency (min_delay = SYSTICK_MIN_DELAY_US * HW_CYCLES_PER_SEC), resolved
at init and refreshed on a runtime frequency change, so it also works when
the frequency is only known at runtime. At ~10 us this matches the old
1024 cycles near 100 MHz and scales sensibly elsewhere. The tick rate is
not involved; if the budget exceeds a tick on some clock, sub-tick timeouts
are simply unavailable there. A dts override still takes precedence.

Floor the result at two cycles: LOAD is programmed as (cycles - 1) and a
LOAD of zero stops the counter, so the smallest usable reload is two cycles
(LOAD 1). On a slow clock such as a 32 kHz SysTick the 10 us budget rounds
below that, so the hardware floor is what applies and the board still needs
no override.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-08-02 11:43:02 -04:00
Neil Chen
927d458eaa drivers: timer: mcux_os_timer: reduce MIN_DELAY default to 50
The default MCUX_OS_TIMER_MIN_DELAY of 1000 cycles is too large
relative to CYC_PER_TICK (100) on platforms clocked at 1 MHz with
SYS_CLOCK_TICKS_PER_SEC=10000. Because MIN_DELAY exceeds CYC_PER_TICK
by 10x, nearly every sys_clock_set_timeout() call adds an extra tick,
inflating short delays and causing the kernel timer_behavior test
(timer_ramp) to fail for low delay values.

Reduce the default to 50 cycles (50 us at 1 MHz), which provides
sufficient hardware setup margin for the match register without
interfering with normal tick scheduling.

Verified on frdm_mcxa344 and frdm_mcxa156 hardware:
- tests/kernel/timer/timer_behavior: all cases pass

Signed-off-by: Neil Chen <cheng.chen_1@nxp.com>
2026-07-31 20:02:54 -04:00
Anas Nashif
b919e796d9 drivers: timer: cortex_m_systick: export raw VAL from elapsed()
The val_out snapshot exported by elapsed() was written after the
wrap-realignment step, so callers received last_load instead of 0
whenever sample C happened to read VAL == 0.

sys_clock_set_timeout() feeds that snapshot into the drift
compensation as val1 and compares it against a later raw read of
SysTick->VAL as val2. Mixing a realigned operand with a raw one makes
val1 - val2 evaluate to a whole counter period that never elapsed, so
cycle_count jumps forward by up to one full timer period on every
reprogram that lands on a zero VAL sample.

The realignment of 0 to last_load is an internal convention of
elapsed(): it exists only so the wrap-detection below can assume
COUNTFLAG and wrapping occur on the same cycle. It has no meaning for
a caller that measures a window against its own raw register read.
Move the export to immediately after the three register reads so
val_out carries the raw sample, which is what the code did before
elapsed() grew the out-parameter. The out-parameter still removes the
gap between the window elapsed() accounts for and the window the
drift compensation measures.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-30 12:50:11 -05:00
Etienne Carriere
a49b9ed52a drivers: timer: Include include/zephyr/sys/clock.h
Header file include/zephyr/sys_clock.h is deprecated and will be removed
someday. Update the whole file tree to include zephyr/sys/clock.h
straight instead of zephyr/sys_clock.h.

This change was made running the sed shell command below:
$ sed -i 's/zephyr\/sys_clock\.h/zephyr\/sys\/clock\.h/' \
      `grep -rsl "zephyr/sys_clock\.h" drivers/timer`

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2026-07-30 07:45:05 -05:00
Surya Prakash T
4c04c7f73d drivers: timer: cortex_m_systick: thread VAL snapshot from elapsed()
Refactor elapsed() to thread the SysTick->VAL snapshot out via an
optional pointer parameter instead of requiring an independent,
racy re-read or duplicating the wrap-detection logic.

An earlier fix for a VAL re-read race in timeout calculation unrolled
elapsed() inside sys_clock_set_timeout(). That unrolling duplicated
the complex wrap-detection logic across two places in the driver,
creating a maintenance hazard.

Resolve this by refactoring elapsed() to accept a uint32_t *val_out
parameter. When provided, it exports the exact val2 snapshot used
to calculate elapsed cycles. In sys_clock_set_timeout(), calling
elapsed(&val1) safely captures this snapshot to seed the drift
calculation, eliminating the timing gap with perfect precision and
no code duplication.

Signed-off-by: Surya Prakash T <suryat@aerlync.com>
2026-07-30 07:42:49 -05:00
Liu Changjie
5e122a12dd drivers: timer: wch: enable systick on CH32H41X
Allow the CH32V QingKe systick timer driver to be selected on the CH32H41X
series. The systick register layout (CTLR/SR/CNT/CMP) is identical to the
existing QingKe parts, so no driver code change is needed; only the Kconfig
dependency has to be extended.

Signed-off-by: Liu Changjie <liucj1228@outlook.com>
2026-07-27 11:36:23 -07:00
Mathieu Choplain
ceeb42a55e drivers: timer: mcux_*: use autogenerated Kconfig variable for compats
Instead of manually defining a variable for the compatibles' names, use
the ones that get autogenerated upon build.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2026-07-27 14:43:22 +02:00
Mathieu Choplain
9b87cdb062 drivers: timer: cmsdk_apb_timer: use autogenerated Kconfig variable
Instead of manually defining a variable for the compatible's name, use
the one that gets autogenerated upon build.

Signed-off-by: Mathieu Choplain <mathieu.choplain-ext@st.com>
2026-07-27 14:43:22 +02:00
Anas Nashif
95a3c9df5b timer: openrisc: do not miss a compare armed at or behind the count
The tick timer fires only on an exact TTMR.TP == TTCR[27:0] match, but
sys_clock_set_timeout() wrote the computed compare value blindly. A
deadline that lands at or behind the count by the time TTMR is written
- for instance a timeout armed just before its tick boundary, or an
arm delayed by interrupt latency - is silently missed until the 28-bit
count wraps, 13.4 seconds at the 20 MHz QEMU clock. The one-tick arms
in the ISR (tickful) and at driver init carry the same, rarer,
exposure.

Route all compare writes through a catch-up: after writing the target,
re-read the count and, while the target is not strictly in the future,
push it MIN_DELAY ahead of the fresh count. A post-write count read
still behind the target proves the match is armed; the exit check
accepts any strictly-future target rather than demanding MIN_DELAY of
remaining headroom, which a count that advances between the write and
the re-read could never satisfy (that stricter form livelocks under
QEMU icount, where the count moves every instruction).

Found by the kernel.timer.timeout_churn near-boundary sweep, which
steps zero-duration timer arms across the tick phase: an arm landing
within the final microseconds of a tick fired 13430 ms late.

Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-24 12:40:58 -07:00
Anas Nashif
7a713dcdac Revert "drivers: timer: esp32_sys_timer: adopt the sys_clock_lock() scheme"
This reverts commit 7e681dbfbd.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
12c33a1e5a Revert "kernel: timeout: add sys_clock_unused() and decide sloppy idle centrally"
This reverts commit 926f7c4c0f.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
cd894d6c82 Revert "drivers: timer: stop the clock via sys_clock_unused()"
This reverts commit 269e2e618a.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
998053d6cd Revert "drivers: timer: mcux_os: track the no-deadline state in sys_clock_unused()"
This reverts commit e5903e4796.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
e0f04c2847 Revert "kernel: timeout: add sys_clock_idle_enter() for low-power idle handoff"
This reverts commit 7d0bb85adc.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
3d7be78899 Revert "kernel: timeout: drop the idle argument of sys_clock_set_timeout()"
This reverts commit fa9b836416.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
5e3755f258 Revert "drivers: timer: add generic tickless system-timer core"
This reverts commit ca254d5a1d.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
1682c93b77 Revert "drivers: timer: riscv_machine: use the generic timer core"
This reverts commit 23ad0c4df5.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
f56b5b7109 Revert "drivers: timer: arm_arch: use the generic timer core"
This reverts commit 772b66b9af.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
2093a8a64f Revert "drivers: timer: riscv_supervisor: use the generic timer core"
This reverts commit d367b01e88.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
702a2a91af Revert "drivers: timer: hpet: use the generic timer core"
This reverts commit 8f0b89f00d.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
5315b021df Revert "drivers: timer: mips_cp0: use the generic timer core"
This reverts commit 86b421a8e0.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
8c51154db3 Revert "drivers: timer: cmsdk_apb: make the cycle read wrap-safe"
This reverts commit 9a29bf5fac.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
cd2767565a Revert "drivers: timer: cmsdk_apb: derive min timeout from cycle rate"
This reverts commit b2a4659e5e.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
51f0dd7080 Revert "drivers: timer: cmsdk_apb: use the generic timer core"
This reverts commit b5e4dc82a4.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
d0c9d59879 Revert "drivers: timer: cortex_m_systick: derive min timeout from cycle rate"
This reverts commit 1b608c64c9.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
9e2830833e Revert "drivers: timer: cortex_m_systick: use the generic timer core"
This reverts commit d277540ce0.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
0a1a94fce9 Revert "drivers: timer: arcv2_timer0: use the generic timer core (non-SMP)"
This reverts commit 72b15164ac.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
fc68c1791a Revert "drivers: timer: renesas_ra_ulpt: use the generic timer core"
This reverts commit 2b432e151b.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
4735f03a72 Revert "drivers: timer: xtensa_sys_timer: use the generic timer core"
This reverts commit 4b0877cebe.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
4788eea582 Revert "drivers: timer: openrisc_tick_timer: use the generic timer core"
This reverts commit fe872510f5.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
2ed81af071 Revert "drivers: timer: leon_gptimer: use the generic timer core"
This reverts commit 112ed46350.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
5ae4107e5c Revert "drivers: timer: native_sim_timer: use the generic timer core"
This reverts commit e8163a1052.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
b78adcb5a0 Revert "drivers: timer: apic_tsc: use the generic timer core"
This reverts commit b63136f402.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
4fc41d796f Revert "drivers: timer: intel_adsp_timer: use the generic timer core"
This reverts commit 72d14283bd.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
5d84d97777 Revert "drivers: timer: esp32_sys_timer: use the generic timer core"
This reverts commit dde56fe906.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
30bde0ca80 Revert "drivers: timer: cortex_m_systick: keep the drift-comp VAL sample raw"
This reverts commit 1f4ecea7f6.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
aa789c4a99 Revert "drivers: timer: fix the equality-match compare catch-up livelock"
This reverts commit 0bbd4843fa.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
919a7f6aac Revert "drivers: timer: declare 64-bit counter widths to the generic core"
This reverts commit 11b5349051.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
ddeb4c1989 Revert "drivers: timer: riscv_supervisor: declare the 64-bit counter width"
This reverts commit c7fa0dbf01.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
12ef92c921 Revert "drivers: timer: openrisc: do not drop a comparator match when rearming"
This reverts commit 2e1bbb20d0.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Anas Nashif
57359d618b Revert "drivers: timer: core: skip redundant rearms and protect catch-up reloads"
This reverts commit 4f6aebf11c.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-22 10:27:24 -04:00
Nicolas Pitre
4f6aebf11c drivers: timer: core: skip redundant rearms and protect catch-up reloads
The kernel re-evaluates its earliest timeout on every add and abort; a
timeslice reset is one of each, so a syscall-heavy thread produces a
sustained stream of sys_clock_set_timeout() calls, most of them landing
on the same tick boundary as the previous one. For a RELOAD backend
every reprogram restarts the hardware counter, so such a stream keeps
any period from ever completing and can postpone the announce for the
stream's duration; it also multiplies the reprogram windows in which
emulated timers misbehave (QEMU's SysTick holds VAL at zero around a
reprogram, and its cancelled-period callbacks race the vCPU).

Two guards in timer_core_arm(), both cheap and backend-agnostic:

- Deadline dedup: cache the armed tick-aligned deadline and program the
  hardware only when it moves. The announce invalidates the cache, as
  do the drivers whose sys_clock_unused() stops or reprograms the
  hardware behind the core's back (cortex_m_systick, cmsdk_apb).

- Catch-up latch: when the reload is floored at TIMER_CORE_MIN_DELAY
  (deadline due, or the un-announced span about to overrun
  TIMER_CORE_CYCLES_MAX), leave an already in-flight floored reload
  alone instead of restarting it, so the fire that resolves the
  catch-up gets through even under a rearm stream faster than the
  floor. Cleared by the announce.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 22:52:09 -05:00