timer-imx-sysctr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright 2017-2019 NXP
  4. #include <linux/interrupt.h>
  5. #include <linux/clockchips.h>
  6. #include <linux/slab.h>
  7. #include "timer-of.h"
  8. #define CMP_OFFSET 0x10000
  9. #define RD_OFFSET 0x20000
  10. #define CNTCV_LO 0x8
  11. #define CNTCV_HI 0xc
  12. #define CMPCV_LO (CMP_OFFSET + 0x20)
  13. #define CMPCV_HI (CMP_OFFSET + 0x24)
  14. #define CMPCR (CMP_OFFSET + 0x2c)
  15. #define CNTCV_LO_IMX95 (RD_OFFSET + 0x8)
  16. #define CNTCV_HI_IMX95 (RD_OFFSET + 0xc)
  17. #define SYS_CTR_EN 0x1
  18. #define SYS_CTR_IRQ_MASK 0x2
  19. #define SYS_CTR_CLK_DIV 0x3
  20. struct sysctr_private {
  21. u32 cmpcr;
  22. u32 lo_off;
  23. u32 hi_off;
  24. };
  25. static void sysctr_timer_enable(struct clock_event_device *evt, bool enable)
  26. {
  27. struct timer_of *to = to_timer_of(evt);
  28. struct sysctr_private *priv = to->private_data;
  29. void __iomem *base = timer_of_base(to);
  30. writel(enable ? priv->cmpcr | SYS_CTR_EN : priv->cmpcr, base + CMPCR);
  31. }
  32. static void sysctr_irq_acknowledge(struct clock_event_device *evt)
  33. {
  34. /*
  35. * clear the enable bit(EN =0) will clear
  36. * the status bit(ISTAT = 0), then the interrupt
  37. * signal will be negated(acknowledged).
  38. */
  39. sysctr_timer_enable(evt, false);
  40. }
  41. static inline u64 sysctr_read_counter(struct clock_event_device *evt)
  42. {
  43. struct timer_of *to = to_timer_of(evt);
  44. struct sysctr_private *priv = to->private_data;
  45. void __iomem *base = timer_of_base(to);
  46. u32 cnt_hi, tmp_hi, cnt_lo;
  47. do {
  48. cnt_hi = readl_relaxed(base + priv->hi_off);
  49. cnt_lo = readl_relaxed(base + priv->lo_off);
  50. tmp_hi = readl_relaxed(base + priv->hi_off);
  51. } while (tmp_hi != cnt_hi);
  52. return ((u64) cnt_hi << 32) | cnt_lo;
  53. }
  54. static int sysctr_set_next_event(unsigned long delta,
  55. struct clock_event_device *evt)
  56. {
  57. struct timer_of *to = to_timer_of(evt);
  58. void __iomem *base = timer_of_base(to);
  59. u32 cmp_hi, cmp_lo;
  60. u64 next;
  61. sysctr_timer_enable(evt, false);
  62. next = sysctr_read_counter(evt);
  63. next += delta;
  64. cmp_hi = (next >> 32) & 0x00fffff;
  65. cmp_lo = next & 0xffffffff;
  66. writel_relaxed(cmp_hi, base + CMPCV_HI);
  67. writel_relaxed(cmp_lo, base + CMPCV_LO);
  68. sysctr_timer_enable(evt, true);
  69. return 0;
  70. }
  71. static int sysctr_set_state_oneshot(struct clock_event_device *evt)
  72. {
  73. return 0;
  74. }
  75. static int sysctr_set_state_shutdown(struct clock_event_device *evt)
  76. {
  77. sysctr_timer_enable(evt, false);
  78. return 0;
  79. }
  80. static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id)
  81. {
  82. struct clock_event_device *evt = dev_id;
  83. sysctr_irq_acknowledge(evt);
  84. evt->event_handler(evt);
  85. return IRQ_HANDLED;
  86. }
  87. static struct timer_of to_sysctr = {
  88. .flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE,
  89. .clkevt = {
  90. .name = "i.MX system counter timer",
  91. .features = CLOCK_EVT_FEAT_ONESHOT |
  92. CLOCK_EVT_FEAT_DYNIRQ,
  93. .set_state_oneshot = sysctr_set_state_oneshot,
  94. .set_next_event = sysctr_set_next_event,
  95. .set_state_shutdown = sysctr_set_state_shutdown,
  96. .rating = 200,
  97. },
  98. .of_irq = {
  99. .handler = sysctr_timer_interrupt,
  100. .flags = IRQF_TIMER,
  101. },
  102. .of_clk = {
  103. .name = "per",
  104. },
  105. };
  106. static int __init __sysctr_timer_init(struct device_node *np)
  107. {
  108. struct sysctr_private *priv;
  109. void __iomem *base;
  110. int ret;
  111. priv = kzalloc_obj(struct sysctr_private);
  112. if (!priv)
  113. return -ENOMEM;
  114. ret = timer_of_init(np, &to_sysctr);
  115. if (ret) {
  116. kfree(priv);
  117. return ret;
  118. }
  119. if (!of_property_read_bool(np, "nxp,no-divider")) {
  120. /* system counter clock is divided by 3 internally */
  121. to_sysctr.of_clk.rate /= SYS_CTR_CLK_DIV;
  122. }
  123. to_sysctr.clkevt.cpumask = cpu_possible_mask;
  124. to_sysctr.private_data = priv;
  125. base = timer_of_base(&to_sysctr);
  126. priv->cmpcr = readl(base + CMPCR) & ~SYS_CTR_EN;
  127. return 0;
  128. }
  129. static int __init sysctr_timer_init(struct device_node *np)
  130. {
  131. struct sysctr_private *priv;
  132. int ret;
  133. ret = __sysctr_timer_init(np);
  134. if (ret)
  135. return ret;
  136. priv = to_sysctr.private_data;
  137. priv->lo_off = CNTCV_LO;
  138. priv->hi_off = CNTCV_HI;
  139. clockevents_config_and_register(&to_sysctr.clkevt,
  140. timer_of_rate(&to_sysctr),
  141. 0xff, 0x7fffffff);
  142. return 0;
  143. }
  144. static int __init sysctr_timer_imx95_init(struct device_node *np)
  145. {
  146. struct sysctr_private *priv;
  147. int ret;
  148. ret = __sysctr_timer_init(np);
  149. if (ret)
  150. return ret;
  151. priv = to_sysctr.private_data;
  152. priv->lo_off = CNTCV_LO_IMX95;
  153. priv->hi_off = CNTCV_HI_IMX95;
  154. clockevents_config_and_register(&to_sysctr.clkevt,
  155. timer_of_rate(&to_sysctr),
  156. 0xff, 0x7fffffff);
  157. return 0;
  158. }
  159. TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer", sysctr_timer_init);
  160. TIMER_OF_DECLARE(sysctr_timer_imx95, "nxp,imx95-sysctr-timer", sysctr_timer_imx95_init);