timer-mtu2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * arch/sh/kernel/timers/timer-mtu2.c - MTU2 Timer Support
  3. *
  4. * Copyright (C) 2005 Paul Mundt
  5. *
  6. * Based off of arch/sh/kernel/timers/timer-tmu.c
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/seqlock.h>
  16. #include <asm/timer.h>
  17. #include <asm/io.h>
  18. #include <asm/irq.h>
  19. #include <asm/clock.h>
  20. /*
  21. * We use channel 1 for our lowly system timer. Channel 2 would be the other
  22. * likely candidate, but we leave it alone as it has higher divisors that
  23. * would be of more use to other more interesting applications.
  24. *
  25. * TODO: Presently we only implement a 16-bit single-channel system timer.
  26. * However, we can implement channel cascade if we go the overflow route and
  27. * get away with using 2 MTU2 channels as a 32-bit timer.
  28. */
  29. #define MTU2_TSTR 0xfffe4280
  30. #define MTU2_TCR_1 0xfffe4380
  31. #define MTU2_TMDR_1 0xfffe4381
  32. #define MTU2_TIOR_1 0xfffe4382
  33. #define MTU2_TIER_1 0xfffe4384
  34. #define MTU2_TSR_1 0xfffe4385
  35. #define MTU2_TCNT_1 0xfffe4386 /* 16-bit counter */
  36. #if defined(CONFIG_CPU_SUBTYPE_SH7201) || \
  37. defined(CONFIG_CPU_SUBTYPE_SH7203)
  38. #define MTU2_TGRA_1 0xfffe4388
  39. #else
  40. #define MTU2_TGRA_1 0xfffe438a
  41. #endif
  42. #define STBCR3 0xfffe0408
  43. #define MTU2_TSTR_CST1 (1 << 1) /* Counter Start 1 */
  44. #define MTU2_TSR_TGFA (1 << 0) /* GRA compare match */
  45. #define MTU2_TIER_TGIEA (1 << 0) /* GRA compare match interrupt enable */
  46. #define MTU2_TCR_INIT 0x22
  47. #define MTU2_TCR_CALIB 0x00
  48. static unsigned long mtu2_timer_get_offset(void)
  49. {
  50. int count;
  51. static int count_p = 0x7fff; /* for the first call after boot */
  52. static unsigned long jiffies_p = 0;
  53. /*
  54. * cache volatile jiffies temporarily; we have IRQs turned off.
  55. */
  56. unsigned long jiffies_t;
  57. /* timer count may underflow right here */
  58. count = ctrl_inw(MTU2_TCNT_1); /* read the latched count */
  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. if (ctrl_inb(MTU2_TSR_1) & MTU2_TSR_TGFA) {
  68. count -= LATCH;
  69. } else {
  70. printk("%s (): hardware timer problem?\n",
  71. __func__);
  72. }
  73. }
  74. } else
  75. jiffies_p = jiffies_t;
  76. count_p = count;
  77. count = ((LATCH-1) - count) * TICK_SIZE;
  78. count = (count + LATCH/2) / LATCH;
  79. return count;
  80. }
  81. static irqreturn_t mtu2_timer_interrupt(int irq, void *dev_id)
  82. {
  83. unsigned long timer_status;
  84. /* Clear TGFA bit */
  85. timer_status = ctrl_inb(MTU2_TSR_1);
  86. timer_status &= ~MTU2_TSR_TGFA;
  87. ctrl_outb(timer_status, MTU2_TSR_1);
  88. /* Do timer tick */
  89. handle_timer_tick();
  90. return IRQ_HANDLED;
  91. }
  92. static struct irqaction mtu2_irq = {
  93. .name = "timer",
  94. .handler = mtu2_timer_interrupt,
  95. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  96. };
  97. static unsigned int divisors[] = { 1, 4, 16, 64, 1, 1, 256 };
  98. static void mtu2_clk_init(struct clk *clk)
  99. {
  100. u8 idx = MTU2_TCR_INIT & 0x7;
  101. clk->rate = clk->parent->rate / divisors[idx];
  102. /* Start TCNT counting */
  103. ctrl_outb(ctrl_inb(MTU2_TSTR) | MTU2_TSTR_CST1, MTU2_TSTR);
  104. }
  105. static void mtu2_clk_recalc(struct clk *clk)
  106. {
  107. u8 idx = ctrl_inb(MTU2_TCR_1) & 0x7;
  108. clk->rate = clk->parent->rate / divisors[idx];
  109. }
  110. static struct clk_ops mtu2_clk_ops = {
  111. .init = mtu2_clk_init,
  112. .recalc = mtu2_clk_recalc,
  113. };
  114. static struct clk mtu2_clk1 = {
  115. .name = "mtu2_clk1",
  116. .ops = &mtu2_clk_ops,
  117. };
  118. static int mtu2_timer_start(void)
  119. {
  120. ctrl_outb(ctrl_inb(MTU2_TSTR) | MTU2_TSTR_CST1, MTU2_TSTR);
  121. return 0;
  122. }
  123. static int mtu2_timer_stop(void)
  124. {
  125. ctrl_outb(ctrl_inb(MTU2_TSTR) & ~MTU2_TSTR_CST1, MTU2_TSTR);
  126. return 0;
  127. }
  128. static int mtu2_timer_init(void)
  129. {
  130. unsigned long interval;
  131. setup_irq(CONFIG_SH_TIMER_IRQ, &mtu2_irq);
  132. mtu2_clk1.parent = clk_get(NULL, "module_clk");
  133. ctrl_outb(ctrl_inb(STBCR3) & (~0x20), STBCR3);
  134. /* Normal operation */
  135. ctrl_outb(0, MTU2_TMDR_1);
  136. ctrl_outb(MTU2_TCR_INIT, MTU2_TCR_1);
  137. ctrl_outb(0x01, MTU2_TIOR_1);
  138. /* Enable underflow interrupt */
  139. ctrl_outb(ctrl_inb(MTU2_TIER_1) | MTU2_TIER_TGIEA, MTU2_TIER_1);
  140. interval = CONFIG_SH_PCLK_FREQ / 16 / HZ;
  141. printk(KERN_INFO "Interval = %ld\n", interval);
  142. ctrl_outw(interval, MTU2_TGRA_1);
  143. ctrl_outw(0, MTU2_TCNT_1);
  144. clk_register(&mtu2_clk1);
  145. clk_enable(&mtu2_clk1);
  146. return 0;
  147. }
  148. struct sys_timer_ops mtu2_timer_ops = {
  149. .init = mtu2_timer_init,
  150. .start = mtu2_timer_start,
  151. .stop = mtu2_timer_stop,
  152. #ifndef CONFIG_GENERIC_TIME
  153. .get_offset = mtu2_timer_get_offset,
  154. #endif
  155. };
  156. struct sys_timer mtu2_timer = {
  157. .name = "mtu2",
  158. .ops = &mtu2_timer_ops,
  159. };