Commit graph zephyr/drivers
Author SHA1 Message Date
Benjamin Cabé
fc09bf7096 drivers: sensor: tad214x: Fix angle full-scale divisor and overflow
The angle conversion divided by 65635 instead of the 16-bit full
scale 65535, and raw_val * 36000 overflows signed 32-bit for large
readings. Use 65535 and widen to uint32_t.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:07:46 +01:00
Benjamin Cabé
0f2e48108e drivers: sensor: tsl2540: Fix analog gain configuration
CFG2 was written with the CFG1 bit pattern so the AGAINMAX/AGAINL
bits were never programmed, and the 128x case loaded the CFG2
register value (20) into the lux gain instead of TSL2540_AGAIN_S128
(140).

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:07:37 +01:00
Benjamin Cabé
456baea0d3 drivers: sensor: si7210: Fix 2000G scale coefficient OTP addresses
The 2000G A3-A5 OTP addresses jumped from 0x29 to 0x30-0x32 instead
of continuing at 0x2A-0x2C, pointing at the wrong registers.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:07:29 +01:00
Benjamin Cabé
a74c96af99 drivers: gpio: adp5585: Configure input pins as inputs
The GPIO_INPUT branch wrote adp5585_pin_output into the direction
register, so pins requested as inputs were driven as outputs.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:07:15 +01:00
Benjamin Cabé
d1fabcf3a7 drivers: i2c: bflb: Clamp phase3 based on its own value
The phase3 clamp tested phase0, so phase3 could overflow its one-byte
register field. Test phase3.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:06:50 +01:00
Benjamin Cabé
0993ce94a3 drivers: serial: silabs: Fix IRQ lock leak in usart rx_buf_rsp
uart_silabs_async_rx_buf_rsp() returned -EBUSY/-EACCES without
releasing the irq_lock taken at entry, leaving interrupts disabled.
Unlock before the early returns.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:06:30 +01:00
Benjamin Cabé
b41c729139 drivers: serial: silabs: Fix IRQ lock leak in eusart rx_buf_rsp
eusart_async_rx_buf_rsp() returned -EBUSY/-EACCES without releasing
the irq_lock taken at entry, leaving interrupts disabled. Unlock
before the early returns.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:06:30 +01:00
Benjamin Cabé
b6938fb624 drivers: serial: uart_rzt2m: Fix baud table bounds check
The enum index guard used '>' against ARRAY_SIZE, allowing a read one
element past the end of baud_settings[]. Use '>='.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:06:08 +01:00
Benjamin Cabé
68319b378d drivers: input: renesas_ra_ctsu: Report touched buttons as pressed
The button callback runs only for buttons whose status bit is set but
reported value 0, so presses were never delivered. Report 1 like the
renesas_rx_ctsu driver.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:05:30 +01:00
Benjamin Cabé
58f4292cd9 drivers: sensor: mmc56x3: Apply new auto-self-reset value on change
The reconfigure path detected an auto_sr change but programmed the
old value, so the attribute never took effect. Pass the new value.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:05:18 +01:00
Benjamin Cabé
02240c6b0e drivers: sensor: afbr_s50: Fix misplaced comparison in BUILD_ASSERT
The dual-frequency assertion placed '== 0' inside the DT_INST_PROP
property-name argument. Move the comparison outside the macro call,
matching the assertion below it.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-22 11:05:07 +01:00
Venkatesh Odela
91d808ec34 drivers: mdio: xlnx_gem: extend MDIO idle timeout
Replace fixed retry polling (~500 us) with WAIT_FOR() and a configurable
idle timeout defaulting to 1 s, so slow PHY bring-up on some platforms
can complete before read/write operations fail with -ETIMEDOUT

Signed-off-by: Venkatesh Odela <venkatesh.odela@amd.com>
2026-07-22 11:04:37 +01:00
Jordan Yates
41a65298dd modem: cellular: default 127 byte MTU for nRF93M1
Default to the larger MTU when using a nRF93M1, the default size fails
to boot `samples/net/cellular_modem` due to dropped frames.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2026-07-22 11:04:22 +01:00
Jordan Yates
67b7fb5810 modem: cellular: vendor: move PPP pointer to config
Move the PPP instance pointer from the `struct modem_cellular_data`
object to `struct modem_cellular_config`. The pointer is automatically
discovered in the `MODEM_CELLULAR_DEFINE_INSTANCE` macro through
`&MODEM_CELLULAR_INST_NAME(ppp, inst)`, which works for all in-tree
modems.

As a result, there are no longer any fields in
`struct modem_cellular_data` that are set at compile time. This saves
~4kB on ROM (for nRF93M1) as the variable no longer needs to exist in
`.data`.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2026-07-22 11:03:27 +01:00
Jordan Yates
abc251ab61 modem: cellular: vendor: move chat config to vendor struct
Move the specification of the chat delimiter and filter strings to the
vendor configuration strings. This is a step towards removing all
compile time field setting of the `struct modem_cellular_data` object
in drivers.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2026-07-22 11:03:27 +01:00
Jordan Yates
523b878e17 modem: cellular: vendor: lex10q1: fix compilation
The `lex10q1` driver was merged without any tests, and therefore no
longer compiles. Fix the driver, and add tests.

