3 * Texas Instruments <www.ti.com>
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
10 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
11 * Alex Zuepke <azu@sysgo.de>
13 * (C) Copyright 2002-2004
14 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
17 * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
19 * SPDX-License-Identifier: GPL-2.0+
25 #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (2 << CONFIG_SYS_PTV))
26 #define TIMER_LOAD_VAL 0xffffffff
28 /* macro to read the 32 bit timer */
29 #define READ_TIMER readl(CONFIG_SYS_TIMERBASE+8) \
30 / (TIMER_CLOCK / CONFIG_SYS_HZ)
32 DECLARE_GLOBAL_DATA_PTR;
34 #define timestamp gd->arch.tbl
35 #define lastdec gd->arch.lastinc
41 /* Start the decrementer ticking down from 0xffffffff */
42 *((int32_t *) (CONFIG_SYS_TIMERBASE + LOAD_TIM)) = TIMER_LOAD_VAL;
43 val = MPUTIM_ST | MPUTIM_AR | MPUTIM_CLOCK_ENABLE | (CONFIG_SYS_PTV << MPUTIM_PTV_BIT);
44 *((int32_t *) (CONFIG_SYS_TIMERBASE + CNTL_TIMER)) = val;
46 /* init the timestamp and lastdec value */
53 * timer without interrupts
55 ulong get_timer (ulong base)
57 return get_timer_masked () - base;
60 /* delay x useconds AND preserve advance timestamp value */
61 void __udelay (unsigned long usec)
65 if(usec >= 1000){ /* if "big" number, spread normalization to seconds */
66 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
67 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
68 tmo /= 1000; /* finish normalize. */
69 }else{ /* else small number, don't kill it prior to HZ multiply */
70 tmo = usec * CONFIG_SYS_HZ;
74 tmp = get_timer (0); /* get current timestamp */
75 if( (tmo + tmp + 1) < tmp ) /* if setting this fordward will roll time stamp */
76 reset_timer_masked (); /* reset "advancing" timestamp to 0, set lastdec value */
78 tmo += tmp; /* else, set advancing stamp wake up time */
80 while (get_timer_masked () < tmo)/* loop till event */
84 void reset_timer_masked (void)
87 lastdec = READ_TIMER; /* capure current decrementer value time */
88 timestamp = 0; /* start "advancing" time stamp from 0 */
91 ulong get_timer_masked (void)
93 ulong now = READ_TIMER; /* current tick value */
95 if (lastdec >= now) { /* normal mode (non roll) */
97 timestamp += lastdec - now; /* move stamp fordward with absoulte diff ticks */
98 } else { /* we have overflow of the count down timer */
99 /* nts = ts + ld + (TLV - now)
100 * ts=old stamp, ld=time that passed before passing through -1
101 * (TLV-now) amount of time after passing though -1
102 * nts = new "advancing time stamp"...it could also roll and cause problems.
104 timestamp += lastdec + (TIMER_LOAD_VAL / (TIMER_CLOCK /
105 CONFIG_SYS_HZ)) - now;
112 /* waits specified delay value and resets timestamp */
113 void udelay_masked (unsigned long usec)
119 if (usec >= 1000) { /* if "big" number, spread normalization to seconds */
120 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
121 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
122 tmo /= 1000; /* finish normalize. */
123 } else { /* else small number, don't kill it prior to HZ multiply */
124 tmo = usec * CONFIG_SYS_HZ;
128 endtime = get_timer_masked () + tmo;
131 ulong now = get_timer_masked ();
132 diff = endtime - now;
137 * This function is derived from PowerPC code (read timebase as long long).
138 * On ARM it just returns the timer value.
140 unsigned long long get_ticks(void)
146 * This function is derived from PowerPC code (timebase clock frequency).
147 * On ARM it returns the number of timer ticks per second.
149 ulong get_tbclk (void)
151 return CONFIG_SYS_HZ;