timer-stm32.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/irq.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/clk.h>
  17. #include <linux/reset.h>
  18. #include <linux/slab.h>
  19. #include "timer-of.h"
  20. #define TIM_CR1 0x00
  21. #define TIM_DIER 0x0c
  22. #define TIM_SR 0x10
  23. #define TIM_EGR 0x14
  24. #define TIM_CNT 0x24
  25. #define TIM_PSC 0x28
  26. #define TIM_ARR 0x2c
  27. #define TIM_CCR1 0x34
  28. #define TIM_CR1_CEN BIT(0)
  29. #define TIM_CR1_UDIS BIT(1)
  30. #define TIM_CR1_OPM BIT(3)
  31. #define TIM_CR1_ARPE BIT(7)
  32. #define TIM_DIER_UIE BIT(0)
  33. #define TIM_DIER_CC1IE BIT(1)
  34. #define TIM_SR_UIF BIT(0)
  35. #define TIM_EGR_UG BIT(0)
  36. #define TIM_PSC_MAX USHRT_MAX
  37. #define TIM_PSC_CLKRATE 10000
  38. struct stm32_timer_private {
  39. int bits;
  40. };
  41. /**
  42. * stm32_timer_of_bits_set - set accessor helper
  43. * @to: a timer_of structure pointer
  44. * @bits: the number of bits (16 or 32)
  45. *
  46. * Accessor helper to set the number of bits in the timer-of private
  47. * structure.
  48. *
  49. */
  50. static void stm32_timer_of_bits_set(struct timer_of *to, int bits)
  51. {
  52. struct stm32_timer_private *pd = to->private_data;
  53. pd->bits = bits;
  54. }
  55. /**
  56. * stm32_timer_of_bits_get - get accessor helper
  57. * @to: a timer_of structure pointer
  58. *
  59. * Accessor helper to get the number of bits in the timer-of private
  60. * structure.
  61. *
  62. * Returns an integer corresponding to the number of bits.
  63. */
  64. static int stm32_timer_of_bits_get(struct timer_of *to)
  65. {
  66. struct stm32_timer_private *pd = to->private_data;
  67. return pd->bits;
  68. }
  69. static void stm32_clock_event_disable(struct timer_of *to)
  70. {
  71. writel_relaxed(0, timer_of_base(to) + TIM_DIER);
  72. }
  73. static void stm32_clock_event_enable(struct timer_of *to)
  74. {
  75. writel_relaxed(TIM_CR1_UDIS | TIM_CR1_CEN, timer_of_base(to) + TIM_CR1);
  76. }
  77. static int stm32_clock_event_shutdown(struct clock_event_device *clkevt)
  78. {
  79. struct timer_of *to = to_timer_of(clkevt);
  80. stm32_clock_event_disable(to);
  81. return 0;
  82. }
  83. static int stm32_clock_event_set_next_event(unsigned long evt,
  84. struct clock_event_device *clkevt)
  85. {
  86. struct timer_of *to = to_timer_of(clkevt);
  87. unsigned long now, next;
  88. next = readl_relaxed(timer_of_base(to) + TIM_CNT) + evt;
  89. writel_relaxed(next, timer_of_base(to) + TIM_CCR1);
  90. now = readl_relaxed(timer_of_base(to) + TIM_CNT);
  91. if ((next - now) > evt)
  92. return -ETIME;
  93. writel_relaxed(TIM_DIER_CC1IE, timer_of_base(to) + TIM_DIER);
  94. return 0;
  95. }
  96. static int stm32_clock_event_set_periodic(struct clock_event_device *clkevt)
  97. {
  98. struct timer_of *to = to_timer_of(clkevt);
  99. stm32_clock_event_enable(to);
  100. return stm32_clock_event_set_next_event(timer_of_period(to), clkevt);
  101. }
  102. static int stm32_clock_event_set_oneshot(struct clock_event_device *clkevt)
  103. {
  104. struct timer_of *to = to_timer_of(clkevt);
  105. stm32_clock_event_enable(to);
  106. return 0;
  107. }
  108. static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id)
  109. {
  110. struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
  111. struct timer_of *to = to_timer_of(clkevt);
  112. writel_relaxed(0, timer_of_base(to) + TIM_SR);
  113. if (clockevent_state_periodic(clkevt))
  114. stm32_clock_event_set_periodic(clkevt);
  115. else
  116. stm32_clock_event_shutdown(clkevt);
  117. clkevt->event_handler(clkevt);
  118. return IRQ_HANDLED;
  119. }
  120. /**
  121. * stm32_timer_width - Sort out the timer width (32/16)
  122. * @to: a pointer to a timer-of structure
  123. *
  124. * Write the 32-bit max value and read/return the result. If the timer
  125. * is 32 bits wide, the result will be UINT_MAX, otherwise it will
  126. * be truncated by the 16-bit register to USHRT_MAX.
  127. *
  128. */
  129. static void __init stm32_timer_set_width(struct timer_of *to)
  130. {
  131. u32 width;
  132. writel_relaxed(UINT_MAX, timer_of_base(to) + TIM_ARR);
  133. width = readl_relaxed(timer_of_base(to) + TIM_ARR);
  134. stm32_timer_of_bits_set(to, width == UINT_MAX ? 32 : 16);
  135. }
  136. /**
  137. * stm32_timer_set_prescaler - Compute and set the prescaler register
  138. * @to: a pointer to a timer-of structure
  139. *
  140. * Depending on the timer width, compute the prescaler to always
  141. * target a 10MHz timer rate for 16 bits. 32-bit timers are
  142. * considered precise and long enough to not use the prescaler.
  143. */
  144. static void __init stm32_timer_set_prescaler(struct timer_of *to)
  145. {
  146. int prescaler = 1;
  147. if (stm32_timer_of_bits_get(to) != 32) {
  148. prescaler = DIV_ROUND_CLOSEST(timer_of_rate(to),
  149. TIM_PSC_CLKRATE);
  150. /*
  151. * The prescaler register is an u16, the variable
  152. * can't be greater than TIM_PSC_MAX, let's cap it in
  153. * this case.
  154. */
  155. prescaler = prescaler < TIM_PSC_MAX ? prescaler : TIM_PSC_MAX;
  156. }
  157. writel_relaxed(prescaler - 1, timer_of_base(to) + TIM_PSC);
  158. writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR);
  159. writel_relaxed(0, timer_of_base(to) + TIM_SR);
  160. /* Adjust rate and period given the prescaler value */
  161. to->of_clk.rate = DIV_ROUND_CLOSEST(to->of_clk.rate, prescaler);
  162. to->of_clk.period = DIV_ROUND_UP(to->of_clk.rate, HZ);
  163. }
  164. static void __init stm32_clockevent_init(struct timer_of *to)
  165. {
  166. u32 bits = stm32_timer_of_bits_get(to);
  167. to->clkevt.name = to->np->full_name;
  168. to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  169. to->clkevt.set_state_shutdown = stm32_clock_event_shutdown;
  170. to->clkevt.set_state_periodic = stm32_clock_event_set_periodic;
  171. to->clkevt.set_state_oneshot = stm32_clock_event_set_oneshot;
  172. to->clkevt.tick_resume = stm32_clock_event_shutdown;
  173. to->clkevt.set_next_event = stm32_clock_event_set_next_event;
  174. to->clkevt.rating = bits == 32 ? 250 : 100;
  175. clockevents_config_and_register(&to->clkevt, timer_of_rate(to), 0x1,
  176. (1 << bits) - 1);
  177. pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n",
  178. to->np, bits);
  179. }
  180. static int __init stm32_timer_init(struct device_node *node)
  181. {
  182. struct reset_control *rstc;
  183. struct timer_of *to;
  184. int ret;
  185. to = kzalloc(sizeof(*to), GFP_KERNEL);
  186. if (!to)
  187. return -ENOMEM;
  188. to->flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE;
  189. to->of_irq.handler = stm32_clock_event_handler;
  190. ret = timer_of_init(node, to);
  191. if (ret)
  192. goto err;
  193. to->private_data = kzalloc(sizeof(struct stm32_timer_private),
  194. GFP_KERNEL);
  195. if (!to->private_data)
  196. goto deinit;
  197. rstc = of_reset_control_get(node, NULL);
  198. if (!IS_ERR(rstc)) {
  199. reset_control_assert(rstc);
  200. reset_control_deassert(rstc);
  201. }
  202. stm32_timer_set_width(to);
  203. stm32_timer_set_prescaler(to);
  204. stm32_clockevent_init(to);
  205. return 0;
  206. deinit:
  207. timer_of_cleanup(to);
  208. err:
  209. kfree(to);
  210. return ret;
  211. }
  212. TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init);