i8253.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * i8253 PIT clocksource
  3. */
  4. #include <linux/clockchips.h>
  5. #include <linux/init.h>
  6. #include <linux/io.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/timex.h>
  9. #include <linux/module.h>
  10. #include <linux/i8253.h>
  11. #include <linux/smp.h>
  12. /*
  13. * Protects access to I/O ports
  14. *
  15. * 0040-0043 : timer0, i8253 / i8254
  16. * 0061-0061 : NMI Control Register which contains two speaker control bits.
  17. */
  18. DEFINE_RAW_SPINLOCK(i8253_lock);
  19. EXPORT_SYMBOL(i8253_lock);
  20. /*
  21. * Handle PIT quirk in pit_shutdown() where zeroing the counter register
  22. * restarts the PIT, negating the shutdown. On platforms with the quirk,
  23. * platform specific code can set this to false.
  24. */
  25. bool i8253_clear_counter_on_shutdown = true;
  26. #ifdef CONFIG_CLKSRC_I8253
  27. /*
  28. * Since the PIT overflows every tick, its not very useful
  29. * to just read by itself. So use jiffies to emulate a free
  30. * running counter:
  31. */
  32. static cycle_t i8253_read(struct clocksource *cs)
  33. {
  34. static int old_count;
  35. static u32 old_jifs;
  36. unsigned long flags;
  37. int count;
  38. u32 jifs;
  39. raw_spin_lock_irqsave(&i8253_lock, flags);
  40. /*
  41. * Although our caller may have the read side of jiffies_lock,
  42. * this is now a seqlock, and we are cheating in this routine
  43. * by having side effects on state that we cannot undo if
  44. * there is a collision on the seqlock and our caller has to
  45. * retry. (Namely, old_jifs and old_count.) So we must treat
  46. * jiffies as volatile despite the lock. We read jiffies
  47. * before latching the timer count to guarantee that although
  48. * the jiffies value might be older than the count (that is,
  49. * the counter may underflow between the last point where
  50. * jiffies was incremented and the point where we latch the
  51. * count), it cannot be newer.
  52. */
  53. jifs = jiffies;
  54. outb_p(0x00, PIT_MODE); /* latch the count ASAP */
  55. count = inb_p(PIT_CH0); /* read the latched count */
  56. count |= inb_p(PIT_CH0) << 8;
  57. /* VIA686a test code... reset the latch if count > max + 1 */
  58. if (count > PIT_LATCH) {
  59. outb_p(0x34, PIT_MODE);
  60. outb_p(PIT_LATCH & 0xff, PIT_CH0);
  61. outb_p(PIT_LATCH >> 8, PIT_CH0);
  62. count = PIT_LATCH - 1;
  63. }
  64. /*
  65. * It's possible for count to appear to go the wrong way for a
  66. * couple of reasons:
  67. *
  68. * 1. The timer counter underflows, but we haven't handled the
  69. * resulting interrupt and incremented jiffies yet.
  70. * 2. Hardware problem with the timer, not giving us continuous time,
  71. * the counter does small "jumps" upwards on some Pentium systems,
  72. * (see c't 95/10 page 335 for Neptun bug.)
  73. *
  74. * Previous attempts to handle these cases intelligently were
  75. * buggy, so we just do the simple thing now.
  76. */
  77. if (count > old_count && jifs == old_jifs)
  78. count = old_count;
  79. old_count = count;
  80. old_jifs = jifs;
  81. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  82. count = (PIT_LATCH - 1) - count;
  83. return (cycle_t)(jifs * PIT_LATCH) + count;
  84. }
  85. static struct clocksource i8253_cs = {
  86. .name = "pit",
  87. .rating = 110,
  88. .read = i8253_read,
  89. .mask = CLOCKSOURCE_MASK(32),
  90. };
  91. int __init clocksource_i8253_init(void)
  92. {
  93. return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
  94. }
  95. #endif
  96. #ifdef CONFIG_CLKEVT_I8253
  97. static int pit_shutdown(struct clock_event_device *evt)
  98. {
  99. if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
  100. return 0;
  101. raw_spin_lock(&i8253_lock);
  102. outb_p(0x30, PIT_MODE);
  103. if (i8253_clear_counter_on_shutdown) {
  104. outb_p(0, PIT_CH0);
  105. outb_p(0, PIT_CH0);
  106. }
  107. raw_spin_unlock(&i8253_lock);
  108. return 0;
  109. }
  110. static int pit_set_oneshot(struct clock_event_device *evt)
  111. {
  112. raw_spin_lock(&i8253_lock);
  113. outb_p(0x38, PIT_MODE);
  114. raw_spin_unlock(&i8253_lock);
  115. return 0;
  116. }
  117. static int pit_set_periodic(struct clock_event_device *evt)
  118. {
  119. raw_spin_lock(&i8253_lock);
  120. /* binary, mode 2, LSB/MSB, ch 0 */
  121. outb_p(0x34, PIT_MODE);
  122. outb_p(PIT_LATCH & 0xff, PIT_CH0); /* LSB */
  123. outb_p(PIT_LATCH >> 8, PIT_CH0); /* MSB */
  124. raw_spin_unlock(&i8253_lock);
  125. return 0;
  126. }
  127. /*
  128. * Program the next event in oneshot mode
  129. *
  130. * Delta is given in PIT ticks
  131. */
  132. static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
  133. {
  134. raw_spin_lock(&i8253_lock);
  135. outb_p(delta & 0xff , PIT_CH0); /* LSB */
  136. outb_p(delta >> 8 , PIT_CH0); /* MSB */
  137. raw_spin_unlock(&i8253_lock);
  138. return 0;
  139. }
  140. /*
  141. * On UP the PIT can serve all of the possible timer functions. On SMP systems
  142. * it can be solely used for the global tick.
  143. */
  144. struct clock_event_device i8253_clockevent = {
  145. .name = "pit",
  146. .features = CLOCK_EVT_FEAT_PERIODIC,
  147. .set_state_shutdown = pit_shutdown,
  148. .set_state_periodic = pit_set_periodic,
  149. .set_next_event = pit_next_event,
  150. };
  151. /*
  152. * Initialize the conversion factor and the min/max deltas of the clock event
  153. * structure and register the clock event source with the framework.
  154. */
  155. void __init clockevent_i8253_init(bool oneshot)
  156. {
  157. if (oneshot) {
  158. i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
  159. i8253_clockevent.set_state_oneshot = pit_set_oneshot;
  160. }
  161. /*
  162. * Start pit with the boot cpu mask. x86 might make it global
  163. * when it is used as broadcast device later.
  164. */
  165. i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
  166. clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
  167. 0xF, 0x7FFF);
  168. }
  169. #endif