blob: bf07a706a029d5ac54b774a099806b99945eb789 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
David Feng85fd5f12013-12-14 11:47:35 +08002/*
3 * (C) Copyright 2013
4 * David Feng <fenghua@phytium.com.cn>
David Feng85fd5f12013-12-14 11:47:35 +08005 */
6
7#include <common.h>
8#include <command.h>
9#include <asm/system.h>
10
Andre Przywarade223fd2016-11-03 00:56:25 +000011DECLARE_GLOBAL_DATA_PTR;
12
David Feng85fd5f12013-12-14 11:47:35 +080013/*
14 * Generic timer implementation of get_tbclk()
15 */
16unsigned long get_tbclk(void)
17{
18 unsigned long cntfrq;
19 asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
20 return cntfrq;
21}
22
23/*
24 * Generic timer implementation of timer_read_counter()
25 */
26unsigned long timer_read_counter(void)
27{
28 unsigned long cntpct;
York Suna7686cf2015-03-20 19:28:05 -070029#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
30 /* This erratum number needs to be confirmed to match ARM document */
31 unsigned long temp;
32#endif
David Feng85fd5f12013-12-14 11:47:35 +080033 isb();
34 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
York Suna7686cf2015-03-20 19:28:05 -070035#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
36 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
37 while (temp != cntpct) {
38 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
39 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
40 }
41#endif
David Feng85fd5f12013-12-14 11:47:35 +080042 return cntpct;
43}
Aneesh Bansald1074b42015-12-08 13:54:26 +053044
Simon Glasse9873932017-04-05 17:53:17 -060045uint64_t get_ticks(void)
Andre Przywarade223fd2016-11-03 00:56:25 +000046{
47 unsigned long ticks = timer_read_counter();
48
49 gd->arch.tbl = ticks;
50
51 return ticks;
52}
53
Aneesh Bansald1074b42015-12-08 13:54:26 +053054unsigned long usec2ticks(unsigned long usec)
55{
56 ulong ticks;
57 if (usec < 1000)
58 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
59 else
60 ticks = ((usec / 10) * (get_tbclk() / 100000));
61
62 return ticks;
63}
Michal Simek16d73b92018-05-15 16:47:02 +020064
65ulong timer_get_boot_us(void)
66{
67 u64 val = get_ticks() * 1000000;
68
69 return val / get_tbclk();
70}