2 * TNETV107X: Timer implementation
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <asm/arch/clock.h>
38 #define regs ((struct timer_regs *)CONFIG_SYS_TIMERBASE)
40 #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
41 #define TIM_CLK_DIV 16
43 static ulong timestamp;
48 clk_enable(TNETV107X_LPSC_TIMER0);
50 lastinc = timestamp = 0;
52 /* We are using timer34 in unchained 32-bit mode, full speed */
53 __raw_writel(0x0, ®s->tcr);
54 __raw_writel(0x0, ®s->tgcr);
55 __raw_writel(0x06 | ((TIM_CLK_DIV - 1) << 8), ®s->tgcr);
56 __raw_writel(0x0, ®s->tim34);
57 __raw_writel(TIMER_LOAD_VAL, ®s->prd34);
58 __raw_writel(2 << 22, ®s->tcr);
63 void reset_timer(void)
65 lastinc = timestamp = 0;
67 __raw_writel(0, ®s->tcr);
68 __raw_writel(0, ®s->tim34);
69 __raw_writel(2 << 22, ®s->tcr);
72 static ulong get_timer_raw(void)
74 ulong now = __raw_readl(®s->tim34);
77 timestamp += now - lastinc;
79 timestamp += now + TIMER_LOAD_VAL - lastinc;
86 ulong get_timer(ulong base)
88 return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base;
91 void set_timer(ulong t)
96 unsigned long long get_ticks(void)
101 void __udelay(unsigned long usec)
107 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
109 tmo /= (1000 * TIM_CLK_DIV);
111 endtime = get_timer_raw() + tmo;
114 ulong now = get_timer_raw();
115 diff = endtime - now;
119 ulong get_tbclk(void)
121 return CONFIG_SYS_HZ;