timer-rda.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * RDA8810PL SoC timer driver
  4. *
  5. * Copyright RDA Microelectronics Company Limited
  6. * Copyright (c) 2017 Andreas Färber
  7. * Copyright (c) 2018 Manivannan Sadhasivam
  8. *
  9. * RDA8810PL has two independent timers: OSTIMER (56 bit) and HWTIMER (64 bit).
  10. * Each timer provides optional interrupt support. In this driver, OSTIMER is
  11. * used for clockevents and HWTIMER is used for clocksource.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include "timer-of.h"
  16. #define RDA_OSTIMER_LOADVAL_L 0x000
  17. #define RDA_OSTIMER_CTRL 0x004
  18. #define RDA_HWTIMER_LOCKVAL_L 0x024
  19. #define RDA_HWTIMER_LOCKVAL_H 0x028
  20. #define RDA_TIMER_IRQ_MASK_SET 0x02c
  21. #define RDA_TIMER_IRQ_MASK_CLR 0x030
  22. #define RDA_TIMER_IRQ_CLR 0x034
  23. #define RDA_OSTIMER_CTRL_ENABLE BIT(24)
  24. #define RDA_OSTIMER_CTRL_REPEAT BIT(28)
  25. #define RDA_OSTIMER_CTRL_LOAD BIT(30)
  26. #define RDA_TIMER_IRQ_MASK_OSTIMER BIT(0)
  27. #define RDA_TIMER_IRQ_CLR_OSTIMER BIT(0)
  28. static int rda_ostimer_start(void __iomem *base, bool periodic, u64 cycles)
  29. {
  30. u32 ctrl, load_l;
  31. load_l = (u32)cycles;
  32. ctrl = ((cycles >> 32) & 0xffffff);
  33. ctrl |= RDA_OSTIMER_CTRL_LOAD | RDA_OSTIMER_CTRL_ENABLE;
  34. if (periodic)
  35. ctrl |= RDA_OSTIMER_CTRL_REPEAT;
  36. /* Enable ostimer interrupt first */
  37. writel_relaxed(RDA_TIMER_IRQ_MASK_OSTIMER,
  38. base + RDA_TIMER_IRQ_MASK_SET);
  39. /* Write low 32 bits first, high 24 bits are with ctrl */
  40. writel_relaxed(load_l, base + RDA_OSTIMER_LOADVAL_L);
  41. writel_relaxed(ctrl, base + RDA_OSTIMER_CTRL);
  42. return 0;
  43. }
  44. static int rda_ostimer_stop(void __iomem *base)
  45. {
  46. /* Disable ostimer interrupt first */
  47. writel_relaxed(RDA_TIMER_IRQ_MASK_OSTIMER,
  48. base + RDA_TIMER_IRQ_MASK_CLR);
  49. writel_relaxed(0, base + RDA_OSTIMER_CTRL);
  50. return 0;
  51. }
  52. static int rda_ostimer_set_state_shutdown(struct clock_event_device *evt)
  53. {
  54. struct timer_of *to = to_timer_of(evt);
  55. rda_ostimer_stop(timer_of_base(to));
  56. return 0;
  57. }
  58. static int rda_ostimer_set_state_oneshot(struct clock_event_device *evt)
  59. {
  60. struct timer_of *to = to_timer_of(evt);
  61. rda_ostimer_stop(timer_of_base(to));
  62. return 0;
  63. }
  64. static int rda_ostimer_set_state_periodic(struct clock_event_device *evt)
  65. {
  66. struct timer_of *to = to_timer_of(evt);
  67. unsigned long cycles_per_jiffy;
  68. rda_ostimer_stop(timer_of_base(to));
  69. cycles_per_jiffy = ((unsigned long long)NSEC_PER_SEC / HZ *
  70. evt->mult) >> evt->shift;
  71. rda_ostimer_start(timer_of_base(to), true, cycles_per_jiffy);
  72. return 0;
  73. }
  74. static int rda_ostimer_tick_resume(struct clock_event_device *evt)
  75. {
  76. return 0;
  77. }
  78. static int rda_ostimer_set_next_event(unsigned long evt,
  79. struct clock_event_device *ev)
  80. {
  81. struct timer_of *to = to_timer_of(ev);
  82. rda_ostimer_start(timer_of_base(to), false, evt);
  83. return 0;
  84. }
  85. static irqreturn_t rda_ostimer_interrupt(int irq, void *dev_id)
  86. {
  87. struct clock_event_device *evt = dev_id;
  88. struct timer_of *to = to_timer_of(evt);
  89. /* clear timer int */
  90. writel_relaxed(RDA_TIMER_IRQ_CLR_OSTIMER,
  91. timer_of_base(to) + RDA_TIMER_IRQ_CLR);
  92. if (evt->event_handler)
  93. evt->event_handler(evt);
  94. return IRQ_HANDLED;
  95. }
  96. static struct timer_of rda_ostimer_of = {
  97. .flags = TIMER_OF_IRQ | TIMER_OF_BASE,
  98. .clkevt = {
  99. .name = "rda-ostimer",
  100. .rating = 250,
  101. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  102. CLOCK_EVT_FEAT_DYNIRQ,
  103. .set_state_shutdown = rda_ostimer_set_state_shutdown,
  104. .set_state_oneshot = rda_ostimer_set_state_oneshot,
  105. .set_state_periodic = rda_ostimer_set_state_periodic,
  106. .tick_resume = rda_ostimer_tick_resume,
  107. .set_next_event = rda_ostimer_set_next_event,
  108. },
  109. .of_base = {
  110. .name = "rda-timer",
  111. .index = 0,
  112. },
  113. .of_irq = {
  114. .name = "ostimer",
  115. .handler = rda_ostimer_interrupt,
  116. .flags = IRQF_TIMER,
  117. },
  118. };
  119. static u64 rda_hwtimer_read(struct clocksource *cs)
  120. {
  121. void __iomem *base = timer_of_base(&rda_ostimer_of);
  122. u32 lo, hi;
  123. /* Always read low 32 bits first */
  124. do {
  125. lo = readl_relaxed(base + RDA_HWTIMER_LOCKVAL_L);
  126. hi = readl_relaxed(base + RDA_HWTIMER_LOCKVAL_H);
  127. } while (hi != readl_relaxed(base + RDA_HWTIMER_LOCKVAL_H));
  128. return ((u64)hi << 32) | lo;
  129. }
  130. static struct clocksource rda_hwtimer_clocksource = {
  131. .name = "rda-timer",
  132. .rating = 400,
  133. .read = rda_hwtimer_read,
  134. .mask = CLOCKSOURCE_MASK(64),
  135. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  136. };
  137. static int __init rda_timer_init(struct device_node *np)
  138. {
  139. unsigned long rate = 2000000;
  140. int ret;
  141. ret = timer_of_init(np, &rda_ostimer_of);
  142. if (ret)
  143. return ret;
  144. clocksource_register_hz(&rda_hwtimer_clocksource, rate);
  145. clockevents_config_and_register(&rda_ostimer_of.clkevt, rate,
  146. 0x2, UINT_MAX);
  147. return 0;
  148. }
  149. TIMER_OF_DECLARE(rda8810pl, "rda,8810pl-timer", rda_timer_init);