Pull button into test branch
[pandora-kernel.git] / arch / mips / kernel / time.c
1 /*
2  * Copyright 2001 MontaVista Software Inc.
3  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
4  * Copyright (c) 2003, 2004  Maciej W. Rozycki
5  *
6  * Common time service routines for MIPS machines. See
7  * Documentation/mips/time.README.
8  *
9  * This program is free software; you can redistribute  it and/or modify it
10  * under  the terms of  the GNU General  Public License as published by the
11  * Free Software Foundation;  either version 2 of the  License, or (at your
12  * option) any later version.
13  */
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/sched.h>
18 #include <linux/param.h>
19 #include <linux/time.h>
20 #include <linux/timex.h>
21 #include <linux/smp.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26
27 #include <asm/bootinfo.h>
28 #include <asm/cache.h>
29 #include <asm/compiler.h>
30 #include <asm/cpu.h>
31 #include <asm/cpu-features.h>
32 #include <asm/div64.h>
33 #include <asm/sections.h>
34 #include <asm/time.h>
35
36 /*
37  * The integer part of the number of usecs per jiffy is taken from tick,
38  * but the fractional part is not recorded, so we calculate it using the
39  * initial value of HZ.  This aids systems where tick isn't really an
40  * integer (e.g. for HZ = 128).
41  */
42 #define USECS_PER_JIFFY         TICK_SIZE
43 #define USECS_PER_JIFFY_FRAC    ((unsigned long)(u32)((1000000ULL << 32) / HZ))
44
45 #define TICK_SIZE       (tick_nsec / 1000)
46
47 /*
48  * forward reference
49  */
50 DEFINE_SPINLOCK(rtc_lock);
51
52 /*
53  * By default we provide the null RTC ops
54  */
55 static unsigned long null_rtc_get_time(void)
56 {
57         return mktime(2000, 1, 1, 0, 0, 0);
58 }
59
60 static int null_rtc_set_time(unsigned long sec)
61 {
62         return 0;
63 }
64
65 unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time;
66 int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time;
67 int (*rtc_mips_set_mmss)(unsigned long);
68
69
70 /* how many counter cycles in a jiffy */
71 static unsigned long cycles_per_jiffy __read_mostly;
72
73 /* expirelo is the count value for next CPU timer interrupt */
74 static unsigned int expirelo;
75
76
77 /*
78  * Null timer ack for systems not needing one (e.g. i8254).
79  */
80 static void null_timer_ack(void) { /* nothing */ }
81
82 /*
83  * Null high precision timer functions for systems lacking one.
84  */
85 static cycle_t null_hpt_read(void)
86 {
87         return 0;
88 }
89
90 /*
91  * Timer ack for an R4k-compatible timer of a known frequency.
92  */
93 static void c0_timer_ack(void)
94 {
95         unsigned int count;
96
97 #ifndef CONFIG_SOC_PNX8550      /* pnx8550 resets to zero */
98         /* Ack this timer interrupt and set the next one.  */
99         expirelo += cycles_per_jiffy;
100 #endif
101         write_c0_compare(expirelo);
102
103         /* Check to see if we have missed any timer interrupts.  */
104         while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
105                 /* missed_timer_count++; */
106                 expirelo = count + cycles_per_jiffy;
107                 write_c0_compare(expirelo);
108         }
109 }
110
111 /*
112  * High precision timer functions for a R4k-compatible timer.
113  */
114 static cycle_t c0_hpt_read(void)
115 {
116         return read_c0_count();
117 }
118
119 /* For use both as a high precision timer and an interrupt source.  */
120 static void __init c0_hpt_timer_init(void)
121 {
122         expirelo = read_c0_count() + cycles_per_jiffy;
123         write_c0_compare(expirelo);
124 }
125
126 int (*mips_timer_state)(void);
127 void (*mips_timer_ack)(void);
128
129 /* last time when xtime and rtc are sync'ed up */
130 static long last_rtc_update;
131
132 /*
133  * local_timer_interrupt() does profiling and process accounting
134  * on a per-CPU basis.
135  *
136  * In UP mode, it is invoked from the (global) timer_interrupt.
137  *
138  * In SMP mode, it might invoked by per-CPU timer interrupt, or
139  * a broadcasted inter-processor interrupt which itself is triggered
140  * by the global timer interrupt.
141  */
142 void local_timer_interrupt(int irq, void *dev_id)
143 {
144         profile_tick(CPU_PROFILING);
145         update_process_times(user_mode(get_irq_regs()));
146 }
147
148 /*
149  * High-level timer interrupt service routines.  This function
150  * is set as irqaction->handler and is invoked through do_IRQ.
151  */
152 irqreturn_t timer_interrupt(int irq, void *dev_id)
153 {
154         write_seqlock(&xtime_lock);
155
156         mips_timer_ack();
157
158         /*
159          * call the generic timer interrupt handling
160          */
161         do_timer(1);
162
163         /*
164          * If we have an externally synchronized Linux clock, then update
165          * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
166          * called as close as possible to 500 ms before the new second starts.
167          */
168         if (ntp_synced() &&
169             xtime.tv_sec > last_rtc_update + 660 &&
170             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
171             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
172                 if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
173                         last_rtc_update = xtime.tv_sec;
174                 } else {
175                         /* do it again in 60 s */
176                         last_rtc_update = xtime.tv_sec - 600;
177                 }
178         }
179
180         write_sequnlock(&xtime_lock);
181
182         /*
183          * In UP mode, we call local_timer_interrupt() to do profiling
184          * and process accouting.
185          *
186          * In SMP mode, local_timer_interrupt() is invoked by appropriate
187          * low-level local timer interrupt handler.
188          */
189         local_timer_interrupt(irq, dev_id);
190
191         return IRQ_HANDLED;
192 }
193
194 int null_perf_irq(void)
195 {
196         return 0;
197 }
198
199 int (*perf_irq)(void) = null_perf_irq;
200
201 EXPORT_SYMBOL(null_perf_irq);
202 EXPORT_SYMBOL(perf_irq);
203
204 asmlinkage void ll_timer_interrupt(int irq)
205 {
206         int r2 = cpu_has_mips_r2;
207
208         irq_enter();
209         kstat_this_cpu.irqs[irq]++;
210
211         /*
212          * Suckage alert:
213          * Before R2 of the architecture there was no way to see if a
214          * performance counter interrupt was pending, so we have to run the
215          * performance counter interrupt handler anyway.
216          */
217         if (!r2 || (read_c0_cause() & (1 << 26)))
218                 if (perf_irq())
219                         goto out;
220
221         /* we keep interrupt disabled all the time */
222         if (!r2 || (read_c0_cause() & (1 << 30)))
223                 timer_interrupt(irq, NULL);
224
225 out:
226         irq_exit();
227 }
228
229 asmlinkage void ll_local_timer_interrupt(int irq)
230 {
231         irq_enter();
232         if (smp_processor_id() != 0)
233                 kstat_this_cpu.irqs[irq]++;
234
235         /* we keep interrupt disabled all the time */
236         local_timer_interrupt(irq, NULL);
237
238         irq_exit();
239 }
240
241 /*
242  * time_init() - it does the following things.
243  *
244  * 1) board_time_init() -
245  *      a) (optional) set up RTC routines,
246  *      b) (optional) calibrate and set the mips_hpt_frequency
247  *          (only needed if you intended to use cpu counter as timer interrupt
248  *           source)
249  * 2) setup xtime based on rtc_mips_get_time().
250  * 3) calculate a couple of cached variables for later usage
251  * 4) plat_timer_setup() -
252  *      a) (optional) over-write any choices made above by time_init().
253  *      b) machine specific code should setup the timer irqaction.
254  *      c) enable the timer interrupt
255  */
256
257 void (*board_time_init)(void);
258
259 unsigned int mips_hpt_frequency;
260
261 static struct irqaction timer_irqaction = {
262         .handler = timer_interrupt,
263         .flags = IRQF_DISABLED,
264         .name = "timer",
265 };
266
267 static unsigned int __init calibrate_hpt(void)
268 {
269         cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
270
271         const int loops = HZ / 10;
272         int log_2_loops = 0;
273         int i;
274
275         /*
276          * We want to calibrate for 0.1s, but to avoid a 64-bit
277          * division we round the number of loops up to the nearest
278          * power of 2.
279          */
280         while (loops > 1 << log_2_loops)
281                 log_2_loops++;
282         i = 1 << log_2_loops;
283
284         /*
285          * Wait for a rising edge of the timer interrupt.
286          */
287         while (mips_timer_state());
288         while (!mips_timer_state());
289
290         /*
291          * Now see how many high precision timer ticks happen
292          * during the calculated number of periods between timer
293          * interrupts.
294          */
295         hpt_start = clocksource_mips.read();
296         do {
297                 while (mips_timer_state());
298                 while (!mips_timer_state());
299         } while (--i);
300         hpt_end = clocksource_mips.read();
301
302         hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
303         hz = HZ;
304         frequency = hpt_count * hz;
305
306         return frequency >> log_2_loops;
307 }
308
309 struct clocksource clocksource_mips = {
310         .name           = "MIPS",
311         .mask           = 0xffffffff,
312         .is_continuous  = 1,
313 };
314
315 static void __init init_mips_clocksource(void)
316 {
317         u64 temp;
318         u32 shift;
319
320         if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
321                 return;
322
323         /* Calclate a somewhat reasonable rating value */
324         clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
325         /* Find a shift value */
326         for (shift = 32; shift > 0; shift--) {
327                 temp = (u64) NSEC_PER_SEC << shift;
328                 do_div(temp, mips_hpt_frequency);
329                 if ((temp >> 32) == 0)
330                         break;
331         }
332         clocksource_mips.shift = shift;
333         clocksource_mips.mult = (u32)temp;
334
335         clocksource_register(&clocksource_mips);
336 }
337
338 void __init time_init(void)
339 {
340         if (board_time_init)
341                 board_time_init();
342
343         if (!rtc_mips_set_mmss)
344                 rtc_mips_set_mmss = rtc_mips_set_time;
345
346         xtime.tv_sec = rtc_mips_get_time();
347         xtime.tv_nsec = 0;
348
349         set_normalized_timespec(&wall_to_monotonic,
350                                 -xtime.tv_sec, -xtime.tv_nsec);
351
352         /* Choose appropriate high precision timer routines.  */
353         if (!cpu_has_counter && !clocksource_mips.read)
354                 /* No high precision timer -- sorry.  */
355                 clocksource_mips.read = null_hpt_read;
356         else if (!mips_hpt_frequency && !mips_timer_state) {
357                 /* A high precision timer of unknown frequency.  */
358                 if (!clocksource_mips.read)
359                         /* No external high precision timer -- use R4k.  */
360                         clocksource_mips.read = c0_hpt_read;
361         } else {
362                 /* We know counter frequency.  Or we can get it.  */
363                 if (!clocksource_mips.read) {
364                         /* No external high precision timer -- use R4k.  */
365                         clocksource_mips.read = c0_hpt_read;
366
367                         if (!mips_timer_state) {
368                                 /* No external timer interrupt -- use R4k.  */
369                                 mips_timer_ack = c0_timer_ack;
370                                 /* Calculate cache parameters.  */
371                                 cycles_per_jiffy =
372                                         (mips_hpt_frequency + HZ / 2) / HZ;
373                                 /*
374                                  * This sets up the high precision
375                                  * timer for the first interrupt.
376                                  */
377                                 c0_hpt_timer_init();
378                         }
379                 }
380                 if (!mips_hpt_frequency)
381                         mips_hpt_frequency = calibrate_hpt();
382
383                 /* Report the high precision timer rate for a reference.  */
384                 printk("Using %u.%03u MHz high precision timer.\n",
385                        ((mips_hpt_frequency + 500) / 1000) / 1000,
386                        ((mips_hpt_frequency + 500) / 1000) % 1000);
387         }
388
389         if (!mips_timer_ack)
390                 /* No timer interrupt ack (e.g. i8254).  */
391                 mips_timer_ack = null_timer_ack;
392
393         /*
394          * Call board specific timer interrupt setup.
395          *
396          * this pointer must be setup in machine setup routine.
397          *
398          * Even if a machine chooses to use a low-level timer interrupt,
399          * it still needs to setup the timer_irqaction.
400          * In that case, it might be better to set timer_irqaction.handler
401          * to be NULL function so that we are sure the high-level code
402          * is not invoked accidentally.
403          */
404         plat_timer_setup(&timer_irqaction);
405
406         init_mips_clocksource();
407 }
408
409 #define FEBRUARY                2
410 #define STARTOFTIME             1970
411 #define SECDAY                  86400L
412 #define SECYR                   (SECDAY * 365)
413 #define leapyear(y)             ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
414 #define days_in_year(y)         (leapyear(y) ? 366 : 365)
415 #define days_in_month(m)        (month_days[(m) - 1])
416
417 static int month_days[12] = {
418         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
419 };
420
421 void to_tm(unsigned long tim, struct rtc_time *tm)
422 {
423         long hms, day, gday;
424         int i;
425
426         gday = day = tim / SECDAY;
427         hms = tim % SECDAY;
428
429         /* Hours, minutes, seconds are easy */
430         tm->tm_hour = hms / 3600;
431         tm->tm_min = (hms % 3600) / 60;
432         tm->tm_sec = (hms % 3600) % 60;
433
434         /* Number of years in days */
435         for (i = STARTOFTIME; day >= days_in_year(i); i++)
436                 day -= days_in_year(i);
437         tm->tm_year = i;
438
439         /* Number of months in days left */
440         if (leapyear(tm->tm_year))
441                 days_in_month(FEBRUARY) = 29;
442         for (i = 1; day >= days_in_month(i); i++)
443                 day -= days_in_month(i);
444         days_in_month(FEBRUARY) = 28;
445         tm->tm_mon = i - 1;             /* tm_mon starts from 0 to 11 */
446
447         /* Days are what is left over (+1) from all that. */
448         tm->tm_mday = day + 1;
449
450         /*
451          * Determine the day of week
452          */
453         tm->tm_wday = (gday + 4) % 7;   /* 1970/1/1 was Thursday */
454 }
455
456 EXPORT_SYMBOL(rtc_lock);
457 EXPORT_SYMBOL(to_tm);
458 EXPORT_SYMBOL(rtc_mips_set_time);
459 EXPORT_SYMBOL(rtc_mips_get_time);
460
461 unsigned long long sched_clock(void)
462 {
463         return (unsigned long long)jiffies*(1000000000/HZ);
464 }