• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Cirrus Logic EP93xx timer support.
4  *
5  * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
6  *
7  * Copyright (C) 2004, 2005
8  * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
9  *
10  * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
11  * author unknown.
12  */
13 
14 #include <common.h>
15 #include <time.h>
16 #include <linux/types.h>
17 #include <asm/arch/ep93xx.h>
18 #include <asm/io.h>
19 #include <div64.h>
20 
21 #define TIMER_CLKSEL	(1 << 3)
22 #define TIMER_ENABLE	(1 << 7)
23 
24 #define TIMER_FREQ			508469		/* ticks / second */
25 #define TIMER_MAX_VAL			0xFFFFFFFF
26 
27 static struct ep93xx_timer
28 {
29 	unsigned long long ticks;
30 	unsigned long last_read;
31 } timer;
32 
usecs_to_ticks(unsigned long usecs)33 static inline unsigned long long usecs_to_ticks(unsigned long usecs)
34 {
35 	unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
36 	do_div(ticks, 1000 * 1000);
37 
38 	return ticks;
39 }
40 
read_timer(void)41 static inline void read_timer(void)
42 {
43 	struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
44 	const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
45 
46 	if (now >= timer.last_read)
47 		timer.ticks += now - timer.last_read;
48 	else
49 		/* an overflow occurred */
50 		timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
51 
52 	timer.last_read = now;
53 }
54 
55 /*
56  * Get the number of ticks (in CONFIG_SYS_HZ resolution)
57  */
get_ticks(void)58 unsigned long long get_ticks(void)
59 {
60 	unsigned long long sys_ticks;
61 
62 	read_timer();
63 
64 	sys_ticks = timer.ticks * CONFIG_SYS_HZ;
65 	do_div(sys_ticks, TIMER_FREQ);
66 
67 	return sys_ticks;
68 }
69 
get_timer(unsigned long base)70 unsigned long get_timer(unsigned long base)
71 {
72 	return get_ticks() - base;
73 }
74 
__udelay(unsigned long usec)75 void __udelay(unsigned long usec)
76 {
77 	unsigned long long target;
78 
79 	read_timer();
80 
81 	target = timer.ticks + usecs_to_ticks(usec);
82 
83 	while (timer.ticks < target)
84 		read_timer();
85 }
86 
timer_init(void)87 int timer_init(void)
88 {
89 	struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
90 
91 	/* use timer 3 with 508KHz and free running, not enabled now */
92 	writel(TIMER_CLKSEL, &timer_regs->timer3.control);
93 
94 	/* set initial timer value */
95 	writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
96 
97 	/* Enable the timer */
98 	writel(TIMER_ENABLE | TIMER_CLKSEL,
99 		&timer_regs->timer3.control);
100 
101 	/* Reset the timer */
102 	read_timer();
103 	timer.ticks = 0;
104 
105 	return 0;
106 }
107 
108 /*
109  * This function is derived from PowerPC code (timebase clock frequency).
110  * On ARM it returns the number of timer ticks per second.
111  */
get_tbclk(void)112 unsigned long get_tbclk(void)
113 {
114 	return CONFIG_SYS_HZ;
115 }
116