Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <malloc.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <asm/i8254.h> |
| 11 | #include <asm/ibmpc.h> |
| 12 | #include <asm/msr.h> |
| 13 | #include <asm/u-boot-x86.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | void timer_set_base(u64 base) |
| 18 | { |
| 19 | gd->arch.tsc_base = base; |
| 20 | } |
| 21 | |
| 22 | /* |
| 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. |
| 26 | */ |
Simon Glass | d8819f9 | 2013-06-11 11:14:52 -0700 | [diff] [blame] | 27 | u64 __attribute__((no_instrument_function)) get_ticks(void) |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 28 | { |
| 29 | u64 now_tick = rdtsc(); |
| 30 | |
| 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; |
| 35 | } |
| 36 | |
| 37 | #define PLATFORM_INFO_MSR 0xce |
| 38 | |
| 39 | /* Get the speed of the TSC timer in MHz */ |
Simon Glass | d8819f9 | 2013-06-11 11:14:52 -0700 | [diff] [blame] | 40 | unsigned __attribute__((no_instrument_function)) long get_tbclk_mhz(void) |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 41 | { |
| 42 | u32 ratio; |
| 43 | u64 platform_info = native_read_msr(PLATFORM_INFO_MSR); |
| 44 | |
| 45 | /* 100MHz times Max Non Turbo ratio */ |
| 46 | ratio = (platform_info >> 8) & 0xff; |
| 47 | return 100 * ratio; |
| 48 | } |
| 49 | |
| 50 | unsigned long get_tbclk(void) |
| 51 | { |
| 52 | return get_tbclk_mhz() * 1000 * 1000; |
| 53 | } |
| 54 | |
| 55 | static ulong get_ms_timer(void) |
| 56 | { |
| 57 | return (get_ticks() * 1000) / get_tbclk(); |
| 58 | } |
| 59 | |
| 60 | ulong get_timer(ulong base) |
| 61 | { |
| 62 | return get_ms_timer() - base; |
| 63 | } |
| 64 | |
Simon Glass | d8819f9 | 2013-06-11 11:14:52 -0700 | [diff] [blame] | 65 | ulong __attribute__((no_instrument_function)) timer_get_us(void) |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 66 | { |
| 67 | return get_ticks() / get_tbclk_mhz(); |
| 68 | } |
| 69 | |
| 70 | ulong timer_get_boot_us(void) |
| 71 | { |
| 72 | return timer_get_us(); |
| 73 | } |
| 74 | |
| 75 | void __udelay(unsigned long usec) |
| 76 | { |
| 77 | u64 now = get_ticks(); |
| 78 | u64 stop; |
| 79 | |
| 80 | stop = now + usec * get_tbclk_mhz(); |
| 81 | |
| 82 | while ((int64_t)(stop - get_ticks()) > 0) |
| 83 | ; |
| 84 | } |
| 85 | |
| 86 | int timer_init(void) |
| 87 | { |
Simon Glass | d0b6f24 | 2013-04-17 16:13:39 +0000 | [diff] [blame] | 88 | #ifdef CONFIG_SYS_PCAT_TIMER |
| 89 | /* Set up the PCAT timer if required */ |
| 90 | pcat_timer_init(); |
| 91 | #endif |
| 92 | |
Simon Glass | e761ecd | 2013-04-17 16:13:36 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |