vserver 1.9.3
[linux-2.6.git] / arch / sparc / kernel / time.c
1 /* $Id: time.c,v 1.60 2002/01/23 14:33:55 davem Exp $
2  * linux/arch/sparc/kernel/time.c
3  *
4  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
6  *
7  * Chris Davis (cdavis@cois.on.ca) 03/27/1998
8  * Added support for the intersil on the sun4/4200
9  *
10  * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
11  * Support for MicroSPARC-IIep, PCI CPU.
12  *
13  * This file handles the Sparc specific time handling details.
14  *
15  * 1997-09-10   Updated NTP code according to technical memorandum Jan '96
16  *              "A Kernel Model for Precision Timekeeping" by Dave Mills
17  */
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>
25 #include <linux/mm.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>
33
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>
39 #include <asm/irq.h>
40 #include <asm/io.h>
41 #include <asm/idprom.h>
42 #include <asm/machines.h>
43 #include <asm/sun4paddr.h>
44 #include <asm/page.h>
45 #include <asm/pcic.h>
46
47 extern unsigned long wall_jiffies;
48
49 u64 jiffies_64 = INITIAL_JIFFIES;
50
51 EXPORT_SYMBOL(jiffies_64);
52
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 = NULL;
58 static int set_rtc_mmss(unsigned long);
59 static int sbus_do_settimeofday(struct timespec *tv);
60
61 #ifdef CONFIG_SUN4
62 struct intersil *intersil_clock;
63 #define intersil_cmd(intersil_reg, intsil_cmd) intersil_reg->int_cmd_reg = \
64         (intsil_cmd)
65
66 #define intersil_intr(intersil_reg, intsil_cmd) intersil_reg->int_intr_reg = \
67         (intsil_cmd)
68
69 #define intersil_start(intersil_reg) intersil_cmd(intersil_reg, \
70         ( INTERSIL_START | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
71           INTERSIL_INTR_ENABLE))
72
73 #define intersil_stop(intersil_reg) intersil_cmd(intersil_reg, \
74         ( INTERSIL_STOP | INTERSIL_32K | INTERSIL_NORMAL | INTERSIL_24H |\
75           INTERSIL_INTR_ENABLE))
76
77 #define intersil_read_intr(intersil_reg, towhere) towhere = \
78         intersil_reg->int_intr_reg
79
80 #endif
81
82 unsigned long profile_pc(struct pt_regs *regs)
83 {
84         extern char __copy_user_begin[], __copy_user_end[];
85         extern char __atomic_begin[], __atomic_end[];
86         extern char __bzero_begin[], __bzero_end[];
87         extern char __bitops_begin[], __bitops_end[];
88
89         unsigned long pc = regs->pc;
90
91         if (in_lock_functions(pc) ||
92             (pc >= (unsigned long) __copy_user_begin &&
93              pc < (unsigned long) __copy_user_end) ||
94             (pc >= (unsigned long) __atomic_begin &&
95              pc < (unsigned long) __atomic_end) ||
96             (pc >= (unsigned long) __bzero_begin &&
97              pc < (unsigned long) __bzero_end) ||
98             (pc >= (unsigned long) __bitops_begin &&
99              pc < (unsigned long) __bitops_end))
100                 pc = regs->u_regs[UREG_RETPC];
101         return pc;
102 }
103
104 __volatile__ unsigned int *master_l10_counter;
105 __volatile__ unsigned int *master_l10_limit;
106
107 /*
108  * timer_interrupt() needs to keep up the real-time clock,
109  * as well as call the "do_timer()" routine every clocktick
110  */
111
112 #define TICK_SIZE (tick_nsec / 1000)
113
114 irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
115 {
116         /* last time the cmos clock got updated */
117         static long last_rtc_update;
118
119 #ifndef CONFIG_SMP
120         profile_tick(CPU_PROFILING, regs);
121 #endif
122
123         /* Protect counter clear so that do_gettimeoffset works */
124         write_seqlock(&xtime_lock);
125 #ifdef CONFIG_SUN4
126         if((idprom->id_machtype == (SM_SUN4 | SM_4_260)) ||
127            (idprom->id_machtype == (SM_SUN4 | SM_4_110))) {
128                 int temp;
129                 intersil_read_intr(intersil_clock, temp);
130                 /* re-enable the irq */
131                 enable_pil_irq(10);
132         }
133 #endif
134         clear_clock_irq();
135
136         do_timer(regs);
137
138         /* Determine when to update the Mostek clock. */
139         if ((time_status & STA_UNSYNC) == 0 &&
140             xtime.tv_sec > last_rtc_update + 660 &&
141             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
142             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
143           if (set_rtc_mmss(xtime.tv_sec) == 0)
144             last_rtc_update = xtime.tv_sec;
145           else
146             last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
147         }
148         write_sequnlock(&xtime_lock);
149
150         return IRQ_HANDLED;
151 }
152
153 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
154 static void __init kick_start_clock(void)
155 {
156         struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
157         unsigned char sec;
158         int i, count;
159
160         prom_printf("CLOCK: Clock was stopped. Kick start ");
161
162         spin_lock_irq(&mostek_lock);
163
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;
169
170         spin_unlock_irq(&mostek_lock);
171
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++)
177                                 /* nothing */ ;
178                 prom_printf(".");
179                 sec = regs->sec;
180         }
181         prom_printf("\n");
182
183         spin_lock_irq(&mostek_lock);
184
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;
196
197         spin_unlock_irq(&mostek_lock);
198
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");
202
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);
208         }
209
210         prom_printf("CLOCK: Kick start procedure successful.\n");
211 }
212
213 /* Return nonzero if the clock chip battery is low. */
214 static __inline__ int has_low_battery(void)
215 {
216         struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
217         unsigned char data1, data2;
218
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);
225
226         return (data1 == data2);        /* Was the write blocked? */
227 }
228
229 /* Probe for the real time clock chip on Sun4 */
230 static __inline__ void sun4_clock_probe(void)
231 {
232 #ifdef CONFIG_SUN4
233         int temp;
234         struct resource r;
235
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), NULL);
242                 mstk48t08_regs = NULL;  /* To catch weirdness */
243                 intersil_clock = NULL;  /* just in case */
244
245                 /* Kick start the clock if it is completely stopped. */
246                 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
247                         kick_start_clock();
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 = NULL;  /* ditto */
257                 /* initialise the clock */
258
259                 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
260
261                 intersil_start(intersil_clock);
262
263                 intersil_read_intr(intersil_clock, temp);
264                 while (!(temp & 0x80))
265                         intersil_read_intr(intersil_clock, temp);
266
267                 intersil_read_intr(intersil_clock, temp);
268                 while (!(temp & 0x80))
269                         intersil_read_intr(intersil_clock, temp);
270
271                 intersil_stop(intersil_clock);
272
273         }
274 #endif
275 }
276
277 /* Probe for the mostek real time clock chip. */
278 static __inline__ void clock_probe(void)
279 {
280         struct linux_prom_registers clk_reg[2];
281         char model[128];
282         register int node, cpuunit, bootbus;
283         struct resource r;
284
285         cpuunit = bootbus = 0;
286         memset(&r, 0, sizeof(r));
287
288         /* Determine the correct starting PROM node for the probe. */
289         node = prom_getchild(prom_root_node);
290         switch (sparc_cpu_model) {
291         case sun4c:
292                 break;
293         case sun4m:
294                 node = prom_getchild(prom_searchsiblings(node, "obio"));
295                 break;
296         case sun4d:
297                 node = prom_getchild(bootbus = prom_searchsiblings(prom_getchild(cpuunit = prom_searchsiblings(node, "cpu-unit")), "bootbus"));
298                 break;
299         default:
300                 prom_printf("CLOCK: Unsupported architecture!\n");
301                 prom_halt();
302         }
303
304         /* Find the PROM node describing the real time clock. */
305         sp_clock_typ = MSTK_INVALID;
306         node = prom_searchsiblings(node,"eeprom");
307         if (!node) {
308                 prom_printf("CLOCK: No clock found!\n");
309                 prom_halt();
310         }
311
312         /* Get the model name and setup everything up. */
313         model[0] = '\0';
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");
319                         prom_halt();
320                 }
321                 if (sparc_cpu_model == sun4d)
322                         prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
323                 else
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 = NULL;  /* 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");
336                         prom_halt();
337                 }
338                 if (sparc_cpu_model == sun4d)
339                         prom_apply_generic_ranges (bootbus, cpuunit, clk_reg, 1);
340                 else
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");
348
349                 mstk48t02_regs = (unsigned long)&mstk48t08_regs->regs;
350         } else {
351                 prom_printf("CLOCK: Unknown model name '%s'\n",model);
352                 prom_halt();
353         }
354
355         /* Report a low battery voltage condition. */
356         if (has_low_battery())
357                 printk(KERN_CRIT "NVRAM: Low battery voltage!\n");
358
359         /* Kick start the clock if it is completely stopped. */
360         if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
361                 kick_start_clock();
362 }
363
364 void __init sbus_time_init(void)
365 {
366         unsigned int year, mon, day, hour, min, sec;
367         struct mostek48t02 *mregs;
368
369 #ifdef CONFIG_SUN4
370         int temp;
371         struct intersil *iregs;
372 #endif
373
374         BTFIXUPSET_CALL(bus_do_settimeofday, sbus_do_settimeofday, BTFIXUPCALL_NORM);
375         btfixup();
376
377         if (ARCH_SUN4)
378                 sun4_clock_probe();
379         else
380                 clock_probe();
381
382         sparc_init_timers(timer_interrupt);
383         
384 #ifdef CONFIG_SUN4
385         if(idprom->id_machtype == (SM_SUN4 | SM_4_330)) {
386 #endif
387         mregs = (struct mostek48t02 *)mstk48t02_regs;
388         if(!mregs) {
389                 prom_printf("Something wrong, clock regs not mapped yet.\n");
390                 prom_halt();
391         }               
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);
401         xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
402         set_normalized_timespec(&wall_to_monotonic,
403                                 -xtime.tv_sec, -xtime.tv_nsec);
404         mregs->creg &= ~MSTK_CREG_READ;
405         spin_unlock_irq(&mostek_lock);
406 #ifdef CONFIG_SUN4
407         } else if(idprom->id_machtype == (SM_SUN4 | SM_4_260) ) {
408                 /* initialise the intersil on sun4 */
409
410                 iregs=intersil_clock;
411                 if(!iregs) {
412                         prom_printf("Something wrong, clock regs not mapped yet.\n");
413                         prom_halt();
414                 }
415
416                 intersil_intr(intersil_clock,INTERSIL_INT_100HZ);
417                 disable_pil_irq(10);
418                 intersil_stop(iregs);
419                 intersil_read_intr(intersil_clock, temp);
420
421                 temp = iregs->clk.int_csec;
422
423                 sec = iregs->clk.int_sec;
424                 min = iregs->clk.int_min;
425                 hour = iregs->clk.int_hour;
426                 day = iregs->clk.int_day;
427                 mon = iregs->clk.int_month;
428                 year = MSTK_CVT_YEAR(iregs->clk.int_year);
429
430                 enable_pil_irq(10);
431                 intersil_start(iregs);
432
433                 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
434                 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
435                 set_normalized_timespec(&wall_to_monotonic,
436                                        -xtime.tv_sec, -xtime.tv_nsec);
437                 printk("%u/%u/%u %u:%u:%u\n",day,mon,year,hour,min,sec);
438         }
439 #endif
440
441         /* Now that OBP ticker has been silenced, it is safe to enable IRQ. */
442         local_irq_enable();
443 }
444
445 void __init time_init(void)
446 {
447 #ifdef CONFIG_PCI
448         extern void pci_time_init(void);
449         if (pcic_present()) {
450                 pci_time_init();
451                 return;
452         }
453 #endif
454         sbus_time_init();
455 }
456
457 extern __inline__ unsigned long do_gettimeoffset(void)
458 {
459         return (*master_l10_counter >> 10) & 0x1fffff;
460 }
461
462 /*
463  * Returns nanoseconds
464  * XXX This is a suboptimal implementation.
465  */
466 unsigned long long sched_clock(void)
467 {
468         return (unsigned long long)jiffies * (1000000000 / HZ);
469 }
470
471 /* Ok, my cute asm atomicity trick doesn't work anymore.
472  * There are just too many variables that need to be protected
473  * now (both members of xtime, wall_jiffies, et al.)
474  */
475 void do_gettimeofday(struct timeval *tv)
476 {
477         unsigned long flags;
478         unsigned long seq;
479         unsigned long usec, sec;
480         unsigned long max_ntp_tick = tick_usec - tickadj;
481
482         do {
483                 unsigned long lost;
484
485                 seq = read_seqbegin_irqsave(&xtime_lock, flags);
486                 usec = do_gettimeoffset();
487                 lost = jiffies - wall_jiffies;
488
489                 /*
490                  * If time_adjust is negative then NTP is slowing the clock
491                  * so make sure not to go into next possible interval.
492                  * Better to lose some accuracy than have time go backwards..
493                  */
494                 if (unlikely(time_adjust < 0)) {
495                         usec = min(usec, max_ntp_tick);
496
497                         if (lost)
498                                 usec += lost * max_ntp_tick;
499                 }
500                 else if (unlikely(lost))
501                         usec += lost * tick_usec;
502
503                 sec = xtime.tv_sec;
504                 usec += (xtime.tv_nsec / 1000);
505         } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
506
507         while (usec >= 1000000) {
508                 usec -= 1000000;
509                 sec++;
510         }
511
512         tv->tv_sec = sec;
513         tv->tv_usec = usec;
514 }
515
516 EXPORT_SYMBOL(do_gettimeofday);
517
518 int do_settimeofday(struct timespec *tv)
519 {
520         int ret;
521
522         write_seqlock_irq(&xtime_lock);
523         ret = bus_do_settimeofday(tv);
524         write_sequnlock_irq(&xtime_lock);
525         clock_was_set();
526         return ret;
527 }
528
529 EXPORT_SYMBOL(do_settimeofday);
530
531 static int sbus_do_settimeofday(struct timespec *tv)
532 {
533         time_t wtm_sec, sec = tv->tv_sec;
534         long wtm_nsec, nsec = tv->tv_nsec;
535
536         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
537                 return -EINVAL;
538
539         /*
540          * This is revolting. We need to set "xtime" correctly. However, the
541          * value in this location is the value at the most recent update of
542          * wall time.  Discover what correction gettimeofday() would have
543          * made, and then undo it!
544          */
545         nsec -= 1000 * (do_gettimeoffset() +
546                         (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ));
547
548         wtm_sec  = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
549         wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
550
551         set_normalized_timespec(&xtime, sec, nsec);
552         set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
553
554         time_adjust = 0;                /* stop active adjtime() */
555         time_status |= STA_UNSYNC;
556         time_maxerror = NTP_PHASE_LIMIT;
557         time_esterror = NTP_PHASE_LIMIT;
558         return 0;
559 }
560
561 /*
562  * BUG: This routine does not handle hour overflow properly; it just
563  *      sets the minutes. Usually you won't notice until after reboot!
564  */
565 static int set_rtc_mmss(unsigned long nowtime)
566 {
567         int real_seconds, real_minutes, mostek_minutes;
568         struct mostek48t02 *regs = (struct mostek48t02 *)mstk48t02_regs;
569         unsigned long flags;
570 #ifdef CONFIG_SUN4
571         struct intersil *iregs = intersil_clock;
572         int temp;
573 #endif
574
575         /* Not having a register set can lead to trouble. */
576         if (!regs) {
577 #ifdef CONFIG_SUN4
578                 if(!iregs)
579                 return -1;
580                 else {
581                         temp = iregs->clk.int_csec;
582
583                         mostek_minutes = iregs->clk.int_min;
584
585                         real_seconds = nowtime % 60;
586                         real_minutes = nowtime / 60;
587                         if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
588                                 real_minutes += 30;     /* correct for half hour time zone */
589                         real_minutes %= 60;
590
591                         if (abs(real_minutes - mostek_minutes) < 30) {
592                                 intersil_stop(iregs);
593                                 iregs->clk.int_sec=real_seconds;
594                                 iregs->clk.int_min=real_minutes;
595                                 intersil_start(iregs);
596                         } else {
597                                 printk(KERN_WARNING
598                                "set_rtc_mmss: can't update from %d to %d\n",
599                                        mostek_minutes, real_minutes);
600                                 return -1;
601                         }
602                         
603                         return 0;
604                 }
605 #endif
606         }
607
608         spin_lock_irqsave(&mostek_lock, flags);
609         /* Read the current RTC minutes. */
610         regs->creg |= MSTK_CREG_READ;
611         mostek_minutes = MSTK_REG_MIN(regs);
612         regs->creg &= ~MSTK_CREG_READ;
613
614         /*
615          * since we're only adjusting minutes and seconds,
616          * don't interfere with hour overflow. This avoids
617          * messing with unknown time zones but requires your
618          * RTC not to be off by more than 15 minutes
619          */
620         real_seconds = nowtime % 60;
621         real_minutes = nowtime / 60;
622         if (((abs(real_minutes - mostek_minutes) + 15)/30) & 1)
623                 real_minutes += 30;     /* correct for half hour time zone */
624         real_minutes %= 60;
625
626         if (abs(real_minutes - mostek_minutes) < 30) {
627                 regs->creg |= MSTK_CREG_WRITE;
628                 MSTK_SET_REG_SEC(regs,real_seconds);
629                 MSTK_SET_REG_MIN(regs,real_minutes);
630                 regs->creg &= ~MSTK_CREG_WRITE;
631                 spin_unlock_irqrestore(&mostek_lock, flags);
632                 return 0;
633         } else {
634                 spin_unlock_irqrestore(&mostek_lock, flags);
635                 return -1;
636         }
637 }