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> | ||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 9 | #include <dm.h> |
10 | #include <errno.h> | ||||
11 | #include <timer.h> | ||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 12 | #include <watchdog.h> |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 13 | #include <div64.h> |
14 | #include <asm/io.h> | ||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 15 | |
16 | #ifndef CONFIG_WD_PERIOD | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 17 | # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ |
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 18 | #endif |
19 | |||||
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
21 | |||||
22 | #ifdef CONFIG_SYS_TIMER_RATE | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 23 | /* Returns tick rate in ticks per second */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 24 | ulong notrace get_tbclk(void) |
25 | { | ||||
26 | return CONFIG_SYS_TIMER_RATE; | ||||
27 | } | ||||
28 | #endif | ||||
29 | |||||
30 | #ifdef CONFIG_SYS_TIMER_COUNTER | ||||
31 | unsigned long notrace timer_read_counter(void) | ||||
32 | { | ||||
33 | #ifdef CONFIG_SYS_TIMER_COUNTS_DOWN | ||||
34 | return ~readl(CONFIG_SYS_TIMER_COUNTER); | ||||
35 | #else | ||||
36 | return readl(CONFIG_SYS_TIMER_COUNTER); | ||||
37 | #endif | ||||
38 | } | ||||
39 | #else | ||||
Rob Herring | 65ba7ad | 2013-11-08 08:40:43 -0600 | [diff] [blame] | 40 | extern unsigned long __weak timer_read_counter(void); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 41 | #endif |
42 | |||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 43 | #ifdef CONFIG_TIMER |
44 | static int notrace dm_timer_init(void) | ||||
45 | { | ||||
46 | struct udevice *dev; | ||||
47 | int ret; | ||||
48 | |||||
49 | if (!gd->timer) { | ||||
50 | ret = uclass_first_device(UCLASS_TIMER, &dev); | ||||
51 | if (ret) | ||||
52 | return ret; | ||||
53 | if (!dev) | ||||
54 | return -ENODEV; | ||||
55 | gd->timer = dev; | ||||
56 | } | ||||
57 | |||||
58 | return 0; | ||||
59 | } | ||||
60 | |||||
61 | ulong notrace get_tbclk(void) | ||||
62 | { | ||||
63 | int ret; | ||||
64 | |||||
65 | ret = dm_timer_init(); | ||||
66 | if (ret) | ||||
67 | return ret; | ||||
68 | |||||
69 | return timer_get_rate(gd->timer); | ||||
70 | } | ||||
71 | |||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 72 | uint64_t notrace get_ticks(void) |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 73 | { |
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 74 | u64 count; |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 75 | int ret; |
76 | |||||
77 | ret = dm_timer_init(); | ||||
78 | if (ret) | ||||
79 | return ret; | ||||
80 | |||||
81 | ret = timer_get_count(gd->timer, &count); | ||||
82 | if (ret) | ||||
83 | return ret; | ||||
84 | |||||
85 | return count; | ||||
86 | } | ||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 87 | |
88 | #else /* !CONFIG_TIMER */ | ||||
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 89 | |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 90 | uint64_t __weak notrace get_ticks(void) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 91 | { |
92 | unsigned long now = timer_read_counter(); | ||||
93 | |||||
94 | /* increment tbu if tbl has rolled over */ | ||||
95 | if (now < gd->timebase_l) | ||||
96 | gd->timebase_h++; | ||||
97 | gd->timebase_l = now; | ||||
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 98 | return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 99 | } |
100 | |||||
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 101 | #endif /* CONFIG_TIMER */ |
102 | |||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 103 | /* Returns time in milliseconds */ |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 104 | static uint64_t notrace tick_to_time(uint64_t tick) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 105 | { |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 106 | ulong div = get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 107 | |
108 | tick *= CONFIG_SYS_HZ; | ||||
109 | do_div(tick, div); | ||||
110 | return tick; | ||||
111 | } | ||||
112 | |||||
Darwin Rambo | de351d6 | 2013-12-19 15:06:12 -0800 | [diff] [blame] | 113 | int __weak timer_init(void) |
114 | { | ||||
115 | return 0; | ||||
116 | } | ||||
117 | |||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 118 | /* Returns time in milliseconds */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 119 | ulong __weak get_timer(ulong base) |
120 | { | ||||
121 | return tick_to_time(get_ticks()) - base; | ||||
122 | } | ||||
123 | |||||
124 | unsigned long __weak notrace timer_get_us(void) | ||||
125 | { | ||||
126 | return tick_to_time(get_ticks() * 1000); | ||||
127 | } | ||||
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 128 | |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 129 | static uint64_t usec_to_tick(unsigned long usec) |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 130 | { |
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 131 | uint64_t tick = usec; |
Stephen Warren | 2cd1b57 | 2013-12-05 12:08:09 -0700 | [diff] [blame] | 132 | tick *= get_tbclk(); |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 133 | do_div(tick, 1000000); |
134 | return tick; | ||||
135 | } | ||||
136 | |||||
137 | void __weak __udelay(unsigned long usec) | ||||
138 | { | ||||
Simon Glass | 19ea467 | 2014-10-15 04:38:33 -0600 | [diff] [blame] | 139 | uint64_t tmp; |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 140 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 141 | tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 142 | |
Pavel Machek | fccacd3 | 2014-07-13 13:14:27 +0200 | [diff] [blame] | 143 | while (get_ticks() < tmp+1) /* loop till event */ |
Rob Herring | 8dfafdd | 2013-10-04 10:22:41 -0500 | [diff] [blame] | 144 | /*NOP*/; |
145 | } | ||||
146 | |||||
Ingo van Lil | 3eb90ba | 2009-11-24 14:09:21 +0100 | [diff] [blame] | 147 | /* ------------------------------------------------------------------------- */ |
148 | |||||
149 | void udelay(unsigned long usec) | ||||
150 | { | ||||
151 | ulong kv; | ||||
152 | |||||
153 | do { | ||||
154 | WATCHDOG_RESET(); | ||||
155 | kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec; | ||||
156 | __udelay (kv); | ||||
157 | usec -= kv; | ||||
158 | } while(usec); | ||||
159 | } | ||||
Anatolij Gustschin | c4c9fbe | 2011-10-12 02:31:39 +0000 | [diff] [blame] | 160 | |
161 | void mdelay(unsigned long msec) | ||||
162 | { | ||||
163 | while (msec--) | ||||
164 | udelay(1000); | ||||
165 | } |