i8253.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 xtime_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. /*
  92. * Initialize the PIT timer.
  93. *
  94. * This is also called after resume to bring the PIT into operation again.
  95. */
  96. static void init_pit_timer(enum clock_event_mode mode,
  97. struct clock_event_device *evt)
  98. {
  99. raw_spin_lock(&i8253_lock);
  100. switch (mode) {
  101. case CLOCK_EVT_MODE_PERIODIC:
  102. /* binary, mode 2, LSB/MSB, ch 0 */
  103. outb_p(0x34, PIT_MODE);
  104. outb_p(PIT_LATCH & 0xff , PIT_CH0); /* LSB */
  105. outb_p(PIT_LATCH >> 8 , PIT_CH0); /* MSB */
  106. break;
  107. case CLOCK_EVT_MODE_SHUTDOWN:
  108. case CLOCK_EVT_MODE_UNUSED:
  109. if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
  110. evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  111. outb_p(0x30, PIT_MODE);
  112. outb_p(0, PIT_CH0);
  113. outb_p(0, PIT_CH0);
  114. }
  115. break;
  116. case CLOCK_EVT_MODE_ONESHOT:
  117. /* One shot setup */
  118. outb_p(0x38, PIT_MODE);
  119. break;
  120. case CLOCK_EVT_MODE_RESUME:
  121. /* Nothing to do here */
  122. break;
  123. }
  124. raw_spin_unlock(&i8253_lock);
  125. }
  126. /*
  127. * Program the next event in oneshot mode
  128. *
  129. * Delta is given in PIT ticks
  130. */
  131. static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
  132. {
  133. raw_spin_lock(&i8253_lock);
  134. outb_p(delta & 0xff , PIT_CH0); /* LSB */
  135. outb_p(delta >> 8 , PIT_CH0); /* MSB */
  136. raw_spin_unlock(&i8253_lock);
  137. return 0;
  138. }
  139. /*
  140. * On UP the PIT can serve all of the possible timer functions. On SMP systems
  141. * it can be solely used for the global tick.
  142. */
  143. struct clock_event_device i8253_clockevent = {
  144. .name = "pit",
  145. .features = CLOCK_EVT_FEAT_PERIODIC,
  146. .set_mode = init_pit_timer,
  147. .set_next_event = pit_next_event,
  148. };
  149. /*
  150. * Initialize the conversion factor and the min/max deltas of the clock event
  151. * structure and register the clock event source with the framework.
  152. */
  153. void __init clockevent_i8253_init(bool oneshot)
  154. {
  155. if (oneshot)
  156. i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
  157. /*
  158. * Start pit with the boot cpu mask. x86 might make it global
  159. * when it is used as broadcast device later.
  160. */
  161. i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
  162. clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
  163. 0xF, 0x7FFF);
  164. }
  165. #endif