timer-rda.c 4.8 KB

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