4 * Richard Woodruff <r-woodruff2@ti.com>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Marius Groeger <mgroeger@sysgo.de>
9 * Alex Zuepke <azu@sysgo.de>
12 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
14 * SPDX-License-Identifier: GPL-2.0+
19 #include <asm/arch/bits.h>
20 #include <asm/arch/omap2420.h>
22 #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
23 #define TIMER_LOAD_VAL 0
25 /* macro to read the 32 bit timer */
26 #define READ_TIMER readl(CONFIG_SYS_TIMERBASE+TCRR) \
27 / (TIMER_CLOCK / CONFIG_SYS_HZ)
29 DECLARE_GLOBAL_DATA_PTR;
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 */
41 gd->arch.lastinc = READ_TIMER; /* capture current incrementer value */
42 gd->arch.tbl = 0; /* start "advancing" time stamp */
47 * timer without interrupts
49 ulong get_timer (ulong base)
51 return get_timer_masked () - base;
54 /* delay x useconds AND preserve advance timestamp value */
55 void __udelay (unsigned long usec)
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;
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 */
74 tmo += tmp; /* else, set advancing stamp wake up time */
76 while (get_timer_masked () < tmo)/* loop till event */
80 ulong get_timer_masked (void)
82 ulong now = READ_TIMER; /* current tick value */
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);
88 /* we have rollover of incrementer */
89 gd->arch.tbl += ((0xFFFFFFFF / (TIMER_CLOCK / CONFIG_SYS_HZ))
90 - gd->arch.lastinc) + now;
92 gd->arch.lastinc = now;
96 /* waits specified delay value and resets timestamp */
97 void udelay_masked (unsigned long usec)
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;
111 endtime = get_timer_masked () + tmo;
114 ulong now = get_timer_masked ();
115 diff = endtime - now;
120 * This function is derived from PowerPC code (read timebase as long long).
121 * On ARM it just returns the timer value.
123 unsigned long long get_ticks(void)
128 * This function is derived from PowerPC code (timebase clock frequency).
129 * On ARM it returns the number of timer ticks per second.
131 ulong get_tbclk (void)
134 tbclk = CONFIG_SYS_HZ;