37522dcd8471f2bc376e9ce769a5647cfd81f36b
[pandora-u-boot.git] / arch / arm / cpu / armv7 / rmobile / timer.c
1 /*
2  * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
3  * (C) Copyright 2012 Renesas Solutions Corp.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <asm/io.h>
26 #include <asm/arch-armv7/globaltimer.h>
27 #include <asm/arch/rmobile.h>
28
29 static struct globaltimer *global_timer = \
30                 (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
31
32 #define CLK2MHZ(clk)    (clk / 1000 / 1000)
33 static u64 get_cpu_global_timer(void)
34 {
35         u32 low, high;
36         u64 timer;
37
38         u32 old = readl(&global_timer->cnt_h);
39         while (1) {
40                 low = readl(&global_timer->cnt_l);
41                 high = readl(&global_timer->cnt_h);
42                 if (old == high)
43                         break;
44                 else
45                         old = high;
46         }
47
48         timer = high;
49         return (u64)((timer << 32) | low);
50 }
51
52 static u64 get_time_us(void)
53 {
54         u64 timer = get_cpu_global_timer();
55
56         timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
57         timer /= (u64)CLK2MHZ(CONFIG_SYS_CPU_CLK);
58         return timer;
59 }
60
61 static ulong get_time_ms(void)
62 {
63         return (ulong)(get_time_us() / 1000);
64 }
65
66 int timer_init(void)
67 {
68         writel(0x01, &global_timer->ctl);
69         return 0;
70 }
71
72 void __udelay(unsigned long usec)
73 {
74         u64 start, current;
75         u64 wait;
76
77         start = get_cpu_global_timer();
78         wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
79         do {
80                 current = get_cpu_global_timer();
81         } while ((current - start) < wait);
82 }
83
84 ulong get_timer(ulong base)
85 {
86         return get_time_ms() - base;
87 }
88
89 unsigned long long get_ticks(void)
90 {
91         return get_cpu_global_timer();
92 }
93
94 ulong get_tbclk(void)
95 {
96         return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
97 }