Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * David Feng <fenghua@phytium.com.cn> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 9 | #include <time.h> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 10 | #include <asm/system.h> |
| 11 | |
Andre Przywara | de223fd | 2016-11-03 00:56:25 +0000 | [diff] [blame] | 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 14 | /* |
| 15 | * Generic timer implementation of get_tbclk() |
| 16 | */ |
| 17 | unsigned long get_tbclk(void) |
| 18 | { |
| 19 | unsigned long cntfrq; |
| 20 | asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq)); |
| 21 | return cntfrq; |
| 22 | } |
| 23 | |
Andre Przywara | 60b7865 | 2018-06-27 01:42:52 +0100 | [diff] [blame] | 24 | #ifdef CONFIG_SYS_FSL_ERRATUM_A008585 |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 25 | /* |
Andre Przywara | 60b7865 | 2018-06-27 01:42:52 +0100 | [diff] [blame] | 26 | * FSL erratum A-008585 says that the ARM generic timer counter "has the |
| 27 | * potential to contain an erroneous value for a small number of core |
| 28 | * clock cycles every time the timer value changes". |
| 29 | * This sometimes leads to a consecutive counter read returning a lower |
| 30 | * value than the previous one, thus reporting the time to go backwards. |
| 31 | * The workaround is to read the counter twice and only return when the value |
| 32 | * was the same in both reads. |
| 33 | * Assumes that the CPU runs in much higher frequency than the timer. |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 34 | */ |
| 35 | unsigned long timer_read_counter(void) |
| 36 | { |
| 37 | unsigned long cntpct; |
York Sun | a7686cf | 2015-03-20 19:28:05 -0700 | [diff] [blame] | 38 | unsigned long temp; |
Andre Przywara | 60b7865 | 2018-06-27 01:42:52 +0100 | [diff] [blame] | 39 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 40 | isb(); |
| 41 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
York Sun | a7686cf | 2015-03-20 19:28:05 -0700 | [diff] [blame] | 42 | asm volatile("mrs %0, cntpct_el0" : "=r" (temp)); |
| 43 | while (temp != cntpct) { |
| 44 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
| 45 | asm volatile("mrs %0, cntpct_el0" : "=r" (temp)); |
| 46 | } |
Andre Przywara | 60b7865 | 2018-06-27 01:42:52 +0100 | [diff] [blame] | 47 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 48 | return cntpct; |
| 49 | } |
Andre Przywara | d1de0bb | 2018-06-27 01:42:53 +0100 | [diff] [blame] | 50 | #elif CONFIG_SUNXI_A64_TIMER_ERRATUM |
| 51 | /* |
| 52 | * This erratum sometimes flips the lower 11 bits of the counter value |
| 53 | * to all 0's or all 1's, leading to jumps forwards or backwards. |
| 54 | * Backwards jumps might be interpreted all roll-overs and be treated as |
| 55 | * huge jumps forward. |
| 56 | * The workaround is to check whether the lower 11 bits of the counter are |
| 57 | * all 0 or all 1, then discard this value and read again. |
| 58 | * This occasionally discards valid values, but will catch all erroneous |
| 59 | * reads and fixes the problem reliably. Also this mostly requires only a |
| 60 | * single read, so does not have any significant overhead. |
| 61 | * The algorithm was conceived by Samuel Holland. |
| 62 | */ |
| 63 | unsigned long timer_read_counter(void) |
| 64 | { |
| 65 | unsigned long cntpct; |
| 66 | |
| 67 | isb(); |
| 68 | do { |
| 69 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
| 70 | } while (((cntpct + 1) & GENMASK(10, 0)) <= 1); |
| 71 | |
| 72 | return cntpct; |
| 73 | } |
Andre Przywara | 60b7865 | 2018-06-27 01:42:52 +0100 | [diff] [blame] | 74 | #else |
| 75 | /* |
| 76 | * timer_read_counter() using the Arm Generic Timer (aka arch timer). |
| 77 | */ |
| 78 | unsigned long timer_read_counter(void) |
| 79 | { |
| 80 | unsigned long cntpct; |
| 81 | |
| 82 | isb(); |
| 83 | asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct)); |
| 84 | |
| 85 | return cntpct; |
| 86 | } |
| 87 | #endif |
Aneesh Bansal | d1074b4 | 2015-12-08 13:54:26 +0530 | [diff] [blame] | 88 | |
Simon Glass | e987393 | 2017-04-05 17:53:17 -0600 | [diff] [blame] | 89 | uint64_t get_ticks(void) |
Andre Przywara | de223fd | 2016-11-03 00:56:25 +0000 | [diff] [blame] | 90 | { |
| 91 | unsigned long ticks = timer_read_counter(); |
| 92 | |
| 93 | gd->arch.tbl = ticks; |
| 94 | |
| 95 | return ticks; |
| 96 | } |
| 97 | |
Aneesh Bansal | d1074b4 | 2015-12-08 13:54:26 +0530 | [diff] [blame] | 98 | unsigned long usec2ticks(unsigned long usec) |
| 99 | { |
| 100 | ulong ticks; |
| 101 | if (usec < 1000) |
| 102 | ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000; |
| 103 | else |
| 104 | ticks = ((usec / 10) * (get_tbclk() / 100000)); |
| 105 | |
| 106 | return ticks; |
| 107 | } |
Michal Simek | 16d73b9 | 2018-05-15 16:47:02 +0200 | [diff] [blame] | 108 | |
| 109 | ulong timer_get_boot_us(void) |
| 110 | { |
| 111 | u64 val = get_ticks() * 1000000; |
| 112 | |
| 113 | return val / get_tbclk(); |
| 114 | } |