1 /* $Id: time.c,v 1.59.2.1 2002/01/23 14:35:45 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/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>
29 #include <linux/ioport.h>
31 #include <asm/oplib.h>
32 #include <asm/segment.h>
33 #include <asm/timer.h>
34 #include <asm/mostek.h>
35 #include <asm/system.h>
38 #include <asm/idprom.h>
39 #include <asm/machines.h>
40 #include <asm/sun4paddr.h>
44 extern rwlock_t xtime_lock;
46 enum sparc_clock_type sp_clock_typ;
47 spinlock_t mostek_lock = SPIN_LOCK_UNLOCKED;
48 unsigned long mstk48t02_regs = 0UL;
49 static struct mostek48t08 *mstk48t08_regs = 0;
50 static int set_rtc_mmss(unsigned long);
51 static void sbus_do_settimeofday(struct timeval *tv);
54 struct intersil *intersil_clock;
55 #define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
58 #define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
61 #define intersil_start(intersil_reg) intersil_cmd(intersil_reg, \
62 ( INTERSIL_START | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
63 INTERSIL_INTR_ENABLE))
65 #define intersil_stop(intersil_reg) intersil_cmd(intersil_reg, \
66 ( INTERSIL_STOP | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
67 INTERSIL_INTR_ENABLE))
69 #define intersil_read_intr(intersil_reg, towhere) towhere = \
70 intersil_reg->int_intr_reg
74 static spinlock_t ticker_lock = SPIN_LOCK_UNLOCKED;
76 /* 32-bit Sparc specific profiling function. */
77 void sparc_do_profile(unsigned long pc, unsigned long o7)
79 if(prof_buffer && current->pid) {
81 extern int __copy_user_begin, __copy_user_end;
82 extern int __atomic_begin, __atomic_end;
83 extern int __bzero_begin, __bzero_end;
84 extern int __bitops_begin, __bitops_end;
86 if ((pc >= (unsigned long) &__copy_user_begin &&
87 pc < (unsigned long) &__copy_user_end) ||
88 (pc >= (unsigned long) &__atomic_begin &&
89 pc < (unsigned long) &__atomic_end) ||
90 (pc >= (unsigned long) &__bzero_begin &&
91 pc < (unsigned long) &__bzero_end) ||
92 (pc >= (unsigned long) &__bitops_begin &&
93 pc < (unsigned long) &__bitops_end))
96 pc -= (unsigned long) &_stext;
99 spin_lock(&ticker_lock);
103 prof_buffer[prof_len - 1]++;
104 spin_unlock(&ticker_lock);
108 __volatile__ unsigned int *master_l10_counter;
109 __volatile__ unsigned int *master_l10_limit;
112 * timer_interrupt() needs to keep up the real-time clock,
113 * as well as call the "do_timer()" routine every clocktick
115 void timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
117 /* last time the cmos clock got updated */
118 static long last_rtc_update;
122 sparc_do_profile(regs->pc, regs->u_regs[UREG_RETPC]);
126 if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
127 (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
129 intersil_read_intr(intersil_clock, temp);
130 /* re-enable the irq */
136 write_lock(&xtime_lock);
140 /* Determine when to update the Mostek clock. */
141 if ((time_status & STA_UNSYNC) == 0 &&
142 xtime.tv_sec > last_rtc_update + 660 &&
143 xtime.tv_usec >= 500000 - ((unsigned) tick) / 2 &&
144 xtime.tv_usec <= 500000 + ((unsigned) tick) / 2) {
145 if (set_rtc_mmss(xtime.tv_sec) == 0)
146 last_rtc_update = xtime.tv_sec;
148 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
150 write_unlock(&xtime_lock);
153 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
154 static void __init kick_start_clock(void)
156 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
160 prom_printf("CLOCK: Clock was stopped. Kick start ");
162 spin_lock_irq(&mostek_lock);
164 /* Turn on the kick start bit to start the oscillator. */
165 regs->creg |= MSTK_CREG_WRITE;
166 regs->sec &= ~MSTK_STOP;
167 regs->hour |= MSTK_KICK_START;
168 regs->creg &= ~MSTK_CREG_WRITE;
170 spin_unlock_irq(&mostek_lock);
172 /* Delay to allow the clock oscillator to start. */
173 sec = MSTK_REG_SEC(regs);
174 for (i = 0; i < 3; i++) {
175 while (sec == MSTK_REG_SEC(regs))
176 for (count = 0; count < 100000; count++)
183 spin_lock_irq(&mostek_lock);
185 /* Turn off kick start and set a "valid" time and date. */
186 regs->creg |= MSTK_CREG_WRITE;
187 regs->hour &= ~MSTK_KICK_START;
188 MSTK_SET_REG_SEC(regs,0);
189 MSTK_SET_REG_MIN(regs,0);
190 MSTK_SET_REG_HOUR(regs,0);
191 MSTK_SET_REG_DOW(regs,5);
192 MSTK_SET_REG_DOM(regs,1);
193 MSTK_SET_REG_MONTH(regs,8);
194 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
195 regs->creg &= ~MSTK_CREG_WRITE;
197 spin_unlock_irq(&mostek_lock);
199 /* Ensure the kick start bit is off. If it isn't, turn it off. */
200 while (regs->hour & MSTK_KICK_START) {
201 prom_printf("CLOCK: Kick start still on!\n");
203 spin_lock_irq(&mostek_lock);
204 regs->creg |= MSTK_CREG_WRITE;
205 regs->hour &= ~MSTK_KICK_START;
206 regs->creg &= ~MSTK_CREG_WRITE;
207 spin_unlock_irq(&mostek_lock);
210 prom_printf("CLOCK: Kick start procedure successful.\n");
213 /* Return nonzero if the clock chip battery is low. */
214 static __inline__ int has_low_battery(void)
216 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
217 unsigned char data1, data2;
219 spin_lock_irq(&mostek_lock);
220 data1 = regs->eeprom[0]; /* Read some data. */
221 regs->eeprom[0] = ~data1; /* Write back the complement. */
222 data2 = regs->eeprom[0]; /* Read back the complement. */
223 regs->eeprom[0] = data1; /* Restore the original value. */
224 spin_unlock_irq(&mostek_lock);
226 return (data1 == data2); /* Was the write blocked? */
229 /* Probe for the real time clock chip on Sun4 */
230 static __inline__ void sun4_clock_probe(void)
236 memset(&r, 0, sizeof(r));
237 if( idprom->id_machtype == (SM_SUN4 | SM_4_330) ) {
238 sp_clock_typ = MSTK48T02;
239 r.start = sun4_clock_physaddr;
240 mstk48t02_regs = sbus_ioremap(&r, 0,
241 sizeof(struct mostek48t02), 0);
242 mstk48t08_regs = 0; /* To catch weirdness */
243 intersil_clock = 0; /* just in case */
245 /* Kick start the clock if it is completely stopped. */
246 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
248 } else if( idprom->id_machtype == (SM_SUN4 | SM_4_260)) {
249 /* intersil setup code */
250 printk("Clock: INTERSIL at %8x ",sun4_clock_physaddr);
251 sp_clock_typ = INTERSIL;
252 r.start = sun4_clock_physaddr;
253 intersil_clock = (struct intersil *)
254 sbus_ioremap(&r, 0, sizeof(*intersil_clock), "intersil");
255 mstk48t02_regs = 0; /* just be sure */
256 mstk48t08_regs = 0; /* ditto */
257 /* initialise the clock */
259 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
261 intersil_start(intersil_clock);
263 intersil_read_intr(intersil_clock, temp);
264 while (!(temp & 0x80))
265 intersil_read_intr(intersil_clock, temp);
267 intersil_read_intr(intersil_clock, temp);
268 while (!(temp & 0x80))
269 intersil_read_intr(intersil_clock, temp);
271 intersil_stop(intersil_clock);
277 /* Probe for the mostek real time clock chip. */
278 static __inline__ void clock_probe(void)
280 struct linux_prom_registers clk_reg[2];
282 register int node, cpuunit, bootbus;
285 cpuunit = bootbus = 0;
286 memset(&r, 0, sizeof(r));
288 /* Determine the correct starting PROM node for the probe. */
289 node = prom_getchild(prom_root_node);
290 switch (sparc_cpu_model) {
294 node = prom_getchild(prom_searchsiblings(node, "obio"));
297 node = prom_getchild(bootbus = prom_searchsiblings(prom_getchild(cpuunit = prom_searchsiblings(node, "cpu-unit")), "bootbus"));
300 prom_printf("CLOCK: Unsupported architecture!\n");
304 /* Find the PROM node describing the real time clock. */
305 sp_clock_typ = MSTK_INVALID;
306 node = prom_searchsiblings(node,"eeprom");
308 prom_printf("CLOCK: No clock found!\n");
312 /* Get the model name and setup everything up. */
314 prom_getstring(node, "model", model, sizeof(model));
315 if (strcmp(model, "mk48t02") == 0) {
316 sp_clock_typ = MSTK48T02;
317 if (prom_getproperty(node, "reg", (char *) clk_reg, sizeof(clk_reg)) == -1) {
318 prom_printf("clock_probe: FAILED!\n");
321 if (sparc_cpu_model == sun4d)
322 prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
324 prom_apply_obio_ranges(clk_reg, 1);
325 /* Map the clock register io area read-only */
326 r.flags = clk_reg[0].which_io;
327 r.start = clk_reg[0].phys_addr;
328 mstk48t02_regs = sbus_ioremap(&r, 0,
329 sizeof(struct mostek48t02), "mk48t02");
330 mstk48t08_regs = 0; /* To catch weirdness */
331 } else if (strcmp(model, "mk48t08") == 0) {
332 sp_clock_typ = MSTK48T08;
333 if(prom_getproperty(node, "reg", (char *) clk_reg,
334 sizeof(clk_reg)) == -1) {
335 prom_printf("clock_probe: FAILED!\n");
338 if (sparc_cpu_model == sun4d)
339 prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
341 prom_apply_obio_ranges(clk_reg, 1);
342 /* Map the clock register io area read-only */
343 /* XXX r/o attribute is somewhere in r.flags */
344 r.flags = clk_reg[0].which_io;
345 r.start = clk_reg[0].phys_addr;
346 mstk48t08_regs = (struct mostek48t08 *) sbus_ioremap(&r, 0,
347 sizeof(struct mostek48t08), "mk48t08");
349 mstk48t02_regs = (unsigned long)&mstk48t08_regs->regs;
351 prom_printf("CLOCK: Unknown model name '%s'\n",model);
355 /* Report a low battery voltage condition. */
356 if (has_low_battery())
357 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
359 /* Kick start the clock if it is completely stopped. */
360 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
364 void __init sbus_time_init(void)
366 unsigned int year, mon, day, hour, min, sec;
367 struct mostek48t02 *mregs;
371 struct intersil *iregs;
374 BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
382 sparc_init_timers(timer_interrupt);
385 if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
387 mregs = (struct mostek48t02 *)mstk48t02_regs;
389 prom_printf("Something wrong, clock regs not mapped yet.\n");
392 spin_lock_irq(&mostek_lock);
393 mregs->creg |= MSTK_CREG_READ;
394 sec = MSTK_REG_SEC(mregs);
395 min = MSTK_REG_MIN(mregs);
396 hour = MSTK_REG_HOUR(mregs);
397 day = MSTK_REG_DOM(mregs);
398 mon = MSTK_REG_MONTH(mregs);
399 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
400 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
402 mregs->creg &= ~MSTK_CREG_READ;
403 spin_unlock_irq(&mostek_lock);
405 } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
406 /* initialise the intersil on sun4 */
408 iregs=intersil_clock;
410 prom_printf("Something wrong, clock regs not mapped yet.\n");
414 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
416 intersil_stop(iregs);
417 intersil_read_intr(intersil_clock, temp);
419 temp = iregs->clk.int_csec;
421 sec = iregs->clk.int_sec;
422 min = iregs->clk.int_min;
423 hour = iregs->clk.int_hour;
424 day = iregs->clk.int_day;
425 mon = iregs->clk.int_month;
426 year = MSTK_CVT_YEAR(iregs->clk.int_year);
429 intersil_start(iregs);
431 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
433 printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
437 /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
441 void __init time_init(void)
444 extern void pci_time_init(void);
445 if (pcic_present()) {
453 extern __inline__ unsigned long do_gettimeoffset(void)
455 struct tasklet_struct *t;
456 unsigned long offset = 0;
459 count = (*master_l10_counter >> 10) & 0x1fffff;
461 t = &bh_task_vec[TIMER_BH];
462 if (test_bit(TASKLET_STATE_SCHED, &t->state))
465 return offset + count;
468 /* This need not obtain the xtime_lock as it is coded in
469 * an implicitly SMP safe way already.
471 void do_gettimeofday(struct timeval *tv)
473 /* Load doubles must be used on xtime so that what we get
474 * is guarenteed to be atomic, this is why we can run this
475 * with interrupts on full blast. Don't touch this... -DaveM
477 __asm__ __volatile__(
478 "sethi %hi(master_l10_counter), %o1\n\t"
479 "ld [%o1 + %lo(master_l10_counter)], %g3\n\t"
480 "sethi %hi(xtime), %g2\n"
482 "ldd [%g2 + %lo(xtime)], %o4\n\t"
484 "ldd [%g2 + %lo(xtime)], %o2\n\t"
485 "xor %o4, %o2, %o2\n\t"
486 "xor %o5, %o3, %o3\n\t"
487 "orcc %o2, %o3, %g0\n\t"
491 " srl %o1, 0xa, %o1\n\t"
492 "sethi %hi(tick), %o3\n\t"
493 "ld [%o3 + %lo(tick)], %o3\n\t"
494 "sethi %hi(0x1fffff), %o2\n\t"
495 "or %o2, %lo(0x1fffff), %o2\n\t"
496 "add %o5, %o3, %o5\n\t"
497 "and %o1, %o2, %o1\n"
499 "add %o5, %o1, %o5\n\t"
500 "sethi %hi(1000000), %o2\n\t"
501 "or %o2, %lo(1000000), %o2\n\t"
504 " st %o4, [%o0 + 0x0]\n\t"
505 "add %o4, 0x1, %o4\n\t"
506 "sub %o5, %o2, %o5\n\t"
507 "st %o4, [%o0 + 0x0]\n"
509 "st %o5, [%o0 + 0x4]\n");
512 void do_settimeofday(struct timeval *tv)
514 write_lock_irq(&xtime_lock);
515 bus_do_settimeofday(tv);
516 write_unlock_irq(&xtime_lock);
519 static void sbus_do_settimeofday(struct timeval *tv)
521 tv->tv_usec -= do_gettimeoffset();
522 if(tv->tv_usec < 0) {
523 tv->tv_usec += 1000000;
527 time_adjust = 0; /* stop active adjtime() */
528 time_status |= STA_UNSYNC;
529 time_maxerror = NTP_PHASE_LIMIT;
530 time_esterror = NTP_PHASE_LIMIT;
534 * BUG: This routine does not handle hour overflow properly; it just
535 * sets the minutes. Usually you won't notice until after reboot!
537 static int set_rtc_mmss(unsigned long nowtime)
539 int real_seconds, real_minutes, mostek_minutes;
540 struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
543 struct intersil *iregs = intersil_clock;
547 /* Not having a register set can lead to trouble. */
553 temp = iregs->clk.int_csec;
555 mostek_minutes = iregs->clk.int_min;
557 real_seconds = nowtime % 60;
558 real_minutes = nowtime / 60;
559 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
560 real_minutes += 30; /* correct for half hour time zone */
563 if (abs(real_minutes - mostek_minutes) < 30) {
564 intersil_stop(iregs);
565 iregs->clk.int_sec=real_seconds;
566 iregs->clk.int_min=real_minutes;
567 intersil_start(iregs);
570 "set_rtc_mmss: can't update from %d to %d\n",
571 mostek_minutes, real_minutes);
580 spin_lock_irqsave(&mostek_lock, flags);
581 /* Read the current RTC minutes. */
582 regs->creg |= MSTK_CREG_READ;
583 mostek_minutes = MSTK_REG_MIN(regs);
584 regs->creg &= ~MSTK_CREG_READ;
587 * since we're only adjusting minutes and seconds,
588 * don't interfere with hour overflow. This avoids
589 * messing with unknown time zones but requires your
590 * RTC not to be off by more than 15 minutes
592 real_seconds = nowtime % 60;
593 real_minutes = nowtime / 60;
594 if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
595 real_minutes += 30; /* correct for half hour time zone */
598 if (abs(real_minutes - mostek_minutes) < 30) {
599 regs->creg |= MSTK_CREG_WRITE;
600 MSTK_SET_REG_SEC(regs,real_seconds);
601 MSTK_SET_REG_MIN(regs,real_minutes);
602 regs->creg &= ~MSTK_CREG_WRITE;
603 spin_unlock_irqrestore(&mostek_lock, flags);
606 spin_unlock_irqrestore(&mostek_lock, flags);