]> git.ipfire.org Git - thirdparty/u-boot.git/blob - arch/arm/cpu/arm1136/omap24xx/timer.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[thirdparty/u-boot.git] / arch / arm / cpu / arm1136 / omap24xx / timer.c
1 /*
2 * (C) Copyright 2004
3 * Texas Instruments
4 * Richard Woodruff <r-woodruff2@ti.com>
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 * Alex Zuepke <azu@sysgo.de>
10 *
11 * (C) Copyright 2002
12 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
13 *
14 * SPDX-License-Identifier: GPL-2.0+
15 */
16
17 #include <common.h>
18 #include <asm/io.h>
19 #include <asm/arch/bits.h>
20 #include <asm/arch/omap2420.h>
21
22 #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
23 #define TIMER_LOAD_VAL 0
24
25 /* macro to read the 32 bit timer */
26 #define READ_TIMER readl(CONFIG_SYS_TIMERBASE+TCRR) \
27 / (TIMER_CLOCK / CONFIG_SYS_HZ)
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 int timer_init (void)
32 {
33 int32_t val;
34
35 /* Start the counter ticking up */
36 *((int32_t *) (CONFIG_SYS_TIMERBASE + TLDR)) = TIMER_LOAD_VAL; /* reload value on overflow*/
37 val = (CONFIG_SYS_PTV << 2) | BIT5 | BIT1 | BIT0; /* mask to enable timer*/
38 *((int32_t *) (CONFIG_SYS_TIMERBASE + TCLR)) = val; /* start timer */
39
40 /* reset time */
41 gd->arch.lastinc = READ_TIMER; /* capture current incrementer value */
42 gd->arch.tbl = 0; /* start "advancing" time stamp */
43
44 return(0);
45 }
46 /*
47 * timer without interrupts
48 */
49 ulong get_timer (ulong base)
50 {
51 return get_timer_masked () - base;
52 }
53
54 /* delay x useconds AND preserve advance timestamp value */
55 void __udelay (unsigned long usec)
56 {
57 ulong tmo, tmp;
58
59 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
60 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
61 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
62 tmo /= 1000; /* finish normalize. */
63 } else { /* else small number, don't kill it prior to HZ multiply */
64 tmo = usec * CONFIG_SYS_HZ;
65 tmo /= (1000*1000);
66 }
67
68 tmp = get_timer (0); /* get current timestamp */
69 if ((tmo + tmp + 1) < tmp) { /* if setting this forward will roll */
70 /* time stamp, then reset time */
71 gd->arch.lastinc = READ_TIMER; /* capture incrementer value */
72 gd->arch.tbl = 0; /* start time stamp */
73 } else {
74 tmo += tmp; /* else, set advancing stamp wake up time */
75 }
76 while (get_timer_masked () < tmo)/* loop till event */
77 /*NOP*/;
78 }
79
80 ulong get_timer_masked (void)
81 {
82 ulong now = READ_TIMER; /* current tick value */
83
84 if (now >= gd->arch.lastinc) { /* normal mode (non roll) */
85 /* move stamp fordward with absoulte diff ticks */
86 gd->arch.tbl += (now - gd->arch.lastinc);
87 } else {
88 /* we have rollover of incrementer */
89 gd->arch.tbl += ((0xFFFFFFFF / (TIMER_CLOCK / CONFIG_SYS_HZ))
90 - gd->arch.lastinc) + now;
91 }
92 gd->arch.lastinc = now;
93 return gd->arch.tbl;
94 }
95
96 /* waits specified delay value and resets timestamp */
97 void udelay_masked (unsigned long usec)
98 {
99 ulong tmo;
100 ulong endtime;
101 signed long diff;
102
103 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
104 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
105 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
106 tmo /= 1000; /* finish normalize. */
107 } else { /* else small number, don't kill it prior to HZ multiply */
108 tmo = usec * CONFIG_SYS_HZ;
109 tmo /= (1000*1000);
110 }
111 endtime = get_timer_masked () + tmo;
112
113 do {
114 ulong now = get_timer_masked ();
115 diff = endtime - now;
116 } while (diff >= 0);
117 }
118
119 /*
120 * This function is derived from PowerPC code (read timebase as long long).
121 * On ARM it just returns the timer value.
122 */
123 unsigned long long get_ticks(void)
124 {
125 return get_timer(0);
126 }
127 /*
128 * This function is derived from PowerPC code (timebase clock frequency).
129 * On ARM it returns the number of timer ticks per second.
130 */
131 ulong get_tbclk (void)
132 {
133 ulong tbclk;
134 tbclk = CONFIG_SYS_HZ;
135 return tbclk;
136 }