3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
7 * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
10 * Texas Instruments, <www.ti.com>
11 * Kshitij Gupta <Kshitij@ti.com>
15 * Philippe Robin, <philippe.robin@arm.com>
17 * See file CREDITS for list of people who contributed to this
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of
23 * the License, or (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
38 /* The Integrator/AP timer1 is clocked at 24MHz
39 * can be divided by 16 or 256
40 * and is a 16-bit counter
42 /* U-Boot expects a 32 bit timer running at CONFIG_SYS_HZ*/
43 static ulong timestamp; /* U-Boot ticks since startup */
44 static ulong total_count = 0; /* Total timer count */
45 static ulong lastdec; /* Timer reading at last call */
46 static ulong div_clock = 256; /* Divisor applied to the timer clock */
47 static ulong div_timer = 1; /* Divisor to convert timer reading
48 * change to U-Boot ticks
50 /* CONFIG_SYS_HZ = CONFIG_SYS_HZ_CLOCK/(div_clock * div_timer) */
52 #define TIMER_LOAD_VAL 0x0000FFFFL
53 #define READ_TIMER ((*(volatile ulong *)(CONFIG_SYS_TIMERBASE+4)) & 0x0000FFFFL)
55 /* all function return values in U-Boot ticks i.e. (1/CONFIG_SYS_HZ) sec
56 * - unless otherwise stated
60 * - the Integrator/AP timer issues an interrupt
61 * each time it reaches zero
65 /* Load timer with initial value */
66 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 0) = TIMER_LOAD_VAL;
74 *(volatile ulong *)(CONFIG_SYS_TIMERBASE + 8) = 0x00000088;
76 /* init the timestamp and lastdec value */
79 div_timer = CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ;
80 div_timer /= div_clock;
86 * timer without interrupts
88 void reset_timer (void)
90 reset_timer_masked ();
93 ulong get_timer (ulong base_ticks)
95 return get_timer_masked () - base_ticks;
98 void set_timer (ulong ticks)
101 total_count = ticks * div_timer;
102 reset_timer_masked();
105 /* delay x useconds */
106 void udelay (unsigned long usec)
110 /* Convert to U-Boot ticks */
111 tmo = usec * CONFIG_SYS_HZ;
114 tmp = get_timer_masked(); /* get current timestamp */
115 tmo += tmp; /* wake up timestamp */
117 while (get_timer_masked () < tmo) { /* loop till event */
122 void reset_timer_masked (void)
125 lastdec = READ_TIMER; /* capture current decrementer value */
126 timestamp = 0; /* start "advancing" time stamp from 0 */
129 /* converts the timer reading to U-Boot ticks */
130 /* the timestamp is the number of ticks since reset */
131 /* This routine does not detect wraps unless called regularly
132 ASSUMES a call at least every 16 seconds to detect every reload */
133 ulong get_timer_masked (void)
135 ulong now = READ_TIMER; /* current count */
138 /* Must have wrapped */
139 total_count += lastdec + TIMER_LOAD_VAL + 1 - now;
141 total_count += lastdec - now;
144 timestamp = total_count/div_timer;
149 /* waits specified delay value and resets timestamp */
150 void udelay_masked (unsigned long usec)
156 * This function is derived from PowerPC code (read timebase as long long).
157 * On ARM it just returns the timer value.
159 unsigned long long get_ticks(void)
165 * Return the timebase clock frequency
166 * i.e. how often the timer decrements
168 ulong get_tbclk (void)
170 return CONFIG_SYS_HZ_CLOCK/div_clock;