Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2009 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <timer.h> |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 11 | #include <watchdog.h> |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 12 | #include <div64.h> |
| 13 | #include <asm/io.h> |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 14 | |
| 15 | #ifndef CONFIG_WD_PERIOD |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 16 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 17 | #endif |
| 18 | |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | #ifdef CONFIG_SYS_TIMER_RATE |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 22 | /* Returns tick rate in ticks per second */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 23 | ulong notrace get_tbclk(void) |
| 24 | { |
| 25 | return CONFIG_SYS_TIMER_RATE; |
| 26 | } |
| 27 | #endif |
| 28 | |
| 29 | #ifdef CONFIG_SYS_TIMER_COUNTER |
| 30 | unsigned long notrace timer_read_counter(void) |
| 31 | { |
| 32 | #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN |
| 33 | return ~readl(CONFIG_SYS_TIMER_COUNTER); |
| 34 | #else |
| 35 | return readl(CONFIG_SYS_TIMER_COUNTER); |
| 36 | #endif |
| 37 | } |
Simon Glass | 037ef5c | 2017-05-22 05:05:22 -0600 | [diff] [blame] | 38 | |
| 39 | ulong timer_get_boot_us(void) |
| 40 | { |
| 41 | ulong count = timer_read_counter(); |
| 42 | |
| 43 | #if CONFIG_SYS_TIMER_RATE == 1000000 |
| 44 | return count; |
| 45 | #elif CONFIG_SYS_TIMER_RATE > 1000000 |
| 46 | return lldiv(count, CONFIG_SYS_TIMER_RATE / 1000000); |
| 47 | #elif defined(CONFIG_SYS_TIMER_RATE) |
| 48 | return (unsigned long long)count * 1000000 / CONFIG_SYS_TIMER_RATE; |
| 49 | #else |
| 50 | /* Assume the counter is in microseconds */ |
| 51 | return count; |
| 52 | #endif |
| 53 | } |
| 54 | |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 55 | #else |
Rob Herring | 316c5c8 | 2013-11-08 08:40:43 -0600 | [diff] [blame] | 56 | extern unsigned long __weak timer_read_counter(void); |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 57 | #endif |
| 58 | |
Kever Yang | f0b5024 | 2019-03-29 22:17:33 +0800 | [diff] [blame] | 59 | #if CONFIG_IS_ENABLED(TIMER) |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 60 | ulong notrace get_tbclk(void) |
| 61 | { |
Simon Glass | 32f6c17 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 62 | if (!gd->timer) { |
| 63 | #ifdef CONFIG_TIMER_EARLY |
| 64 | return timer_early_get_rate(); |
| 65 | #else |
| 66 | int ret; |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 67 | |
Simon Glass | 32f6c17 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 68 | ret = dm_timer_init(); |
| 69 | if (ret) |
| 70 | return ret; |
| 71 | #endif |
| 72 | } |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 73 | |
| 74 | return timer_get_rate(gd->timer); |
| 75 | } |
| 76 | |
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 77 | uint64_t notrace get_ticks(void) |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 78 | { |
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 79 | u64 count; |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 80 | int ret; |
| 81 | |
Simon Glass | 32f6c17 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 82 | if (!gd->timer) { |
| 83 | #ifdef CONFIG_TIMER_EARLY |
| 84 | return timer_early_get_count(); |
| 85 | #else |
| 86 | int ret; |
| 87 | |
| 88 | ret = dm_timer_init(); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | #endif |
| 92 | } |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 93 | |
| 94 | ret = timer_get_count(gd->timer, &count); |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | |
| 98 | return count; |
| 99 | } |
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 100 | |
| 101 | #else /* !CONFIG_TIMER */ |
Thomas Chou | fb798b1 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 102 | |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 103 | uint64_t __weak notrace get_ticks(void) |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 104 | { |
| 105 | unsigned long now = timer_read_counter(); |
| 106 | |
| 107 | /* increment tbu if tbl has rolled over */ |
| 108 | if (now < gd->timebase_l) |
| 109 | gd->timebase_h++; |
| 110 | gd->timebase_l = now; |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 111 | return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l; |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Bin Meng | ab841b6 | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 114 | #endif /* CONFIG_TIMER */ |
| 115 | |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 116 | /* Returns time in milliseconds */ |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 117 | static uint64_t notrace tick_to_time(uint64_t tick) |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 118 | { |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 119 | ulong div = get_tbclk(); |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 120 | |
| 121 | tick *= CONFIG_SYS_HZ; |
| 122 | do_div(tick, div); |
| 123 | return tick; |
| 124 | } |
| 125 | |
Darwin Rambo | f283e4f | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 126 | int __weak timer_init(void) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
| 130 | |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 131 | /* Returns time in milliseconds */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 132 | ulong __weak get_timer(ulong base) |
| 133 | { |
| 134 | return tick_to_time(get_ticks()) - base; |
| 135 | } |
| 136 | |
Marek Vasut | ebc5033 | 2019-10-15 22:43:41 +0200 | [diff] [blame^] | 137 | static uint64_t notrace tick_to_time_us(uint64_t tick) |
| 138 | { |
| 139 | ulong div = get_tbclk() / 1000; |
| 140 | |
| 141 | tick *= CONFIG_SYS_HZ; |
| 142 | do_div(tick, div); |
| 143 | return tick; |
| 144 | } |
| 145 | |
| 146 | uint64_t __weak get_timer_us(uint64_t base) |
| 147 | { |
| 148 | return tick_to_time_us(get_ticks()) - base; |
| 149 | } |
| 150 | |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 151 | unsigned long __weak notrace timer_get_us(void) |
| 152 | { |
| 153 | return tick_to_time(get_ticks() * 1000); |
| 154 | } |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 155 | |
Heinrich Schuchardt | daf69fc | 2019-06-02 21:02:10 +0200 | [diff] [blame] | 156 | uint64_t usec_to_tick(unsigned long usec) |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 157 | { |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 158 | uint64_t tick = usec; |
Stephen Warren | ff81b48 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 159 | tick *= get_tbclk(); |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 160 | do_div(tick, 1000000); |
| 161 | return tick; |
| 162 | } |
| 163 | |
| 164 | void __weak __udelay(unsigned long usec) |
| 165 | { |
Simon Glass | 0e10316 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 166 | uint64_t tmp; |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 167 | |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 168 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 169 | |
Pavel Machek | cb21e37 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 170 | while (get_ticks() < tmp+1) /* loop till event */ |
Rob Herring | 5715694 | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 171 | /*NOP*/; |
| 172 | } |
| 173 | |
Ingo van Lil | f0f778a | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 174 | /* ------------------------------------------------------------------------- */ |
| 175 | |
| 176 | void udelay(unsigned long usec) |
| 177 | { |
| 178 | ulong kv; |
| 179 | |
| 180 | do { |
| 181 | WATCHDOG_RESET(); |
| 182 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; |
| 183 | __udelay (kv); |
| 184 | usec -= kv; |
| 185 | } while(usec); |
| 186 | } |