timer-cmt.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. {
  85. unsigned long timer_status;
  86. /* Clear CMF bit */
  87. timer_status = ctrl_inw(CMT_CMCSR_0);
  88. timer_status &= ~0x80;
  89. ctrl_outw(timer_status, CMT_CMCSR_0);
  90. /*
  91. * Here we are in the timer irq handler. We just have irqs locally
  92. * disabled but we don't know if the timer_bh is running on the other
  93. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  94. * the irq version of write_lock because as just said we have irq
  95. * locally disabled. -arca
  96. */
  97. write_seqlock(&xtime_lock);
  98. handle_timer_tick();
  99. write_sequnlock(&xtime_lock);
  100. return IRQ_HANDLED;
  101. }
  102. static struct irqaction cmt_irq = {
  103. .name = "timer",
  104. .handler = cmt_timer_interrupt,
  105. .flags = IRQF_DISABLED,
  106. .mask = CPU_MASK_NONE,
  107. };
  108. static void cmt_clk_init(struct clk *clk)
  109. {
  110. u8 divisor = CMT_CMCSR_INIT & 0x3;
  111. ctrl_inw(CMT_CMCSR_0);
  112. ctrl_outw(CMT_CMCSR_INIT, CMT_CMCSR_0);
  113. clk->parent = clk_get("module_clk");
  114. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  115. }
  116. static void cmt_clk_recalc(struct clk *clk)
  117. {
  118. u8 divisor = ctrl_inw(CMT_CMCSR_0) & 0x3;
  119. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  120. }
  121. static struct clk_ops cmt_clk_ops = {
  122. .init = cmt_clk_init,
  123. .recalc = cmt_clk_recalc,
  124. };
  125. static struct clk cmt0_clk = {
  126. .name = "cmt0_clk",
  127. .ops = &cmt_clk_ops,
  128. };
  129. static int cmt_timer_start(void)
  130. {
  131. ctrl_outw(ctrl_inw(CMT_CMSTR) | 0x01, CMT_CMSTR);
  132. return 0;
  133. }
  134. static int cmt_timer_stop(void)
  135. {
  136. ctrl_outw(ctrl_inw(CMT_CMSTR) & ~0x01, CMT_CMSTR);
  137. return 0;
  138. }
  139. static int cmt_timer_init(void)
  140. {
  141. unsigned long interval;
  142. cmt_clock_enable();
  143. setup_irq(TIMER_IRQ, &cmt_irq);
  144. cmt0_clk.parent = clk_get("module_clk");
  145. cmt_timer_stop();
  146. interval = cmt0_clk.parent->rate / 8 / HZ;
  147. printk(KERN_INFO "Interval = %ld\n", interval);
  148. ctrl_outw(interval, CMT_CMCOR_0);
  149. clk_register(&cmt0_clk);
  150. clk_enable(&cmt0_clk);
  151. cmt_timer_start();
  152. return 0;
  153. }
  154. struct sys_timer_ops cmt_timer_ops = {
  155. .init = cmt_timer_init,
  156. .start = cmt_timer_start,
  157. .stop = cmt_timer_stop,
  158. #ifndef CONFIG_GENERIC_TIME
  159. .get_offset = cmt_timer_get_offset,
  160. #endif
  161. };
  162. struct sys_timer cmt_timer = {
  163. .name = "cmt",
  164. .ops = &cmt_timer_ops,
  165. };