Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 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 | */ |
Simon Glass | 42081ce | 2013-06-11 11:14:52 -0700 | [diff] [blame^] | 40 | u64 __attribute__((no_instrument_function)) get_ticks(void) |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 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 */ |
Simon Glass | 42081ce | 2013-06-11 11:14:52 -0700 | [diff] [blame^] | 53 | unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void) |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 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 | |
Simon Glass | 42081ce | 2013-06-11 11:14:52 -0700 | [diff] [blame^] | 78 | ulong __attribute__((no_instrument_function)) timer_get_us(void) |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 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 | { |
Simon Glass | 3e8b605 | 2013-04-17 16:13:39 +0000 | [diff] [blame] | 101 | #ifdef CONFIG_SYS_PCAT_TIMER |
| 102 | /* Set up the PCAT timer if required */ |
| 103 | pcat_timer_init(); |
| 104 | #endif |
| 105 | |
Simon Glass | 11d7a5b | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |