xref: /openbmc/u-boot/arch/arm/cpu/armv7/arch_timer.c (revision 85231c08)
1 /*
2  * (C) Copyright 2012-2014
3  *     Texas Instruments Incorporated, <www.ti.com>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <asm/io.h>
10 #include <div64.h>
11 #include <bootstage.h>
12 
13 DECLARE_GLOBAL_DATA_PTR;
14 
15 #ifndef CONFIG_SYS_HZ_CLOCK
16 static inline u32 read_cntfrq(void)
17 {
18 	u32 frq;
19 
20 	asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (frq));
21 	return frq;
22 }
23 #endif
24 
25 int timer_init(void)
26 {
27 	gd->arch.tbl = 0;
28 	gd->arch.tbu = 0;
29 
30 #ifdef CONFIG_SYS_HZ_CLOCK
31 	gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK;
32 #else
33 	gd->arch.timer_rate_hz = read_cntfrq();
34 #endif
35 	return 0;
36 }
37 
38 unsigned long long get_ticks(void)
39 {
40 	ulong nowl, nowu;
41 
42 	asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (nowl), "=r" (nowu));
43 
44 	gd->arch.tbl = nowl;
45 	gd->arch.tbu = nowu;
46 
47 	return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
48 }
49 
50 
51 ulong timer_get_boot_us(void)
52 {
53 	return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000);
54 }
55 
56 ulong get_tbclk(void)
57 {
58 	return gd->arch.timer_rate_hz;
59 }
60