blob: 871629c2333b2b782fbf0ebd9a9978d7992f5b3c [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>
Vitaly Andrianov0e902872014-04-04 13:16:49 -040010#include <asm/io.h>
11#include <div64.h>
Patrick Delaunaycfab8482016-11-22 17:31:33 +010012#include <bootstage.h>
Vitaly Andrianov0e902872014-04-04 13:16:49 -040013
14DECLARE_GLOBAL_DATA_PTR;
15
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010016#ifndef CONFIG_SYS_HZ_CLOCK
17static 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
Vitaly Andrianov0e902872014-04-04 13:16:49 -040026int timer_init(void)
27{
28 gd->arch.tbl = 0;
29 gd->arch.tbu = 0;
30
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010031#ifdef CONFIG_SYS_HZ_CLOCK
Patrick Delaunayd502ee02018-03-12 10:46:06 +010032 gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK;
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010033#else
34 gd->arch.timer_rate_hz = read_cntfrq();
35#endif
Vitaly Andrianov0e902872014-04-04 13:16:49 -040036 return 0;
37}
38
39unsigned 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
Patrick Delaunaycfab8482016-11-22 17:31:33 +010052ulong timer_get_boot_us(void)
53{
Patrick Delaunaye4dfef92019-04-18 17:32:46 +020054 if (!gd->arch.timer_rate_hz)
55 timer_init();
56
Patrick Delaunayb3c5a9f2018-03-20 11:41:23 +010057 return lldiv(get_ticks(), gd->arch.timer_rate_hz / 1000000);
Vitaly Andrianov0e902872014-04-04 13:16:49 -040058}
59
60ulong get_tbclk(void)
61{
62 return gd->arch.timer_rate_hz;
63}