* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / mips / kernel / time.c
blob9d0118aeab4d49b929c918f44e521d22495c4939
1 /* $Id: time.c,v 1.12 1999/06/13 16:30:34 ralf Exp $
3 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
4 * Copyright (C) 1996, 1997, 1998 Ralf Baechle
6 * This file contains the time handling details for PC-style clocks as
7 * found in some MIPS systems.
8 */
9 #include <linux/config.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/param.h>
15 #include <linux/string.h>
16 #include <linux/mm.h>
17 #include <linux/interrupt.h>
19 #include <asm/bootinfo.h>
20 #include <asm/mipsregs.h>
21 #include <asm/io.h>
22 #include <asm/irq.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/timex.h>
27 extern volatile unsigned long lost_ticks;
30 * Change this if you have some constant time drift
32 /* This is the value for the PC-style PICs. */
33 /* #define USECS_PER_JIFFY (1000020/HZ) */
35 /* This is for machines which generate the exact clock. */
36 #define USECS_PER_JIFFY (1000000/HZ)
38 /* Cycle counter value at the previous timer interrupt.. */
40 static unsigned int timerhi = 0, timerlo = 0;
43 * On MIPS only R4000 and better have a cycle counter.
45 * FIXME: Does playing with the RP bit in c0_status interfere with this code?
47 static unsigned long do_fast_gettimeoffset(void)
49 u32 count;
50 unsigned long res, tmp;
52 /* Last jiffy when do_fast_gettimeoffset() was called. */
53 static unsigned long last_jiffies=0;
54 unsigned long quotient;
57 * Cached "1/(clocks per usec)*2^32" value.
58 * It has to be recalculated once each jiffy.
60 static unsigned long cached_quotient=0;
62 tmp = jiffies;
64 quotient = cached_quotient;
66 if (tmp && last_jiffies != tmp) {
67 last_jiffies = tmp;
68 __asm__(".set\tnoreorder\n\t"
69 ".set\tnoat\n\t"
70 ".set\tmips3\n\t"
71 "lwu\t%0,%2\n\t"
72 "dsll32\t$1,%1,0\n\t"
73 "or\t$1,$1,%0\n\t"
74 "ddivu\t$0,$1,%3\n\t"
75 "mflo\t$1\n\t"
76 "dsll32\t%0,%4,0\n\t"
77 "nop\n\t"
78 "ddivu\t$0,%0,$1\n\t"
79 "mflo\t%0\n\t"
80 ".set\tmips0\n\t"
81 ".set\tat\n\t"
82 ".set\treorder"
83 :"=&r" (quotient)
84 :"r" (timerhi),
85 "m" (timerlo),
86 "r" (tmp),
87 "r" (USECS_PER_JIFFY)
88 :"$1");
89 cached_quotient = quotient;
92 /* Get last timer tick in absolute kernel time */
93 count = read_32bit_cp0_register(CP0_COUNT);
95 /* .. relative to previous jiffy (32 bits is enough) */
96 count -= timerlo;
97 //printk("count: %08lx, %08lx:%08lx\n", count, timerhi, timerlo);
99 __asm__("multu\t%1,%2\n\t"
100 "mfhi\t%0"
101 :"=r" (res)
102 :"r" (count),
103 "r" (quotient));
106 * Due to possible jiffies inconsistencies, we need to check
107 * the result so that we'll get a timer that is monotonic.
109 if (res >= USECS_PER_JIFFY)
110 res = USECS_PER_JIFFY-1;
112 return res;
115 /* This function must be called with interrupts disabled
116 * It was inspired by Steve McCanne's microtime-i386 for BSD. -- jrs
118 * However, the pc-audio speaker driver changes the divisor so that
119 * it gets interrupted rather more often - it loads 64 into the
120 * counter rather than 11932! This has an adverse impact on
121 * do_gettimeoffset() -- it stops working! What is also not
122 * good is that the interval that our timer function gets called
123 * is no longer 10.0002 ms, but 9.9767 ms. To get around this
124 * would require using a different timing source. Maybe someone
125 * could use the RTC - I know that this can interrupt at frequencies
126 * ranging from 8192Hz to 2Hz. If I had the energy, I'd somehow fix
127 * it so that at startup, the timer code in sched.c would select
128 * using either the RTC or the 8253 timer. The decision would be
129 * based on whether there was any other device around that needed
130 * to trample on the 8253. I'd set up the RTC to interrupt at 1024 Hz,
131 * and then do some jiggery to have a version of do_timer that
132 * advanced the clock by 1/1024 s. Every time that reached over 1/100
133 * of a second, then do all the old code. If the time was kept correct
134 * then do_gettimeoffset could just return 0 - there is no low order
135 * divider that can be accessed.
137 * Ideally, you would be able to use the RTC for the speaker driver,
138 * but it appears that the speaker driver really needs interrupt more
139 * often than every 120 us or so.
141 * Anyway, this needs more thought.... pjsg (1993-08-28)
143 * If you are really that interested, you should be reading
144 * comp.protocols.time.ntp!
147 #define TICK_SIZE tick
149 static unsigned long do_slow_gettimeoffset(void)
151 int count;
153 static int count_p = LATCH; /* for the first call after boot */
154 static unsigned long jiffies_p = 0;
157 * cache volatile jiffies temporarily; we have IRQs turned off.
159 unsigned long jiffies_t;
161 /* timer count may underflow right here */
162 outb_p(0x00, 0x43); /* latch the count ASAP */
164 count = inb_p(0x40); /* read the latched count */
167 * We do this guaranteed double memory access instead of a _p
168 * postfix in the previous port access. Wheee, hackady hack
170 jiffies_t = jiffies;
172 count |= inb_p(0x40) << 8;
175 * avoiding timer inconsistencies (they are rare, but they happen)...
176 * there are two kinds of problems that must be avoided here:
177 * 1. the timer counter underflows
178 * 2. hardware problem with the timer, not giving us continuous time,
179 * the counter does small "jumps" upwards on some Pentium systems,
180 * (see c't 95/10 page 335 for Neptun bug.)
183 if( jiffies_t == jiffies_p ) {
184 if( count > count_p ) {
185 /* the nutcase */
187 outb_p(0x0A, 0x20);
189 /* assumption about timer being IRQ1 */
190 if (inb(0x20) & 0x01) {
192 * We cannot detect lost timer interrupts ...
193 * well, that's why we call them lost, don't we? :)
194 * [hmm, on the Pentium and Alpha we can ... sort of]
196 count -= LATCH;
197 } else {
198 printk("do_slow_gettimeoffset(): hardware timer problem?\n");
201 } else
202 jiffies_p = jiffies_t;
204 count_p = count;
206 count = ((LATCH-1) - count) * TICK_SIZE;
207 count = (count + LATCH/2) / LATCH;
209 return count;
212 static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
215 * This version of gettimeofday has near microsecond resolution.
217 void do_gettimeofday(struct timeval *tv)
219 unsigned long flags;
221 save_and_cli(flags);
222 *tv = xtime;
223 tv->tv_usec += do_gettimeoffset();
226 * xtime is atomically updated in timer_bh. lost_ticks is
227 * nonzero if the timer bottom half hasnt executed yet.
229 if (lost_ticks)
230 tv->tv_usec += USECS_PER_JIFFY;
232 restore_flags(flags);
234 if (tv->tv_usec >= 1000000) {
235 tv->tv_usec -= 1000000;
236 tv->tv_sec++;
240 void do_settimeofday(struct timeval *tv)
242 cli();
243 /* This is revolting. We need to set the xtime.tv_usec
244 * correctly. However, the value in this location is
245 * is value at the last tick.
246 * Discover what correction gettimeofday
247 * would have done, and then undo it!
249 tv->tv_usec -= do_gettimeoffset();
251 if (tv->tv_usec < 0) {
252 tv->tv_usec += 1000000;
253 tv->tv_sec--;
256 xtime = *tv;
257 time_adjust = 0; /* stop active adjtime() */
258 time_status |= STA_UNSYNC;
259 time_maxerror = NTP_PHASE_LIMIT;
260 time_esterror = NTP_PHASE_LIMIT;
261 sti();
265 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
266 * called 500 ms after the second nowtime has started, because when
267 * nowtime is written into the registers of the CMOS clock, it will
268 * jump to the next second precisely 500 ms later. Check the Motorola
269 * MC146818A or Dallas DS12887 data sheet for details.
271 * BUG: This routine does not handle hour overflow properly; it just
272 * sets the minutes. Usually you won't notice until after reboot!
274 static int set_rtc_mmss(unsigned long nowtime)
276 int retval = 0;
277 int real_seconds, real_minutes, cmos_minutes;
278 unsigned char save_control, save_freq_select;
280 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
281 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
283 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
284 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
286 cmos_minutes = CMOS_READ(RTC_MINUTES);
287 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
288 BCD_TO_BIN(cmos_minutes);
291 * since we're only adjusting minutes and seconds,
292 * don't interfere with hour overflow. This avoids
293 * messing with unknown time zones but requires your
294 * RTC not to be off by more than 15 minutes
296 real_seconds = nowtime % 60;
297 real_minutes = nowtime / 60;
298 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
299 real_minutes += 30; /* correct for half hour time zone */
300 real_minutes %= 60;
302 if (abs(real_minutes - cmos_minutes) < 30) {
303 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
304 BIN_TO_BCD(real_seconds);
305 BIN_TO_BCD(real_minutes);
307 CMOS_WRITE(real_seconds,RTC_SECONDS);
308 CMOS_WRITE(real_minutes,RTC_MINUTES);
309 } else {
310 printk(KERN_WARNING
311 "set_rtc_mmss: can't update from %d to %d\n",
312 cmos_minutes, real_minutes);
313 retval = -1;
316 /* The following flags have to be released exactly in this order,
317 * otherwise the DS12887 (popular MC146818A clone with integrated
318 * battery and quartz) will not reset the oscillator and will not
319 * update precisely 500 ms later. You won't find this mentioned in
320 * the Dallas Semiconductor data sheets, but who believes data
321 * sheets anyway ... -- Markus Kuhn
323 CMOS_WRITE(save_control, RTC_CONTROL);
324 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
326 return retval;
329 /* last time the cmos clock got updated */
330 static long last_rtc_update = 0;
333 * timer_interrupt() needs to keep up the real-time clock,
334 * as well as call the "do_timer()" routine every clocktick
336 static void inline
337 timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
339 #ifdef CONFIG_PROFILE
340 if(!user_mode(regs)) {
341 if (prof_buffer && current->pid) {
342 extern int _stext;
343 unsigned long pc = regs->cp0_epc;
345 pc -= (unsigned long) &_stext;
346 pc >>= prof_shift;
348 * Dont ignore out-of-bounds pc values silently,
349 * put them into the last histogram slot, so if
350 * present, they will show up as a sharp peak.
352 if (pc > prof_len-1)
353 pc = prof_len-1;
354 atomic_inc((atomic_t *)&prof_buffer[pc]);
357 #endif
358 do_timer(regs);
361 * If we have an externally synchronized Linux clock, then update
362 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
363 * called as close as possible to 500 ms before the new second starts.
365 if ((time_status & STA_UNSYNC) == 0 &&
366 xtime.tv_sec > last_rtc_update + 660 &&
367 xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
368 xtime.tv_usec <= 500000 + ((unsigned) tick) / 2)
369 if (set_rtc_mmss(xtime.tv_sec) == 0)
370 last_rtc_update = xtime.tv_sec;
371 else
372 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
373 /* As we return to user mode fire off the other CPU schedulers.. this is
374 basically because we don't yet share IRQ's around. This message is
375 rigged to be safe on the 386 - basically it's a hack, so don't look
376 closely for now.. */
377 /*smp_message_pass(MSG_ALL_BUT_SELF, MSG_RESCHEDULE, 0L, 0); */
380 static void r4k_timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
382 unsigned int count;
385 * The cycle counter is only 32 bit which is good for about
386 * a minute at current count rates of upto 150MHz or so.
388 count = read_32bit_cp0_register(CP0_COUNT);
389 timerhi += (count < timerlo); /* Wrap around */
390 timerlo = count;
392 timer_interrupt(irq, dev_id, regs);
394 if (!jiffies)
397 * If jiffies has overflowed in this timer_interrupt we must
398 * update the timer[hi]/[lo] to make do_fast_gettimeoffset()
399 * quotient calc still valid. -arca
401 timerhi = timerlo = 0;
405 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
406 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
407 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
409 * [For the Julian calendar (which was used in Russia before 1917,
410 * Britain & colonies before 1752, anywhere else before 1582,
411 * and is still in use by some communities) leave out the
412 * -year/100+year/400 terms, and add 10.]
414 * This algorithm was first published by Gauss (I think).
416 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
417 * machines were long is 32-bit! (However, as time_t is signed, we
418 * will already get problems at other places on 2038-01-19 03:14:08)
420 static inline unsigned long mktime(unsigned int year, unsigned int mon,
421 unsigned int day, unsigned int hour,
422 unsigned int min, unsigned int sec)
424 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
425 mon += 12; /* Puts Feb last since it has leap day */
426 year -= 1;
428 return (((
429 (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
430 year*365 - 719499
431 )*24 + hour /* now have hours */
432 )*60 + min /* now have minutes */
433 )*60 + sec; /* finally seconds */
436 char cyclecounter_available;
438 static inline void init_cycle_counter(void)
440 switch(mips_cputype) {
441 case CPU_UNKNOWN:
442 case CPU_R2000:
443 case CPU_R3000:
444 case CPU_R3000A:
445 case CPU_R3041:
446 case CPU_R3051:
447 case CPU_R3052:
448 case CPU_R3081:
449 case CPU_R3081E:
450 case CPU_R6000:
451 case CPU_R6000A:
452 case CPU_R8000: /* Not shure about that one, play safe */
453 cyclecounter_available = 0;
454 break;
455 case CPU_R4000PC:
456 case CPU_R4000SC:
457 case CPU_R4000MC:
458 case CPU_R4200:
459 case CPU_R4400PC:
460 case CPU_R4400SC:
461 case CPU_R4400MC:
462 case CPU_R4600:
463 case CPU_R10000:
464 case CPU_R4300:
465 case CPU_R4650:
466 case CPU_R4700:
467 case CPU_R5000:
468 case CPU_R5000A:
469 case CPU_R4640:
470 case CPU_NEVADA:
471 cyclecounter_available = 1;
472 break;
476 struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, 0,
477 "timer", NULL, NULL};
480 void (*board_time_init)(struct irqaction *irq);
482 void __init time_init(void)
484 unsigned int epoch, year, mon, day, hour, min, sec;
485 int i;
487 /* The Linux interpretation of the CMOS clock register contents:
488 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
489 * RTC registers show the second which has precisely just started.
490 * Let's hope other operating systems interpret the RTC the same way.
492 /* read RTC exactly on falling edge of update flag */
493 for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
494 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
495 break;
496 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
497 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
498 break;
499 do { /* Isn't this overkill ? UIP above should guarantee consistency */
500 sec = CMOS_READ(RTC_SECONDS);
501 min = CMOS_READ(RTC_MINUTES);
502 hour = CMOS_READ(RTC_HOURS);
503 day = CMOS_READ(RTC_DAY_OF_MONTH);
504 mon = CMOS_READ(RTC_MONTH);
505 year = CMOS_READ(RTC_YEAR);
506 } while (sec != CMOS_READ(RTC_SECONDS));
507 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
509 BCD_TO_BIN(sec);
510 BCD_TO_BIN(min);
511 BCD_TO_BIN(hour);
512 BCD_TO_BIN(day);
513 BCD_TO_BIN(mon);
514 BCD_TO_BIN(year);
517 /* Attempt to guess the epoch. This is the same heuristic as in rtc.c so
518 no stupid things will happen to timekeeping. Who knows, maybe Ultrix
519 also uses 1952 as epoch ... */
520 if (year > 10 && year < 44) {
521 epoch = 1980;
522 } else if (year < 96) {
523 epoch = 1952;
525 year += epoch;
527 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
528 xtime.tv_usec = 0;
530 init_cycle_counter();
532 if (cyclecounter_available) {
533 write_32bit_cp0_register(CP0_COUNT, 0);
534 do_gettimeoffset = do_fast_gettimeoffset;
535 irq0.handler = r4k_timer_interrupt;
538 board_time_init(&irq0);