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