time.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * linux/arch/arm/plat-mxc/time.c
  3. *
  4. * Copyright (C) 2000-2001 Deep Blue Solutions
  5. * Copyright (C) 2002 Shane Nay (shane@minirl.com)
  6. * Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
  7. * Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. * MA 02110-1301, USA.
  22. */
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/clk.h>
  27. #include <mach/hardware.h>
  28. #include <asm/mach/time.h>
  29. #include <mach/common.h>
  30. #include <mach/mxc_timer.h>
  31. static struct clock_event_device clockevent_mxc;
  32. static enum clock_event_mode clockevent_mode = CLOCK_EVT_MODE_UNUSED;
  33. /* clock source */
  34. static cycle_t mxc_get_cycles(struct clocksource *cs)
  35. {
  36. return __raw_readl(TIMER_BASE + MXC_TCN);
  37. }
  38. static struct clocksource clocksource_mxc = {
  39. .name = "mxc_timer1",
  40. .rating = 200,
  41. .read = mxc_get_cycles,
  42. .mask = CLOCKSOURCE_MASK(32),
  43. .shift = 20,
  44. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  45. };
  46. static int __init mxc_clocksource_init(struct clk *timer_clk)
  47. {
  48. unsigned int c = clk_get_rate(timer_clk);
  49. clocksource_mxc.mult = clocksource_hz2mult(c,
  50. clocksource_mxc.shift);
  51. clocksource_register(&clocksource_mxc);
  52. return 0;
  53. }
  54. /* clock event */
  55. static int mxc_set_next_event(unsigned long evt,
  56. struct clock_event_device *unused)
  57. {
  58. unsigned long tcmp;
  59. tcmp = __raw_readl(TIMER_BASE + MXC_TCN) + evt;
  60. __raw_writel(tcmp, TIMER_BASE + MXC_TCMP);
  61. return (int)(tcmp - __raw_readl(TIMER_BASE + MXC_TCN)) < 0 ?
  62. -ETIME : 0;
  63. }
  64. #ifdef DEBUG
  65. static const char *clock_event_mode_label[] = {
  66. [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC",
  67. [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT",
  68. [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN",
  69. [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED"
  70. };
  71. #endif /* DEBUG */
  72. static void mxc_set_mode(enum clock_event_mode mode,
  73. struct clock_event_device *evt)
  74. {
  75. unsigned long flags;
  76. /*
  77. * The timer interrupt generation is disabled at least
  78. * for enough time to call mxc_set_next_event()
  79. */
  80. local_irq_save(flags);
  81. /* Disable interrupt in GPT module */
  82. gpt_irq_disable();
  83. if (mode != clockevent_mode) {
  84. /* Set event time into far-far future */
  85. __raw_writel(__raw_readl(TIMER_BASE + MXC_TCN) - 3,
  86. TIMER_BASE + MXC_TCMP);
  87. /* Clear pending interrupt */
  88. gpt_irq_acknowledge();
  89. }
  90. #ifdef DEBUG
  91. printk(KERN_INFO "mxc_set_mode: changing mode from %s to %s\n",
  92. clock_event_mode_label[clockevent_mode],
  93. clock_event_mode_label[mode]);
  94. #endif /* DEBUG */
  95. /* Remember timer mode */
  96. clockevent_mode = mode;
  97. local_irq_restore(flags);
  98. switch (mode) {
  99. case CLOCK_EVT_MODE_PERIODIC:
  100. printk(KERN_ERR"mxc_set_mode: Periodic mode is not "
  101. "supported for i.MX\n");
  102. break;
  103. case CLOCK_EVT_MODE_ONESHOT:
  104. /*
  105. * Do not put overhead of interrupt enable/disable into
  106. * mxc_set_next_event(), the core has about 4 minutes
  107. * to call mxc_set_next_event() or shutdown clock after
  108. * mode switching
  109. */
  110. local_irq_save(flags);
  111. gpt_irq_enable();
  112. local_irq_restore(flags);
  113. break;
  114. case CLOCK_EVT_MODE_SHUTDOWN:
  115. case CLOCK_EVT_MODE_UNUSED:
  116. case CLOCK_EVT_MODE_RESUME:
  117. /* Left event sources disabled, no more interrupts appear */
  118. break;
  119. }
  120. }
  121. /*
  122. * IRQ handler for the timer
  123. */
  124. static irqreturn_t mxc_timer_interrupt(int irq, void *dev_id)
  125. {
  126. struct clock_event_device *evt = &clockevent_mxc;
  127. uint32_t tstat;
  128. tstat = __raw_readl(TIMER_BASE + MXC_TSTAT);
  129. gpt_irq_acknowledge();
  130. evt->event_handler(evt);
  131. return IRQ_HANDLED;
  132. }
  133. static struct irqaction mxc_timer_irq = {
  134. .name = "i.MX Timer Tick",
  135. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  136. .handler = mxc_timer_interrupt,
  137. };
  138. static struct clock_event_device clockevent_mxc = {
  139. .name = "mxc_timer1",
  140. .features = CLOCK_EVT_FEAT_ONESHOT,
  141. .shift = 32,
  142. .set_mode = mxc_set_mode,
  143. .set_next_event = mxc_set_next_event,
  144. .rating = 200,
  145. };
  146. static int __init mxc_clockevent_init(struct clk *timer_clk)
  147. {
  148. unsigned int c = clk_get_rate(timer_clk);
  149. clockevent_mxc.mult = div_sc(c, NSEC_PER_SEC,
  150. clockevent_mxc.shift);
  151. clockevent_mxc.max_delta_ns =
  152. clockevent_delta2ns(0xfffffffe, &clockevent_mxc);
  153. clockevent_mxc.min_delta_ns =
  154. clockevent_delta2ns(0xff, &clockevent_mxc);
  155. clockevent_mxc.cpumask = cpumask_of(0);
  156. clockevents_register_device(&clockevent_mxc);
  157. return 0;
  158. }
  159. void __init mxc_timer_init(struct clk *timer_clk)
  160. {
  161. clk_enable(timer_clk);
  162. /*
  163. * Initialise to a known state (all timers off, and timing reset)
  164. */
  165. __raw_writel(0, TIMER_BASE + MXC_TCTL);
  166. __raw_writel(0, TIMER_BASE + MXC_TPRER); /* see datasheet note */
  167. __raw_writel(TCTL_FRR | /* free running */
  168. TCTL_VAL | /* set clocksource and arch specific bits */
  169. TCTL_TEN, /* start the timer */
  170. TIMER_BASE + MXC_TCTL);
  171. /* init and register the timer to the framework */
  172. mxc_clocksource_init(timer_clk);
  173. mxc_clockevent_init(timer_clk);
  174. /* Make irqs happen */
  175. setup_irq(TIMER_INTERRUPT, &mxc_timer_irq);
  176. }