timer.c 8.7 KB

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