Skip to content

Conversation

@yongxu-wang15
Copy link
Contributor

@yongxu-wang15 yongxu-wang15 commented Aug 29, 2025

  1. added lptmr tickless function
  2. verified in imx95 m7 core which enabled PM and tickless
    Uploading image.png…

@yongxu-wang15
Copy link
Contributor Author

Updated to fixed dts conflict with newly commit modification.

Hi, this PR is aim to support nxp lptmr tickless function, and supported it in imx95 m7, can you help review it when you time free?

Thanks

@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch 2 times, most recently from a4522f5 to 5b1791a Compare September 29, 2025 01:52
@zephyrbot zephyrbot added platform: NXP NXP area: Tests Issues related to a particular existing or missing test area: Boards/SoCs labels Sep 29, 2025
@zephyrbot zephyrbot requested a review from nashif September 29, 2025 01:54
@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch from 5b1791a to 099ead3 Compare September 29, 2025 05:15
@decsny
Copy link
Member

decsny commented Sep 29, 2025

@mmahadevan108 please help review this

@mmahadevan108
Copy link
Contributor

ping @henrikbrixandersen @DerekSnell
There is a mention in the Reference Manual that "If LPTMR is enabled, you must alter CMR only if TCF = 1." Is this accounted for in your PR.

@henrikbrixandersen
Copy link
Member

ping @henrikbrixandersen @DerekSnell There is a mention in the Reference Manual that "If LPTMR is enabled, you must alter CMR only if TCF = 1." Is this accounted for in your PR.

Yeah, this is the reason I did not implement tickless mode in this driver when I originally submitted it (see 21806b5).

@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch from 099ead3 to 6719da2 Compare October 16, 2025 08:32
@yongxu-wang15
Copy link
Contributor Author

Hi @mmahadevan108 , I have added disable/start code logic in LPTMR_SetTimerPeriod to avoid the hardware limitations, tested passed on imx95 m7 power_mgmt_soc test case, the time sleep and tick record is right in my side, please help reivew it when you time free, thanks

Copy link
Member

@henrikbrixandersen henrikbrixandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stopping/starting the timer when setting a new timeout will affect the ticks (as the time all the instructions between stopping and starting will not be counted). I don't think this is an acceptable solution.

@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch 2 times, most recently from 2446f3e to ac6e777 Compare October 17, 2025 08:56
@yongxu-wang15
Copy link
Contributor Author

Hi @henrikbrixandersen I fully understand your concerns. After reconfirming with the design team, we need to ensure that there is sufficient time when updating the CMP. We can choose to reset(but this will cause the time to go back instead of monotonically increasing. Even through software, it is difficult to guarantee that there are no corner cases). In the latest commit, I added a protection mechanism. When the distance is very close, It will postpone a windo to prevent it from being triggered. Of course, this should depend on different SoCs, please help review it again when you time free, thanks a lot

@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch from ac6e777 to 43ce283 Compare October 17, 2025 09:04
@sonarqubecloud
Copy link

@henrikbrixandersen
Copy link
Member

Hi @henrikbrixandersen I fully understand your concerns. After reconfirming with the design team, we need to ensure that there is sufficient time when updating the CMP. We can choose to reset(but this will cause the time to go back instead of monotonically increasing. Even through software, it is difficult to guarantee that there are no corner cases). In the latest commit, I added a protection mechanism. When the distance is very close, It will postpone a windo to prevent it from being triggered. Of course, this should depend on different SoCs, please help review it again when you time free, thanks a lot

I'd like to hear @andyross and @teburd (System timer maintainer/collaborator) view on this. I am unsure if this is a good solution.

@yongxu-wang15
Copy link
Contributor Author

Hi @henrikbrixandersen, Has there been any progress on this issue? thanks

@henrikbrixandersen
Copy link
Member

Hi @henrikbrixandersen, Has there been any progress on this issue? thanks

@teburd, @andyross Ping?

@teburd
Copy link
Contributor

teburd commented Nov 18, 2025

The design of kernel timers is time never stops (free running counter) that you may set a compare to catch sometime in the future.

Stopping/Starting the underlying timer will surely introduce timing drift and jitter, something that tends to surprise and upset people when they see it. My opinion is that if your hardware needs to stop and start to set the compare, this hardware isn't a good source for kernel timers without at least some build warnings informing people building with it that yes, this is what they really want.

I'm not going to stop a vendor from supplying a driver that introduces drift/jitter. But I do feel like I can say we should warn about it.

Unless you can perhaps show in the PR the external tool test from https://github.com/zephyrproject-rtos/zephyr/tree/main/tests/kernel/timer/timer_behavior with correct timer behavior with this driver with the start/stop behavior? Maybe its ok, I don't know!

default y

config SYS_CLOCK_TICKS_PER_SEC
default 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider to set default to 1024.
For default 1000, since SYS_CLOCK_HW_CYCLES_PER_SEC = 32768, then CYCLES_PER_TICK = 32768 / 1000 = 32 (fractional part is dropped), so one OS “tick” is 32 hardware cycles, than tick_rate = tick_rate = 32768 / 32 = 1024 ticks/s, but the configuration says "1000 ticks/s", there is 2.4% drift.

