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
28
29
30
31
32#include <linux/module.h>
33#include <linux/profile.h>
34#include <linux/interrupt.h>
35#include <linux/time.h>
36#include <linux/irq.h>
37
38#include <asm/blackfin.h>
39#include <asm/time.h>
40
41
42#define TICK_SIZE (tick_nsec / 1000)
43
44static void time_sched_init(irq_handler_t timer_routine);
45static unsigned long gettimeoffset(void);
46
47static struct irqaction bfin_timer_irq = {
48 .name = "BFIN Timer Tick",
49 .flags = IRQF_DISABLED
50};
51
52static void
53time_sched_init(irq_handler_t timer_routine)
54{
55 u32 tcount;
56
57
58 bfin_write_TCNTL(1);
59 CSYNC();
60
61
62
63
64 bfin_write_TSCALE((TIME_SCALE - 1));
65
66 tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
67 bfin_write_TPERIOD(tcount);
68 bfin_write_TCOUNT(tcount);
69
70
71 CSYNC();
72
73 bfin_write_TCNTL(7);
74
75 bfin_timer_irq.handler = (irq_handler_t)timer_routine;
76
77
78
79 setup_irq(IRQ_CORETMR, &bfin_timer_irq);
80}
81
82
83
84
85static unsigned long gettimeoffset(void)
86{
87 unsigned long offset;
88 unsigned long clocks_per_jiffy;
89
90 clocks_per_jiffy = bfin_read_TPERIOD();
91 offset =
92 (clocks_per_jiffy -
93 bfin_read_TCOUNT()) / (((clocks_per_jiffy + 1) * HZ) /
94 USEC_PER_SEC);
95
96
97 if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
98 && (offset < (100000 / HZ / 2)))
99 offset += (USEC_PER_SEC / HZ);
100
101 return offset;
102}
103
104static inline int set_rtc_mmss(unsigned long nowtime)
105{
106 return 0;
107}
108
109
110
111
112
113#ifdef CONFIG_CORE_TIMER_IRQ_L1
114irqreturn_t timer_interrupt(int irq, void *dummy)__attribute__((l1_text));
115#endif
116
117irqreturn_t timer_interrupt(int irq, void *dummy)
118{
119
120 static long last_rtc_update;
121
122 write_seqlock(&xtime_lock);
123
124 do_timer(1);
125
126 profile_tick(CPU_PROFILING);
127
128
129
130
131
132
133
134 if (ntp_synced() &&
135 xtime.tv_sec > last_rtc_update + 660 &&
136 (xtime.tv_nsec / NSEC_PER_USEC) >=
137 500000 - ((unsigned)TICK_SIZE) / 2
138 && (xtime.tv_nsec / NSEC_PER_USEC) <=
139 500000 + ((unsigned)TICK_SIZE) / 2) {
140 if (set_rtc_mmss(xtime.tv_sec) == 0)
141 last_rtc_update = xtime.tv_sec;
142 else
143
144 last_rtc_update = xtime.tv_sec - 600;
145 }
146 write_sequnlock(&xtime_lock);
147
148#ifndef CONFIG_SMP
149 update_process_times(user_mode(get_irq_regs()));
150#endif
151
152 return IRQ_HANDLED;
153}
154
155void __init time_init(void)
156{
157 time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60;
158
159#ifdef CONFIG_RTC_DRV_BFIN
160
161
162
163
164 if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
165 printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
166 bfin_write_RTC_STAT(0);
167 }
168#endif
169
170
171 xtime.tv_sec = secs_since_1970;
172 xtime.tv_nsec = 0;
173
174 wall_to_monotonic.tv_sec = -xtime.tv_sec;
175
176 time_sched_init(timer_interrupt);
177}
178
179#ifndef CONFIG_GENERIC_TIME
180void do_gettimeofday(struct timeval *tv)
181{
182 unsigned long flags;
183 unsigned long seq;
184 unsigned long usec, sec;
185
186 do {
187 seq = read_seqbegin_irqsave(&xtime_lock, flags);
188 usec = gettimeoffset();
189 sec = xtime.tv_sec;
190 usec += (xtime.tv_nsec / NSEC_PER_USEC);
191 }
192 while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
193
194 while (usec >= USEC_PER_SEC) {
195 usec -= USEC_PER_SEC;
196 sec++;
197 }
198
199 tv->tv_sec = sec;
200 tv->tv_usec = usec;
201}
202EXPORT_SYMBOL(do_gettimeofday);
203
204int do_settimeofday(struct timespec *tv)
205{
206 time_t wtm_sec, sec = tv->tv_sec;
207 long wtm_nsec, nsec = tv->tv_nsec;
208
209 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
210 return -EINVAL;
211
212 write_seqlock_irq(&xtime_lock);
213
214
215
216
217
218
219
220 nsec -= (gettimeoffset() * NSEC_PER_USEC);
221
222 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
223 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
224
225 set_normalized_timespec(&xtime, sec, nsec);
226 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
227
228 ntp_clear();
229
230 write_sequnlock_irq(&xtime_lock);
231 clock_was_set();
232
233 return 0;
234}
235EXPORT_SYMBOL(do_settimeofday);
236#endif
237
238
239
240
241unsigned long long sched_clock(void)
242{
243 return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
244}
245