timer-stm32.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #define TIM_CR1 0x00
  20. #define TIM_DIER 0x0c
  21. #define TIM_SR 0x10
  22. #define TIM_EGR 0x14
  23. #define TIM_PSC 0x28
  24. #define TIM_ARR 0x2c
  25. #define TIM_CR1_CEN BIT(0)
  26. #define TIM_CR1_OPM BIT(3)
  27. #define TIM_CR1_ARPE BIT(7)
  28. #define TIM_DIER_UIE BIT(0)
  29. #define TIM_SR_UIF BIT(0)
  30. #define TIM_EGR_UG BIT(0)
  31. struct stm32_clock_event_ddata {
  32. struct clock_event_device evtdev;
  33. unsigned periodic_top;
  34. void __iomem *base;
  35. };
  36. static int stm32_clock_event_shutdown(struct clock_event_device *evtdev)
  37. {
  38. struct stm32_clock_event_ddata *data =
  39. container_of(evtdev, struct stm32_clock_event_ddata, evtdev);
  40. void *base = data->base;
  41. writel_relaxed(0, base + TIM_CR1);
  42. return 0;
  43. }
  44. static int stm32_clock_event_set_periodic(struct clock_event_device *evtdev)
  45. {
  46. struct stm32_clock_event_ddata *data =
  47. container_of(evtdev, struct stm32_clock_event_ddata, evtdev);
  48. void *base = data->base;
  49. writel_relaxed(data->periodic_top, base + TIM_ARR);
  50. writel_relaxed(TIM_CR1_ARPE | TIM_CR1_CEN, base + TIM_CR1);
  51. return 0;
  52. }
  53. static int stm32_clock_event_set_next_event(unsigned long evt,
  54. struct clock_event_device *evtdev)
  55. {
  56. struct stm32_clock_event_ddata *data =
  57. container_of(evtdev, struct stm32_clock_event_ddata, evtdev);
  58. writel_relaxed(evt, data->base + TIM_ARR);
  59. writel_relaxed(TIM_CR1_ARPE | TIM_CR1_OPM | TIM_CR1_CEN,
  60. data->base + TIM_CR1);
  61. return 0;
  62. }
  63. static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id)
  64. {
  65. struct stm32_clock_event_ddata *data = dev_id;
  66. writel_relaxed(0, data->base + TIM_SR);
  67. data->evtdev.event_handler(&data->evtdev);
  68. return IRQ_HANDLED;
  69. }
  70. static struct stm32_clock_event_ddata clock_event_ddata = {
  71. .evtdev = {
  72. .name = "stm32 clockevent",
  73. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  74. .set_state_shutdown = stm32_clock_event_shutdown,
  75. .set_state_periodic = stm32_clock_event_set_periodic,
  76. .set_state_oneshot = stm32_clock_event_shutdown,
  77. .tick_resume = stm32_clock_event_shutdown,
  78. .set_next_event = stm32_clock_event_set_next_event,
  79. .rating = 200,
  80. },
  81. };
  82. static int __init stm32_clockevent_init(struct device_node *np)
  83. {
  84. struct stm32_clock_event_ddata *data = &clock_event_ddata;
  85. struct clk *clk;
  86. struct reset_control *rstc;
  87. unsigned long rate, max_delta;
  88. int irq, ret, bits, prescaler = 1;
  89. data = kmemdup(&clock_event_ddata, sizeof(*data), GFP_KERNEL);
  90. if (!data)
  91. return -ENOMEM;
  92. clk = of_clk_get(np, 0);
  93. if (IS_ERR(clk)) {
  94. ret = PTR_ERR(clk);
  95. pr_err("failed to get clock for clockevent (%d)\n", ret);
  96. goto err_clk_get;
  97. }
  98. ret = clk_prepare_enable(clk);
  99. if (ret) {
  100. pr_err("failed to enable timer clock for clockevent (%d)\n",
  101. ret);
  102. goto err_clk_enable;
  103. }
  104. rate = clk_get_rate(clk);
  105. rstc = of_reset_control_get(np, NULL);
  106. if (!IS_ERR(rstc)) {
  107. reset_control_assert(rstc);
  108. reset_control_deassert(rstc);
  109. }
  110. data->base = of_iomap(np, 0);
  111. if (!data->base) {
  112. ret = -ENXIO;
  113. pr_err("failed to map registers for clockevent\n");
  114. goto err_iomap;
  115. }
  116. irq = irq_of_parse_and_map(np, 0);
  117. if (!irq) {
  118. ret = -EINVAL;
  119. pr_err("%s: failed to get irq.\n", np->full_name);
  120. goto err_get_irq;
  121. }
  122. /* Detect whether the timer is 16 or 32 bits */
  123. writel_relaxed(~0U, data->base + TIM_ARR);
  124. max_delta = readl_relaxed(data->base + TIM_ARR);
  125. if (max_delta == ~0U) {
  126. prescaler = 1;
  127. bits = 32;
  128. } else {
  129. prescaler = 1024;
  130. bits = 16;
  131. }
  132. writel_relaxed(0, data->base + TIM_ARR);
  133. writel_relaxed(prescaler - 1, data->base + TIM_PSC);
  134. writel_relaxed(TIM_EGR_UG, data->base + TIM_EGR);
  135. writel_relaxed(0, data->base + TIM_SR);
  136. writel_relaxed(TIM_DIER_UIE, data->base + TIM_DIER);
  137. data->periodic_top = DIV_ROUND_CLOSEST(rate, prescaler * HZ);
  138. clockevents_config_and_register(&data->evtdev,
  139. DIV_ROUND_CLOSEST(rate, prescaler),
  140. 0x1, max_delta);
  141. ret = request_irq(irq, stm32_clock_event_handler, IRQF_TIMER,
  142. "stm32 clockevent", data);
  143. if (ret) {
  144. pr_err("%s: failed to request irq.\n", np->full_name);
  145. goto err_get_irq;
  146. }
  147. pr_info("%s: STM32 clockevent driver initialized (%d bits)\n",
  148. np->full_name, bits);
  149. return ret;
  150. err_get_irq:
  151. iounmap(data->base);
  152. err_iomap:
  153. clk_disable_unprepare(clk);
  154. err_clk_enable:
  155. clk_put(clk);
  156. err_clk_get:
  157. kfree(data);
  158. return ret;
  159. }
  160. CLOCKSOURCE_OF_DECLARE(stm32, "st,stm32-timer", stm32_clockevent_init);