mxs_timer.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2000-2001 Deep Blue Solutions
  4. // Copyright (C) 2002 Shane Nay ([email protected])
  5. // Copyright (C) 2006-2007 Pavel Pisa ([email protected])
  6. // Copyright (C) 2008 Juergen Beisert ([email protected])
  7. // Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/irq.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/clk.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/stmp_device.h>
  17. #include <linux/sched_clock.h>
  18. /*
  19. * There are 2 versions of the timrot on Freescale MXS-based SoCs.
  20. * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
  21. * extends the counter to 32 bits.
  22. *
  23. * The implementation uses two timers, one for clock_event and
  24. * another for clocksource. MX28 uses timrot 0 and 1, while MX23
  25. * uses 0 and 2.
  26. */
  27. #define MX23_TIMROT_VERSION_OFFSET 0x0a0
  28. #define MX28_TIMROT_VERSION_OFFSET 0x120
  29. #define BP_TIMROT_MAJOR_VERSION 24
  30. #define BV_TIMROT_VERSION_1 0x01
  31. #define BV_TIMROT_VERSION_2 0x02
  32. #define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1)
  33. /*
  34. * There are 4 registers for each timrotv2 instance, and 2 registers
  35. * for each timrotv1. So address step 0x40 in macros below strides
  36. * one instance of timrotv2 while two instances of timrotv1.
  37. *
  38. * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
  39. * on MX28 while timrot2 on MX23.
  40. */
  41. /* common between v1 and v2 */
  42. #define HW_TIMROT_ROTCTRL 0x00
  43. #define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40)
  44. /* v1 only */
  45. #define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40)
  46. /* v2 only */
  47. #define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40)
  48. #define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40)
  49. #define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6)
  50. #define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7)
  51. #define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14)
  52. #define BM_TIMROT_TIMCTRLn_IRQ (1 << 15)
  53. #define BP_TIMROT_TIMCTRLn_SELECT 0
  54. #define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8
  55. #define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb
  56. #define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS 0xf
  57. static struct clock_event_device mxs_clockevent_device;
  58. static void __iomem *mxs_timrot_base;
  59. static u32 timrot_major_version;
  60. static inline void timrot_irq_disable(void)
  61. {
  62. __raw_writel(BM_TIMROT_TIMCTRLn_IRQ_EN, mxs_timrot_base +
  63. HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_CLR);
  64. }
  65. static inline void timrot_irq_enable(void)
  66. {
  67. __raw_writel(BM_TIMROT_TIMCTRLn_IRQ_EN, mxs_timrot_base +
  68. HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_SET);
  69. }
  70. static void timrot_irq_acknowledge(void)
  71. {
  72. __raw_writel(BM_TIMROT_TIMCTRLn_IRQ, mxs_timrot_base +
  73. HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_CLR);
  74. }
  75. static u64 timrotv1_get_cycles(struct clocksource *cs)
  76. {
  77. return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
  78. & 0xffff0000) >> 16);
  79. }
  80. static int timrotv1_set_next_event(unsigned long evt,
  81. struct clock_event_device *dev)
  82. {
  83. /* timrot decrements the count */
  84. __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
  85. return 0;
  86. }
  87. static int timrotv2_set_next_event(unsigned long evt,
  88. struct clock_event_device *dev)
  89. {
  90. /* timrot decrements the count */
  91. __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
  92. return 0;
  93. }
  94. static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
  95. {
  96. struct clock_event_device *evt = dev_id;
  97. timrot_irq_acknowledge();
  98. evt->event_handler(evt);
  99. return IRQ_HANDLED;
  100. }
  101. static void mxs_irq_clear(char *state)
  102. {
  103. /* Disable interrupt in timer module */
  104. timrot_irq_disable();
  105. /* Set event time into the furthest future */
  106. if (timrot_is_v1())
  107. __raw_writel(0xffff, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  108. else
  109. __raw_writel(0xffffffff,
  110. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  111. /* Clear pending interrupt */
  112. timrot_irq_acknowledge();
  113. pr_debug("%s: changing mode to %s\n", __func__, state);
  114. }
  115. static int mxs_shutdown(struct clock_event_device *evt)
  116. {
  117. mxs_irq_clear("shutdown");
  118. return 0;
  119. }
  120. static int mxs_set_oneshot(struct clock_event_device *evt)
  121. {
  122. if (clockevent_state_oneshot(evt))
  123. mxs_irq_clear("oneshot");
  124. timrot_irq_enable();
  125. return 0;
  126. }
  127. static struct clock_event_device mxs_clockevent_device = {
  128. .name = "mxs_timrot",
  129. .features = CLOCK_EVT_FEAT_ONESHOT,
  130. .set_state_shutdown = mxs_shutdown,
  131. .set_state_oneshot = mxs_set_oneshot,
  132. .tick_resume = mxs_shutdown,
  133. .set_next_event = timrotv2_set_next_event,
  134. .rating = 200,
  135. };
  136. static int __init mxs_clockevent_init(struct clk *timer_clk)
  137. {
  138. if (timrot_is_v1())
  139. mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
  140. mxs_clockevent_device.cpumask = cpumask_of(0);
  141. clockevents_config_and_register(&mxs_clockevent_device,
  142. clk_get_rate(timer_clk),
  143. timrot_is_v1() ? 0xf : 0x2,
  144. timrot_is_v1() ? 0xfffe : 0xfffffffe);
  145. return 0;
  146. }
  147. static struct clocksource clocksource_mxs = {
  148. .name = "mxs_timer",
  149. .rating = 200,
  150. .read = timrotv1_get_cycles,
  151. .mask = CLOCKSOURCE_MASK(16),
  152. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  153. };
  154. static u64 notrace mxs_read_sched_clock_v2(void)
  155. {
  156. return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
  157. }
  158. static int __init mxs_clocksource_init(struct clk *timer_clk)
  159. {
  160. unsigned int c = clk_get_rate(timer_clk);
  161. if (timrot_is_v1())
  162. clocksource_register_hz(&clocksource_mxs, c);
  163. else {
  164. clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
  165. "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
  166. sched_clock_register(mxs_read_sched_clock_v2, 32, c);
  167. }
  168. return 0;
  169. }
  170. static int __init mxs_timer_init(struct device_node *np)
  171. {
  172. struct clk *timer_clk;
  173. int irq, ret;
  174. mxs_timrot_base = of_iomap(np, 0);
  175. WARN_ON(!mxs_timrot_base);
  176. timer_clk = of_clk_get(np, 0);
  177. if (IS_ERR(timer_clk)) {
  178. pr_err("%s: failed to get clk\n", __func__);
  179. return PTR_ERR(timer_clk);
  180. }
  181. ret = clk_prepare_enable(timer_clk);
  182. if (ret)
  183. return ret;
  184. /*
  185. * Initialize timers to a known state
  186. */
  187. stmp_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
  188. /* get timrot version */
  189. timrot_major_version = __raw_readl(mxs_timrot_base +
  190. (of_device_is_compatible(np, "fsl,imx23-timrot") ?
  191. MX23_TIMROT_VERSION_OFFSET :
  192. MX28_TIMROT_VERSION_OFFSET));
  193. timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
  194. /* one for clock_event */
  195. __raw_writel((timrot_is_v1() ?
  196. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  197. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  198. BM_TIMROT_TIMCTRLn_UPDATE |
  199. BM_TIMROT_TIMCTRLn_IRQ_EN,
  200. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  201. /* another for clocksource */
  202. __raw_writel((timrot_is_v1() ?
  203. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  204. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  205. BM_TIMROT_TIMCTRLn_RELOAD,
  206. mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
  207. /* set clocksource timer fixed count to the maximum */
  208. if (timrot_is_v1())
  209. __raw_writel(0xffff,
  210. mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  211. else
  212. __raw_writel(0xffffffff,
  213. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  214. /* init and register the timer to the framework */
  215. ret = mxs_clocksource_init(timer_clk);
  216. if (ret)
  217. return ret;
  218. ret = mxs_clockevent_init(timer_clk);
  219. if (ret)
  220. return ret;
  221. /* Make irqs happen */
  222. irq = irq_of_parse_and_map(np, 0);
  223. if (irq <= 0)
  224. return -EINVAL;
  225. return request_irq(irq, mxs_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
  226. "MXS Timer Tick", &mxs_clockevent_device);
  227. }
  228. TIMER_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init);