timer-imx-sysctr.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "timer-of.h"
  7. #define CMP_OFFSET 0x10000
  8. #define CNTCV_LO 0x8
  9. #define CNTCV_HI 0xc
  10. #define CMPCV_LO (CMP_OFFSET + 0x20)
  11. #define CMPCV_HI (CMP_OFFSET + 0x24)
  12. #define CMPCR (CMP_OFFSET + 0x2c)
  13. #define SYS_CTR_EN 0x1
  14. #define SYS_CTR_IRQ_MASK 0x2
  15. #define SYS_CTR_CLK_DIV 0x3
  16. static void __iomem *sys_ctr_base __ro_after_init;
  17. static u32 cmpcr __ro_after_init;
  18. static void sysctr_timer_enable(bool enable)
  19. {
  20. writel(enable ? cmpcr | SYS_CTR_EN : cmpcr, sys_ctr_base + CMPCR);
  21. }
  22. static void sysctr_irq_acknowledge(void)
  23. {
  24. /*
  25. * clear the enable bit(EN =0) will clear
  26. * the status bit(ISTAT = 0), then the interrupt
  27. * signal will be negated(acknowledged).
  28. */
  29. sysctr_timer_enable(false);
  30. }
  31. static inline u64 sysctr_read_counter(void)
  32. {
  33. u32 cnt_hi, tmp_hi, cnt_lo;
  34. do {
  35. cnt_hi = readl_relaxed(sys_ctr_base + CNTCV_HI);
  36. cnt_lo = readl_relaxed(sys_ctr_base + CNTCV_LO);
  37. tmp_hi = readl_relaxed(sys_ctr_base + CNTCV_HI);
  38. } while (tmp_hi != cnt_hi);
  39. return ((u64) cnt_hi << 32) | cnt_lo;
  40. }
  41. static int sysctr_set_next_event(unsigned long delta,
  42. struct clock_event_device *evt)
  43. {
  44. u32 cmp_hi, cmp_lo;
  45. u64 next;
  46. sysctr_timer_enable(false);
  47. next = sysctr_read_counter();
  48. next += delta;
  49. cmp_hi = (next >> 32) & 0x00fffff;
  50. cmp_lo = next & 0xffffffff;
  51. writel_relaxed(cmp_hi, sys_ctr_base + CMPCV_HI);
  52. writel_relaxed(cmp_lo, sys_ctr_base + CMPCV_LO);
  53. sysctr_timer_enable(true);
  54. return 0;
  55. }
  56. static int sysctr_set_state_oneshot(struct clock_event_device *evt)
  57. {
  58. return 0;
  59. }
  60. static int sysctr_set_state_shutdown(struct clock_event_device *evt)
  61. {
  62. sysctr_timer_enable(false);
  63. return 0;
  64. }
  65. static irqreturn_t sysctr_timer_interrupt(int irq, void *dev_id)
  66. {
  67. struct clock_event_device *evt = dev_id;
  68. sysctr_irq_acknowledge();
  69. evt->event_handler(evt);
  70. return IRQ_HANDLED;
  71. }
  72. static struct timer_of to_sysctr = {
  73. .flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE,
  74. .clkevt = {
  75. .name = "i.MX system counter timer",
  76. .features = CLOCK_EVT_FEAT_ONESHOT |
  77. CLOCK_EVT_FEAT_DYNIRQ,
  78. .set_state_oneshot = sysctr_set_state_oneshot,
  79. .set_next_event = sysctr_set_next_event,
  80. .set_state_shutdown = sysctr_set_state_shutdown,
  81. .rating = 200,
  82. },
  83. .of_irq = {
  84. .handler = sysctr_timer_interrupt,
  85. .flags = IRQF_TIMER,
  86. },
  87. .of_clk = {
  88. .name = "per",
  89. },
  90. };
  91. static void __init sysctr_clockevent_init(void)
  92. {
  93. to_sysctr.clkevt.cpumask = cpu_possible_mask;
  94. clockevents_config_and_register(&to_sysctr.clkevt,
  95. timer_of_rate(&to_sysctr),
  96. 0xff, 0x7fffffff);
  97. }
  98. static int __init sysctr_timer_init(struct device_node *np)
  99. {
  100. int ret = 0;
  101. ret = timer_of_init(np, &to_sysctr);
  102. if (ret)
  103. return ret;
  104. if (!of_property_read_bool(np, "nxp,no-divider")) {
  105. /* system counter clock is divided by 3 internally */
  106. to_sysctr.of_clk.rate /= SYS_CTR_CLK_DIV;
  107. }
  108. sys_ctr_base = timer_of_base(&to_sysctr);
  109. cmpcr = readl(sys_ctr_base + CMPCR);
  110. cmpcr &= ~SYS_CTR_EN;
  111. sysctr_clockevent_init();
  112. return 0;
  113. }
  114. TIMER_OF_DECLARE(sysctr_timer, "nxp,sysctr-timer", sysctr_timer_init);