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