i8253.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_SPINLOCK(i8253_lock);
  18. EXPORT_SYMBOL(i8253_lock);
  19. #ifdef CONFIG_X86_32
  20. static void pit_disable_clocksource(void);
  21. #else
  22. static inline void pit_disable_clocksource(void) { }
  23. #endif
  24. /*
  25. * HPET replaces the PIT, when enabled. So we need to know, which of
  26. * the two timers is used
  27. */
  28. struct clock_event_device *global_clock_event;
  29. /*
  30. * Initialize the PIT timer.
  31. *
  32. * This is also called after resume to bring the PIT into operation again.
  33. */
  34. static void init_pit_timer(enum clock_event_mode mode,
  35. struct clock_event_device *evt)
  36. {
  37. spin_lock(&i8253_lock);
  38. switch (mode) {
  39. case CLOCK_EVT_MODE_PERIODIC:
  40. /* binary, mode 2, LSB/MSB, ch 0 */
  41. outb_pit(0x34, PIT_MODE);
  42. outb_pit(LATCH & 0xff , PIT_CH0); /* LSB */
  43. outb_pit(LATCH >> 8 , PIT_CH0); /* MSB */
  44. break;
  45. case CLOCK_EVT_MODE_SHUTDOWN:
  46. case CLOCK_EVT_MODE_UNUSED:
  47. if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
  48. evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  49. outb_pit(0x30, PIT_MODE);
  50. outb_pit(0, PIT_CH0);
  51. outb_pit(0, PIT_CH0);
  52. }
  53. pit_disable_clocksource();
  54. break;
  55. case CLOCK_EVT_MODE_ONESHOT:
  56. /* One shot setup */
  57. pit_disable_clocksource();
  58. outb_pit(0x38, PIT_MODE);
  59. break;
  60. case CLOCK_EVT_MODE_RESUME:
  61. /* Nothing to do here */
  62. break;
  63. }
  64. spin_unlock(&i8253_lock);
  65. }
  66. /*
  67. * Program the next event in oneshot mode
  68. *
  69. * Delta is given in PIT ticks
  70. */
  71. static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
  72. {
  73. spin_lock(&i8253_lock);
  74. outb_pit(delta & 0xff , PIT_CH0); /* LSB */
  75. outb_pit(delta >> 8 , PIT_CH0); /* MSB */
  76. spin_unlock(&i8253_lock);
  77. return 0;
  78. }
  79. /*
  80. * On UP the PIT can serve all of the possible timer functions. On SMP systems
  81. * it can be solely used for the global tick.
  82. *
  83. * The profiling and update capabilities are switched off once the local apic is
  84. * registered. This mechanism replaces the previous #ifdef LOCAL_APIC -
  85. * !using_apic_timer decisions in do_timer_interrupt_hook()
  86. */
  87. static struct clock_event_device pit_ce = {
  88. .name = "pit",
  89. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  90. .set_mode = init_pit_timer,
  91. .set_next_event = pit_next_event,
  92. .shift = 32,
  93. .irq = 0,
  94. };
  95. /*
  96. * Initialize the conversion factor and the min/max deltas of the clock event
  97. * structure and register the clock event source with the framework.
  98. */
  99. void __init setup_pit_timer(void)
  100. {
  101. /*
  102. * Start pit with the boot cpu mask and make it global after the
  103. * IO_APIC has been initialized.
  104. */
  105. pit_ce.cpumask = cpumask_of(smp_processor_id());
  106. pit_ce.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, pit_ce.shift);
  107. pit_ce.max_delta_ns = clockevent_delta2ns(0x7FFF, &pit_ce);
  108. pit_ce.min_delta_ns = clockevent_delta2ns(0xF, &pit_ce);
  109. clockevents_register_device(&pit_ce);
  110. global_clock_event = &pit_ce;
  111. }
  112. #ifndef CONFIG_X86_64
  113. /*
  114. * Since the PIT overflows every tick, its not very useful
  115. * to just read by itself. So use jiffies to emulate a free
  116. * running counter:
  117. */
  118. static cycle_t pit_read(struct clocksource *cs)
  119. {
  120. static int old_count;
  121. static u32 old_jifs;
  122. unsigned long flags;
  123. int count;
  124. u32 jifs;
  125. spin_lock_irqsave(&i8253_lock, flags);
  126. /*
  127. * Although our caller may have the read side of xtime_lock,
  128. * this is now a seqlock, and we are cheating in this routine
  129. * by having side effects on state that we cannot undo if
  130. * there is a collision on the seqlock and our caller has to
  131. * retry. (Namely, old_jifs and old_count.) So we must treat
  132. * jiffies as volatile despite the lock. We read jiffies
  133. * before latching the timer count to guarantee that although
  134. * the jiffies value might be older than the count (that is,
  135. * the counter may underflow between the last point where
  136. * jiffies was incremented and the point where we latch the
  137. * count), it cannot be newer.
  138. */
  139. jifs = jiffies;
  140. outb_pit(0x00, PIT_MODE); /* latch the count ASAP */
  141. count = inb_pit(PIT_CH0); /* read the latched count */
  142. count |= inb_pit(PIT_CH0) << 8;
  143. /* VIA686a test code... reset the latch if count > max + 1 */
  144. if (count > LATCH) {
  145. outb_pit(0x34, PIT_MODE);
  146. outb_pit(LATCH & 0xff, PIT_CH0);
  147. outb_pit(LATCH >> 8, PIT_CH0);
  148. count = LATCH - 1;
  149. }
  150. /*
  151. * It's possible for count to appear to go the wrong way for a
  152. * couple of reasons:
  153. *
  154. * 1. The timer counter underflows, but we haven't handled the
  155. * resulting interrupt and incremented jiffies yet.
  156. * 2. Hardware problem with the timer, not giving us continuous time,
  157. * the counter does small "jumps" upwards on some Pentium systems,
  158. * (see c't 95/10 page 335 for Neptun bug.)
  159. *
  160. * Previous attempts to handle these cases intelligently were
  161. * buggy, so we just do the simple thing now.
  162. */
  163. if (count > old_count && jifs == old_jifs)
  164. count = old_count;
  165. old_count = count;
  166. old_jifs = jifs;
  167. spin_unlock_irqrestore(&i8253_lock, flags);
  168. count = (LATCH - 1) - count;
  169. return (cycle_t)(jifs * LATCH) + count;
  170. }
  171. static struct clocksource pit_cs = {
  172. .name = "pit",
  173. .rating = 110,
  174. .read = pit_read,
  175. .mask = CLOCKSOURCE_MASK(32),
  176. .mult = 0,
  177. .shift = 20,
  178. };
  179. static void pit_disable_clocksource(void)
  180. {
  181. /*
  182. * Use mult to check whether it is registered or not
  183. */
  184. if (pit_cs.mult) {
  185. clocksource_unregister(&pit_cs);
  186. pit_cs.mult = 0;
  187. }
  188. }
  189. static int __init init_pit_clocksource(void)
  190. {
  191. /*
  192. * Several reasons not to register PIT as a clocksource:
  193. *
  194. * - On SMP PIT does not scale due to i8253_lock
  195. * - when HPET is enabled
  196. * - when local APIC timer is active (PIT is switched off)
  197. */
  198. if (num_possible_cpus() > 1 || is_hpet_enabled() ||
  199. pit_ce.mode != CLOCK_EVT_MODE_PERIODIC)
  200. return 0;
  201. pit_cs.mult = clocksource_hz2mult(CLOCK_TICK_RATE, pit_cs.shift);
  202. return clocksource_register(&pit_cs);
  203. }
  204. arch_initcall(init_pit_clocksource);
  205. #endif /* !CONFIG_X86_64 */