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 <init.h>
9 #include <time.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <div64.h>
13 #include <bootstage.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 #ifndef CONFIG_SYS_HZ_CLOCK
read_cntfrq(void)18 static inline u32 read_cntfrq(void)
19 {
20 	u32 frq;
21 
22 	asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (frq));
23 	return frq;
24 }
25 #endif
26 
timer_init(void)27 int timer_init(void)
28 {
29 	gd->arch.tbl = 0;
30 	gd->arch.tbu = 0;
31 
32 #ifdef CONFIG_SYS_HZ_CLOCK
33 	gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK;
34 #else
35 	gd->arch.timer_rate_hz = read_cntfrq();
36 #endif
37 	return 0;
38 }
39 
get_ticks(void)40 unsigned long long get_ticks(void)
41 {
42 	ulong nowl, nowu;
43 
44 	asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (nowl), "=r" (nowu));
45 
46 	gd->arch.tbl = nowl;
47 	gd->arch.tbu = nowu;
48 
49 	return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
50 }
51 
52 
timer_get_boot_us(void)53 ulong timer_get_boot_us(void)
54 {
55 	if (!gd->arch.timer_rate_hz)
56 		timer_init();
57 
58 	return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000);
59 }
60 
get_tbclk(void)61 ulong get_tbclk(void)
62 {
63 	return gd->arch.timer_rate_hz;
64 }
65