blob: c7b026498be67e28f02cf070acf73a43e3c18ad4 [file] [log] [blame]
Ingo van Lil3eb90ba2009-11-24 14:09:21 +01001/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Ingo van Lil3eb90ba2009-11-24 14:09:21 +01006 */
7
8#include <common.h>
9#include <watchdog.h>
Rob Herring8dfafdd2013-10-04 10:22:41 -050010#include <div64.h>
11#include <asm/io.h>
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010012
Rob Herring2108f4c2013-10-04 08:40:03 -050013#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 Lil3eb90ba2009-11-24 14:09:21 +010017#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020018# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010019#endif
20
Rob Herring8dfafdd2013-10-04 10:22:41 -050021DECLARE_GLOBAL_DATA_PTR;
22
23#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020024/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050025ulong notrace get_tbclk(void)
26{
27 return CONFIG_SYS_TIMER_RATE;
28}
29#endif
30
31#ifdef CONFIG_SYS_TIMER_COUNTER
32unsigned 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 Herring65ba7ad2013-11-08 08:40:43 -060041extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050042#endif
43
44unsigned 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 Machekfccacd32014-07-13 13:14:27 +020055/* Returns time in milliseconds */
56static unsigned long long notrace tick_to_time(unsigned long long tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -050057{
Pavel Machekfccacd32014-07-13 13:14:27 +020058 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -050059
60 tick *= CONFIG_SYS_HZ;
61 do_div(tick, div);
62 return tick;
63}
64
Darwin Rambode351d62013-12-19 15:06:12 -080065int __weak timer_init(void)
66{
67 return 0;
68}
69
Pavel Machekfccacd32014-07-13 13:14:27 +020070/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -050071ulong __weak get_timer(ulong base)
72{
73 return tick_to_time(get_ticks()) - base;
74}
75
76unsigned long __weak notrace timer_get_us(void)
77{
78 return tick_to_time(get_ticks() * 1000);
79}
Pavel Machekfccacd32014-07-13 13:14:27 +020080
Rob Herring8dfafdd2013-10-04 10:22:41 -050081static unsigned long long usec_to_tick(unsigned long usec)
82{
Pavel Machekfccacd32014-07-13 13:14:27 +020083 unsigned long long tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -070084 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -050085 do_div(tick, 1000000);
86 return tick;
87}
88
89void __weak __udelay(unsigned long usec)
90{
91 unsigned long long tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -050092
Pavel Machekfccacd32014-07-13 13:14:27 +020093 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -050094
Pavel Machekfccacd32014-07-13 13:14:27 +020095 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -050096 /*NOP*/;
97}
98
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010099/* ------------------------------------------------------------------------- */
100
101void 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 Gustschinc4c9fbe2011-10-12 02:31:39 +0000112
113void mdelay(unsigned long msec)
114{
115 while (msec--)
116 udelay(1000);
117}