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#include <common.h>
28#include <asm/io.h>
29#include <faraday/fttmr010.h>
30
31static ulong timestamp;
32static ulong lastdec;
33
34int timer_init(void)
35{
36 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
37 unsigned int cr;
38
39 debug("%s()\n", __func__);
40
41
42 writel(0, &tmr->cr);
43
44#ifdef CONFIG_FTTMR010_EXT_CLK
45
46 ftpmu010_32768osc_enable();
47#endif
48
49
50 writel(TIMER_LOAD_VAL, &tmr->timer3_load);
51 writel(TIMER_LOAD_VAL, &tmr->timer3_counter);
52 writel(0, &tmr->timer3_match1);
53 writel(0, &tmr->timer3_match2);
54
55
56 writel(FTTMR010_TM3_MATCH1 |
57 FTTMR010_TM3_MATCH2 |
58 FTTMR010_TM3_OVERFLOW,
59 &tmr->interrupt_mask);
60
61 cr = readl(&tmr->cr);
62#ifdef CONFIG_FTTMR010_EXT_CLK
63 cr |= FTTMR010_TM3_CLOCK;
64#endif
65 cr |= FTTMR010_TM3_ENABLE;
66 writel(cr, &tmr->cr);
67
68
69 reset_timer_masked();
70
71 return 0;
72}
73
74
75
76
77
78
79
80
81void reset_timer_masked(void)
82{
83 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
84
85
86#ifdef CONFIG_FTTMR010_EXT_CLK
87 lastdec = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
88#else
89 lastdec = readl(&tmr->timer3_counter) / (CONFIG_SYS_CLK_FREQ / 2);
90#endif
91 timestamp = 0;
92
93 debug("%s(): lastdec = %lx\n", __func__, lastdec);
94}
95
96void reset_timer(void)
97{
98 debug("%s()\n", __func__);
99 reset_timer_masked();
100}
101
102
103
104
105ulong get_timer_masked(void)
106{
107 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
108
109
110#ifdef CONFIG_FTTMR010_EXT_CLK
111 ulong now = readl(&tmr->timer3_counter) / (TIMER_CLOCK / CONFIG_SYS_HZ);
112#else
113 ulong now = readl(&tmr->timer3_counter) / \
114 (CONFIG_SYS_CLK_FREQ / 2 / 1024);
115#endif
116
117 debug("%s(): now = %lx, lastdec = %lx\n", __func__, now, lastdec);
118
119 if (lastdec >= now) {
120
121
122
123
124 timestamp += lastdec - now;
125 } else {
126
127
128
129
130
131
132
133
134
135 timestamp += lastdec + TIMER_LOAD_VAL - now;
136 }
137
138 lastdec = now;
139
140 debug("%s() returns %lx\n", __func__, timestamp);
141
142 return timestamp;
143}
144
145
146
147
148ulong get_timer(ulong base)
149{
150 debug("%s(%lx)\n", __func__, base);
151 return get_timer_masked() - base;
152}
153
154void set_timer(ulong t)
155{
156 debug("%s(%lx)\n", __func__, t);
157 timestamp = t;
158}
159
160
161void __udelay(unsigned long usec)
162{
163 struct fttmr010 *tmr = (struct fttmr010 *)CONFIG_FTTMR010_BASE;
164
165#ifdef CONFIG_FTTMR010_EXT_CLK
166 long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
167#else
168 long tmo = usec * ((CONFIG_SYS_CLK_FREQ / 2) / 1000) / 1000;
169#endif
170 unsigned long now, last = readl(&tmr->timer3_counter);
171
172 debug("%s(%lu)\n", __func__, usec);
173 while (tmo > 0) {
174 now = readl(&tmr->timer3_counter);
175 if (now > last)
176 tmo -= TIMER_LOAD_VAL + last - now;
177 else
178 tmo -= last - now;
179 last = now;
180 }
181}
182
183
184
185
186
187unsigned long long get_ticks(void)
188{
189 debug("%s()\n", __func__);
190 return get_timer(0);
191}
192
193
194
195
196
197ulong get_tbclk(void)
198{
199 debug("%s()\n", __func__);
200#ifdef CONFIG_FTTMR010_EXT_CLK
201 return CONFIG_SYS_HZ;
202#else
203 return CONFIG_SYS_CLK_FREQ;
204#endif
205}
206