]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/x86/lib/tsc_timer.c
Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / arch / x86 / lib / tsc_timer.c
1 /*
2  * Copyright (c) 2012 The Chromium OS Authors.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307 USA
18  */
19
20 #include <common.h>
21 #include <malloc.h>
22 #include <asm/io.h>
23 #include <asm/i8254.h>
24 #include <asm/ibmpc.h>
25 #include <asm/msr.h>
26 #include <asm/u-boot-x86.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 void timer_set_base(u64 base)
31 {
32         gd->arch.tsc_base = base;
33 }
34
35 /*
36  * Get the number of CPU time counter ticks since it was read first time after
37  * restart. This yields a free running counter guaranteed to take almost 6
38  * years to wrap around even at 100GHz clock rate.
39  */
40 u64 get_ticks(void)
41 {
42         u64 now_tick = rdtsc();
43
44         /* We assume that 0 means the base hasn't been set yet */
45         if (!gd->arch.tsc_base)
46                 panic("No tick base available");
47         return now_tick - gd->arch.tsc_base;
48 }
49
50 #define PLATFORM_INFO_MSR 0xce
51
52 /* Get the speed of the TSC timer in MHz */
53 unsigned long get_tbclk_mhz(void)
54 {
55         u32 ratio;
56         u64 platform_info = native_read_msr(PLATFORM_INFO_MSR);
57
58         /* 100MHz times Max Non Turbo ratio */
59         ratio = (platform_info >> 8) & 0xff;
60         return 100 * ratio;
61 }
62
63 unsigned long get_tbclk(void)
64 {
65         return get_tbclk_mhz() * 1000 * 1000;
66 }
67
68 static ulong get_ms_timer(void)
69 {
70         return (get_ticks() * 1000) / get_tbclk();
71 }
72
73 ulong get_timer(ulong base)
74 {
75         return get_ms_timer() - base;
76 }
77
78 ulong timer_get_us(void)
79 {
80         return get_ticks() / get_tbclk_mhz();
81 }
82
83 ulong timer_get_boot_us(void)
84 {
85         return timer_get_us();
86 }
87
88 void __udelay(unsigned long usec)
89 {
90         u64 now = get_ticks();
91         u64 stop;
92
93         stop = now + usec * get_tbclk_mhz();
94
95         while ((int64_t)(stop - get_ticks()) > 0)
96                 ;
97 }
98
99 int timer_init(void)
100 {
101 #ifdef CONFIG_SYS_PCAT_TIMER
102         /* Set up the PCAT timer if required */
103         pcat_timer_init();
104 #endif
105
106         return 0;
107 }