]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/sparc64/kernel/time.c
sparc64: Use generic BQ4802 RTC driver.
[linux-2.6-omap-h63xx.git] / arch / sparc64 / kernel / time.c
1 /* time.c: UltraSparc timer and TOD clock support.
2  *
3  * Copyright (C) 1997, 2008 David S. Miller (davem@davemloft.net)
4  * Copyright (C) 1998 Eddie C. Dost   (ecd@skynet.be)
5  *
6  * Based largely on code which is:
7  *
8  * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
9  */
10
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/smp_lock.h>
15 #include <linux/kernel.h>
16 #include <linux/param.h>
17 #include <linux/string.h>
18 #include <linux/mm.h>
19 #include <linux/interrupt.h>
20 #include <linux/time.h>
21 #include <linux/timex.h>
22 #include <linux/init.h>
23 #include <linux/ioport.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/delay.h>
26 #include <linux/profile.h>
27 #include <linux/bcd.h>
28 #include <linux/jiffies.h>
29 #include <linux/cpufreq.h>
30 #include <linux/percpu.h>
31 #include <linux/miscdevice.h>
32 #include <linux/rtc.h>
33 #include <linux/rtc/m48t59.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/clockchips.h>
36 #include <linux/clocksource.h>
37 #include <linux/of_device.h>
38 #include <linux/platform_device.h>
39
40 #include <asm/oplib.h>
41 #include <asm/timer.h>
42 #include <asm/irq.h>
43 #include <asm/io.h>
44 #include <asm/prom.h>
45 #include <asm/starfire.h>
46 #include <asm/smp.h>
47 #include <asm/sections.h>
48 #include <asm/cpudata.h>
49 #include <asm/uaccess.h>
50 #include <asm/irq_regs.h>
51
52 #include "entry.h"
53
54 DEFINE_SPINLOCK(rtc_lock);
55
56 #define TICK_PRIV_BIT   (1UL << 63)
57 #define TICKCMP_IRQ_BIT (1UL << 63)
58
59 #ifdef CONFIG_SMP
60 unsigned long profile_pc(struct pt_regs *regs)
61 {
62         unsigned long pc = instruction_pointer(regs);
63
64         if (in_lock_functions(pc))
65                 return regs->u_regs[UREG_RETPC];
66         return pc;
67 }
68 EXPORT_SYMBOL(profile_pc);
69 #endif
70
71 static void tick_disable_protection(void)
72 {
73         /* Set things up so user can access tick register for profiling
74          * purposes.  Also workaround BB_ERRATA_1 by doing a dummy
75          * read back of %tick after writing it.
76          */
77         __asm__ __volatile__(
78         "       ba,pt   %%xcc, 1f\n"
79         "        nop\n"
80         "       .align  64\n"
81         "1:     rd      %%tick, %%g2\n"
82         "       add     %%g2, 6, %%g2\n"
83         "       andn    %%g2, %0, %%g2\n"
84         "       wrpr    %%g2, 0, %%tick\n"
85         "       rdpr    %%tick, %%g0"
86         : /* no outputs */
87         : "r" (TICK_PRIV_BIT)
88         : "g2");
89 }
90
91 static void tick_disable_irq(void)
92 {
93         __asm__ __volatile__(
94         "       ba,pt   %%xcc, 1f\n"
95         "        nop\n"
96         "       .align  64\n"
97         "1:     wr      %0, 0x0, %%tick_cmpr\n"
98         "       rd      %%tick_cmpr, %%g0"
99         : /* no outputs */
100         : "r" (TICKCMP_IRQ_BIT));
101 }
102
103 static void tick_init_tick(void)
104 {
105         tick_disable_protection();
106         tick_disable_irq();
107 }
108
109 static unsigned long tick_get_tick(void)
110 {
111         unsigned long ret;
112
113         __asm__ __volatile__("rd        %%tick, %0\n\t"
114                              "mov       %0, %0"
115                              : "=r" (ret));
116
117         return ret & ~TICK_PRIV_BIT;
118 }
119
120 static int tick_add_compare(unsigned long adj)
121 {
122         unsigned long orig_tick, new_tick, new_compare;
123
124         __asm__ __volatile__("rd        %%tick, %0"
125                              : "=r" (orig_tick));
126
127         orig_tick &= ~TICKCMP_IRQ_BIT;
128
129         /* Workaround for Spitfire Errata (#54 I think??), I discovered
130          * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
131          * number 103640.
132          *
133          * On Blackbird writes to %tick_cmpr can fail, the
134          * workaround seems to be to execute the wr instruction
135          * at the start of an I-cache line, and perform a dummy
136          * read back from %tick_cmpr right after writing to it. -DaveM
137          */
138         __asm__ __volatile__("ba,pt     %%xcc, 1f\n\t"
139                              " add      %1, %2, %0\n\t"
140                              ".align    64\n"
141                              "1:\n\t"
142                              "wr        %0, 0, %%tick_cmpr\n\t"
143                              "rd        %%tick_cmpr, %%g0\n\t"
144                              : "=r" (new_compare)
145                              : "r" (orig_tick), "r" (adj));
146
147         __asm__ __volatile__("rd        %%tick, %0"
148                              : "=r" (new_tick));
149         new_tick &= ~TICKCMP_IRQ_BIT;
150
151         return ((long)(new_tick - (orig_tick+adj))) > 0L;
152 }
153
154 static unsigned long tick_add_tick(unsigned long adj)
155 {
156         unsigned long new_tick;
157
158         /* Also need to handle Blackbird bug here too. */
159         __asm__ __volatile__("rd        %%tick, %0\n\t"
160                              "add       %0, %1, %0\n\t"
161                              "wrpr      %0, 0, %%tick\n\t"
162                              : "=&r" (new_tick)
163                              : "r" (adj));
164
165         return new_tick;
166 }
167
168 static struct sparc64_tick_ops tick_operations __read_mostly = {
169         .name           =       "tick",
170         .init_tick      =       tick_init_tick,
171         .disable_irq    =       tick_disable_irq,
172         .get_tick       =       tick_get_tick,
173         .add_tick       =       tick_add_tick,
174         .add_compare    =       tick_add_compare,
175         .softint_mask   =       1UL << 0,
176 };
177
178 struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
179
180 static void stick_disable_irq(void)
181 {
182         __asm__ __volatile__(
183         "wr     %0, 0x0, %%asr25"
184         : /* no outputs */
185         : "r" (TICKCMP_IRQ_BIT));
186 }
187
188 static void stick_init_tick(void)
189 {
190         /* Writes to the %tick and %stick register are not
191          * allowed on sun4v.  The Hypervisor controls that
192          * bit, per-strand.
193          */
194         if (tlb_type != hypervisor) {
195                 tick_disable_protection();
196                 tick_disable_irq();
197
198                 /* Let the user get at STICK too. */
199                 __asm__ __volatile__(
200                 "       rd      %%asr24, %%g2\n"
201                 "       andn    %%g2, %0, %%g2\n"
202                 "       wr      %%g2, 0, %%asr24"
203                 : /* no outputs */
204                 : "r" (TICK_PRIV_BIT)
205                 : "g1", "g2");
206         }
207
208         stick_disable_irq();
209 }
210
211 static unsigned long stick_get_tick(void)
212 {
213         unsigned long ret;
214
215         __asm__ __volatile__("rd        %%asr24, %0"
216                              : "=r" (ret));
217
218         return ret & ~TICK_PRIV_BIT;
219 }
220
221 static unsigned long stick_add_tick(unsigned long adj)
222 {
223         unsigned long new_tick;
224
225         __asm__ __volatile__("rd        %%asr24, %0\n\t"
226                              "add       %0, %1, %0\n\t"
227                              "wr        %0, 0, %%asr24\n\t"
228                              : "=&r" (new_tick)
229                              : "r" (adj));
230
231         return new_tick;
232 }
233
234 static int stick_add_compare(unsigned long adj)
235 {
236         unsigned long orig_tick, new_tick;
237
238         __asm__ __volatile__("rd        %%asr24, %0"
239                              : "=r" (orig_tick));
240         orig_tick &= ~TICKCMP_IRQ_BIT;
241
242         __asm__ __volatile__("wr        %0, 0, %%asr25"
243                              : /* no outputs */
244                              : "r" (orig_tick + adj));
245
246         __asm__ __volatile__("rd        %%asr24, %0"
247                              : "=r" (new_tick));
248         new_tick &= ~TICKCMP_IRQ_BIT;
249
250         return ((long)(new_tick - (orig_tick+adj))) > 0L;
251 }
252
253 static struct sparc64_tick_ops stick_operations __read_mostly = {
254         .name           =       "stick",
255         .init_tick      =       stick_init_tick,
256         .disable_irq    =       stick_disable_irq,
257         .get_tick       =       stick_get_tick,
258         .add_tick       =       stick_add_tick,
259         .add_compare    =       stick_add_compare,
260         .softint_mask   =       1UL << 16,
261 };
262
263 /* On Hummingbird the STICK/STICK_CMPR register is implemented
264  * in I/O space.  There are two 64-bit registers each, the
265  * first holds the low 32-bits of the value and the second holds
266  * the high 32-bits.
267  *
268  * Since STICK is constantly updating, we have to access it carefully.
269  *
270  * The sequence we use to read is:
271  * 1) read high
272  * 2) read low
273  * 3) read high again, if it rolled re-read both low and high again.
274  *
275  * Writing STICK safely is also tricky:
276  * 1) write low to zero
277  * 2) write high
278  * 3) write low
279  */
280 #define HBIRD_STICKCMP_ADDR     0x1fe0000f060UL
281 #define HBIRD_STICK_ADDR        0x1fe0000f070UL
282
283 static unsigned long __hbird_read_stick(void)
284 {
285         unsigned long ret, tmp1, tmp2, tmp3;
286         unsigned long addr = HBIRD_STICK_ADDR+8;
287
288         __asm__ __volatile__("ldxa      [%1] %5, %2\n"
289                              "1:\n\t"
290                              "sub       %1, 0x8, %1\n\t"
291                              "ldxa      [%1] %5, %3\n\t"
292                              "add       %1, 0x8, %1\n\t"
293                              "ldxa      [%1] %5, %4\n\t"
294                              "cmp       %4, %2\n\t"
295                              "bne,a,pn  %%xcc, 1b\n\t"
296                              " mov      %4, %2\n\t"
297                              "sllx      %4, 32, %4\n\t"
298                              "or        %3, %4, %0\n\t"
299                              : "=&r" (ret), "=&r" (addr),
300                                "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
301                              : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
302
303         return ret;
304 }
305
306 static void __hbird_write_stick(unsigned long val)
307 {
308         unsigned long low = (val & 0xffffffffUL);
309         unsigned long high = (val >> 32UL);
310         unsigned long addr = HBIRD_STICK_ADDR;
311
312         __asm__ __volatile__("stxa      %%g0, [%0] %4\n\t"
313                              "add       %0, 0x8, %0\n\t"
314                              "stxa      %3, [%0] %4\n\t"
315                              "sub       %0, 0x8, %0\n\t"
316                              "stxa      %2, [%0] %4"
317                              : "=&r" (addr)
318                              : "0" (addr), "r" (low), "r" (high),
319                                "i" (ASI_PHYS_BYPASS_EC_E));
320 }
321
322 static void __hbird_write_compare(unsigned long val)
323 {
324         unsigned long low = (val & 0xffffffffUL);
325         unsigned long high = (val >> 32UL);
326         unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
327
328         __asm__ __volatile__("stxa      %3, [%0] %4\n\t"
329                              "sub       %0, 0x8, %0\n\t"
330                              "stxa      %2, [%0] %4"
331                              : "=&r" (addr)
332                              : "0" (addr), "r" (low), "r" (high),
333                                "i" (ASI_PHYS_BYPASS_EC_E));
334 }
335
336 static void hbtick_disable_irq(void)
337 {
338         __hbird_write_compare(TICKCMP_IRQ_BIT);
339 }
340
341 static void hbtick_init_tick(void)
342 {
343         tick_disable_protection();
344
345         /* XXX This seems to be necessary to 'jumpstart' Hummingbird
346          * XXX into actually sending STICK interrupts.  I think because
347          * XXX of how we store %tick_cmpr in head.S this somehow resets the
348          * XXX {TICK + STICK} interrupt mux.  -DaveM
349          */
350         __hbird_write_stick(__hbird_read_stick());
351
352         hbtick_disable_irq();
353 }
354
355 static unsigned long hbtick_get_tick(void)
356 {
357         return __hbird_read_stick() & ~TICK_PRIV_BIT;
358 }
359
360 static unsigned long hbtick_add_tick(unsigned long adj)
361 {
362         unsigned long val;
363
364         val = __hbird_read_stick() + adj;
365         __hbird_write_stick(val);
366
367         return val;
368 }
369
370 static int hbtick_add_compare(unsigned long adj)
371 {
372         unsigned long val = __hbird_read_stick();
373         unsigned long val2;
374
375         val &= ~TICKCMP_IRQ_BIT;
376         val += adj;
377         __hbird_write_compare(val);
378
379         val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
380
381         return ((long)(val2 - val)) > 0L;
382 }
383
384 static struct sparc64_tick_ops hbtick_operations __read_mostly = {
385         .name           =       "hbtick",
386         .init_tick      =       hbtick_init_tick,
387         .disable_irq    =       hbtick_disable_irq,
388         .get_tick       =       hbtick_get_tick,
389         .add_tick       =       hbtick_add_tick,
390         .add_compare    =       hbtick_add_compare,
391         .softint_mask   =       1UL << 0,
392 };
393
394 static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
395
396 int update_persistent_clock(struct timespec now)
397 {
398         struct rtc_device *rtc = rtc_class_open("rtc0");
399
400         if (rtc)
401                 return rtc_set_mmss(rtc, now.tv_sec);
402
403         return -1;
404 }
405
406 /* davem suggests we keep this within the 4M locked kernel image */
407 static u32 starfire_get_time(void)
408 {
409         static char obp_gettod[32];
410         static u32 unix_tod;
411
412         sprintf(obp_gettod, "h# %08x unix-gettod",
413                 (unsigned int) (long) &unix_tod);
414         prom_feval(obp_gettod);
415
416         return unix_tod;
417 }
418
419 static int starfire_set_time(u32 val)
420 {
421         /* Do nothing, time is set using the service processor
422          * console on this platform.
423          */
424         return 0;
425 }
426
427 static u32 hypervisor_get_time(void)
428 {
429         unsigned long ret, time;
430         int retries = 10000;
431
432 retry:
433         ret = sun4v_tod_get(&time);
434         if (ret == HV_EOK)
435                 return time;
436         if (ret == HV_EWOULDBLOCK) {
437                 if (--retries > 0) {
438                         udelay(100);
439                         goto retry;
440                 }
441                 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
442                 return 0;
443         }
444         printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
445         return 0;
446 }
447
448 static int hypervisor_set_time(u32 secs)
449 {
450         unsigned long ret;
451         int retries = 10000;
452
453 retry:
454         ret = sun4v_tod_set(secs);
455         if (ret == HV_EOK)
456                 return 0;
457         if (ret == HV_EWOULDBLOCK) {
458                 if (--retries > 0) {
459                         udelay(100);
460                         goto retry;
461                 }
462                 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
463                 return -EAGAIN;
464         }
465         printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
466         return -EOPNOTSUPP;
467 }
468
469 unsigned long cmos_regs;
470 EXPORT_SYMBOL(cmos_regs);
471
472 struct resource rtc_cmos_resource;
473
474 static struct platform_device rtc_cmos_device = {
475         .name           = "rtc_cmos",
476         .id             = -1,
477         .resource       = &rtc_cmos_resource,
478         .num_resources  = 1,
479 };
480
481 static int __devinit rtc_probe(struct of_device *op, const struct of_device_id *match)
482 {
483         struct resource *r;
484
485         printk(KERN_INFO "%s: RTC regs at 0x%lx\n",
486                op->node->full_name, op->resource[0].start);
487
488         /* The CMOS RTC driver only accepts IORESOURCE_IO, so cons
489          * up a fake resource so that the probe works for all cases.
490          * When the RTC is behind an ISA bus it will have IORESOURCE_IO
491          * already, whereas when it's behind EBUS is will be IORESOURCE_MEM.
492          */
493
494         r = &rtc_cmos_resource;
495         r->flags = IORESOURCE_IO;
496         r->name = op->resource[0].name;
497         r->start = op->resource[0].start;
498         r->end = op->resource[0].end;
499
500         cmos_regs = op->resource[0].start;
501         return platform_device_register(&rtc_cmos_device);
502 }
503
504 static struct of_device_id rtc_match[] = {
505         {
506                 .name = "rtc",
507                 .compatible = "m5819",
508         },
509         {
510                 .name = "rtc",
511                 .compatible = "isa-m5819p",
512         },
513         {
514                 .name = "rtc",
515                 .compatible = "isa-m5823p",
516         },
517         {
518                 .name = "rtc",
519                 .compatible = "ds1287",
520         },
521         {},
522 };
523
524 static struct of_platform_driver rtc_driver = {
525         .match_table    = rtc_match,
526         .probe          = rtc_probe,
527         .driver         = {
528                 .name   = "rtc",
529         },
530 };
531
532 static struct platform_device rtc_bq4802_device = {
533         .name           = "rtc-bq4802",
534         .id             = -1,
535         .num_resources  = 1,
536 };
537
538 static int __devinit bq4802_probe(struct of_device *op, const struct of_device_id *match)
539 {
540
541         printk(KERN_INFO "%s: BQ4802 regs at 0x%lx\n",
542                op->node->full_name, op->resource[0].start);
543
544         rtc_bq4802_device.resource = &op->resource[0];
545         return platform_device_register(&rtc_bq4802_device);
546 }
547
548 static struct of_device_id bq4802_match[] = {
549         {
550                 .name = "rtc",
551                 .compatible = "bq4802",
552         },
553 };
554
555 static struct of_platform_driver bq4802_driver = {
556         .match_table    = bq4802_match,
557         .probe          = bq4802_probe,
558         .driver         = {
559                 .name   = "bq4802",
560         },
561 };
562
563 static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
564 {
565         struct platform_device *pdev = to_platform_device(dev);
566         void __iomem *regs;
567         unsigned char val;
568
569         regs = (void __iomem *) pdev->resource[0].start;
570         val = readb(regs + ofs);
571
572         /* the year 0 is 1968 */
573         if (ofs == M48T59_YEAR) {
574                 val += 0x68;
575                 if ((val & 0xf) > 9)
576                         val += 6;
577         }
578         return val;
579 }
580
581 static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
582 {
583         struct platform_device *pdev = to_platform_device(dev);
584         void __iomem *regs;
585
586         regs = (void __iomem *) pdev->resource[0].start;
587         if (ofs == M48T59_YEAR) {
588                 if (val < 0x68)
589                         val += 0x32;
590                 else
591                         val -= 0x68;
592                 if ((val & 0xf) > 9)
593                         val += 6;
594                 if ((val & 0xf0) > 0x9A)
595                         val += 0x60;
596         }
597         writeb(val, regs + ofs);
598 }
599
600 static struct m48t59_plat_data m48t59_data = {
601         .read_byte      = mostek_read_byte,
602         .write_byte     = mostek_write_byte,
603 };
604
605 static struct platform_device m48t59_rtc = {
606         .name           = "rtc-m48t59",
607         .id             = 0,
608         .num_resources  = 1,
609         .dev    = {
610                 .platform_data = &m48t59_data,
611         },
612 };
613
614 static int __devinit mostek_probe(struct of_device *op, const struct of_device_id *match)
615 {
616         struct device_node *dp = op->node;
617
618         /* On an Enterprise system there can be multiple mostek clocks.
619          * We should only match the one that is on the central FHC bus.
620          */
621         if (!strcmp(dp->parent->name, "fhc") &&
622             strcmp(dp->parent->parent->name, "central") != 0)
623                 return -ENODEV;
624
625         printk(KERN_INFO "%s: Mostek regs at 0x%lx\n",
626                dp->full_name, op->resource[0].start);
627
628         m48t59_rtc.resource = &op->resource[0];
629         return platform_device_register(&m48t59_rtc);
630 }
631
632 static struct of_device_id mostek_match[] = {
633         {
634                 .name = "eeprom",
635         },
636         {},
637 };
638
639 static struct of_platform_driver mostek_driver = {
640         .match_table    = mostek_match,
641         .probe          = mostek_probe,
642         .driver         = {
643                 .name   = "mostek",
644         },
645 };
646
647 static int __init clock_init(void)
648 {
649         if (this_is_starfire) {
650                 xtime.tv_sec = starfire_get_time();
651                 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
652                 set_normalized_timespec(&wall_to_monotonic,
653                                         -xtime.tv_sec, -xtime.tv_nsec);
654                 return 0;
655         }
656         if (tlb_type == hypervisor) {
657                 xtime.tv_sec = hypervisor_get_time();
658                 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
659                 set_normalized_timespec(&wall_to_monotonic,
660                                         -xtime.tv_sec, -xtime.tv_nsec);
661                 return 0;
662         }
663
664         (void) of_register_driver(&rtc_driver, &of_platform_bus_type);
665         (void) of_register_driver(&mostek_driver, &of_platform_bus_type);
666         (void) of_register_driver(&bq4802_driver, &of_platform_bus_type);
667
668         return 0;
669 }
670
671 /* Must be after subsys_initcall() so that busses are probed.  Must
672  * be before device_initcall() because things like the RTC driver
673  * need to see the clock registers.
674  */
675 fs_initcall(clock_init);
676
677 /* This is gets the master TICK_INT timer going. */
678 static unsigned long sparc64_init_timers(void)
679 {
680         struct device_node *dp;
681         unsigned long clock;
682
683         dp = of_find_node_by_path("/");
684         if (tlb_type == spitfire) {
685                 unsigned long ver, manuf, impl;
686
687                 __asm__ __volatile__ ("rdpr %%ver, %0"
688                                       : "=&r" (ver));
689                 manuf = ((ver >> 48) & 0xffff);
690                 impl = ((ver >> 32) & 0xffff);
691                 if (manuf == 0x17 && impl == 0x13) {
692                         /* Hummingbird, aka Ultra-IIe */
693                         tick_ops = &hbtick_operations;
694                         clock = of_getintprop_default(dp, "stick-frequency", 0);
695                 } else {
696                         tick_ops = &tick_operations;
697                         clock = local_cpu_data().clock_tick;
698                 }
699         } else {
700                 tick_ops = &stick_operations;
701                 clock = of_getintprop_default(dp, "stick-frequency", 0);
702         }
703
704         return clock;
705 }
706
707 struct freq_table {
708         unsigned long clock_tick_ref;
709         unsigned int ref_freq;
710 };
711 static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
712
713 unsigned long sparc64_get_clock_tick(unsigned int cpu)
714 {
715         struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
716
717         if (ft->clock_tick_ref)
718                 return ft->clock_tick_ref;
719         return cpu_data(cpu).clock_tick;
720 }
721
722 #ifdef CONFIG_CPU_FREQ
723
724 static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
725                                     void *data)
726 {
727         struct cpufreq_freqs *freq = data;
728         unsigned int cpu = freq->cpu;
729         struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
730
731         if (!ft->ref_freq) {
732                 ft->ref_freq = freq->old;
733                 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
734         }
735         if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
736             (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
737             (val == CPUFREQ_RESUMECHANGE)) {
738                 cpu_data(cpu).clock_tick =
739                         cpufreq_scale(ft->clock_tick_ref,
740                                       ft->ref_freq,
741                                       freq->new);
742         }
743
744         return 0;
745 }
746
747 static struct notifier_block sparc64_cpufreq_notifier_block = {
748         .notifier_call  = sparc64_cpufreq_notifier
749 };
750
751 static int __init register_sparc64_cpufreq_notifier(void)
752 {
753
754         cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
755                                   CPUFREQ_TRANSITION_NOTIFIER);
756         return 0;
757 }
758
759 core_initcall(register_sparc64_cpufreq_notifier);
760
761 #endif /* CONFIG_CPU_FREQ */
762
763 static int sparc64_next_event(unsigned long delta,
764                               struct clock_event_device *evt)
765 {
766         return tick_ops->add_compare(delta) ? -ETIME : 0;
767 }
768
769 static void sparc64_timer_setup(enum clock_event_mode mode,
770                                 struct clock_event_device *evt)
771 {
772         switch (mode) {
773         case CLOCK_EVT_MODE_ONESHOT:
774         case CLOCK_EVT_MODE_RESUME:
775                 break;
776
777         case CLOCK_EVT_MODE_SHUTDOWN:
778                 tick_ops->disable_irq();
779                 break;
780
781         case CLOCK_EVT_MODE_PERIODIC:
782         case CLOCK_EVT_MODE_UNUSED:
783                 WARN_ON(1);
784                 break;
785         };
786 }
787
788 static struct clock_event_device sparc64_clockevent = {
789         .features       = CLOCK_EVT_FEAT_ONESHOT,
790         .set_mode       = sparc64_timer_setup,
791         .set_next_event = sparc64_next_event,
792         .rating         = 100,
793         .shift          = 30,
794         .irq            = -1,
795 };
796 static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
797
798 void timer_interrupt(int irq, struct pt_regs *regs)
799 {
800         struct pt_regs *old_regs = set_irq_regs(regs);
801         unsigned long tick_mask = tick_ops->softint_mask;
802         int cpu = smp_processor_id();
803         struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
804
805         clear_softint(tick_mask);
806
807         irq_enter();
808
809         kstat_this_cpu.irqs[0]++;
810
811         if (unlikely(!evt->event_handler)) {
812                 printk(KERN_WARNING
813                        "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
814         } else
815                 evt->event_handler(evt);
816
817         irq_exit();
818
819         set_irq_regs(old_regs);
820 }
821
822 void __devinit setup_sparc64_timer(void)
823 {
824         struct clock_event_device *sevt;
825         unsigned long pstate;
826
827         /* Guarantee that the following sequences execute
828          * uninterrupted.
829          */
830         __asm__ __volatile__("rdpr      %%pstate, %0\n\t"
831                              "wrpr      %0, %1, %%pstate"
832                              : "=r" (pstate)
833                              : "i" (PSTATE_IE));
834
835         tick_ops->init_tick();
836
837         /* Restore PSTATE_IE. */
838         __asm__ __volatile__("wrpr      %0, 0x0, %%pstate"
839                              : /* no outputs */
840                              : "r" (pstate));
841
842         sevt = &__get_cpu_var(sparc64_events);
843
844         memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
845         sevt->cpumask = cpumask_of_cpu(smp_processor_id());
846
847         clockevents_register_device(sevt);
848 }
849
850 #define SPARC64_NSEC_PER_CYC_SHIFT      10UL
851
852 static struct clocksource clocksource_tick = {
853         .rating         = 100,
854         .mask           = CLOCKSOURCE_MASK(64),
855         .shift          = 16,
856         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
857 };
858
859 static void __init setup_clockevent_multiplier(unsigned long hz)
860 {
861         unsigned long mult, shift = 32;
862
863         while (1) {
864                 mult = div_sc(hz, NSEC_PER_SEC, shift);
865                 if (mult && (mult >> 32UL) == 0UL)
866                         break;
867
868                 shift--;
869         }
870
871         sparc64_clockevent.shift = shift;
872         sparc64_clockevent.mult = mult;
873 }
874
875 static unsigned long tb_ticks_per_usec __read_mostly;
876
877 void __delay(unsigned long loops)
878 {
879         unsigned long bclock, now;
880
881         bclock = tick_ops->get_tick();
882         do {
883                 now = tick_ops->get_tick();
884         } while ((now-bclock) < loops);
885 }
886 EXPORT_SYMBOL(__delay);
887
888 void udelay(unsigned long usecs)
889 {
890         __delay(tb_ticks_per_usec * usecs);
891 }
892 EXPORT_SYMBOL(udelay);
893
894 void __init time_init(void)
895 {
896         unsigned long clock = sparc64_init_timers();
897
898         tb_ticks_per_usec = clock / USEC_PER_SEC;
899
900         timer_ticks_per_nsec_quotient =
901                 clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
902
903         clocksource_tick.name = tick_ops->name;
904         clocksource_tick.mult =
905                 clocksource_hz2mult(clock,
906                                     clocksource_tick.shift);
907         clocksource_tick.read = tick_ops->get_tick;
908
909         printk("clocksource: mult[%x] shift[%d]\n",
910                clocksource_tick.mult, clocksource_tick.shift);
911
912         clocksource_register(&clocksource_tick);
913
914         sparc64_clockevent.name = tick_ops->name;
915
916         setup_clockevent_multiplier(clock);
917
918         sparc64_clockevent.max_delta_ns =
919                 clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
920         sparc64_clockevent.min_delta_ns =
921                 clockevent_delta2ns(0xF, &sparc64_clockevent);
922
923         printk("clockevent: mult[%lx] shift[%d]\n",
924                sparc64_clockevent.mult, sparc64_clockevent.shift);
925
926         setup_sparc64_timer();
927 }
928
929 unsigned long long sched_clock(void)
930 {
931         unsigned long ticks = tick_ops->get_tick();
932
933         return (ticks * timer_ticks_per_nsec_quotient)
934                 >> SPARC64_NSEC_PER_CYC_SHIFT;
935 }
936
937 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
938 static unsigned char mini_rtc_status;   /* bitmapped status byte.       */
939
940 #define FEBRUARY        2
941 #define STARTOFTIME     1970
942 #define SECDAY          86400L
943 #define SECYR           (SECDAY * 365)
944 #define leapyear(year)          ((year) % 4 == 0 && \
945                                  ((year) % 100 != 0 || (year) % 400 == 0))
946 #define days_in_year(a)         (leapyear(a) ? 366 : 365)
947 #define days_in_month(a)        (month_days[(a) - 1])
948
949 static int month_days[12] = {
950         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
951 };
952
953 /*
954  * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
955  */
956 static void GregorianDay(struct rtc_time * tm)
957 {
958         int leapsToDate;
959         int lastYear;
960         int day;
961         int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
962
963         lastYear = tm->tm_year - 1;
964
965         /*
966          * Number of leap corrections to apply up to end of last year
967          */
968         leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
969
970         /*
971          * This year is a leap year if it is divisible by 4 except when it is
972          * divisible by 100 unless it is divisible by 400
973          *
974          * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
975          */
976         day = tm->tm_mon > 2 && leapyear(tm->tm_year);
977
978         day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
979                    tm->tm_mday;
980
981         tm->tm_wday = day % 7;
982 }
983
984 static void to_tm(int tim, struct rtc_time *tm)
985 {
986         register int    i;
987         register long   hms, day;
988
989         day = tim / SECDAY;
990         hms = tim % SECDAY;
991
992         /* Hours, minutes, seconds are easy */
993         tm->tm_hour = hms / 3600;
994         tm->tm_min = (hms % 3600) / 60;
995         tm->tm_sec = (hms % 3600) % 60;
996
997         /* Number of years in days */
998         for (i = STARTOFTIME; day >= days_in_year(i); i++)
999                 day -= days_in_year(i);
1000         tm->tm_year = i;
1001
1002         /* Number of months in days left */
1003         if (leapyear(tm->tm_year))
1004                 days_in_month(FEBRUARY) = 29;
1005         for (i = 1; day >= days_in_month(i); i++)
1006                 day -= days_in_month(i);
1007         days_in_month(FEBRUARY) = 28;
1008         tm->tm_mon = i;
1009
1010         /* Days are what is left over (+1) from all that. */
1011         tm->tm_mday = day + 1;
1012
1013         /*
1014          * Determine the day of week
1015          */
1016         GregorianDay(tm);
1017 }
1018
1019 /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1020  * aka Unix time.  So we have to convert to/from rtc_time.
1021  */
1022 static void starfire_get_rtc_time(struct rtc_time *time)
1023 {
1024         u32 seconds = starfire_get_time();
1025
1026         to_tm(seconds, time);
1027         time->tm_year -= 1900;
1028         time->tm_mon -= 1;
1029 }
1030
1031 static int starfire_set_rtc_time(struct rtc_time *time)
1032 {
1033         u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1034                              time->tm_mday, time->tm_hour,
1035                              time->tm_min, time->tm_sec);
1036
1037         return starfire_set_time(seconds);
1038 }
1039
1040 static void hypervisor_get_rtc_time(struct rtc_time *time)
1041 {
1042         u32 seconds = hypervisor_get_time();
1043
1044         to_tm(seconds, time);
1045         time->tm_year -= 1900;
1046         time->tm_mon -= 1;
1047 }
1048
1049 static int hypervisor_set_rtc_time(struct rtc_time *time)
1050 {
1051         u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1052                              time->tm_mday, time->tm_hour,
1053                              time->tm_min, time->tm_sec);
1054
1055         return hypervisor_set_time(seconds);
1056 }
1057
1058 struct mini_rtc_ops {
1059         void (*get_rtc_time)(struct rtc_time *);
1060         int (*set_rtc_time)(struct rtc_time *);
1061 };
1062
1063 static struct mini_rtc_ops starfire_rtc_ops = {
1064         .get_rtc_time = starfire_get_rtc_time,
1065         .set_rtc_time = starfire_set_rtc_time,
1066 };
1067
1068 static struct mini_rtc_ops hypervisor_rtc_ops = {
1069         .get_rtc_time = hypervisor_get_rtc_time,
1070         .set_rtc_time = hypervisor_set_rtc_time,
1071 };
1072
1073 static struct mini_rtc_ops *mini_rtc_ops;
1074
1075 static inline void mini_get_rtc_time(struct rtc_time *time)
1076 {
1077         unsigned long flags;
1078
1079         spin_lock_irqsave(&rtc_lock, flags);
1080         mini_rtc_ops->get_rtc_time(time);
1081         spin_unlock_irqrestore(&rtc_lock, flags);
1082 }
1083
1084 static inline int mini_set_rtc_time(struct rtc_time *time)
1085 {
1086         unsigned long flags;
1087         int err;
1088
1089         spin_lock_irqsave(&rtc_lock, flags);
1090         err = mini_rtc_ops->set_rtc_time(time);
1091         spin_unlock_irqrestore(&rtc_lock, flags);
1092
1093         return err;
1094 }
1095
1096 static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1097                           unsigned int cmd, unsigned long arg)
1098 {
1099         struct rtc_time wtime;
1100         void __user *argp = (void __user *)arg;
1101
1102         switch (cmd) {
1103
1104         case RTC_PLL_GET:
1105                 return -EINVAL;
1106
1107         case RTC_PLL_SET:
1108                 return -EINVAL;
1109
1110         case RTC_UIE_OFF:       /* disable ints from RTC updates.       */
1111                 return 0;
1112
1113         case RTC_UIE_ON:        /* enable ints for RTC updates. */
1114                 return -EINVAL;
1115
1116         case RTC_RD_TIME:       /* Read the time/date from RTC  */
1117                 /* this doesn't get week-day, who cares */
1118                 memset(&wtime, 0, sizeof(wtime));
1119                 mini_get_rtc_time(&wtime);
1120
1121                 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1122
1123         case RTC_SET_TIME:      /* Set the RTC */
1124             {
1125                 int year, days;
1126
1127                 if (!capable(CAP_SYS_TIME))
1128                         return -EACCES;
1129
1130                 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1131                         return -EFAULT;
1132
1133                 year = wtime.tm_year + 1900;
1134                 days = month_days[wtime.tm_mon] +
1135                        ((wtime.tm_mon == 1) && leapyear(year));
1136
1137                 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
1138                     (wtime.tm_mday < 1))
1139                         return -EINVAL;
1140
1141                 if (wtime.tm_mday < 0 || wtime.tm_mday > days)
1142                         return -EINVAL;
1143
1144                 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1145                     wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1146                     wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1147                         return -EINVAL;
1148
1149                 return mini_set_rtc_time(&wtime);
1150             }
1151         }
1152
1153         return -EINVAL;
1154 }
1155
1156 static int mini_rtc_open(struct inode *inode, struct file *file)
1157 {
1158         lock_kernel();
1159         if (mini_rtc_status & RTC_IS_OPEN) {
1160                 unlock_kernel();
1161                 return -EBUSY;
1162         }
1163
1164         mini_rtc_status |= RTC_IS_OPEN;
1165         unlock_kernel();
1166
1167         return 0;
1168 }
1169
1170 static int mini_rtc_release(struct inode *inode, struct file *file)
1171 {
1172         mini_rtc_status &= ~RTC_IS_OPEN;
1173         return 0;
1174 }
1175
1176
1177 static const struct file_operations mini_rtc_fops = {
1178         .owner          = THIS_MODULE,
1179         .ioctl          = mini_rtc_ioctl,
1180         .open           = mini_rtc_open,
1181         .release        = mini_rtc_release,
1182 };
1183
1184 static struct miscdevice rtc_mini_dev =
1185 {
1186         .minor          = RTC_MINOR,
1187         .name           = "rtc",
1188         .fops           = &mini_rtc_fops,
1189 };
1190
1191 static int __init rtc_mini_init(void)
1192 {
1193         int retval;
1194
1195         if (tlb_type == hypervisor)
1196                 mini_rtc_ops = &hypervisor_rtc_ops;
1197         else if (this_is_starfire)
1198                 mini_rtc_ops = &starfire_rtc_ops;
1199         else
1200                 return -ENODEV;
1201
1202         printk(KERN_INFO "Mini RTC Driver\n");
1203
1204         retval = misc_register(&rtc_mini_dev);
1205         if (retval < 0)
1206                 return retval;
1207
1208         return 0;
1209 }
1210
1211 static void __exit rtc_mini_exit(void)
1212 {
1213         misc_deregister(&rtc_mini_dev);
1214 }
1215
1216 int __devinit read_current_timer(unsigned long *timer_val)
1217 {
1218         *timer_val = tick_ops->get_tick();
1219         return 0;
1220 }
1221
1222 module_init(rtc_mini_init);
1223 module_exit(rtc_mini_exit);