timer-cmt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. __func__);
  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. handle_timer_tick();
  88. return IRQ_HANDLED;
  89. }
  90. static struct irqaction cmt_irq = {
  91. .name = "timer",
  92. .handler = cmt_timer_interrupt,
  93. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  94. };
  95. static void cmt_clk_init(struct clk *clk)
  96. {
  97. u8 divisor = CMT_CMCSR_INIT & 0x3;
  98. ctrl_inw(CMT_CMCSR_0);
  99. ctrl_outw(CMT_CMCSR_INIT, CMT_CMCSR_0);
  100. clk->parent = clk_get(NULL, "module_clk");
  101. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  102. }
  103. static void cmt_clk_recalc(struct clk *clk)
  104. {
  105. u8 divisor = ctrl_inw(CMT_CMCSR_0) & 0x3;
  106. clk->rate = clk->parent->rate / (8 << (divisor << 1));
  107. }
  108. static struct clk_ops cmt_clk_ops = {
  109. .init = cmt_clk_init,
  110. .recalc = cmt_clk_recalc,
  111. };
  112. static struct clk cmt0_clk = {
  113. .name = "cmt0_clk",
  114. .ops = &cmt_clk_ops,
  115. };
  116. static int cmt_timer_start(void)
  117. {
  118. ctrl_outw(ctrl_inw(CMT_CMSTR) | 0x01, CMT_CMSTR);
  119. return 0;
  120. }
  121. static int cmt_timer_stop(void)
  122. {
  123. ctrl_outw(ctrl_inw(CMT_CMSTR) & ~0x01, CMT_CMSTR);
  124. return 0;
  125. }
  126. static int cmt_timer_init(void)
  127. {
  128. unsigned long interval;
  129. cmt_clock_enable();
  130. setup_irq(CONFIG_SH_TIMER_IRQ, &cmt_irq);
  131. cmt0_clk.parent = clk_get(NULL, "module_clk");
  132. cmt_timer_stop();
  133. interval = cmt0_clk.parent->rate / 8 / HZ;
  134. printk(KERN_INFO "Interval = %ld\n", interval);
  135. ctrl_outw(interval, CMT_CMCOR_0);
  136. clk_register(&cmt0_clk);
  137. clk_enable(&cmt0_clk);
  138. cmt_timer_start();
  139. return 0;
  140. }
  141. static struct sys_timer_ops cmt_timer_ops = {
  142. .init = cmt_timer_init,
  143. .start = cmt_timer_start,
  144. .stop = cmt_timer_stop,
  145. #ifndef CONFIG_GENERIC_TIME
  146. .get_offset = cmt_timer_get_offset,
  147. #endif
  148. };
  149. struct sys_timer cmt_timer = {
  150. .name = "cmt",
  151. .ops = &cmt_timer_ops,
  152. };