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> |
| 8 | #include <asm/io.h> |
| 9 | #include <div64.h> |
Patrick Delaunay | cfab848 | 2016-11-22 17:31:33 +0100 | [diff] [blame] | 10 | #include <bootstage.h> |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 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 | |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 24 | int timer_init(void) |
| 25 | { |
| 26 | gd->arch.tbl = 0; |
| 27 | gd->arch.tbu = 0; |
| 28 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 29 | #ifdef CONFIG_SYS_HZ_CLOCK |
Patrick Delaunay | d502ee0 | 2018-03-12 10:46:06 +0100 | [diff] [blame] | 30 | gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK; |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 31 | #else |
| 32 | gd->arch.timer_rate_hz = read_cntfrq(); |
| 33 | #endif |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 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 | |
Patrick Delaunay | cfab848 | 2016-11-22 17:31:33 +0100 | [diff] [blame] | 50 | ulong timer_get_boot_us(void) |
| 51 | { |
Patrick Delaunay | e4dfef9 | 2019-04-18 17:32:46 +0200 | [diff] [blame] | 52 | if (!gd->arch.timer_rate_hz) |
| 53 | timer_init(); |
| 54 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 55 | return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000); |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | ulong get_tbclk(void) |
| 59 | { |
| 60 | return gd->arch.timer_rate_hz; |
| 61 | } |