timer-cmt.c 4.5 KB

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