blob: 477440de16faa3319852a6119935628f63aa8531 [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
13#ifndef CONFIG_WD_PERIOD
Pavel Machekfccacd32014-07-13 13:14:27 +020014# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010015#endif
16
Rob Herring8dfafdd2013-10-04 10:22:41 -050017DECLARE_GLOBAL_DATA_PTR;
18
19#ifdef CONFIG_SYS_TIMER_RATE
Pavel Machekfccacd32014-07-13 13:14:27 +020020/* Returns tick rate in ticks per second */
Rob Herring8dfafdd2013-10-04 10:22:41 -050021ulong notrace get_tbclk(void)
22{
23 return CONFIG_SYS_TIMER_RATE;
24}
25#endif
26
27#ifdef CONFIG_SYS_TIMER_COUNTER
28unsigned 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 Herring65ba7ad2013-11-08 08:40:43 -060037extern unsigned long __weak timer_read_counter(void);
Rob Herring8dfafdd2013-10-04 10:22:41 -050038#endif
39
Simon Glass19ea4672014-10-15 04:38:33 -060040uint64_t __weak notrace get_ticks(void)
Rob Herring8dfafdd2013-10-04 10:22:41 -050041{
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;
Simon Glass19ea4672014-10-15 04:38:33 -060048 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
Rob Herring8dfafdd2013-10-04 10:22:41 -050049}
50
Pavel Machekfccacd32014-07-13 13:14:27 +020051/* Returns time in milliseconds */
Simon Glass19ea4672014-10-15 04:38:33 -060052static uint64_t notrace tick_to_time(uint64_t tick)
Rob Herring8dfafdd2013-10-04 10:22:41 -050053{
Pavel Machekfccacd32014-07-13 13:14:27 +020054 ulong div = get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -050055
56 tick *= CONFIG_SYS_HZ;
57 do_div(tick, div);
58 return tick;
59}
60
Darwin Rambode351d62013-12-19 15:06:12 -080061int __weak timer_init(void)
62{
63 return 0;
64}
65
Pavel Machekfccacd32014-07-13 13:14:27 +020066/* Returns time in milliseconds */
Rob Herring8dfafdd2013-10-04 10:22:41 -050067ulong __weak get_timer(ulong base)
68{
69 return tick_to_time(get_ticks()) - base;
70}
71
72unsigned long __weak notrace timer_get_us(void)
73{
74 return tick_to_time(get_ticks() * 1000);
75}
Pavel Machekfccacd32014-07-13 13:14:27 +020076
Simon Glass19ea4672014-10-15 04:38:33 -060077static uint64_t usec_to_tick(unsigned long usec)
Rob Herring8dfafdd2013-10-04 10:22:41 -050078{
Simon Glass19ea4672014-10-15 04:38:33 -060079 uint64_t tick = usec;
Stephen Warren2cd1b572013-12-05 12:08:09 -070080 tick *= get_tbclk();
Rob Herring8dfafdd2013-10-04 10:22:41 -050081 do_div(tick, 1000000);
82 return tick;
83}
84
85void __weak __udelay(unsigned long usec)
86{
Simon Glass19ea4672014-10-15 04:38:33 -060087 uint64_t tmp;
Rob Herring8dfafdd2013-10-04 10:22:41 -050088
Pavel Machekfccacd32014-07-13 13:14:27 +020089 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
Rob Herring8dfafdd2013-10-04 10:22:41 -050090
Pavel Machekfccacd32014-07-13 13:14:27 +020091 while (get_ticks() < tmp+1) /* loop till event */
Rob Herring8dfafdd2013-10-04 10:22:41 -050092 /*NOP*/;
93}
94
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010095/* ------------------------------------------------------------------------- */
96
97void 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 Gustschinc4c9fbe2011-10-12 02:31:39 +0000108
109void mdelay(unsigned long msec)
110{
111 while (msec--)
112 udelay(1000);
113}