pit.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 int cf_pit_set_periodic(struct clock_event_device *evt)
  38. {
  39. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  40. __raw_writew(PIT_CYCLES_PER_JIFFY, TA(MCFPIT_PMR));
  41. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE |
  42. MCFPIT_PCSR_OVW | MCFPIT_PCSR_RLD |
  43. MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  44. return 0;
  45. }
  46. static int cf_pit_set_oneshot(struct clock_event_device *evt)
  47. {
  48. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  49. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE |
  50. MCFPIT_PCSR_OVW | MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  51. return 0;
  52. }
  53. static int cf_pit_shutdown(struct clock_event_device *evt)
  54. {
  55. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  56. return 0;
  57. }
  58. /*
  59. * Program the next event in oneshot mode
  60. *
  61. * Delta is given in PIT ticks
  62. */
  63. static int cf_pit_next_event(unsigned long delta,
  64. struct clock_event_device *evt)
  65. {
  66. __raw_writew(delta, TA(MCFPIT_PMR));
  67. return 0;
  68. }
  69. struct clock_event_device cf_pit_clockevent = {
  70. .name = "pit",
  71. .features = CLOCK_EVT_FEAT_PERIODIC |
  72. CLOCK_EVT_FEAT_ONESHOT,
  73. .set_state_shutdown = cf_pit_shutdown,
  74. .set_state_periodic = cf_pit_set_periodic,
  75. .set_state_oneshot = cf_pit_set_oneshot,
  76. .set_next_event = cf_pit_next_event,
  77. .shift = 32,
  78. .irq = MCF_IRQ_PIT1,
  79. };
  80. /***************************************************************************/
  81. static irqreturn_t pit_tick(int irq, void *dummy)
  82. {
  83. struct clock_event_device *evt = &cf_pit_clockevent;
  84. u16 pcsr;
  85. /* Reset the ColdFire timer */
  86. pcsr = __raw_readw(TA(MCFPIT_PCSR));
  87. __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
  88. pit_cnt += PIT_CYCLES_PER_JIFFY;
  89. evt->event_handler(evt);
  90. return IRQ_HANDLED;
  91. }
  92. /***************************************************************************/
  93. static struct irqaction pit_irq = {
  94. .name = "timer",
  95. .flags = IRQF_TIMER,
  96. .handler = pit_tick,
  97. };
  98. /***************************************************************************/
  99. static cycle_t pit_read_clk(struct clocksource *cs)
  100. {
  101. unsigned long flags;
  102. u32 cycles;
  103. u16 pcntr;
  104. local_irq_save(flags);
  105. pcntr = __raw_readw(TA(MCFPIT_PCNTR));
  106. cycles = pit_cnt;
  107. local_irq_restore(flags);
  108. return cycles + PIT_CYCLES_PER_JIFFY - pcntr;
  109. }
  110. /***************************************************************************/
  111. static struct clocksource pit_clk = {
  112. .name = "pit",
  113. .rating = 100,
  114. .read = pit_read_clk,
  115. .mask = CLOCKSOURCE_MASK(32),
  116. };
  117. /***************************************************************************/
  118. void hw_timer_init(irq_handler_t handler)
  119. {
  120. cf_pit_clockevent.cpumask = cpumask_of(smp_processor_id());
  121. cf_pit_clockevent.mult = div_sc(FREQ, NSEC_PER_SEC, 32);
  122. cf_pit_clockevent.max_delta_ns =
  123. clockevent_delta2ns(0xFFFF, &cf_pit_clockevent);
  124. cf_pit_clockevent.min_delta_ns =
  125. clockevent_delta2ns(0x3f, &cf_pit_clockevent);
  126. clockevents_register_device(&cf_pit_clockevent);
  127. setup_irq(MCF_IRQ_PIT1, &pit_irq);
  128. clocksource_register_hz(&pit_clk, FREQ);
  129. }
  130. /***************************************************************************/