timer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2000-2001 Deep Blue Solutions
  3. * Copyright (C) 2002 Shane Nay (shane@minirl.com)
  4. * Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
  5. * Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de)
  6. * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. * MA 02110-1301, USA.
  21. */
  22. #include <linux/err.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/clk.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <asm/mach/time.h>
  31. #include <asm/sched_clock.h>
  32. #include <mach/mxs.h>
  33. #include <mach/common.h>
  34. /*
  35. * There are 2 versions of the timrot on Freescale MXS-based SoCs.
  36. * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
  37. * extends the counter to 32 bits.
  38. *
  39. * The implementation uses two timers, one for clock_event and
  40. * another for clocksource. MX28 uses timrot 0 and 1, while MX23
  41. * uses 0 and 2.
  42. */
  43. #define MX23_TIMROT_VERSION_OFFSET 0x0a0
  44. #define MX28_TIMROT_VERSION_OFFSET 0x120
  45. #define BP_TIMROT_MAJOR_VERSION 24
  46. #define BV_TIMROT_VERSION_1 0x01
  47. #define BV_TIMROT_VERSION_2 0x02
  48. #define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1)
  49. /*
  50. * There are 4 registers for each timrotv2 instance, and 2 registers
  51. * for each timrotv1. So address step 0x40 in macros below strides
  52. * one instance of timrotv2 while two instances of timrotv1.
  53. *
  54. * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
  55. * on MX28 while timrot2 on MX23.
  56. */
  57. /* common between v1 and v2 */
  58. #define HW_TIMROT_ROTCTRL 0x00
  59. #define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40)
  60. /* v1 only */
  61. #define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40)
  62. /* v2 only */
  63. #define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40)
  64. #define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40)
  65. #define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6)
  66. #define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7)
  67. #define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14)
  68. #define BM_TIMROT_TIMCTRLn_IRQ (1 << 15)
  69. #define BP_TIMROT_TIMCTRLn_SELECT 0
  70. #define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8
  71. #define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb
  72. #define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS 0xf
  73. static struct clock_event_device mxs_clockevent_device;
  74. static enum clock_event_mode mxs_clockevent_mode = CLOCK_EVT_MODE_UNUSED;
  75. static void __iomem *mxs_timrot_base;
  76. static u32 timrot_major_version;
  77. static inline void timrot_irq_disable(void)
  78. {
  79. __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ_EN,
  80. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  81. }
  82. static inline void timrot_irq_enable(void)
  83. {
  84. __mxs_setl(BM_TIMROT_TIMCTRLn_IRQ_EN,
  85. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  86. }
  87. static void timrot_irq_acknowledge(void)
  88. {
  89. __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ,
  90. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  91. }
  92. static cycle_t timrotv1_get_cycles(struct clocksource *cs)
  93. {
  94. return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
  95. & 0xffff0000) >> 16);
  96. }
  97. static int timrotv1_set_next_event(unsigned long evt,
  98. struct clock_event_device *dev)
  99. {
  100. /* timrot decrements the count */
  101. __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
  102. return 0;
  103. }
  104. static int timrotv2_set_next_event(unsigned long evt,
  105. struct clock_event_device *dev)
  106. {
  107. /* timrot decrements the count */
  108. __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
  109. return 0;
  110. }
  111. static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
  112. {
  113. struct clock_event_device *evt = dev_id;
  114. timrot_irq_acknowledge();
  115. evt->event_handler(evt);
  116. return IRQ_HANDLED;
  117. }
  118. static struct irqaction mxs_timer_irq = {
  119. .name = "MXS Timer Tick",
  120. .dev_id = &mxs_clockevent_device,
  121. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  122. .handler = mxs_timer_interrupt,
  123. };
  124. #ifdef DEBUG
  125. static const char *clock_event_mode_label[] const = {
  126. [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
  127. [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT",
  128. [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
  129. [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED"
  130. };
  131. #endif /* DEBUG */
  132. static void mxs_set_mode(enum clock_event_mode mode,
  133. struct clock_event_device *evt)
  134. {
  135. /* Disable interrupt in timer module */
  136. timrot_irq_disable();
  137. if (mode != mxs_clockevent_mode) {
  138. /* Set event time into the furthest future */
  139. if (timrot_is_v1())
  140. __raw_writel(0xffff,
  141. mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  142. else
  143. __raw_writel(0xffffffff,
  144. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  145. /* Clear pending interrupt */
  146. timrot_irq_acknowledge();
  147. }
  148. #ifdef DEBUG
  149. pr_info("%s: changing mode from %s to %s\n", __func__,
  150. clock_event_mode_label[mxs_clockevent_mode],
  151. clock_event_mode_label[mode]);
  152. #endif /* DEBUG */
  153. /* Remember timer mode */
  154. mxs_clockevent_mode = mode;
  155. switch (mode) {
  156. case CLOCK_EVT_MODE_PERIODIC:
  157. pr_err("%s: Periodic mode is not implemented\n", __func__);
  158. break;
  159. case CLOCK_EVT_MODE_ONESHOT:
  160. timrot_irq_enable();
  161. break;
  162. case CLOCK_EVT_MODE_SHUTDOWN:
  163. case CLOCK_EVT_MODE_UNUSED:
  164. case CLOCK_EVT_MODE_RESUME:
  165. /* Left event sources disabled, no more interrupts appear */
  166. break;
  167. }
  168. }
  169. static struct clock_event_device mxs_clockevent_device = {
  170. .name = "mxs_timrot",
  171. .features = CLOCK_EVT_FEAT_ONESHOT,
  172. .set_mode = mxs_set_mode,
  173. .set_next_event = timrotv2_set_next_event,
  174. .rating = 200,
  175. };
  176. static int __init mxs_clockevent_init(struct clk *timer_clk)
  177. {
  178. if (timrot_is_v1())
  179. mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
  180. mxs_clockevent_device.cpumask = cpumask_of(0);
  181. clockevents_config_and_register(&mxs_clockevent_device,
  182. clk_get_rate(timer_clk),
  183. timrot_is_v1() ? 0xf : 0x2,
  184. timrot_is_v1() ? 0xfffe : 0xfffffffe);
  185. return 0;
  186. }
  187. static struct clocksource clocksource_mxs = {
  188. .name = "mxs_timer",
  189. .rating = 200,
  190. .read = timrotv1_get_cycles,
  191. .mask = CLOCKSOURCE_MASK(16),
  192. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  193. };
  194. static u32 notrace mxs_read_sched_clock_v2(void)
  195. {
  196. return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
  197. }
  198. static int __init mxs_clocksource_init(struct clk *timer_clk)
  199. {
  200. unsigned int c = clk_get_rate(timer_clk);
  201. if (timrot_is_v1())
  202. clocksource_register_hz(&clocksource_mxs, c);
  203. else {
  204. clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
  205. "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
  206. setup_sched_clock(mxs_read_sched_clock_v2, 32, c);
  207. }
  208. return 0;
  209. }
  210. static void __init mxs_timer_init(struct device_node *np)
  211. {
  212. struct clk *timer_clk;
  213. int irq;
  214. mxs_timrot_base = of_iomap(np, 0);
  215. WARN_ON(!mxs_timrot_base);
  216. timer_clk = of_clk_get(np, 0);
  217. if (IS_ERR(timer_clk)) {
  218. pr_err("%s: failed to get clk\n", __func__);
  219. return;
  220. }
  221. clk_prepare_enable(timer_clk);
  222. /*
  223. * Initialize timers to a known state
  224. */
  225. mxs_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
  226. /* get timrot version */
  227. timrot_major_version = __raw_readl(mxs_timrot_base +
  228. (of_device_is_compatible(np, "fsl,imx23-timrot") ?
  229. MX23_TIMROT_VERSION_OFFSET :
  230. MX28_TIMROT_VERSION_OFFSET));
  231. timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
  232. /* one for clock_event */
  233. __raw_writel((timrot_is_v1() ?
  234. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  235. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  236. BM_TIMROT_TIMCTRLn_UPDATE |
  237. BM_TIMROT_TIMCTRLn_IRQ_EN,
  238. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  239. /* another for clocksource */
  240. __raw_writel((timrot_is_v1() ?
  241. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  242. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  243. BM_TIMROT_TIMCTRLn_RELOAD,
  244. mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
  245. /* set clocksource timer fixed count to the maximum */
  246. if (timrot_is_v1())
  247. __raw_writel(0xffff,
  248. mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  249. else
  250. __raw_writel(0xffffffff,
  251. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  252. /* init and register the timer to the framework */
  253. mxs_clocksource_init(timer_clk);
  254. mxs_clockevent_init(timer_clk);
  255. /* Make irqs happen */
  256. irq = irq_of_parse_and_map(np, 0);
  257. setup_irq(irq, &mxs_timer_irq);
  258. }
  259. CLOCKSOURCE_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init)