1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2007,2008 Nobobuhiro Iwamatsu <iwamatsu@nigauri.org> 4 * Copyright (C) 2008 Renesas Solutions Corp. 5 * 6 * (C) Copyright 2003 7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 8 */ 9 10 #include <init.h> 11 #include <time.h> 12 #include <asm/io.h> 13 #include <asm/processor.h> 14 #include <linux/delay.h> 15 16 #define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */ 17 #define CMT_CMCSR_CALIB 0x0000 18 #define CMT_MAX_COUNTER (0xFFFFFFFF) 19 #define CMT_TIMER_RESET (0xFFFF) 20 21 static vu_long cmt0_timer; 22 cmt_timer_start(unsigned int timer)23static void cmt_timer_start(unsigned int timer) 24 { 25 writew(readw(CMSTR) | 0x01, CMSTR); 26 } 27 cmt_timer_stop(unsigned int timer)28static void cmt_timer_stop(unsigned int timer) 29 { 30 writew(readw(CMSTR) & ~0x01, CMSTR); 31 } 32 timer_init(void)33int timer_init(void) 34 { 35 cmt0_timer = 0; 36 /* Divide clock by 32 */ 37 readw(CMCSR_0); 38 writew(CMT_CMCSR_INIT, CMCSR_0); 39 40 /* User Device 0 only */ 41 cmt_timer_stop(0); 42 writew(CMT_TIMER_RESET, CMCOR_0); 43 cmt_timer_start(0); 44 45 return 0; 46 } 47 get_ticks(void)48unsigned long long get_ticks(void) 49 { 50 return cmt0_timer; 51 } 52 53 static vu_long cmcnt = 0; get_usec(void)54static unsigned long get_usec (void) 55 { 56 ulong data = readw(CMCNT_0); 57 58 if (data >= cmcnt) 59 cmcnt = data - cmcnt; 60 else 61 cmcnt = (CMT_TIMER_RESET - cmcnt) + data; 62 63 if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER) 64 cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER); 65 else 66 cmt0_timer += cmcnt; 67 68 cmcnt = data; 69 return cmt0_timer; 70 } 71 72 /* return msec */ get_timer(ulong base)73ulong get_timer(ulong base) 74 { 75 return (get_usec() / 1000) - base; 76 } 77 __udelay(unsigned long usec)78void __udelay(unsigned long usec) 79 { 80 unsigned long end = get_usec() + usec; 81 82 while (get_usec() < end) 83 continue; 84 } 85 get_tbclk(void)86unsigned long get_tbclk(void) 87 { 88 return CONFIG_SH_CMT_CLK_FREQ; 89 } 90