timer-tmu.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * arch/sh/kernel/timers/timer-tmu.c - TMU Timer Support
  3. *
  4. * Copyright (C) 2005 - 2007 Paul Mundt
  5. *
  6. * TMU handling code hacked out of arch/sh/kernel/time.c
  7. *
  8. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  9. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  10. * Copyright (C) 2002, 2003, 2004 Paul Mundt
  11. * Copyright (C) 2002 M. R. Brown <mrbrown@linux-sh.org>
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/seqlock.h>
  21. #include <linux/clockchips.h>
  22. #include <asm/timer.h>
  23. #include <asm/rtc.h>
  24. #include <asm/io.h>
  25. #include <asm/irq.h>
  26. #include <asm/clock.h>
  27. #define TMU_TOCR_INIT 0x00
  28. #define TMU_TCR_INIT 0x0020
  29. static int tmu_timer_start(void)
  30. {
  31. ctrl_outb(ctrl_inb(TMU_TSTR) | 0x3, TMU_TSTR);
  32. return 0;
  33. }
  34. static void tmu0_timer_set_interval(unsigned long interval, unsigned int reload)
  35. {
  36. ctrl_outl(interval, TMU0_TCNT);
  37. /*
  38. * TCNT reloads from TCOR on underflow, clear it if we don't
  39. * intend to auto-reload
  40. */
  41. if (reload)
  42. ctrl_outl(interval, TMU0_TCOR);
  43. else
  44. ctrl_outl(0, TMU0_TCOR);
  45. tmu_timer_start();
  46. }
  47. static int tmu_timer_stop(void)
  48. {
  49. ctrl_outb(ctrl_inb(TMU_TSTR) & ~0x3, TMU_TSTR);
  50. return 0;
  51. }
  52. static cycle_t tmu_timer_read(void)
  53. {
  54. return ~ctrl_inl(TMU1_TCNT);
  55. }
  56. static int tmu_set_next_event(unsigned long cycles,
  57. struct clock_event_device *evt)
  58. {
  59. tmu0_timer_set_interval(cycles, 1);
  60. return 0;
  61. }
  62. static void tmu_set_mode(enum clock_event_mode mode,
  63. struct clock_event_device *evt)
  64. {
  65. switch (mode) {
  66. case CLOCK_EVT_MODE_PERIODIC:
  67. ctrl_outl(ctrl_inl(TMU0_TCNT), TMU0_TCOR);
  68. break;
  69. case CLOCK_EVT_MODE_ONESHOT:
  70. ctrl_outl(0, TMU0_TCOR);
  71. break;
  72. case CLOCK_EVT_MODE_UNUSED:
  73. case CLOCK_EVT_MODE_SHUTDOWN:
  74. break;
  75. }
  76. }
  77. static struct clock_event_device tmu0_clockevent = {
  78. .name = "tmu0",
  79. .shift = 32,
  80. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  81. .set_mode = tmu_set_mode,
  82. .set_next_event = tmu_set_next_event,
  83. };
  84. static irqreturn_t tmu_timer_interrupt(int irq, void *dummy)
  85. {
  86. struct clock_event_device *evt = &tmu0_clockevent;
  87. unsigned long timer_status;
  88. /* Clear UNF bit */
  89. timer_status = ctrl_inw(TMU0_TCR);
  90. timer_status &= ~0x100;
  91. ctrl_outw(timer_status, TMU0_TCR);
  92. evt->event_handler(evt);
  93. return IRQ_HANDLED;
  94. }
  95. static struct irqaction tmu0_irq = {
  96. .name = "periodic timer",
  97. .handler = tmu_timer_interrupt,
  98. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  99. .mask = CPU_MASK_NONE,
  100. };
  101. static void tmu0_clk_init(struct clk *clk)
  102. {
  103. u8 divisor = TMU_TCR_INIT & 0x7;
  104. ctrl_outw(TMU_TCR_INIT, TMU0_TCR);
  105. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  106. }
  107. static void tmu0_clk_recalc(struct clk *clk)
  108. {
  109. u8 divisor = ctrl_inw(TMU0_TCR) & 0x7;
  110. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  111. }
  112. static struct clk_ops tmu0_clk_ops = {
  113. .init = tmu0_clk_init,
  114. .recalc = tmu0_clk_recalc,
  115. };
  116. static struct clk tmu0_clk = {
  117. .name = "tmu0_clk",
  118. .ops = &tmu0_clk_ops,
  119. };
  120. static void tmu1_clk_init(struct clk *clk)
  121. {
  122. u8 divisor = TMU_TCR_INIT & 0x7;
  123. ctrl_outw(divisor, TMU1_TCR);
  124. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  125. }
  126. static void tmu1_clk_recalc(struct clk *clk)
  127. {
  128. u8 divisor = ctrl_inw(TMU1_TCR) & 0x7;
  129. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  130. }
  131. static struct clk_ops tmu1_clk_ops = {
  132. .init = tmu1_clk_init,
  133. .recalc = tmu1_clk_recalc,
  134. };
  135. static struct clk tmu1_clk = {
  136. .name = "tmu1_clk",
  137. .ops = &tmu1_clk_ops,
  138. };
  139. static int tmu_timer_init(void)
  140. {
  141. unsigned long interval;
  142. unsigned long frequency;
  143. setup_irq(CONFIG_SH_TIMER_IRQ, &tmu0_irq);
  144. tmu0_clk.parent = clk_get(NULL, "module_clk");
  145. tmu1_clk.parent = clk_get(NULL, "module_clk");
  146. tmu_timer_stop();
  147. #if !defined(CONFIG_CPU_SUBTYPE_SH7300) && \
  148. !defined(CONFIG_CPU_SUBTYPE_SH7760) && \
  149. !defined(CONFIG_CPU_SUBTYPE_SH7785)
  150. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  151. #endif
  152. clk_register(&tmu0_clk);
  153. clk_register(&tmu1_clk);
  154. clk_enable(&tmu0_clk);
  155. clk_enable(&tmu1_clk);
  156. frequency = clk_get_rate(&tmu0_clk);
  157. interval = (frequency + HZ / 2) / HZ;
  158. sh_hpt_frequency = clk_get_rate(&tmu1_clk);
  159. ctrl_outl(~0, TMU1_TCNT);
  160. ctrl_outl(~0, TMU1_TCOR);
  161. tmu0_timer_set_interval(interval, 1);
  162. tmu0_clockevent.mult = div_sc(frequency, NSEC_PER_SEC,
  163. tmu0_clockevent.shift);
  164. tmu0_clockevent.max_delta_ns =
  165. clockevent_delta2ns(-1, &tmu0_clockevent);
  166. tmu0_clockevent.min_delta_ns =
  167. clockevent_delta2ns(1, &tmu0_clockevent);
  168. tmu0_clockevent.cpumask = cpumask_of_cpu(0);
  169. clockevents_register_device(&tmu0_clockevent);
  170. return 0;
  171. }
  172. struct sys_timer_ops tmu_timer_ops = {
  173. .init = tmu_timer_init,
  174. .start = tmu_timer_start,
  175. .stop = tmu_timer_stop,
  176. .read = tmu_timer_read,
  177. };
  178. struct sys_timer tmu_timer = {
  179. .name = "tmu",
  180. .ops = &tmu_timer_ops,
  181. };