summaryrefslogtreecommitdiff
path: root/arch/xtensa/lib/time.c
blob: 1c927d2a6a3cdcdd3eac807a60064f4544dd37ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2008 - 2013 Tensilica Inc.
 */

#include <common.h>
#include <clock_legacy.h>
#include <time.h>
#include <asm/global_data.h>
#include <linux/delay.h>
#include <linux/stringify.h>

DECLARE_GLOBAL_DATA_PTR;

#if XCHAL_HAVE_CCOUNT
static ulong get_ccount(void)
{
	ulong ccount;
	asm volatile ("rsr %0,"__stringify(CCOUNT) : "=a" (ccount));
	return ccount;
}
#else
static ulong fake_ccount;
#define get_ccount() fake_ccount
#endif

static void delay_cycles(unsigned cycles)
{
#if XCHAL_HAVE_CCOUNT
	unsigned expiry = get_ccount() + cycles;
	while ((signed)(expiry - get_ccount()) > 0)
		;
#else
#warning "Without Xtensa timer option, timing will not be accurate."

	/*
	 * Approximate the cycle count by a loop iteration count.
	 * This is highly dependent on config and optimization.
	 */

	volatile unsigned i;
	for (i = cycles >> 4U; i > 0; --i)
		;
	fake_ccount += cycles;
#endif
}

/*
 * Delay (busy-wait) for a number of microseconds.
 */

void __udelay(unsigned long usec)
{
	ulong lo, hi, i;
	ulong mhz = get_board_sys_clk() / 1000000;

	/* Scale to support full 32-bit usec range */

	lo = usec & ((1<<22)-1);
	hi = usec >> 22UL;
	for (i = 0; i < hi; ++i)
		delay_cycles(mhz << 22);
	delay_cycles(mhz * lo);
}


/*
 * Return the elapsed time (ticks) since 'base'.
 */

ulong get_timer(ulong base)
{
	/* Don't tie up a timer; use cycle counter if available (or fake it) */

#if XCHAL_HAVE_CCOUNT
	register ulong ccount;
	__asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
	return ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base;
#else
	/*
	 * Add at least the overhead of this call (in cycles).
	 * Avoids hanging in case caller doesn't use udelay().
	 * Note that functions that don't call udelay() (such as
	 * the "sleep" command) will not get a significant delay
	 * because there is no time reference.
	 */

	fake_ccount += 20;
	return fake_ccount / (get_board_sys_clk() / CONFIG_SYS_HZ) - base;
#endif
}


/*
 * This function is derived from ARM/PowerPC code (read timebase as long long).
 * On Xtensa it just returns the timer value.
 */
unsigned long long get_ticks(void)
{
	return get_timer(0);
}

/*
 * This function is derived from ARM/PowerPC code (timebase clock frequency).
 * On Xtensa it returns the number of timer ticks per second.
 */
ulong get_tbclk(void)
{
	return CONFIG_SYS_HZ;
}

#if XCHAL_HAVE_CCOUNT
unsigned long timer_get_us(void)
{
	unsigned long ccount;

	__asm__ volatile ("rsr %0, CCOUNT" : "=a"(ccount));
	return ccount / (get_board_sys_clk() / 1000000);
}
#endif