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