2 * Copyright (c) 2012 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/i8254.h>
11 #include <asm/ibmpc.h>
13 #include <asm/u-boot-x86.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 void timer_set_base(u64 base)
19 gd->arch.tsc_base = base;
23 * Get the number of CPU time counter ticks since it was read first time after
24 * restart. This yields a free running counter guaranteed to take almost 6
25 * years to wrap around even at 100GHz clock rate.
27 u64 __attribute__((no_instrument_function)) get_ticks(void)
29 u64 now_tick = rdtsc();
31 /* We assume that 0 means the base hasn't been set yet */
32 if (!gd->arch.tsc_base)
33 panic("No tick base available");
34 return now_tick - gd->arch.tsc_base;
37 #define PLATFORM_INFO_MSR 0xce
39 /* Get the speed of the TSC timer in MHz */
40 unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void)
43 u64 platform_info = native_read_msr(PLATFORM_INFO_MSR);
45 /* 100MHz times Max Non Turbo ratio */
46 ratio = (platform_info >> 8) & 0xff;
50 unsigned long get_tbclk(void)
52 return get_tbclk_mhz() * 1000 * 1000;
55 static ulong get_ms_timer(void)
57 return (get_ticks() * 1000) / get_tbclk();
60 ulong get_timer(ulong base)
62 return get_ms_timer() - base;
65 ulong __attribute__((no_instrument_function)) timer_get_us(void)
67 return get_ticks() / get_tbclk_mhz();
70 ulong timer_get_boot_us(void)
72 return timer_get_us();
75 void __udelay(unsigned long usec)
77 u64 now = get_ticks();
80 stop = now + usec * get_tbclk_mhz();
82 while ((int64_t)(stop - get_ticks()) > 0)
88 #ifdef CONFIG_SYS_PCAT_TIMER
89 /* Set up the PCAT timer if required */