Merge branch 'audit.b3' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit...
[pandora-kernel.git] / arch / sparc64 / kernel / time.c
1 /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $
2  * time.c: UltraSparc timer and TOD clock support.
3  *
4  * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1998 Eddie C. Dost   (ecd@skynet.be)
6  *
7  * Based largely on code which is:
8  *
9  * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
10  */
11
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/param.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/interrupt.h>
21 #include <linux/time.h>
22 #include <linux/timex.h>
23 #include <linux/init.h>
24 #include <linux/ioport.h>
25 #include <linux/mc146818rtc.h>
26 #include <linux/delay.h>
27 #include <linux/profile.h>
28 #include <linux/bcd.h>
29 #include <linux/jiffies.h>
30 #include <linux/cpufreq.h>
31 #include <linux/percpu.h>
32 #include <linux/profile.h>
33 #include <linux/miscdevice.h>
34 #include <linux/rtc.h>
35
36 #include <asm/oplib.h>
37 #include <asm/mostek.h>
38 #include <asm/timer.h>
39 #include <asm/irq.h>
40 #include <asm/io.h>
41 #include <asm/sbus.h>
42 #include <asm/fhc.h>
43 #include <asm/pbm.h>
44 #include <asm/ebus.h>
45 #include <asm/isa.h>
46 #include <asm/starfire.h>
47 #include <asm/smp.h>
48 #include <asm/sections.h>
49 #include <asm/cpudata.h>
50 #include <asm/uaccess.h>
51
52 DEFINE_SPINLOCK(mostek_lock);
53 DEFINE_SPINLOCK(rtc_lock);
54 void __iomem *mstk48t02_regs = NULL;
55 #ifdef CONFIG_PCI
56 unsigned long ds1287_regs = 0UL;
57 #endif
58
59 extern unsigned long wall_jiffies;
60
61 static void __iomem *mstk48t08_regs;
62 static void __iomem *mstk48t59_regs;
63
64 static int set_rtc_mmss(unsigned long);
65
66 #define TICK_PRIV_BIT   (1UL << 63)
67
68 #ifdef CONFIG_SMP
69 unsigned long profile_pc(struct pt_regs *regs)
70 {
71         unsigned long pc = instruction_pointer(regs);
72
73         if (in_lock_functions(pc))
74                 return regs->u_regs[UREG_RETPC];
75         return pc;
76 }
77 EXPORT_SYMBOL(profile_pc);
78 #endif
79
80 static void tick_disable_protection(void)
81 {
82         /* Set things up so user can access tick register for profiling
83          * purposes.  Also workaround BB_ERRATA_1 by doing a dummy
84          * read back of %tick after writing it.
85          */
86         __asm__ __volatile__(
87         "       ba,pt   %%xcc, 1f\n"
88         "        nop\n"
89         "       .align  64\n"
90         "1:     rd      %%tick, %%g2\n"
91         "       add     %%g2, 6, %%g2\n"
92         "       andn    %%g2, %0, %%g2\n"
93         "       wrpr    %%g2, 0, %%tick\n"
94         "       rdpr    %%tick, %%g0"
95         : /* no outputs */
96         : "r" (TICK_PRIV_BIT)
97         : "g2");
98 }
99
100 static void tick_init_tick(unsigned long offset)
101 {
102         tick_disable_protection();
103
104         __asm__ __volatile__(
105         "       rd      %%tick, %%g1\n"
106         "       andn    %%g1, %1, %%g1\n"
107         "       ba,pt   %%xcc, 1f\n"
108         "        add    %%g1, %0, %%g1\n"
109         "       .align  64\n"
110         "1:     wr      %%g1, 0x0, %%tick_cmpr\n"
111         "       rd      %%tick_cmpr, %%g0"
112         : /* no outputs */
113         : "r" (offset), "r" (TICK_PRIV_BIT)
114         : "g1");
115 }
116
117 static unsigned long tick_get_tick(void)
118 {
119         unsigned long ret;
120
121         __asm__ __volatile__("rd        %%tick, %0\n\t"
122                              "mov       %0, %0"
123                              : "=r" (ret));
124
125         return ret & ~TICK_PRIV_BIT;
126 }
127
128 static unsigned long tick_get_compare(void)
129 {
130         unsigned long ret;
131
132         __asm__ __volatile__("rd        %%tick_cmpr, %0\n\t"
133                              "mov       %0, %0"
134                              : "=r" (ret));
135
136         return ret;
137 }
138
139 static unsigned long tick_add_compare(unsigned long adj)
140 {
141         unsigned long new_compare;
142
143         /* Workaround for Spitfire Errata (#54 I think??), I discovered
144          * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
145          * number 103640.
146          *
147          * On Blackbird writes to %tick_cmpr can fail, the
148          * workaround seems to be to execute the wr instruction
149          * at the start of an I-cache line, and perform a dummy
150          * read back from %tick_cmpr right after writing to it. -DaveM
151          */
152         __asm__ __volatile__("rd        %%tick_cmpr, %0\n\t"
153                              "ba,pt     %%xcc, 1f\n\t"
154                              " add      %0, %1, %0\n\t"
155                              ".align    64\n"
156                              "1:\n\t"
157                              "wr        %0, 0, %%tick_cmpr\n\t"
158                              "rd        %%tick_cmpr, %%g0"
159                              : "=&r" (new_compare)
160                              : "r" (adj));
161
162         return new_compare;
163 }
164
165 static unsigned long tick_add_tick(unsigned long adj, unsigned long offset)
166 {
167         unsigned long new_tick, tmp;
168
169         /* Also need to handle Blackbird bug here too. */
170         __asm__ __volatile__("rd        %%tick, %0\n\t"
171                              "add       %0, %2, %0\n\t"
172                              "wrpr      %0, 0, %%tick\n\t"
173                              "andn      %0, %4, %1\n\t"
174                              "ba,pt     %%xcc, 1f\n\t"
175                              " add      %1, %3, %1\n\t"
176                              ".align    64\n"
177                              "1:\n\t"
178                              "wr        %1, 0, %%tick_cmpr\n\t"
179                              "rd        %%tick_cmpr, %%g0"
180                              : "=&r" (new_tick), "=&r" (tmp)
181                              : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
182
183         return new_tick;
184 }
185
186 static struct sparc64_tick_ops tick_operations __read_mostly = {
187         .init_tick      =       tick_init_tick,
188         .get_tick       =       tick_get_tick,
189         .get_compare    =       tick_get_compare,
190         .add_tick       =       tick_add_tick,
191         .add_compare    =       tick_add_compare,
192         .softint_mask   =       1UL << 0,
193 };
194
195 struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
196
197 static void stick_init_tick(unsigned long offset)
198 {
199         /* Writes to the %tick and %stick register are not
200          * allowed on sun4v.  The Hypervisor controls that
201          * bit, per-strand.
202          */
203         if (tlb_type != hypervisor) {
204                 tick_disable_protection();
205
206                 /* Let the user get at STICK too. */
207                 __asm__ __volatile__(
208                 "       rd      %%asr24, %%g2\n"
209                 "       andn    %%g2, %0, %%g2\n"
210                 "       wr      %%g2, 0, %%asr24"
211                 : /* no outputs */
212                 : "r" (TICK_PRIV_BIT)
213                 : "g1", "g2");
214         }
215
216         __asm__ __volatile__(
217         "       rd      %%asr24, %%g1\n"
218         "       andn    %%g1, %1, %%g1\n"
219         "       add     %%g1, %0, %%g1\n"
220         "       wr      %%g1, 0x0, %%asr25"
221         : /* no outputs */
222         : "r" (offset), "r" (TICK_PRIV_BIT)
223         : "g1");
224 }
225
226 static unsigned long stick_get_tick(void)
227 {
228         unsigned long ret;
229
230         __asm__ __volatile__("rd        %%asr24, %0"
231                              : "=r" (ret));
232
233         return ret & ~TICK_PRIV_BIT;
234 }
235
236 static unsigned long stick_get_compare(void)
237 {
238         unsigned long ret;
239
240         __asm__ __volatile__("rd        %%asr25, %0"
241                              : "=r" (ret));
242
243         return ret;
244 }
245
246 static unsigned long stick_add_tick(unsigned long adj, unsigned long offset)
247 {
248         unsigned long new_tick, tmp;
249
250         __asm__ __volatile__("rd        %%asr24, %0\n\t"
251                              "add       %0, %2, %0\n\t"
252                              "wr        %0, 0, %%asr24\n\t"
253                              "andn      %0, %4, %1\n\t"
254                              "add       %1, %3, %1\n\t"
255                              "wr        %1, 0, %%asr25"
256                              : "=&r" (new_tick), "=&r" (tmp)
257                              : "r" (adj), "r" (offset), "r" (TICK_PRIV_BIT));
258
259         return new_tick;
260 }
261
262 static unsigned long stick_add_compare(unsigned long adj)
263 {
264         unsigned long new_compare;
265
266         __asm__ __volatile__("rd        %%asr25, %0\n\t"
267                              "add       %0, %1, %0\n\t"
268                              "wr        %0, 0, %%asr25"
269                              : "=&r" (new_compare)
270                              : "r" (adj));
271
272         return new_compare;
273 }
274
275 static struct sparc64_tick_ops stick_operations __read_mostly = {
276         .init_tick      =       stick_init_tick,
277         .get_tick       =       stick_get_tick,
278         .get_compare    =       stick_get_compare,
279         .add_tick       =       stick_add_tick,
280         .add_compare    =       stick_add_compare,
281         .softint_mask   =       1UL << 16,
282 };
283
284 /* On Hummingbird the STICK/STICK_CMPR register is implemented
285  * in I/O space.  There are two 64-bit registers each, the
286  * first holds the low 32-bits of the value and the second holds
287  * the high 32-bits.
288  *
289  * Since STICK is constantly updating, we have to access it carefully.
290  *
291  * The sequence we use to read is:
292  * 1) read high
293  * 2) read low
294  * 3) read high again, if it rolled re-read both low and high again.
295  *
296  * Writing STICK safely is also tricky:
297  * 1) write low to zero
298  * 2) write high
299  * 3) write low
300  */
301 #define HBIRD_STICKCMP_ADDR     0x1fe0000f060UL
302 #define HBIRD_STICK_ADDR        0x1fe0000f070UL
303
304 static unsigned long __hbird_read_stick(void)
305 {
306         unsigned long ret, tmp1, tmp2, tmp3;
307         unsigned long addr = HBIRD_STICK_ADDR+8;
308
309         __asm__ __volatile__("ldxa      [%1] %5, %2\n"
310                              "1:\n\t"
311                              "sub       %1, 0x8, %1\n\t"
312                              "ldxa      [%1] %5, %3\n\t"
313                              "add       %1, 0x8, %1\n\t"
314                              "ldxa      [%1] %5, %4\n\t"
315                              "cmp       %4, %2\n\t"
316                              "bne,a,pn  %%xcc, 1b\n\t"
317                              " mov      %4, %2\n\t"
318                              "sllx      %4, 32, %4\n\t"
319                              "or        %3, %4, %0\n\t"
320                              : "=&r" (ret), "=&r" (addr),
321                                "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
322                              : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
323
324         return ret;
325 }
326
327 static unsigned long __hbird_read_compare(void)
328 {
329         unsigned long low, high;
330         unsigned long addr = HBIRD_STICKCMP_ADDR;
331
332         __asm__ __volatile__("ldxa      [%2] %3, %0\n\t"
333                              "add       %2, 0x8, %2\n\t"
334                              "ldxa      [%2] %3, %1"
335                              : "=&r" (low), "=&r" (high), "=&r" (addr)
336                              : "i" (ASI_PHYS_BYPASS_EC_E), "2" (addr));
337
338         return (high << 32UL) | low;
339 }
340
341 static void __hbird_write_stick(unsigned long val)
342 {
343         unsigned long low = (val & 0xffffffffUL);
344         unsigned long high = (val >> 32UL);
345         unsigned long addr = HBIRD_STICK_ADDR;
346
347         __asm__ __volatile__("stxa      %%g0, [%0] %4\n\t"
348                              "add       %0, 0x8, %0\n\t"
349                              "stxa      %3, [%0] %4\n\t"
350                              "sub       %0, 0x8, %0\n\t"
351                              "stxa      %2, [%0] %4"
352                              : "=&r" (addr)
353                              : "0" (addr), "r" (low), "r" (high),
354                                "i" (ASI_PHYS_BYPASS_EC_E));
355 }
356
357 static void __hbird_write_compare(unsigned long val)
358 {
359         unsigned long low = (val & 0xffffffffUL);
360         unsigned long high = (val >> 32UL);
361         unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
362
363         __asm__ __volatile__("stxa      %3, [%0] %4\n\t"
364                              "sub       %0, 0x8, %0\n\t"
365                              "stxa      %2, [%0] %4"
366                              : "=&r" (addr)
367                              : "0" (addr), "r" (low), "r" (high),
368                                "i" (ASI_PHYS_BYPASS_EC_E));
369 }
370
371 static void hbtick_init_tick(unsigned long offset)
372 {
373         unsigned long val;
374
375         tick_disable_protection();
376
377         /* XXX This seems to be necessary to 'jumpstart' Hummingbird
378          * XXX into actually sending STICK interrupts.  I think because
379          * XXX of how we store %tick_cmpr in head.S this somehow resets the
380          * XXX {TICK + STICK} interrupt mux.  -DaveM
381          */
382         __hbird_write_stick(__hbird_read_stick());
383
384         val = __hbird_read_stick() & ~TICK_PRIV_BIT;
385         __hbird_write_compare(val + offset);
386 }
387
388 static unsigned long hbtick_get_tick(void)
389 {
390         return __hbird_read_stick() & ~TICK_PRIV_BIT;
391 }
392
393 static unsigned long hbtick_get_compare(void)
394 {
395         return __hbird_read_compare();
396 }
397
398 static unsigned long hbtick_add_tick(unsigned long adj, unsigned long offset)
399 {
400         unsigned long val;
401
402         val = __hbird_read_stick() + adj;
403         __hbird_write_stick(val);
404
405         val &= ~TICK_PRIV_BIT;
406         __hbird_write_compare(val + offset);
407
408         return val;
409 }
410
411 static unsigned long hbtick_add_compare(unsigned long adj)
412 {
413         unsigned long val = __hbird_read_compare() + adj;
414
415         val &= ~TICK_PRIV_BIT;
416         __hbird_write_compare(val);
417
418         return val;
419 }
420
421 static struct sparc64_tick_ops hbtick_operations __read_mostly = {
422         .init_tick      =       hbtick_init_tick,
423         .get_tick       =       hbtick_get_tick,
424         .get_compare    =       hbtick_get_compare,
425         .add_tick       =       hbtick_add_tick,
426         .add_compare    =       hbtick_add_compare,
427         .softint_mask   =       1UL << 0,
428 };
429
430 /* timer_interrupt() needs to keep up the real-time clock,
431  * as well as call the "do_timer()" routine every clocktick
432  *
433  * NOTE: On SUN5 systems the ticker interrupt comes in using 2
434  *       interrupts, one at level14 and one with softint bit 0.
435  */
436 unsigned long timer_tick_offset __read_mostly;
437
438 static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
439
440 #define TICK_SIZE (tick_nsec / 1000)
441
442 static inline void timer_check_rtc(void)
443 {
444         /* last time the cmos clock got updated */
445         static long last_rtc_update;
446
447         /* Determine when to update the Mostek clock. */
448         if (ntp_synced() &&
449             xtime.tv_sec > last_rtc_update + 660 &&
450             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
451             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
452                 if (set_rtc_mmss(xtime.tv_sec) == 0)
453                         last_rtc_update = xtime.tv_sec;
454                 else
455                         last_rtc_update = xtime.tv_sec - 600;
456                         /* do it again in 60 s */
457         }
458 }
459
460 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs * regs)
461 {
462         unsigned long ticks, compare, pstate;
463
464         write_seqlock(&xtime_lock);
465
466         do {
467 #ifndef CONFIG_SMP
468                 profile_tick(CPU_PROFILING, regs);
469                 update_process_times(user_mode(regs));
470 #endif
471                 do_timer(regs);
472
473                 /* Guarantee that the following sequences execute
474                  * uninterrupted.
475                  */
476                 __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
477                                      "wrpr      %0, %1, %%pstate"
478                                      : "=r" (pstate)
479                                      : "i" (PSTATE_IE));
480
481                 compare = tick_ops->add_compare(timer_tick_offset);
482                 ticks = tick_ops->get_tick();
483
484                 /* Restore PSTATE_IE. */
485                 __asm__ __volatile__("wrpr      %0, 0x0, %%pstate"
486                                      : /* no outputs */
487                                      : "r" (pstate));
488         } while (time_after_eq(ticks, compare));
489
490         timer_check_rtc();
491
492         write_sequnlock(&xtime_lock);
493
494         return IRQ_HANDLED;
495 }
496
497 #ifdef CONFIG_SMP
498 void timer_tick_interrupt(struct pt_regs *regs)
499 {
500         write_seqlock(&xtime_lock);
501
502         do_timer(regs);
503
504         timer_check_rtc();
505
506         write_sequnlock(&xtime_lock);
507 }
508 #endif
509
510 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
511 static void __init kick_start_clock(void)
512 {
513         void __iomem *regs = mstk48t02_regs;
514         u8 sec, tmp;
515         int i, count;
516
517         prom_printf("CLOCK: Clock was stopped. Kick start ");
518
519         spin_lock_irq(&mostek_lock);
520
521         /* Turn on the kick start bit to start the oscillator. */
522         tmp = mostek_read(regs + MOSTEK_CREG);
523         tmp |= MSTK_CREG_WRITE;
524         mostek_write(regs + MOSTEK_CREG, tmp);
525         tmp = mostek_read(regs + MOSTEK_SEC);
526         tmp &= ~MSTK_STOP;
527         mostek_write(regs + MOSTEK_SEC, tmp);
528         tmp = mostek_read(regs + MOSTEK_HOUR);
529         tmp |= MSTK_KICK_START;
530         mostek_write(regs + MOSTEK_HOUR, tmp);
531         tmp = mostek_read(regs + MOSTEK_CREG);
532         tmp &= ~MSTK_CREG_WRITE;
533         mostek_write(regs + MOSTEK_CREG, tmp);
534
535         spin_unlock_irq(&mostek_lock);
536
537         /* Delay to allow the clock oscillator to start. */
538         sec = MSTK_REG_SEC(regs);
539         for (i = 0; i < 3; i++) {
540                 while (sec == MSTK_REG_SEC(regs))
541                         for (count = 0; count < 100000; count++)
542                                 /* nothing */ ;
543                 prom_printf(".");
544                 sec = MSTK_REG_SEC(regs);
545         }
546         prom_printf("\n");
547
548         spin_lock_irq(&mostek_lock);
549
550         /* Turn off kick start and set a "valid" time and date. */
551         tmp = mostek_read(regs + MOSTEK_CREG);
552         tmp |= MSTK_CREG_WRITE;
553         mostek_write(regs + MOSTEK_CREG, tmp);
554         tmp = mostek_read(regs + MOSTEK_HOUR);
555         tmp &= ~MSTK_KICK_START;
556         mostek_write(regs + MOSTEK_HOUR, tmp);
557         MSTK_SET_REG_SEC(regs,0);
558         MSTK_SET_REG_MIN(regs,0);
559         MSTK_SET_REG_HOUR(regs,0);
560         MSTK_SET_REG_DOW(regs,5);
561         MSTK_SET_REG_DOM(regs,1);
562         MSTK_SET_REG_MONTH(regs,8);
563         MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
564         tmp = mostek_read(regs + MOSTEK_CREG);
565         tmp &= ~MSTK_CREG_WRITE;
566         mostek_write(regs + MOSTEK_CREG, tmp);
567
568         spin_unlock_irq(&mostek_lock);
569
570         /* Ensure the kick start bit is off. If it isn't, turn it off. */
571         while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
572                 prom_printf("CLOCK: Kick start still on!\n");
573
574                 spin_lock_irq(&mostek_lock);
575
576                 tmp = mostek_read(regs + MOSTEK_CREG);
577                 tmp |= MSTK_CREG_WRITE;
578                 mostek_write(regs + MOSTEK_CREG, tmp);
579
580                 tmp = mostek_read(regs + MOSTEK_HOUR);
581                 tmp &= ~MSTK_KICK_START;
582                 mostek_write(regs + MOSTEK_HOUR, tmp);
583
584                 tmp = mostek_read(regs + MOSTEK_CREG);
585                 tmp &= ~MSTK_CREG_WRITE;
586                 mostek_write(regs + MOSTEK_CREG, tmp);
587
588                 spin_unlock_irq(&mostek_lock);
589         }
590
591         prom_printf("CLOCK: Kick start procedure successful.\n");
592 }
593
594 /* Return nonzero if the clock chip battery is low. */
595 static int __init has_low_battery(void)
596 {
597         void __iomem *regs = mstk48t02_regs;
598         u8 data1, data2;
599
600         spin_lock_irq(&mostek_lock);
601
602         data1 = mostek_read(regs + MOSTEK_EEPROM);      /* Read some data. */
603         mostek_write(regs + MOSTEK_EEPROM, ~data1);     /* Write back the complement. */
604         data2 = mostek_read(regs + MOSTEK_EEPROM);      /* Read back the complement. */
605         mostek_write(regs + MOSTEK_EEPROM, data1);      /* Restore original value. */
606
607         spin_unlock_irq(&mostek_lock);
608
609         return (data1 == data2);        /* Was the write blocked? */
610 }
611
612 /* Probe for the real time clock chip. */
613 static void __init set_system_time(void)
614 {
615         unsigned int year, mon, day, hour, min, sec;
616         void __iomem *mregs = mstk48t02_regs;
617 #ifdef CONFIG_PCI
618         unsigned long dregs = ds1287_regs;
619 #else
620         unsigned long dregs = 0UL;
621 #endif
622         u8 tmp;
623
624         if (!mregs && !dregs) {
625                 prom_printf("Something wrong, clock regs not mapped yet.\n");
626                 prom_halt();
627         }               
628
629         if (mregs) {
630                 spin_lock_irq(&mostek_lock);
631
632                 /* Traditional Mostek chip. */
633                 tmp = mostek_read(mregs + MOSTEK_CREG);
634                 tmp |= MSTK_CREG_READ;
635                 mostek_write(mregs + MOSTEK_CREG, tmp);
636
637                 sec = MSTK_REG_SEC(mregs);
638                 min = MSTK_REG_MIN(mregs);
639                 hour = MSTK_REG_HOUR(mregs);
640                 day = MSTK_REG_DOM(mregs);
641                 mon = MSTK_REG_MONTH(mregs);
642                 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
643         } else {
644                 int i;
645
646                 /* Dallas 12887 RTC chip. */
647
648                 /* Stolen from arch/i386/kernel/time.c, see there for
649                  * credits and descriptive comments.
650                  */
651                 for (i = 0; i < 1000000; i++) {
652                         if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
653                                 break;
654                         udelay(10);
655                 }
656                 for (i = 0; i < 1000000; i++) {
657                         if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
658                                 break;
659                         udelay(10);
660                 }
661                 do {
662                         sec  = CMOS_READ(RTC_SECONDS);
663                         min  = CMOS_READ(RTC_MINUTES);
664                         hour = CMOS_READ(RTC_HOURS);
665                         day  = CMOS_READ(RTC_DAY_OF_MONTH);
666                         mon  = CMOS_READ(RTC_MONTH);
667                         year = CMOS_READ(RTC_YEAR);
668                 } while (sec != CMOS_READ(RTC_SECONDS));
669                 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
670                         BCD_TO_BIN(sec);
671                         BCD_TO_BIN(min);
672                         BCD_TO_BIN(hour);
673                         BCD_TO_BIN(day);
674                         BCD_TO_BIN(mon);
675                         BCD_TO_BIN(year);
676                 }
677                 if ((year += 1900) < 1970)
678                         year += 100;
679         }
680
681         xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
682         xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
683         set_normalized_timespec(&wall_to_monotonic,
684                                 -xtime.tv_sec, -xtime.tv_nsec);
685
686         if (mregs) {
687                 tmp = mostek_read(mregs + MOSTEK_CREG);
688                 tmp &= ~MSTK_CREG_READ;
689                 mostek_write(mregs + MOSTEK_CREG, tmp);
690
691                 spin_unlock_irq(&mostek_lock);
692         }
693 }
694
695 /* davem suggests we keep this within the 4M locked kernel image */
696 static u32 starfire_get_time(void)
697 {
698         static char obp_gettod[32];
699         static u32 unix_tod;
700
701         sprintf(obp_gettod, "h# %08x unix-gettod",
702                 (unsigned int) (long) &unix_tod);
703         prom_feval(obp_gettod);
704
705         return unix_tod;
706 }
707
708 static int starfire_set_time(u32 val)
709 {
710         /* Do nothing, time is set using the service processor
711          * console on this platform.
712          */
713         return 0;
714 }
715
716 static u32 hypervisor_get_time(void)
717 {
718         register unsigned long func asm("%o5");
719         register unsigned long arg0 asm("%o0");
720         register unsigned long arg1 asm("%o1");
721         int retries = 10000;
722
723 retry:
724         func = HV_FAST_TOD_GET;
725         arg0 = 0;
726         arg1 = 0;
727         __asm__ __volatile__("ta        %6"
728                              : "=&r" (func), "=&r" (arg0), "=&r" (arg1)
729                              : "0" (func), "1" (arg0), "2" (arg1),
730                                "i" (HV_FAST_TRAP));
731         if (arg0 == HV_EOK)
732                 return arg1;
733         if (arg0 == HV_EWOULDBLOCK) {
734                 if (--retries > 0) {
735                         udelay(100);
736                         goto retry;
737                 }
738                 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
739                 return 0;
740         }
741         printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
742         return 0;
743 }
744
745 static int hypervisor_set_time(u32 secs)
746 {
747         register unsigned long func asm("%o5");
748         register unsigned long arg0 asm("%o0");
749         int retries = 10000;
750
751 retry:
752         func = HV_FAST_TOD_SET;
753         arg0 = secs;
754         __asm__ __volatile__("ta        %4"
755                              : "=&r" (func), "=&r" (arg0)
756                              : "0" (func), "1" (arg0),
757                                "i" (HV_FAST_TRAP));
758         if (arg0 == HV_EOK)
759                 return 0;
760         if (arg0 == HV_EWOULDBLOCK) {
761                 if (--retries > 0) {
762                         udelay(100);
763                         goto retry;
764                 }
765                 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
766                 return -EAGAIN;
767         }
768         printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
769         return -EOPNOTSUPP;
770 }
771
772 void __init clock_probe(void)
773 {
774         struct linux_prom_registers clk_reg[2];
775         char model[128];
776         int node, busnd = -1, err;
777         unsigned long flags;
778         struct linux_central *cbus;
779 #ifdef CONFIG_PCI
780         struct linux_ebus *ebus = NULL;
781         struct sparc_isa_bridge *isa_br = NULL;
782 #endif
783         static int invoked;
784
785         if (invoked)
786                 return;
787         invoked = 1;
788
789
790         if (this_is_starfire) {
791                 xtime.tv_sec = starfire_get_time();
792                 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
793                 set_normalized_timespec(&wall_to_monotonic,
794                                         -xtime.tv_sec, -xtime.tv_nsec);
795                 return;
796         }
797         if (tlb_type == hypervisor) {
798                 xtime.tv_sec = hypervisor_get_time();
799                 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
800                 set_normalized_timespec(&wall_to_monotonic,
801                                         -xtime.tv_sec, -xtime.tv_nsec);
802                 return;
803         }
804
805         local_irq_save(flags);
806
807         cbus = central_bus;
808         if (cbus != NULL)
809                 busnd = central_bus->child->prom_node;
810
811         /* Check FHC Central then EBUSs then ISA bridges then SBUSs.
812          * That way we handle the presence of multiple properly.
813          *
814          * As a special case, machines with Central must provide the
815          * timer chip there.
816          */
817 #ifdef CONFIG_PCI
818         if (ebus_chain != NULL) {
819                 ebus = ebus_chain;
820                 if (busnd == -1)
821                         busnd = ebus->prom_node;
822         }
823         if (isa_chain != NULL) {
824                 isa_br = isa_chain;
825                 if (busnd == -1)
826                         busnd = isa_br->prom_node;
827         }
828 #endif
829         if (sbus_root != NULL && busnd == -1)
830                 busnd = sbus_root->prom_node;
831
832         if (busnd == -1) {
833                 prom_printf("clock_probe: problem, cannot find bus to search.\n");
834                 prom_halt();
835         }
836
837         node = prom_getchild(busnd);
838
839         while (1) {
840                 if (!node)
841                         model[0] = 0;
842                 else
843                         prom_getstring(node, "model", model, sizeof(model));
844                 if (strcmp(model, "mk48t02") &&
845                     strcmp(model, "mk48t08") &&
846                     strcmp(model, "mk48t59") &&
847                     strcmp(model, "m5819") &&
848                     strcmp(model, "m5819p") &&
849                     strcmp(model, "m5823") &&
850                     strcmp(model, "ds1287")) {
851                         if (cbus != NULL) {
852                                 prom_printf("clock_probe: Central bus lacks timer chip.\n");
853                                 prom_halt();
854                         }
855
856                         if (node != 0)
857                                 node = prom_getsibling(node);
858 #ifdef CONFIG_PCI
859                         while ((node == 0) && ebus != NULL) {
860                                 ebus = ebus->next;
861                                 if (ebus != NULL) {
862                                         busnd = ebus->prom_node;
863                                         node = prom_getchild(busnd);
864                                 }
865                         }
866                         while ((node == 0) && isa_br != NULL) {
867                                 isa_br = isa_br->next;
868                                 if (isa_br != NULL) {
869                                         busnd = isa_br->prom_node;
870                                         node = prom_getchild(busnd);
871                                 }
872                         }
873 #endif
874                         if (node == 0) {
875                                 prom_printf("clock_probe: Cannot find timer chip\n");
876                                 prom_halt();
877                         }
878                         continue;
879                 }
880
881                 err = prom_getproperty(node, "reg", (char *)clk_reg,
882                                        sizeof(clk_reg));
883                 if(err == -1) {
884                         prom_printf("clock_probe: Cannot get Mostek reg property\n");
885                         prom_halt();
886                 }
887
888                 if (cbus != NULL) {
889                         apply_fhc_ranges(central_bus->child, clk_reg, 1);
890                         apply_central_ranges(central_bus, clk_reg, 1);
891                 }
892 #ifdef CONFIG_PCI
893                 else if (ebus != NULL) {
894                         struct linux_ebus_device *edev;
895
896                         for_each_ebusdev(edev, ebus)
897                                 if (edev->prom_node == node)
898                                         break;
899                         if (edev == NULL) {
900                                 if (isa_chain != NULL)
901                                         goto try_isa_clock;
902                                 prom_printf("%s: Mostek not probed by EBUS\n",
903                                             __FUNCTION__);
904                                 prom_halt();
905                         }
906
907                         if (!strcmp(model, "ds1287") ||
908                             !strcmp(model, "m5819") ||
909                             !strcmp(model, "m5819p") ||
910                             !strcmp(model, "m5823")) {
911                                 ds1287_regs = edev->resource[0].start;
912                         } else {
913                                 mstk48t59_regs = (void __iomem *)
914                                         edev->resource[0].start;
915                                 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
916                         }
917                         break;
918                 }
919                 else if (isa_br != NULL) {
920                         struct sparc_isa_device *isadev;
921
922 try_isa_clock:
923                         for_each_isadev(isadev, isa_br)
924                                 if (isadev->prom_node == node)
925                                         break;
926                         if (isadev == NULL) {
927                                 prom_printf("%s: Mostek not probed by ISA\n");
928                                 prom_halt();
929                         }
930                         if (!strcmp(model, "ds1287") ||
931                             !strcmp(model, "m5819") ||
932                             !strcmp(model, "m5819p") ||
933                             !strcmp(model, "m5823")) {
934                                 ds1287_regs = isadev->resource.start;
935                         } else {
936                                 mstk48t59_regs = (void __iomem *)
937                                         isadev->resource.start;
938                                 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
939                         }
940                         break;
941                 }
942 #endif
943                 else {
944                         if (sbus_root->num_sbus_ranges) {
945                                 int nranges = sbus_root->num_sbus_ranges;
946                                 int rngc;
947
948                                 for (rngc = 0; rngc < nranges; rngc++)
949                                         if (clk_reg[0].which_io ==
950                                             sbus_root->sbus_ranges[rngc].ot_child_space)
951                                                 break;
952                                 if (rngc == nranges) {
953                                         prom_printf("clock_probe: Cannot find ranges for "
954                                                     "clock regs.\n");
955                                         prom_halt();
956                                 }
957                                 clk_reg[0].which_io =
958                                         sbus_root->sbus_ranges[rngc].ot_parent_space;
959                                 clk_reg[0].phys_addr +=
960                                         sbus_root->sbus_ranges[rngc].ot_parent_base;
961                         }
962                 }
963
964                 if(model[5] == '0' && model[6] == '2') {
965                         mstk48t02_regs = (void __iomem *)
966                                 (((u64)clk_reg[0].phys_addr) |
967                                  (((u64)clk_reg[0].which_io)<<32UL));
968                 } else if(model[5] == '0' && model[6] == '8') {
969                         mstk48t08_regs = (void __iomem *)
970                                 (((u64)clk_reg[0].phys_addr) |
971                                  (((u64)clk_reg[0].which_io)<<32UL));
972                         mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
973                 } else {
974                         mstk48t59_regs = (void __iomem *)
975                                 (((u64)clk_reg[0].phys_addr) |
976                                  (((u64)clk_reg[0].which_io)<<32UL));
977                         mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
978                 }
979                 break;
980         }
981
982         if (mstk48t02_regs != NULL) {
983                 /* Report a low battery voltage condition. */
984                 if (has_low_battery())
985                         prom_printf("NVRAM: Low battery voltage!\n");
986
987                 /* Kick start the clock if it is completely stopped. */
988                 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
989                         kick_start_clock();
990         }
991
992         set_system_time();
993         
994         local_irq_restore(flags);
995 }
996
997 /* This is gets the master TICK_INT timer going. */
998 static unsigned long sparc64_init_timers(void)
999 {
1000         unsigned long clock;
1001         int node;
1002 #ifdef CONFIG_SMP
1003         extern void smp_tick_init(void);
1004 #endif
1005
1006         if (tlb_type == spitfire) {
1007                 unsigned long ver, manuf, impl;
1008
1009                 __asm__ __volatile__ ("rdpr %%ver, %0"
1010                                       : "=&r" (ver));
1011                 manuf = ((ver >> 48) & 0xffff);
1012                 impl = ((ver >> 32) & 0xffff);
1013                 if (manuf == 0x17 && impl == 0x13) {
1014                         /* Hummingbird, aka Ultra-IIe */
1015                         tick_ops = &hbtick_operations;
1016                         node = prom_root_node;
1017                         clock = prom_getint(node, "stick-frequency");
1018                 } else {
1019                         tick_ops = &tick_operations;
1020                         cpu_find_by_instance(0, &node, NULL);
1021                         clock = prom_getint(node, "clock-frequency");
1022                 }
1023         } else {
1024                 tick_ops = &stick_operations;
1025                 node = prom_root_node;
1026                 clock = prom_getint(node, "stick-frequency");
1027         }
1028         timer_tick_offset = clock / HZ;
1029
1030 #ifdef CONFIG_SMP
1031         smp_tick_init();
1032 #endif
1033
1034         return clock;
1035 }
1036
1037 static void sparc64_start_timers(irqreturn_t (*cfunc)(int, void *, struct pt_regs *))
1038 {
1039         unsigned long pstate;
1040         int err;
1041
1042         /* Register IRQ handler. */
1043         err = request_irq(build_irq(0, 0, 0UL, 0UL), cfunc, 0,
1044                           "timer", NULL);
1045
1046         if (err) {
1047                 prom_printf("Serious problem, cannot register TICK_INT\n");
1048                 prom_halt();
1049         }
1050
1051         /* Guarantee that the following sequences execute
1052          * uninterrupted.
1053          */
1054         __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
1055                              "wrpr      %0, %1, %%pstate"
1056                              : "=r" (pstate)
1057                              : "i" (PSTATE_IE));
1058
1059         tick_ops->init_tick(timer_tick_offset);
1060
1061         /* Restore PSTATE_IE. */
1062         __asm__ __volatile__("wrpr      %0, 0x0, %%pstate"
1063                              : /* no outputs */
1064                              : "r" (pstate));
1065
1066         local_irq_enable();
1067 }
1068
1069 struct freq_table {
1070         unsigned long clock_tick_ref;
1071         unsigned int ref_freq;
1072 };
1073 static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
1074
1075 unsigned long sparc64_get_clock_tick(unsigned int cpu)
1076 {
1077         struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1078
1079         if (ft->clock_tick_ref)
1080                 return ft->clock_tick_ref;
1081         return cpu_data(cpu).clock_tick;
1082 }
1083
1084 #ifdef CONFIG_CPU_FREQ
1085
1086 static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
1087                                     void *data)
1088 {
1089         struct cpufreq_freqs *freq = data;
1090         unsigned int cpu = freq->cpu;
1091         struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
1092
1093         if (!ft->ref_freq) {
1094                 ft->ref_freq = freq->old;
1095                 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
1096         }
1097         if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
1098             (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
1099             (val == CPUFREQ_RESUMECHANGE)) {
1100                 cpu_data(cpu).clock_tick =
1101                         cpufreq_scale(ft->clock_tick_ref,
1102                                       ft->ref_freq,
1103                                       freq->new);
1104         }
1105
1106         return 0;
1107 }
1108
1109 static struct notifier_block sparc64_cpufreq_notifier_block = {
1110         .notifier_call  = sparc64_cpufreq_notifier
1111 };
1112
1113 #endif /* CONFIG_CPU_FREQ */
1114
1115 static struct time_interpolator sparc64_cpu_interpolator = {
1116         .source         =       TIME_SOURCE_CPU,
1117         .shift          =       16,
1118         .mask           =       0xffffffffffffffffLL
1119 };
1120
1121 /* The quotient formula is taken from the IA64 port. */
1122 #define SPARC64_NSEC_PER_CYC_SHIFT      30UL
1123 void __init time_init(void)
1124 {
1125         unsigned long clock = sparc64_init_timers();
1126
1127         sparc64_cpu_interpolator.frequency = clock;
1128         register_time_interpolator(&sparc64_cpu_interpolator);
1129
1130         /* Now that the interpolator is registered, it is
1131          * safe to start the timer ticking.
1132          */
1133         sparc64_start_timers(timer_interrupt);
1134
1135         timer_ticks_per_nsec_quotient =
1136                 (((NSEC_PER_SEC << SPARC64_NSEC_PER_CYC_SHIFT) +
1137                   (clock / 2)) / clock);
1138
1139 #ifdef CONFIG_CPU_FREQ
1140         cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
1141                                   CPUFREQ_TRANSITION_NOTIFIER);
1142 #endif
1143 }
1144
1145 unsigned long long sched_clock(void)
1146 {
1147         unsigned long ticks = tick_ops->get_tick();
1148
1149         return (ticks * timer_ticks_per_nsec_quotient)
1150                 >> SPARC64_NSEC_PER_CYC_SHIFT;
1151 }
1152
1153 static int set_rtc_mmss(unsigned long nowtime)
1154 {
1155         int real_seconds, real_minutes, chip_minutes;
1156         void __iomem *mregs = mstk48t02_regs;
1157 #ifdef CONFIG_PCI
1158         unsigned long dregs = ds1287_regs;
1159 #else
1160         unsigned long dregs = 0UL;
1161 #endif
1162         unsigned long flags;
1163         u8 tmp;
1164
1165         /* 
1166          * Not having a register set can lead to trouble.
1167          * Also starfire doesn't have a tod clock.
1168          */
1169         if (!mregs && !dregs) 
1170                 return -1;
1171
1172         if (mregs) {
1173                 spin_lock_irqsave(&mostek_lock, flags);
1174
1175                 /* Read the current RTC minutes. */
1176                 tmp = mostek_read(mregs + MOSTEK_CREG);
1177                 tmp |= MSTK_CREG_READ;
1178                 mostek_write(mregs + MOSTEK_CREG, tmp);
1179
1180                 chip_minutes = MSTK_REG_MIN(mregs);
1181
1182                 tmp = mostek_read(mregs + MOSTEK_CREG);
1183                 tmp &= ~MSTK_CREG_READ;
1184                 mostek_write(mregs + MOSTEK_CREG, tmp);
1185
1186                 /*
1187                  * since we're only adjusting minutes and seconds,
1188                  * don't interfere with hour overflow. This avoids
1189                  * messing with unknown time zones but requires your
1190                  * RTC not to be off by more than 15 minutes
1191                  */
1192                 real_seconds = nowtime % 60;
1193                 real_minutes = nowtime / 60;
1194                 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1195                         real_minutes += 30;     /* correct for half hour time zone */
1196                 real_minutes %= 60;
1197
1198                 if (abs(real_minutes - chip_minutes) < 30) {
1199                         tmp = mostek_read(mregs + MOSTEK_CREG);
1200                         tmp |= MSTK_CREG_WRITE;
1201                         mostek_write(mregs + MOSTEK_CREG, tmp);
1202
1203                         MSTK_SET_REG_SEC(mregs,real_seconds);
1204                         MSTK_SET_REG_MIN(mregs,real_minutes);
1205
1206                         tmp = mostek_read(mregs + MOSTEK_CREG);
1207                         tmp &= ~MSTK_CREG_WRITE;
1208                         mostek_write(mregs + MOSTEK_CREG, tmp);
1209
1210                         spin_unlock_irqrestore(&mostek_lock, flags);
1211
1212                         return 0;
1213                 } else {
1214                         spin_unlock_irqrestore(&mostek_lock, flags);
1215
1216                         return -1;
1217                 }
1218         } else {
1219                 int retval = 0;
1220                 unsigned char save_control, save_freq_select;
1221
1222                 /* Stolen from arch/i386/kernel/time.c, see there for
1223                  * credits and descriptive comments.
1224                  */
1225                 spin_lock_irqsave(&rtc_lock, flags);
1226                 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1227                 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1228
1229                 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1230                 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1231
1232                 chip_minutes = CMOS_READ(RTC_MINUTES);
1233                 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1234                         BCD_TO_BIN(chip_minutes);
1235                 real_seconds = nowtime % 60;
1236                 real_minutes = nowtime / 60;
1237                 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1238                         real_minutes += 30;
1239                 real_minutes %= 60;
1240
1241                 if (abs(real_minutes - chip_minutes) < 30) {
1242                         if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1243                                 BIN_TO_BCD(real_seconds);
1244                                 BIN_TO_BCD(real_minutes);
1245                         }
1246                         CMOS_WRITE(real_seconds,RTC_SECONDS);
1247                         CMOS_WRITE(real_minutes,RTC_MINUTES);
1248                 } else {
1249                         printk(KERN_WARNING
1250                                "set_rtc_mmss: can't update from %d to %d\n",
1251                                chip_minutes, real_minutes);
1252                         retval = -1;
1253                 }
1254
1255                 CMOS_WRITE(save_control, RTC_CONTROL);
1256                 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1257                 spin_unlock_irqrestore(&rtc_lock, flags);
1258
1259                 return retval;
1260         }
1261 }
1262
1263 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
1264 static unsigned char mini_rtc_status;   /* bitmapped status byte.       */
1265
1266 /* months start at 0 now */
1267 static unsigned char days_in_mo[] =
1268 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1269
1270 #define FEBRUARY        2
1271 #define STARTOFTIME     1970
1272 #define SECDAY          86400L
1273 #define SECYR           (SECDAY * 365)
1274 #define leapyear(year)          ((year) % 4 == 0 && \
1275                                  ((year) % 100 != 0 || (year) % 400 == 0))
1276 #define days_in_year(a)         (leapyear(a) ? 366 : 365)
1277 #define days_in_month(a)        (month_days[(a) - 1])
1278
1279 static int month_days[12] = {
1280         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1281 };
1282
1283 /*
1284  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1285  */
1286 static void GregorianDay(struct rtc_time * tm)
1287 {
1288         int leapsToDate;
1289         int lastYear;
1290         int day;
1291         int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1292
1293         lastYear = tm->tm_year - 1;
1294
1295         /*
1296          * Number of leap corrections to apply up to end of last year
1297          */
1298         leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1299
1300         /*
1301          * This year is a leap year if it is divisible by 4 except when it is
1302          * divisible by 100 unless it is divisible by 400
1303          *
1304          * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1305          */
1306         day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1307
1308         day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1309                    tm->tm_mday;
1310
1311         tm->tm_wday = day % 7;
1312 }
1313
1314 static void to_tm(int tim, struct rtc_time *tm)
1315 {
1316         register int    i;
1317         register long   hms, day;
1318
1319         day = tim / SECDAY;
1320         hms = tim % SECDAY;
1321
1322         /* Hours, minutes, seconds are easy */
1323         tm->tm_hour = hms / 3600;
1324         tm->tm_min = (hms % 3600) / 60;
1325         tm->tm_sec = (hms % 3600) % 60;
1326
1327         /* Number of years in days */
1328         for (i = STARTOFTIME; day >= days_in_year(i); i++)
1329                 day -= days_in_year(i);
1330         tm->tm_year = i;
1331
1332         /* Number of months in days left */
1333         if (leapyear(tm->tm_year))
1334                 days_in_month(FEBRUARY) = 29;
1335         for (i = 1; day >= days_in_month(i); i++)
1336                 day -= days_in_month(i);
1337         days_in_month(FEBRUARY) = 28;
1338         tm->tm_mon = i;
1339
1340         /* Days are what is left over (+1) from all that. */
1341         tm->tm_mday = day + 1;
1342
1343         /*
1344          * Determine the day of week
1345          */
1346         GregorianDay(tm);
1347 }
1348
1349 /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1350  * aka Unix time.  So we have to convert to/from rtc_time.
1351  */
1352 static inline void mini_get_rtc_time(struct rtc_time *time)
1353 {
1354         unsigned long flags;
1355         u32 seconds;
1356
1357         spin_lock_irqsave(&rtc_lock, flags);
1358         seconds = 0;
1359         if (this_is_starfire)
1360                 seconds = starfire_get_time();
1361         else if (tlb_type == hypervisor)
1362                 seconds = hypervisor_get_time();
1363         spin_unlock_irqrestore(&rtc_lock, flags);
1364
1365         to_tm(seconds, time);
1366         time->tm_year -= 1900;
1367         time->tm_mon -= 1;
1368 }
1369
1370 static inline int mini_set_rtc_time(struct rtc_time *time)
1371 {
1372         u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1373                              time->tm_mday, time->tm_hour,
1374                              time->tm_min, time->tm_sec);
1375         unsigned long flags;
1376         int err;
1377
1378         spin_lock_irqsave(&rtc_lock, flags);
1379         err = -ENODEV;
1380         if (this_is_starfire)
1381                 err = starfire_set_time(seconds);
1382         else  if (tlb_type == hypervisor)
1383                 err = hypervisor_set_time(seconds);
1384         spin_unlock_irqrestore(&rtc_lock, flags);
1385
1386         return err;
1387 }
1388
1389 static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1390                           unsigned int cmd, unsigned long arg)
1391 {
1392         struct rtc_time wtime;
1393         void __user *argp = (void __user *)arg;
1394
1395         switch (cmd) {
1396
1397         case RTC_PLL_GET:
1398                 return -EINVAL;
1399
1400         case RTC_PLL_SET:
1401                 return -EINVAL;
1402
1403         case RTC_UIE_OFF:       /* disable ints from RTC updates.       */
1404                 return 0;
1405
1406         case RTC_UIE_ON:        /* enable ints for RTC updates. */
1407                 return -EINVAL;
1408
1409         case RTC_RD_TIME:       /* Read the time/date from RTC  */
1410                 /* this doesn't get week-day, who cares */
1411                 memset(&wtime, 0, sizeof(wtime));
1412                 mini_get_rtc_time(&wtime);
1413
1414                 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1415
1416         case RTC_SET_TIME:      /* Set the RTC */
1417             {
1418                 int year;
1419                 unsigned char leap_yr;
1420
1421                 if (!capable(CAP_SYS_TIME))
1422                         return -EACCES;
1423
1424                 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1425                         return -EFAULT;
1426
1427                 year = wtime.tm_year + 1900;
1428                 leap_yr = ((!(year % 4) && (year % 100)) ||
1429                            !(year % 400));
1430
1431                 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
1432                         return -EINVAL;
1433
1434                 if (wtime.tm_mday < 0 || wtime.tm_mday >
1435                     (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
1436                         return -EINVAL;
1437
1438                 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1439                     wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1440                     wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1441                         return -EINVAL;
1442
1443                 return mini_set_rtc_time(&wtime);
1444             }
1445         }
1446
1447         return -EINVAL;
1448 }
1449
1450 static int mini_rtc_open(struct inode *inode, struct file *file)
1451 {
1452         if (mini_rtc_status & RTC_IS_OPEN)
1453                 return -EBUSY;
1454
1455         mini_rtc_status |= RTC_IS_OPEN;
1456
1457         return 0;
1458 }
1459
1460 static int mini_rtc_release(struct inode *inode, struct file *file)
1461 {
1462         mini_rtc_status &= ~RTC_IS_OPEN;
1463         return 0;
1464 }
1465
1466
1467 static struct file_operations mini_rtc_fops = {
1468         .owner          = THIS_MODULE,
1469         .ioctl          = mini_rtc_ioctl,
1470         .open           = mini_rtc_open,
1471         .release        = mini_rtc_release,
1472 };
1473
1474 static struct miscdevice rtc_mini_dev =
1475 {
1476         .minor          = RTC_MINOR,
1477         .name           = "rtc",
1478         .fops           = &mini_rtc_fops,
1479 };
1480
1481 static int __init rtc_mini_init(void)
1482 {
1483         int retval;
1484
1485         if (tlb_type != hypervisor && !this_is_starfire)
1486                 return -ENODEV;
1487
1488         printk(KERN_INFO "Mini RTC Driver\n");
1489
1490         retval = misc_register(&rtc_mini_dev);
1491         if (retval < 0)
1492                 return retval;
1493
1494         return 0;
1495 }
1496
1497 static void __exit rtc_mini_exit(void)
1498 {
1499         misc_deregister(&rtc_mini_dev);
1500 }
1501
1502
1503 module_init(rtc_mini_init);
1504 module_exit(rtc_mini_exit);