pit.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /***************************************************************************/
  3. /*
  4. * pit.c -- Freescale ColdFire PIT timer. Currently this type of
  5. * hardware timer only exists in the Freescale ColdFire
  6. * 5270/5271, 5282 and 5208 CPUs. No doubt newer ColdFire
  7. * family members will probably use it too.
  8. *
  9. * Copyright (C) 1999-2008, Greg Ungerer (gerg@snapgear.com)
  10. * Copyright (C) 2001-2004, SnapGear Inc. (www.snapgear.com)
  11. */
  12. /***************************************************************************/
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/param.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/clockchips.h>
  20. #include <asm/machdep.h>
  21. #include <asm/io.h>
  22. #include <asm/coldfire.h>
  23. #include <asm/mcfpit.h>
  24. #include <asm/mcfsim.h>
  25. /***************************************************************************/
  26. /*
  27. * By default use timer1 as the system clock timer.
  28. */
  29. #define FREQ ((MCF_CLK / 2) / 64)
  30. #define TA(a) (MCFPIT_BASE1 + (a))
  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 int cf_pit_set_periodic(struct clock_event_device *evt)
  39. {
  40. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  41. __raw_writew(PIT_CYCLES_PER_JIFFY, TA(MCFPIT_PMR));
  42. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE |
  43. MCFPIT_PCSR_OVW | MCFPIT_PCSR_RLD |
  44. MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  45. return 0;
  46. }
  47. static int cf_pit_set_oneshot(struct clock_event_device *evt)
  48. {
  49. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  50. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE |
  51. MCFPIT_PCSR_OVW | MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  52. return 0;
  53. }
  54. static int cf_pit_shutdown(struct clock_event_device *evt)
  55. {
  56. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  57. return 0;
  58. }
  59. /*
  60. * Program the next event in oneshot mode
  61. *
  62. * Delta is given in PIT ticks
  63. */
  64. static int cf_pit_next_event(unsigned long delta,
  65. struct clock_event_device *evt)
  66. {
  67. __raw_writew(delta, TA(MCFPIT_PMR));
  68. return 0;
  69. }
  70. struct clock_event_device cf_pit_clockevent = {
  71. .name = "pit",
  72. .features = CLOCK_EVT_FEAT_PERIODIC |
  73. CLOCK_EVT_FEAT_ONESHOT,
  74. .set_state_shutdown = cf_pit_shutdown,
  75. .set_state_periodic = cf_pit_set_periodic,
  76. .set_state_oneshot = cf_pit_set_oneshot,
  77. .set_next_event = cf_pit_next_event,
  78. .shift = 32,
  79. .irq = MCF_IRQ_PIT1,
  80. };
  81. /***************************************************************************/
  82. static irqreturn_t pit_tick(int irq, void *dummy)
  83. {
  84. struct clock_event_device *evt = &cf_pit_clockevent;
  85. u16 pcsr;
  86. /* Reset the ColdFire timer */
  87. pcsr = __raw_readw(TA(MCFPIT_PCSR));
  88. __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
  89. pit_cnt += PIT_CYCLES_PER_JIFFY;
  90. evt->event_handler(evt);
  91. return IRQ_HANDLED;
  92. }
  93. /***************************************************************************/
  94. static struct irqaction pit_irq = {
  95. .name = "timer",
  96. .flags = IRQF_TIMER,
  97. .handler = pit_tick,
  98. };
  99. /***************************************************************************/
  100. static u64 pit_read_clk(struct clocksource *cs)
  101. {
  102. unsigned long flags;
  103. u32 cycles;
  104. u16 pcntr;
  105. local_irq_save(flags);
  106. pcntr = __raw_readw(TA(MCFPIT_PCNTR));
  107. cycles = pit_cnt;
  108. local_irq_restore(flags);
  109. return cycles + PIT_CYCLES_PER_JIFFY - pcntr;
  110. }
  111. /***************************************************************************/
  112. static struct clocksource pit_clk = {
  113. .name = "pit",
  114. .rating = 100,
  115. .read = pit_read_clk,
  116. .mask = CLOCKSOURCE_MASK(32),
  117. };
  118. /***************************************************************************/
  119. void hw_timer_init(irq_handler_t handler)
  120. {
  121. cf_pit_clockevent.cpumask = cpumask_of(smp_processor_id());
  122. cf_pit_clockevent.mult = div_sc(FREQ, NSEC_PER_SEC, 32);
  123. cf_pit_clockevent.max_delta_ns =
  124. clockevent_delta2ns(0xFFFF, &cf_pit_clockevent);
  125. cf_pit_clockevent.max_delta_ticks = 0xFFFF;
  126. cf_pit_clockevent.min_delta_ns =
  127. clockevent_delta2ns(0x3f, &cf_pit_clockevent);
  128. cf_pit_clockevent.min_delta_ticks = 0x3f;
  129. clockevents_register_device(&cf_pit_clockevent);
  130. setup_irq(MCF_IRQ_PIT1, &pit_irq);
  131. clocksource_register_hz(&pit_clk, FREQ);
  132. }
  133. /***************************************************************************/