blob: c7b026498be67e28f02cf070acf73a43e3c18ad4 [file] [log] [blame]
Ingo van Lilf0f778a2009-11-24 14:09:21 +01001/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Ingo van Lilf0f778a2009-11-24 14:09:21 +01006 */
7
8#include <common.h>
9#include <watchdog.h>
Rob Herring57156942013-10-04 10:22:41 -050010#include <div64.h>
11#include <asm/io.h>
Ingo van Lilf0f778a2009-11-24 14:09:21 +010012
Rob Herringa0f85d32013-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 Lilf0f778a2009-11-24 14:09:21 +010017#ifndef CONFIG_WD_PERIOD
Pavel Machekcb21e372014-07-13 13:14:27 +020018# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lilf0f778a2009-11-24 14:09:21 +010019#endif
20
Rob Herring57156942013-10-04 10:22:41 -050021DECLARE_GLOBAL_DATA_PTR;
22
23#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekcb21e372014-07-13 13:14:27 +020024/* Returns tick rate in ticks per second */
Rob Herring57156942013-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 Herring316c5c82013-11-08 08:40:43 -060041extern unsigned long __weak timer_read_counter(void);
Rob Herring57156942013-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 Machekcb21e372014-07-13 13:14:27 +020055/* Returns time in milliseconds */
56static unsigned long long notrace tick_to_time(unsigned long long tick)
Rob Herring57156942013-10-04 10:22:41 -050057{
Pavel Machekcb21e372014-07-13 13:14:27 +020058 ulong div = get_tbclk();
Rob Herring57156942013-10-04 10:22:41 -050059
60 tick *= CONFIG_SYS_HZ;
61 do_div(tick, div);
62 return tick;
63}
64
Darwin Rambof283e4f2013-12-19 15:06:12 -080065int __weak timer_init(void)
66{
67 return 0;
68}
69
Pavel Machekcb21e372014-07-13 13:14:27 +020070/* Returns time in milliseconds */
Rob Herring57156942013-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 Machekcb21e372014-07-13 13:14:27 +020080
Rob Herring57156942013-10-04 10:22:41 -050081static unsigned long long usec_to_tick(unsigned long usec)
82{
Pavel Machekcb21e372014-07-13 13:14:27 +020083 unsigned long long tick = usec;
Stephen Warrenff81b482013-12-05 12:08:09 -070084 tick *= get_tbclk();
Rob Herring57156942013-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 Herring57156942013-10-04 10:22:41 -050092
Pavel Machekcb21e372014-07-13 13:14:27 +020093 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring57156942013-10-04 10:22:41 -050094
Pavel Machekcb21e372014-07-13 13:14:27 +020095 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring57156942013-10-04 10:22:41 -050096 /*NOP*/;
97}
98
Ingo van Lilf0f778a2009-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 Gustschin589c2ff2011-10-12 02:31:39 +0000112
113void mdelay(unsigned long msec)
114{
115 while (msec--)
116 udelay(1000);
117}