timer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * System timer for Freescale STMP37XX/STMP378X
  3. *
  4. * Embedded Alley Solutions, Inc <source@embeddedalley.com>
  5. *
  6. * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
  7. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  8. */
  9. /*
  10. * The code contained herein is licensed under the GNU General Public
  11. * License. You may obtain a copy of the GNU General Public License
  12. * Version 2 or later at the following locations:
  13. *
  14. * http://www.opensource.org/licenses/gpl-license.html
  15. * http://www.gnu.org/copyleft/gpl.html
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/clocksource.h>
  21. #include <linux/clockchips.h>
  22. #include <linux/io.h>
  23. #include <linux/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <asm/mach/time.h>
  26. #include <mach/stmp3xxx.h>
  27. #include <mach/platform.h>
  28. #include <mach/regs-timrot.h>
  29. static irqreturn_t
  30. stmp3xxx_timer_interrupt(int irq, void *dev_id)
  31. {
  32. struct clock_event_device *c = dev_id;
  33. /* timer 0 */
  34. if (__raw_readl(REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0) &
  35. BM_TIMROT_TIMCTRLn_IRQ) {
  36. stmp3xxx_clearl(BM_TIMROT_TIMCTRLn_IRQ,
  37. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  38. c->event_handler(c);
  39. }
  40. /* timer 1 */
  41. else if (__raw_readl(REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1)
  42. & BM_TIMROT_TIMCTRLn_IRQ) {
  43. stmp3xxx_clearl(BM_TIMROT_TIMCTRLn_IRQ,
  44. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1);
  45. stmp3xxx_clearl(BM_TIMROT_TIMCTRLn_IRQ_EN,
  46. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1);
  47. __raw_writel(0xFFFF, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  48. }
  49. return IRQ_HANDLED;
  50. }
  51. static cycle_t stmp3xxx_clock_read(struct clocksource *cs)
  52. {
  53. return ~((__raw_readl(REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1)
  54. & 0xFFFF0000) >> 16);
  55. }
  56. static int
  57. stmp3xxx_timrot_set_next_event(unsigned long delta,
  58. struct clock_event_device *dev)
  59. {
  60. /* reload the timer */
  61. __raw_writel(delta, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  62. return 0;
  63. }
  64. static void
  65. stmp3xxx_timrot_set_mode(enum clock_event_mode mode,
  66. struct clock_event_device *dev)
  67. {
  68. }
  69. static struct clock_event_device ckevt_timrot = {
  70. .name = "timrot",
  71. .features = CLOCK_EVT_FEAT_ONESHOT,
  72. .shift = 32,
  73. .set_next_event = stmp3xxx_timrot_set_next_event,
  74. .set_mode = stmp3xxx_timrot_set_mode,
  75. };
  76. static struct clocksource cksrc_stmp3xxx = {
  77. .name = "cksrc_stmp3xxx",
  78. .rating = 250,
  79. .read = stmp3xxx_clock_read,
  80. .mask = CLOCKSOURCE_MASK(16),
  81. .shift = 10,
  82. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  83. };
  84. static struct irqaction stmp3xxx_timer_irq = {
  85. .name = "stmp3xxx_timer",
  86. .flags = IRQF_DISABLED | IRQF_TIMER,
  87. .handler = stmp3xxx_timer_interrupt,
  88. .dev_id = &ckevt_timrot,
  89. };
  90. /*
  91. * Set up timer interrupt, and return the current time in seconds.
  92. */
  93. static void __init stmp3xxx_init_timer(void)
  94. {
  95. cksrc_stmp3xxx.mult = clocksource_hz2mult(CLOCK_TICK_RATE,
  96. cksrc_stmp3xxx.shift);
  97. ckevt_timrot.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC,
  98. ckevt_timrot.shift);
  99. ckevt_timrot.min_delta_ns = clockevent_delta2ns(2, &ckevt_timrot);
  100. ckevt_timrot.max_delta_ns = clockevent_delta2ns(0xFFF, &ckevt_timrot);
  101. ckevt_timrot.cpumask = cpumask_of(0);
  102. stmp3xxx_reset_block(REGS_TIMROT_BASE, false);
  103. /* clear two timers */
  104. __raw_writel(0, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  105. __raw_writel(0, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  106. /* configure them */
  107. __raw_writel(
  108. (8 << BP_TIMROT_TIMCTRLn_SELECT) | /* 32 kHz */
  109. BM_TIMROT_TIMCTRLn_RELOAD |
  110. BM_TIMROT_TIMCTRLn_UPDATE |
  111. BM_TIMROT_TIMCTRLn_IRQ_EN,
  112. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  113. __raw_writel(
  114. (8 << BP_TIMROT_TIMCTRLn_SELECT) | /* 32 kHz */
  115. BM_TIMROT_TIMCTRLn_RELOAD |
  116. BM_TIMROT_TIMCTRLn_UPDATE |
  117. BM_TIMROT_TIMCTRLn_IRQ_EN,
  118. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1);
  119. __raw_writel(CLOCK_TICK_RATE / HZ - 1,
  120. REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  121. __raw_writel(0xFFFF, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  122. setup_irq(IRQ_TIMER0, &stmp3xxx_timer_irq);
  123. clocksource_register(&cksrc_stmp3xxx);
  124. clockevents_register_device(&ckevt_timrot);
  125. }
  126. #ifdef CONFIG_PM
  127. void stmp3xxx_suspend_timer(void)
  128. {
  129. stmp3xxx_clearl(BM_TIMROT_TIMCTRLn_IRQ_EN | BM_TIMROT_TIMCTRLn_IRQ,
  130. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  131. stmp3xxx_setl(BM_TIMROT_ROTCTRL_CLKGATE,
  132. REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);
  133. }
  134. void stmp3xxx_resume_timer(void)
  135. {
  136. stmp3xxx_clearl(BM_TIMROT_ROTCTRL_SFTRST | BM_TIMROT_ROTCTRL_CLKGATE,
  137. REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);
  138. __raw_writel(
  139. 8 << BP_TIMROT_TIMCTRLn_SELECT | /* 32 kHz */
  140. BM_TIMROT_TIMCTRLn_RELOAD |
  141. BM_TIMROT_TIMCTRLn_UPDATE |
  142. BM_TIMROT_TIMCTRLn_IRQ_EN,
  143. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  144. __raw_writel(
  145. 8 << BP_TIMROT_TIMCTRLn_SELECT | /* 32 kHz */
  146. BM_TIMROT_TIMCTRLn_RELOAD |
  147. BM_TIMROT_TIMCTRLn_UPDATE |
  148. BM_TIMROT_TIMCTRLn_IRQ_EN,
  149. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1);
  150. __raw_writel(CLOCK_TICK_RATE / HZ - 1,
  151. REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  152. __raw_writel(0xFFFF, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  153. }
  154. #else
  155. #define stmp3xxx_suspend_timer NULL
  156. #define stmp3xxx_resume_timer NULL
  157. #endif /* CONFIG_PM */
  158. struct sys_timer stmp3xxx_timer = {
  159. .init = stmp3xxx_init_timer,
  160. .suspend = stmp3xxx_suspend_timer,
  161. .resume = stmp3xxx_resume_timer,
  162. };