timer-rtl-otto.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /*
  44. * Simple internal register functions
  45. */
  46. static inline void rttm_set_counter(void __iomem *base, unsigned int counter)
  47. {
  48. iowrite32(counter, base + RTTM_CNT);
  49. }
  50. static inline unsigned int rttm_get_counter(void __iomem *base)
  51. {
  52. return ioread32(base + RTTM_CNT);
  53. }
  54. static inline void rttm_set_period(void __iomem *base, unsigned int period)
  55. {
  56. iowrite32(period, base + RTTM_DATA);
  57. }
  58. static inline void rttm_disable_timer(void __iomem *base)
  59. {
  60. iowrite32(0, base + RTTM_CTRL);
  61. }
  62. static inline void rttm_enable_timer(void __iomem *base, u32 mode, u32 divisor)
  63. {
  64. iowrite32(RTTM_CTRL_ENABLE | mode | divisor, base + RTTM_CTRL);
  65. }
  66. static inline void rttm_ack_irq(void __iomem *base)
  67. {
  68. iowrite32(ioread32(base + RTTM_INT) | RTTM_INT_PENDING, base + RTTM_INT);
  69. }
  70. static inline void rttm_enable_irq(void __iomem *base)
  71. {
  72. iowrite32(RTTM_INT_ENABLE, base + RTTM_INT);
  73. }
  74. static inline void rttm_disable_irq(void __iomem *base)
  75. {
  76. iowrite32(0, base + RTTM_INT);
  77. }
  78. /*
  79. * Aggregated control functions for kernel clock framework
  80. */
  81. #define RTTM_DEBUG(base) \
  82. pr_debug("------------- %s %d %08x\n", __func__, \
  83. smp_processor_id(), (u32)base)
  84. static irqreturn_t rttm_timer_interrupt(int irq, void *dev_id)
  85. {
  86. struct clock_event_device *clkevt = dev_id;
  87. struct timer_of *to = to_timer_of(clkevt);
  88. rttm_ack_irq(to->of_base.base);
  89. RTTM_DEBUG(to->of_base.base);
  90. clkevt->event_handler(clkevt);
  91. return IRQ_HANDLED;
  92. }
  93. static void rttm_stop_timer(void __iomem *base)
  94. {
  95. rttm_disable_timer(base);
  96. rttm_ack_irq(base);
  97. }
  98. static void rttm_start_timer(struct timer_of *to, u32 mode)
  99. {
  100. rttm_set_counter(to->of_base.base, 0);
  101. rttm_enable_timer(to->of_base.base, mode, to->of_clk.rate / RTTM_TICKS_PER_SEC);
  102. }
  103. static int rttm_next_event(unsigned long delta, struct clock_event_device *clkevt)
  104. {
  105. struct timer_of *to = to_timer_of(clkevt);
  106. RTTM_DEBUG(to->of_base.base);
  107. rttm_stop_timer(to->of_base.base);
  108. rttm_set_period(to->of_base.base, delta);
  109. rttm_start_timer(to, RTTM_CTRL_COUNTER);
  110. return 0;
  111. }
  112. static int rttm_state_oneshot(struct clock_event_device *clkevt)
  113. {
  114. struct timer_of *to = to_timer_of(clkevt);
  115. RTTM_DEBUG(to->of_base.base);
  116. rttm_stop_timer(to->of_base.base);
  117. rttm_set_period(to->of_base.base, RTTM_TICKS_PER_SEC / HZ);
  118. rttm_start_timer(to, RTTM_CTRL_COUNTER);
  119. return 0;
  120. }
  121. static int rttm_state_periodic(struct clock_event_device *clkevt)
  122. {
  123. struct timer_of *to = to_timer_of(clkevt);
  124. RTTM_DEBUG(to->of_base.base);
  125. rttm_stop_timer(to->of_base.base);
  126. rttm_set_period(to->of_base.base, RTTM_TICKS_PER_SEC / HZ);
  127. rttm_start_timer(to, RTTM_CTRL_TIMER);
  128. return 0;
  129. }
  130. static int rttm_state_shutdown(struct clock_event_device *clkevt)
  131. {
  132. struct timer_of *to = to_timer_of(clkevt);
  133. RTTM_DEBUG(to->of_base.base);
  134. rttm_stop_timer(to->of_base.base);
  135. return 0;
  136. }
  137. static void rttm_setup_timer(void __iomem *base)
  138. {
  139. RTTM_DEBUG(base);
  140. rttm_stop_timer(base);
  141. rttm_set_period(base, 0);
  142. }
  143. static u64 rttm_read_clocksource(struct clocksource *cs)
  144. {
  145. struct rttm_cs *rcs = container_of(cs, struct rttm_cs, cs);
  146. return (u64)rttm_get_counter(rcs->to.of_base.base);
  147. }
  148. /*
  149. * Module initialization part.
  150. */
  151. static DEFINE_PER_CPU(struct timer_of, rttm_to) = {
  152. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK | TIMER_OF_IRQ,
  153. .of_irq = {
  154. .flags = IRQF_PERCPU | IRQF_TIMER,
  155. .handler = rttm_timer_interrupt,
  156. },
  157. .clkevt = {
  158. .rating = 400,
  159. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  160. .set_state_periodic = rttm_state_periodic,
  161. .set_state_shutdown = rttm_state_shutdown,
  162. .set_state_oneshot = rttm_state_oneshot,
  163. .set_next_event = rttm_next_event
  164. },
  165. };
  166. static int rttm_enable_clocksource(struct clocksource *cs)
  167. {
  168. struct rttm_cs *rcs = container_of(cs, struct rttm_cs, cs);
  169. rttm_disable_irq(rcs->to.of_base.base);
  170. rttm_setup_timer(rcs->to.of_base.base);
  171. rttm_enable_timer(rcs->to.of_base.base, RTTM_CTRL_TIMER,
  172. rcs->to.of_clk.rate / RTTM_TICKS_PER_SEC);
  173. return 0;
  174. }
  175. struct rttm_cs rttm_cs = {
  176. .to = {
  177. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
  178. },
  179. .cs = {
  180. .name = "realtek_otto_timer",
  181. .rating = 400,
  182. .mask = CLOCKSOURCE_MASK(RTTM_BIT_COUNT),
  183. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  184. .read = rttm_read_clocksource,
  185. .enable = rttm_enable_clocksource
  186. }
  187. };
  188. static u64 notrace rttm_read_clock(void)
  189. {
  190. return (u64)rttm_get_counter(rttm_cs.to.of_base.base);
  191. }
  192. static int rttm_cpu_starting(unsigned int cpu)
  193. {
  194. struct timer_of *to = per_cpu_ptr(&rttm_to, cpu);
  195. RTTM_DEBUG(to->of_base.base);
  196. to->clkevt.cpumask = cpumask_of(cpu);
  197. irq_set_affinity(to->of_irq.irq, to->clkevt.cpumask);
  198. clockevents_config_and_register(&to->clkevt, RTTM_TICKS_PER_SEC,
  199. RTTM_MIN_DELTA, RTTM_MAX_DELTA);
  200. rttm_enable_irq(to->of_base.base);
  201. return 0;
  202. }
  203. static int __init rttm_probe(struct device_node *np)
  204. {
  205. int cpu, cpu_rollback;
  206. struct timer_of *to;
  207. int clkidx = num_possible_cpus();
  208. /*
  209. * Use the first n timers as per CPU clock event generators
  210. */
  211. for_each_possible_cpu(cpu) {
  212. to = per_cpu_ptr(&rttm_to, cpu);
  213. to->of_irq.index = to->of_base.index = cpu;
  214. if (timer_of_init(np, to)) {
  215. pr_err("%s: setup of timer %d failed\n", __func__, cpu);
  216. goto rollback;
  217. }
  218. rttm_setup_timer(to->of_base.base);
  219. }
  220. /*
  221. * Activate the n'th+1 timer as a stable CPU clocksource.
  222. */
  223. to = &rttm_cs.to;
  224. to->of_base.index = clkidx;
  225. timer_of_init(np, to);
  226. if (rttm_cs.to.of_base.base && rttm_cs.to.of_clk.rate) {
  227. clocksource_register_hz(&rttm_cs.cs, RTTM_TICKS_PER_SEC);
  228. sched_clock_register(rttm_read_clock, RTTM_BIT_COUNT, RTTM_TICKS_PER_SEC);
  229. } else
  230. pr_err("%s: setup of timer %d as clocksoure failed", __func__, clkidx);
  231. return cpuhp_setup_state(CPUHP_AP_REALTEK_TIMER_STARTING,
  232. "timer/realtek:online",
  233. rttm_cpu_starting, NULL);
  234. rollback:
  235. pr_err("%s: timer registration failed\n", __func__);
  236. for_each_possible_cpu(cpu_rollback) {
  237. if (cpu_rollback == cpu)
  238. break;
  239. to = per_cpu_ptr(&rttm_to, cpu_rollback);
  240. timer_of_cleanup(to);
  241. }
  242. return -EINVAL;
  243. }
  244. TIMER_OF_DECLARE(otto_timer, "realtek,otto-timer", rttm_probe);