1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2007,2008 Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
4 * Copyright (C) 2008 Renesas Solutions Corp.
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 #include <asm/processor.h>
14 #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
15 #define CMT_CMCSR_CALIB 0x0000
16 #define CMT_MAX_COUNTER (0xFFFFFFFF)
17 #define CMT_TIMER_RESET (0xFFFF)
19 static vu_long cmt0_timer;
21 static void cmt_timer_start(unsigned int timer)
23 writew(readw(CMSTR) | 0x01, CMSTR);
26 static void cmt_timer_stop(unsigned int timer)
28 writew(readw(CMSTR) & ~0x01, CMSTR);
34 /* Divide clock by 32 */
36 writew(CMT_CMCSR_INIT, CMCSR_0);
38 /* User Device 0 only */
40 writew(CMT_TIMER_RESET, CMCOR_0);
46 unsigned long long get_ticks(void)
51 static vu_long cmcnt = 0;
52 static unsigned long get_usec (void)
54 ulong data = readw(CMCNT_0);
59 cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
61 if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
62 cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
71 ulong get_timer(ulong base)
73 return (get_usec() / 1000) - base;
76 void __udelay(unsigned long usec)
78 unsigned long end = get_usec() + usec;
80 while (get_usec() < end)
84 unsigned long get_tbclk(void)
86 return CONFIG_SH_CMT_CLK_FREQ;