Signed-off-by: Jordan Yates <jordan@embeint.com>
2026-07-22 11:03:27 +01:00
Paulo Santos
c19ef4f72b modem: cellular: report EG25-G serving-cell info via QENG
Add AT+QENG="servingcell" to the Quectel EG25-G periodic script and decode
the LTE serving-cell report into the generic network-status event, so
consumers receive cell identity (MCC/MNC/CI/PCI/TAC), EARFCN, band and
RSRP/RSRQ rather than only registration state and RSSI.

The handler fills struct cellular_evt_network_status and reports it via
modem_cellular_emit_network_status(). Only the LTE report is decoded;
other radio access technologies use a different field layout and a shorter
report (state SEARCH/LIMSRV) is ignored via an argument-count guard.

Signed-off-by: Paulo Santos <pauloxrms@gmail.com>
2026-07-22 11:02:14 +01:00
Paulo Santos
3381a2bb4c modem: cellular: emit nrf93m1 network status only on change
Route the nrf93m1 %BCINFO handler through
modem_cellular_emit_network_status() so the periodic poll raises
CELLULAR_EVENT_NETWORK_STATUS_CHANGED only when the serving cell differs
from the last report.

Signed-off-by: Paulo Santos <pauloxrms@gmail.com>
2026-07-22 11:02:14 +01:00
Paulo Santos
b699833666 modem: cellular: cache and expose last network status
Cache the last reported serving-cell status and add an optional
get_network_status driver op plus cellular_get_network_status(), so it can
be read on demand rather than only through the
CELLULAR_EVENT_NETWORK_STATUS_CHANGED callback. The snapshot is returned
under the API lock; -ENODATA before the first report or after a
registration change until the next poll, -ENOSYS where unimplemented.

modem_cellular_emit_network_status() stores the latest status and emits the
event only when it changed, ignoring signal quality (rsrp/rsrq) so periodic
pollers do not re-fire it on signal fluctuation. The cache is dropped on a
registration change so a stale serving cell is not reported after
re-registering.

Signed-off-by: Paulo Santos <pauloxrms@gmail.com>
2026-07-22 11:02:14 +01:00
Fin Maaß
7233acb33d drivers: ethernet: mdio: use mdio without ethernet
allow use of mdio without ethernet and networking.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2026-07-22 11:01:22 +01:00
Aric Radzin
b7d95129cc drivers: adc: add periodic read option to adc shell
Add an optional `period_ms` argument to the `read` command of adc shell
that makes the `read` command periodcially read and print until any key is
pressed.

Signed-off-by: Aric Radzin <aric.radzin@gmail.com>
2026-07-22 10:59:55 +01:00
Muhammed Asif
be1fdf2cf7 drivers: microchip: pic32cmjh: Updates counter tcc g1 driver
- Updates the driver to support families which have only one
  IRQ line for all the channel interrupts and overflow interrupt
- Adds single ISR for handling the interrupts for such cases.
- Updates custom return values to standard return values

Signed-off-by: Muhammed Asif <asifp3104@gmail.com>
2026-07-22 10:59:44 +01:00
minyuan xue
ba9f12abd1 drivers: pwm: add ameba pwm driver
Add realtek ameba pwm driver.

Signed-off-by: minyuan xue <minyuan_xue@realsil.com.cn>
2026-07-22 10:58:45 +01: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
Nicolas Pitre
2e1bbb20d0 drivers: timer: openrisc: do not drop a comparator match when rearming
Writing TTMR clears a pending TTMR.IP. The kernel rearms the comparator
on every timeout add and abort (a timeslice reset is one of each), so
under a rearm-heavy workload the write that follows a comparator match
can eat the match before the exception is taken. Announces then only
happen when a later target is reached cleanly; with the 28-bit compare
domain a long enough streak of eaten matches lets the masked elapsed
delta saturate and alias, and uptime drifts behind real time.

Write TTMR with IP set when arming (IP is cleared by writing 0 and
unaffected by writing 1) so a match that raced the rewrite stays
pending, and acknowledge only in the ISR, which now clears the pending
bit in both the tickless and the tickful case.

The rearm catch-up loop in this driver has a livelock of its own,
addressed separately (it also affects mips_cp0 and xtensa).

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 22:52:09 -05:00
Nicolas Pitre
c7fa0dbf01 drivers: timer: riscv_supervisor: declare the 64-bit counter width
Same omission as the one fixed for apic_tsc, hpet and
riscv_machine_timer: the driver reads the 64-bit "time" CSR and
declares TIMER_CORE_64BIT_CYCLES, but without TIMER_CORE_CYCLES_WIDTH
the generic core masks deltas at the native register width, so an RV32
build aliases any delta beyond 2^32 cycles and the SBI deadline ends up
a whole 2^32-cycle period behind the counter.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 22:52:09 -05:00
Anas Nashif
11b5349051 drivers: timer: declare 64-bit counter widths to the generic core
The generic timer core masks announce deltas to TIMER_CORE_CYCLES_WIDTH,
which defaults to the native register width. The hpet, riscv_machine
and apic_tsc conversions kept their genuinely 64-bit counters but did
not declare the width, so a 32-bit build silently masks every delta to
32 bits (TIMER_CORE_64BIT_CYCLES only emits sys_clock_cycle_get_64(),
it does not widen the mask). The pre-conversion drivers all carried
full 64-bit tick math.

