]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/time.c
arm: socfpga: dts: Add bank-name property to each GPIO bank
[people/ms/u-boot.git] / lib / time.c
CommitLineData
3eb90bad
IL
1/*
2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
3eb90bad
IL
6 */
7
8#include <common.h>
9#include <watchdog.h>
8dfafdde
RH
10#include <div64.h>
11#include <asm/io.h>
3eb90bad
IL
12
13#ifndef CONFIG_WD_PERIOD
fccacd3b 14# define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */
3eb90bad
IL
15#endif
16
8dfafdde
RH
17DECLARE_GLOBAL_DATA_PTR;
18
19#ifdef CONFIG_SYS_TIMER_RATE
fccacd3b 20/* Returns tick rate in ticks per second */
8dfafdde
RH
21ulong 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
65ba7add 37extern unsigned long __weak timer_read_counter(void);
8dfafdde
RH
38#endif
39
19ea4678 40uint64_t __weak notrace get_ticks(void)
8dfafdde
RH
41{
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;
19ea4678 48 return ((uint64_t)gd->timebase_h << 32) | gd->timebase_l;
8dfafdde
RH
49}
50
fccacd3b 51/* Returns time in milliseconds */
19ea4678 52static uint64_t notrace tick_to_time(uint64_t tick)
8dfafdde 53{
fccacd3b 54 ulong div = get_tbclk();
8dfafdde
RH
55
56 tick *= CONFIG_SYS_HZ;
57 do_div(tick, div);
58 return tick;
59}
60
de351d6b
DR
61int __weak timer_init(void)
62{
63 return 0;
64}
65
fccacd3b 66/* Returns time in milliseconds */
8dfafdde
RH
67ulong __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}
fccacd3b 76
19ea4678 77static uint64_t usec_to_tick(unsigned long usec)
8dfafdde 78{
19ea4678 79 uint64_t tick = usec;
2cd1b572 80 tick *= get_tbclk();
8dfafdde
RH
81 do_div(tick, 1000000);
82 return tick;
83}
84
85void __weak __udelay(unsigned long usec)
86{
19ea4678 87 uint64_t tmp;
8dfafdde 88
fccacd3b 89 tmp = get_ticks() + usec_to_tick(usec); /* get current timestamp */
8dfafdde 90
fccacd3b 91 while (get_ticks() < tmp+1) /* loop till event */
8dfafdde
RH
92 /*NOP*/;
93}
94
3eb90bad
IL
95/* ------------------------------------------------------------------------- */
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}
c4c9fbeb
AG
108
109void mdelay(unsigned long msec)
110{
111 while (msec--)
112 udelay(1000);
113}