timer-stm32.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (C) Maxime Coquelin 2015
  3. * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
  4. * License terms: GNU General Public License (GPL), version 2
  5. *
  6. * Inspired by time-efm32.c from Uwe Kleine-Koenig
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/delay.h>
  12. #include <linux/irq.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/clk.h>
  18. #include <linux/reset.h>
  19. #include <linux/sched_clock.h>
  20. #include <linux/slab.h>
  21. #include "timer-of.h"
  22. #define TIM_CR1 0x00
  23. #define TIM_DIER 0x0c
  24. #define TIM_SR 0x10
  25. #define TIM_EGR 0x14
  26. #define TIM_CNT 0x24
  27. #define TIM_PSC 0x28
  28. #define TIM_ARR 0x2c
  29. #define TIM_CCR1 0x34
  30. #define TIM_CR1_CEN BIT(0)
  31. #define TIM_CR1_UDIS BIT(1)
  32. #define TIM_CR1_OPM BIT(3)
  33. #define TIM_CR1_ARPE BIT(7)
  34. #define TIM_DIER_UIE BIT(0)
  35. #define TIM_DIER_CC1IE BIT(1)
  36. #define TIM_SR_UIF BIT(0)
  37. #define TIM_EGR_UG BIT(0)
  38. #define TIM_PSC_MAX USHRT_MAX
  39. #define TIM_PSC_CLKRATE 10000
  40. struct stm32_timer_private {
  41. int bits;
  42. };
  43. /**
  44. * stm32_timer_of_bits_set - set accessor helper
  45. * @to: a timer_of structure pointer
  46. * @bits: the number of bits (16 or 32)
  47. *
  48. * Accessor helper to set the number of bits in the timer-of private
  49. * structure.
  50. *
  51. */
  52. static void stm32_timer_of_bits_set(struct timer_of *to, int bits)
  53. {
  54. struct stm32_timer_private *pd = to->private_data;
  55. pd->bits = bits;
  56. }
  57. /**
  58. * stm32_timer_of_bits_get - get accessor helper
  59. * @to: a timer_of structure pointer
  60. *
  61. * Accessor helper to get the number of bits in the timer-of private
  62. * structure.
  63. *
  64. * Returns an integer corresponding to the number of bits.
  65. */
  66. static int stm32_timer_of_bits_get(struct timer_of *to)
  67. {
  68. struct stm32_timer_private *pd = to->private_data;
  69. return pd->bits;
  70. }
  71. static void __iomem *stm32_timer_cnt __read_mostly;
  72. static u64 notrace stm32_read_sched_clock(void)
  73. {
  74. return readl_relaxed(stm32_timer_cnt);
  75. }
  76. static struct delay_timer stm32_timer_delay;
  77. static unsigned long stm32_read_delay(void)
  78. {
  79. return readl_relaxed(stm32_timer_cnt);
  80. }
  81. static void stm32_clock_event_disable(struct timer_of *to)
  82. {
  83. writel_relaxed(0, timer_of_base(to) + TIM_DIER);
  84. }
  85. static void stm32_clock_event_enable(struct timer_of *to)
  86. {
  87. writel_relaxed(TIM_CR1_UDIS | TIM_CR1_CEN, timer_of_base(to) + TIM_CR1);
  88. }
  89. static int stm32_clock_event_shutdown(struct clock_event_device *clkevt)
  90. {
  91. struct timer_of *to = to_timer_of(clkevt);
  92. stm32_clock_event_disable(to);
  93. return 0;
  94. }
  95. static int stm32_clock_event_set_next_event(unsigned long evt,
  96. struct clock_event_device *clkevt)
  97. {
  98. struct timer_of *to = to_timer_of(clkevt);
  99. unsigned long now, next;
  100. next = readl_relaxed(timer_of_base(to) + TIM_CNT) + evt;
  101. writel_relaxed(next, timer_of_base(to) + TIM_CCR1);
  102. now = readl_relaxed(timer_of_base(to) + TIM_CNT);
  103. if ((next - now) > evt)
  104. return -ETIME;
  105. writel_relaxed(TIM_DIER_CC1IE, timer_of_base(to) + TIM_DIER);
  106. return 0;
  107. }
  108. static int stm32_clock_event_set_periodic(struct clock_event_device *clkevt)
  109. {
  110. struct timer_of *to = to_timer_of(clkevt);
  111. stm32_clock_event_enable(to);
  112. return stm32_clock_event_set_next_event(timer_of_period(to), clkevt);
  113. }
  114. static int stm32_clock_event_set_oneshot(struct clock_event_device *clkevt)
  115. {
  116. struct timer_of *to = to_timer_of(clkevt);
  117. stm32_clock_event_enable(to);
  118. return 0;
  119. }
  120. static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id)
  121. {
  122. struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
  123. struct timer_of *to = to_timer_of(clkevt);
  124. writel_relaxed(0, timer_of_base(to) + TIM_SR);
  125. if (clockevent_state_periodic(clkevt))
  126. stm32_clock_event_set_periodic(clkevt);
  127. else
  128. stm32_clock_event_shutdown(clkevt);
  129. clkevt->event_handler(clkevt);
  130. return IRQ_HANDLED;
  131. }
  132. /**
  133. * stm32_timer_width - Sort out the timer width (32/16)
  134. * @to: a pointer to a timer-of structure
  135. *
  136. * Write the 32-bit max value and read/return the result. If the timer
  137. * is 32 bits wide, the result will be UINT_MAX, otherwise it will
  138. * be truncated by the 16-bit register to USHRT_MAX.
  139. *
  140. */
  141. static void __init stm32_timer_set_width(struct timer_of *to)
  142. {
  143. u32 width;
  144. writel_relaxed(UINT_MAX, timer_of_base(to) + TIM_ARR);
  145. width = readl_relaxed(timer_of_base(to) + TIM_ARR);
  146. stm32_timer_of_bits_set(to, width == UINT_MAX ? 32 : 16);
  147. }
  148. /**
  149. * stm32_timer_set_prescaler - Compute and set the prescaler register
  150. * @to: a pointer to a timer-of structure
  151. *
  152. * Depending on the timer width, compute the prescaler to always
  153. * target a 10MHz timer rate for 16 bits. 32-bit timers are
  154. * considered precise and long enough to not use the prescaler.
  155. */
  156. static void __init stm32_timer_set_prescaler(struct timer_of *to)
  157. {
  158. int prescaler = 1;
  159. if (stm32_timer_of_bits_get(to) != 32) {
  160. prescaler = DIV_ROUND_CLOSEST(timer_of_rate(to),
  161. TIM_PSC_CLKRATE);
  162. /*
  163. * The prescaler register is an u16, the variable
  164. * can't be greater than TIM_PSC_MAX, let's cap it in
  165. * this case.
  166. */
  167. prescaler = prescaler < TIM_PSC_MAX ? prescaler : TIM_PSC_MAX;
  168. }
  169. writel_relaxed(prescaler - 1, timer_of_base(to) + TIM_PSC);
  170. writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR);
  171. writel_relaxed(0, timer_of_base(to) + TIM_SR);
  172. /* Adjust rate and period given the prescaler value */
  173. to->of_clk.rate = DIV_ROUND_CLOSEST(to->of_clk.rate, prescaler);
  174. to->of_clk.period = DIV_ROUND_UP(to->of_clk.rate, HZ);
  175. }
  176. static int __init stm32_clocksource_init(struct timer_of *to)
  177. {
  178. u32 bits = stm32_timer_of_bits_get(to);
  179. const char *name = to->np->full_name;
  180. /*
  181. * This driver allows to register several timers and relies on
  182. * the generic time framework to select the right one.
  183. * However, nothing allows to do the same for the
  184. * sched_clock. We are not interested in a sched_clock for the
  185. * 16-bit timers but only for the 32-bit one, so if no 32-bit
  186. * timer is registered yet, we select this 32-bit timer as a
  187. * sched_clock.
  188. */
  189. if (bits == 32 && !stm32_timer_cnt) {
  190. stm32_timer_cnt = timer_of_base(to) + TIM_CNT;
  191. sched_clock_register(stm32_read_sched_clock, bits, timer_of_rate(to));
  192. pr_info("%s: STM32 sched_clock registered\n", name);
  193. stm32_timer_delay.read_current_timer = stm32_read_delay;
  194. stm32_timer_delay.freq = timer_of_rate(to);
  195. register_current_timer_delay(&stm32_timer_delay);
  196. pr_info("%s: STM32 delay timer registered\n", name);
  197. }
  198. return clocksource_mmio_init(timer_of_base(to) + TIM_CNT, name,
  199. timer_of_rate(to), bits == 32 ? 250 : 100,
  200. bits, clocksource_mmio_readl_up);
  201. }
  202. static void __init stm32_clockevent_init(struct timer_of *to)
  203. {
  204. u32 bits = stm32_timer_of_bits_get(to);
  205. to->clkevt.name = to->np->full_name;
  206. to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  207. to->clkevt.set_state_shutdown = stm32_clock_event_shutdown;
  208. to->clkevt.set_state_periodic = stm32_clock_event_set_periodic;
  209. to->clkevt.set_state_oneshot = stm32_clock_event_set_oneshot;
  210. to->clkevt.tick_resume = stm32_clock_event_shutdown;
  211. to->clkevt.set_next_event = stm32_clock_event_set_next_event;
  212. to->clkevt.rating = bits == 32 ? 250 : 100;
  213. clockevents_config_and_register(&to->clkevt, timer_of_rate(to), 0x1,
  214. (1 << bits) - 1);
  215. pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n",
  216. to->np, bits);
  217. }
  218. static int __init stm32_timer_init(struct device_node *node)
  219. {
  220. struct reset_control *rstc;
  221. struct timer_of *to;
  222. int ret;
  223. to = kzalloc(sizeof(*to), GFP_KERNEL);
  224. if (!to)
  225. return -ENOMEM;
  226. to->flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE;
  227. to->of_irq.handler = stm32_clock_event_handler;
  228. ret = timer_of_init(node, to);
  229. if (ret)
  230. goto err;
  231. to->private_data = kzalloc(sizeof(struct stm32_timer_private),
  232. GFP_KERNEL);
  233. if (!to->private_data)
  234. goto deinit;
  235. rstc = of_reset_control_get(node, NULL);
  236. if (!IS_ERR(rstc)) {
  237. reset_control_assert(rstc);
  238. reset_control_deassert(rstc);
  239. }
  240. stm32_timer_set_width(to);
  241. stm32_timer_set_prescaler(to);
  242. ret = stm32_clocksource_init(to);
  243. if (ret)
  244. goto deinit;
  245. stm32_clockevent_init(to);
  246. return 0;
  247. deinit:
  248. timer_of_cleanup(to);
  249. err:
  250. kfree(to);
  251. return ret;
  252. }
  253. TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init);