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 | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 9 | #include <time.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <div64.h> |
Patrick Delaunay | cfab848 | 2016-11-22 17:31:33 +0100 | [diff] [blame] | 13 | #include <bootstage.h> |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 17 | #ifndef CFG_SYS_HZ_CLOCK |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 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 | |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 27 | int timer_init(void) |
| 28 | { |
| 29 | gd->arch.tbl = 0; |
| 30 | gd->arch.tbu = 0; |
| 31 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 32 | #ifdef CFG_SYS_HZ_CLOCK |
| 33 | gd->arch.timer_rate_hz = CFG_SYS_HZ_CLOCK; |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 34 | #else |
| 35 | gd->arch.timer_rate_hz = read_cntfrq(); |
| 36 | #endif |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
| 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 | |
Patrick Delaunay | cfab848 | 2016-11-22 17:31:33 +0100 | [diff] [blame] | 53 | ulong timer_get_boot_us(void) |
| 54 | { |
Patrick Delaunay | e4dfef9 | 2019-04-18 17:32:46 +0200 | [diff] [blame] | 55 | if (!gd->arch.timer_rate_hz) |
| 56 | timer_init(); |
| 57 | |
Patrick Delaunay | b3c5a9f | 2018-03-20 11:41:23 +0100 | [diff] [blame] | 58 | return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000); |
Vitaly Andrianov | 0e90287 | 2014-04-04 13:16:49 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ulong get_tbclk(void) |
| 62 | { |
| 63 | return gd->arch.timer_rate_hz; |
| 64 | } |