Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012-2014 |
| 4 | * Texas Instruments Incorporated, <www.ti.com> |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 8 | #include <time.h> |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <div64.h> |
Patrick Delaunay | cfab848 | 2016-11-22 17:31:33 +0100 | [diff] [blame] | 11 | #include <bootstage.h> |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 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 | |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 25 | int timer_init(void) |
| 26 | { |
| 27 | gd->arch.tbl = 0; |
| 28 | gd->arch.tbu = 0; |
| 29 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_SYS_HZ_CLOCK |
Patrick Delaunay | d502ee0 | 2018-03-12 10:46:06 +0100 | [diff] [blame] | 31 | gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK; |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 32 | #else |
| 33 | gd->arch.timer_rate_hz = read_cntfrq(); |
| 34 | #endif |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 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 | |
Patrick Delaunay | cfab848 | 2016-11-22 17:31:33 +0100 | [diff] [blame] | 51 | ulong timer_get_boot_us(void) |
| 52 | { |
Patrick Delaunay | e4dfef9 | 2019-04-18 17:32:46 +0200 | [diff] [blame] | 53 | if (!gd->arch.timer_rate_hz) |
| 54 | timer_init(); |
| 55 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 56 | return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000); |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | ulong get_tbclk(void) |
| 60 | { |
| 61 | return gd->arch.timer_rate_hz; |
| 62 | } |