i8253.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * 8253/PIT functions
  3. *
  4. */
  5. #include <linux/clockchips.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/module.h>
  10. #include <linux/timex.h>
  11. #include <linux/delay.h>
  12. #include <linux/i8253.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <asm/hpet.h>
  16. #include <asm/time.h>
  17. #include <asm/smp.h>
  18. /*
  19. * HPET replaces the PIT, when enabled. So we need to know, which of
  20. * the two timers is used
  21. */
  22. struct clock_event_device *global_clock_event;
  23. /*
  24. * Initialize the PIT timer.
  25. *
  26. * This is also called after resume to bring the PIT into operation again.
  27. */
  28. static void init_pit_timer(enum clock_event_mode mode,
  29. struct clock_event_device *evt)
  30. {
  31. raw_spin_lock(&i8253_lock);
  32. switch (mode) {
  33. case CLOCK_EVT_MODE_PERIODIC:
  34. /* binary, mode 2, LSB/MSB, ch 0 */
  35. outb_pit(0x34, PIT_MODE);
  36. outb_pit(LATCH & 0xff , PIT_CH0); /* LSB */
  37. outb_pit(LATCH >> 8 , PIT_CH0); /* MSB */
  38. break;
  39. case CLOCK_EVT_MODE_SHUTDOWN:
  40. case CLOCK_EVT_MODE_UNUSED:
  41. if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
  42. evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  43. outb_pit(0x30, PIT_MODE);
  44. outb_pit(0, PIT_CH0);
  45. outb_pit(0, PIT_CH0);
  46. }
  47. break;
  48. case CLOCK_EVT_MODE_ONESHOT:
  49. /* One shot setup */
  50. outb_pit(0x38, PIT_MODE);
  51. break;
  52. case CLOCK_EVT_MODE_RESUME:
  53. /* Nothing to do here */
  54. break;
  55. }
  56. raw_spin_unlock(&i8253_lock);
  57. }
  58. /*
  59. * Program the next event in oneshot mode
  60. *
  61. * Delta is given in PIT ticks
  62. */
  63. static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
  64. {
  65. raw_spin_lock(&i8253_lock);
  66. outb_pit(delta & 0xff , PIT_CH0); /* LSB */
  67. outb_pit(delta >> 8 , PIT_CH0); /* MSB */
  68. raw_spin_unlock(&i8253_lock);
  69. return 0;
  70. }
  71. /*
  72. * On UP the PIT can serve all of the possible timer functions. On SMP systems
  73. * it can be solely used for the global tick.
  74. *
  75. * The profiling and update capabilities are switched off once the local apic is
  76. * registered. This mechanism replaces the previous #ifdef LOCAL_APIC -
  77. * !using_apic_timer decisions in do_timer_interrupt_hook()
  78. */
  79. static struct clock_event_device pit_ce = {
  80. .name = "pit",
  81. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  82. .set_mode = init_pit_timer,
  83. .set_next_event = pit_next_event,
  84. .irq = 0,
  85. };
  86. /*
  87. * Initialize the conversion factor and the min/max deltas of the clock event
  88. * structure and register the clock event source with the framework.
  89. */
  90. void __init setup_pit_timer(void)
  91. {
  92. /*
  93. * Start pit with the boot cpu mask and make it global after the
  94. * IO_APIC has been initialized.
  95. */
  96. pit_ce.cpumask = cpumask_of(smp_processor_id());
  97. clockevents_config_and_register(&pit_ce, CLOCK_TICK_RATE, 0xF, 0x7FFF);
  98. global_clock_event = &pit_ce;
  99. }
  100. #ifndef CONFIG_X86_64
  101. static int __init init_pit_clocksource(void)
  102. {
  103. /*
  104. * Several reasons not to register PIT as a clocksource:
  105. *
  106. * - On SMP PIT does not scale due to i8253_lock
  107. * - when HPET is enabled
  108. * - when local APIC timer is active (PIT is switched off)
  109. */
  110. if (num_possible_cpus() > 1 || is_hpet_enabled() ||
  111. pit_ce.mode != CLOCK_EVT_MODE_PERIODIC)
  112. return 0;
  113. return clocksource_i8253_init();
  114. }
  115. arch_initcall(init_pit_clocksource);
  116. #endif /* !CONFIG_X86_64 */