timer-stm32.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. static void stm32_clock_event_disable(struct timer_of *to)
  39. {
  40. writel_relaxed(0, timer_of_base(to) + TIM_DIER);
  41. }
  42. static void stm32_clock_event_enable(struct timer_of *to)
  43. {
  44. writel_relaxed(TIM_CR1_UDIS | TIM_CR1_CEN, timer_of_base(to) + TIM_CR1);
  45. }
  46. static int stm32_clock_event_shutdown(struct clock_event_device *clkevt)
  47. {
  48. struct timer_of *to = to_timer_of(clkevt);
  49. stm32_clock_event_disable(to);
  50. return 0;
  51. }
  52. static int stm32_clock_event_set_next_event(unsigned long evt,
  53. struct clock_event_device *clkevt)
  54. {
  55. struct timer_of *to = to_timer_of(clkevt);
  56. unsigned long now, next;
  57. next = readl_relaxed(timer_of_base(to) + TIM_CNT) + evt;
  58. writel_relaxed(next, timer_of_base(to) + TIM_CCR1);
  59. now = readl_relaxed(timer_of_base(to) + TIM_CNT);
  60. if ((next - now) > evt)
  61. return -ETIME;
  62. writel_relaxed(TIM_DIER_CC1IE, timer_of_base(to) + TIM_DIER);
  63. return 0;
  64. }
  65. static int stm32_clock_event_set_periodic(struct clock_event_device *clkevt)
  66. {
  67. struct timer_of *to = to_timer_of(clkevt);
  68. stm32_clock_event_enable(to);
  69. return stm32_clock_event_set_next_event(timer_of_period(to), clkevt);
  70. }
  71. static int stm32_clock_event_set_oneshot(struct clock_event_device *clkevt)
  72. {
  73. struct timer_of *to = to_timer_of(clkevt);
  74. stm32_clock_event_enable(to);
  75. return 0;
  76. }
  77. static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id)
  78. {
  79. struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
  80. struct timer_of *to = to_timer_of(clkevt);
  81. writel_relaxed(0, timer_of_base(to) + TIM_SR);
  82. if (clockevent_state_periodic(clkevt))
  83. stm32_clock_event_set_periodic(clkevt);
  84. else
  85. stm32_clock_event_shutdown(clkevt);
  86. clkevt->event_handler(clkevt);
  87. return IRQ_HANDLED;
  88. }
  89. /**
  90. * stm32_timer_width - Sort out the timer width (32/16)
  91. * @to: a pointer to a timer-of structure
  92. *
  93. * Write the 32-bit max value and read/return the result. If the timer
  94. * is 32 bits wide, the result will be UINT_MAX, otherwise it will
  95. * be truncated by the 16-bit register to USHRT_MAX.
  96. *
  97. * Returns UINT_MAX if the timer is 32 bits wide, USHRT_MAX if it is a
  98. * 16 bits wide.
  99. */
  100. static u32 __init stm32_timer_width(struct timer_of *to)
  101. {
  102. writel_relaxed(UINT_MAX, timer_of_base(to) + TIM_ARR);
  103. return readl_relaxed(timer_of_base(to) + TIM_ARR);
  104. }
  105. static void __init stm32_clockevent_init(struct timer_of *to)
  106. {
  107. u32 width = 0;
  108. int prescaler;
  109. to->clkevt.name = to->np->full_name;
  110. to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  111. to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  112. to->clkevt.set_state_shutdown = stm32_clock_event_shutdown;
  113. to->clkevt.set_state_periodic = stm32_clock_event_set_periodic;
  114. to->clkevt.set_state_oneshot = stm32_clock_event_set_oneshot;
  115. to->clkevt.tick_resume = stm32_clock_event_shutdown;
  116. to->clkevt.set_next_event = stm32_clock_event_set_next_event;
  117. width = stm32_timer_width(to);
  118. if (width == UINT_MAX) {
  119. prescaler = 1;
  120. to->clkevt.rating = 250;
  121. } else {
  122. prescaler = DIV_ROUND_CLOSEST(timer_of_rate(to),
  123. TIM_PSC_CLKRATE);
  124. /*
  125. * The prescaler register is an u16, the variable
  126. * can't be greater than TIM_PSC_MAX, let's cap it in
  127. * this case.
  128. */
  129. prescaler = prescaler < TIM_PSC_MAX ? prescaler : TIM_PSC_MAX;
  130. to->clkevt.rating = 100;
  131. }
  132. writel_relaxed(prescaler - 1, timer_of_base(to) + TIM_PSC);
  133. writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR);
  134. writel_relaxed(0, timer_of_base(to) + TIM_SR);
  135. /* Adjust rate and period given the prescaler value */
  136. to->of_clk.rate = DIV_ROUND_CLOSEST(to->of_clk.rate, prescaler);
  137. to->of_clk.period = DIV_ROUND_UP(to->of_clk.rate, HZ);
  138. clockevents_config_and_register(&to->clkevt,
  139. timer_of_rate(to), 0x1, width);
  140. pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n",
  141. to->np, width == UINT_MAX ? 32 : 16);
  142. }
  143. static int __init stm32_timer_init(struct device_node *node)
  144. {
  145. struct reset_control *rstc;
  146. struct timer_of *to;
  147. int ret;
  148. to = kzalloc(sizeof(*to), GFP_KERNEL);
  149. if (!to)
  150. return -ENOMEM;
  151. to->flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE;
  152. to->of_irq.handler = stm32_clock_event_handler;
  153. ret = timer_of_init(node, to);
  154. if (ret)
  155. goto err;
  156. rstc = of_reset_control_get(node, NULL);
  157. if (!IS_ERR(rstc)) {
  158. reset_control_assert(rstc);
  159. reset_control_deassert(rstc);
  160. }
  161. stm32_clockevent_init(to);
  162. return 0;
  163. err:
  164. kfree(to);
  165. return ret;
  166. }
  167. TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init);