i8253.c 3.4 KB

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