2 * Copyright (C) 2007,2008 Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
3 * Copyright (C) 2008 Renesas Solutions Corp.
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/processor.h>
31 #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
32 #define CMT_CMCSR_CALIB 0x0000
33 #define CMT_MAX_COUNTER (0xFFFFFFFF)
34 #define CMT_TIMER_RESET (0xFFFF)
36 static vu_long cmt0_timer;
38 static void cmt_timer_start(unsigned int timer)
40 writew(readw(CMSTR) | 0x01, CMSTR);
43 static void cmt_timer_stop(unsigned int timer)
45 writew(readw(CMSTR) & ~0x01, CMSTR);
51 /* Divide clock by 32 */
53 writew(CMT_CMCSR_INIT, CMCSR_0);
55 /* User Device 0 only */
57 set_timer(CMT_TIMER_RESET);
63 unsigned long long get_ticks(void)
69 ulong get_timer(ulong base)
71 ulong data = readw(CMCNT_0);
76 cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
78 if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
79 cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
84 return cmt0_timer - base;
87 void set_timer(ulong t)
89 writew((u16) t, CMCOR_0);
92 void reset_timer(void)
95 set_timer(CMT_TIMER_RESET);
100 void udelay(unsigned long usec)
102 unsigned int start = get_timer(0);
104 while (get_timer((ulong) start) < (usec * (CONFIG_SYS_HZ / 1000000)))
108 unsigned long get_tbclk(void)
110 return CONFIG_SYS_HZ;