timer-cmt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * arch/sh/kernel/timers/timer-cmt.c - CMT Timer Support
  3. *
  4. * Copyright (C) 2005 Yoshinori Sato
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/seqlock.h>
  15. #include <asm/timer.h>
  16. #include <asm/rtc.h>
  17. #include <asm/io.h>
  18. #include <asm/irq.h>
  19. #include <asm/clock.h>
  20. #if defined(CONFIG_CPU_SUBTYPE_SH7619)
  21. #define CMT_CMSTR 0xf84a0070
  22. #define CMT_CMCSR_0 0xf84a0072
  23. #define CMT_CMCNT_0 0xf84a0074
  24. #define CMT_CMCOR_0 0xf84a0076
  25. #define CMT_CMCSR_1 0xf84a0078
  26. #define CMT_CMCNT_1 0xf84a007a
  27. #define CMT_CMCOR_1 0xf84a007c
  28. #define STBCR3 0xf80a0000
  29. #define cmt_clock_enable() do { ctrl_outb(ctrl_inb(STBCR3) & ~0x10, STBCR3); } while(0)
  30. #define CMT_CMCSR_INIT 0x0040
  31. #define CMT_CMCSR_CALIB 0x0000
  32. #elif defined(CONFIG_CPU_SUBTYPE_SH7206)
  33. #define CMT_CMSTR 0xfffec000
  34. #define CMT_CMCSR_0 0xfffec002
  35. #define CMT_CMCNT_0 0xfffec004
  36. #define CMT_CMCOR_0 0xfffec006
  37. #define STBCR4 0xfffe040c
  38. #define cmt_clock_enable() do { ctrl_outb(ctrl_inb(STBCR4) & ~0x04, STBCR4); } while(0)
  39. #define CMT_CMCSR_INIT 0x0040
  40. #define CMT_CMCSR_CALIB 0x0000
  41. #else
  42. #error "Unknown CPU SUBTYPE"
  43. #endif
  44. static DEFINE_SPINLOCK(cmt0_lock);
  45. static unsigned long cmt_timer_get_offset(void)
  46. {
  47. int count;
  48. unsigned long flags;
  49. static unsigned short count_p = 0xffff; /* 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(&cmt0_lock, flags);
  56. /* timer count may underflow right here */
  57. count = ctrl_inw(CMT_CMCOR_0);
  58. count -= ctrl_inw(CMT_CMCNT_0);
  59. jiffies_t = jiffies;
  60. /*
  61. * avoiding timer inconsistencies (they are rare, but they happen)...
  62. * there is one kind of problem that must be avoided here:
  63. * 1. the timer counter underflows
  64. */
  65. if (jiffies_t == jiffies_p) {
  66. if (count > count_p) {
  67. /* the nutcase */
  68. if (ctrl_inw(CMT_CMCSR_0) & 0x80) { /* Check CMF bit */
  69. count -= LATCH;
  70. } else {
  71. printk("%s (): hardware timer problem?\n",
  72. __FUNCTION__);
  73. }
  74. }
  75. } else
  76. jiffies_p = jiffies_t;
  77. count_p = count;
  78. spin_unlock_irqrestore(&cmt0_lock, flags);
  79. count = ((LATCH-1) - count) * TICK_SIZE;
  80. count = (count + LATCH/2) / LATCH;
  81. return count;
  82. }
  83. static irqreturn_t cmt_timer_interrupt(int irq, void *dev_id,
  84. struct pt_regs *regs)
  85. {
  86. unsigned long timer_status;
  87. /* Clear CMF bit */
  88. timer_status = ctrl_inw(CMT_CMCSR_0);
  89. timer_status &= ~0x80;
  90. ctrl_outw(timer_status, CMT_CMCSR_0);
  91. /*
  92. * Here we are in the timer irq handler. We just have irqs locally
  93. * disabled but we don't know if the timer_bh is running on the other
  94. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  95. * the irq version of write_lock because as just said we have irq
  96. * locally disabled. -arca
  97. */
  98. write_seqlock(&xtime_lock);
  99. handle_timer_tick(regs);
  100. write_sequnlock(&xtime_lock);
  101. return IRQ_HANDLED;
  102. }
  103. static struct irqaction cmt_irq = {
  104. .name = "timer",
  105. .handler = cmt_timer_interrupt,
  106. .flags = SA_INTERRUPT,
  107. .mask = CPU_MASK_NONE,
  108. };
  109. /*
  110. * Hah! We'll see if this works (switching from usecs to nsecs).
  111. */
  112. static unsigned long cmt_timer_get_frequency(void)
  113. {
  114. u32 freq;
  115. struct timespec ts1, ts2;
  116. unsigned long diff_nsec;
  117. unsigned long factor;
  118. /* Setup the timer: We don't want to generate interrupts, just
  119. * have it count down at its natural rate.
  120. */
  121. ctrl_outw(ctrl_inw(CMT_CMSTR) & ~0x01, CMT_CMSTR);
  122. ctrl_outw(CMT_CMCSR_CALIB, CMT_CMCSR_0);
  123. ctrl_outw(0xffff, CMT_CMCOR_0);
  124. ctrl_outw(0xffff, CMT_CMCNT_0);
  125. rtc_sh_get_time(&ts2);
  126. do {
  127. rtc_sh_get_time(&ts1);
  128. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  129. /* actually start the timer */
  130. ctrl_outw(ctrl_inw(CMT_CMSTR) | 0x01, CMT_CMSTR);
  131. do {
  132. rtc_sh_get_time(&ts2);
  133. } while (ts1.tv_nsec == ts2.tv_nsec && ts1.tv_sec == ts2.tv_sec);
  134. freq = 0xffff - ctrl_inw(CMT_CMCNT_0);
  135. if (ts2.tv_nsec < ts1.tv_nsec) {
  136. ts2.tv_nsec += 1000000000;
  137. ts2.tv_sec--;
  138. }
  139. diff_nsec = (ts2.tv_sec - ts1.tv_sec) * 1000000000 + (ts2.tv_nsec - ts1.tv_nsec);
  140. /* this should work well if the RTC has a precision of n Hz, where
  141. * n is an integer. I don't think we have to worry about the other
  142. * cases. */
  143. factor = (1000000000 + diff_nsec/2) / diff_nsec;
  144. if (factor * diff_nsec > 1100000000 ||
  145. factor * diff_nsec < 900000000)
  146. panic("weird RTC (diff_nsec %ld)", diff_nsec);
  147. return freq * factor;
  148. }
  149. static void cmt_clk_init(struct clk *clk)
  150. {
  151. u8 divisor = CMT_CMCSR_INIT & 0x3;
  152. ctrl_inw(CMT_CMCSR_0);
  153. ctrl_outw(CMT_CMCSR_INIT, CMT_CMCSR_0);
  154. clk->parent = clk_get("module_clk");
  155. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  156. }
  157. static void cmt_clk_recalc(struct clk *clk)
  158. {
  159. u8 divisor = ctrl_inw(CMT_CMCSR_0) & 0x3;
  160. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  161. }
  162. static struct clk_ops cmt_clk_ops = {
  163. .init = cmt_clk_init,
  164. .recalc = cmt_clk_recalc,
  165. };
  166. static struct clk cmt0_clk = {
  167. .name = "cmt0_clk",
  168. .ops = &cmt_clk_ops,
  169. };
  170. static int cmt_timer_start(void)
  171. {
  172. ctrl_outw(ctrl_inw(CMT_CMSTR) | 0x01, CMT_CMSTR);
  173. return 0;
  174. }
  175. static int cmt_timer_stop(void)
  176. {
  177. ctrl_outw(ctrl_inw(CMT_CMSTR) & ~0x01, CMT_CMSTR);
  178. return 0;
  179. }
  180. static int cmt_timer_init(void)
  181. {
  182. unsigned long interval;
  183. cmt_clock_enable();
  184. setup_irq(TIMER_IRQ, &cmt_irq);
  185. cmt0_clk.parent = clk_get("module_clk");
  186. cmt_timer_stop();
  187. interval = cmt0_clk.parent->rate / 8 / HZ;
  188. printk(KERN_INFO "Interval = %ld\n", interval);
  189. ctrl_outw(interval, CMT_CMCOR_0);
  190. clk_register(&cmt0_clk);
  191. clk_enable(&cmt0_clk);
  192. cmt_timer_start();
  193. return 0;
  194. }
  195. struct sys_timer_ops cmt_timer_ops = {
  196. .init = cmt_timer_init,
  197. .start = cmt_timer_start,
  198. .stop = cmt_timer_stop,
  199. .get_frequency = cmt_timer_get_frequency,
  200. .get_offset = cmt_timer_get_offset,
  201. };
  202. struct sys_timer cmt_timer = {
  203. .name = "cmt",
  204. .ops = &cmt_timer_ops,
  205. };