timer-rtl-otto.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #include <linux/clk.h>
  3. #include <linux/clockchips.h>
  4. #include <linux/cpu.h>
  5. #include <linux/cpuhotplug.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/sched_clock.h>
  8. #include "timer-of.h"
  9. #define RTTM_DATA 0x0
  10. #define RTTM_CNT 0x4
  11. #define RTTM_CTRL 0x8
  12. #define RTTM_INT 0xc
  13. #define RTTM_CTRL_ENABLE BIT(28)
  14. #define RTTM_INT_PENDING BIT(16)
  15. #define RTTM_INT_ENABLE BIT(20)
  16. /*
  17. * The Otto platform provides multiple 28 bit timers/counters with the following
  18. * operating logic. If enabled the timer counts up. Per timer one can set a
  19. * maximum counter value as an end marker. If end marker is reached the timer
  20. * fires an interrupt. If the timer "overflows" by reaching the end marker or
  21. * by adding 1 to 0x0fffffff the counter is reset to 0. When this happens and
  22. * the timer is in operating mode COUNTER it stops. In mode TIMER it will
  23. * continue to count up.
  24. */
  25. #define RTTM_CTRL_COUNTER 0
  26. #define RTTM_CTRL_TIMER BIT(24)
  27. #define RTTM_BIT_COUNT 28
  28. #define RTTM_MIN_DELTA 8
  29. #define RTTM_MAX_DELTA CLOCKSOURCE_MASK(28)
  30. /*
  31. * Timers are derived from the LXB clock frequency. Usually this is a fixed
  32. * multiple of the 25 MHz oscillator. The 930X SOC is an exception from that.
  33. * Its LXB clock has only dividers and uses the switch PLL of 2.45 GHz as its
  34. * base. The only meaningful frequencies we can achieve from that are 175.000
  35. * MHz and 153.125 MHz. The greatest common divisor of all explained possible
  36. * speeds is 3125000. Pin the timers to this 3.125 MHz reference frequency.
  37. */
  38. #define RTTM_TICKS_PER_SEC 3125000
  39. struct rttm_cs {
  40. struct timer_of to;
  41. struct clocksource cs;
  42. };
  43. /* Simple internal register functions */
  44. static inline void rttm_set_counter(void __iomem *base, unsigned int counter)
  45. {
  46. iowrite32(counter, base + RTTM_CNT);
  47. }
  48. static inline unsigned int rttm_get_counter(void __iomem *base)
  49. {
  50. return ioread32(base + RTTM_CNT);
  51. }
  52. static inline void rttm_set_period(void __iomem *base, unsigned int period)
  53. {
  54. iowrite32(period, base + RTTM_DATA);
  55. }
  56. static inline void rttm_disable_timer(void __iomem *base)
  57. {
  58. iowrite32(0, base + RTTM_CTRL);
  59. }
  60. static inline void rttm_enable_timer(void __iomem *base, u32 mode, u32 divisor)
  61. {
  62. iowrite32(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
  63. }
  64. static inline void rttm_ack_irq(void __iomem *base)
  65. {
  66. iowrite32(ioread32(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
  67. }
  68. static inline void rttm_enable_irq(void __iomem *base)
  69. {
  70. iowrite32(RTTM_INT_ENABLE, base + RTTM_INT);
  71. }
  72. static inline void rttm_disable_irq(void __iomem *base)
  73. {
  74. iowrite32(0, base + RTTM_INT);
  75. }
  76. /* Aggregated control functions for kernel clock framework */
  77. #define RTTM_DEBUG(base) \
  78. pr_debug("------------- %s %d %08x\n", __func__, \
  79. smp_processor_id(), (u32)base)
  80. static irqreturn_t rttm_timer_interrupt(int irq, void *dev_id)
  81. {
  82. struct clock_event_device *clkevt = dev_id;
  83. struct timer_of *to = to_timer_of(clkevt);
  84. rttm_ack_irq(to->of_base.base);
  85. RTTM_DEBUG(to->of_base.base);
  86. clkevt->event_handler(clkevt);
  87. return IRQ_HANDLED;
  88. }
  89. static void rttm_stop_timer(void __iomem *base)
  90. {
  91. rttm_disable_timer(base);
  92. rttm_ack_irq(base);
  93. }
  94. static void rttm_start_timer(struct timer_of *to, u32 mode)
  95. {
  96. rttm_set_counter(to->of_base.base, 0);
  97. rttm_enable_timer(to->of_base.base, mode, to->of_clk.rate / RTTM_TICKS_PER_SEC);
  98. }
  99. static int rttm_next_event(unsigned long delta, struct clock_event_device *clkevt)
  100. {
  101. struct timer_of *to = to_timer_of(clkevt);
  102. RTTM_DEBUG(to->of_base.base);
  103. rttm_stop_timer(to->of_base.base);
  104. rttm_set_period(to->of_base.base, delta);
  105. rttm_start_timer(to, RTTM_CTRL_COUNTER);
  106. return 0;
  107. }
  108. static int rttm_state_oneshot(struct clock_event_device *clkevt)
  109. {
  110. struct timer_of *to = to_timer_of(clkevt);
  111. RTTM_DEBUG(to->of_base.base);
  112. rttm_stop_timer(to->of_base.base);
  113. rttm_set_period(to->of_base.base, RTTM_TICKS_PER_SEC / HZ);
  114. rttm_start_timer(to, RTTM_CTRL_COUNTER);
  115. return 0;
  116. }
  117. static int rttm_state_periodic(struct clock_event_device *clkevt)
  118. {
  119. struct timer_of *to = to_timer_of(clkevt);
  120. RTTM_DEBUG(to->of_base.base);
  121. rttm_stop_timer(to->of_base.base);
  122. rttm_set_period(to->of_base.base, RTTM_TICKS_PER_SEC / HZ);
  123. rttm_start_timer(to, RTTM_CTRL_TIMER);
  124. return 0;
  125. }
  126. static int rttm_state_shutdown(struct clock_event_device *clkevt)
  127. {
  128. struct timer_of *to = to_timer_of(clkevt);
  129. RTTM_DEBUG(to->of_base.base);
  130. rttm_stop_timer(to->of_base.base);
  131. return 0;
  132. }
  133. static void rttm_setup_timer(void __iomem *base)
  134. {
  135. RTTM_DEBUG(base);
  136. rttm_stop_timer(base);
  137. rttm_set_period(base, 0);
  138. }
  139. static u64 rttm_read_clocksource(struct clocksource *cs)
  140. {
  141. struct rttm_cs *rcs = container_of(cs, struct rttm_cs, cs);
  142. return (u64)rttm_get_counter(rcs->to.of_base.base);
  143. }
  144. /* Module initialization part. */
  145. static DEFINE_PER_CPU(struct timer_of, rttm_to) = {
  146. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK | TIMER_OF_IRQ,
  147. .of_irq = {
  148. .flags = IRQF_PERCPU | IRQF_TIMER,
  149. .handler = rttm_timer_interrupt,
  150. },
  151. .clkevt = {
  152. .rating = 400,
  153. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  154. .set_state_periodic = rttm_state_periodic,
  155. .set_state_shutdown = rttm_state_shutdown,
  156. .set_state_oneshot = rttm_state_oneshot,
  157. .set_next_event = rttm_next_event
  158. },
  159. };
  160. static int rttm_enable_clocksource(struct clocksource *cs)
  161. {
  162. struct rttm_cs *rcs = container_of(cs, struct rttm_cs, cs);
  163. rttm_disable_irq(rcs->to.of_base.base);
  164. rttm_setup_timer(rcs->to.of_base.base);
  165. rttm_enable_timer(rcs->to.of_base.base, RTTM_CTRL_TIMER,
  166. rcs->to.of_clk.rate / RTTM_TICKS_PER_SEC);
  167. return 0;
  168. }
  169. struct rttm_cs rttm_cs = {
  170. .to = {
  171. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
  172. },
  173. .cs = {
  174. .name = "realtek_otto_timer",
  175. .rating = 400,
  176. .mask = CLOCKSOURCE_MASK(RTTM_BIT_COUNT),
  177. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  178. .read = rttm_read_clocksource,
  179. }
  180. };
  181. static u64 notrace rttm_read_clock(void)
  182. {
  183. return (u64)rttm_get_counter(rttm_cs.to.of_base.base);
  184. }
  185. static int rttm_cpu_starting(unsigned int cpu)
  186. {
  187. struct timer_of *to = per_cpu_ptr(&rttm_to, cpu);
  188. RTTM_DEBUG(to->of_base.base);
  189. to->clkevt.cpumask = cpumask_of(cpu);
  190. irq_force_affinity(to->of_irq.irq, to->clkevt.cpumask);
  191. clockevents_config_and_register(&to->clkevt, RTTM_TICKS_PER_SEC,
  192. RTTM_MIN_DELTA, RTTM_MAX_DELTA);
  193. rttm_enable_irq(to->of_base.base);
  194. return 0;
  195. }
  196. static int __init rttm_probe(struct device_node *np)
  197. {
  198. int cpu, cpu_rollback;
  199. struct timer_of *to;
  200. int clkidx = num_possible_cpus();
  201. /* Use the first n timers as per CPU clock event generators */
  202. for_each_possible_cpu(cpu) {
  203. to = per_cpu_ptr(&rttm_to, cpu);
  204. to->of_irq.index = to->of_base.index = cpu;
  205. if (timer_of_init(np, to)) {
  206. pr_err("%s: setup of timer %d failed\n", __func__, cpu);
  207. goto rollback;
  208. }
  209. rttm_setup_timer(to->of_base.base);
  210. }
  211. /* Activate the n'th + 1 timer as a stable CPU clocksource. */
  212. to = &rttm_cs.to;
  213. to->of_base.index = clkidx;
  214. timer_of_init(np, to);
  215. if (rttm_cs.to.of_base.base && rttm_cs.to.of_clk.rate) {
  216. rttm_enable_clocksource(&rttm_cs.cs);
  217. clocksource_register_hz(&rttm_cs.cs, RTTM_TICKS_PER_SEC);
  218. sched_clock_register(rttm_read_clock, RTTM_BIT_COUNT, RTTM_TICKS_PER_SEC);
  219. } else
  220. pr_err("%s: setup of timer %d as clocksoure failed", __func__, clkidx);
  221. return cpuhp_setup_state(CPUHP_AP_REALTEK_TIMER_STARTING,
  222. "timer/realtek:online",
  223. rttm_cpu_starting, NULL);
  224. rollback:
  225. pr_err("%s: timer registration failed\n", __func__);
  226. for_each_possible_cpu(cpu_rollback) {
  227. if (cpu_rollback == cpu)
  228. break;
  229. to = per_cpu_ptr(&rttm_to, cpu_rollback);
  230. timer_of_cleanup(to);
  231. }
  232. return -EINVAL;
  233. }
  234. TIMER_OF_DECLARE(otto_timer, "realtek,otto-timer", rttm_probe);