epit.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * linux/arch/arm/plat-mxc/epit.c
  3. *
  4. * Copyright (C) 2010 Sascha Hauer <s.hauer@pengutronix.de>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #define EPITCR 0x00
  21. #define EPITSR 0x04
  22. #define EPITLR 0x08
  23. #define EPITCMPR 0x0c
  24. #define EPITCNR 0x10
  25. #define EPITCR_EN (1 << 0)
  26. #define EPITCR_ENMOD (1 << 1)
  27. #define EPITCR_OCIEN (1 << 2)
  28. #define EPITCR_RLD (1 << 3)
  29. #define EPITCR_PRESC(x) (((x) & 0xfff) << 4)
  30. #define EPITCR_SWR (1 << 16)
  31. #define EPITCR_IOVW (1 << 17)
  32. #define EPITCR_DBGEN (1 << 18)
  33. #define EPITCR_WAITEN (1 << 19)
  34. #define EPITCR_RES (1 << 20)
  35. #define EPITCR_STOPEN (1 << 21)
  36. #define EPITCR_OM_DISCON (0 << 22)
  37. #define EPITCR_OM_TOGGLE (1 << 22)
  38. #define EPITCR_OM_CLEAR (2 << 22)
  39. #define EPITCR_OM_SET (3 << 22)
  40. #define EPITCR_CLKSRC_OFF (0 << 24)
  41. #define EPITCR_CLKSRC_PERIPHERAL (1 << 24)
  42. #define EPITCR_CLKSRC_REF_HIGH (1 << 24)
  43. #define EPITCR_CLKSRC_REF_LOW (3 << 24)
  44. #define EPITSR_OCIF (1 << 0)
  45. #include <linux/interrupt.h>
  46. #include <linux/irq.h>
  47. #include <linux/clockchips.h>
  48. #include <linux/clk.h>
  49. #include <mach/hardware.h>
  50. #include <asm/mach/time.h>
  51. #include <mach/common.h>
  52. static struct clock_event_device clockevent_epit;
  53. static enum clock_event_mode clockevent_mode = CLOCK_EVT_MODE_UNUSED;
  54. static void __iomem *timer_base;
  55. static inline void epit_irq_disable(void)
  56. {
  57. u32 val;
  58. val = __raw_readl(timer_base + EPITCR);
  59. val &= ~EPITCR_OCIEN;
  60. __raw_writel(val, timer_base + EPITCR);
  61. }
  62. static inline void epit_irq_enable(void)
  63. {
  64. u32 val;
  65. val = __raw_readl(timer_base + EPITCR);
  66. val |= EPITCR_OCIEN;
  67. __raw_writel(val, timer_base + EPITCR);
  68. }
  69. static void epit_irq_acknowledge(void)
  70. {
  71. __raw_writel(EPITSR_OCIF, timer_base + EPITSR);
  72. }
  73. static cycle_t epit_read(struct clocksource *cs)
  74. {
  75. return 0 - __raw_readl(timer_base + EPITCNR);
  76. }
  77. static struct clocksource clocksource_epit = {
  78. .name = "epit",
  79. .rating = 200,
  80. .read = epit_read,
  81. .mask = CLOCKSOURCE_MASK(32),
  82. .shift = 20,
  83. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  84. };
  85. static int __init epit_clocksource_init(struct clk *timer_clk)
  86. {
  87. unsigned int c = clk_get_rate(timer_clk);
  88. clocksource_epit.mult = clocksource_hz2mult(c,
  89. clocksource_epit.shift);
  90. clocksource_register(&clocksource_epit);
  91. return 0;
  92. }
  93. /* clock event */
  94. static int epit_set_next_event(unsigned long evt,
  95. struct clock_event_device *unused)
  96. {
  97. unsigned long tcmp;
  98. tcmp = __raw_readl(timer_base + EPITCNR);
  99. __raw_writel(tcmp - evt, timer_base + EPITCMPR);
  100. return 0;
  101. }
  102. static void epit_set_mode(enum clock_event_mode mode,
  103. struct clock_event_device *evt)
  104. {
  105. unsigned long flags;
  106. /*
  107. * The timer interrupt generation is disabled at least
  108. * for enough time to call epit_set_next_event()
  109. */
  110. local_irq_save(flags);
  111. /* Disable interrupt in GPT module */
  112. epit_irq_disable();
  113. if (mode != clockevent_mode) {
  114. /* Set event time into far-far future */
  115. /* Clear pending interrupt */
  116. epit_irq_acknowledge();
  117. }
  118. /* Remember timer mode */
  119. clockevent_mode = mode;
  120. local_irq_restore(flags);
  121. switch (mode) {
  122. case CLOCK_EVT_MODE_PERIODIC:
  123. printk(KERN_ERR "epit_set_mode: Periodic mode is not "
  124. "supported for i.MX EPIT\n");
  125. break;
  126. case CLOCK_EVT_MODE_ONESHOT:
  127. /*
  128. * Do not put overhead of interrupt enable/disable into
  129. * epit_set_next_event(), the core has about 4 minutes
  130. * to call epit_set_next_event() or shutdown clock after
  131. * mode switching
  132. */
  133. local_irq_save(flags);
  134. epit_irq_enable();
  135. local_irq_restore(flags);
  136. break;
  137. case CLOCK_EVT_MODE_SHUTDOWN:
  138. case CLOCK_EVT_MODE_UNUSED:
  139. case CLOCK_EVT_MODE_RESUME:
  140. /* Left event sources disabled, no more interrupts appear */
  141. break;
  142. }
  143. }
  144. /*
  145. * IRQ handler for the timer
  146. */
  147. static irqreturn_t epit_timer_interrupt(int irq, void *dev_id)
  148. {
  149. struct clock_event_device *evt = &clockevent_epit;
  150. epit_irq_acknowledge();
  151. evt->event_handler(evt);
  152. return IRQ_HANDLED;
  153. }
  154. static struct irqaction epit_timer_irq = {
  155. .name = "i.MX EPIT Timer Tick",
  156. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  157. .handler = epit_timer_interrupt,
  158. };
  159. static struct clock_event_device clockevent_epit = {
  160. .name = "epit",
  161. .features = CLOCK_EVT_FEAT_ONESHOT,
  162. .shift = 32,
  163. .set_mode = epit_set_mode,
  164. .set_next_event = epit_set_next_event,
  165. .rating = 200,
  166. };
  167. static int __init epit_clockevent_init(struct clk *timer_clk)
  168. {
  169. unsigned int c = clk_get_rate(timer_clk);
  170. clockevent_epit.mult = div_sc(c, NSEC_PER_SEC,
  171. clockevent_epit.shift);
  172. clockevent_epit.max_delta_ns =
  173. clockevent_delta2ns(0xfffffffe, &clockevent_epit);
  174. clockevent_epit.min_delta_ns =
  175. clockevent_delta2ns(0x800, &clockevent_epit);
  176. clockevent_epit.cpumask = cpumask_of(0);
  177. clockevents_register_device(&clockevent_epit);
  178. return 0;
  179. }
  180. void __init epit_timer_init(struct clk *timer_clk, void __iomem *base, int irq)
  181. {
  182. clk_enable(timer_clk);
  183. timer_base = base;
  184. /*
  185. * Initialise to a known state (all timers off, and timing reset)
  186. */
  187. __raw_writel(0x0, timer_base + EPITCR);
  188. __raw_writel(0xffffffff, timer_base + EPITLR);
  189. __raw_writel(EPITCR_EN | EPITCR_CLKSRC_REF_HIGH | EPITCR_WAITEN,
  190. timer_base + EPITCR);
  191. /* init and register the timer to the framework */
  192. epit_clocksource_init(timer_clk);
  193. epit_clockevent_init(timer_clk);
  194. /* Make irqs happen */
  195. setup_irq(irq, &epit_timer_irq);
  196. }