2.2.0-final
[davej-history.git] / arch / alpha / kernel / time.c
blob2f93631131df2c2c9e50317ef95c08f26b199dad
1 /*
2 * linux/arch/alpha/kernel/time.c
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 * This file contains the PC-specific time handling details:
7 * reading the RTC at bootup, etc..
8 * 1994-07-02 Alan Modra
9 * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
10 * 1995-03-26 Markus Kuhn
11 * fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
12 * precision CMOS clock update
13 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
14 * "A Kernel Model for Precision Timekeeping" by Dave Mills
15 * 1997-01-09 Adrian Sun
16 * use interval timer if CONFIG_RTC=y
17 * 1997-10-29 John Bowman (bowman@math.ualberta.ca)
18 * fixed tick loss calculation in timer_interrupt
19 * (round system clock to nearest tick instead of truncating)
20 * fixed algorithm in time_init for getting time from CMOS clock
22 #include <linux/config.h>
23 #include <linux/errno.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/param.h>
27 #include <linux/string.h>
28 #include <linux/mm.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
32 #include <asm/uaccess.h>
33 #include <asm/io.h>
34 #include <asm/hwrpb.h>
36 #include <linux/mc146818rtc.h>
37 #include <linux/timex.h>
39 #include "proto.h"
40 #include "irq.h"
42 static int set_rtc_mmss(unsigned long);
46 * Shift amount by which scaled_ticks_per_cycle is scaled. Shifting
47 * by 48 gives us 16 bits for HZ while keeping the accuracy good even
48 * for large CPU clock rates.
50 #define FIX_SHIFT 48
52 /* lump static variables together for more efficient access: */
53 static struct {
54 /* cycle counter last time it got invoked */
55 __u32 last_time;
56 /* ticks/cycle * 2^48 */
57 unsigned long scaled_ticks_per_cycle;
58 /* last time the CMOS clock got updated */
59 time_t last_rtc_update;
60 /* partial unused tick */
61 unsigned long partial_tick;
62 } state;
64 unsigned long est_cycle_freq;
67 static inline __u32 rpcc(void)
69 __u32 result;
70 asm volatile ("rpcc %0" : "=r"(result));
71 return result;
76 * timer_interrupt() needs to keep up the real-time clock,
77 * as well as call the "do_timer()" routine every clocktick
79 void timer_interrupt(int irq, void *dev, struct pt_regs * regs)
81 unsigned long delta;
82 __u32 now;
83 long nticks;
85 #ifdef __SMP__
86 extern void smp_percpu_timer_interrupt(struct pt_regs *);
87 extern unsigned int boot_cpu_id;
88 /* when SMP, do this for *all* CPUs,
89 but only do the rest for the boot CPU */
90 smp_percpu_timer_interrupt(regs);
91 if (smp_processor_id() != boot_cpu_id)
92 return;
93 #endif
96 * Calculate how many ticks have passed since the last update,
97 * including any previous partial leftover. Save any resulting
98 * fraction for the next pass.
100 now = rpcc();
101 delta = now - state.last_time;
102 state.last_time = now;
103 delta = delta * state.scaled_ticks_per_cycle + state.partial_tick;
104 state.partial_tick = delta & ((1UL << FIX_SHIFT) - 1);
105 nticks = delta >> FIX_SHIFT;
107 while (nticks > 0) {
108 do_timer(regs);
109 nticks--;
113 * If we have an externally synchronized Linux clock, then update
114 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
115 * called as close as possible to 500 ms before the new second starts.
117 if ((time_status & STA_UNSYNC) == 0
118 && xtime.tv_sec > state.last_rtc_update + 660
119 && xtime.tv_usec >= 500000 - ((unsigned) tick) / 2
120 && xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
121 int tmp = set_rtc_mmss(xtime.tv_sec);
122 state.last_rtc_update = xtime.tv_sec - (tmp ? 600 : 0);
127 * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
128 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
129 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
131 * [For the Julian calendar (which was used in Russia before 1917,
132 * Britain & colonies before 1752, anywhere else before 1582,
133 * and is still in use by some communities) leave out the
134 * -year/100+year/400 terms, and add 10.]
136 * This algorithm was first published by Gauss (I think).
138 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
139 * machines were long is 32-bit! (However, as time_t is signed, we
140 * will already get problems at other places on 2038-01-19 03:14:08)
142 static inline unsigned long mktime(unsigned int year, unsigned int mon,
143 unsigned int day, unsigned int hour,
144 unsigned int min, unsigned int sec)
146 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
147 mon += 12; /* Puts Feb last since it has leap day */
148 year -= 1;
150 return (((
151 (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
152 year*365 - 719499
153 )*24 + hour /* now have hours */
154 )*60 + min /* now have minutes */
155 )*60 + sec; /* finally seconds */
159 * Initialize Programmable Interval Timers with standard values. Some
160 * drivers depend on them being initialized (e.g., joystick driver).
163 #ifdef CONFIG_RTC
164 void
165 rtc_init_pit (void)
167 unsigned char control;
169 /* Turn off RTC interrupts before /dev/rtc is initialized */
170 control = CMOS_READ(RTC_CONTROL);
171 control &= ~(RTC_PIE | RTC_AIE | RTC_UIE);
172 CMOS_WRITE(control, RTC_CONTROL);
173 (void) CMOS_READ(RTC_INTR_FLAGS);
175 request_region(0x40, 0x20, "timer"); /* reserve pit */
177 /* Setup interval timer. */
178 outb(0x34, 0x43); /* binary, mode 2, LSB/MSB, ch 0 */
179 outb(LATCH & 0xff, 0x40); /* LSB */
180 outb(LATCH >> 8, 0x40); /* MSB */
182 outb(0xb6, 0x43); /* pit counter 2: speaker */
183 outb(0x31, 0x42);
184 outb(0x13, 0x42);
186 #endif
188 void
189 generic_init_pit (void)
191 unsigned char x;
193 /* Reset periodic interrupt frequency. */
194 x = CMOS_READ(RTC_FREQ_SELECT) & 0x3f;
195 if (x != 0x26 && x != 0x19 && x != 0x06) {
196 printk("Setting RTC_FREQ to 1024 Hz (%x)\n", x);
197 CMOS_WRITE(0x26, RTC_FREQ_SELECT);
200 /* Turn on periodic interrupts. */
201 x = CMOS_READ(RTC_CONTROL);
202 if (!(x & RTC_PIE)) {
203 printk("Turning on RTC interrupts.\n");
204 x |= RTC_PIE;
205 x &= ~(RTC_AIE | RTC_UIE);
206 CMOS_WRITE(x, RTC_CONTROL);
208 (void) CMOS_READ(RTC_INTR_FLAGS);
210 request_region(RTC_PORT(0), 0x10, "timer"); /* reserve rtc */
212 outb(0x36, 0x43); /* pit counter 0: system timer */
213 outb(0x00, 0x40);
214 outb(0x00, 0x40);
216 outb(0xb6, 0x43); /* pit counter 2: speaker */
217 outb(0x31, 0x42);
218 outb(0x13, 0x42);
221 void
222 time_init(void)
224 void (*irq_handler)(int, void *, struct pt_regs *);
225 unsigned int year, mon, day, hour, min, sec, cc1, cc2;
226 unsigned long cycle_freq;
229 * The Linux interpretation of the CMOS clock register contents:
230 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
231 * RTC registers show the second which has precisely just started.
232 * Let's hope other operating systems interpret the RTC the same way.
234 do { } while (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP));
235 do { } while (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
237 /* Read cycle counter exactly on falling edge of update flag */
238 cc1 = rpcc();
240 /* If our cycle frequency isn't valid, go another round and give
241 a guess at what it should be. */
242 cycle_freq = hwrpb->cycle_freq;
243 if (cycle_freq == 0) {
244 printk("HWRPB cycle frequency bogus. Estimating... ");
246 do { } while (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP));
247 do { } while (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
248 cc2 = rpcc();
249 est_cycle_freq = cycle_freq = cc2 - cc1;
250 cc1 = cc2;
252 printk("%lu Hz\n", cycle_freq);
255 /* From John Bowman <bowman@math.ualberta.ca>: allow the values
256 to settle, as the Update-In-Progress bit going low isn't good
257 enough on some hardware. 2ms is our guess; we havn't found
258 bogomips yet, but this is close on a 500Mhz box. */
259 __delay(1000000);
261 sec = CMOS_READ(RTC_SECONDS);
262 min = CMOS_READ(RTC_MINUTES);
263 hour = CMOS_READ(RTC_HOURS);
264 day = CMOS_READ(RTC_DAY_OF_MONTH);
265 mon = CMOS_READ(RTC_MONTH);
266 year = CMOS_READ(RTC_YEAR);
268 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
270 BCD_TO_BIN(sec);
271 BCD_TO_BIN(min);
272 BCD_TO_BIN(hour);
273 BCD_TO_BIN(day);
274 BCD_TO_BIN(mon);
275 BCD_TO_BIN(year);
277 #ifdef ALPHA_PRE_V1_2_SRM_CONSOLE
279 * The meaning of life, the universe, and everything. Plus
280 * this makes the year come out right on SRM consoles earlier
281 * than v1.2.
283 year -= 42;
284 #endif
285 if ((year += 1900) < 1970)
286 year += 100;
287 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
288 xtime.tv_usec = 0;
290 if (HZ > (1<<16)) {
291 extern void __you_loose (void);
292 __you_loose();
295 state.last_time = cc1;
296 state.scaled_ticks_per_cycle
297 = ((unsigned long) HZ << FIX_SHIFT) / cycle_freq;
298 state.last_rtc_update = 0;
299 state.partial_tick = 0L;
301 /* setup timer */
302 irq_handler = timer_interrupt;
303 if (request_irq(TIMER_IRQ, irq_handler, 0, "timer", NULL))
304 panic("Could not allocate timer IRQ!");
308 * Use the cycle counter to estimate an displacement from the last time
309 * tick. Unfortunately the Alpha designers made only the low 32-bits of
310 * the cycle counter active, so we overflow on 8.2 seconds on a 500MHz
311 * part. So we can't do the "find absolute time in terms of cycles" thing
312 * that the other ports do.
314 void
315 do_gettimeofday(struct timeval *tv)
317 unsigned long flags, now, delta_cycles, delta_usec;
318 unsigned long sec, usec;
320 now = rpcc();
321 save_and_cli(flags);
322 sec = xtime.tv_sec;
323 usec = xtime.tv_usec;
324 delta_cycles = now - state.last_time;
325 restore_flags(flags);
328 * usec = cycles * ticks_per_cycle * 2**48 * 1e6 / (2**48 * ticks)
329 * = cycles * (s_t_p_c) * 1e6 / (2**48 * ticks)
330 * = cycles * (s_t_p_c) * 15625 / (2**42 * ticks)
332 * which, given a 600MHz cycle and a 1024Hz tick, has a
333 * dynamic range of about 1.7e17, which is less than the
334 * 1.8e19 in an unsigned long, so we are safe from overflow.
336 * Round, but with .5 up always, since .5 to even is harder
337 * with no clear gain.
340 delta_usec = delta_cycles * state.scaled_ticks_per_cycle * 15625;
341 delta_usec = ((delta_usec / ((1UL << (FIX_SHIFT-6-1)) * HZ)) + 1) / 2;
343 usec += delta_usec;
344 if (usec >= 1000000) {
345 sec += 1;
346 usec -= 1000000;
349 tv->tv_sec = sec;
350 tv->tv_usec = usec;
353 void
354 do_settimeofday(struct timeval *tv)
356 cli();
357 xtime = *tv;
358 time_adjust = 0; /* stop active adjtime() */
359 time_status |= STA_UNSYNC;
360 time_state = TIME_ERROR; /* p. 24, (a) */
361 time_maxerror = NTP_PHASE_LIMIT;
362 time_esterror = NTP_PHASE_LIMIT;
363 sti();
368 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
369 * called 500 ms after the second nowtime has started, because when
370 * nowtime is written into the registers of the CMOS clock, it will
371 * jump to the next second precisely 500 ms later. Check the Motorola
372 * MC146818A or Dallas DS12887 data sheet for details.
374 * BUG: This routine does not handle hour overflow properly; it just
375 * sets the minutes. Usually you won't notice until after reboot!
377 static int
378 set_rtc_mmss(unsigned long nowtime)
380 int retval = 0;
381 int real_seconds, real_minutes, cmos_minutes;
382 unsigned char save_control, save_freq_select;
384 /* Tell the clock it's being set */
385 save_control = CMOS_READ(RTC_CONTROL);
386 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
388 /* Stop and reset prescaler */
389 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
390 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
392 cmos_minutes = CMOS_READ(RTC_MINUTES);
393 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
394 BCD_TO_BIN(cmos_minutes);
397 * since we're only adjusting minutes and seconds,
398 * don't interfere with hour overflow. This avoids
399 * messing with unknown time zones but requires your
400 * RTC not to be off by more than 15 minutes
402 real_seconds = nowtime % 60;
403 real_minutes = nowtime / 60;
404 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) {
405 /* correct for half hour time zone */
406 real_minutes += 30;
408 real_minutes %= 60;
410 if (abs(real_minutes - cmos_minutes) < 30) {
411 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
412 BIN_TO_BCD(real_seconds);
413 BIN_TO_BCD(real_minutes);
415 CMOS_WRITE(real_seconds,RTC_SECONDS);
416 CMOS_WRITE(real_minutes,RTC_MINUTES);
417 } else {
418 printk(KERN_WARNING
419 "set_rtc_mmss: can't update from %d to %d\n",
420 cmos_minutes, real_minutes);
421 retval = -1;
424 /* The following flags have to be released exactly in this order,
425 * otherwise the DS12887 (popular MC146818A clone with integrated
426 * battery and quartz) will not reset the oscillator and will not
427 * update precisely 500 ms later. You won't find this mentioned in
428 * the Dallas Semiconductor data sheets, but who believes data
429 * sheets anyway ... -- Markus Kuhn
431 CMOS_WRITE(save_control, RTC_CONTROL);
432 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
434 return retval;