blob: 17bd53dae847c46c5ed95d909b2db230c65836ed [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vitaly Andrianov0e902872014-04-04 13:16:49 -04002/*
3 * (C) Copyright 2012-2014
4 * Texas Instruments Incorporated, <www.ti.com>
Vitaly Andrianov0e902872014-04-04 13:16:49 -04005 */
6
7#include <common.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Simon Glass495a5dc2019-11-14 12:57:30 -07009#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Vitaly Andrianov0e902872014-04-04 13:16:49 -040011#include <asm/io.h>
12#include <div64.h>
Patrick Delaunaycfab8482016-11-22 17:31:33 +010013#include <bootstage.h>
Vitaly Andrianov0e902872014-04-04 13:16:49 -040014
15DECLARE_GLOBAL_DATA_PTR;
16
Tom Rini6a5dccc2022-11-16 13:10:41 -050017#ifndef CFG_SYS_HZ_CLOCK
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010018static 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 Andrianov0e902872014-04-04 13:16:49 -040027int timer_init(void)
28{
29 gd->arch.tbl = 0;
30 gd->arch.tbu = 0;
31
Tom Rini6a5dccc2022-11-16 13:10:41 -050032#ifdef CFG_SYS_HZ_CLOCK
33 gd->arch.timer_rate_hz = CFG_SYS_HZ_CLOCK;
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010034#else
35 gd->arch.timer_rate_hz = read_cntfrq();
36#endif
Vitaly Andrianov0e902872014-04-04 13:16:49 -040037 return 0;
38}
39
40unsigned 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 Delaunaycfab8482016-11-22 17:31:33 +010053ulong timer_get_boot_us(void)
54{
Patrick Delaunaye4dfef92019-04-18 17:32:46 +020055 if (!gd->arch.timer_rate_hz)
56 timer_init();
57
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010058 return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000);
Vitaly Andrianov0e902872014-04-04 13:16:49 -040059}
60
61ulong get_tbclk(void)
62{
63 return gd->arch.timer_rate_hz;
64}