Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
| 4 | * |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 5 | * TSC calibration codes are adapted from Linux kernel |
| 6 | * arch/x86/kernel/tsc_msr.c and arch/x86/kernel/tsc.c |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 12 | #include <timer.h> |
Bin Meng | d159ffb | 2017-07-25 20:12:01 -0700 | [diff] [blame] | 13 | #include <asm/cpu.h> |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <asm/i8254.h> |
| 16 | #include <asm/ibmpc.h> |
| 17 | #include <asm/msr.h> |
| 18 | #include <asm/u-boot-x86.h> |
| 19 | |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 20 | #define MAX_NUM_FREQS 9 |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 21 | |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Christian Gmeiner | 1b1472a | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 24 | static unsigned long cpu_mhz_from_cpuid(void) |
| 25 | { |
| 26 | if (gd->arch.x86_vendor != X86_VENDOR_INTEL) |
| 27 | return 0; |
| 28 | |
| 29 | if (cpuid_eax(0) < 0x16) |
| 30 | return 0; |
| 31 | |
| 32 | return cpuid_eax(0x16); |
| 33 | } |
| 34 | |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 35 | /* |
| 36 | * According to Intel 64 and IA-32 System Programming Guide, |
| 37 | * if MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be |
| 38 | * read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40]. |
| 39 | * Unfortunately some Intel Atom SoCs aren't quite compliant to this, |
| 40 | * so we need manually differentiate SoC families. This is what the |
| 41 | * field msr_plat does. |
| 42 | */ |
| 43 | struct freq_desc { |
| 44 | u8 x86_family; /* CPU family */ |
| 45 | u8 x86_model; /* model */ |
Simon Glass | 40a8c35 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 46 | /* 2: use 100MHz, 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */ |
| 47 | u8 msr_plat; |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 48 | u32 freqs[MAX_NUM_FREQS]; |
| 49 | }; |
| 50 | |
| 51 | static struct freq_desc freq_desc_tables[] = { |
| 52 | /* PNW */ |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 53 | { 6, 0x27, 0, { 0, 0, 0, 0, 0, 99840, 0, 83200, 0 } }, |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 54 | /* CLV+ */ |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 55 | { 6, 0x35, 0, { 0, 133200, 0, 0, 0, 99840, 0, 83200, 0 } }, |
Bin Meng | aeb581a | 2017-07-25 20:12:03 -0700 | [diff] [blame] | 56 | /* TNG - Intel Atom processor Z3400 series */ |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 57 | { 6, 0x4a, 1, { 0, 100000, 133300, 0, 0, 0, 0, 0, 0 } }, |
Bin Meng | aeb581a | 2017-07-25 20:12:03 -0700 | [diff] [blame] | 58 | /* VLV2 - Intel Atom processor E3000, Z3600, Z3700 series */ |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 59 | { 6, 0x37, 1, { 83300, 100000, 133300, 116700, 80000, 0, 0, 0, 0 } }, |
Bin Meng | aeb581a | 2017-07-25 20:12:03 -0700 | [diff] [blame] | 60 | /* ANN - Intel Atom processor Z3500 series */ |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 61 | { 6, 0x5a, 1, { 83300, 100000, 133300, 100000, 0, 0, 0, 0, 0 } }, |
| 62 | /* AMT - Intel Atom processor X7-Z8000 and X5-Z8000 series */ |
| 63 | { 6, 0x4c, 1, { 83300, 100000, 133300, 116700, |
| 64 | 80000, 93300, 90000, 88900, 87500 } }, |
Simon Glass | 40a8c35 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 65 | /* Ivybridge */ |
Bin Meng | 7943dc2 | 2017-08-15 22:41:50 -0700 | [diff] [blame] | 66 | { 6, 0x3a, 2, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static int match_cpu(u8 family, u8 model) |
| 70 | { |
| 71 | int i; |
| 72 | |
| 73 | for (i = 0; i < ARRAY_SIZE(freq_desc_tables); i++) { |
| 74 | if ((family == freq_desc_tables[i].x86_family) && |
| 75 | (model == freq_desc_tables[i].x86_model)) |
| 76 | return i; |
| 77 | } |
| 78 | |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | /* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */ |
| 83 | #define id_to_freq(cpu_index, freq_id) \ |
| 84 | (freq_desc_tables[cpu_index].freqs[freq_id]) |
| 85 | |
| 86 | /* |
Bin Meng | 6ffad64 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 87 | * TSC on Intel Atom SoCs capable of determining TSC frequency by MSR is |
| 88 | * reliable and the frequency is known (provided by HW). |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 89 | * |
Bin Meng | 6ffad64 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 90 | * On these platforms PIT/HPET is generally not available so calibration won't |
| 91 | * work at all and there is no other clocksource to act as a watchdog for the |
| 92 | * TSC, so we have no other choice than to trust it. |
| 93 | * |
| 94 | * Returns the TSC frequency in MHz or 0 if HW does not provide it. |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 95 | */ |
Bin Meng | 6ffad64 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 96 | static unsigned long __maybe_unused cpu_mhz_from_msr(void) |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 97 | { |
| 98 | u32 lo, hi, ratio, freq_id, freq; |
| 99 | unsigned long res; |
| 100 | int cpu_index; |
| 101 | |
Bin Meng | d159ffb | 2017-07-25 20:12:01 -0700 | [diff] [blame] | 102 | if (gd->arch.x86_vendor != X86_VENDOR_INTEL) |
| 103 | return 0; |
| 104 | |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 105 | cpu_index = match_cpu(gd->arch.x86, gd->arch.x86_model); |
| 106 | if (cpu_index < 0) |
| 107 | return 0; |
| 108 | |
| 109 | if (freq_desc_tables[cpu_index].msr_plat) { |
| 110 | rdmsr(MSR_PLATFORM_INFO, lo, hi); |
Bin Meng | f4ed4d7 | 2017-07-25 20:12:00 -0700 | [diff] [blame] | 111 | ratio = (lo >> 8) & 0xff; |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 112 | } else { |
| 113 | rdmsr(MSR_IA32_PERF_STATUS, lo, hi); |
| 114 | ratio = (hi >> 8) & 0x1f; |
| 115 | } |
| 116 | debug("Maximum core-clock to bus-clock ratio: 0x%x\n", ratio); |
| 117 | |
Simon Glass | 40a8c35 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 118 | if (freq_desc_tables[cpu_index].msr_plat == 2) { |
| 119 | /* TODO: Figure out how best to deal with this */ |
Bin Meng | 23ee9ab | 2017-07-25 20:12:04 -0700 | [diff] [blame] | 120 | freq = 100000; |
Simon Glass | 40a8c35 | 2014-11-12 22:42:04 -0700 | [diff] [blame] | 121 | debug("Using frequency: %u KHz\n", freq); |
| 122 | } else { |
| 123 | /* Get FSB FREQ ID */ |
| 124 | rdmsr(MSR_FSB_FREQ, lo, hi); |
| 125 | freq_id = lo & 0x7; |
| 126 | freq = id_to_freq(cpu_index, freq_id); |
| 127 | debug("Resolved frequency ID: %u, frequency: %u KHz\n", |
| 128 | freq_id, freq); |
| 129 | } |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 130 | |
| 131 | /* TSC frequency = maximum resolved freq * maximum resolved bus ratio */ |
| 132 | res = freq * ratio / 1000; |
| 133 | debug("TSC runs at %lu MHz\n", res); |
| 134 | |
| 135 | return res; |
Bin Meng | 49f7099 | 2014-11-09 22:19:13 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Bin Meng | bba9705 | 2014-11-09 22:19:25 +0800 | [diff] [blame] | 138 | /* |
| 139 | * This reads the current MSB of the PIT counter, and |
| 140 | * checks if we are running on sufficiently fast and |
| 141 | * non-virtualized hardware. |
| 142 | * |
| 143 | * Our expectations are: |
| 144 | * |
| 145 | * - the PIT is running at roughly 1.19MHz |
| 146 | * |
| 147 | * - each IO is going to take about 1us on real hardware, |
| 148 | * but we allow it to be much faster (by a factor of 10) or |
| 149 | * _slightly_ slower (ie we allow up to a 2us read+counter |
| 150 | * update - anything else implies a unacceptably slow CPU |
| 151 | * or PIT for the fast calibration to work. |
| 152 | * |
| 153 | * - with 256 PIT ticks to read the value, we have 214us to |
| 154 | * see the same MSB (and overhead like doing a single TSC |
| 155 | * read per MSB value etc). |
| 156 | * |
| 157 | * - We're doing 2 reads per loop (LSB, MSB), and we expect |
| 158 | * them each to take about a microsecond on real hardware. |
| 159 | * So we expect a count value of around 100. But we'll be |
| 160 | * generous, and accept anything over 50. |
| 161 | * |
| 162 | * - if the PIT is stuck, and we see *many* more reads, we |
| 163 | * return early (and the next caller of pit_expect_msb() |
| 164 | * then consider it a failure when they don't see the |
| 165 | * next expected value). |
| 166 | * |
| 167 | * These expectations mean that we know that we have seen the |
| 168 | * transition from one expected value to another with a fairly |
| 169 | * high accuracy, and we didn't miss any events. We can thus |
| 170 | * use the TSC value at the transitions to calculate a pretty |
| 171 | * good value for the TSC frequencty. |
| 172 | */ |
| 173 | static inline int pit_verify_msb(unsigned char val) |
| 174 | { |
| 175 | /* Ignore LSB */ |
| 176 | inb(0x42); |
| 177 | return inb(0x42) == val; |
| 178 | } |
| 179 | |
| 180 | static inline int pit_expect_msb(unsigned char val, u64 *tscp, |
| 181 | unsigned long *deltap) |
| 182 | { |
| 183 | int count; |
| 184 | u64 tsc = 0, prev_tsc = 0; |
| 185 | |
| 186 | for (count = 0; count < 50000; count++) { |
| 187 | if (!pit_verify_msb(val)) |
| 188 | break; |
| 189 | prev_tsc = tsc; |
| 190 | tsc = rdtsc(); |
| 191 | } |
| 192 | *deltap = rdtsc() - prev_tsc; |
| 193 | *tscp = tsc; |
| 194 | |
| 195 | /* |
| 196 | * We require _some_ success, but the quality control |
| 197 | * will be based on the error terms on the TSC values. |
| 198 | */ |
| 199 | return count > 5; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * How many MSB values do we want to see? We aim for |
| 204 | * a maximum error rate of 500ppm (in practice the |
| 205 | * real error is much smaller), but refuse to spend |
| 206 | * more than 50ms on it. |
| 207 | */ |
| 208 | #define MAX_QUICK_PIT_MS 50 |
| 209 | #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) |
| 210 | |
Bin Meng | b2eb48b | 2015-01-06 22:14:14 +0800 | [diff] [blame] | 211 | static unsigned long __maybe_unused quick_pit_calibrate(void) |
Bin Meng | bba9705 | 2014-11-09 22:19:25 +0800 | [diff] [blame] | 212 | { |
| 213 | int i; |
| 214 | u64 tsc, delta; |
| 215 | unsigned long d1, d2; |
| 216 | |
| 217 | /* Set the Gate high, disable speaker */ |
| 218 | outb((inb(0x61) & ~0x02) | 0x01, 0x61); |
| 219 | |
| 220 | /* |
| 221 | * Counter 2, mode 0 (one-shot), binary count |
| 222 | * |
| 223 | * NOTE! Mode 2 decrements by two (and then the |
| 224 | * output is flipped each time, giving the same |
| 225 | * final output frequency as a decrement-by-one), |
| 226 | * so mode 0 is much better when looking at the |
| 227 | * individual counts. |
| 228 | */ |
| 229 | outb(0xb0, 0x43); |
| 230 | |
| 231 | /* Start at 0xffff */ |
| 232 | outb(0xff, 0x42); |
| 233 | outb(0xff, 0x42); |
| 234 | |
| 235 | /* |
| 236 | * The PIT starts counting at the next edge, so we |
| 237 | * need to delay for a microsecond. The easiest way |
| 238 | * to do that is to just read back the 16-bit counter |
| 239 | * once from the PIT. |
| 240 | */ |
| 241 | pit_verify_msb(0); |
| 242 | |
| 243 | if (pit_expect_msb(0xff, &tsc, &d1)) { |
| 244 | for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) { |
| 245 | if (!pit_expect_msb(0xff-i, &delta, &d2)) |
| 246 | break; |
| 247 | |
| 248 | /* |
| 249 | * Iterate until the error is less than 500 ppm |
| 250 | */ |
| 251 | delta -= tsc; |
| 252 | if (d1+d2 >= delta >> 11) |
| 253 | continue; |
| 254 | |
| 255 | /* |
| 256 | * Check the PIT one more time to verify that |
| 257 | * all TSC reads were stable wrt the PIT. |
| 258 | * |
| 259 | * This also guarantees serialization of the |
| 260 | * last cycle read ('d2') in pit_expect_msb. |
| 261 | */ |
| 262 | if (!pit_verify_msb(0xfe - i)) |
| 263 | break; |
| 264 | goto success; |
| 265 | } |
| 266 | } |
| 267 | debug("Fast TSC calibration failed\n"); |
| 268 | return 0; |
| 269 | |
| 270 | success: |
| 271 | /* |
| 272 | * Ok, if we get here, then we've seen the |
| 273 | * MSB of the PIT decrement 'i' times, and the |
| 274 | * error has shrunk to less than 500 ppm. |
| 275 | * |
| 276 | * As a result, we can depend on there not being |
| 277 | * any odd delays anywhere, and the TSC reads are |
| 278 | * reliable (within the error). |
| 279 | * |
| 280 | * kHz = ticks / time-in-seconds / 1000; |
| 281 | * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000 |
| 282 | * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000) |
| 283 | */ |
| 284 | delta *= PIT_TICK_RATE; |
| 285 | delta /= (i*256*1000); |
| 286 | debug("Fast TSC calibration using PIT\n"); |
| 287 | return delta / 1000; |
| 288 | } |
| 289 | |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 290 | /* Get the speed of the TSC timer in MHz */ |
Bin Meng | 500361e | 2015-11-13 00:11:20 -0800 | [diff] [blame] | 291 | unsigned notrace long get_tbclk_mhz(void) |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 292 | { |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 293 | return get_tbclk() / 1000000; |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 296 | static ulong get_ms_timer(void) |
| 297 | { |
| 298 | return (get_ticks() * 1000) / get_tbclk(); |
| 299 | } |
| 300 | |
| 301 | ulong get_timer(ulong base) |
| 302 | { |
| 303 | return get_ms_timer() - base; |
| 304 | } |
| 305 | |
Bin Meng | 500361e | 2015-11-13 00:11:20 -0800 | [diff] [blame] | 306 | ulong notrace timer_get_us(void) |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 307 | { |
| 308 | return get_ticks() / get_tbclk_mhz(); |
| 309 | } |
| 310 | |
| 311 | ulong timer_get_boot_us(void) |
| 312 | { |
| 313 | return timer_get_us(); |
| 314 | } |
| 315 | |
| 316 | void __udelay(unsigned long usec) |
| 317 | { |
| 318 | u64 now = get_ticks(); |
| 319 | u64 stop; |
| 320 | |
| 321 | stop = now + usec * get_tbclk_mhz(); |
| 322 | |
| 323 | while ((int64_t)(stop - get_ticks()) > 0) |
Miao Yan | b9f3277 | 2015-07-27 19:16:07 +0800 | [diff] [blame] | 324 | #if defined(CONFIG_QEMU) && defined(CONFIG_SMP) |
| 325 | /* |
| 326 | * Add a 'pause' instruction on qemu target, |
| 327 | * to give other VCPUs a chance to run. |
| 328 | */ |
| 329 | asm volatile("pause"); |
| 330 | #else |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 331 | ; |
Miao Yan | b9f3277 | 2015-07-27 19:16:07 +0800 | [diff] [blame] | 332 | #endif |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 335 | static int tsc_timer_get_count(struct udevice *dev, u64 *count) |
| 336 | { |
| 337 | u64 now_tick = rdtsc(); |
| 338 | |
| 339 | *count = now_tick - gd->arch.tsc_base; |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 344 | static void tsc_timer_ensure_setup(void) |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 345 | { |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 346 | if (gd->arch.tsc_base) |
| 347 | return; |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 348 | gd->arch.tsc_base = rdtsc(); |
| 349 | |
| 350 | /* |
| 351 | * If there is no clock frequency specified in the device tree, |
| 352 | * calibrate it by ourselves. |
| 353 | */ |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 354 | if (!gd->arch.clock_rate) { |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 355 | unsigned long fast_calibrate; |
| 356 | |
Christian Gmeiner | 1b1472a | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 357 | fast_calibrate = cpu_mhz_from_cpuid(); |
| 358 | if (fast_calibrate) |
| 359 | goto done; |
| 360 | |
Bin Meng | 6ffad64 | 2017-07-25 20:12:05 -0700 | [diff] [blame] | 361 | fast_calibrate = cpu_mhz_from_msr(); |
Christian Gmeiner | 1b1472a | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 362 | if (fast_calibrate) |
| 363 | goto done; |
| 364 | |
| 365 | fast_calibrate = quick_pit_calibrate(); |
| 366 | if (fast_calibrate) |
| 367 | goto done; |
| 368 | |
| 369 | panic("TSC frequency is ZERO"); |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 370 | |
Christian Gmeiner | 1b1472a | 2018-05-14 11:32:17 +0200 | [diff] [blame] | 371 | done: |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 372 | gd->arch.clock_rate = fast_calibrate * 1000000; |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 373 | } |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static int tsc_timer_probe(struct udevice *dev) |
| 377 | { |
| 378 | struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 379 | |
Bin Meng | 1394fbb | 2018-06-23 03:03:47 -0700 | [diff] [blame] | 380 | if (!uc_priv->clock_rate) { |
| 381 | tsc_timer_ensure_setup(); |
| 382 | uc_priv->clock_rate = gd->arch.clock_rate; |
| 383 | } else { |
| 384 | gd->arch.tsc_base = rdtsc(); |
| 385 | } |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 390 | unsigned long notrace timer_early_get_rate(void) |
| 391 | { |
Bin Meng | 1394fbb | 2018-06-23 03:03:47 -0700 | [diff] [blame] | 392 | /* |
| 393 | * When TSC timer is used as the early timer, be warned that the timer |
| 394 | * clock rate can only be calibrated via some hardware ways. Specifying |
| 395 | * it in the device tree won't work for the early timer. |
| 396 | */ |
Simon Glass | 471919d | 2017-09-05 19:49:46 -0600 | [diff] [blame] | 397 | tsc_timer_ensure_setup(); |
| 398 | |
| 399 | return gd->arch.clock_rate; |
| 400 | } |
| 401 | |
| 402 | u64 notrace timer_early_get_count(void) |
| 403 | { |
| 404 | return rdtsc() - gd->arch.tsc_base; |
| 405 | } |
| 406 | |
Bin Meng | 976c2e8 | 2015-11-13 00:11:21 -0800 | [diff] [blame] | 407 | static const struct timer_ops tsc_timer_ops = { |
| 408 | .get_count = tsc_timer_get_count, |
| 409 | }; |
| 410 | |
| 411 | static const struct udevice_id tsc_timer_ids[] = { |
| 412 | { .compatible = "x86,tsc-timer", }, |
| 413 | { } |
| 414 | }; |
| 415 | |
| 416 | U_BOOT_DRIVER(tsc_timer) = { |
| 417 | .name = "tsc_timer", |
| 418 | .id = UCLASS_TIMER, |
| 419 | .of_match = tsc_timer_ids, |
| 420 | .probe = tsc_timer_probe, |
| 421 | .ops = &tsc_timer_ops, |
| 422 | .flags = DM_FLAG_PRE_RELOC, |
| 423 | }; |