On 32-bit x86 with the HPET this is fatal as soon as more than 2^32
cycles (43 s at 100 MHz) elapse without an announce: the masked delta
aliases modulo 2^32, the announce reports ~0 ticks, and the re-armed
deadline lands a whole 2^32-cycle period behind the counter. The
comparator helper checks its target against the full 64-bit count, so
it treats the deadline as past and pins the comparator to "now",
firing immediately; the following announce again aliases to 0 ticks
and re-arms behind. The result is a permanent interrupt storm with
kernel time frozen at tick zero.

Declare TIMER_CORE_CYCLES_WIDTH 64 in all three drivers, as the
arm_arch, intel_adsp and esp32 conversions already do. For riscv
(64-bit mtime on RV32) and apic_tsc (64-bit TSC) the aliasing is the
same latent defect awaiting a long enough gap.

Assisted by Claude (claude-opus-4-8).
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-21 22:52:09 -05:00
Anas Nashif
0bbd4843fa drivers: timer: fix the equality-match compare catch-up livelock
The generic-core conversions gave the equality-match COMPARE drivers
(openrisc_tick_timer, mips_cp0_timer, xtensa_sys_timer) a catch-up
loop that pushes a too-near target ahead of the counter:

  while (diff(next, now) < MIN_DELAY) {
          next = now + MIN_DELAY;
          set_compare(next);
          now = get_count();
  }

The exit condition demands the target still be a full MIN_DELAY ahead
after re-reading the counter, but the counter keeps moving between the
compare write and the re-read, and each retry re-arms exactly
MIN_DELAY ahead of the previous, already stale, read. The loop can
therefore only exit if the counter advances zero cycles per iteration.
Wherever it advances every iteration the loop spins forever: under
QEMU icount (qemu_or1k runs with -icount shift=6, advancing TTCR ~1.3
cycles per instruction) entry means a deterministic livelock, and on
real hardware whose count runs at CPU speed (Xtensa CCOUNT) the same
holds.

An equality comparator only requires that the target was still ahead
of the count when the compare register was written, and a post-write
count read that is still behind the target proves exactly that. Relax
the exit check to accept any strictly-future target, the same
convergent form hpet.c already uses; a retry then lands MIN_DELAY
ahead of a fresh read and is confirmed by the next one. The other
COMPARE conversions are unaffected: hpet already checks this way, and
esp32/intel_adsp bump once before the write against >=-style alarms.

Assisted by Claude (claude-opus-4-8).
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-21 22:52:09 -05:00
Anas Nashif
1f4ecea7f6 drivers: timer: cortex_m_systick: keep the drift-comp VAL sample raw
The conversion to the generic timer core had elapsed() return its
SysTick->VAL sample through an out-parameter so that
timer_driver_set_reload() can reuse it gap-free for the drift
compensation. The value handed out was the wrap-realigned one
(0 -> last_load), but the drift-compensation formula operates on raw
register samples: the pre-conversion driver read a fresh raw
SysTick->VAL for it.

When the counter genuinely reads zero, the realigned sample makes the
compensation compute (val1=last_load) - (val2=0) and credit a full
timer period that never elapsed. On hardware VAL==0 is a single-cycle
transient, but QEMU's SysTick holds VAL at zero for up to 10 us of
virtual time after every reprogram (deferred ptimer reload plus the
10 us timeout floor), so any two set_timeout() calls landing within
that window inject the whole previous period as phantom elapsed
cycles. With preemptive time-slicing constantly re-arming the timer,
an idle arm followed by a slice re-arm advanced the software clock by
the full 16.7M-cycle idle span in one step: uptime raced ahead of
real time and every k_timeout fired far too early in wall-clock
terms.

Store the out-parameter before the realignment so the caller gets the
sample as read. The zero-window then contributes zero drift, matching
the pre-conversion behaviour, and the genuine-wrap cases are
unchanged: the wrap itself is accounted through COUNTFLAG/overflow_cyc
and the compensation only adds the small post-wrap remainder.

Assisted by Claude (claude-opus-4-8).
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-07-21 22:52:09 -05:00
Benjamin Cabé
7aac0120ca drivers: input: pmw3610: Use the cached device in the work handler
The work handler already caches data->dev in a local, so reuse it in
both input_report_rel() calls instead of dereferencing data->dev again.

Assisted-by: Claude:opus-4.8
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-21 18:04:41 -04:00
Benjamin Cabé
d1cfc8e88a drivers: can: xmc4xxx: Read the receive error counter
get_state_from_status() read the transmit error counter into both
tec and rec, so the reported RX error count mirrored the TX count
and the error-passive state could be reported incorrectly. Use the
receive error counter accessor for rec.

