Nobuhiro Iwamatsu | 4fb44e2 | 2012-06-13 16:29:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com> |
| 3 | * (C) Copyright 2012 Renesas Solutions Corp. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 4fb44e2 | 2012-06-13 16:29:47 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Nobuhiro Iwamatsu | 7579751 | 2013-11-28 17:52:46 +0900 | [diff] [blame] | 9 | #include <div64.h> |
Nobuhiro Iwamatsu | 4fb44e2 | 2012-06-13 16:29:47 +0900 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/arch-armv7/globaltimer.h> |
| 12 | #include <asm/arch/rmobile.h> |
| 13 | |
| 14 | static struct globaltimer *global_timer = \ |
| 15 | (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR; |
| 16 | |
| 17 | #define CLK2MHZ(clk) (clk / 1000 / 1000) |
| 18 | static u64 get_cpu_global_timer(void) |
| 19 | { |
| 20 | u32 low, high; |
| 21 | u64 timer; |
| 22 | |
| 23 | u32 old = readl(&global_timer->cnt_h); |
| 24 | while (1) { |
| 25 | low = readl(&global_timer->cnt_l); |
| 26 | high = readl(&global_timer->cnt_h); |
| 27 | if (old == high) |
| 28 | break; |
| 29 | else |
| 30 | old = high; |
| 31 | } |
| 32 | |
| 33 | timer = high; |
| 34 | return (u64)((timer << 32) | low); |
| 35 | } |
| 36 | |
| 37 | static u64 get_time_us(void) |
| 38 | { |
| 39 | u64 timer = get_cpu_global_timer(); |
| 40 | |
| 41 | timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1)); |
Nobuhiro Iwamatsu | 7579751 | 2013-11-28 17:52:46 +0900 | [diff] [blame] | 42 | do_div(timer, CLK2MHZ(CONFIG_SYS_CPU_CLK)); |
Nobuhiro Iwamatsu | 4fb44e2 | 2012-06-13 16:29:47 +0900 | [diff] [blame] | 43 | return timer; |
| 44 | } |
| 45 | |
| 46 | static ulong get_time_ms(void) |
| 47 | { |
Nobuhiro Iwamatsu | 7579751 | 2013-11-28 17:52:46 +0900 | [diff] [blame] | 48 | u64 us = get_time_us(); |
| 49 | |
| 50 | do_div(us, 1000); |
| 51 | return us; |
Nobuhiro Iwamatsu | 4fb44e2 | 2012-06-13 16:29:47 +0900 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int timer_init(void) |
| 55 | { |
| 56 | writel(0x01, &global_timer->ctl); |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | void __udelay(unsigned long usec) |
| 61 | { |
| 62 | u64 start, current; |
| 63 | u64 wait; |
| 64 | |
| 65 | start = get_cpu_global_timer(); |
| 66 | wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2); |
| 67 | do { |
| 68 | current = get_cpu_global_timer(); |
| 69 | } while ((current - start) < wait); |
| 70 | } |
| 71 | |
| 72 | ulong get_timer(ulong base) |
| 73 | { |
| 74 | return get_time_ms() - base; |
| 75 | } |
| 76 | |
| 77 | unsigned long long get_ticks(void) |
| 78 | { |
| 79 | return get_cpu_global_timer(); |
| 80 | } |
| 81 | |
| 82 | ulong get_tbclk(void) |
| 83 | { |
| 84 | return (ulong)(CONFIG_SYS_CPU_CLK >> 2); |
| 85 | } |