mxs_timer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright (C) 2000-2001 Deep Blue Solutions
  4. // Copyright (C) 2002 Shane Nay (shane@minirl.com)
  5. // Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
  6. // Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de)
  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 struct irqaction mxs_timer_irq = {
  102. .name = "MXS Timer Tick",
  103. .dev_id = &mxs_clockevent_device,
  104. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  105. .handler = mxs_timer_interrupt,
  106. };
  107. static void mxs_irq_clear(char *state)
  108. {
  109. /* Disable interrupt in timer module */
  110. timrot_irq_disable();
  111. /* Set event time into the furthest future */
  112. if (timrot_is_v1())
  113. __raw_writel(0xffff, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  114. else
  115. __raw_writel(0xffffffff,
  116. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  117. /* Clear pending interrupt */
  118. timrot_irq_acknowledge();
  119. #ifdef DEBUG
  120. pr_info("%s: changing mode to %s\n", __func__, state)
  121. #endif /* DEBUG */
  122. }
  123. static int mxs_shutdown(struct clock_event_device *evt)
  124. {
  125. mxs_irq_clear("shutdown");
  126. return 0;
  127. }
  128. static int mxs_set_oneshot(struct clock_event_device *evt)
  129. {
  130. if (clockevent_state_oneshot(evt))
  131. mxs_irq_clear("oneshot");
  132. timrot_irq_enable();
  133. return 0;
  134. }
  135. static struct clock_event_device mxs_clockevent_device = {
  136. .name = "mxs_timrot",
  137. .features = CLOCK_EVT_FEAT_ONESHOT,
  138. .set_state_shutdown = mxs_shutdown,
  139. .set_state_oneshot = mxs_set_oneshot,
  140. .tick_resume = mxs_shutdown,
  141. .set_next_event = timrotv2_set_next_event,
  142. .rating = 200,
  143. };
  144. static int __init mxs_clockevent_init(struct clk *timer_clk)
  145. {
  146. if (timrot_is_v1())
  147. mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
  148. mxs_clockevent_device.cpumask = cpumask_of(0);
  149. clockevents_config_and_register(&mxs_clockevent_device,
  150. clk_get_rate(timer_clk),
  151. timrot_is_v1() ? 0xf : 0x2,
  152. timrot_is_v1() ? 0xfffe : 0xfffffffe);
  153. return 0;
  154. }
  155. static struct clocksource clocksource_mxs = {
  156. .name = "mxs_timer",
  157. .rating = 200,
  158. .read = timrotv1_get_cycles,
  159. .mask = CLOCKSOURCE_MASK(16),
  160. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  161. };
  162. static u64 notrace mxs_read_sched_clock_v2(void)
  163. {
  164. return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
  165. }
  166. static int __init mxs_clocksource_init(struct clk *timer_clk)
  167. {
  168. unsigned int c = clk_get_rate(timer_clk);
  169. if (timrot_is_v1())
  170. clocksource_register_hz(&clocksource_mxs, c);
  171. else {
  172. clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
  173. "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
  174. sched_clock_register(mxs_read_sched_clock_v2, 32, c);
  175. }
  176. return 0;
  177. }
  178. static int __init mxs_timer_init(struct device_node *np)
  179. {
  180. struct clk *timer_clk;
  181. int irq, ret;
  182. mxs_timrot_base = of_iomap(np, 0);
  183. WARN_ON(!mxs_timrot_base);
  184. timer_clk = of_clk_get(np, 0);
  185. if (IS_ERR(timer_clk)) {
  186. pr_err("%s: failed to get clk\n", __func__);
  187. return PTR_ERR(timer_clk);
  188. }
  189. ret = clk_prepare_enable(timer_clk);
  190. if (ret)
  191. return ret;
  192. /*
  193. * Initialize timers to a known state
  194. */
  195. stmp_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
  196. /* get timrot version */
  197. timrot_major_version = __raw_readl(mxs_timrot_base +
  198. (of_device_is_compatible(np, "fsl,imx23-timrot") ?
  199. MX23_TIMROT_VERSION_OFFSET :
  200. MX28_TIMROT_VERSION_OFFSET));
  201. timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
  202. /* one for clock_event */
  203. __raw_writel((timrot_is_v1() ?
  204. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  205. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  206. BM_TIMROT_TIMCTRLn_UPDATE |
  207. BM_TIMROT_TIMCTRLn_IRQ_EN,
  208. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  209. /* another for clocksource */
  210. __raw_writel((timrot_is_v1() ?
  211. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  212. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  213. BM_TIMROT_TIMCTRLn_RELOAD,
  214. mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
  215. /* set clocksource timer fixed count to the maximum */
  216. if (timrot_is_v1())
  217. __raw_writel(0xffff,
  218. mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  219. else
  220. __raw_writel(0xffffffff,
  221. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  222. /* init and register the timer to the framework */
  223. ret = mxs_clocksource_init(timer_clk);
  224. if (ret)
  225. return ret;
  226. ret = mxs_clockevent_init(timer_clk);
  227. if (ret)
  228. return ret;
  229. /* Make irqs happen */
  230. irq = irq_of_parse_and_map(np, 0);
  231. if (irq <= 0)
  232. return -EINVAL;
  233. return setup_irq(irq, &mxs_timer_irq);
  234. }
  235. TIMER_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init);