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>
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>
37 #include <asm/idprom.h>
38 #include <asm/machines.h>
39 #include <asm/sun4paddr.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
);
51 struct intersil
*intersil_clock
;
52 #define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
55 #define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
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
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;
84 if((idprom
->id_machtype
== (SM_SUN4
| SM_4_260
)) ||
85 (idprom
->id_machtype
== (SM_SUN4
| SM_4_110
))) {
87 intersil_read_intr(intersil_clock
, temp
);
88 /* re-enable the irq */
94 write_lock(&xtime_lock
);
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
;
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 */
135 (unsigned long)(year
/4 - year
/100 + year
/400 + 367*mon
/12 + day
) +
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
;
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
++)
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)
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
),
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
)
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
),
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
);
253 /* Probe for the mostek real time clock chip. */
254 static __inline__
void clock_probe(void)
256 struct linux_prom_registers clk_reg
[2];
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
) {
268 node
= prom_getchild(prom_searchsiblings(node
, "obio"));
271 node
= prom_getchild(bootbus
= prom_searchsiblings(prom_getchild(cpuunit
= prom_searchsiblings(node
, "cpu-unit")), "bootbus"));
274 prom_printf("CLOCK: Unsupported architecture!\n");
278 /* Find the PROM node describing the real time clock. */
279 sp_clock_typ
= MSTK_INVALID
;
280 node
= prom_searchsiblings(node
,"eeprom");
282 prom_printf("CLOCK: No clock found!\n");
286 /* Get the model name and setup everything up. */
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");
295 if (sparc_cpu_model
== sun4d
)
296 prom_apply_generic_ranges (bootbus
, cpuunit
, clk_reg
, 1);
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");
312 if (sparc_cpu_model
== sun4d
)
313 prom_apply_generic_ranges (bootbus
, cpuunit
, clk_reg
, 1);
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
;
324 prom_printf("CLOCK: Unknown model name '%s'\n",model
);
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
)
337 void __init
sbus_time_init(void)
339 unsigned int year
, mon
, day
, hour
, min
, sec
;
340 struct mostek48t02
*mregs
;
344 struct intersil
*iregs
;
347 do_get_fast_time
= do_gettimeofday
;
348 BTFIXUPSET_CALL(bus_do_settimeofday
, sbus_do_settimeofday
, BTFIXUPCALL_NORM
);
352 init_timers(timer_interrupt
);
353 ap_init_time(&xtime
);
362 init_timers(timer_interrupt
);
365 if(idprom
->id_machtype
== (SM_SUN4
| SM_4_330
)) {
367 mregs
= (struct mostek48t02
*)mstk48t02_regs
;
369 prom_printf("Something wrong, clock regs not mapped yet.\n");
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
);
381 mregs
->creg
&= ~MSTK_CREG_READ
;
383 } else if(idprom
->id_machtype
== (SM_SUN4
| SM_4_260
) ) {
384 /* initialise the intersil on sun4 */
386 iregs
=intersil_clock
;
388 prom_printf("Something wrong, clock regs not mapped yet.\n");
392 intersil_intr(intersil_clock
,INTERSIL_INT_100HZ
);
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
);
407 intersil_start(iregs
);
409 xtime
.tv_sec
= mktime(year
, mon
, day
, hour
, min
, sec
);
411 printk("%u/%u/%u %u:%u:%u\n",day
,mon
,year
,hour
,min
,sec
);
415 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
419 void __init
time_init(void)
422 extern void pci_time_init(void);
431 extern __inline__
unsigned long do_gettimeoffset(void)
433 unsigned long offset
= 0;
436 count
= (*master_l10_counter
>> 10) & 0x1fffff;
438 if(test_bit(TIMER_BH
, &bh_active
))
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
)
453 ap_gettimeofday(&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
467 ldd [%g2 + %lo(xtime)], %o2
476 ld [%o3 + %lo(tick)], %o3
477 sethi %hi(0x1fffff), %o2
478 or %o2, %lo(0x1fffff), %o2
482 sethi %hi(1000000), %o2
483 or %o2, %lo(1000000), %o2
490 1: st %o5, [%o0 + 0x4]");
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
)
504 tv
->tv_usec
-= do_gettimeoffset();
505 if(tv
->tv_usec
< 0) {
506 tv
->tv_usec
+= 1000000;
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
;
526 struct intersil
*iregs
= intersil_clock
;
530 /* Not having a register set can lead to trouble. */
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 */
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
);
553 "set_rtc_mmss: can't update from %d to %d\n",
554 mostek_minutes
, real_minutes
);
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 */
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
;