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