2 * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
3 * (C) Copyright 2012 Renesas Solutions Corp.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <asm/arch-armv7/globaltimer.h>
27 #include <asm/arch/rmobile.h>
29 static struct globaltimer *global_timer = \
30 (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
32 #define CLK2MHZ(clk) (clk / 1000 / 1000)
33 static u64 get_cpu_global_timer(void)
38 u32 old = readl(&global_timer->cnt_h);
40 low = readl(&global_timer->cnt_l);
41 high = readl(&global_timer->cnt_h);
49 return (u64)((timer << 32) | low);
52 static u64 get_time_us(void)
54 u64 timer = get_cpu_global_timer();
56 timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
57 timer /= (u64)CLK2MHZ(CONFIG_SYS_CPU_CLK);
61 static ulong get_time_ms(void)
63 return (ulong)(get_time_us() / 1000);
68 writel(0x01, &global_timer->ctl);
72 void __udelay(unsigned long usec)
77 start = get_cpu_global_timer();
78 wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
80 current = get_cpu_global_timer();
81 } while ((current - start) < wait);
84 ulong get_timer(ulong base)
86 return get_time_ms() - base;
89 unsigned long long get_ticks(void)
91 return get_cpu_global_timer();
96 return (ulong)(CONFIG_SYS_CPU_CLK >> 2);