* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / mips / dec / time.c
blob9f009680d648fcd3f42a87dd26db5724b664f84e
2 /*
3 * linux/arch/mips/kernel/time.c
5 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
7 * This file contains the time handling details for PC-style clocks as
8 * found in some MIPS systems.
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/param.h>
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <linux/interrupt.h>
20 #include <asm/bootinfo.h>
21 #include <asm/mipsregs.h>
22 #include <asm/io.h>
23 #include <asm/irq.h>
25 #include <linux/mc146818rtc.h>
26 #include <linux/timex.h>
28 extern volatile unsigned long lost_ticks;
31 * Change this if you have some constant time drift
33 /* This is the value for the PC-style PICs. */
34 /* #define USECS_PER_JIFFY (1000020/HZ) */
36 /* This is for machines which generate the exact clock. */
37 #define USECS_PER_JIFFY (1000000/HZ)
39 /* Cycle counter value at the previous timer interrupt.. */
41 static unsigned int timerhi = 0, timerlo = 0;
44 * On MIPS only R4000 and better have a cycle counter.
46 * FIXME: Does playing with the RP bit in c0_status interfere with this code?
48 static unsigned long do_fast_gettimeoffset(void)
50 u32 count;
51 unsigned long res, tmp;
53 /* Last jiffy when do_fast_gettimeoffset() was called. */
54 static unsigned long last_jiffies = 0;
55 unsigned long quotient;
58 * Cached "1/(clocks per usec)*2^32" value.
59 * It has to be recalculated once each jiffy.
61 static unsigned long cached_quotient = 0;
63 tmp = jiffies;
65 quotient = cached_quotient;
67 if (last_jiffies != tmp) {
68 last_jiffies = tmp;
69 __asm__(".set\tnoreorder\n\t"
70 ".set\tnoat\n\t"
71 ".set\tmips3\n\t"
72 "lwu\t%0,%2\n\t"
73 "dsll32\t$1,%1,0\n\t"
74 "or\t$1,$1,%0\n\t"
75 "ddivu\t$0,$1,%3\n\t"
76 "mflo\t$1\n\t"
77 "dsll32\t%0,%4,0\n\t"
78 "nop\n\t"
79 "ddivu\t$0,%0,$1\n\t"
80 "mflo\t%0\n\t"
81 ".set\tmips0\n\t"
82 ".set\tat\n\t"
83 ".set\treorder"
84 : "=&r"(quotient)
85 : "r"(timerhi),
86 "m"(timerlo),
87 "r"(tmp),
88 "r"(USECS_PER_JIFFY)
89 : "$1");
90 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)
152 * This is a kludge until I find a way for the
153 * DECstations without bus cycle counter. HK
155 return 0;
158 static unsigned long (*do_gettimeoffset) (void) = do_slow_gettimeoffset;
161 * This version of gettimeofday has near microsecond resolution.
163 void do_gettimeofday(struct timeval *tv)
165 unsigned long flags;
167 save_and_cli(flags);
168 *tv = xtime;
169 tv->tv_usec += do_gettimeoffset();
172 * xtime is atomically updated in timer_bh. lost_ticks is
173 * nonzero if the timer bottom half hasnt executed yet.
175 if (lost_ticks)
176 tv->tv_usec += USECS_PER_JIFFY;
178 restore_flags(flags);
180 if (tv->tv_usec >= 1000000) {
181 tv->tv_usec -= 1000000;
182 tv->tv_sec++;
186 void do_settimeofday(struct timeval *tv)
188 cli();
189 /* This is revolting. We need to set the xtime.tv_usec
190 * correctly. However, the value in this location is
191 * is value at the last tick.
192 * Discover what correction gettimeofday
193 * would have done, and then undo it!
195 tv->tv_usec -= do_gettimeoffset();
197 if (tv->tv_usec < 0) {
198 tv->tv_usec += 1000000;
199 tv->tv_sec--;
201 xtime = *tv;
202 time_state = TIME_BAD;
203 time_maxerror = MAXPHASE;
204 time_esterror = MAXPHASE;
205 sti();
209 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
210 * called 500 ms after the second nowtime has started, because when
211 * nowtime is written into the registers of the CMOS clock, it will
212 * jump to the next second precisely 500 ms later. Check the Motorola
213 * MC146818A or Dallas DS12887 data sheet for details.
215 static int set_rtc_mmss(unsigned long nowtime)
217 int retval = 0;
218 int real_seconds, real_minutes, cmos_minutes;
219 unsigned char save_control, save_freq_select;
221 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
222 CMOS_WRITE((save_control | RTC_SET), RTC_CONTROL);
224 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
225 CMOS_WRITE((save_freq_select | RTC_DIV_RESET2), RTC_FREQ_SELECT);
227 cmos_minutes = CMOS_READ(RTC_MINUTES);
228 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
229 BCD_TO_BIN(cmos_minutes);
232 * since we're only adjusting minutes and seconds,
233 * don't interfere with hour overflow. This avoids
234 * messing with unknown time zones but requires your
235 * RTC not to be off by more than 15 minutes
237 real_seconds = nowtime % 60;
238 real_minutes = nowtime / 60;
239 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
240 real_minutes += 30; /* correct for half hour time zone */
241 real_minutes %= 60;
243 if (abs(real_minutes - cmos_minutes) < 30) {
244 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
245 BIN_TO_BCD(real_seconds);
246 BIN_TO_BCD(real_minutes);
248 CMOS_WRITE(real_seconds, RTC_SECONDS);
249 CMOS_WRITE(real_minutes, RTC_MINUTES);
250 } else
251 retval = -1;
253 /* The following flags have to be released exactly in this order,
254 * otherwise the DS12887 (popular MC146818A clone with integrated
255 * battery and quartz) will not reset the oscillator and will not
256 * update precisely 500 ms later. You won't find this mentioned in
257 * the Dallas Semiconductor data sheets, but who believes data
258 * sheets anyway ... -- Markus Kuhn
260 CMOS_WRITE(save_control, RTC_CONTROL);
261 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
263 return retval;
266 /* last time the cmos clock got updated */
267 static long last_rtc_update = 0;
270 * timer_interrupt() needs to keep up the real-time clock,
271 * as well as call the "do_timer()" routine every clocktick
273 static void inline
274 timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
276 volatile unsigned char dummy;
278 dummy = CMOS_READ(RTC_REG_C); /* ACK RTC Interrupt */
279 do_timer(regs);
282 * If we have an externally synchronized Linux clock, then update
283 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
284 * called as close as possible to 500 ms before the new second starts.
286 if (time_state != TIME_BAD && xtime.tv_sec > last_rtc_update + 660 &&
287 xtime.tv_usec > 500000 - (tick >> 1) &&
288 xtime.tv_usec < 500000 + (tick >> 1))
289 if (set_rtc_mmss(xtime.tv_sec) == 0)
290 last_rtc_update = xtime.tv_sec;
291 else
292 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
295 static void r4k_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
297 unsigned int count;
300 * The cycle counter is only 32 bit which is good for about
301 * a minute at current count rates of upto 150MHz or so.
303 count = read_32bit_cp0_register(CP0_COUNT);
304 timerhi += (count < timerlo); /* Wrap around */
305 timerlo = count;
307 timer_interrupt(irq, dev_id, regs);
310 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
311 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
312 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
314 * [For the Julian calendar (which was used in Russia before 1917,
315 * Britain & colonies before 1752, anywhere else before 1582,
316 * and is still in use by some communities) leave out the
317 * -year/100+year/400 terms, and add 10.]
319 * This algorithm was first published by Gauss (I think).
321 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
322 * machines were long is 32-bit! (However, as time_t is signed, we
323 * will already get problems at other places on 2038-01-19 03:14:08)
325 static inline unsigned long mktime(unsigned int year, unsigned int mon,
326 unsigned int day, unsigned int hour,
327 unsigned int min, unsigned int sec)
329 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
330 mon += 12; /* Puts Feb last since it has leap day */
331 year -= 1;
333 return (((
334 (unsigned long) (year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) +
335 year * 365 - 719499
336 ) * 24 + hour /* now have hours */
337 ) * 60 + min /* now have minutes */
338 ) * 60 + sec; /* finally seconds */
341 char cyclecounter_available;
343 static inline void init_cycle_counter(void)
345 switch (mips_cputype) {
346 case CPU_UNKNOWN:
347 case CPU_R2000:
348 case CPU_R3000:
349 case CPU_R3000A:
350 case CPU_R3041:
351 case CPU_R3051:
352 case CPU_R3052:
353 case CPU_R3081:
354 case CPU_R3081E:
355 case CPU_R6000:
356 case CPU_R6000A:
357 case CPU_R8000: /* Not shure about that one, play safe */
358 cyclecounter_available = 0;
359 break;
360 case CPU_R4000PC:
361 case CPU_R4000SC:
362 case CPU_R4000MC:
363 case CPU_R4200:
364 case CPU_R4400PC:
365 case CPU_R4400SC:
366 case CPU_R4400MC:
367 case CPU_R4600:
368 case CPU_R10000:
369 case CPU_R4300:
370 case CPU_R4650:
371 case CPU_R4700:
372 case CPU_R5000:
373 case CPU_R5000A:
374 case CPU_R4640:
375 case CPU_NEVADA:
376 cyclecounter_available = 1;
377 break;
381 struct irqaction irq0 =
382 {timer_interrupt, SA_INTERRUPT, 0,
383 "timer", NULL, NULL};
386 void (*board_time_init) (struct irqaction * irq);
388 void __init time_init(void)
390 unsigned int year, mon, day, hour, min, sec;
391 int i;
393 /* The Linux interpretation of the CMOS clock register contents:
394 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
395 * RTC registers show the second which has precisely just started.
396 * Let's hope other operating systems interpret the RTC the same way.
398 /* read RTC exactly on falling edge of update flag */
399 for (i = 0; i < 1000000; i++) /* may take up to 1 second... */
400 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
401 break;
402 for (i = 0; i < 1000000; i++) /* must try at least 2.228 ms */
403 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
404 break;
405 do { /* Isn't this overkill ? UIP above should guarantee consistency */
406 sec = CMOS_READ(RTC_SECONDS);
407 min = CMOS_READ(RTC_MINUTES);
408 hour = CMOS_READ(RTC_HOURS);
409 day = CMOS_READ(RTC_DAY_OF_MONTH);
410 mon = CMOS_READ(RTC_MONTH);
411 year = CMOS_READ(RTC_YEAR);
412 } while (sec != CMOS_READ(RTC_SECONDS));
413 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
414 BCD_TO_BIN(sec);
415 BCD_TO_BIN(min);
416 BCD_TO_BIN(hour);
417 BCD_TO_BIN(day);
418 BCD_TO_BIN(mon);
419 BCD_TO_BIN(year);
422 * The DECstation RTC is used as a TOY (Time Of Year).
423 * The PROM will reset the year to either '70, '71 or '72.
424 * This hack will only work until Dec 31 2001.
426 year += 1927;
428 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
429 xtime.tv_usec = 0;
431 init_cycle_counter();
433 if (cyclecounter_available) {
434 write_32bit_cp0_register(CP0_COUNT, 0);
435 do_gettimeoffset = do_fast_gettimeoffset;
436 irq0.handler = r4k_timer_interrupt;
438 board_time_init(&irq0);