pit.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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) (MCFPIT_BASE1 + (a))
  30. #define PIT_CYCLES_PER_JIFFY (FREQ / HZ)
  31. static u32 pit_cnt;
  32. /*
  33. * Initialize the PIT timer.
  34. *
  35. * This is also called after resume to bring the PIT into operation again.
  36. */
  37. static void init_cf_pit_timer(enum clock_event_mode mode,
  38. struct clock_event_device *evt)
  39. {
  40. switch (mode) {
  41. case CLOCK_EVT_MODE_PERIODIC:
  42. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  43. __raw_writew(PIT_CYCLES_PER_JIFFY, TA(MCFPIT_PMR));
  44. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | \
  45. MCFPIT_PCSR_OVW | MCFPIT_PCSR_RLD | \
  46. MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  47. break;
  48. case CLOCK_EVT_MODE_SHUTDOWN:
  49. case CLOCK_EVT_MODE_UNUSED:
  50. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  51. break;
  52. case CLOCK_EVT_MODE_ONESHOT:
  53. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  54. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | \
  55. MCFPIT_PCSR_OVW | MCFPIT_PCSR_CLK64, \
  56. TA(MCFPIT_PCSR));
  57. break;
  58. case CLOCK_EVT_MODE_RESUME:
  59. /* Nothing to do here */
  60. break;
  61. }
  62. }
  63. /*
  64. * Program the next event in oneshot mode
  65. *
  66. * Delta is given in PIT ticks
  67. */
  68. static int cf_pit_next_event(unsigned long delta,
  69. struct clock_event_device *evt)
  70. {
  71. __raw_writew(delta, TA(MCFPIT_PMR));
  72. return 0;
  73. }
  74. struct clock_event_device cf_pit_clockevent = {
  75. .name = "pit",
  76. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  77. .set_mode = init_cf_pit_timer,
  78. .set_next_event = cf_pit_next_event,
  79. .shift = 32,
  80. .irq = MCFINT_VECBASE + MCFINT_PIT1,
  81. };
  82. /***************************************************************************/
  83. static irqreturn_t pit_tick(int irq, void *dummy)
  84. {
  85. struct clock_event_device *evt = &cf_pit_clockevent;
  86. u16 pcsr;
  87. /* Reset the ColdFire timer */
  88. pcsr = __raw_readw(TA(MCFPIT_PCSR));
  89. __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
  90. pit_cnt += PIT_CYCLES_PER_JIFFY;
  91. evt->event_handler(evt);
  92. return IRQ_HANDLED;
  93. }
  94. /***************************************************************************/
  95. static struct irqaction pit_irq = {
  96. .name = "timer",
  97. .flags = IRQF_DISABLED | IRQF_TIMER,
  98. .handler = pit_tick,
  99. };
  100. /***************************************************************************/
  101. static cycle_t pit_read_clk(struct clocksource *cs)
  102. {
  103. unsigned long flags;
  104. u32 cycles;
  105. u16 pcntr;
  106. local_irq_save(flags);
  107. pcntr = __raw_readw(TA(MCFPIT_PCNTR));
  108. cycles = pit_cnt;
  109. local_irq_restore(flags);
  110. return cycles + PIT_CYCLES_PER_JIFFY - pcntr;
  111. }
  112. /***************************************************************************/
  113. static struct clocksource pit_clk = {
  114. .name = "pit",
  115. .rating = 100,
  116. .read = pit_read_clk,
  117. .mask = CLOCKSOURCE_MASK(32),
  118. };
  119. /***************************************************************************/
  120. void hw_timer_init(irq_handler_t handler)
  121. {
  122. cf_pit_clockevent.cpumask = cpumask_of(smp_processor_id());
  123. cf_pit_clockevent.mult = div_sc(FREQ, NSEC_PER_SEC, 32);
  124. cf_pit_clockevent.max_delta_ns =
  125. clockevent_delta2ns(0xFFFF, &cf_pit_clockevent);
  126. cf_pit_clockevent.min_delta_ns =
  127. clockevent_delta2ns(0x3f, &cf_pit_clockevent);
  128. clockevents_register_device(&cf_pit_clockevent);
  129. setup_irq(MCFINT_VECBASE + MCFINT_PIT1, &pit_irq);
  130. clocksource_register_hz(&pit_clk, FREQ);
  131. }
  132. /***************************************************************************/