timer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  82. };
  83. static struct irqaction stmp3xxx_timer_irq = {
  84. .name = "stmp3xxx_timer",
  85. .flags = IRQF_DISABLED | IRQF_TIMER,
  86. .handler = stmp3xxx_timer_interrupt,
  87. .dev_id = &ckevt_timrot,
  88. };
  89. /*
  90. * Set up timer interrupt, and return the current time in seconds.
  91. */
  92. static void __init stmp3xxx_init_timer(void)
  93. {
  94. ckevt_timrot.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC,
  95. ckevt_timrot.shift);
  96. ckevt_timrot.min_delta_ns = clockevent_delta2ns(2, &ckevt_timrot);
  97. ckevt_timrot.max_delta_ns = clockevent_delta2ns(0xFFF, &ckevt_timrot);
  98. ckevt_timrot.cpumask = cpumask_of(0);
  99. stmp3xxx_reset_block(REGS_TIMROT_BASE, false);
  100. /* clear two timers */
  101. __raw_writel(0, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  102. __raw_writel(0, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  103. /* configure them */
  104. __raw_writel(
  105. (8 << BP_TIMROT_TIMCTRLn_SELECT) | /* 32 kHz */
  106. BM_TIMROT_TIMCTRLn_RELOAD |
  107. BM_TIMROT_TIMCTRLn_UPDATE |
  108. BM_TIMROT_TIMCTRLn_IRQ_EN,
  109. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  110. __raw_writel(
  111. (8 << BP_TIMROT_TIMCTRLn_SELECT) | /* 32 kHz */
  112. BM_TIMROT_TIMCTRLn_RELOAD |
  113. BM_TIMROT_TIMCTRLn_UPDATE |
  114. BM_TIMROT_TIMCTRLn_IRQ_EN,
  115. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1);
  116. __raw_writel(CLOCK_TICK_RATE / HZ - 1,
  117. REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  118. __raw_writel(0xFFFF, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  119. setup_irq(IRQ_TIMER0, &stmp3xxx_timer_irq);
  120. clocksource_register_hz(&cksrc_stmp3xxx, CLOCK_TICK_RATE);
  121. clockevents_register_device(&ckevt_timrot);
  122. }
  123. #ifdef CONFIG_PM
  124. void stmp3xxx_suspend_timer(void)
  125. {
  126. stmp3xxx_clearl(BM_TIMROT_TIMCTRLn_IRQ_EN | BM_TIMROT_TIMCTRLn_IRQ,
  127. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  128. stmp3xxx_setl(BM_TIMROT_ROTCTRL_CLKGATE,
  129. REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);
  130. }
  131. void stmp3xxx_resume_timer(void)
  132. {
  133. stmp3xxx_clearl(BM_TIMROT_ROTCTRL_SFTRST | BM_TIMROT_ROTCTRL_CLKGATE,
  134. REGS_TIMROT_BASE + HW_TIMROT_ROTCTRL);
  135. __raw_writel(
  136. 8 << BP_TIMROT_TIMCTRLn_SELECT | /* 32 kHz */
  137. BM_TIMROT_TIMCTRLn_RELOAD |
  138. BM_TIMROT_TIMCTRLn_UPDATE |
  139. BM_TIMROT_TIMCTRLn_IRQ_EN,
  140. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL0);
  141. __raw_writel(
  142. 8 << BP_TIMROT_TIMCTRLn_SELECT | /* 32 kHz */
  143. BM_TIMROT_TIMCTRLn_RELOAD |
  144. BM_TIMROT_TIMCTRLn_UPDATE |
  145. BM_TIMROT_TIMCTRLn_IRQ_EN,
  146. REGS_TIMROT_BASE + HW_TIMROT_TIMCTRL1);
  147. __raw_writel(CLOCK_TICK_RATE / HZ - 1,
  148. REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT0);
  149. __raw_writel(0xFFFF, REGS_TIMROT_BASE + HW_TIMROT_TIMCOUNT1);
  150. }
  151. #else
  152. #define stmp3xxx_suspend_timer NULL
  153. #define stmp3xxx_resume_timer NULL
  154. #endif /* CONFIG_PM */
  155. struct sys_timer stmp3xxx_timer = {
  156. .init = stmp3xxx_init_timer,
  157. .suspend = stmp3xxx_suspend_timer,
  158. .resume = stmp3xxx_resume_timer,
  159. };