Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2009 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <watchdog.h> |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 10 | #include <div64.h> |
| 11 | #include <asm/io.h> |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 12 | |
| 13 | #ifndef CONFIG_WD_PERIOD |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 14 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 15 | #endif |
| 16 | |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | #ifdef CONFIG_SYS_TIMER_RATE |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 20 | /* Returns tick rate in ticks per second */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 21 | ulong notrace get_tbclk(void) |
| 22 | { |
| 23 | return CONFIG_SYS_TIMER_RATE; |
| 24 | } |
| 25 | #endif |
| 26 | |
| 27 | #ifdef CONFIG_SYS_TIMER_COUNTER |
| 28 | unsigned long notrace timer_read_counter(void) |
| 29 | { |
| 30 | #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN |
| 31 | return ~readl(CONFIG_SYS_TIMER_COUNTER); |
| 32 | #else |
| 33 | return readl(CONFIG_SYS_TIMER_COUNTER); |
| 34 | #endif |
| 35 | } |
| 36 | #else |
Rob Herring | 65ba7ad | 2013-11-08 08:40:43 -0600 | [diff] [blame] | 37 | extern unsigned long __weak timer_read_counter(void); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | unsigned long long __weak notrace get_ticks(void) |
| 41 | { |
| 42 | unsigned long now = timer_read_counter(); |
| 43 | |
| 44 | /* increment tbu if tbl has rolled over */ |
| 45 | if (now < gd->timebase_l) |
| 46 | gd->timebase_h++; |
| 47 | gd->timebase_l = now; |
| 48 | return ((unsigned long long)gd->timebase_h << 32) | gd->timebase_l; |
| 49 | } |
| 50 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 51 | /* Returns time in milliseconds */ |
| 52 | static unsigned long long notrace tick_to_time(unsigned long long tick) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 53 | { |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 54 | ulong div = get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 55 | |
| 56 | tick *= CONFIG_SYS_HZ; |
| 57 | do_div(tick, div); |
| 58 | return tick; |
| 59 | } |
| 60 | |
Darwin Rambo | de351d6 | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 61 | int __weak timer_init(void) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 66 | /* Returns time in milliseconds */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 67 | ulong __weak get_timer(ulong base) |
| 68 | { |
| 69 | return tick_to_time(get_ticks()) - base; |
| 70 | } |
| 71 | |
| 72 | unsigned long __weak notrace timer_get_us(void) |
| 73 | { |
| 74 | return tick_to_time(get_ticks() * 1000); |
| 75 | } |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 76 | |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 77 | static unsigned long long usec_to_tick(unsigned long usec) |
| 78 | { |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 79 | unsigned long long tick = usec; |
Stephen Warren | 2cd1b57 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 80 | tick *= get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 81 | do_div(tick, 1000000); |
| 82 | return tick; |
| 83 | } |
| 84 | |
| 85 | void __weak __udelay(unsigned long usec) |
| 86 | { |
| 87 | unsigned long long tmp; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 88 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 89 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 90 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 91 | while (get_ticks() < tmp+1) /* loop till event */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 92 | /*NOP*/; |
| 93 | } |
| 94 | |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 95 | /* ------------------------------------------------------------------------- */ |
| 96 | |
| 97 | void udelay(unsigned long usec) |
| 98 | { |
| 99 | ulong kv; |
| 100 | |
| 101 | do { |
| 102 | WATCHDOG_RESET(); |
| 103 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; |
| 104 | __udelay (kv); |
| 105 | usec -= kv; |
| 106 | } while(usec); |
| 107 | } |
Anatolij Gustschin | c4c9fbe | 2011-10-12 02:31:39 +0000 | [diff] [blame] | 108 | |
| 109 | void mdelay(unsigned long msec) |
| 110 | { |
| 111 | while (msec--) |
| 112 | udelay(1000); |
| 113 | } |