timer-rtl-otto.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #define RTTM_MAX_DIVISOR GENMASK(15, 0)
  36. /*
  37. * Timers are derived from the lexra bus (LXB) clock frequency. This is 175 MHz
  38. * on RTL930x and 200 MHz on the other platforms. With 3.125 MHz choose a common
  39. * divisor to have enough range and detail. This provides comparability between
  40. * the different platforms.
  41. */
  42. #define RTTM_TICKS_PER_SEC 3125000
  43. struct rttm_cs {
  44. struct timer_of to;
  45. struct clocksource cs;
  46. };
  47. /* Simple internal register functions */
  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("------------- %d %p\n", \
  79. smp_processor_id(), 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_bounce_timer(void __iomem *base, u32 mode)
  90. {
  91. /*
  92. * When a running timer has less than ~5us left, a stop/start sequence
  93. * might fail. While the details are unknown the most evident effect is
  94. * that the subsequent interrupt will not be fired.
  95. *
  96. * As a workaround issue an intermediate restart with a very slow
  97. * frequency of ~3kHz keeping the target counter (>=8). So the follow
  98. * up restart will always be issued outside the critical window.
  99. */
  100. rttm_disable_timer(base);
  101. rttm_enable_timer(base, mode, RTTM_MAX_DIVISOR);
  102. }
  103. static void rttm_stop_timer(void __iomem *base)
  104. {
  105. rttm_disable_timer(base);
  106. rttm_ack_irq(base);
  107. }
  108. static void rttm_start_timer(struct timer_of *to, u32 mode)
  109. {
  110. rttm_enable_timer(to->of_base.base, mode, to->of_clk.rate / RTTM_TICKS_PER_SEC);
  111. }
  112. static int rttm_next_event(unsigned long delta, struct clock_event_device *clkevt)
  113. {
  114. struct timer_of *to = to_timer_of(clkevt);
  115. RTTM_DEBUG(to->of_base.base);
  116. rttm_bounce_timer(to->of_base.base, RTTM_CTRL_COUNTER);
  117. rttm_disable_timer(to->of_base.base);
  118. rttm_set_period(to->of_base.base, delta);
  119. rttm_start_timer(to, RTTM_CTRL_COUNTER);
  120. return 0;
  121. }
  122. static int rttm_state_oneshot(struct clock_event_device *clkevt)
  123. {
  124. struct timer_of *to = to_timer_of(clkevt);
  125. RTTM_DEBUG(to->of_base.base);
  126. rttm_bounce_timer(to->of_base.base, RTTM_CTRL_COUNTER);
  127. rttm_disable_timer(to->of_base.base);
  128. rttm_set_period(to->of_base.base, RTTM_TICKS_PER_SEC / HZ);
  129. rttm_start_timer(to, RTTM_CTRL_COUNTER);
  130. return 0;
  131. }
  132. static int rttm_state_periodic(struct clock_event_device *clkevt)
  133. {
  134. struct timer_of *to = to_timer_of(clkevt);
  135. RTTM_DEBUG(to->of_base.base);
  136. rttm_bounce_timer(to->of_base.base, RTTM_CTRL_TIMER);
  137. rttm_disable_timer(to->of_base.base);
  138. rttm_set_period(to->of_base.base, RTTM_TICKS_PER_SEC / HZ);
  139. rttm_start_timer(to, RTTM_CTRL_TIMER);
  140. return 0;
  141. }
  142. static int rttm_state_shutdown(struct clock_event_device *clkevt)
  143. {
  144. struct timer_of *to = to_timer_of(clkevt);
  145. RTTM_DEBUG(to->of_base.base);
  146. rttm_stop_timer(to->of_base.base);
  147. return 0;
  148. }
  149. static void rttm_setup_timer(void __iomem *base)
  150. {
  151. RTTM_DEBUG(base);
  152. rttm_stop_timer(base);
  153. rttm_set_period(base, 0);
  154. }
  155. static u64 rttm_read_clocksource(struct clocksource *cs)
  156. {
  157. struct rttm_cs *rcs = container_of(cs, struct rttm_cs, cs);
  158. return rttm_get_counter(rcs->to.of_base.base);
  159. }
  160. /* Module initialization part. */
  161. static DEFINE_PER_CPU(struct timer_of, rttm_to) = {
  162. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK | TIMER_OF_IRQ,
  163. .of_irq = {
  164. .flags = IRQF_PERCPU | IRQF_TIMER,
  165. .handler = rttm_timer_interrupt,
  166. },
  167. .clkevt = {
  168. .rating = 400,
  169. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  170. .set_state_periodic = rttm_state_periodic,
  171. .set_state_shutdown = rttm_state_shutdown,
  172. .set_state_oneshot = rttm_state_oneshot,
  173. .set_next_event = rttm_next_event
  174. },
  175. };
  176. static int rttm_enable_clocksource(struct clocksource *cs)
  177. {
  178. struct rttm_cs *rcs = container_of(cs, struct rttm_cs, cs);
  179. rttm_disable_irq(rcs->to.of_base.base);
  180. rttm_setup_timer(rcs->to.of_base.base);
  181. rttm_enable_timer(rcs->to.of_base.base, RTTM_CTRL_TIMER,
  182. rcs->to.of_clk.rate / RTTM_TICKS_PER_SEC);
  183. return 0;
  184. }
  185. struct rttm_cs rttm_cs = {
  186. .to = {
  187. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
  188. },
  189. .cs = {
  190. .name = "realtek_otto_timer",
  191. .rating = 400,
  192. .mask = CLOCKSOURCE_MASK(RTTM_BIT_COUNT),
  193. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  194. .read = rttm_read_clocksource,
  195. }
  196. };
  197. static u64 notrace rttm_read_clock(void)
  198. {
  199. return rttm_get_counter(rttm_cs.to.of_base.base);
  200. }
  201. static int rttm_cpu_starting(unsigned int cpu)
  202. {
  203. struct timer_of *to = per_cpu_ptr(&rttm_to, cpu);
  204. RTTM_DEBUG(to->of_base.base);
  205. to->clkevt.cpumask = cpumask_of(cpu);
  206. irq_force_affinity(to->of_irq.irq, to->clkevt.cpumask);
  207. clockevents_config_and_register(&to->clkevt, RTTM_TICKS_PER_SEC,
  208. RTTM_MIN_DELTA, RTTM_MAX_DELTA);
  209. rttm_enable_irq(to->of_base.base);
  210. return 0;
  211. }
  212. static int __init rttm_probe(struct device_node *np)
  213. {
  214. unsigned int cpu, cpu_rollback;
  215. struct timer_of *to;
  216. unsigned int clkidx = num_possible_cpus();
  217. /* Use the first n timers as per CPU clock event generators */
  218. for_each_possible_cpu(cpu) {
  219. to = per_cpu_ptr(&rttm_to, cpu);
  220. to->of_irq.index = to->of_base.index = cpu;
  221. if (timer_of_init(np, to)) {
  222. pr_err("setup of timer %d failed\n", cpu);
  223. goto rollback;
  224. }
  225. rttm_setup_timer(to->of_base.base);
  226. }
  227. /* Activate the n'th + 1 timer as a stable CPU clocksource. */
  228. to = &rttm_cs.to;
  229. to->of_base.index = clkidx;
  230. timer_of_init(np, to);
  231. if (rttm_cs.to.of_base.base && rttm_cs.to.of_clk.rate) {
  232. rttm_enable_clocksource(&rttm_cs.cs);
  233. clocksource_register_hz(&rttm_cs.cs, RTTM_TICKS_PER_SEC);
  234. sched_clock_register(rttm_read_clock, RTTM_BIT_COUNT, RTTM_TICKS_PER_SEC);
  235. } else
  236. pr_err(" setup of timer %d as clocksource failed", clkidx);
  237. return cpuhp_setup_state(CPUHP_AP_REALTEK_TIMER_STARTING,
  238. "timer/realtek:online",
  239. rttm_cpu_starting, NULL);
  240. rollback:
  241. pr_err("timer registration failed\n");
  242. for_each_possible_cpu(cpu_rollback) {
  243. if (cpu_rollback == cpu)
  244. break;
  245. to = per_cpu_ptr(&rttm_to, cpu_rollback);
  246. timer_of_cleanup(to);
  247. }
  248. return -EINVAL;
  249. }
  250. TIMER_OF_DECLARE(otto_timer, "realtek,otto-timer", rttm_probe);