timer-nxp-stm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2016 Freescale Semiconductor, Inc.
  4. * Copyright 2018,2021-2025 NXP
  5. *
  6. * NXP System Timer Module:
  7. *
  8. * STM supports commonly required system and application software
  9. * timing functions. STM includes a 32-bit count-up timer and four
  10. * 32-bit compare channels with a separate interrupt source for each
  11. * channel. The timer is driven by the STM module clock divided by an
  12. * 8-bit prescale value (1 to 256). It has ability to stop the timer
  13. * in Debug mode
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/cpuhotplug.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/sched_clock.h>
  23. #include <linux/units.h>
  24. #define STM_CR(__base) (__base)
  25. #define STM_CR_TEN BIT(0)
  26. #define STM_CR_FRZ BIT(1)
  27. #define STM_CR_CPS_OFFSET 8u
  28. #define STM_CR_CPS_MASK GENMASK(15, STM_CR_CPS_OFFSET)
  29. #define STM_CNT(__base) ((__base) + 0x04)
  30. #define STM_CCR0(__base) ((__base) + 0x10)
  31. #define STM_CCR1(__base) ((__base) + 0x20)
  32. #define STM_CCR2(__base) ((__base) + 0x30)
  33. #define STM_CCR3(__base) ((__base) + 0x40)
  34. #define STM_CCR_CEN BIT(0)
  35. #define STM_CIR0(__base) ((__base) + 0x14)
  36. #define STM_CIR1(__base) ((__base) + 0x24)
  37. #define STM_CIR2(__base) ((__base) + 0x34)
  38. #define STM_CIR3(__base) ((__base) + 0x44)
  39. #define STM_CIR_CIF BIT(0)
  40. #define STM_CMP0(__base) ((__base) + 0x18)
  41. #define STM_CMP1(__base) ((__base) + 0x28)
  42. #define STM_CMP2(__base) ((__base) + 0x38)
  43. #define STM_CMP3(__base) ((__base) + 0x48)
  44. #define STM_ENABLE_MASK (STM_CR_FRZ | STM_CR_TEN)
  45. struct stm_timer {
  46. void __iomem *base;
  47. unsigned long rate;
  48. unsigned long delta;
  49. unsigned long counter;
  50. struct clock_event_device ced;
  51. struct clocksource cs;
  52. atomic_t refcnt;
  53. };
  54. static DEFINE_PER_CPU(struct stm_timer *, stm_timers);
  55. static struct stm_timer *stm_sched_clock;
  56. /*
  57. * Global structure for multiple STMs initialization
  58. */
  59. static int stm_instances;
  60. /*
  61. * This global lock is used to prevent race conditions with the
  62. * stm_instances in case the driver is using the ASYNC option
  63. */
  64. static DEFINE_MUTEX(stm_instances_lock);
  65. DEFINE_GUARD(stm_instances, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
  66. static struct stm_timer *cs_to_stm(struct clocksource *cs)
  67. {
  68. return container_of(cs, struct stm_timer, cs);
  69. }
  70. static struct stm_timer *ced_to_stm(struct clock_event_device *ced)
  71. {
  72. return container_of(ced, struct stm_timer, ced);
  73. }
  74. static u64 notrace nxp_stm_read_sched_clock(void)
  75. {
  76. return readl(STM_CNT(stm_sched_clock->base));
  77. }
  78. static u32 nxp_stm_clocksource_getcnt(struct stm_timer *stm_timer)
  79. {
  80. return readl(STM_CNT(stm_timer->base));
  81. }
  82. static void nxp_stm_clocksource_setcnt(struct stm_timer *stm_timer, u32 cnt)
  83. {
  84. writel(cnt, STM_CNT(stm_timer->base));
  85. }
  86. static u64 nxp_stm_clocksource_read(struct clocksource *cs)
  87. {
  88. struct stm_timer *stm_timer = cs_to_stm(cs);
  89. return (u64)nxp_stm_clocksource_getcnt(stm_timer);
  90. }
  91. static void nxp_stm_module_enable(struct stm_timer *stm_timer)
  92. {
  93. u32 reg;
  94. reg = readl(STM_CR(stm_timer->base));
  95. reg |= STM_ENABLE_MASK;
  96. writel(reg, STM_CR(stm_timer->base));
  97. }
  98. static void nxp_stm_module_disable(struct stm_timer *stm_timer)
  99. {
  100. u32 reg;
  101. reg = readl(STM_CR(stm_timer->base));
  102. reg &= ~STM_ENABLE_MASK;
  103. writel(reg, STM_CR(stm_timer->base));
  104. }
  105. static void nxp_stm_module_put(struct stm_timer *stm_timer)
  106. {
  107. if (atomic_dec_and_test(&stm_timer->refcnt))
  108. nxp_stm_module_disable(stm_timer);
  109. }
  110. static void nxp_stm_module_get(struct stm_timer *stm_timer)
  111. {
  112. if (atomic_inc_return(&stm_timer->refcnt) == 1)
  113. nxp_stm_module_enable(stm_timer);
  114. }
  115. static int nxp_stm_clocksource_enable(struct clocksource *cs)
  116. {
  117. struct stm_timer *stm_timer = cs_to_stm(cs);
  118. nxp_stm_module_get(stm_timer);
  119. return 0;
  120. }
  121. static void nxp_stm_clocksource_disable(struct clocksource *cs)
  122. {
  123. struct stm_timer *stm_timer = cs_to_stm(cs);
  124. nxp_stm_module_put(stm_timer);
  125. }
  126. static void nxp_stm_clocksource_suspend(struct clocksource *cs)
  127. {
  128. struct stm_timer *stm_timer = cs_to_stm(cs);
  129. nxp_stm_clocksource_disable(cs);
  130. stm_timer->counter = nxp_stm_clocksource_getcnt(stm_timer);
  131. }
  132. static void nxp_stm_clocksource_resume(struct clocksource *cs)
  133. {
  134. struct stm_timer *stm_timer = cs_to_stm(cs);
  135. nxp_stm_clocksource_setcnt(stm_timer, stm_timer->counter);
  136. nxp_stm_clocksource_enable(cs);
  137. }
  138. static void devm_clocksource_unregister(void *data)
  139. {
  140. struct stm_timer *stm_timer = data;
  141. clocksource_unregister(&stm_timer->cs);
  142. }
  143. static int nxp_stm_clocksource_init(struct device *dev, struct stm_timer *stm_timer,
  144. const char *name, void __iomem *base, struct clk *clk)
  145. {
  146. int ret;
  147. stm_timer->base = base;
  148. stm_timer->rate = clk_get_rate(clk);
  149. stm_timer->cs.name = name;
  150. stm_timer->cs.rating = 460;
  151. stm_timer->cs.read = nxp_stm_clocksource_read;
  152. stm_timer->cs.enable = nxp_stm_clocksource_enable;
  153. stm_timer->cs.disable = nxp_stm_clocksource_disable;
  154. stm_timer->cs.suspend = nxp_stm_clocksource_suspend;
  155. stm_timer->cs.resume = nxp_stm_clocksource_resume;
  156. stm_timer->cs.mask = CLOCKSOURCE_MASK(32);
  157. stm_timer->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  158. stm_timer->cs.owner = THIS_MODULE;
  159. ret = clocksource_register_hz(&stm_timer->cs, stm_timer->rate);
  160. if (ret)
  161. return ret;
  162. ret = devm_add_action_or_reset(dev, devm_clocksource_unregister, stm_timer);
  163. if (ret)
  164. return ret;
  165. stm_sched_clock = stm_timer;
  166. sched_clock_register(nxp_stm_read_sched_clock, 32, stm_timer->rate);
  167. dev_dbg(dev, "Registered clocksource %s\n", name);
  168. return 0;
  169. }
  170. static int nxp_stm_clockevent_read_counter(struct stm_timer *stm_timer)
  171. {
  172. return readl(STM_CNT(stm_timer->base));
  173. }
  174. static void nxp_stm_clockevent_disable(struct stm_timer *stm_timer)
  175. {
  176. writel(0, STM_CCR0(stm_timer->base));
  177. }
  178. static void nxp_stm_clockevent_enable(struct stm_timer *stm_timer)
  179. {
  180. writel(STM_CCR_CEN, STM_CCR0(stm_timer->base));
  181. }
  182. static int nxp_stm_clockevent_shutdown(struct clock_event_device *ced)
  183. {
  184. struct stm_timer *stm_timer = ced_to_stm(ced);
  185. nxp_stm_clockevent_disable(stm_timer);
  186. return 0;
  187. }
  188. static int nxp_stm_clockevent_set_next_event(unsigned long delta, struct clock_event_device *ced)
  189. {
  190. struct stm_timer *stm_timer = ced_to_stm(ced);
  191. u32 val;
  192. nxp_stm_clockevent_disable(stm_timer);
  193. stm_timer->delta = delta;
  194. val = nxp_stm_clockevent_read_counter(stm_timer) + delta;
  195. writel(val, STM_CMP0(stm_timer->base));
  196. /*
  197. * The counter is shared across the channels and can not be
  198. * stopped while we are setting the next event. If the delta
  199. * is very small it is possible the counter increases above
  200. * the computed 'val'. The min_delta value specified when
  201. * registering the clockevent will prevent that. The second
  202. * case is if the counter wraps while we compute the 'val' and
  203. * before writing the comparator register. We read the counter,
  204. * check if we are back in time and abort the timer with -ETIME.
  205. */
  206. if (val > nxp_stm_clockevent_read_counter(stm_timer) + delta)
  207. return -ETIME;
  208. nxp_stm_clockevent_enable(stm_timer);
  209. return 0;
  210. }
  211. static int nxp_stm_clockevent_set_periodic(struct clock_event_device *ced)
  212. {
  213. struct stm_timer *stm_timer = ced_to_stm(ced);
  214. return nxp_stm_clockevent_set_next_event(stm_timer->rate, ced);
  215. }
  216. static void nxp_stm_clockevent_suspend(struct clock_event_device *ced)
  217. {
  218. struct stm_timer *stm_timer = ced_to_stm(ced);
  219. nxp_stm_module_put(stm_timer);
  220. }
  221. static void nxp_stm_clockevent_resume(struct clock_event_device *ced)
  222. {
  223. struct stm_timer *stm_timer = ced_to_stm(ced);
  224. nxp_stm_module_get(stm_timer);
  225. }
  226. static int nxp_stm_clockevent_per_cpu_init(struct device *dev, struct stm_timer *stm_timer,
  227. const char *name, void __iomem *base, int irq,
  228. struct clk *clk, int cpu)
  229. {
  230. stm_timer->base = base;
  231. stm_timer->rate = clk_get_rate(clk);
  232. stm_timer->ced.name = name;
  233. stm_timer->ced.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  234. stm_timer->ced.set_state_shutdown = nxp_stm_clockevent_shutdown;
  235. stm_timer->ced.set_state_periodic = nxp_stm_clockevent_set_periodic;
  236. stm_timer->ced.set_next_event = nxp_stm_clockevent_set_next_event;
  237. stm_timer->ced.suspend = nxp_stm_clockevent_suspend;
  238. stm_timer->ced.resume = nxp_stm_clockevent_resume;
  239. stm_timer->ced.cpumask = cpumask_of(cpu);
  240. stm_timer->ced.rating = 460;
  241. stm_timer->ced.irq = irq;
  242. stm_timer->ced.owner = THIS_MODULE;
  243. per_cpu(stm_timers, cpu) = stm_timer;
  244. nxp_stm_module_get(stm_timer);
  245. dev_dbg(dev, "Initialized per cpu clockevent name=%s, irq=%d, cpu=%d\n", name, irq, cpu);
  246. return 0;
  247. }
  248. static int nxp_stm_clockevent_starting_cpu(unsigned int cpu)
  249. {
  250. struct stm_timer *stm_timer = per_cpu(stm_timers, cpu);
  251. int ret;
  252. if (WARN_ON(!stm_timer))
  253. return -EFAULT;
  254. ret = irq_force_affinity(stm_timer->ced.irq, cpumask_of(cpu));
  255. if (ret)
  256. return ret;
  257. /*
  258. * The timings measurement show reading the counter register
  259. * and writing to the comparator register takes as a maximum
  260. * value 1100 ns at 133MHz rate frequency. The timer must be
  261. * set above this value and to be secure we set the minimum
  262. * value equal to 2000ns, so 2us.
  263. *
  264. * minimum ticks = (rate / MICRO) * 2
  265. */
  266. clockevents_config_and_register(&stm_timer->ced, stm_timer->rate,
  267. (stm_timer->rate / MICRO) * 2, ULONG_MAX);
  268. return 0;
  269. }
  270. static irqreturn_t nxp_stm_module_interrupt(int irq, void *dev_id)
  271. {
  272. struct stm_timer *stm_timer = dev_id;
  273. struct clock_event_device *ced = &stm_timer->ced;
  274. u32 val;
  275. /*
  276. * The interrupt is shared across the channels in the
  277. * module. But this one is configured to run only one channel,
  278. * consequently it is pointless to test the interrupt flags
  279. * before and we can directly reset the channel 0 irq flag
  280. * register.
  281. */
  282. writel(STM_CIR_CIF, STM_CIR0(stm_timer->base));
  283. /*
  284. * Update STM_CMP value using the counter value
  285. */
  286. val = nxp_stm_clockevent_read_counter(stm_timer) + stm_timer->delta;
  287. writel(val, STM_CMP0(stm_timer->base));
  288. /*
  289. * stm hardware doesn't support oneshot, it will generate an
  290. * interrupt and start the counter again so software needs to
  291. * disable the timer to stop the counter loop in ONESHOT mode.
  292. */
  293. if (likely(clockevent_state_oneshot(ced)))
  294. nxp_stm_clockevent_disable(stm_timer);
  295. ced->event_handler(ced);
  296. return IRQ_HANDLED;
  297. }
  298. static int nxp_stm_timer_probe(struct platform_device *pdev)
  299. {
  300. struct stm_timer *stm_timer;
  301. struct device *dev = &pdev->dev;
  302. struct device_node *np = dev->of_node;
  303. const char *name = of_node_full_name(np);
  304. struct clk *clk;
  305. void __iomem *base;
  306. int irq, ret;
  307. /*
  308. * The device tree can have multiple STM nodes described, so
  309. * it makes this driver a good candidate for the async probe.
  310. * It is still unclear if the time framework correctly handles
  311. * parallel loading of the timers but at least this driver is
  312. * ready to support the option.
  313. */
  314. guard(stm_instances)(&stm_instances_lock);
  315. /*
  316. * The S32Gx are SoCs featuring a diverse set of cores. Linux
  317. * is expected to run on Cortex-A53 cores, while other
  318. * software stacks will operate on Cortex-M cores. The number
  319. * of STM instances has been sized to include at most one
  320. * instance per core.
  321. *
  322. * As we need a clocksource and a clockevent per cpu, we
  323. * simply initialize a clocksource per cpu along with the
  324. * clockevent which makes the resulting code simpler.
  325. *
  326. * However if the device tree is describing more STM instances
  327. * than the number of cores, then we ignore them.
  328. */
  329. if (stm_instances >= num_possible_cpus())
  330. return 0;
  331. base = devm_of_iomap(dev, np, 0, NULL);
  332. if (IS_ERR(base))
  333. return dev_err_probe(dev, PTR_ERR(base), "Failed to iomap %pOFn\n", np);
  334. irq = platform_get_irq(pdev, 0);
  335. if (irq < 0)
  336. return dev_err_probe(dev, irq, "Failed to get IRQ\n");
  337. clk = devm_clk_get_enabled(dev, NULL);
  338. if (IS_ERR(clk))
  339. return dev_err_probe(dev, PTR_ERR(clk), "Clock not found\n");
  340. stm_timer = devm_kzalloc(dev, sizeof(*stm_timer), GFP_KERNEL);
  341. if (!stm_timer)
  342. return -ENOMEM;
  343. ret = devm_request_irq(dev, irq, nxp_stm_module_interrupt,
  344. IRQF_TIMER | IRQF_NOBALANCING, name, stm_timer);
  345. if (ret)
  346. return dev_err_probe(dev, ret, "Unable to allocate interrupt line\n");
  347. ret = nxp_stm_clocksource_init(dev, stm_timer, name, base, clk);
  348. if (ret)
  349. return ret;
  350. /*
  351. * Next probed STM will be a per CPU clockevent, until we
  352. * probe as many as we have CPUs available on the system, we
  353. * do a partial initialization
  354. */
  355. ret = nxp_stm_clockevent_per_cpu_init(dev, stm_timer, name,
  356. base, irq, clk,
  357. stm_instances);
  358. if (ret)
  359. return ret;
  360. stm_instances++;
  361. /*
  362. * The number of probed STMs for per CPU clockevent is
  363. * equal to the number of available CPUs on the
  364. * system. We install the cpu hotplug to finish the
  365. * initialization by registering the clockevents
  366. */
  367. if (stm_instances == num_possible_cpus()) {
  368. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "STM timer:starting",
  369. nxp_stm_clockevent_starting_cpu, NULL);
  370. if (ret < 0)
  371. return ret;
  372. }
  373. return 0;
  374. }
  375. static const struct of_device_id nxp_stm_of_match[] = {
  376. { .compatible = "nxp,s32g2-stm" },
  377. { }
  378. };
  379. MODULE_DEVICE_TABLE(of, nxp_stm_of_match);
  380. static struct platform_driver nxp_stm_driver = {
  381. .probe = nxp_stm_timer_probe,
  382. .driver = {
  383. .name = "nxp-stm",
  384. .of_match_table = nxp_stm_of_match,
  385. .suppress_bind_attrs = true,
  386. },
  387. };
  388. builtin_platform_driver(nxp_stm_driver);
  389. MODULE_DESCRIPTION("NXP System Timer Module driver");
  390. MODULE_LICENSE("GPL");