timer.c 8.4 KB

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