Import 2.3.18pre1
[davej-history.git] / arch / sparc / kernel / time.c
blob7ee196241cfac28d1e1984a4b78c7259c2d965a7
1 /* $Id: time.c,v 1.46 1999/08/31 13:11:26 anton Exp $
2 * linux/arch/sparc/kernel/time.c
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
7 * Chris Davis (cdavis@cois.on.ca) 03/27/1998
8 * Added support for the intersil on the sun4/4200
10 * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
11 * Support for MicroSPARC-IIep, PCI CPU.
13 * This file handles the Sparc specific time handling details.
15 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
16 * "A Kernel Model for Precision Timekeeping" by Dave Mills
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/param.h>
23 #include <linux/string.h>
24 #include <linux/mm.h>
25 #include <linux/interrupt.h>
26 #include <linux/timex.h>
27 #include <linux/init.h>
28 #include <linux/pci.h>
30 #include <asm/oplib.h>
31 #include <asm/segment.h>
32 #include <asm/timer.h>
33 #include <asm/mostek.h>
34 #include <asm/system.h>
35 #include <asm/irq.h>
36 #include <asm/io.h>
37 #include <asm/idprom.h>
38 #include <asm/machines.h>
39 #include <asm/sun4paddr.h>
40 #include <asm/page.h>
42 extern rwlock_t xtime_lock;
44 enum sparc_clock_type sp_clock_typ;
45 unsigned long mstk48t02_regs = 0UL;
46 static struct mostek48t08 *mstk48t08_regs = 0;
47 static int set_rtc_mmss(unsigned long);
48 static void sbus_do_settimeofday(struct timeval *tv);
50 #ifdef CONFIG_SUN4
51 struct intersil *intersil_clock;
52 #define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
53 (intsil_cmd)
55 #define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
56 (intsil_cmd)
58 #define intersil_start(intersil_reg) intersil_cmd(intersil_reg, \
59 ( INTERSIL_START | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
60 INTERSIL_INTR_ENABLE))
62 #define intersil_stop(intersil_reg) intersil_cmd(intersil_reg, \
63 ( INTERSIL_STOP | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
64 INTERSIL_INTR_ENABLE))
66 #define intersil_read_intr(intersil_reg, towhere) towhere = \
67 intersil_reg->int_intr_reg
69 #endif
71 __volatile__ unsigned int *master_l10_counter;
72 __volatile__ unsigned int *master_l10_limit;
75 * timer_interrupt() needs to keep up the real-time clock,
76 * as well as call the "do_timer()" routine every clocktick
78 void timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
80 /* last time the cmos clock got updated */
81 static long last_rtc_update=0;
83 #ifdef CONFIG_SUN4
84 if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
85 (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
86 int temp;
87 intersil_read_intr(intersil_clock, temp);
88 /* re-enable the irq */
89 enable_pil_irq(10);
91 #endif
92 clear_clock_irq();
94 write_lock(&xtime_lock);
96 do_timer(regs);
98 /* Determine when to update the Mostek clock. */
99 if ((time_status & STA_UNSYNC) == 0 &&
100 xtime.tv_sec > last_rtc_update + 660 &&
101 xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
102 xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
103 if (set_rtc_mmss(xtime.tv_sec) == 0)
104 last_rtc_update = xtime.tv_sec;
105 else
106 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
108 write_unlock(&xtime_lock);
111 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
112 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
113 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
115 * [For the Julian calendar (which was used in Russia before 1917,
116 * Britain & colonies before 1752, anywhere else before 1582,
117 * and is still in use by some communities) leave out the
118 * -year/100+year/400 terms, and add 10.]
120 * This algorithm was first published by Gauss (I think).
122 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
123 * machines were long is 32-bit! (However, as time_t is signed, we
124 * will already get problems at other places on 2038-01-19 03:14:08)
126 static inline unsigned long mktime(unsigned int year, unsigned int mon,
127 unsigned int day, unsigned int hour,
128 unsigned int min, unsigned int sec)
130 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
131 mon += 12; /* Puts Feb last since it has leap day */
132 year -= 1;
134 return (((
135 (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
136 year*365 - 719499
137 )*24 + hour /* now have hours */
138 )*60 + min /* now have minutes */
139 )*60 + sec; /* finally seconds */
142 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
143 static void __init kick_start_clock(void)
145 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
146 unsigned char sec;
147 int i, count;
149 prom_printf("CLOCK: Clock was stopped. Kick start ");
151 /* Turn on the kick start bit to start the oscillator. */
152 regs->creg |= MSTK_CREG_WRITE;
153 regs->sec &= ~MSTK_STOP;
154 regs->hour |= MSTK_KICK_START;
155 regs->creg &= ~MSTK_CREG_WRITE;
157 /* Delay to allow the clock oscillator to start. */
158 sec = MSTK_REG_SEC(regs);
159 for (i = 0; i < 3; i++) {
160 while (sec == MSTK_REG_SEC(regs))
161 for (count = 0; count < 100000; count++)
162 /* nothing */ ;
163 prom_printf(".");
164 sec = regs->sec;
166 prom_printf("\n");
168 /* Turn off kick start and set a "valid" time and date. */
169 regs->creg |= MSTK_CREG_WRITE;
170 regs->hour &= ~MSTK_KICK_START;
171 MSTK_SET_REG_SEC(regs,0);
172 MSTK_SET_REG_MIN(regs,0);
173 MSTK_SET_REG_HOUR(regs,0);
174 MSTK_SET_REG_DOW(regs,5);
175 MSTK_SET_REG_DOM(regs,1);
176 MSTK_SET_REG_MONTH(regs,8);
177 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
178 regs->creg &= ~MSTK_CREG_WRITE;
180 /* Ensure the kick start bit is off. If it isn't, turn it off. */
181 while (regs->hour & MSTK_KICK_START) {
182 prom_printf("CLOCK: Kick start still on!\n");
183 regs->creg |= MSTK_CREG_WRITE;
184 regs->hour &= ~MSTK_KICK_START;
185 regs->creg &= ~MSTK_CREG_WRITE;
188 prom_printf("CLOCK: Kick start procedure successful.\n");
191 /* Return nonzero if the clock chip battery is low. */
192 static __inline__ int has_low_battery(void)
194 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
195 unsigned char data1, data2;
197 data1 = regs->eeprom[0]; /* Read some data. */
198 regs->eeprom[0] = ~data1; /* Write back the complement. */
199 data2 = regs->eeprom[0]; /* Read back the complement. */
200 regs->eeprom[0] = data1; /* Restore the original value. */
202 return (data1 == data2); /* Was the write blocked? */
205 /* Probe for the real time clock chip on Sun4 */
206 static __inline__ void sun4_clock_probe(void)
208 #ifdef CONFIG_SUN4
209 int temp;
211 if( idprom->id_machtype == (SM_SUN4 | SM_4_330) ) {
212 sp_clock_typ = MSTK48T02;
213 mstk48t02_regs = (unsigned long)
214 sparc_alloc_io(sun4_clock_physaddr, 0,
215 sizeof(struct mostek48t02),
216 "clock", 0x0, 0x0);
217 mstk48t08_regs = 0; /* To catch weirdness */
218 intersil_clock = 0; /* just in case */
220 /* Kick start the clock if it is completely stopped. */
221 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
222 kick_start_clock();
223 } else if( idprom->id_machtype == (SM_SUN4 | SM_4_260)) {
224 /* intersil setup code */
225 printk("Clock: INTERSIL at %8x ",sun4_clock_physaddr);
226 sp_clock_typ = INTERSIL;
227 intersil_clock = (struct intersil *)
228 sparc_alloc_io(sun4_clock_physaddr, 0,
229 sizeof(*intersil_clock),
230 "clock", 0x0, 0x0);
231 mstk48t02_regs = 0; /* just be sure */
232 mstk48t08_regs = 0; /* ditto */
233 /* initialise the clock */
235 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
237 intersil_start(intersil_clock);
239 intersil_read_intr(intersil_clock, temp);
240 while (!(temp & 0x80))
241 intersil_read_intr(intersil_clock, temp);
243 intersil_read_intr(intersil_clock, temp);
244 while (!(temp & 0x80))
245 intersil_read_intr(intersil_clock, temp);
247 intersil_stop(intersil_clock);
250 #endif
253 /* Probe for the mostek real time clock chip. */
254 static __inline__ void clock_probe(void)
256 struct linux_prom_registers clk_reg[2];
257 char model[128];
258 register int node, cpuunit, bootbus;
260 cpuunit = bootbus = 0;
262 /* Determine the correct starting PROM node for the probe. */
263 node = prom_getchild(prom_root_node);
264 switch (sparc_cpu_model) {
265 case sun4c:
266 break;
267 case sun4m:
268 node = prom_getchild(prom_searchsiblings(node, "obio"));
269 break;
270 case sun4d:
271 node = prom_getchild(bootbus = prom_searchsiblings(prom_getchild(cpuunit = prom_searchsiblings(node, "cpu-unit")), "bootbus"));
272 break;
273 default:
274 prom_printf("CLOCK: Unsupported architecture!\n");
275 prom_halt();
278 /* Find the PROM node describing the real time clock. */
279 sp_clock_typ = MSTK_INVALID;
280 node = prom_searchsiblings(node,"eeprom");
281 if (!node) {
282 prom_printf("CLOCK: No clock found!\n");
283 prom_halt();
286 /* Get the model name and setup everything up. */
287 model[0] = '\0';
288 prom_getstring(node, "model", model, sizeof(model));
289 if (strcmp(model, "mk48t02") == 0) {
290 sp_clock_typ = MSTK48T02;
291 if (prom_getproperty(node, "reg", (char *) clk_reg, sizeof(clk_reg)) == -1) {
292 prom_printf("clock_probe: FAILED!\n");
293 prom_halt();
295 if (sparc_cpu_model == sun4d)
296 prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
297 else
298 prom_apply_obio_ranges(clk_reg, 1);
299 /* Map the clock register io area read-only */
300 mstk48t02_regs = (unsigned long)
301 sparc_alloc_io(clk_reg[0].phys_addr,
302 (void *) 0, sizeof(struct mostek48t02),
303 "clock", clk_reg[0].which_io, 0x0);
304 mstk48t08_regs = 0; /* To catch weirdness */
305 } else if (strcmp(model, "mk48t08") == 0) {
306 sp_clock_typ = MSTK48T08;
307 if(prom_getproperty(node, "reg", (char *) clk_reg,
308 sizeof(clk_reg)) == -1) {
309 prom_printf("clock_probe: FAILED!\n");
310 prom_halt();
312 if (sparc_cpu_model == sun4d)
313 prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
314 else
315 prom_apply_obio_ranges(clk_reg, 1);
316 /* Map the clock register io area read-only */
317 mstk48t08_regs = (struct mostek48t08 *)
318 sparc_alloc_io(clk_reg[0].phys_addr,
319 (void *) 0, sizeof(*mstk48t08_regs),
320 "clock", clk_reg[0].which_io, 0x0);
322 mstk48t02_regs = (unsigned long)&mstk48t08_regs->regs;
323 } else {
324 prom_printf("CLOCK: Unknown model name '%s'\n",model);
325 prom_halt();
328 /* Report a low battery voltage condition. */
329 if (has_low_battery())
330 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
332 /* Kick start the clock if it is completely stopped. */
333 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
334 kick_start_clock();
337 void __init sbus_time_init(void)
339 unsigned int year, mon, day, hour, min, sec;
340 struct mostek48t02 *mregs;
342 #ifdef CONFIG_SUN4
343 int temp;
344 struct intersil *iregs;
345 #endif
347 do_get_fast_time = do_gettimeofday;
348 BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
349 btfixup();
351 #if CONFIG_AP1000
352 init_timers(timer_interrupt);
353 ap_init_time(&xtime);
354 return;
355 #endif
357 if (ARCH_SUN4)
358 sun4_clock_probe();
359 else
360 clock_probe();
362 init_timers(timer_interrupt);
364 #ifdef CONFIG_SUN4
365 if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
366 #endif
367 mregs = (struct mostek48t02 *)mstk48t02_regs;
368 if(!mregs) {
369 prom_printf("Something wrong, clock regs not mapped yet.\n");
370 prom_halt();
372 mregs->creg |= MSTK_CREG_READ;
373 sec = MSTK_REG_SEC(mregs);
374 min = MSTK_REG_MIN(mregs);
375 hour = MSTK_REG_HOUR(mregs);
376 day = MSTK_REG_DOM(mregs);
377 mon = MSTK_REG_MONTH(mregs);
378 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
379 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
380 xtime.tv_usec = 0;
381 mregs->creg &= ~MSTK_CREG_READ;
382 #ifdef CONFIG_SUN4
383 } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
384 /* initialise the intersil on sun4 */
386 iregs=intersil_clock;
387 if(!iregs) {
388 prom_printf("Something wrong, clock regs not mapped yet.\n");
389 prom_halt();
392 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
393 disable_pil_irq(10);
394 intersil_stop(iregs);
395 intersil_read_intr(intersil_clock, temp);
397 temp = iregs->clk.int_csec;
399 sec = iregs->clk.int_sec;
400 min = iregs->clk.int_min;
401 hour = iregs->clk.int_hour;
402 day = iregs->clk.int_day;
403 mon = iregs->clk.int_month;
404 year = MSTK_CVT_YEAR(iregs->clk.int_year);
406 enable_pil_irq(10);
407 intersil_start(iregs);
409 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
410 xtime.tv_usec = 0;
411 printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
413 #endif
415 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
416 __sti();
419 void __init time_init(void)
421 #ifdef CONFIG_PCI
422 extern void pci_time_init(void);
423 if (pci_present()) {
424 pci_time_init();
425 return;
427 #endif
428 sbus_time_init();
431 extern __inline__ unsigned long do_gettimeoffset(void)
433 unsigned long offset = 0;
434 unsigned int count;
436 count = (*master_l10_counter >> 10) & 0x1fffff;
438 if(test_bit(TIMER_BH, &bh_active))
439 offset = 1000000;
441 return offset + count;
444 /* This need not obtain the xtime_lock as it is coded in
445 * an implicitly SMP safe way already.
447 void do_gettimeofday(struct timeval *tv)
449 #if CONFIG_AP1000
450 unsigned long flags;
452 save_and_cli(flags);
453 ap_gettimeofday(&xtime);
454 *tv = xtime;
455 restore_flags(flags);
456 #else /* !(CONFIG_AP1000) */
457 /* Load doubles must be used on xtime so that what we get
458 * is guarenteed to be atomic, this is why we can run this
459 * with interrupts on full blast. Don't touch this... -DaveM
461 __asm__ __volatile__("
462 sethi %hi(master_l10_counter), %o1
463 ld [%o1 + %lo(master_l10_counter)], %g3
464 sethi %hi(xtime), %g2
465 1: ldd [%g2 + %lo(xtime)], %o4
466 ld [%g3], %o1
467 ldd [%g2 + %lo(xtime)], %o2
468 xor %o4, %o2, %o2
469 xor %o5, %o3, %o3
470 orcc %o2, %o3, %g0
471 bne 1b
472 cmp %o1, 0
473 bge 1f
474 srl %o1, 0xa, %o1
475 sethi %hi(tick), %o3
476 ld [%o3 + %lo(tick)], %o3
477 sethi %hi(0x1fffff), %o2
478 or %o2, %lo(0x1fffff), %o2
479 add %o5, %o3, %o5
480 and %o1, %o2, %o1
481 1: add %o5, %o1, %o5
482 sethi %hi(1000000), %o2
483 or %o2, %lo(1000000), %o2
484 cmp %o5, %o2
485 bl,a 1f
486 st %o4, [%o0 + 0x0]
487 add %o4, 0x1, %o4
488 sub %o5, %o2, %o5
489 st %o4, [%o0 + 0x0]
490 1: st %o5, [%o0 + 0x4]");
491 #endif
494 void do_settimeofday(struct timeval *tv)
496 write_lock_irq(&xtime_lock);
497 bus_do_settimeofday(tv);
498 write_unlock_irq(&xtime_lock);
501 static void sbus_do_settimeofday(struct timeval *tv)
503 #if !CONFIG_AP1000
504 tv->tv_usec -= do_gettimeoffset();
505 if(tv->tv_usec < 0) {
506 tv->tv_usec += 1000000;
507 tv->tv_sec--;
509 #endif
510 xtime = *tv;
511 time_adjust = 0; /* stop active adjtime() */
512 time_status |= STA_UNSYNC;
513 time_maxerror = NTP_PHASE_LIMIT;
514 time_esterror = NTP_PHASE_LIMIT;
518 * BUG: This routine does not handle hour overflow properly; it just
519 * sets the minutes. Usually you won't notice until after reboot!
521 static int set_rtc_mmss(unsigned long nowtime)
523 int real_seconds, real_minutes, mostek_minutes;
524 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
525 #ifdef CONFIG_SUN4
526 struct intersil *iregs = intersil_clock;
527 int temp;
528 #endif
530 /* Not having a register set can lead to trouble. */
531 if (!regs) {
532 #ifdef CONFIG_SUN4
533 if(!iregs)
534 return -1;
535 else {
536 temp = iregs->clk.int_csec;
538 mostek_minutes = iregs->clk.int_min;
540 real_seconds = nowtime % 60;
541 real_minutes = nowtime / 60;
542 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
543 real_minutes += 30; /* correct for half hour time zone */
544 real_minutes %= 60;
546 if (abs(real_minutes - mostek_minutes) < 30) {
547 intersil_stop(iregs);
548 iregs->clk.int_sec=real_seconds;
549 iregs->clk.int_min=real_minutes;
550 intersil_start(iregs);
551 } else {
552 printk(KERN_WARNING
553 "set_rtc_mmss: can't update from %d to %d\n",
554 mostek_minutes, real_minutes);
555 return -1;
558 return 0;
560 #endif
562 /* Read the current RTC minutes. */
563 regs->creg |= MSTK_CREG_READ;
564 mostek_minutes = MSTK_REG_MIN(regs);
565 regs->creg &= ~MSTK_CREG_READ;
568 * since we're only adjusting minutes and seconds,
569 * don't interfere with hour overflow. This avoids
570 * messing with unknown time zones but requires your
571 * RTC not to be off by more than 15 minutes
573 real_seconds = nowtime % 60;
574 real_minutes = nowtime / 60;
575 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
576 real_minutes += 30; /* correct for half hour time zone */
577 real_minutes %= 60;
579 if (abs(real_minutes - mostek_minutes) < 30) {
580 regs->creg |= MSTK_CREG_WRITE;
581 MSTK_SET_REG_SEC(regs,real_seconds);
582 MSTK_SET_REG_MIN(regs,real_minutes);
583 regs->creg &= ~MSTK_CREG_WRITE;
584 } else
585 return -1;
587 return 0;