3 * Ingenic Semiconductor, <jlwei@ingenic.cn>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <asm/jz4740.h>
28 #define TIMER_FDATA 0xffff /* Timer full data value */
30 DECLARE_GLOBAL_DATA_PTR;
32 static struct jz4740_tcu *tcu = (struct jz4740_tcu *)JZ4740_TCU_BASE;
34 void reset_timer_masked(void)
37 gd->lastinc = readw(&tcu->tcnt0);
41 ulong get_timer_masked(void)
43 ulong now = readw(&tcu->tcnt0);
45 if (gd->lastinc <= now)
46 gd->tbl += now - gd->lastinc; /* normal mode */
48 /* we have an overflow ... */
49 gd->tbl += TIMER_FDATA + now - gd->lastinc;
57 void udelay_masked(unsigned long usec)
70 tmo = usec * CONFIG_SYS_HZ;
76 endtime = get_timer_masked() + tmo;
79 ulong now = get_timer_masked();
86 writew(TCU_TCSR_PRESCALE256 | TCU_TCSR_EXT_EN, &tcu->tcsr0);
88 writew(0, &tcu->tcnt0);
89 writew(0, &tcu->tdhr0);
90 writew(TIMER_FDATA, &tcu->tdfr0);
93 writel((1 << TIMER_CHAN) | (1 << (TIMER_CHAN + 16)), &tcu->tmsr);
94 writel(1 << TIMER_CHAN, &tcu->tscr); /* enable timer clock */
95 writeb(1 << TIMER_CHAN, &tcu->tesr); /* start counting up */
103 void reset_timer(void)
105 reset_timer_masked();
108 ulong get_timer(ulong base)
110 return get_timer_masked() - base;
113 void set_timer(ulong t)
118 void __udelay(unsigned long usec)
125 tmo *= CONFIG_SYS_HZ;
129 tmo = usec * CONFIG_SYS_HZ;
135 /* check for rollover during this delay */
137 if ((tmp + tmo) < tmp)
138 reset_timer_masked(); /* timer would roll over */
142 while (get_timer_masked() < tmo)
147 * This function is derived from PowerPC code (read timebase as long long).
148 * On MIPS it just returns the timer value.
150 unsigned long long get_ticks(void)
156 * This function is derived from PowerPC code (timebase clock frequency).
157 * On MIPS it returns the number of timer ticks per second.
159 ulong get_tbclk(void)
161 return CONFIG_SYS_HZ;