blob: 8f47a82043b00519100bb4e600edc23c47ac8d90 [file] [log] [blame]
David Feng85fd5f12013-12-14 11:47:35 +08001/*
2 * (C) Copyright 2013
3 * David Feng <fenghua@phytium.com.cn>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <command.h>
10#include <asm/system.h>
11
12/*
13 * Generic timer implementation of get_tbclk()
14 */
15unsigned long get_tbclk(void)
16{
17 unsigned long cntfrq;
18 asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
19 return cntfrq;
20}
21
22/*
23 * Generic timer implementation of timer_read_counter()
24 */
25unsigned long timer_read_counter(void)
26{
27 unsigned long cntpct;
York Suna7686cf2015-03-20 19:28:05 -070028#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
29 /* This erratum number needs to be confirmed to match ARM document */
30 unsigned long temp;
31#endif
David Feng85fd5f12013-12-14 11:47:35 +080032 isb();
33 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
York Suna7686cf2015-03-20 19:28:05 -070034#ifdef CONFIG_SYS_FSL_ERRATUM_A008585
35 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
36 while (temp != cntpct) {
37 asm volatile("mrs %0, cntpct_el0" : "=r" (cntpct));
38 asm volatile("mrs %0, cntpct_el0" : "=r" (temp));
39 }
40#endif
David Feng85fd5f12013-12-14 11:47:35 +080041 return cntpct;
42}
Aneesh Bansald1074b42015-12-08 13:54:26 +053043
44unsigned long usec2ticks(unsigned long usec)
45{
46 ulong ticks;
47 if (usec < 1000)
48 ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
49 else
50 ticks = ((usec / 10) * (get_tbclk() / 100000));
51
52 return ticks;
53}