xref: /openbmc/u-boot/arch/arm/cpu/armv7/arch_timer.c (revision e77e65df)
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 
12 DECLARE_GLOBAL_DATA_PTR;
13 
14 int timer_init(void)
15 {
16 	gd->arch.tbl = 0;
17 	gd->arch.tbu = 0;
18 
19 	gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ;
20 
21 	return 0;
22 }
23 
24 unsigned long long get_ticks(void)
25 {
26 	ulong nowl, nowu;
27 
28 	asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (nowl), "=r" (nowu));
29 
30 	gd->arch.tbl = nowl;
31 	gd->arch.tbu = nowu;
32 
33 	return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
34 }
35 
36 
37 ulong get_timer(ulong base)
38 {
39 	return lldiv(get_ticks(), gd->arch.timer_rate_hz) - base;
40 }
41 
42 void __udelay(unsigned long usec)
43 {
44 	unsigned long long endtime;
45 
46 	endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
47 			1000UL);
48 
49 	endtime += get_ticks();
50 
51 	while (get_ticks() < endtime)
52 		;
53 }
54 
55 ulong get_tbclk(void)
56 {
57 	return gd->arch.timer_rate_hz;
58 }
59