i8253.c 4.8 KB

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