3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Alex Zuepke <azu@sysgo.de>
11 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 static ulong timer_load_val = 0;
37 /* macro to read the 16 bit timer */
38 static inline ulong READ_TIMER(void)
40 lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
41 lh7a40x_timer_t* timer = &timers->timer1;
43 return (timer->value & 0x0000ffff);
46 static ulong timestamp;
51 lh7a40x_timers_t* timers = LH7A40X_TIMERS_PTR;
52 lh7a40x_timer_t* timer = &timers->timer1;
54 /* a periodic timer using the 508kHz source */
55 timer->control = (TIMER_PER | TIMER_CLK508K);
57 if (timer_load_val == 0) {
59 * 10ms period with 508.469kHz clock = 5084
61 timer_load_val = CONFIG_SYS_HZ/100;
64 /* load value for 10 ms timeout */
65 lastdec = timer->load = timer_load_val;
67 /* auto load, start timer */
68 timer->control = timer->control | TIMER_EN;
75 * timer without interrupts
77 ulong get_timer (ulong base)
79 return (get_timer_masked() - base);
82 void __udelay (unsigned long usec)
94 tmo = usec * CONFIG_SYS_HZ;
101 /* check for rollover during this delay */
103 if ((tmp + tmo) < tmp )
104 reset_timer_masked(); /* timer would roll over */
108 while (get_timer_masked () < tmo);
111 void reset_timer_masked (void)
114 lastdec = READ_TIMER();
118 ulong get_timer_masked (void)
120 ulong now = READ_TIMER();
122 if (lastdec >= now) {
124 timestamp += (lastdec - now);
126 /* we have an overflow ... */
127 timestamp += ((lastdec + timer_load_val) - now);
134 void udelay_masked (unsigned long usec)
143 tmo *= CONFIG_SYS_HZ;
147 tmo = usec * CONFIG_SYS_HZ;
154 endtime = get_timer_masked () + tmo;
157 ulong now = get_timer_masked ();
158 diff = endtime - now;
163 * This function is derived from PowerPC code (read timebase as long long).
164 * On ARM it just returns the timer value.
166 unsigned long long get_ticks(void)
172 * This function is derived from PowerPC code (timebase clock frequency).
173 * On ARM it returns the number of timer ticks per second.
175 ulong get_tbclk (void)
179 tbclk = timer_load_val * 100;