i8253.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * i8253.c 8253/PIT functions
  3. *
  4. */
  5. #include <linux/clocksource.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/jiffies.h>
  8. #include <linux/sysdev.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <asm/smp.h>
  12. #include <asm/delay.h>
  13. #include <asm/i8253.h>
  14. #include <asm/io.h>
  15. #include "io_ports.h"
  16. DEFINE_SPINLOCK(i8253_lock);
  17. EXPORT_SYMBOL(i8253_lock);
  18. void setup_pit_timer(void)
  19. {
  20. unsigned long flags;
  21. spin_lock_irqsave(&i8253_lock, flags);
  22. outb_p(0x34,PIT_MODE); /* binary, mode 2, LSB/MSB, ch 0 */
  23. udelay(10);
  24. outb_p(LATCH & 0xff , PIT_CH0); /* LSB */
  25. udelay(10);
  26. outb(LATCH >> 8 , PIT_CH0); /* MSB */
  27. spin_unlock_irqrestore(&i8253_lock, flags);
  28. }
  29. /*
  30. * Since the PIT overflows every tick, its not very useful
  31. * to just read by itself. So use jiffies to emulate a free
  32. * running counter:
  33. */
  34. static cycle_t pit_read(void)
  35. {
  36. unsigned long flags;
  37. int count;
  38. u64 jifs;
  39. spin_lock_irqsave(&i8253_lock, flags);
  40. outb_p(0x00, PIT_MODE); /* latch the count ASAP */
  41. count = inb_p(PIT_CH0); /* read the latched count */
  42. count |= inb_p(PIT_CH0) << 8;
  43. /* VIA686a test code... reset the latch if count > max + 1 */
  44. if (count > LATCH) {
  45. outb_p(0x34, PIT_MODE);
  46. outb_p(LATCH & 0xff, PIT_CH0);
  47. outb(LATCH >> 8, PIT_CH0);
  48. count = LATCH - 1;
  49. }
  50. spin_unlock_irqrestore(&i8253_lock, flags);
  51. jifs = jiffies_64;
  52. jifs -= INITIAL_JIFFIES;
  53. count = (LATCH-1) - count;
  54. return (cycle_t)(jifs * LATCH) + count;
  55. }
  56. static struct clocksource clocksource_pit = {
  57. .name = "pit",
  58. .rating = 110,
  59. .read = pit_read,
  60. .mask = CLOCKSOURCE_MASK(64),
  61. .mult = 0,
  62. .shift = 20,
  63. };
  64. static int __init init_pit_clocksource(void)
  65. {
  66. if (num_possible_cpus() > 4) /* PIT does not scale! */
  67. return 0;
  68. clocksource_pit.mult = clocksource_hz2mult(CLOCK_TICK_RATE, 20);
  69. return clocksource_register(&clocksource_pit);
  70. }
  71. module_init(init_pit_clocksource);