1 /* $Id: time.c,v 1.60 2002/01/23 14:33:55 davem 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/module.h>
21 #include <linux/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/param.h>
24 #include <linux/string.h>
26 #include <linux/interrupt.h>
27 #include <linux/time.h>
28 #include <linux/timex.h>
29 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/ioport.h>
32 #include <linux/profile.h>
34 #include <asm/oplib.h>
35 #include <asm/segment.h>
36 #include <asm/timer.h>
37 #include <asm/mostek.h>
38 #include <asm/system.h>
41 #include <asm/idprom.h>
42 #include <asm/machines.h>
43 #include <asm/sun4paddr.h>
47 extern unsigned long wall_jiffies;
49 u64 jiffies_64 = INITIAL_JIFFIES;
51 EXPORT_SYMBOL(jiffies_64);
53 spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
54 enum sparc_clock_type sp_clock_typ;
55 spinlock_t mostek_lock = SPIN_LOCK_UNLOCKED;
56 unsigned long mstk48t02_regs = 0UL;
57 static struct mostek48t08 *mstk48t08_regs = 0;
58 static int set_rtc_mmss(unsigned long);
59 static int sbus_do_settimeofday(struct timespec *tv);
62 struct intersil *intersil_clock;
63 #define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
66 #define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
69 #define intersil_start(intersil_reg) intersil_cmd(intersil_reg, \
70 ( INTERSIL_START | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
71 INTERSIL_INTR_ENABLE))
73 #define intersil_stop(intersil_reg) intersil_cmd(intersil_reg, \
74 ( INTERSIL_STOP | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
75 INTERSIL_INTR_ENABLE))
77 #define intersil_read_intr(intersil_reg, towhere) towhere = \
78 intersil_reg->int_intr_reg
82 static spinlock_t ticker_lock = SPIN_LOCK_UNLOCKED;
84 /* 32-bit Sparc specific profiling function. */
85 void sparc_do_profile(unsigned long pc, unsigned long o7)
87 if(prof_buffer && current->pid) {
89 extern int __copy_user_begin, __copy_user_end;
90 extern int __atomic_begin, __atomic_end;
91 extern int __bzero_begin, __bzero_end;
92 extern int __bitops_begin, __bitops_end;
94 if ((pc >= (unsigned long) &__copy_user_begin &&
95 pc < (unsigned long) &__copy_user_end) ||
96 (pc >= (unsigned long) &__atomic_begin &&
97 pc < (unsigned long) &__atomic_end) ||
98 (pc >= (unsigned long) &__bzero_begin &&
99 pc < (unsigned long) &__bzero_end) ||
100 (pc >= (unsigned long) &__bitops_begin &&
101 pc < (unsigned long) &__bitops_end))
104 pc -= (unsigned long) &_stext;
107 spin_lock(&ticker_lock);
111 prof_buffer[prof_len - 1]++;
112 spin_unlock(&ticker_lock);
116 __volatile__ unsigned int *master_l10_counter;
117 __volatile__ unsigned int *master_l10_limit;
120 * timer_interrupt() needs to keep up the real-time clock,
121 * as well as call the "do_timer()" routine every clocktick
124 #define TICK_SIZE (tick_nsec / 1000)
126 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
128 /* last time the cmos clock got updated */
129 static long last_rtc_update;
133 sparc_do_profile(regs->pc, regs->u_regs[UREG_RETPC]);
136 /* Protect counter clear so that do_gettimeoffset works */
137 write_seqlock(&xtime_lock);
139 if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
140 (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
142 intersil_read_intr(intersil_clock, temp);
143 /* re-enable the irq */
151 /* Determine when to update the Mostek clock. */
152 if ((time_status & STA_UNSYNC) == 0 &&
153 xtime.tv_sec > last_rtc_update + 660 &&
154 (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
155 (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
156 if (set_rtc_mmss(xtime.tv_sec) == 0)
157 last_rtc_update = xtime.tv_sec;
159 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
161 write_sequnlock(&xtime_lock);
166 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
167 static void __init kick_start_clock(void)
169 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
173 prom_printf("CLOCK: Clock was stopped. Kick start ");
175 spin_lock_irq(&mostek_lock);
177 /* Turn on the kick start bit to start the oscillator. */
178 regs->creg |= MSTK_CREG_WRITE;
179 regs->sec &= ~MSTK_STOP;
180 regs->hour |= MSTK_KICK_START;
181 regs->creg &= ~MSTK_CREG_WRITE;
183 spin_unlock_irq(&mostek_lock);
185 /* Delay to allow the clock oscillator to start. */
186 sec = MSTK_REG_SEC(regs);
187 for (i = 0; i < 3; i++) {
188 while (sec == MSTK_REG_SEC(regs))
189 for (count = 0; count < 100000; count++)
196 spin_lock_irq(&mostek_lock);
198 /* Turn off kick start and set a "valid" time and date. */
199 regs->creg |= MSTK_CREG_WRITE;
200 regs->hour &= ~MSTK_KICK_START;
201 MSTK_SET_REG_SEC(regs,0);
202 MSTK_SET_REG_MIN(regs,0);
203 MSTK_SET_REG_HOUR(regs,0);
204 MSTK_SET_REG_DOW(regs,5);
205 MSTK_SET_REG_DOM(regs,1);
206 MSTK_SET_REG_MONTH(regs,8);
207 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
208 regs->creg &= ~MSTK_CREG_WRITE;
210 spin_unlock_irq(&mostek_lock);
212 /* Ensure the kick start bit is off. If it isn't, turn it off. */
213 while (regs->hour & MSTK_KICK_START) {
214 prom_printf("CLOCK: Kick start still on!\n");
216 spin_lock_irq(&mostek_lock);
217 regs->creg |= MSTK_CREG_WRITE;
218 regs->hour &= ~MSTK_KICK_START;
219 regs->creg &= ~MSTK_CREG_WRITE;
220 spin_unlock_irq(&mostek_lock);
223 prom_printf("CLOCK: Kick start procedure successful.\n");
226 /* Return nonzero if the clock chip battery is low. */
227 static __inline__ int has_low_battery(void)
229 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
230 unsigned char data1, data2;
232 spin_lock_irq(&mostek_lock);
233 data1 = regs->eeprom[0]; /* Read some data. */
234 regs->eeprom[0] = ~data1; /* Write back the complement. */
235 data2 = regs->eeprom[0]; /* Read back the complement. */
236 regs->eeprom[0] = data1; /* Restore the original value. */
237 spin_unlock_irq(&mostek_lock);
239 return (data1 == data2); /* Was the write blocked? */
242 /* Probe for the real time clock chip on Sun4 */
243 static __inline__ void sun4_clock_probe(void)
249 memset(&r, 0, sizeof(r));
250 if( idprom->id_machtype == (SM_SUN4 | SM_4_330) ) {
251 sp_clock_typ = MSTK48T02;
252 r.start = sun4_clock_physaddr;
253 mstk48t02_regs = sbus_ioremap(&r, 0,
254 sizeof(struct mostek48t02), 0);
255 mstk48t08_regs = 0; /* To catch weirdness */
256 intersil_clock = 0; /* just in case */
258 /* Kick start the clock if it is completely stopped. */
259 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
261 } else if( idprom->id_machtype == (SM_SUN4 | SM_4_260)) {
262 /* intersil setup code */
263 printk("Clock: INTERSIL at %8x ",sun4_clock_physaddr);
264 sp_clock_typ = INTERSIL;
265 r.start = sun4_clock_physaddr;
266 intersil_clock = (struct intersil *)
267 sbus_ioremap(&r, 0, sizeof(*intersil_clock), "intersil");
268 mstk48t02_regs = 0; /* just be sure */
269 mstk48t08_regs = 0; /* ditto */
270 /* initialise the clock */
272 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
274 intersil_start(intersil_clock);
276 intersil_read_intr(intersil_clock, temp);
277 while (!(temp & 0x80))
278 intersil_read_intr(intersil_clock, temp);
280 intersil_read_intr(intersil_clock, temp);
281 while (!(temp & 0x80))
282 intersil_read_intr(intersil_clock, temp);
284 intersil_stop(intersil_clock);
290 /* Probe for the mostek real time clock chip. */
291 static __inline__ void clock_probe(void)
293 struct linux_prom_registers clk_reg[2];
295 register int node, cpuunit, bootbus;
298 cpuunit = bootbus = 0;
299 memset(&r, 0, sizeof(r));
301 /* Determine the correct starting PROM node for the probe. */
302 node = prom_getchild(prom_root_node);
303 switch (sparc_cpu_model) {
307 node = prom_getchild(prom_searchsiblings(node, "obio"));
310 node = prom_getchild(bootbus = prom_searchsiblings(prom_getchild(cpuunit = prom_searchsiblings(node, "cpu-unit")), "bootbus"));
313 prom_printf("CLOCK: Unsupported architecture!\n");
317 /* Find the PROM node describing the real time clock. */
318 sp_clock_typ = MSTK_INVALID;
319 node = prom_searchsiblings(node,"eeprom");
321 prom_printf("CLOCK: No clock found!\n");
325 /* Get the model name and setup everything up. */
327 prom_getstring(node, "model", model, sizeof(model));
328 if (strcmp(model, "mk48t02") == 0) {
329 sp_clock_typ = MSTK48T02;
330 if (prom_getproperty(node, "reg", (char *) clk_reg, sizeof(clk_reg)) == -1) {
331 prom_printf("clock_probe: FAILED!\n");
334 if (sparc_cpu_model == sun4d)
335 prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
337 prom_apply_obio_ranges(clk_reg, 1);
338 /* Map the clock register io area read-only */
339 r.flags = clk_reg[0].which_io;
340 r.start = clk_reg[0].phys_addr;
341 mstk48t02_regs = sbus_ioremap(&r, 0,
342 sizeof(struct mostek48t02), "mk48t02");
343 mstk48t08_regs = 0; /* To catch weirdness */
344 } else if (strcmp(model, "mk48t08") == 0) {
345 sp_clock_typ = MSTK48T08;
346 if(prom_getproperty(node, "reg", (char *) clk_reg,
347 sizeof(clk_reg)) == -1) {
348 prom_printf("clock_probe: FAILED!\n");
351 if (sparc_cpu_model == sun4d)
352 prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
354 prom_apply_obio_ranges(clk_reg, 1);
355 /* Map the clock register io area read-only */
356 /* XXX r/o attribute is somewhere in r.flags */
357 r.flags = clk_reg[0].which_io;
358 r.start = clk_reg[0].phys_addr;
359 mstk48t08_regs = (struct mostek48t08 *) sbus_ioremap(&r, 0,
360 sizeof(struct mostek48t08), "mk48t08");
362 mstk48t02_regs = (unsigned long)&mstk48t08_regs->regs;
364 prom_printf("CLOCK: Unknown model name '%s'\n",model);
368 /* Report a low battery voltage condition. */
369 if (has_low_battery())
370 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
372 /* Kick start the clock if it is completely stopped. */
373 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
377 void __init sbus_time_init(void)
379 unsigned int year, mon, day, hour, min, sec;
380 struct mostek48t02 *mregs;
384 struct intersil *iregs;
387 BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
395 sparc_init_timers(timer_interrupt);
398 if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
400 mregs = (struct mostek48t02 *)mstk48t02_regs;
402 prom_printf("Something wrong, clock regs not mapped yet.\n");
405 spin_lock_irq(&mostek_lock);
406 mregs->creg |= MSTK_CREG_READ;
407 sec = MSTK_REG_SEC(mregs);
408 min = MSTK_REG_MIN(mregs);
409 hour = MSTK_REG_HOUR(mregs);
410 day = MSTK_REG_DOM(mregs);
411 mon = MSTK_REG_MONTH(mregs);
412 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
413 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
414 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
415 set_normalized_timespec(&wall_to_monotonic,
416 -xtime.tv_sec, -xtime.tv_nsec);
417 mregs->creg &= ~MSTK_CREG_READ;
418 spin_unlock_irq(&mostek_lock);
420 } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
421 /* initialise the intersil on sun4 */
423 iregs=intersil_clock;
425 prom_printf("Something wrong, clock regs not mapped yet.\n");
429 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
431 intersil_stop(iregs);
432 intersil_read_intr(intersil_clock, temp);
434 temp = iregs->clk.int_csec;
436 sec = iregs->clk.int_sec;
437 min = iregs->clk.int_min;
438 hour = iregs->clk.int_hour;
439 day = iregs->clk.int_day;
440 mon = iregs->clk.int_month;
441 year = MSTK_CVT_YEAR(iregs->clk.int_year);
444 intersil_start(iregs);
446 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
447 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
448 set_normalized_timespec(&wall_to_monotonic,
449 -xtime.tv_sec, -xtime.tv_nsec);
450 printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
454 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
458 void __init time_init(void)
461 extern void pci_time_init(void);
462 if (pcic_present()) {
470 extern __inline__ unsigned long do_gettimeoffset(void)
472 return (*master_l10_counter >> 10) & 0x1fffff;
476 * Returns nanoseconds
477 * XXX This is a suboptimal implementation.
479 unsigned long long sched_clock(void)
481 return (unsigned long long)jiffies * (1000000000 / HZ);
484 /* Ok, my cute asm atomicity trick doesn't work anymore.
485 * There are just too many variables that need to be protected
486 * now (both members of xtime, wall_jiffies, et al.)
488 void do_gettimeofday(struct timeval *tv)
492 unsigned long usec, sec;
493 unsigned long max_ntp_tick = tick_usec - tickadj;
498 seq = read_seqbegin_irqsave(&xtime_lock, flags);
499 usec = do_gettimeoffset();
500 lost = jiffies - wall_jiffies;
503 * If time_adjust is negative then NTP is slowing the clock
504 * so make sure not to go into next possible interval.
505 * Better to lose some accuracy than have time go backwards..
507 if (unlikely(time_adjust < 0)) {
508 usec = min(usec, max_ntp_tick);
511 usec += lost * max_ntp_tick;
513 else if (unlikely(lost))
514 usec += lost * tick_usec;
517 usec += (xtime.tv_nsec / 1000);
518 } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
520 while (usec >= 1000000) {
529 EXPORT_SYMBOL(do_gettimeofday);
531 int do_settimeofday(struct timespec *tv)
535 write_seqlock_irq(&xtime_lock);
536 ret = bus_do_settimeofday(tv);
537 write_sequnlock_irq(&xtime_lock);
542 EXPORT_SYMBOL(do_settimeofday);
544 static int sbus_do_settimeofday(struct timespec *tv)
546 time_t wtm_sec, sec = tv->tv_sec;
547 long wtm_nsec, nsec = tv->tv_nsec;
549 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
553 * This is revolting. We need to set "xtime" correctly. However, the
554 * value in this location is the value at the most recent update of
555 * wall time. Discover what correction gettimeofday() would have
556 * made, and then undo it!
558 nsec -= 1000 * (do_gettimeoffset() +
559 (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ));
561 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
562 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
564 set_normalized_timespec(&xtime, sec, nsec);
565 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
567 time_adjust = 0; /* stop active adjtime() */
568 time_status |= STA_UNSYNC;
569 time_maxerror = NTP_PHASE_LIMIT;
570 time_esterror = NTP_PHASE_LIMIT;
575 * BUG: This routine does not handle hour overflow properly; it just
576 * sets the minutes. Usually you won't notice until after reboot!
578 static int set_rtc_mmss(unsigned long nowtime)
580 int real_seconds, real_minutes, mostek_minutes;
581 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
584 struct intersil *iregs = intersil_clock;
588 /* Not having a register set can lead to trouble. */
594 temp = iregs->clk.int_csec;
596 mostek_minutes = iregs->clk.int_min;
598 real_seconds = nowtime % 60;
599 real_minutes = nowtime / 60;
600 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
601 real_minutes += 30; /* correct for half hour time zone */
604 if (abs(real_minutes - mostek_minutes) < 30) {
605 intersil_stop(iregs);
606 iregs->clk.int_sec=real_seconds;
607 iregs->clk.int_min=real_minutes;
608 intersil_start(iregs);
611 "set_rtc_mmss: can't update from %d to %d\n",
612 mostek_minutes, real_minutes);
621 spin_lock_irqsave(&mostek_lock, flags);
622 /* Read the current RTC minutes. */
623 regs->creg |= MSTK_CREG_READ;
624 mostek_minutes = MSTK_REG_MIN(regs);
625 regs->creg &= ~MSTK_CREG_READ;
628 * since we're only adjusting minutes and seconds,
629 * don't interfere with hour overflow. This avoids
630 * messing with unknown time zones but requires your
631 * RTC not to be off by more than 15 minutes
633 real_seconds = nowtime % 60;
634 real_minutes = nowtime / 60;
635 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
636 real_minutes += 30; /* correct for half hour time zone */
639 if (abs(real_minutes - mostek_minutes) < 30) {
640 regs->creg |= MSTK_CREG_WRITE;
641 MSTK_SET_REG_SEC(regs,real_seconds);
642 MSTK_SET_REG_MIN(regs,real_minutes);
643 regs->creg &= ~MSTK_CREG_WRITE;
644 spin_unlock_irqrestore(&mostek_lock, flags);
647 spin_unlock_irqrestore(&mostek_lock, flags);