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