blob: 06889737d1a21dece02a26ec766ed05bd9fed3c4 [file] [log] [blame]
Simon Glass11d7a5b2013-04-17 16:13:36 +00001/*
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
28DECLARE_GLOBAL_DATA_PTR;
29
30void 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 */
Simon Glass42081ce2013-06-11 11:14:52 -070040u64 __attribute__((no_instrument_function)) get_ticks(void)
Simon Glass11d7a5b2013-04-17 16:13:36 +000041{
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 */
Simon Glass42081ce2013-06-11 11:14:52 -070053unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void)
Simon Glass11d7a5b2013-04-17 16:13:36 +000054{
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
63unsigned long get_tbclk(void)
64{
65 return get_tbclk_mhz() * 1000 * 1000;
66}
67
68static ulong get_ms_timer(void)
69{
70 return (get_ticks() * 1000) / get_tbclk();
71}
72
73ulong get_timer(ulong base)
74{
75 return get_ms_timer() - base;
76}
77
Simon Glass42081ce2013-06-11 11:14:52 -070078ulong __attribute__((no_instrument_function)) timer_get_us(void)
Simon Glass11d7a5b2013-04-17 16:13:36 +000079{
80 return get_ticks() / get_tbclk_mhz();
81}
82
83ulong timer_get_boot_us(void)
84{
85 return timer_get_us();
86}
87
88void __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
99int timer_init(void)
100{
Simon Glass3e8b6052013-04-17 16:13:39 +0000101#ifdef CONFIG_SYS_PCAT_TIMER
102 /* Set up the PCAT timer if required */
103 pcat_timer_init();
104#endif
105
Simon Glass11d7a5b2013-04-17 16:13:36 +0000106 return 0;
107}