Assisted-by: Claude:fable-5
Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-07-21 18:04:31 -04:00
Raffael Rostagno
f9506e67ac drivers: serial: uart: esp32: Reject non-DMA TX buffers
Return -ENOTSUP when async TX uses a buffer GDMA cannot read.
Allow flash/DROM TX only on SoCs with SOC_DMA_CAN_ACCESS_FLASH.

Signed-off-by: Raffael Rostagno <raffael.rostagno@espressif.com>
2026-07-21 18:04:20 -04:00
Nicolas Pitre
dde56fe906 drivers: timer: esp32_sys_timer: use the generic timer core
Convert the ESP32 systimer to system_timer_generic.h. The free-running
64-bit systimer counter with a one-shot alarm is a COMPARE backend: the
driver keeps z_clock_cycle_get() (the counter) and z_clock_set_compare()
(arm the alarm), and the core takes over the tick accounting, the deadline
math, the range clamp and the announce. The hand-rolled last_count
baseline and the sys_clock_elapsed() / set_timeout() tick math move into
the core; the ISR reduces to clearing the alarm interrupt and announcing.

z_clock_set_compare() keeps the target at least MIN_DELAY ahead so one
that latency left too close is not missed. Both the cycle read and the
alarm arm keep their systimer_hal.dev NULL guard, so a call before the
timer is brought up, or after sys_clock_disable() has torn the HAL down,
does not dereference a cleared dev. The light-sleep compensation
(CONFIG_PM) is preserved: on exit the driver advances the stalled counter
by the missed cycles and lets the core announce the resulting delta.

Build-verified on esp32c3_devkitm, with and without CONFIG_PM. No QEMU
target runs this driver, so this is a build-only conversion.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
72d14283bd drivers: timer: intel_adsp_timer: use the generic timer core
Convert the Intel ADSP wall clock timer to system_timer_generic.h. The
free-running 64-bit wall clock (external to the CPUs, shared across SMP)
with an absolute comparator is a COMPARE backend: the driver keeps
z_clock_cycle_get() (the counter) and z_clock_set_compare() (arm the
comparator), and the core takes over the tick accounting, the deadline
math, the range clamp and the announce. The hand-rolled last_count
baseline and the sys_clock_elapsed() / set_timeout() tick math move into
the core; the ISR reduces to acknowledging the trigger and announcing.

Arming disarms and rearms the comparator, so z_clock_set_compare() keeps
the target at least MIN_DELAY ahead of the counter, so that sequence
cannot race the counter past it. The efficient single-register cycle read
(count32(), one read rather than the three the 64-bit read needs) is kept
as sys_clock_cycle_get_32().

The wall clock is a genuine 64-bit counter on this 32-bit CPU, so
CYCLES_WIDTH is set to 64 to use its full range; the native-width default
would clamp announce deltas to 32 bits.

The previous build assertion requiring MIN_DELAY to be smaller than one
tick is dropped. A minimum comparator delay larger than a tick is not
fatal: it only coarsens the granularity, and the core copes by announcing
whatever actually elapsed, so it no longer needs to be a build-time
constraint.

Build-verified on intel_adsp/ace15_mtpm. No QEMU target runs this driver
(its targets are the ADSP simulator and hardware), so this is a build-only
conversion.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
b63136f402 drivers: timer: apic_tsc: use the generic timer core
Convert the x86 APIC TSC timer to system_timer_generic.h. The free-running
64-bit TSC with an absolute deadline (the TSC_DEADLINE MSR, or the local
APIC timer ICR in one-shot mode as the fallback) is a COMPARE backend: the
driver keeps z_clock_cycle_get() (rdtsc) and z_clock_set_compare()
(set_trigger), and the core takes over the tick accounting, the deadline
math, the range clamp and the announce. The hand-rolled last_cycle
baseline and the sys_clock_elapsed() / set_timeout() tick math move into
the core; the ISR reduces to an announce, and smp_timer_init() keeps
copying CPU0's LVT to each secondary.

The frequency is a build-time constant here (this driver is not
runtime-frequency), so the core derives cycles-per-tick from
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC as before. CYCLES_WIDTH defaults to the
unsigned long width, matching the driver's former native-register divisor.

The deadline arm range narrows from the driver's former three-quarter
span to the core's default half span. Half the span reserves the upper
half as IRQ-latency headroom, so a late announce of a maximum-length arm
still yields an in-range delta; the three-quarter bound did not keep that
margin. The realistic tick range is far below either bound, so this is not
a functional change.

The one dropped behaviour is the set_timeout() guard that pinned a deadline
to UINT64_MAX when the computed deadline wrapped the 64-bit TSC: it guarded
an unreachable, untestable case (the 64-bit TSC takes centuries to wrap,
about 585 years at 1 GHz) and has no clean place in the core, whose range
clamp already bounds the realistic range.

