Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / arch / arm / cpu / armv7 / arch_timer.c
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/io.h>
11 #include <div64.h>
12 #include <bootstage.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 #ifndef CONFIG_SYS_HZ_CLOCK
17 static inline u32 read_cntfrq(void)
18 {
19         u32 frq;
20
21         asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (frq));
22         return frq;
23 }
24 #endif
25
26 int timer_init(void)
27 {
28         gd->arch.tbl = 0;
29         gd->arch.tbu = 0;
30
31 #ifdef CONFIG_SYS_HZ_CLOCK
32         gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK;
33 #else
34         gd->arch.timer_rate_hz = read_cntfrq();
35 #endif
36         return 0;
37 }
38
39 unsigned long long get_ticks(void)
40 {
41         ulong nowl, nowu;
42
43         asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (nowl), "=r" (nowu));
44
45         gd->arch.tbl = nowl;
46         gd->arch.tbu = nowu;
47
48         return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
49 }
50
51
52 ulong timer_get_boot_us(void)
53 {
54         if (!gd->arch.timer_rate_hz)
55                 timer_init();
56
57         return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000);
58 }
59
60 ulong get_tbclk(void)
61 {
62         return gd->arch.timer_rate_hz;
63 }