blob: 5484c543c6c8284a67716e3dcff824dd1ff16c1b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +09002/*
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.
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +09008 */
9
10#include <common.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Simon Glass10453152019-11-14 12:57:30 -070012#include <time.h>
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090013#include <asm/io.h>
14#include <asm/processor.h>
Simon Glassc05ed002020-05-10 11:40:11 -060015#include <linux/delay.h>
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090016
Jean-Christophe PLAGNIOL-VILLARD85cb0522008-12-20 15:27:45 +010017#define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090018#define CMT_CMCSR_CALIB 0x0000
19#define CMT_MAX_COUNTER (0xFFFFFFFF)
20#define CMT_TIMER_RESET (0xFFFF)
21
22static vu_long cmt0_timer;
23
24static void cmt_timer_start(unsigned int timer)
25{
26 writew(readw(CMSTR) | 0x01, CMSTR);
27}
28
29static void cmt_timer_stop(unsigned int timer)
30{
31 writew(readw(CMSTR) & ~0x01, CMSTR);
32}
33
34int timer_init(void)
35{
36 cmt0_timer = 0;
37 /* Divide clock by 32 */
38 readw(CMCSR_0);
39 writew(CMT_CMCSR_INIT, CMCSR_0);
40
41 /* User Device 0 only */
42 cmt_timer_stop(0);
Graeme Russ5c8404a2011-07-15 02:18:12 +000043 writew(CMT_TIMER_RESET, CMCOR_0);
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090044 cmt_timer_start(0);
45
46 return 0;
47}
48
49unsigned long long get_ticks(void)
50{
51 return cmt0_timer;
52}
53
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +090054static vu_long cmcnt = 0;
55static unsigned long get_usec (void)
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090056{
57 ulong data = readw(CMCNT_0);
58
59 if (data >= cmcnt)
60 cmcnt = data - cmcnt;
61 else
62 cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
63
64 if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
65 cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
66 else
67 cmt0_timer += cmcnt;
68
69 cmcnt = data;
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +090070 return cmt0_timer;
71}
72
73/* return msec */
74ulong get_timer(ulong base)
75{
Jean-Christophe PLAGNIOL-VILLARD85cb0522008-12-20 15:27:45 +010076 return (get_usec() / 1000) - base;
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090077}
78
Ingo van Lil3eb90ba2009-11-24 14:09:21 +010079void __udelay(unsigned long usec)
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090080{
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +090081 unsigned long end = get_usec() + usec;
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090082
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +090083 while (get_usec() < end)
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090084 continue;
85}
86
87unsigned long get_tbclk(void)
88{
Nobuhiro Iwamatsu8f0960e2014-01-08 14:57:30 +090089 return CONFIG_SH_CMT_CLK_FREQ;
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090090}