Build-verified on qemu_x86_64 and qemu_x86 (the 64- and 32-bit unsigned
long paths) with the TSC deadline comparator, and on qemu_x86_64 with the
APIC_TIMER_TSC ICR fallback. No QEMU target runs this driver (qemu_x86*
default to HPET, and forcing the TSC timer on QEMU's atom model delivers no
deadline interrupts, on mainline too), so this is a build-only conversion;
its real users are ACRN and x86 hardware.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
e8163a1052 drivers: timer: native_sim_timer: use the generic timer core
Convert the native_sim timer to system_timer_generic.h. The simulator has
no compare register; its time base is the microsecond counter
nsi_hws_get_time() and its only lever is hwtimer_set_silent_ticks(), which
skips that many periodic tick interrupts before raising the next. That
still maps onto a COMPARE backend: z_clock_cycle_get() reads the counter
and z_clock_set_compare() converts the core's absolute, tick-aligned
deadline into the number of ticks to skip (rounding up to the next tick
boundary, since the simulator only interrupts on boundaries and the
counter may sit mid-tick after a busy-wait). The counter is 64-bit and
never wraps (CYCLES_WIDTH 64), and the deadline is a full 64-bit value, so
the long fast-forward idle periods the simulator relies on are preserved
(a 32-bit RELOAD reload would have capped them).

The hand-rolled last_tick_time baseline, the ISR elapsed computation and
sys_clock_elapsed() move into the core; the ISR reduces to an announce.
sys_clock_disable() and the test ISR hook are unchanged.

timer_api, timer_behavior, context, sleep, tickless and common pass on
native_sim, timer_api and context pass on native_sim/native/64, and
timer_api passes on native_sim with CONFIG_TICKLESS_KERNEL=n.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
112ed46350 drivers: timer: leon_gptimer: use the generic timer core
Convert the LEON GPTIMER to system_timer_generic.h. GPTIMER has no
absolute compare register, only a relative reload, so it is a RELOAD
backend: subtimer 1 free-runs as the cycle source and subtimer 0 is
loaded with the relative delay the core derives from a tick-aligned
deadline. The driver keeps z_clock_cycle_get() (the free-running counter)
and z_clock_set_reload() (program subtimer 0); the core takes over the
tick accounting, the deadline math, the range clamp and the announce.

The subtimers run at the system rate divided by the shared prescaler, so
the core works in that (divided) cycle domain: z_clock_cycle_get() and
CYC_PER_TICK share it, and only the public sys_clock_cycle_get_32() scales
back up to the system cycle rate. The hand-rolled announce baseline and
the sys_clock_elapsed()/set_timeout() tick math move into the core; the
ISR reduces to acknowledging (one-shot stop when tickless, keep periodic
otherwise) and announcing.

timer_api, context, sleep and tickless pass on qemu_leon3, and timer_api
passes there with CONFIG_TICKLESS_KERNEL=n (the periodic path).

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
fe872510f5 drivers: timer: openrisc_tick_timer: use the generic timer core
Convert the OpenRISC tick timer to system_timer_generic.h. The
free-running TTCR count with an equality-match TTMR compare is a COMPARE
backend: the driver keeps z_clock_cycle_get() (TTCR) and
z_clock_set_compare() (TTMR), and the core takes over the tick
accounting, the deadline math, the range clamp and the announce.

The compare field TTMR.TP is only 28 bits while TTCR is 32, so the
counter is declared 28-bit wide (CYCLES_WIDTH) and the core clamps arming
to that span, which subsumes the driver's open-coded half-range clamp.
Because TTMR matches only TTCR[27:0] == TP, z_clock_set_compare() keeps a
minimum-delay bump (in the 28-bit domain) so an already-past target is not
missed until the count wraps.

The hand-rolled last_count baseline, the private spinlock and the
sys_clock_elapsed()/set_timeout() tick math move into the core; the ISR
reduces to disarming the compare (tickless) and announcing.

timer_api, context, sleep and tickless pass on qemu_or1k.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
4b0877cebe drivers: timer: xtensa_sys_timer: use the generic timer core
Convert the Xtensa CCOUNT/CCOMPARE timer to system_timer_generic.h. The
free-running 32-bit CCOUNT with an equality-match CCOMPARE is a COMPARE
backend: the driver keeps z_clock_cycle_get() (CCOUNT) and
z_clock_set_compare() (CCOMPARE), and the core takes over the tick
accounting, the deadline math, the range clamp and the announce. Because
CCOMPARE fires only on CCOUNT == CCOMPARE, z_clock_set_compare() keeps the
driver's minimum-delay bump so an already-past target is not missed until
CCOUNT wraps, matching the core's "must not miss a past deadline"
contract.

The hand-rolled last_count baseline, the tickful re-arm and the
sys_clock_elapsed()/set_timeout() tick math all move into the core.
smp_timer_init() primes the per-CPU timer through z_clock_smp_prime().

The low-power compensation is preserved: ccount() still adds the
compensation offset, so the cycle count and every announced delta pick it
up for free, and the Espressif LPM hook
(CONFIG_XTENSA_TIMER_LPM_TIMER_HOOK) now just folds the missed cycles into
that offset and lets the core announce. The non-low-power idle path drops
to the weak sys_clock_idle_enter() default (which arms through
sys_clock_set_timeout()).

