pit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /***************************************************************************/
  2. /*
  3. * pit.c -- Freescale ColdFire PIT timer. Currently this type of
  4. * hardware timer only exists in the Freescale ColdFire
  5. * 5270/5271, 5282 and 5208 CPUs. No doubt newer ColdFire
  6. * family members will probably use it too.
  7. *
  8. * Copyright (C) 1999-2008, Greg Ungerer (gerg@snapgear.com)
  9. * Copyright (C) 2001-2004, SnapGear Inc. (www.snapgear.com)
  10. */
  11. /***************************************************************************/
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/param.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/clockchips.h>
  19. #include <asm/machdep.h>
  20. #include <asm/io.h>
  21. #include <asm/coldfire.h>
  22. #include <asm/mcfpit.h>
  23. #include <asm/mcfsim.h>
  24. /***************************************************************************/
  25. /*
  26. * By default use timer1 as the system clock timer.
  27. */
  28. #define FREQ ((MCF_CLK / 2) / 64)
  29. #define TA(a) (MCF_IPSBAR + MCFPIT_BASE1 + (a))
  30. #define INTC0 (MCF_IPSBAR + MCFICM_INTC0)
  31. #define PIT_CYCLES_PER_JIFFY (FREQ / HZ)
  32. static u32 pit_cnt;
  33. /*
  34. * Initialize the PIT timer.
  35. *
  36. * This is also called after resume to bring the PIT into operation again.
  37. */
  38. static void init_cf_pit_timer(enum clock_event_mode mode,
  39. struct clock_event_device *evt)
  40. {
  41. switch (mode) {
  42. case CLOCK_EVT_MODE_PERIODIC:
  43. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  44. __raw_writew(PIT_CYCLES_PER_JIFFY, TA(MCFPIT_PMR));
  45. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | \
  46. MCFPIT_PCSR_OVW | MCFPIT_PCSR_RLD | \
  47. MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  48. break;
  49. case CLOCK_EVT_MODE_SHUTDOWN:
  50. case CLOCK_EVT_MODE_UNUSED:
  51. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  52. break;
  53. case CLOCK_EVT_MODE_ONESHOT:
  54. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  55. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | \
  56. MCFPIT_PCSR_OVW | MCFPIT_PCSR_CLK64, \
  57. TA(MCFPIT_PCSR));
  58. break;
  59. case CLOCK_EVT_MODE_RESUME:
  60. /* Nothing to do here */
  61. break;
  62. }
  63. }
  64. /*
  65. * Program the next event in oneshot mode
  66. *
  67. * Delta is given in PIT ticks
  68. */
  69. static int cf_pit_next_event(unsigned long delta,
  70. struct clock_event_device *evt)
  71. {
  72. __raw_writew(delta, TA(MCFPIT_PMR));
  73. return 0;
  74. }
  75. struct clock_event_device cf_pit_clockevent = {
  76. .name = "pit",
  77. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  78. .set_mode = init_cf_pit_timer,
  79. .set_next_event = cf_pit_next_event,
  80. .shift = 32,
  81. .irq = MCFINT_VECBASE + MCFINT_PIT1,
  82. };
  83. /***************************************************************************/
  84. static irqreturn_t pit_tick(int irq, void *dummy)
  85. {
  86. struct clock_event_device *evt = &cf_pit_clockevent;
  87. u16 pcsr;
  88. /* Reset the ColdFire timer */
  89. pcsr = __raw_readw(TA(MCFPIT_PCSR));
  90. __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
  91. pit_cnt += PIT_CYCLES_PER_JIFFY;
  92. evt->event_handler(evt);
  93. return IRQ_HANDLED;
  94. }
  95. /***************************************************************************/
  96. static struct irqaction pit_irq = {
  97. .name = "timer",
  98. .flags = IRQF_DISABLED | IRQF_TIMER,
  99. .handler = pit_tick,
  100. };
  101. /***************************************************************************/
  102. static cycle_t pit_read_clk(void)
  103. {
  104. unsigned long flags;
  105. u32 cycles;
  106. u16 pcntr;
  107. local_irq_save(flags);
  108. pcntr = __raw_readw(TA(MCFPIT_PCNTR));
  109. cycles = pit_cnt;
  110. local_irq_restore(flags);
  111. return cycles + PIT_CYCLES_PER_JIFFY - pcntr;
  112. }
  113. /***************************************************************************/
  114. static struct clocksource pit_clk = {
  115. .name = "pit",
  116. .rating = 100,
  117. .read = pit_read_clk,
  118. .shift = 20,
  119. .mask = CLOCKSOURCE_MASK(32),
  120. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  121. };
  122. /***************************************************************************/
  123. void hw_timer_init(void)
  124. {
  125. u32 imr;
  126. cf_pit_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
  127. cf_pit_clockevent.mult = div_sc(FREQ, NSEC_PER_SEC, 32);
  128. cf_pit_clockevent.max_delta_ns =
  129. clockevent_delta2ns(0xFFFF, &cf_pit_clockevent);
  130. cf_pit_clockevent.min_delta_ns =
  131. clockevent_delta2ns(0x3f, &cf_pit_clockevent);
  132. clockevents_register_device(&cf_pit_clockevent);
  133. setup_irq(MCFINT_VECBASE + MCFINT_PIT1, &pit_irq);
  134. __raw_writeb(ICR_INTRCONF, INTC0 + MCFINTC_ICR0 + MCFINT_PIT1);
  135. imr = __raw_readl(INTC0 + MCFPIT_IMR);
  136. imr &= ~MCFPIT_IMR_IBIT;
  137. __raw_writel(imr, INTC0 + MCFPIT_IMR);
  138. pit_clk.mult = clocksource_hz2mult(FREQ, pit_clk.shift);
  139. clocksource_register(&pit_clk);
  140. }
  141. /***************************************************************************/