blob: 789b46f0e0261273cfb2545f34bfc3254a7be29c [file] [log] [blame]
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +09001/*
2 * Copyright (C) 2007,2008 Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
3 * Copyright (C) 2008 Renesas Solutions Corp.
4 *
5 * (C) Copyright 2003
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <asm/io.h>
29#include <asm/processor.h>
30
Jean-Christophe PLAGNIOL-VILLARD85cb0522008-12-20 15:27:45 +010031#define CMT_CMCSR_INIT 0x0001 /* PCLK/32 */
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090032#define CMT_CMCSR_CALIB 0x0000
33#define CMT_MAX_COUNTER (0xFFFFFFFF)
34#define CMT_TIMER_RESET (0xFFFF)
35
36static vu_long cmt0_timer;
37
38static void cmt_timer_start(unsigned int timer)
39{
40 writew(readw(CMSTR) | 0x01, CMSTR);
41}
42
43static void cmt_timer_stop(unsigned int timer)
44{
45 writew(readw(CMSTR) & ~0x01, CMSTR);
46}
47
48int timer_init(void)
49{
50 cmt0_timer = 0;
51 /* Divide clock by 32 */
52 readw(CMCSR_0);
53 writew(CMT_CMCSR_INIT, CMCSR_0);
54
55 /* User Device 0 only */
56 cmt_timer_stop(0);
57 set_timer(CMT_TIMER_RESET);
58 cmt_timer_start(0);
59
60 return 0;
61}
62
63unsigned long long get_ticks(void)
64{
65 return cmt0_timer;
66}
67
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +090068static vu_long cmcnt = 0;
69static unsigned long get_usec (void)
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090070{
71 ulong data = readw(CMCNT_0);
72
73 if (data >= cmcnt)
74 cmcnt = data - cmcnt;
75 else
76 cmcnt = (CMT_TIMER_RESET - cmcnt) + data;
77
78 if ((cmt0_timer + cmcnt) > CMT_MAX_COUNTER)
79 cmt0_timer = ((cmt0_timer + cmcnt) - CMT_MAX_COUNTER);
80 else
81 cmt0_timer += cmcnt;
82
83 cmcnt = data;
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +090084 return cmt0_timer;
85}
86
87/* return msec */
88ulong get_timer(ulong base)
89{
Jean-Christophe PLAGNIOL-VILLARD85cb0522008-12-20 15:27:45 +010090 return (get_usec() / 1000) - base;
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +090091}
92
93void set_timer(ulong t)
94{
95 writew((u16) t, CMCOR_0);
96}
97
98void reset_timer(void)
99{
100 cmt_timer_stop(0);
101 set_timer(CMT_TIMER_RESET);
102 cmt0_timer = 0;
103 cmt_timer_start(0);
104}
105
Ingo van Lil3eb90ba2009-11-24 14:09:21 +0100106void __udelay(unsigned long usec)
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +0900107{
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +0900108 unsigned long end = get_usec() + usec;
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +0900109
Nobuhiro Iwamatsud8bbc512008-12-09 11:32:46 +0900110 while (get_usec() < end)
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +0900111 continue;
112}
113
114unsigned long get_tbclk(void)
115{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200116 return CONFIG_SYS_HZ;
Nobuhiro Iwamatsu6ad43d02008-08-31 22:48:33 +0900117}