/*1* i8253 PIT clocksource2*/3#include <linux/clocksource.h>4#include <linux/init.h>5#include <linux/io.h>6#include <linux/spinlock.h>7#include <linux/timex.h>89#include <asm/i8253.h>1011/*12* Since the PIT overflows every tick, its not very useful13* to just read by itself. So use jiffies to emulate a free14* running counter:15*/16static cycle_t i8253_read(struct clocksource *cs)17{18static int old_count;19static u32 old_jifs;20unsigned long flags;21int count;22u32 jifs;2324raw_spin_lock_irqsave(&i8253_lock, flags);25/*26* Although our caller may have the read side of xtime_lock,27* this is now a seqlock, and we are cheating in this routine28* by having side effects on state that we cannot undo if29* there is a collision on the seqlock and our caller has to30* retry. (Namely, old_jifs and old_count.) So we must treat31* jiffies as volatile despite the lock. We read jiffies32* before latching the timer count to guarantee that although33* the jiffies value might be older than the count (that is,34* the counter may underflow between the last point where35* jiffies was incremented and the point where we latch the36* count), it cannot be newer.37*/38jifs = jiffies;39outb_pit(0x00, PIT_MODE); /* latch the count ASAP */40count = inb_pit(PIT_CH0); /* read the latched count */41count |= inb_pit(PIT_CH0) << 8;4243/* VIA686a test code... reset the latch if count > max + 1 */44if (count > LATCH) {45outb_pit(0x34, PIT_MODE);46outb_pit(PIT_LATCH & 0xff, PIT_CH0);47outb_pit(PIT_LATCH >> 8, PIT_CH0);48count = PIT_LATCH - 1;49}5051/*52* It's possible for count to appear to go the wrong way for a53* couple of reasons:54*55* 1. The timer counter underflows, but we haven't handled the56* resulting interrupt and incremented jiffies yet.57* 2. Hardware problem with the timer, not giving us continuous time,58* the counter does small "jumps" upwards on some Pentium systems,59* (see c't 95/10 page 335 for Neptun bug.)60*61* Previous attempts to handle these cases intelligently were62* buggy, so we just do the simple thing now.63*/64if (count > old_count && jifs == old_jifs)65count = old_count;6667old_count = count;68old_jifs = jifs;6970raw_spin_unlock_irqrestore(&i8253_lock, flags);7172count = (PIT_LATCH - 1) - count;7374return (cycle_t)(jifs * PIT_LATCH) + count;75}7677static struct clocksource i8253_cs = {78.name = "pit",79.rating = 110,80.read = i8253_read,81.mask = CLOCKSOURCE_MASK(32),82};8384int __init clocksource_i8253_init(void)85{86return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);87}888990