timer-tmu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * arch/sh/kernel/timers/timer-tmu.c - TMU Timer Support
  3. *
  4. * Copyright (C) 2005 Paul Mundt
  5. *
  6. * TMU handling code hacked out of arch/sh/kernel/time.c
  7. *
  8. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  9. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  10. * Copyright (C) 2002, 2003, 2004 Paul Mundt
  11. * Copyright (C) 2002 M. R. Brown <mrbrown@linux-sh.org>
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/seqlock.h>
  22. #include <asm/timer.h>
  23. #include <asm/rtc.h>
  24. #include <asm/io.h>
  25. #include <asm/irq.h>
  26. #include <asm/clock.h>
  27. #define TMU_TOCR_INIT 0x00
  28. #define TMU0_TCR_INIT 0x0020
  29. #define TMU_TSTR_INIT 1
  30. #define TMU0_TCR_CALIB 0x0000
  31. static DEFINE_SPINLOCK(tmu0_lock);
  32. static unsigned long tmu_timer_get_offset(void)
  33. {
  34. int count;
  35. unsigned long flags;
  36. static int count_p = 0x7fffffff; /* for the first call after boot */
  37. static unsigned long jiffies_p = 0;
  38. /*
  39. * cache volatile jiffies temporarily; we have IRQs turned off.
  40. */
  41. unsigned long jiffies_t;
  42. spin_lock_irqsave(&tmu0_lock, flags);
  43. /* timer count may underflow right here */
  44. count = ctrl_inl(TMU0_TCNT); /* read the latched count */
  45. jiffies_t = jiffies;
  46. /*
  47. * avoiding timer inconsistencies (they are rare, but they happen)...
  48. * there is one kind of problem that must be avoided here:
  49. * 1. the timer counter underflows
  50. */
  51. if (jiffies_t == jiffies_p) {
  52. if (count > count_p) {
  53. /* the nutcase */
  54. if (ctrl_inw(TMU0_TCR) & 0x100) { /* Check UNF bit */
  55. count -= LATCH;
  56. } else {
  57. printk("%s (): hardware timer problem?\n",
  58. __FUNCTION__);
  59. }
  60. }
  61. } else
  62. jiffies_p = jiffies_t;
  63. count_p = count;
  64. spin_unlock_irqrestore(&tmu0_lock, flags);
  65. count = ((LATCH-1) - count) * TICK_SIZE;
  66. count = (count + LATCH/2) / LATCH;
  67. return count;
  68. }
  69. static irqreturn_t tmu_timer_interrupt(int irq, void *dev_id,
  70. struct pt_regs *regs)
  71. {
  72. unsigned long timer_status;
  73. /* Clear UNF bit */
  74. timer_status = ctrl_inw(TMU0_TCR);
  75. timer_status &= ~0x100;
  76. ctrl_outw(timer_status, TMU0_TCR);
  77. /*
  78. * Here we are in the timer irq handler. We just have irqs locally
  79. * disabled but we don't know if the timer_bh is running on the other
  80. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  81. * the irq version of write_lock because as just said we have irq
  82. * locally disabled. -arca
  83. */
  84. write_seqlock(&xtime_lock);
  85. handle_timer_tick(regs);
  86. write_sequnlock(&xtime_lock);
  87. return IRQ_HANDLED;
  88. }
  89. static struct irqaction tmu_irq = {
  90. .name = "timer",
  91. .handler = tmu_timer_interrupt,
  92. .flags = SA_INTERRUPT,
  93. .mask = CPU_MASK_NONE,
  94. };
  95. /*
  96. * Hah! We'll see if this works (switching from usecs to nsecs).
  97. */
  98. static unsigned long tmu_timer_get_frequency(void)
  99. {
  100. u32 freq;
  101. struct timespec ts1, ts2;
  102. unsigned long diff_nsec;
  103. unsigned long factor;
  104. /* Setup the timer: We don't want to generate interrupts, just
  105. * have it count down at its natural rate.
  106. */
  107. ctrl_outb(0, TMU_TSTR);
  108. #if !defined(CONFIG_CPU_SUBTYPE_SH7300) && !defined(CONFIG_CPU_SUBTYPE_SH7760)
  109. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  110. #endif
  111. ctrl_outw(TMU0_TCR_CALIB, TMU0_TCR);
  112. ctrl_outl(0xffffffff, TMU0_TCOR);
  113. ctrl_outl(0xffffffff, TMU0_TCNT);
  114. rtc_get_time(&ts2);
  115. do {
  116. rtc_get_time(&ts1);
  117. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  118. /* actually start the timer */
  119. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  120. do {
  121. rtc_get_time(&ts2);
  122. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  123. freq = 0xffffffff - ctrl_inl(TMU0_TCNT);
  124. if (ts2.tv_nsec < ts1.tv_nsec) {
  125. ts2.tv_nsec += 1000000000;
  126. ts2.tv_sec--;
  127. }
  128. diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
  129. /* this should work well if the RTC has a precision of n Hz, where
  130. * n is an integer. I don't think we have to worry about the other
  131. * cases. */
  132. factor = (1000000000 + diff_nsec/2) / diff_nsec;
  133. if (factor * diff_nsec > 1100000000 ||
  134. factor * diff_nsec < 900000000)
  135. panic("weird RTC (diff_nsec %ld)", diff_nsec);
  136. return freq * factor;
  137. }
  138. static void tmu_clk_init(struct clk *clk)
  139. {
  140. u8 divisor = TMU0_TCR_INIT & 0x7;
  141. ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
  142. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  143. }
  144. static void tmu_clk_recalc(struct clk *clk)
  145. {
  146. u8 divisor = ctrl_inw(TMU0_TCR) & 0x7;
  147. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  148. }
  149. static struct clk_ops tmu_clk_ops = {
  150. .init = tmu_clk_init,
  151. .recalc = tmu_clk_recalc,
  152. };
  153. static struct clk tmu0_clk = {
  154. .name = "tmu0_clk",
  155. .ops = &tmu_clk_ops,
  156. };
  157. static int tmu_timer_init(void)
  158. {
  159. unsigned long interval;
  160. setup_irq(TIMER_IRQ, &tmu_irq);
  161. tmu0_clk.parent = clk_get("module_clk");
  162. /* Start TMU0 */
  163. ctrl_outb(0, TMU_TSTR);
  164. #if !defined(CONFIG_CPU_SUBTYPE_SH7300) && !defined(CONFIG_CPU_SUBTYPE_SH7760)
  165. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  166. #endif
  167. clk_register(&tmu0_clk);
  168. clk_enable(&tmu0_clk);
  169. interval = (clk_get_rate(&tmu0_clk) + HZ / 2) / HZ;
  170. printk(KERN_INFO "Interval = %ld\n", interval);
  171. ctrl_outl(interval, TMU0_TCOR);
  172. ctrl_outl(interval, TMU0_TCNT);
  173. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  174. return 0;
  175. }
  176. struct sys_timer_ops tmu_timer_ops = {
  177. .init = tmu_timer_init,
  178. .get_frequency = tmu_timer_get_frequency,
  179. .get_offset = tmu_timer_get_offset,
  180. };
  181. struct sys_timer tmu_timer = {
  182. .name = "tmu",
  183. .ops = &tmu_timer_ops,
  184. };