timer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_irq.h>
  29. #include <asm/mach/time.h>
  30. #include <asm/sched_clock.h>
  31. #include <mach/mxs.h>
  32. #include <mach/common.h>
  33. /*
  34. * There are 2 versions of the timrot on Freescale MXS-based SoCs.
  35. * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
  36. * extends the counter to 32 bits.
  37. *
  38. * The implementation uses two timers, one for clock_event and
  39. * another for clocksource. MX28 uses timrot 0 and 1, while MX23
  40. * uses 0 and 2.
  41. */
  42. #define MX23_TIMROT_VERSION_OFFSET 0x0a0
  43. #define MX28_TIMROT_VERSION_OFFSET 0x120
  44. #define BP_TIMROT_MAJOR_VERSION 24
  45. #define BV_TIMROT_VERSION_1 0x01
  46. #define BV_TIMROT_VERSION_2 0x02
  47. #define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1)
  48. /*
  49. * There are 4 registers for each timrotv2 instance, and 2 registers
  50. * for each timrotv1. So address step 0x40 in macros below strides
  51. * one instance of timrotv2 while two instances of timrotv1.
  52. *
  53. * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
  54. * on MX28 while timrot2 on MX23.
  55. */
  56. /* common between v1 and v2 */
  57. #define HW_TIMROT_ROTCTRL 0x00
  58. #define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40)
  59. /* v1 only */
  60. #define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40)
  61. /* v2 only */
  62. #define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40)
  63. #define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40)
  64. #define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6)
  65. #define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7)
  66. #define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14)
  67. #define BM_TIMROT_TIMCTRLn_IRQ (1 << 15)
  68. #define BP_TIMROT_TIMCTRLn_SELECT 0
  69. #define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8
  70. #define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb
  71. #define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS 0xf
  72. static struct clock_event_device mxs_clockevent_device;
  73. static enum clock_event_mode mxs_clockevent_mode = CLOCK_EVT_MODE_UNUSED;
  74. static void __iomem *mxs_timrot_base = MXS_IO_ADDRESS(MXS_TIMROT_BASE_ADDR);
  75. static u32 timrot_major_version;
  76. static inline void timrot_irq_disable(void)
  77. {
  78. __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ_EN,
  79. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  80. }
  81. static inline void timrot_irq_enable(void)
  82. {
  83. __mxs_setl(BM_TIMROT_TIMCTRLn_IRQ_EN,
  84. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  85. }
  86. static void timrot_irq_acknowledge(void)
  87. {
  88. __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ,
  89. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  90. }
  91. static cycle_t timrotv1_get_cycles(struct clocksource *cs)
  92. {
  93. return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
  94. & 0xffff0000) >> 16);
  95. }
  96. static int timrotv1_set_next_event(unsigned long evt,
  97. struct clock_event_device *dev)
  98. {
  99. /* timrot decrements the count */
  100. __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
  101. return 0;
  102. }
  103. static int timrotv2_set_next_event(unsigned long evt,
  104. struct clock_event_device *dev)
  105. {
  106. /* timrot decrements the count */
  107. __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
  108. return 0;
  109. }
  110. static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
  111. {
  112. struct clock_event_device *evt = dev_id;
  113. timrot_irq_acknowledge();
  114. evt->event_handler(evt);
  115. return IRQ_HANDLED;
  116. }
  117. static struct irqaction mxs_timer_irq = {
  118. .name = "MXS Timer Tick",
  119. .dev_id = &mxs_clockevent_device,
  120. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  121. .handler = mxs_timer_interrupt,
  122. };
  123. #ifdef DEBUG
  124. static const char *clock_event_mode_label[] const = {
  125. [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
  126. [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT",
  127. [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
  128. [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED"
  129. };
  130. #endif /* DEBUG */
  131. static void mxs_set_mode(enum clock_event_mode mode,
  132. struct clock_event_device *evt)
  133. {
  134. /* Disable interrupt in timer module */
  135. timrot_irq_disable();
  136. if (mode != mxs_clockevent_mode) {
  137. /* Set event time into the furthest future */
  138. if (timrot_is_v1())
  139. __raw_writel(0xffff,
  140. mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  141. else
  142. __raw_writel(0xffffffff,
  143. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  144. /* Clear pending interrupt */
  145. timrot_irq_acknowledge();
  146. }
  147. #ifdef DEBUG
  148. pr_info("%s: changing mode from %s to %s\n", __func__,
  149. clock_event_mode_label[mxs_clockevent_mode],
  150. clock_event_mode_label[mode]);
  151. #endif /* DEBUG */
  152. /* Remember timer mode */
  153. mxs_clockevent_mode = mode;
  154. switch (mode) {
  155. case CLOCK_EVT_MODE_PERIODIC:
  156. pr_err("%s: Periodic mode is not implemented\n", __func__);
  157. break;
  158. case CLOCK_EVT_MODE_ONESHOT:
  159. timrot_irq_enable();
  160. break;
  161. case CLOCK_EVT_MODE_SHUTDOWN:
  162. case CLOCK_EVT_MODE_UNUSED:
  163. case CLOCK_EVT_MODE_RESUME:
  164. /* Left event sources disabled, no more interrupts appear */
  165. break;
  166. }
  167. }
  168. static struct clock_event_device mxs_clockevent_device = {
  169. .name = "mxs_timrot",
  170. .features = CLOCK_EVT_FEAT_ONESHOT,
  171. .set_mode = mxs_set_mode,
  172. .set_next_event = timrotv2_set_next_event,
  173. .rating = 200,
  174. };
  175. static int __init mxs_clockevent_init(struct clk *timer_clk)
  176. {
  177. if (timrot_is_v1())
  178. mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
  179. mxs_clockevent_device.cpumask = cpumask_of(0);
  180. clockevents_config_and_register(&mxs_clockevent_device,
  181. clk_get_rate(timer_clk),
  182. timrot_is_v1() ? 0xf : 0x2,
  183. timrot_is_v1() ? 0xfffe : 0xfffffffe);
  184. return 0;
  185. }
  186. static struct clocksource clocksource_mxs = {
  187. .name = "mxs_timer",
  188. .rating = 200,
  189. .read = timrotv1_get_cycles,
  190. .mask = CLOCKSOURCE_MASK(16),
  191. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  192. };
  193. static u32 notrace mxs_read_sched_clock_v2(void)
  194. {
  195. return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
  196. }
  197. static int __init mxs_clocksource_init(struct clk *timer_clk)
  198. {
  199. unsigned int c = clk_get_rate(timer_clk);
  200. if (timrot_is_v1())
  201. clocksource_register_hz(&clocksource_mxs, c);
  202. else {
  203. clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
  204. "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
  205. setup_sched_clock(mxs_read_sched_clock_v2, 32, c);
  206. }
  207. return 0;
  208. }
  209. void __init mxs_timer_init(void)
  210. {
  211. struct device_node *np;
  212. struct clk *timer_clk;
  213. int irq;
  214. np = of_find_compatible_node(NULL, NULL, "fsl,timrot");
  215. if (!np) {
  216. pr_err("%s: failed find timrot node\n", __func__);
  217. return;
  218. }
  219. timer_clk = clk_get_sys("timrot", NULL);
  220. if (IS_ERR(timer_clk)) {
  221. pr_err("%s: failed to get clk\n", __func__);
  222. return;
  223. }
  224. clk_prepare_enable(timer_clk);
  225. /*
  226. * Initialize timers to a known state
  227. */
  228. mxs_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
  229. /* get timrot version */
  230. timrot_major_version = __raw_readl(mxs_timrot_base +
  231. (cpu_is_mx23() ? MX23_TIMROT_VERSION_OFFSET :
  232. MX28_TIMROT_VERSION_OFFSET));
  233. timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
  234. /* one for clock_event */
  235. __raw_writel((timrot_is_v1() ?
  236. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  237. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  238. BM_TIMROT_TIMCTRLn_UPDATE |
  239. BM_TIMROT_TIMCTRLn_IRQ_EN,
  240. mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
  241. /* another for clocksource */
  242. __raw_writel((timrot_is_v1() ?
  243. BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
  244. BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
  245. BM_TIMROT_TIMCTRLn_RELOAD,
  246. mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
  247. /* set clocksource timer fixed count to the maximum */
  248. if (timrot_is_v1())
  249. __raw_writel(0xffff,
  250. mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
  251. else
  252. __raw_writel(0xffffffff,
  253. mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
  254. /* init and register the timer to the framework */
  255. mxs_clocksource_init(timer_clk);
  256. mxs_clockevent_init(timer_clk);
  257. /* Make irqs happen */
  258. irq = irq_of_parse_and_map(np, 0);
  259. setup_irq(irq, &mxs_timer_irq);
  260. }