timer-cmt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/seqlock.h>
  14. #include <asm/timer.h>
  15. #include <asm/rtc.h>
  16. #include <asm/io.h>
  17. #include <asm/irq.h>
  18. #include <asm/clock.h>
  19. #if defined(CONFIG_CPU_SUBTYPE_SH7619)
  20. #define CMT_CMSTR 0xf84a0070
  21. #define CMT_CMCSR_0 0xf84a0072
  22. #define CMT_CMCNT_0 0xf84a0074
  23. #define CMT_CMCOR_0 0xf84a0076
  24. #define CMT_CMCSR_1 0xf84a0078
  25. #define CMT_CMCNT_1 0xf84a007a
  26. #define CMT_CMCOR_1 0xf84a007c
  27. #define STBCR3 0xf80a0000
  28. #define cmt_clock_enable() do { ctrl_outb(ctrl_inb(STBCR3) & ~0x10, STBCR3); } while(0)
  29. #define CMT_CMCSR_INIT 0x0040
  30. #define CMT_CMCSR_CALIB 0x0000
  31. #elif defined(CONFIG_CPU_SUBTYPE_SH7206)
  32. #define CMT_CMSTR 0xfffec000
  33. #define CMT_CMCSR_0 0xfffec002
  34. #define CMT_CMCNT_0 0xfffec004
  35. #define CMT_CMCOR_0 0xfffec006
  36. #define STBCR4 0xfffe040c
  37. #define cmt_clock_enable() do { ctrl_outb(ctrl_inb(STBCR4) & ~0x04, STBCR4); } while(0)
  38. #define CMT_CMCSR_INIT 0x0040
  39. #define CMT_CMCSR_CALIB 0x0000
  40. #else
  41. #error "Unknown CPU SUBTYPE"
  42. #endif
  43. static unsigned long cmt_timer_get_offset(void)
  44. {
  45. int count;
  46. static unsigned short count_p = 0xffff; /* for the first call after boot */
  47. static unsigned long jiffies_p = 0;
  48. /*
  49. * cache volatile jiffies temporarily; we have IRQs turned off.
  50. */
  51. unsigned long jiffies_t;
  52. /* timer count may underflow right here */
  53. count = ctrl_inw(CMT_CMCOR_0);
  54. count -= ctrl_inw(CMT_CMCNT_0);
  55. jiffies_t = jiffies;
  56. /*
  57. * avoiding timer inconsistencies (they are rare, but they happen)...
  58. * there is one kind of problem that must be avoided here:
  59. * 1. the timer counter underflows
  60. */
  61. if (jiffies_t == jiffies_p) {
  62. if (count > count_p) {
  63. /* the nutcase */
  64. if (ctrl_inw(CMT_CMCSR_0) & 0x80) { /* Check CMF bit */
  65. count -= LATCH;
  66. } else {
  67. printk("%s (): hardware timer problem?\n",
  68. __FUNCTION__);
  69. }
  70. }
  71. } else
  72. jiffies_p = jiffies_t;
  73. count_p = count;
  74. count = ((LATCH-1) - count) * TICK_SIZE;
  75. count = (count + LATCH/2) / LATCH;
  76. return count;
  77. }
  78. static irqreturn_t cmt_timer_interrupt(int irq, void *dev_id)
  79. {
  80. unsigned long timer_status;
  81. /* Clear CMF bit */
  82. timer_status = ctrl_inw(CMT_CMCSR_0);
  83. timer_status &= ~0x80;
  84. ctrl_outw(timer_status, CMT_CMCSR_0);
  85. /*
  86. * Here we are in the timer irq handler. We just have irqs locally
  87. * disabled but we don't know if the timer_bh is running on the other
  88. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  89. * the irq version of write_lock because as just said we have irq
  90. * locally disabled. -arca
  91. */
  92. write_seqlock(&xtime_lock);
  93. handle_timer_tick();
  94. write_sequnlock(&xtime_lock);
  95. return IRQ_HANDLED;
  96. }
  97. static struct irqaction cmt_irq = {
  98. .name = "timer",
  99. .handler = cmt_timer_interrupt,
  100. .flags = IRQF_DISABLED | IRQF_TIMER,
  101. .mask = CPU_MASK_NONE,
  102. };
  103. static void cmt_clk_init(struct clk *clk)
  104. {
  105. u8 divisor = CMT_CMCSR_INIT & 0x3;
  106. ctrl_inw(CMT_CMCSR_0);
  107. ctrl_outw(CMT_CMCSR_INIT, CMT_CMCSR_0);
  108. clk->parent = clk_get(NULL, "module_clk");
  109. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  110. }
  111. static void cmt_clk_recalc(struct clk *clk)
  112. {
  113. u8 divisor = ctrl_inw(CMT_CMCSR_0) & 0x3;
  114. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  115. }
  116. static struct clk_ops cmt_clk_ops = {
  117. .init = cmt_clk_init,
  118. .recalc = cmt_clk_recalc,
  119. };
  120. static struct clk cmt0_clk = {
  121. .name = "cmt0_clk",
  122. .ops = &cmt_clk_ops,
  123. };
  124. static int cmt_timer_start(void)
  125. {
  126. ctrl_outw(ctrl_inw(CMT_CMSTR) | 0x01, CMT_CMSTR);
  127. return 0;
  128. }
  129. static int cmt_timer_stop(void)
  130. {
  131. ctrl_outw(ctrl_inw(CMT_CMSTR) & ~0x01, CMT_CMSTR);
  132. return 0;
  133. }
  134. static int cmt_timer_init(void)
  135. {
  136. unsigned long interval;
  137. cmt_clock_enable();
  138. setup_irq(CONFIG_SH_TIMER_IRQ, &cmt_irq);
  139. cmt0_clk.parent = clk_get(NULL, "module_clk");
  140. cmt_timer_stop();
  141. interval = cmt0_clk.parent->rate / 8 / HZ;
  142. printk(KERN_INFO "Interval = %ld\n", interval);
  143. ctrl_outw(interval, CMT_CMCOR_0);
  144. clk_register(&cmt0_clk);
  145. clk_enable(&cmt0_clk);
  146. cmt_timer_start();
  147. return 0;
  148. }
  149. struct sys_timer_ops cmt_timer_ops = {
  150. .init = cmt_timer_init,
  151. .start = cmt_timer_start,
  152. .stop = cmt_timer_stop,
  153. #ifndef CONFIG_GENERIC_TIME
  154. .get_offset = cmt_timer_get_offset,
  155. #endif
  156. };
  157. struct sys_timer cmt_timer = {
  158. .name = "cmt",
  159. .ops = &cmt_timer_ops,
  160. };