timer-tmu.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_012_TSTR) | 0x3, TMU_012_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_012_TSTR) & ~0x3, TMU_012_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. case CLOCK_EVT_MODE_RESUME:
  75. break;
  76. }
  77. }
  78. static struct clock_event_device tmu0_clockevent = {
  79. .name = "tmu0",
  80. .shift = 32,
  81. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  82. .set_mode = tmu_set_mode,
  83. .set_next_event = tmu_set_next_event,
  84. };
  85. static irqreturn_t tmu_timer_interrupt(int irq, void *dummy)
  86. {
  87. struct clock_event_device *evt = &tmu0_clockevent;
  88. unsigned long timer_status;
  89. /* Clear UNF bit */
  90. timer_status = ctrl_inw(TMU0_TCR);
  91. timer_status &= ~0x100;
  92. ctrl_outw(timer_status, TMU0_TCR);
  93. evt->event_handler(evt);
  94. return IRQ_HANDLED;
  95. }
  96. static struct irqaction tmu0_irq = {
  97. .name = "periodic timer",
  98. .handler = tmu_timer_interrupt,
  99. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  100. .mask = CPU_MASK_NONE,
  101. };
  102. static void tmu0_clk_init(struct clk *clk)
  103. {
  104. u8 divisor = TMU_TCR_INIT & 0x7;
  105. ctrl_outw(TMU_TCR_INIT, TMU0_TCR);
  106. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  107. }
  108. static void tmu0_clk_recalc(struct clk *clk)
  109. {
  110. u8 divisor = ctrl_inw(TMU0_TCR) & 0x7;
  111. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  112. }
  113. static struct clk_ops tmu0_clk_ops = {
  114. .init = tmu0_clk_init,
  115. .recalc = tmu0_clk_recalc,
  116. };
  117. static struct clk tmu0_clk = {
  118. .name = "tmu0_clk",
  119. .ops = &tmu0_clk_ops,
  120. };
  121. static void tmu1_clk_init(struct clk *clk)
  122. {
  123. u8 divisor = TMU_TCR_INIT & 0x7;
  124. ctrl_outw(divisor, TMU1_TCR);
  125. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  126. }
  127. static void tmu1_clk_recalc(struct clk *clk)
  128. {
  129. u8 divisor = ctrl_inw(TMU1_TCR) & 0x7;
  130. clk->rate = clk->parent->rate / (4 << (divisor << 1));
  131. }
  132. static struct clk_ops tmu1_clk_ops = {
  133. .init = tmu1_clk_init,
  134. .recalc = tmu1_clk_recalc,
  135. };
  136. static struct clk tmu1_clk = {
  137. .name = "tmu1_clk",
  138. .ops = &tmu1_clk_ops,
  139. };
  140. static int tmu_timer_init(void)
  141. {
  142. unsigned long interval;
  143. unsigned long frequency;
  144. setup_irq(CONFIG_SH_TIMER_IRQ, &tmu0_irq);
  145. tmu0_clk.parent = clk_get(NULL, "module_clk");
  146. tmu1_clk.parent = clk_get(NULL, "module_clk");
  147. tmu_timer_stop();
  148. #if !defined(CONFIG_CPU_SUBTYPE_SH7720) && \
  149. !defined(CONFIG_CPU_SUBTYPE_SH7721) && \
  150. !defined(CONFIG_CPU_SUBTYPE_SH7760) && \
  151. !defined(CONFIG_CPU_SUBTYPE_SH7785) && \
  152. !defined(CONFIG_CPU_SUBTYPE_SHX3)
  153. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  154. #endif
  155. clk_register(&tmu0_clk);
  156. clk_register(&tmu1_clk);
  157. clk_enable(&tmu0_clk);
  158. clk_enable(&tmu1_clk);
  159. frequency = clk_get_rate(&tmu0_clk);
  160. interval = (frequency + HZ / 2) / HZ;
  161. sh_hpt_frequency = clk_get_rate(&tmu1_clk);
  162. ctrl_outl(~0, TMU1_TCNT);
  163. ctrl_outl(~0, TMU1_TCOR);
  164. tmu0_timer_set_interval(interval, 1);
  165. tmu0_clockevent.mult = div_sc(frequency, NSEC_PER_SEC,
  166. tmu0_clockevent.shift);
  167. tmu0_clockevent.max_delta_ns =
  168. clockevent_delta2ns(-1, &tmu0_clockevent);
  169. tmu0_clockevent.min_delta_ns =
  170. clockevent_delta2ns(1, &tmu0_clockevent);
  171. tmu0_clockevent.cpumask = cpumask_of_cpu(0);
  172. clockevents_register_device(&tmu0_clockevent);
  173. return 0;
  174. }
  175. static struct sys_timer_ops tmu_timer_ops = {
  176. .init = tmu_timer_init,
  177. .start = tmu_timer_start,
  178. .stop = tmu_timer_stop,
  179. .read = tmu_timer_read,
  180. };
  181. struct sys_timer tmu_timer = {
  182. .name = "tmu",
  183. .ops = &tmu_timer_ops,
  184. };