timer-rtl-otto.c 7.6 KB

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