1 /* $Id: time.c,v 1.16 1998/09/05 17:25:28 jj Exp $
2 * time.c: UltraSparc timer and TOD clock support.
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
7 * Based largely on code which is:
9 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/param.h>
17 #include <linux/string.h>
19 #include <linux/interrupt.h>
20 #include <linux/timex.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
24 #include <asm/oplib.h>
25 #include <asm/mostek.h>
26 #include <asm/timer.h>
34 struct mostek48t02
*mstk48t02_regs
= 0;
35 static struct mostek48t08
*mstk48t08_regs
= 0;
36 static struct mostek48t59
*mstk48t59_regs
= 0;
38 static int set_rtc_mmss(unsigned long);
40 /* timer_interrupt() needs to keep up the real-time clock,
41 * as well as call the "do_timer()" routine every clocktick
43 * NOTE: On SUN5 systems the ticker interrupt comes in using 2
44 * interrupts, one at level14 and one with softint bit 0.
46 unsigned long timer_tick_offset
;
47 static unsigned long timer_tick_compare
;
48 static unsigned long timer_ticks_per_usec
;
50 static __inline__
void timer_check_rtc(void)
52 /* last time the cmos clock got updated */
53 static long last_rtc_update
=0;
55 /* Determine when to update the Mostek clock. */
56 if ((time_status
& STA_UNSYNC
) == 0 &&
57 xtime
.tv_sec
> last_rtc_update
+ 660 &&
58 xtime
.tv_usec
>= 500000 - ((unsigned) tick
) / 2 &&
59 xtime
.tv_usec
<= 500000 + ((unsigned) tick
) / 2) {
60 if (set_rtc_mmss(xtime
.tv_sec
) == 0)
61 last_rtc_update
= xtime
.tv_sec
;
63 last_rtc_update
= xtime
.tv_sec
- 600;
64 /* do it again in 60 s */
68 static void timer_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
75 __asm__
__volatile__("
80 : "=&r" (timer_tick_compare
), "=r" (ticks
)
81 : "r" (timer_tick_offset
));
82 } while (ticks
>= timer_tick_compare
);
88 void timer_tick_interrupt(struct pt_regs
*regs
)
93 * Only keep timer_tick_offset uptodate, but don't set TICK_CMPR.
95 __asm__
__volatile__("
98 : "=&r" (timer_tick_compare
)
99 : "r" (timer_tick_offset
));
105 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
106 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
107 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
109 * [For the Julian calendar (which was used in Russia before 1917,
110 * Britain & colonies before 1752, anywhere else before 1582,
111 * and is still in use by some communities) leave out the
112 * -year/100+year/400 terms, and add 10.]
114 * This algorithm was first published by Gauss (I think).
116 * WARNING: this function will overflow on 2106-02-07 06:28:16 on
117 * machines were long is 32-bit! (However, as time_t is signed, we
118 * will already get problems at other places on 2038-01-19 03:14:08)
120 static inline unsigned long mktime(unsigned int year
, unsigned int mon
,
121 unsigned int day
, unsigned int hour
,
122 unsigned int min
, unsigned int sec
)
124 if (0 >= (int) (mon
-= 2)) { /* 1..12 -> 11,12,1..10 */
125 mon
+= 12; /* Puts Feb last since it has leap day */
129 (unsigned long)(year
/4 - year
/100 + year
/400 + 367*mon
/12 + day
) +
131 )*24 + hour
/* now have hours */
132 )*60 + min
/* now have minutes */
133 )*60 + sec
; /* finally seconds */
136 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
137 static void __init
kick_start_clock(void)
139 register struct mostek48t02
*regs
= mstk48t02_regs
;
143 prom_printf("CLOCK: Clock was stopped. Kick start ");
145 /* Turn on the kick start bit to start the oscillator. */
146 regs
->creg
|= MSTK_CREG_WRITE
;
147 regs
->sec
&= ~MSTK_STOP
;
148 regs
->hour
|= MSTK_KICK_START
;
149 regs
->creg
&= ~MSTK_CREG_WRITE
;
151 /* Delay to allow the clock oscillator to start. */
152 sec
= MSTK_REG_SEC(regs
);
153 for (i
= 0; i
< 3; i
++) {
154 while (sec
== MSTK_REG_SEC(regs
))
155 for (count
= 0; count
< 100000; count
++)
162 /* Turn off kick start and set a "valid" time and date. */
163 regs
->creg
|= MSTK_CREG_WRITE
;
164 regs
->hour
&= ~MSTK_KICK_START
;
165 MSTK_SET_REG_SEC(regs
,0);
166 MSTK_SET_REG_MIN(regs
,0);
167 MSTK_SET_REG_HOUR(regs
,0);
168 MSTK_SET_REG_DOW(regs
,5);
169 MSTK_SET_REG_DOM(regs
,1);
170 MSTK_SET_REG_MONTH(regs
,8);
171 MSTK_SET_REG_YEAR(regs
,1996 - MSTK_YEAR_ZERO
);
172 regs
->creg
&= ~MSTK_CREG_WRITE
;
174 /* Ensure the kick start bit is off. If it isn't, turn it off. */
175 while (regs
->hour
& MSTK_KICK_START
) {
176 prom_printf("CLOCK: Kick start still on!\n");
177 regs
->creg
|= MSTK_CREG_WRITE
;
178 regs
->hour
&= ~MSTK_KICK_START
;
179 regs
->creg
&= ~MSTK_CREG_WRITE
;
182 prom_printf("CLOCK: Kick start procedure successful.\n");
185 /* Return nonzero if the clock chip battery is low. */
186 static int __init
has_low_battery(void)
188 register struct mostek48t02
*regs
= mstk48t02_regs
;
189 unsigned char data1
, data2
;
191 data1
= regs
->eeprom
[0]; /* Read some data. */
192 regs
->eeprom
[0] = ~data1
; /* Write back the complement. */
193 data2
= regs
->eeprom
[0]; /* Read back the complement. */
194 regs
->eeprom
[0] = data1
; /* Restore the original value. */
196 return (data1
== data2
); /* Was the write blocked? */
200 /* Probe for the real time clock chip. */
201 static void __init
set_system_time(void)
203 unsigned int year
, mon
, day
, hour
, min
, sec
;
204 struct mostek48t02
*mregs
;
206 do_get_fast_time
= do_gettimeofday
;
208 mregs
= mstk48t02_regs
;
210 prom_printf("Something wrong, clock regs not mapped yet.\n");
214 mregs
->creg
|= MSTK_CREG_READ
;
215 sec
= MSTK_REG_SEC(mregs
);
216 min
= MSTK_REG_MIN(mregs
);
217 hour
= MSTK_REG_HOUR(mregs
);
218 day
= MSTK_REG_DOM(mregs
);
219 mon
= MSTK_REG_MONTH(mregs
);
220 year
= MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs
) );
221 xtime
.tv_sec
= mktime(year
, mon
, day
, hour
, min
, sec
);
223 mregs
->creg
&= ~MSTK_CREG_READ
;
226 void __init
clock_probe(void)
228 struct linux_prom_registers clk_reg
[2];
230 int node
, busnd
= -1, err
;
233 struct linux_ebus
*ebus
= 0;
236 __save_and_cli(flags
);
238 if(central_bus
!= NULL
) {
239 busnd
= central_bus
->child
->prom_node
;
242 else if (ebus_chain
!= NULL
) {
244 busnd
= ebus
->prom_node
;
248 busnd
= SBus_chain
->prom_node
;
252 prom_printf("clock_probe: problem, cannot find bus to search.\n");
256 node
= prom_getchild(busnd
);
259 prom_getstring(node
, "model", model
, sizeof(model
));
260 if(strcmp(model
, "mk48t02") &&
261 strcmp(model
, "mk48t08") &&
262 strcmp(model
, "mk48t59")) {
263 node
= prom_getsibling(node
);
265 if ((node
== 0) && ebus
) {
268 busnd
= ebus
->prom_node
;
269 node
= prom_getchild(busnd
);
274 prom_printf("clock_probe: Cannot find timer chip\n");
280 err
= prom_getproperty(node
, "reg", (char *)clk_reg
,
283 prom_printf("clock_probe: Cannot get Mostek reg property\n");
288 prom_apply_fhc_ranges(central_bus
->child
, clk_reg
, 1);
289 prom_apply_central_ranges(central_bus
, clk_reg
, 1);
292 else if (ebus_chain
) {
293 struct linux_ebus_device
*edev
;
295 for_each_ebusdev(edev
, ebus
)
296 if (edev
->prom_node
== node
)
299 prom_printf("%s: Mostek not probed by EBUS\n",
304 if (check_region(edev
->base_address
[0],
305 sizeof(struct mostek48t59
))) {
306 prom_printf("%s: Can't get region %lx, %d\n",
307 __FUNCTION__
, edev
->base_address
[0],
308 sizeof(struct mostek48t59
));
311 request_region(edev
->base_address
[0],
312 sizeof(struct mostek48t59
), "clock");
314 mstk48t59_regs
= (struct mostek48t59
*)
315 edev
->base_address
[0];
316 mstk48t02_regs
= &mstk48t59_regs
->regs
;
321 prom_adjust_regs(clk_reg
, 1,
322 SBus_chain
->sbus_ranges
,
323 SBus_chain
->num_sbus_ranges
);
326 if(model
[5] == '0' && model
[6] == '2') {
327 mstk48t02_regs
= (struct mostek48t02
*)
328 sparc_alloc_io(clk_reg
[0].phys_addr
,
329 (void *) 0, sizeof(*mstk48t02_regs
),
330 "clock", clk_reg
[0].which_io
, 0x0);
331 } else if(model
[5] == '0' && model
[6] == '8') {
332 mstk48t08_regs
= (struct mostek48t08
*)
333 sparc_alloc_io(clk_reg
[0].phys_addr
,
334 (void *) 0, sizeof(*mstk48t08_regs
),
335 "clock", clk_reg
[0].which_io
, 0x0);
336 mstk48t02_regs
= &mstk48t08_regs
->regs
;
338 mstk48t59_regs
= (struct mostek48t59
*)
339 sparc_alloc_io(clk_reg
[0].phys_addr
,
340 (void *) 0, sizeof(*mstk48t59_regs
),
341 "clock", clk_reg
[0].which_io
, 0x0);
342 mstk48t02_regs
= &mstk48t59_regs
->regs
;
347 /* Report a low battery voltage condition. */
348 if (has_low_battery())
349 prom_printf("NVRAM: Low battery voltage!\n");
351 /* Kick start the clock if it is completely stopped. */
352 if (mstk48t02_regs
->sec
& MSTK_STOP
)
357 __restore_flags(flags
);
361 #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
365 #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
368 extern void init_timers(void (*func
)(int, void *, struct pt_regs
*),
371 void __init
time_init(void)
373 /* clock_probe() is now done at end of [se]bus_init on sparc64
374 * so that sbus, fhc and ebus bus information is probed and
379 init_timers(timer_interrupt
, &clock
);
380 timer_tick_offset
= clock
/ HZ
;
381 timer_ticks_per_usec
= clock
/ 1000000;
384 static __inline__
unsigned long do_gettimeoffset(void)
388 __asm__
__volatile__("
394 : "r" (timer_tick_offset
), "r" (timer_tick_compare
)
397 return ticks
/ timer_ticks_per_usec
;
400 void do_gettimeofday(struct timeval
*tv
)
402 /* Load doubles must be used on xtime so that what we get
403 * is guarenteed to be atomic, this is why we can run this
404 * with interrupts on full blast. Don't touch this... -DaveM
406 * Note with time_t changes to the timeval type, I must now use
407 * nucleus atomic quad 128-bit loads.
409 __asm__
__volatile__("
410 sethi %hi(timer_tick_offset), %g3
411 sethi %hi(xtime), %g2
412 sethi %hi(timer_tick_compare), %g1
413 ldx [%g3 + %lo(timer_tick_offset)], %g3
414 or %g2, %lo(xtime), %g2
415 or %g1, %lo(timer_tick_compare), %g1
416 1: ldda [%g2] 0x24, %o4
417 membar #LoadLoad | #MemIssue
420 membar #LoadLoad | #MemIssue
427 sethi %hi(lost_ticks), %o2
428 sethi %hi(timer_ticks_per_usec), %o3
429 ldx [%o2 + %lo(lost_ticks)], %o2
431 ldx [%o3 + %lo(timer_ticks_per_usec)], %o3
435 sethi %hi(10000), %g2
436 or %g2, %lo(10000), %g2
438 1: sethi %hi(1000000), %o2
440 or %o2, %lo(1000000), %o2
448 1: st %o5, [%o0 + 0x8]");
451 void do_settimeofday(struct timeval
*tv
)
455 tv
->tv_usec
-= do_gettimeoffset();
456 if(tv
->tv_usec
< 0) {
457 tv
->tv_usec
+= 1000000;
462 time_adjust
= 0; /* stop active adjtime() */
463 time_status
|= STA_UNSYNC
;
464 time_state
= TIME_ERROR
; /* p. 24, (a) */
465 time_maxerror
= NTP_PHASE_LIMIT
;
466 time_esterror
= NTP_PHASE_LIMIT
;
470 static int set_rtc_mmss(unsigned long nowtime
)
472 int real_seconds
, real_minutes
, mostek_minutes
;
473 struct mostek48t02
*regs
= mstk48t02_regs
;
475 /* Not having a register set can lead to trouble. */
479 /* Read the current RTC minutes. */
480 regs
->creg
|= MSTK_CREG_READ
;
481 mostek_minutes
= MSTK_REG_MIN(regs
);
482 regs
->creg
&= ~MSTK_CREG_READ
;
485 * since we're only adjusting minutes and seconds,
486 * don't interfere with hour overflow. This avoids
487 * messing with unknown time zones but requires your
488 * RTC not to be off by more than 15 minutes
490 real_seconds
= nowtime
% 60;
491 real_minutes
= nowtime
/ 60;
492 if (((abs(real_minutes
- mostek_minutes
) + 15)/30) & 1)
493 real_minutes
+= 30; /* correct for half hour time zone */
496 if (abs(real_minutes
- mostek_minutes
) < 30) {
497 regs
->creg
|= MSTK_CREG_WRITE
;
498 MSTK_SET_REG_SEC(regs
,real_seconds
);
499 MSTK_SET_REG_MIN(regs
,real_minutes
);
500 regs
->creg
&= ~MSTK_CREG_WRITE
;