i8253.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * i8253 PIT clocksource
  3. */
  4. #include <linux/clocksource.h>
  5. #include <linux/init.h>
  6. #include <linux/io.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/timex.h>
  9. #include <asm/i8253.h>
  10. /*
  11. * Since the PIT overflows every tick, its not very useful
  12. * to just read by itself. So use jiffies to emulate a free
  13. * running counter:
  14. */
  15. static cycle_t i8253_read(struct clocksource *cs)
  16. {
  17. static int old_count;
  18. static u32 old_jifs;
  19. unsigned long flags;
  20. int count;
  21. u32 jifs;
  22. raw_spin_lock_irqsave(&i8253_lock, flags);
  23. /*
  24. * Although our caller may have the read side of xtime_lock,
  25. * this is now a seqlock, and we are cheating in this routine
  26. * by having side effects on state that we cannot undo if
  27. * there is a collision on the seqlock and our caller has to
  28. * retry. (Namely, old_jifs and old_count.) So we must treat
  29. * jiffies as volatile despite the lock. We read jiffies
  30. * before latching the timer count to guarantee that although
  31. * the jiffies value might be older than the count (that is,
  32. * the counter may underflow between the last point where
  33. * jiffies was incremented and the point where we latch the
  34. * count), it cannot be newer.
  35. */
  36. jifs = jiffies;
  37. outb_pit(0x00, PIT_MODE); /* latch the count ASAP */
  38. count = inb_pit(PIT_CH0); /* read the latched count */
  39. count |= inb_pit(PIT_CH0) << 8;
  40. /* VIA686a test code... reset the latch if count > max + 1 */
  41. if (count > LATCH) {
  42. outb_pit(0x34, PIT_MODE);
  43. outb_pit(PIT_LATCH & 0xff, PIT_CH0);
  44. outb_pit(PIT_LATCH >> 8, PIT_CH0);
  45. count = PIT_LATCH - 1;
  46. }
  47. /*
  48. * It's possible for count to appear to go the wrong way for a
  49. * couple of reasons:
  50. *
  51. * 1. The timer counter underflows, but we haven't handled the
  52. * resulting interrupt and incremented jiffies yet.
  53. * 2. Hardware problem with the timer, not giving us continuous time,
  54. * the counter does small "jumps" upwards on some Pentium systems,
  55. * (see c't 95/10 page 335 for Neptun bug.)
  56. *
  57. * Previous attempts to handle these cases intelligently were
  58. * buggy, so we just do the simple thing now.
  59. */
  60. if (count > old_count && jifs == old_jifs)
  61. count = old_count;
  62. old_count = count;
  63. old_jifs = jifs;
  64. raw_spin_unlock_irqrestore(&i8253_lock, flags);
  65. count = (PIT_LATCH - 1) - count;
  66. return (cycle_t)(jifs * PIT_LATCH) + count;
  67. }
  68. static struct clocksource i8253_cs = {
  69. .name = "pit",
  70. .rating = 110,
  71. .read = i8253_read,
  72. .mask = CLOCKSOURCE_MASK(32),
  73. };
  74. int __init clocksource_i8253_init(void)
  75. {
  76. return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
  77. }