timer-mtu2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * arch/sh/kernel/timers/timer-mtu2.c - MTU2 Timer Support
  3. *
  4. * Copyright (C) 2005 Paul Mundt
  5. *
  6. * Based off of arch/sh/kernel/timers/timer-tmu.c
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/seqlock.h>
  17. #include <asm/timer.h>
  18. #include <asm/io.h>
  19. #include <asm/irq.h>
  20. #include <asm/clock.h>
  21. /*
  22. * We use channel 1 for our lowly system timer. Channel 2 would be the other
  23. * likely candidate, but we leave it alone as it has higher divisors that
  24. * would be of more use to other more interesting applications.
  25. *
  26. * TODO: Presently we only implement a 16-bit single-channel system timer.
  27. * However, we can implement channel cascade if we go the overflow route and
  28. * get away with using 2 MTU2 channels as a 32-bit timer.
  29. */
  30. static DEFINE_SPINLOCK(mtu2_lock);
  31. #define MTU2_TSTR 0xfffe4280
  32. #define MTU2_TCR_1 0xfffe4380
  33. #define MTU2_TMDR_1 0xfffe4381
  34. #define MTU2_TIOR_1 0xfffe4382
  35. #define MTU2_TIER_1 0xfffe4384
  36. #define MTU2_TSR_1 0xfffe4385
  37. #define MTU2_TCNT_1 0xfffe4386 /* 16-bit counter */
  38. #define MTU2_TGRA_1 0xfffe438a
  39. #define STBCR3 0xfffe0408
  40. #define MTU2_TSTR_CST1 (1 << 1) /* Counter Start 1 */
  41. #define MTU2_TSR_TGFA (1 << 0) /* GRA compare match */
  42. #define MTU2_TIER_TGIEA (1 << 0) /* GRA compare match interrupt enable */
  43. #define MTU2_TCR_INIT 0x22
  44. #define MTU2_TCR_CALIB 0x00
  45. static unsigned long mtu2_timer_get_offset(void)
  46. {
  47. int count;
  48. unsigned long flags;
  49. static int count_p = 0x7fff; /* for the first call after boot */
  50. static unsigned long jiffies_p = 0;
  51. /*
  52. * cache volatile jiffies temporarily; we have IRQs turned off.
  53. */
  54. unsigned long jiffies_t;
  55. spin_lock_irqsave(&mtu2_lock, flags);
  56. /* timer count may underflow right here */
  57. count = ctrl_inw(MTU2_TCNT_1); /* read the latched count */
  58. jiffies_t = jiffies;
  59. /*
  60. * avoiding timer inconsistencies (they are rare, but they happen)...
  61. * there is one kind of problem that must be avoided here:
  62. * 1. the timer counter underflows
  63. */
  64. if (jiffies_t == jiffies_p) {
  65. if (count > count_p) {
  66. if (ctrl_inb(MTU2_TSR_1) & MTU2_TSR_TGFA) {
  67. count -= LATCH;
  68. } else {
  69. printk("%s (): hardware timer problem?\n",
  70. __FUNCTION__);
  71. }
  72. }
  73. } else
  74. jiffies_p = jiffies_t;
  75. count_p = count;
  76. spin_unlock_irqrestore(&mtu2_lock, flags);
  77. count = ((LATCH-1) - count) * TICK_SIZE;
  78. count = (count + LATCH/2) / LATCH;
  79. return count;
  80. }
  81. static irqreturn_t mtu2_timer_interrupt(int irq, void *dev_id,
  82. struct pt_regs *regs)
  83. {
  84. unsigned long timer_status;
  85. /* Clear TGFA bit */
  86. timer_status = ctrl_inb(MTU2_TSR_1);
  87. timer_status &= ~MTU2_TSR_TGFA;
  88. ctrl_outb(timer_status, MTU2_TSR_1);
  89. /* Do timer tick */
  90. write_seqlock(&xtime_lock);
  91. handle_timer_tick(regs);
  92. write_sequnlock(&xtime_lock);
  93. return IRQ_HANDLED;
  94. }
  95. static struct irqaction mtu2_irq = {
  96. .name = "timer",
  97. .handler = mtu2_timer_interrupt,
  98. .flags = SA_INTERRUPT,
  99. .mask = CPU_MASK_NONE,
  100. };
  101. /*
  102. * Hah! We'll see if this works (switching from usecs to nsecs).
  103. */
  104. static unsigned long mtu2_timer_get_frequency(void)
  105. {
  106. u32 freq;
  107. struct timespec ts1, ts2;
  108. unsigned long diff_nsec;
  109. unsigned long factor;
  110. /* Setup the timer: We don't want to generate interrupts, just
  111. * have it count down at its natural rate.
  112. */
  113. ctrl_outb(ctrl_inb(MTU2_TSTR) & ~MTU2_TSTR_CST1, MTU2_TSTR);
  114. ctrl_outb(MTU2_TCR_CALIB, MTU2_TCR_1);
  115. ctrl_outb(ctrl_inb(MTU2_TIER_1) & ~MTU2_TIER_TGIEA, MTU2_TIER_1);
  116. ctrl_outw(0, MTU2_TCNT_1);
  117. rtc_get_time(&ts2);
  118. do {
  119. rtc_get_time(&ts1);
  120. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  121. /* actually start the timer */
  122. ctrl_outw(ctrl_inw(CMT_CMSTR) | 0x01, CMT_CMSTR);
  123. do {
  124. rtc_get_time(&ts2);
  125. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  126. freq = ctrl_inw(MTU2_TCNT_0);
  127. if (ts2.tv_nsec < ts1.tv_nsec) {
  128. ts2.tv_nsec += 1000000000;
  129. ts2.tv_sec--;
  130. }
  131. diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
  132. /* this should work well if the RTC has a precision of n Hz, where
  133. * n is an integer. I don't think we have to worry about the other
  134. * cases. */
  135. factor = (1000000000 + diff_nsec/2) / diff_nsec;
  136. if (factor * diff_nsec > 1100000000 ||
  137. factor * diff_nsec < 900000000)
  138. panic("weird RTC (diff_nsec %ld)", diff_nsec);
  139. return freq * factor;
  140. }
  141. static unsigned int divisors[] = { 1, 4, 16, 64, 1, 1, 256 };
  142. static void mtu2_clk_init(struct clk *clk)
  143. {
  144. u8 idx = MTU2_TCR_INIT & 0x7;
  145. clk->rate = clk->parent->rate / divisors[idx];
  146. /* Start TCNT counting */
  147. ctrl_outb(ctrl_inb(MTU2_TSTR) | MTU2_TSTR_CST1, MTU2_TSTR);
  148. }
  149. static void mtu2_clk_recalc(struct clk *clk)
  150. {
  151. u8 idx = ctrl_inb(MTU2_TCR_1) & 0x7;
  152. clk->rate = clk->parent->rate / divisors[idx];
  153. }
  154. static struct clk_ops mtu2_clk_ops = {
  155. .init = mtu2_clk_init,
  156. .recalc = mtu2_clk_recalc,
  157. };
  158. static struct clk mtu2_clk1 = {
  159. .name = "mtu2_clk1",
  160. .ops = &mtu2_clk_ops,
  161. };
  162. static int mtu2_timer_start(void)
  163. {
  164. ctrl_outb(ctrl_inb(MTU2_TSTR) | MTU2_TSTR_CST1, MTU2_TSTR);
  165. return 0;
  166. }
  167. static int mtu2_timer_stop(void)
  168. {
  169. ctrl_outb(ctrl_inb(MTU2_TSTR) & ~MTU2_TSTR_CST1, MTU2_TSTR);
  170. return 0;
  171. }
  172. static int mtu2_timer_init(void)
  173. {
  174. u8 tmp;
  175. unsigned long interval;
  176. setup_irq(TIMER_IRQ, &mtu2_irq);
  177. mtu2_clk1.parent = clk_get("module_clk");
  178. ctrl_outb(ctrl_inb(STBCR3) & (~0x20), STBCR3);
  179. /* Normal operation */
  180. ctrl_outb(0, MTU2_TMDR_1);
  181. ctrl_outb(MTU2_TCR_INIT, MTU2_TCR_1);
  182. ctrl_outb(0x01, MTU2_TIOR_1);
  183. /* Enable underflow interrupt */
  184. ctrl_outb(ctrl_inb(MTU2_TIER_1) | MTU2_TIER_TGIEA, MTU2_TIER_1);
  185. interval = CONFIG_SH_PCLK_FREQ / 16 / HZ;
  186. printk(KERN_INFO "Interval = %ld\n", interval);
  187. ctrl_outw(interval, MTU2_TGRA_1);
  188. ctrl_outw(0, MTU2_TCNT_1);
  189. clk_register(&mtu2_clk1);
  190. clk_enable(&mtu2_clk1);
  191. return 0;
  192. }
  193. struct sys_timer_ops mtu2_timer_ops = {
  194. .init = mtu2_timer_init,
  195. .start = mtu2_timer_start,
  196. .stop = mtu2_timer_stop,
  197. .get_frequency = mtu2_timer_get_frequency,
  198. .get_offset = mtu2_timer_get_offset,
  199. };
  200. struct sys_timer mtu2_timer = {
  201. .name = "mtu2",
  202. .ops = &mtu2_timer_ops,
  203. };