For a 32768 Hz clock, Zephyr typically uses tick rates that divide 32768 exactly (1024, 512, 256, 128, …), in this way, there is no rafe shift.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, update into 1024 now, thanks

@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch from 43ce283 to 2f0b7c7 Compare November 19, 2025 08:52
add lptmr1 node into imx95 m7 dts which locate on Always On
domain and can continue work when whole soc enter system sleep mode

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
remove lptmr2 and enable lptmr1 interface as default

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
Enable lptmr interface to support counter_basic_api test case

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
Use lptmr interface as system clock if enabled PM
as system tick can't continue work in system suspend mode

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
When tickless is enabled, it is necessary to ensure
that lptmr can be set for a period of time instead of
generating an interrupt at one tick

Set lptmr to the free running mode to ensure that
the current cycles value of lptmr can be directly obtained in ISR

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
Add safety window mechanism to prevent race conditions when updating
LPTMR compare register (CMR).

Problem:
- Setting CMR too close to current hardware counter can cause missed
  interrupts and system hangs

Solution:
- Implement lptmr_set_safe_immediate() with configurable safety window

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
Tested imx95 m7 lptmr as system tick in timer_behavior test case

Signed-off-by: Yongxu Wang <yongxu.wang@nxp.com>
@yongxu-wang15 yongxu-wang15 force-pushed the NXP-support-lptmr-tickless-function branch from 2f0b7c7 to 12f0265 Compare November 19, 2025 09:14
@yongxu-wang15
Copy link
Contributor Author

The design of kernel timers is time never stops (free running counter) that you may set a compare to catch sometime in the future.

Stopping/Starting the underlying timer will surely introduce timing drift and jitter, something that tends to surprise and upset people when they see it. My opinion is that if your hardware needs to stop and start to set the compare, this hardware isn't a good source for kernel timers without at least some build warnings informing people building with it that yes, this is what they really want.

I'm not going to stop a vendor from supplying a driver that introduces drift/jitter. But I do feel like I can say we should warn about it.

Unless you can perhaps show in the PR the external tool test from https://github.com/zephyrproject-rtos/zephyr/tree/main/tests/kernel/timer/timer_behavior with correct timer behavior with this driver with the start/stop behavior? Maybe its ok, I don't know!

thanks your comments, have droped stop/start flow now, just added one customer safe window now, its 10 as default, and it can be set into different value to adapter socs. And added LOG_WRN in driver init, if you want to enlarge the safe window value, it will may introduce some timing drift and jitter.
And used lptmr 10 safe window to test as system tick, it seem passed in zephyr/tests/kernel/timer/timer_behavior, there is log:------ TESTSUITE SUMMARY START ------

SUITE PASS - 100.00% [timer_jitter_drift]: pass = 2, fail = 0, skip = 0, total = 2 duration = 39.339 seconds

  • PASS - [timer_jitter_drift.test_jitter_drift_timer_period] duration = 19.669 seconds
  • PASS - [timer_jitter_drift.test_jitter_drift_timer_startdelay] duration = 19.670 seconds

SUITE PASS - 100.00% [timer_tick_train]: pass = 1, fail = 0, skip = 0, total = 1 duration = 10.362 seconds

  • PASS - [timer_tick_train.test_one_tick_timer_train] duration = 10.362 seconds

------ TESTSUITE SUMMARY END ------

===================================================================
PROJECT EXECUTION SUCCESSFUL

thanks a lot for your time

@sonarqubecloud
Copy link

If enabled, the driver will use SoC-specific default values.

config MCUX_LPTMR_TIMER_SAFETY_WINDOW_CYCLES
int "MCUX LPTMR timer safety window cycles"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can user choose a proper value, is there a guidance?


#define MAX_TICKS ((COUNTER_MAX / CYCLES_PER_TICK) - 1)
#define MAX_CYCLES (MAX_TICKS * CYCLES_PER_TICK)
#define MIN_DELAY 1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this value decided?

#define CYCLES_PER_TICK ((uint32_t)((uint64_t)sys_clock_hw_cycles_per_sec() \
/ (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC))

#define COUNTER_MAX 0xffffffff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is LPTMR which uses 16-bit counter. such as kw45.

next += CYCLES_PER_TICK;
}

next += announced_cycles;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this next overflow? which means in function lptmr_set_safe_immediate, target_cycles < hw_counter

default y

config SYS_CLOCK_TICKS_PER_SEC
default 1024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be

default 1024 if MCUX_LPTMR_TIMER
default 1000 if CORTEX_M_SYSTICK

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, I guess this should be outside of if PM ... endif

@@ -0,0 +1,3 @@
CONFIG_MCUX_LPTMR_TIMER=y
CONFIG_TICKLESS_KERNEL=y
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be removed if done in soc/nxp/imx/imx9/imx95/Kconfig.defconfig.mimx95.m7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants