1 // SPDX-License-Identifier: GPL-2.0+
3 * Cirrus Logic EP93xx timer support.
5 * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
7 * Copyright (C) 2004, 2005
8 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
10 * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
15 #include <linux/types.h>
16 #include <asm/arch/ep93xx.h>
20 #define TIMER_CLKSEL (1 << 3)
21 #define TIMER_ENABLE (1 << 7)
23 #define TIMER_FREQ 508469 /* ticks / second */
24 #define TIMER_MAX_VAL 0xFFFFFFFF
26 static struct ep93xx_timer
28 unsigned long long ticks;
29 unsigned long last_read;
32 static inline unsigned long long usecs_to_ticks(unsigned long usecs)
34 unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
35 do_div(ticks, 1000 * 1000);
40 static inline void read_timer(void)
42 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
43 const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
45 if (now >= timer.last_read)
46 timer.ticks += now - timer.last_read;
48 /* an overflow occurred */
49 timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
51 timer.last_read = now;
55 * Get the number of ticks (in CONFIG_SYS_HZ resolution)
57 unsigned long long get_ticks(void)
59 unsigned long long sys_ticks;
63 sys_ticks = timer.ticks * CONFIG_SYS_HZ;
64 do_div(sys_ticks, TIMER_FREQ);
69 unsigned long get_timer_masked(void)
74 unsigned long get_timer(unsigned long base)
76 return get_timer_masked() - base;
79 void __udelay(unsigned long usec)
81 unsigned long long target;
85 target = timer.ticks + usecs_to_ticks(usec);
87 while (timer.ticks < target)
93 struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
95 /* use timer 3 with 508KHz and free running, not enabled now */
96 writel(TIMER_CLKSEL, &timer_regs->timer3.control);
98 /* set initial timer value */
99 writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
101 /* Enable the timer */
102 writel(TIMER_ENABLE | TIMER_CLKSEL,
103 &timer_regs->timer3.control);
105 /* Reset the timer */
113 * This function is derived from PowerPC code (timebase clock frequency).
114 * On ARM it returns the number of timer ticks per second.
116 unsigned long get_tbclk(void)
118 return CONFIG_SYS_HZ;