diff options
Diffstat (limited to 'arch/i386/kernel/timers/common.c')
-rw-r--r-- | arch/i386/kernel/timers/common.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/arch/i386/kernel/timers/common.c b/arch/i386/kernel/timers/common.c new file mode 100644 index 000000000000..f7f90005e22e --- /dev/null +++ b/arch/i386/kernel/timers/common.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /* | ||
2 | * Common functions used across the timers go here | ||
3 | */ | ||
4 | |||
5 | #include <linux/init.h> | ||
6 | #include <linux/timex.h> | ||
7 | #include <linux/errno.h> | ||
8 | #include <linux/jiffies.h> | ||
9 | |||
10 | #include <asm/io.h> | ||
11 | #include <asm/timer.h> | ||
12 | #include <asm/hpet.h> | ||
13 | |||
14 | #include "mach_timer.h" | ||
15 | |||
16 | /* ------ Calibrate the TSC ------- | ||
17 | * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset(). | ||
18 | * Too much 64-bit arithmetic here to do this cleanly in C, and for | ||
19 | * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2) | ||
20 | * output busy loop as low as possible. We avoid reading the CTC registers | ||
21 | * directly because of the awkward 8-bit access mechanism of the 82C54 | ||
22 | * device. | ||
23 | */ | ||
24 | |||
25 | #define CALIBRATE_TIME (5 * 1000020/HZ) | ||
26 | |||
27 | unsigned long __init calibrate_tsc(void) | ||
28 | { | ||
29 | mach_prepare_counter(); | ||
30 | |||
31 | { | ||
32 | unsigned long startlow, starthigh; | ||
33 | unsigned long endlow, endhigh; | ||
34 | unsigned long count; | ||
35 | |||
36 | rdtsc(startlow,starthigh); | ||
37 | mach_countup(&count); | ||
38 | rdtsc(endlow,endhigh); | ||
39 | |||
40 | |||
41 | /* Error: ECTCNEVERSET */ | ||
42 | if (count <= 1) | ||
43 | goto bad_ctc; | ||
44 | |||
45 | /* 64-bit subtract - gcc just messes up with long longs */ | ||
46 | __asm__("subl %2,%0\n\t" | ||
47 | "sbbl %3,%1" | ||
48 | :"=a" (endlow), "=d" (endhigh) | ||
49 | :"g" (startlow), "g" (starthigh), | ||
50 | "0" (endlow), "1" (endhigh)); | ||
51 | |||
52 | /* Error: ECPUTOOFAST */ | ||
53 | if (endhigh) | ||
54 | goto bad_ctc; | ||
55 | |||
56 | /* Error: ECPUTOOSLOW */ | ||
57 | if (endlow <= CALIBRATE_TIME) | ||
58 | goto bad_ctc; | ||
59 | |||
60 | __asm__("divl %2" | ||
61 | :"=a" (endlow), "=d" (endhigh) | ||
62 | :"r" (endlow), "0" (0), "1" (CALIBRATE_TIME)); | ||
63 | |||
64 | return endlow; | ||
65 | } | ||
66 | |||
67 | /* | ||
68 | * The CTC wasn't reliable: we got a hit on the very first read, | ||
69 | * or the CPU was so fast/slow that the quotient wouldn't fit in | ||
70 | * 32 bits.. | ||
71 | */ | ||
72 | bad_ctc: | ||
73 | return 0; | ||
74 | } | ||
75 | |||
76 | #ifdef CONFIG_HPET_TIMER | ||
77 | /* ------ Calibrate the TSC using HPET ------- | ||
78 | * Return 2^32 * (1 / (TSC clocks per usec)) for getting the CPU freq. | ||
79 | * Second output is parameter 1 (when non NULL) | ||
80 | * Set 2^32 * (1 / (tsc per HPET clk)) for delay_hpet(). | ||
81 | * calibrate_tsc() calibrates the processor TSC by comparing | ||
82 | * it to the HPET timer of known frequency. | ||
83 | * Too much 64-bit arithmetic here to do this cleanly in C | ||
84 | */ | ||
85 | #define CALIBRATE_CNT_HPET (5 * hpet_tick) | ||
86 | #define CALIBRATE_TIME_HPET (5 * KERNEL_TICK_USEC) | ||
87 | |||
88 | unsigned long __init calibrate_tsc_hpet(unsigned long *tsc_hpet_quotient_ptr) | ||
89 | { | ||
90 | unsigned long tsc_startlow, tsc_starthigh; | ||
91 | unsigned long tsc_endlow, tsc_endhigh; | ||
92 | unsigned long hpet_start, hpet_end; | ||
93 | unsigned long result, remain; | ||
94 | |||
95 | hpet_start = hpet_readl(HPET_COUNTER); | ||
96 | rdtsc(tsc_startlow, tsc_starthigh); | ||
97 | do { | ||
98 | hpet_end = hpet_readl(HPET_COUNTER); | ||
99 | } while ((hpet_end - hpet_start) < CALIBRATE_CNT_HPET); | ||
100 | rdtsc(tsc_endlow, tsc_endhigh); | ||
101 | |||
102 | /* 64-bit subtract - gcc just messes up with long longs */ | ||
103 | __asm__("subl %2,%0\n\t" | ||
104 | "sbbl %3,%1" | ||
105 | :"=a" (tsc_endlow), "=d" (tsc_endhigh) | ||
106 | :"g" (tsc_startlow), "g" (tsc_starthigh), | ||
107 | "0" (tsc_endlow), "1" (tsc_endhigh)); | ||
108 | |||
109 | /* Error: ECPUTOOFAST */ | ||
110 | if (tsc_endhigh) | ||
111 | goto bad_calibration; | ||
112 | |||
113 | /* Error: ECPUTOOSLOW */ | ||
114 | if (tsc_endlow <= CALIBRATE_TIME_HPET) | ||
115 | goto bad_calibration; | ||
116 | |||
117 | ASM_DIV64_REG(result, remain, tsc_endlow, 0, CALIBRATE_TIME_HPET); | ||
118 | if (remain > (tsc_endlow >> 1)) | ||
119 | result++; /* rounding the result */ | ||
120 | |||
121 | if (tsc_hpet_quotient_ptr) { | ||
122 | unsigned long tsc_hpet_quotient; | ||
123 | |||
124 | ASM_DIV64_REG(tsc_hpet_quotient, remain, tsc_endlow, 0, | ||
125 | CALIBRATE_CNT_HPET); | ||
126 | if (remain > (tsc_endlow >> 1)) | ||
127 | tsc_hpet_quotient++; /* rounding the result */ | ||
128 | *tsc_hpet_quotient_ptr = tsc_hpet_quotient; | ||
129 | } | ||
130 | |||
131 | return result; | ||
132 | bad_calibration: | ||
133 | /* | ||
134 | * the CPU was so fast/slow that the quotient wouldn't fit in | ||
135 | * 32 bits.. | ||
136 | */ | ||
137 | return 0; | ||
138 | } | ||
139 | #endif | ||
140 | |||
141 | /* calculate cpu_khz */ | ||
142 | void __init init_cpu_khz(void) | ||
143 | { | ||
144 | if (cpu_has_tsc) { | ||
145 | unsigned long tsc_quotient = calibrate_tsc(); | ||
146 | if (tsc_quotient) { | ||
147 | /* report CPU clock rate in Hz. | ||
148 | * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) = | ||
149 | * clock/second. Our precision is about 100 ppm. | ||
150 | */ | ||
151 | { unsigned long eax=0, edx=1000; | ||
152 | __asm__("divl %2" | ||
153 | :"=a" (cpu_khz), "=d" (edx) | ||
154 | :"r" (tsc_quotient), | ||
155 | "0" (eax), "1" (edx)); | ||
156 | printk("Detected %lu.%03lu MHz processor.\n", cpu_khz / 1000, cpu_khz % 1000); | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | } | ||