0ee60342200b796d557eabd8748ea48a272908f4
[oweals/u-boot.git] / arch / arm / cpu / arm920t / ep93xx / timer.c
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 <init.h>
16 #include <time.h>
17 #include <linux/types.h>
18 #include <asm/arch/ep93xx.h>
19 #include <asm/io.h>
20 #include <div64.h>
21
22 #define TIMER_CLKSEL    (1 << 3)
23 #define TIMER_ENABLE    (1 << 7)
24
25 #define TIMER_FREQ                      508469          /* ticks / second */
26 #define TIMER_MAX_VAL                   0xFFFFFFFF
27
28 static struct ep93xx_timer
29 {
30         unsigned long long ticks;
31         unsigned long last_read;
32 } timer;
33
34 static inline unsigned long long usecs_to_ticks(unsigned long usecs)
35 {
36         unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
37         do_div(ticks, 1000 * 1000);
38
39         return ticks;
40 }
41
42 static inline void read_timer(void)
43 {
44         struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
45         const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
46
47         if (now >= timer.last_read)
48                 timer.ticks += now - timer.last_read;
49         else
50                 /* an overflow occurred */
51                 timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
52
53         timer.last_read = now;
54 }
55
56 /*
57  * Get the number of ticks (in CONFIG_SYS_HZ resolution)
58  */
59 unsigned long long get_ticks(void)
60 {
61         unsigned long long sys_ticks;
62
63         read_timer();
64
65         sys_ticks = timer.ticks * CONFIG_SYS_HZ;
66         do_div(sys_ticks, TIMER_FREQ);
67
68         return sys_ticks;
69 }
70
71 unsigned long get_timer(unsigned long base)
72 {
73         return get_ticks() - base;
74 }
75
76 void __udelay(unsigned long usec)
77 {
78         unsigned long long target;
79
80         read_timer();
81
82         target = timer.ticks + usecs_to_ticks(usec);
83
84         while (timer.ticks < target)
85                 read_timer();
86 }
87
88 int timer_init(void)
89 {
90         struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
91
92         /* use timer 3 with 508KHz and free running, not enabled now */
93         writel(TIMER_CLKSEL, &timer_regs->timer3.control);
94
95         /* set initial timer value */
96         writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
97
98         /* Enable the timer */
99         writel(TIMER_ENABLE | TIMER_CLKSEL,
100                 &timer_regs->timer3.control);
101
102         /* Reset the timer */
103         read_timer();
104         timer.ticks = 0;
105
106         return 0;
107 }
108
109 /*
110  * This function is derived from PowerPC code (timebase clock frequency).
111  * On ARM it returns the number of timer ticks per second.
112  */
113 unsigned long get_tbclk(void)
114 {
115         return CONFIG_SYS_HZ;
116 }