2 * Copyright (C) 2012 Altera Corporation <www.altera.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <asm/arch/timer.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 static const struct socfpga_timer *timer_base = (void *)CONFIG_SYS_TIMERBASE;
27 * Timer initialization
31 writel(TIMER_LOAD_VAL, &timer_base->load_val);
32 writel(TIMER_LOAD_VAL, &timer_base->curr_val);
33 writel(readl(&timer_base->ctrl) | 0x3, &timer_base->ctrl);
37 static u32 read_timer(void)
39 return readl(&timer_base->curr_val);
45 void __udelay(unsigned long usec)
47 unsigned long now, last;
49 * get the tmo value based on timer clock speed
50 * tmo = delay required / period of timer clock
52 long tmo = usec * CONFIG_TIMER_CLOCK_KHZ / 1000;
58 /* normal mode (non roll) */
61 /* we have overflow of the count down timer */
62 tmo -= TIMER_LOAD_VAL - last + now;
70 ulong get_timer(ulong base)
72 return get_timer_masked() - base;
76 * Timer : get the time difference
77 * Unit of tick is based on the CONFIG_SYS_HZ
79 ulong get_timer_masked(void)
81 /* current tick value */
82 ulong now = read_timer() / (CONFIG_TIMER_CLOCK_KHZ/CONFIG_SYS_HZ);
83 if (gd->lastinc >= now) {
84 /* normal mode (non roll) */
85 /* move stamp forward with absolute diff ticks */
86 gd->tbl += gd->lastinc - now;
88 /* we have overflow of the count down timer */
89 gd->tbl += TIMER_LOAD_VAL - gd->lastinc + now;
98 void reset_timer(void)
100 /* capture current decrementer value time */
101 gd->lastinc = read_timer() / (CONFIG_TIMER_CLOCK_KHZ/CONFIG_SYS_HZ);
102 /* start "advancing" time stamp from 0 */