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 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #define TIMER_CLOCK (CONFIG_SYS_CLK_FREQ / (1 << (CONFIG_TIMER_PRESCALE * 4)))
37 #define TIMER_LOAD_VAL 0xFFFFFF
39 /* macro to read the 32 bit timer */
40 #define READ_TIMER (TIMER_LOAD_VAL - readl(CONFIG_SYS_TIMERBASE + TIMER_CURR)) \
41 / (TIMER_CLOCK / CONFIG_SYS_HZ)
43 #define READ_TIMER_HW (TIMER_LOAD_VAL - readl(CONFIG_SYS_TIMERBASE + TIMER_CURR))
45 DECLARE_GLOBAL_DATA_PTR;
51 /* Start the counter ticking up */
52 writel(TIMER_LOAD_VAL, CONFIG_SYS_TIMERBASE + TIMER_LOAD); /* reload value on overflow*/
54 val = (CONFIG_TIMER_PRESCALE << TIMER_PRESCALE_SHIFT) |
55 (TIMER_MODE_PERIODIC << TIMER_MODE_SHIFT) |
56 (TIMER_ENABLE << TIMER_ENABLE_SHIFT); /* mask to enable timer*/
57 writel(val, CONFIG_SYS_TIMERBASE + TIMER_CTRL); /* start timer */
60 gd->arch.lastinc = READ_TIMER; /* capture current incrementer value */
61 gd->arch.tbl = 0; /* start "advancing" time stamp */
66 * timer without interrupts
68 ulong get_timer (ulong base)
70 return get_timer_masked () - base;
73 /* delay x useconds AND preserve advance timestamp value */
74 void __udelay (unsigned long usec)
78 if (usec > 100000) { /* if "big" number, spread normalization to seconds */
79 tmo = usec / 1000; /* start to normalize for usec to ticks per sec */
80 tmo *= CONFIG_SYS_HZ; /* find number of "ticks" to wait to achieve target */
81 tmo /= 1000; /* finish normalize. */
83 tmp = get_timer (0); /* get current timestamp */
84 while (get_timer (tmp) < tmo)/* loop till event */
86 } else { /* else small number, convert to hw ticks */
87 tmo = usec * (TIMER_CLOCK / 1000) / 1000;
88 /* timeout is no more than 0.1s, and the hw timer will roll over at most once */
90 while (((READ_TIMER_HW -tmp) & TIMER_LOAD_VAL) < tmo)/* loop till event */
95 ulong get_timer_masked (void)
97 ulong now = READ_TIMER; /* current tick value */
99 if (now >= gd->arch.lastinc) { /* normal mode (non roll) */
100 /* move stamp fordward with absoulte diff ticks */
101 gd->arch.tbl += (now - gd->arch.lastinc);
103 /* we have rollover of incrementer */
104 gd->arch.tbl += ((TIMER_LOAD_VAL / (TIMER_CLOCK / CONFIG_SYS_HZ))
105 - gd->arch.lastinc) + now;
107 gd->arch.lastinc = now;
113 * This function is derived from PowerPC code (read timebase as long long).
114 * On ARM it just returns the timer value.
116 unsigned long long get_ticks(void)
121 * This function is derived from PowerPC code (timebase clock frequency).
122 * On ARM it returns the number of timer ticks per second.
124 ulong get_tbclk (void)
127 tbclk = CONFIG_SYS_HZ;