Skip to content

Commit 89c0b8e

Browse files
author
Russell King
committed
clocksource: add common i8253 PIT clocksource
This is based upon both arch/arm/mach-footbridge/isa-timer.c and arch/x86/kernel/i8253.c. Acked-by: John Stultz <john.stultz@linaro.org> Acked-by: Thomas Gleixner <tglx@linutronix.de> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Ralf Baechle <ralf@linux-mips.org> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent 8b06161 commit 89c0b8e

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎drivers/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,7 @@ source "drivers/platform/Kconfig"
119119
source "drivers/clk/Kconfig"
120120

121121
source "drivers/hwspinlock/Kconfig"
122+
123+
source "drivers/clocksource/Kconfig"
124+
122125
endmenu

‎drivers/clocksource/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config CLKSRC_I8253
2+
bool

‎drivers/clocksource/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ obj-$(CONFIG_CS5535_CLOCK_EVENT_SRC) += cs5535-clockevt.o
66
obj-$(CONFIG_SH_TIMER_CMT) += sh_cmt.o
77
obj-$(CONFIG_SH_TIMER_MTU2) += sh_mtu2.o
88
obj-$(CONFIG_SH_TIMER_TMU) += sh_tmu.o
9+
obj-$(CONFIG_CLKSRC_I8253) += i8253.o

‎drivers/clocksource/i8253.c‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* i8253 PIT clocksource
3+
*/
4+
#include <linux/clocksource.h>
5+
#include <linux/init.h>
6+
#include <linux/io.h>
7+
#include <linux/spinlock.h>
8+
#include <linux/timex.h>
9+
10+
#include <asm/i8253.h>
11+
12+
/*
13+
* Since the PIT overflows every tick, its not very useful
14+
* to just read by itself. So use jiffies to emulate a free
15+
* running counter:
16+
*/
17+
static cycle_t i8253_read(struct clocksource *cs)
18+
{
19+
static int old_count;
20+
static u32 old_jifs;
21+
unsigned long flags;
22+
int count;
23+
u32 jifs;
24+
25+
raw_spin_lock_irqsave(&i8253_lock, flags);
26+
/*
27+
* Although our caller may have the read side of xtime_lock,
28+
* this is now a seqlock, and we are cheating in this routine
29+
* by having side effects on state that we cannot undo if
30+
* there is a collision on the seqlock and our caller has to
31+
* retry. (Namely, old_jifs and old_count.) So we must treat
32+
* jiffies as volatile despite the lock. We read jiffies
33+
* before latching the timer count to guarantee that although
34+
* the jiffies value might be older than the count (that is,
35+
* the counter may underflow between the last point where
36+
* jiffies was incremented and the point where we latch the
37+
* count), it cannot be newer.
38+
*/
39+
jifs = jiffies;
40+
outb_pit(0x00, PIT_MODE); /* latch the count ASAP */
41+
count = inb_pit(PIT_CH0); /* read the latched count */
42+
count |= inb_pit(PIT_CH0) << 8;
43+
44+
/* VIA686a test code... reset the latch if count > max + 1 */
45+
if (count > LATCH) {
46+
outb_pit(0x34, PIT_MODE);
47+
outb_pit(PIT_LATCH & 0xff, PIT_CH0);
48+
outb_pit(PIT_LATCH >> 8, PIT_CH0);
49+
count = PIT_LATCH - 1;
50+
}
51+
52+
/*
53+
* It's possible for count to appear to go the wrong way for a
54+
* couple of reasons:
55+
*
56+
* 1. The timer counter underflows, but we haven't handled the
57+
* resulting interrupt and incremented jiffies yet.
58+
* 2. Hardware problem with the timer, not giving us continuous time,
59+
* the counter does small "jumps" upwards on some Pentium systems,
60+
* (see c't 95/10 page 335 for Neptun bug.)
61+
*
62+
* Previous attempts to handle these cases intelligently were
63+
* buggy, so we just do the simple thing now.
64+
*/
65+
if (count > old_count && jifs == old_jifs)
66+
count = old_count;
67+
68+
old_count = count;
69+
old_jifs = jifs;
70+
71+
raw_spin_unlock_irqrestore(&i8253_lock, flags);
72+
73+
count = (PIT_LATCH - 1) - count;
74+
75+
return (cycle_t)(jifs * PIT_LATCH) + count;
76+
}
77+
78+
static struct clocksource i8253_cs = {
79+
.name = "pit",
80+
.rating = 110,
81+
.read = i8253_read,
82+
.mask = CLOCKSOURCE_MASK(32),
83+
};
84+
85+
int __init clocksource_i8253_init(void)
86+
{
87+
return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
88+
}

0 commit comments

Comments
 (0)