2.2.0-final
[davej-history.git] / arch / sparc64 / kernel / time.c
blobc826ce56db56905e8995243ec347be7db5c7b416
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>
18 #include <linux/mm.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>
27 #include <asm/irq.h>
28 #include <asm/io.h>
29 #include <asm/sbus.h>
30 #include <asm/fhc.h>
31 #include <asm/pbm.h>
32 #include <asm/ebus.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;
62 else
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)
70 unsigned long ticks;
72 do {
73 do_timer(regs);
75 __asm__ __volatile__("
76 rd %%tick_cmpr, %0
77 add %0, %2, %0
78 wr %0, 0, %%tick_cmpr
79 rd %%tick, %1"
80 : "=&r" (timer_tick_compare), "=r" (ticks)
81 : "r" (timer_tick_offset));
82 } while (ticks >= timer_tick_compare);
84 timer_check_rtc();
87 #ifdef __SMP__
88 void timer_tick_interrupt(struct pt_regs *regs)
90 do_timer(regs);
93 * Only keep timer_tick_offset uptodate, but don't set TICK_CMPR.
95 __asm__ __volatile__("
96 rd %%tick_cmpr, %0
97 add %0, %1, %0"
98 : "=&r" (timer_tick_compare)
99 : "r" (timer_tick_offset));
101 timer_check_rtc();
103 #endif
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 */
126 year -= 1;
128 return (((
129 (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
130 year*365 - 719499
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;
140 unsigned char sec;
141 int i, count;
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++)
156 /* nothing */ ;
157 prom_printf(".");
158 sec = regs->sec;
160 prom_printf("\n");
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;
209 if(!mregs) {
210 prom_printf("Something wrong, clock regs not mapped yet.\n");
211 prom_halt();
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);
222 xtime.tv_usec = 0;
223 mregs->creg &= ~MSTK_CREG_READ;
226 void __init clock_probe(void)
228 struct linux_prom_registers clk_reg[2];
229 char model[128];
230 int node, busnd = -1, err;
231 unsigned long flags;
232 #ifdef CONFIG_PCI
233 struct linux_ebus *ebus = 0;
234 #endif
236 __save_and_cli(flags);
238 if(central_bus != NULL) {
239 busnd = central_bus->child->prom_node;
241 #ifdef CONFIG_PCI
242 else if (ebus_chain != NULL) {
243 ebus = ebus_chain;
244 busnd = ebus->prom_node;
246 #endif
247 else {
248 busnd = SBus_chain->prom_node;
251 if(busnd == -1) {
252 prom_printf("clock_probe: problem, cannot find bus to search.\n");
253 prom_halt();
256 node = prom_getchild(busnd);
258 while(1) {
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);
264 #ifdef CONFIG_PCI
265 if ((node == 0) && ebus) {
266 ebus = ebus->next;
267 if (ebus) {
268 busnd = ebus->prom_node;
269 node = prom_getchild(busnd);
272 #endif
273 if(node == 0) {
274 prom_printf("clock_probe: Cannot find timer chip\n");
275 prom_halt();
277 continue;
280 err = prom_getproperty(node, "reg", (char *)clk_reg,
281 sizeof(clk_reg));
282 if(err == -1) {
283 prom_printf("clock_probe: Cannot get Mostek reg property\n");
284 prom_halt();
287 if(central_bus) {
288 prom_apply_fhc_ranges(central_bus->child, clk_reg, 1);
289 prom_apply_central_ranges(central_bus, clk_reg, 1);
291 #ifdef CONFIG_PCI
292 else if (ebus_chain) {
293 struct linux_ebus_device *edev;
295 for_each_ebusdev(edev, ebus)
296 if (edev->prom_node == node)
297 break;
298 if (!edev) {
299 prom_printf("%s: Mostek not probed by EBUS\n",
300 __FUNCTION__);
301 prom_halt();
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));
309 prom_halt();
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;
317 break;
319 #endif
320 else {
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;
337 } else {
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;
344 break;
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)
353 kick_start_clock();
355 set_system_time();
357 __restore_flags(flags);
360 #ifndef BCD_TO_BIN
361 #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)
362 #endif
364 #ifndef BIN_TO_BCD
365 #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)
366 #endif
368 extern void init_timers(void (*func)(int, void *, struct pt_regs *),
369 unsigned long *);
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
375 * available.
377 unsigned long clock;
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)
386 unsigned long ticks;
388 __asm__ __volatile__("
389 rd %%tick, %%g1
390 add %1, %%g1, %0
391 sub %0, %2, %0
393 : "=r" (ticks)
394 : "r" (timer_tick_offset), "r" (timer_tick_compare)
395 : "g1", "g2");
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
418 rd %tick, %o1
419 ldx [%g1], %g7
420 membar #LoadLoad | #MemIssue
421 ldda [%g2] 0x24, %o2
422 membar #LoadLoad
423 xor %o4, %o2, %o2
424 xor %o5, %o3, %o3
425 orcc %o2, %o3, %g0
426 bne,pn %xcc, 1b
427 sethi %hi(lost_ticks), %o2
428 sethi %hi(timer_ticks_per_usec), %o3
429 ldx [%o2 + %lo(lost_ticks)], %o2
430 add %g3, %o1, %o1
431 ldx [%o3 + %lo(timer_ticks_per_usec)], %o3
432 sub %o1, %g7, %o1
433 brz,pt %o2, 1f
434 udivx %o1, %o3, %o1
435 sethi %hi(10000), %g2
436 or %g2, %lo(10000), %g2
437 add %o1, %g2, %o1
438 1: sethi %hi(1000000), %o2
439 srlx %o5, 32, %o5
440 or %o2, %lo(1000000), %o2
441 add %o5, %o1, %o5
442 cmp %o5, %o2
443 bl,a,pn %xcc, 1f
444 stx %o4, [%o0 + 0x0]
445 add %o4, 0x1, %o4
446 sub %o5, %o2, %o5
447 stx %o4, [%o0 + 0x0]
448 1: st %o5, [%o0 + 0x8]");
451 void do_settimeofday(struct timeval *tv)
453 cli();
455 tv->tv_usec -= do_gettimeoffset();
456 if(tv->tv_usec < 0) {
457 tv->tv_usec += 1000000;
458 tv->tv_sec--;
461 xtime = *tv;
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;
467 sti();
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. */
476 if (!regs)
477 return -1;
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 */
494 real_minutes %= 60;
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;
501 } else
502 return -1;
504 return 0;