2 * Copyright (c) 2012 The Chromium OS Authors.
4 * TSC calibration codes are adapted from Linux kernel
5 * arch/x86/kernel/tsc_msr.c and arch/x86/kernel/tsc.c
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/i8254.h>
14 #include <asm/ibmpc.h>
16 #include <asm/u-boot-x86.h>
18 /* CPU reference clock frequency: in KHz */
20 #define FREQ_100 99840
21 #define FREQ_133 133200
22 #define FREQ_166 166400
24 #define MAX_NUM_FREQS 8
26 DECLARE_GLOBAL_DATA_PTR;
29 * According to Intel 64 and IA-32 System Programming Guide,
30 * if MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
31 * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
32 * Unfortunately some Intel Atom SoCs aren't quite compliant to this,
33 * so we need manually differentiate SoC families. This is what the
34 * field msr_plat does.
37 u8 x86_family; /* CPU family */
38 u8 x86_model; /* model */
39 u8 msr_plat; /* 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
40 u32 freqs[MAX_NUM_FREQS];
43 static struct freq_desc freq_desc_tables[] = {
45 { 6, 0x27, 0, { 0, 0, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
47 { 6, 0x35, 0, { 0, FREQ_133, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
49 { 6, 0x4a, 1, { 0, FREQ_100, FREQ_133, 0, 0, 0, 0, 0 } },
51 { 6, 0x37, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_166, 0, 0, 0, 0 } },
53 { 6, 0x5a, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_100, 0, 0, 0, 0 } },
56 static int match_cpu(u8 family, u8 model)
60 for (i = 0; i < ARRAY_SIZE(freq_desc_tables); i++) {
61 if ((family == freq_desc_tables[i].x86_family) &&
62 (model == freq_desc_tables[i].x86_model))
69 /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */
70 #define id_to_freq(cpu_index, freq_id) \
71 (freq_desc_tables[cpu_index].freqs[freq_id])
74 * Do MSR calibration only for known/supported CPUs.
76 * Returns the calibration value or 0 if MSR calibration failed.
78 static unsigned long try_msr_calibrate_tsc(void)
80 u32 lo, hi, ratio, freq_id, freq;
84 cpu_index = match_cpu(gd->arch.x86, gd->arch.x86_model);
88 if (freq_desc_tables[cpu_index].msr_plat) {
89 rdmsr(MSR_PLATFORM_INFO, lo, hi);
90 ratio = (lo >> 8) & 0x1f;
92 rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
93 ratio = (hi >> 8) & 0x1f;
95 debug("Maximum core-clock to bus-clock ratio: 0x%x\n", ratio);
100 /* Get FSB FREQ ID */
101 rdmsr(MSR_FSB_FREQ, lo, hi);
103 freq = id_to_freq(cpu_index, freq_id);
104 debug("Resolved frequency ID: %u, frequency: %u KHz\n", freq_id, freq);
108 /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
109 res = freq * ratio / 1000;
110 debug("TSC runs at %lu MHz\n", res);
115 debug("Fast TSC calibration using MSR failed\n");
120 * This reads the current MSB of the PIT counter, and
121 * checks if we are running on sufficiently fast and
122 * non-virtualized hardware.
124 * Our expectations are:
126 * - the PIT is running at roughly 1.19MHz
128 * - each IO is going to take about 1us on real hardware,
129 * but we allow it to be much faster (by a factor of 10) or
130 * _slightly_ slower (ie we allow up to a 2us read+counter
131 * update - anything else implies a unacceptably slow CPU
132 * or PIT for the fast calibration to work.
134 * - with 256 PIT ticks to read the value, we have 214us to
135 * see the same MSB (and overhead like doing a single TSC
136 * read per MSB value etc).
138 * - We're doing 2 reads per loop (LSB, MSB), and we expect
139 * them each to take about a microsecond on real hardware.
140 * So we expect a count value of around 100. But we'll be
141 * generous, and accept anything over 50.
143 * - if the PIT is stuck, and we see *many* more reads, we
144 * return early (and the next caller of pit_expect_msb()
145 * then consider it a failure when they don't see the
146 * next expected value).
148 * These expectations mean that we know that we have seen the
149 * transition from one expected value to another with a fairly
150 * high accuracy, and we didn't miss any events. We can thus
151 * use the TSC value at the transitions to calculate a pretty
152 * good value for the TSC frequencty.
154 static inline int pit_verify_msb(unsigned char val)
158 return inb(0x42) == val;
161 static inline int pit_expect_msb(unsigned char val, u64 *tscp,
162 unsigned long *deltap)
165 u64 tsc = 0, prev_tsc = 0;
167 for (count = 0; count < 50000; count++) {
168 if (!pit_verify_msb(val))
173 *deltap = rdtsc() - prev_tsc;
177 * We require _some_ success, but the quality control
178 * will be based on the error terms on the TSC values.
184 * How many MSB values do we want to see? We aim for
185 * a maximum error rate of 500ppm (in practice the
186 * real error is much smaller), but refuse to spend
187 * more than 50ms on it.
189 #define MAX_QUICK_PIT_MS 50
190 #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
192 static unsigned long quick_pit_calibrate(void)
196 unsigned long d1, d2;
198 /* Set the Gate high, disable speaker */
199 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
202 * Counter 2, mode 0 (one-shot), binary count
204 * NOTE! Mode 2 decrements by two (and then the
205 * output is flipped each time, giving the same
206 * final output frequency as a decrement-by-one),
207 * so mode 0 is much better when looking at the
212 /* Start at 0xffff */
217 * The PIT starts counting at the next edge, so we
218 * need to delay for a microsecond. The easiest way
219 * to do that is to just read back the 16-bit counter
224 if (pit_expect_msb(0xff, &tsc, &d1)) {
225 for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
226 if (!pit_expect_msb(0xff-i, &delta, &d2))
230 * Iterate until the error is less than 500 ppm
233 if (d1+d2 >= delta >> 11)
237 * Check the PIT one more time to verify that
238 * all TSC reads were stable wrt the PIT.
240 * This also guarantees serialization of the
241 * last cycle read ('d2') in pit_expect_msb.
243 if (!pit_verify_msb(0xfe - i))
248 debug("Fast TSC calibration failed\n");
253 * Ok, if we get here, then we've seen the
254 * MSB of the PIT decrement 'i' times, and the
255 * error has shrunk to less than 500 ppm.
257 * As a result, we can depend on there not being
258 * any odd delays anywhere, and the TSC reads are
259 * reliable (within the error).
261 * kHz = ticks / time-in-seconds / 1000;
262 * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000
263 * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000)
265 delta *= PIT_TICK_RATE;
266 delta /= (i*256*1000);
267 debug("Fast TSC calibration using PIT\n");
271 void timer_set_base(u64 base)
273 gd->arch.tsc_base = base;
277 * Get the number of CPU time counter ticks since it was read first time after
278 * restart. This yields a free running counter guaranteed to take almost 6
279 * years to wrap around even at 100GHz clock rate.
281 u64 __attribute__((no_instrument_function)) get_ticks(void)
283 u64 now_tick = rdtsc();
285 /* We assume that 0 means the base hasn't been set yet */
286 if (!gd->arch.tsc_base)
287 panic("No tick base available");
288 return now_tick - gd->arch.tsc_base;
291 /* Get the speed of the TSC timer in MHz */
292 unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void)
294 unsigned long fast_calibrate;
296 fast_calibrate = try_msr_calibrate_tsc();
298 return fast_calibrate;
300 fast_calibrate = quick_pit_calibrate();
302 panic("TSC frequency is ZERO");
304 return fast_calibrate;
307 unsigned long get_tbclk(void)
309 return get_tbclk_mhz() * 1000 * 1000;
312 static ulong get_ms_timer(void)
314 return (get_ticks() * 1000) / get_tbclk();
317 ulong get_timer(ulong base)
319 return get_ms_timer() - base;
322 ulong __attribute__((no_instrument_function)) timer_get_us(void)
324 return get_ticks() / get_tbclk_mhz();
327 ulong timer_get_boot_us(void)
329 return timer_get_us();
332 void __udelay(unsigned long usec)
334 u64 now = get_ticks();
337 stop = now + usec * get_tbclk_mhz();
339 while ((int64_t)(stop - get_ticks()) > 0)
345 #ifdef CONFIG_SYS_PCAT_TIMER
346 /* Set up the PCAT timer if required */