timer_api, context, sleep and tickless pass on qemu_xtensa/dc233c.
Build-verified on esp32 with the LPM hook (CONFIG_PM=y) and with SMP.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
2b432e151b drivers: timer: renesas_ra_ulpt: use the generic timer core
Convert the Renesas RA ULPT timer to system_timer_generic.h. ULPT
instance 1 free-runs and provides the cycle counter (its down-counter
read back inverted counts up); ULPT instance 0 is armed with a relative
delay and its underflow announces ticks. That is a RELOAD backend whose
cycle source is a genuine free-running counter, so the driver keeps only
z_clock_cycle_get() (~INST1 counter) and z_clock_set_reload() (arm INST0),
and the core takes over the tick accounting and the tick-aligned deadline.

This removes the driver's open-coded "ticks - 1" adjustment and round-up
dance (the code carrying the "preserve the original behavior even though
it looks wrong; to be revisited" comment): the core computes the reload
from an absolute tick-aligned deadline, which is the correct behavior. The
private spinlock and the announce baseline (cycle_announced) go with it.

Compile-verified on ek_ra8p1 (RENESAS_RA_ULPT_TIMER forced on; the driver
is the default system timer there when CONFIG_PM is enabled). No QEMU
target runs this driver, so this is a build-only conversion.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
72b15164ac drivers: timer: arcv2_timer0: use the generic timer core (non-SMP)
Convert the non-SMP ARCv2 timer0 path to system_timer_generic.h. Timer0
is an up-counter that counts to LIMIT and resets on match: a RELOAD
backend, and the compare-match-reset variant of the reload family (the
cmsdk/systick down-counters were the down-to-zero variant). The driver
keeps its synthesized cycle count with the wrap-aware elapsed() (it folds
a pending overflow in via the IP bit, so the count stays monotonic) and
programs the next LIMIT in z_clock_set_reload(); the core takes over the
tick accounting, the deadline-to-reload conversion, the range clamp and
the announce, replacing the driver's own unannounced/round-up math.

The SMP path (a separate implementation on the gfrc wall clock) is left
unchanged, guarded so only the non-SMP build pulls in the core and its
emitted sys_clock_* entry points.

timer_api and context pass on qemu_arc/qemu_arc_em.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
d277540ce0 drivers: timer: cortex_m_systick: use the generic timer core
Convert the Cortex-M SysTick to system_timer_generic.h. SysTick is an
auto-reload 24-bit down-counter: a RELOAD backend. The driver keeps its
hardware-specific parts, the synthesized cycle count with COUNTFLAG-based
wrap detection (timer_driver_cycle_get) and the reprogram-with-drift-
compensation of the LOAD register (timer_driver_set_reload, the val1/val2
recovery), and the core takes over the tick accounting: announced_cycles
and last_elapsed move into the core, and the deadline-to-reload
conversion, the range clamp and the announce come from it. The 24-bit
LOAD caps the arm at TIMER_CORE_CYCLES_MAX; the reload floor is the
min_delay from the preceding commit (TIMER_CORE_MIN_DELAY).

The driver carries no cycles-per-tick of its own. With the reload floor
no longer expressed in ticks, the only remaining need for that value is
establishing the one-tick LOAD at init and on the low-power restart, which
reads the core's TIMER_CORE_CYC_PER_TICK; the core derives it from the
counter rate (the system clock rate), so the driver no longer defines it.
The former "tickless does nothing" pragma goes too: the core already
build-asserts a non-zero cycles-per-tick.

Care was taken to preserve the subtle behaviour of this driver:

  - The overflow commit at ISR entry runs under the clock lock, held
    through the announce (via the core's timer_core_announce_from() entry
    that takes an already-acquired key), so a higher-priority interrupt
    reading the cycle counter never sees a half-committed overflow, as
    before. sys_clock_idle_exit() adds the missed low-power cycles under
    the same lock and hands off the same way.

  - The drift compensation keeps the gap-free val1 sampling of the
    approved fix in PR #109574: elapsed() returns its SysTick->VAL
    snapshot through an out-parameter, so timer_driver_set_reload() reads
    no separate SysTick->VAL and loses no function-return cycles.

  - With a 64-bit cycle counter the synthesized count is set as the core
    counter width (CYCLES_WIDTH = 64) so the core does not mask announce
    deltas to 32 bits: a low-power sleep longer than 2^32 cycles reports
    a >32-bit delta, and truncating it would announce far too few ticks
    (the k_sleep-never-wakes failure addressed by PR #112806).

  - The reload is computed in the counter's cycle domain, not from the
    linear absolute deadline, so it stays correct across a counter wrap;
    a starved ISR collapses to the reload floor as the old unannounced<0
    guard did.

The runtime frequency update rescales the synthesized counter and, via
the core's timer_core_rescale(), the announce baseline. The reprogrammed
LOAD is now a pure cycle-domain scaling in both modes: the tickless
remainder and the tickful one-tick period are each the previous value
scaled by the new/old rate ratio, so the rescale needs no cycles-per-tick.
The low-power companion and the 64-bit and sloppy-idle-stop paths are all
kept.

Validated on qemu_cortex_m3 and mps2/an385: timer_api and context pass
tickless (default) and tickful, with CORTEX_M_SYSTICK_64BIT_CYCLE_COUNTER,
and the sys_clock_hw_cycles_update test passes (the frequency-rescale
path). The low-power companion path is compile-verified on frdm_mcxa153
with CONFIG_PM; it has no QEMU target to run here.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
1b608c64c9 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-07-21 18:02:43 -04:00
Nicolas Pitre
b5e4dc82a4 drivers: timer: cmsdk_apb: use the generic timer core
Convert the ARM CMSDK APB timer to system_timer_generic.h, the first
driver to exercise the core's RELOAD backend. The auto-reload
down-counter keeps its synthesized cycle count (timer_driver_cycle_get)
and its reprogram-with-drift-compensation (timer_driver_set_reload, the
val1/val2 recovery that loses no cycles between reading and rewriting the
counter); the core takes over the tick accounting, the deadline-to-reload
conversion, the range clamp and the announce. The driver no longer states
its own cycles-per-tick: with the reload floor derived from the cycle rate
by the preceding commit, the only remaining use is the one-tick init
reload, which reads the core's TIMER_CORE_CYC_PER_TICK. sys_clock_unused()
reloads the longest interval the core's range allows under sloppy idle
(capped at TIMER_CORE_CYCLES_MAX, since a full-span reload would wrap the
32-bit synthesized count and alias the wrap announce), as an auto-reload
counter cannot simply stop being reprogrammed. The public cycle counter
carries forward the wrap-safe read from the preceding commits.

Being the first RELOAD user, it exercises two core paths for the first
time:

  - The non-tickless path: a COMPARE timer re-arms its comparator each
    ISR, but an auto-reload counter free-runs from the LOAD set at init,
    so the core skips the per-tick re-arm for RELOAD.

  - The reload is computed in the counter's cycle domain (cycles to the
    deadline minus the masked cycles already elapsed since the last
    announce), not from the linear absolute deadline. The synthesized
    counter wraps at its width while the announce baseline is linear, so
    the absolute form would diverge once the counter passes its wrap;
    the domain-relative form stays correct, and a starved ISR still
    collapses to the reload floor.

Verified behaviour-preserving by pointing mps2/an385's chosen
system-timer at its cmsdk timer0 and running timer_api: it passes,
including under QEMU icount. No in-tree board selects cmsdk as the system
timer, so this configuration is not otherwise exercised in CI.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
b2a4659e5e drivers: timer: cmsdk_apb: derive min timeout from cycle rate
MIN_DELAY_CYCLES is the floor of the reload the driver will program: the
closest-in timeout it can schedule. It must exceed the longest the timer
interrupt can stay masked, otherwise the counter can wrap more than once
between elapsed_cyc() reads and lose a period. 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
slow counter the 1024-cycle floor is tens of milliseconds, so the driver
could never schedule a short timeout and such a board would have to
override CMSDK_APB_TIMER_MIN_DELAY_CYCLES by hand.

Compute it instead as a fixed wall-clock budget scaled to the actual
frequency (k_us_to_cyc_ceil32(CMSDK_MIN_DELAY_US)), resolved at init, so
it also tracks a runtime frequency. At ~10 us this matches the old 1024
cycles near 100 MHz and scales sensibly elsewhere. The
CMSDK_APB_TIMER_MIN_DELAY_OVERRIDE Kconfig still takes precedence.

Floor the result at two cycles so the reload is never degenerate on a
slow clock where the time budget rounds below that; elapsed_cyc()'s reads
still straddle at most one wrap.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
9a29bf5fac drivers: timer: cmsdk_apb: make the cycle read wrap-safe
sys_clock_cycle_get_32() and sys_clock_elapsed() return
cycle_count + elapsed(), where elapsed() is only the in-period offset
(load - value). Between a counter wrap (the down-counter reloads) and the
ISR crediting that period to cycle_count, that offset drops back near
zero, so the reported hardware cycle count briefly goes backwards. A
non-monotonic cycle counter breaks k_busy_wait() and the tick accounting
built on it.

The window is a race under real time, but QEMU icount makes the timing
deterministic and hits it every idle wake, so kernel.timer.timer_api's
test_timer_k_define fails when a cmsdk timer is selected as the system
timer under icount.

Account the pending wrap in the read path the way cortex_m_systick does:
sample the counter either side of the interrupt-status flag and add a
full period if the flag is set or the counter was seen reloading. It is
added to the returned value only; the ISR still commits the period into
cycle_count and clears the flag, so it is never counted twice. The
separate elapsed_monotonic() keeps sys_clock_set_timeout()'s reprogram
math on the raw offset, unchanged.

Reproduce (no in-tree board selects the cmsdk timer as its system timer,
so it has to be chosen by hand; mps2/an385 already has cmsdk timer0 in
its devicetree and runs QEMU with icount):

  $ cat > cmsdk.overlay <<'DTS'
  / { chosen { zephyr,system-timer = &timer0; }; };
  DTS
  $ cat > cmsdk.conf <<'CONF'
  CONFIG_CORTEX_M_SYSTICK=n
  CONFIG_CMSDK_APB_TIMER=y
  CONF
  $ west build -b mps2/an385 tests/kernel/timer/timer_api \
        -- -DEXTRA_DTC_OVERLAY_FILE=cmsdk.overlay \
        -DEXTRA_CONF_FILE=cmsdk.conf
  $ west build -t run

  Before this change test_timer_k_define fails (a spurious
  multi-thousand-tick announce races uptime ahead of the cycle-based
  busy-wait); after it the suite passes 17/17.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
86b421a8e0 drivers: timer: mips_cp0: use the generic timer core
Convert the MIPS CP0 timer to system_timer_generic.h. The CP0 Count
counter with its Compare register is a COMPARE backend, and the first
converted driver whose counter actually wraps during operation: on
32-bit MIPS CYCLES_WIDTH defaults to the native 32 bits, so the core
masks every delta at the 2^32 boundary.

Compare raises the interrupt only on Count == Compare, so an already-past
target is missed until Count wraps all the way around. z_clock_set_compare
therefore bumps the target until it is at least MIN_DELAY ahead, which is
the core's "must not miss a past deadline" contract; the read-back loop
replaces the driver's former open-coded MIN_DELAY handling in both
set_timeout and the ISR. Writing Compare also clears the interrupt.

The core now owns the tick accounting (previously the driver resynced its
baseline to the raw count each ISR, discarding the sub-tick remainder and
drifting slow; the core keeps the baseline tick-aligned, so that is fixed
too) and the locking, so the driver's private spinlock is gone.

timer_api and context pass on qemu_malta.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
8f0b89f00d drivers: timer: hpet: use the generic timer core
Convert the HPET to system_timer_generic.h. The free-running 64-bit
counter with its comparator is a COMPARE backend: the driver keeps
z_clock_cycle_get() (the counter) and z_clock_set_compare()
(hpet_timer_comparator_set_safe), and the core takes over the tick
accounting, the deadline math, the range clamp and the announce.
CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME resolves the cycle rate to
the value read from the counter period at init.

Two HPET specifics stay: the level-trigger interrupt acknowledge in the
ISR, and the sloppy-idle stop / resume in sys_clock_unused() /
sys_clock_idle_exit(). The equality-match comparator (it fires only on
count == cmp) is why z_clock_set_compare() uses the read-back retry;
that is exactly the core's "must not miss a past deadline" contract.

Under QEMU SMP the shared counter can be observed reading backwards; the
driver enables the core's Z_CLOCK_COUNTER_NONMONOTONIC hook to treat such
a read as no elapse, which also drops the arm limit to a quarter span so
the backwards test stays unambiguous. This is HPET's first user.

timer_api and context pass on qemu_x86_64 (SMP, so the backwards hook and
the runtime frequency path are exercised) and qemu_x86.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
d367b01e88 drivers: timer: riscv_supervisor: use the generic timer core
Convert the RISC-V supervisor timer to system_timer_generic.h. The "time"
CSR with the SBI set-timer deadline is a COMPARE backend: the driver
keeps z_clock_cycle_get() (stime) and z_clock_set_compare()
(sbi_set_timer), and the core takes over the tick accounting, the
deadline math, the range clamp and the announce. smp_timer_init() primes
the timer through z_clock_smp_prime().

This also drops the driver's private spinlock: it now runs under the
kernel timer lock via the core's sys_clock_lock() / announce_locked(),
which is the intended locking for a system-timer driver.

timer_api and context pass on qemu_riscv64/qemu_virt_riscv64/smode
(supervisor timer under OpenSBI).

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00
Nicolas Pitre
772b66b9af drivers: timer: arm_arch: use the generic timer core
Convert the ARM generic timer to system_timer_generic.h. The
free-running system counter with its absolute compare register is a
COMPARE backend; the driver keeps z_clock_cycle_get() (the counter) and
z_clock_set_compare() (set the comparator and unmask), and the core takes
over the tick accounting, the deadline math, the range clamp and the
announce. CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME still resolves
CYC_PER_TICK to the value cached at init. smp_timer_init() primes the
per-CPU comparator through z_clock_smp_prime().

The ISR keeps its hardware specifics: the Cortex-A9 erratum 740657
spurious-interrupt handling, and masking the level-asserted compare in
tickless mode (the core's rearm unmasks it). The counter uses the core's
default cycle-get; declaring its 64-bit width with TIMER_CORE_CYCLES_WIDTH
removes the driver's former CONFIG_GDBSTUB-only widening of the cycle
diff, which existed only because the width was not stated. Behaviour is
unchanged; timer_api and context pass on qemu_cortex_a53 UP and SMP.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2026-07-21 18:02:43 -04:00