1 /* $Id: time.c,v 1.42 2002/01/23 14:33:55 davem Exp $
2 * time.c: UltraSparc timer and TOD clock support.
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
7 * Based largely on code which is:
9 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/param.h>
17 #include <linux/string.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/profile.h>
32 #include <linux/miscdevice.h>
33 #include <linux/rtc.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/clockchips.h>
36 #include <linux/clocksource.h>
38 #include <asm/oplib.h>
39 #include <asm/mostek.h>
40 #include <asm/timer.h>
44 #include <asm/of_device.h>
45 #include <asm/starfire.h>
47 #include <asm/sections.h>
48 #include <asm/cpudata.h>
49 #include <asm/uaccess.h>
51 #include <asm/irq_regs.h>
53 DEFINE_SPINLOCK(mostek_lock
);
54 DEFINE_SPINLOCK(rtc_lock
);
55 void __iomem
*mstk48t02_regs
= NULL
;
57 unsigned long ds1287_regs
= 0UL;
60 static void __iomem
*mstk48t08_regs
;
61 static void __iomem
*mstk48t59_regs
;
63 static int set_rtc_mmss(unsigned long);
65 #define TICK_PRIV_BIT (1UL << 63)
66 #define TICKCMP_IRQ_BIT (1UL << 63)
69 unsigned long profile_pc(struct pt_regs
*regs
)
71 unsigned long pc
= instruction_pointer(regs
);
73 if (in_lock_functions(pc
))
74 return regs
->u_regs
[UREG_RETPC
];
77 EXPORT_SYMBOL(profile_pc
);
80 static void tick_disable_protection(void)
82 /* Set things up so user can access tick register for profiling
83 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
84 * read back of %tick after writing it.
90 "1: rd %%tick, %%g2\n"
91 " add %%g2, 6, %%g2\n"
92 " andn %%g2, %0, %%g2\n"
93 " wrpr %%g2, 0, %%tick\n"
100 static void tick_disable_irq(void)
102 __asm__
__volatile__(
106 "1: wr %0, 0x0, %%tick_cmpr\n"
107 " rd %%tick_cmpr, %%g0"
109 : "r" (TICKCMP_IRQ_BIT
));
112 static void tick_init_tick(void)
114 tick_disable_protection();
118 static unsigned long tick_get_tick(void)
122 __asm__
__volatile__("rd %%tick, %0\n\t"
126 return ret
& ~TICK_PRIV_BIT
;
129 static int tick_add_compare(unsigned long adj
)
131 unsigned long orig_tick
, new_tick
, new_compare
;
133 __asm__
__volatile__("rd %%tick, %0"
136 orig_tick
&= ~TICKCMP_IRQ_BIT
;
138 /* Workaround for Spitfire Errata (#54 I think??), I discovered
139 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
142 * On Blackbird writes to %tick_cmpr can fail, the
143 * workaround seems to be to execute the wr instruction
144 * at the start of an I-cache line, and perform a dummy
145 * read back from %tick_cmpr right after writing to it. -DaveM
147 __asm__
__volatile__("ba,pt %%xcc, 1f\n\t"
148 " add %1, %2, %0\n\t"
151 "wr %0, 0, %%tick_cmpr\n\t"
152 "rd %%tick_cmpr, %%g0\n\t"
154 : "r" (orig_tick
), "r" (adj
));
156 __asm__
__volatile__("rd %%tick, %0"
158 new_tick
&= ~TICKCMP_IRQ_BIT
;
160 return ((long)(new_tick
- (orig_tick
+adj
))) > 0L;
163 static unsigned long tick_add_tick(unsigned long adj
)
165 unsigned long new_tick
;
167 /* Also need to handle Blackbird bug here too. */
168 __asm__
__volatile__("rd %%tick, %0\n\t"
170 "wrpr %0, 0, %%tick\n\t"
177 static struct sparc64_tick_ops tick_operations __read_mostly
= {
179 .init_tick
= tick_init_tick
,
180 .disable_irq
= tick_disable_irq
,
181 .get_tick
= tick_get_tick
,
182 .add_tick
= tick_add_tick
,
183 .add_compare
= tick_add_compare
,
184 .softint_mask
= 1UL << 0,
187 struct sparc64_tick_ops
*tick_ops __read_mostly
= &tick_operations
;
189 static void stick_disable_irq(void)
191 __asm__
__volatile__(
192 "wr %0, 0x0, %%asr25"
194 : "r" (TICKCMP_IRQ_BIT
));
197 static void stick_init_tick(void)
199 /* Writes to the %tick and %stick register are not
200 * allowed on sun4v. The Hypervisor controls that
203 if (tlb_type
!= hypervisor
) {
204 tick_disable_protection();
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"
213 : "r" (TICK_PRIV_BIT
)
220 static unsigned long stick_get_tick(void)
224 __asm__
__volatile__("rd %%asr24, %0"
227 return ret
& ~TICK_PRIV_BIT
;
230 static unsigned long stick_add_tick(unsigned long adj
)
232 unsigned long new_tick
;
234 __asm__
__volatile__("rd %%asr24, %0\n\t"
236 "wr %0, 0, %%asr24\n\t"
243 static int stick_add_compare(unsigned long adj
)
245 unsigned long orig_tick
, new_tick
;
247 __asm__
__volatile__("rd %%asr24, %0"
249 orig_tick
&= ~TICKCMP_IRQ_BIT
;
251 __asm__
__volatile__("wr %0, 0, %%asr25"
253 : "r" (orig_tick
+ adj
));
255 __asm__
__volatile__("rd %%asr24, %0"
257 new_tick
&= ~TICKCMP_IRQ_BIT
;
259 return ((long)(new_tick
- (orig_tick
+adj
))) > 0L;
262 static struct sparc64_tick_ops stick_operations __read_mostly
= {
264 .init_tick
= stick_init_tick
,
265 .disable_irq
= stick_disable_irq
,
266 .get_tick
= stick_get_tick
,
267 .add_tick
= stick_add_tick
,
268 .add_compare
= stick_add_compare
,
269 .softint_mask
= 1UL << 16,
272 /* On Hummingbird the STICK/STICK_CMPR register is implemented
273 * in I/O space. There are two 64-bit registers each, the
274 * first holds the low 32-bits of the value and the second holds
277 * Since STICK is constantly updating, we have to access it carefully.
279 * The sequence we use to read is:
282 * 3) read high again, if it rolled re-read both low and high again.
284 * Writing STICK safely is also tricky:
285 * 1) write low to zero
289 #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
290 #define HBIRD_STICK_ADDR 0x1fe0000f070UL
292 static unsigned long __hbird_read_stick(void)
294 unsigned long ret
, tmp1
, tmp2
, tmp3
;
295 unsigned long addr
= HBIRD_STICK_ADDR
+8;
297 __asm__
__volatile__("ldxa [%1] %5, %2\n"
299 "sub %1, 0x8, %1\n\t"
300 "ldxa [%1] %5, %3\n\t"
301 "add %1, 0x8, %1\n\t"
302 "ldxa [%1] %5, %4\n\t"
304 "bne,a,pn %%xcc, 1b\n\t"
306 "sllx %4, 32, %4\n\t"
308 : "=&r" (ret
), "=&r" (addr
),
309 "=&r" (tmp1
), "=&r" (tmp2
), "=&r" (tmp3
)
310 : "i" (ASI_PHYS_BYPASS_EC_E
), "1" (addr
));
315 static void __hbird_write_stick(unsigned long val
)
317 unsigned long low
= (val
& 0xffffffffUL
);
318 unsigned long high
= (val
>> 32UL);
319 unsigned long addr
= HBIRD_STICK_ADDR
;
321 __asm__
__volatile__("stxa %%g0, [%0] %4\n\t"
322 "add %0, 0x8, %0\n\t"
323 "stxa %3, [%0] %4\n\t"
324 "sub %0, 0x8, %0\n\t"
327 : "0" (addr
), "r" (low
), "r" (high
),
328 "i" (ASI_PHYS_BYPASS_EC_E
));
331 static void __hbird_write_compare(unsigned long val
)
333 unsigned long low
= (val
& 0xffffffffUL
);
334 unsigned long high
= (val
>> 32UL);
335 unsigned long addr
= HBIRD_STICKCMP_ADDR
+ 0x8UL
;
337 __asm__
__volatile__("stxa %3, [%0] %4\n\t"
338 "sub %0, 0x8, %0\n\t"
341 : "0" (addr
), "r" (low
), "r" (high
),
342 "i" (ASI_PHYS_BYPASS_EC_E
));
345 static void hbtick_disable_irq(void)
347 __hbird_write_compare(TICKCMP_IRQ_BIT
);
350 static void hbtick_init_tick(void)
352 tick_disable_protection();
354 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
355 * XXX into actually sending STICK interrupts. I think because
356 * XXX of how we store %tick_cmpr in head.S this somehow resets the
357 * XXX {TICK + STICK} interrupt mux. -DaveM
359 __hbird_write_stick(__hbird_read_stick());
361 hbtick_disable_irq();
364 static unsigned long hbtick_get_tick(void)
366 return __hbird_read_stick() & ~TICK_PRIV_BIT
;
369 static unsigned long hbtick_add_tick(unsigned long adj
)
373 val
= __hbird_read_stick() + adj
;
374 __hbird_write_stick(val
);
379 static int hbtick_add_compare(unsigned long adj
)
381 unsigned long val
= __hbird_read_stick();
384 val
&= ~TICKCMP_IRQ_BIT
;
386 __hbird_write_compare(val
);
388 val2
= __hbird_read_stick() & ~TICKCMP_IRQ_BIT
;
390 return ((long)(val2
- val
)) > 0L;
393 static struct sparc64_tick_ops hbtick_operations __read_mostly
= {
395 .init_tick
= hbtick_init_tick
,
396 .disable_irq
= hbtick_disable_irq
,
397 .get_tick
= hbtick_get_tick
,
398 .add_tick
= hbtick_add_tick
,
399 .add_compare
= hbtick_add_compare
,
400 .softint_mask
= 1UL << 0,
403 static unsigned long timer_ticks_per_nsec_quotient __read_mostly
;
405 #define TICK_SIZE (tick_nsec / 1000)
407 #define USEC_AFTER 500000
408 #define USEC_BEFORE 500000
410 static void sync_cmos_clock(unsigned long dummy
);
412 static DEFINE_TIMER(sync_cmos_timer
, sync_cmos_clock
, 0, 0);
414 static void sync_cmos_clock(unsigned long dummy
)
416 struct timeval now
, next
;
420 * If we have an externally synchronized Linux clock, then update
421 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
422 * called as close as possible to 500 ms before the new second starts.
423 * This code is run on a timer. If the clock is set, that timer
424 * may not expire at the correct time. Thus, we adjust...
428 * Not synced, exit, do not restart a timer (if one is
429 * running, let it run out).
433 do_gettimeofday(&now
);
434 if (now
.tv_usec
>= USEC_AFTER
- ((unsigned) TICK_SIZE
) / 2 &&
435 now
.tv_usec
<= USEC_BEFORE
+ ((unsigned) TICK_SIZE
) / 2)
436 fail
= set_rtc_mmss(now
.tv_sec
);
438 next
.tv_usec
= USEC_AFTER
- now
.tv_usec
;
439 if (next
.tv_usec
<= 0)
440 next
.tv_usec
+= USEC_PER_SEC
;
447 if (next
.tv_usec
>= USEC_PER_SEC
) {
449 next
.tv_usec
-= USEC_PER_SEC
;
451 mod_timer(&sync_cmos_timer
, jiffies
+ timeval_to_jiffies(&next
));
454 void notify_arch_cmos_timer(void)
456 mod_timer(&sync_cmos_timer
, jiffies
+ 1);
459 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
460 static void __init
kick_start_clock(void)
462 void __iomem
*regs
= mstk48t02_regs
;
466 prom_printf("CLOCK: Clock was stopped. Kick start ");
468 spin_lock_irq(&mostek_lock
);
470 /* Turn on the kick start bit to start the oscillator. */
471 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
472 tmp
|= MSTK_CREG_WRITE
;
473 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
474 tmp
= mostek_read(regs
+ MOSTEK_SEC
);
476 mostek_write(regs
+ MOSTEK_SEC
, tmp
);
477 tmp
= mostek_read(regs
+ MOSTEK_HOUR
);
478 tmp
|= MSTK_KICK_START
;
479 mostek_write(regs
+ MOSTEK_HOUR
, tmp
);
480 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
481 tmp
&= ~MSTK_CREG_WRITE
;
482 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
484 spin_unlock_irq(&mostek_lock
);
486 /* Delay to allow the clock oscillator to start. */
487 sec
= MSTK_REG_SEC(regs
);
488 for (i
= 0; i
< 3; i
++) {
489 while (sec
== MSTK_REG_SEC(regs
))
490 for (count
= 0; count
< 100000; count
++)
493 sec
= MSTK_REG_SEC(regs
);
497 spin_lock_irq(&mostek_lock
);
499 /* Turn off kick start and set a "valid" time and date. */
500 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
501 tmp
|= MSTK_CREG_WRITE
;
502 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
503 tmp
= mostek_read(regs
+ MOSTEK_HOUR
);
504 tmp
&= ~MSTK_KICK_START
;
505 mostek_write(regs
+ MOSTEK_HOUR
, tmp
);
506 MSTK_SET_REG_SEC(regs
,0);
507 MSTK_SET_REG_MIN(regs
,0);
508 MSTK_SET_REG_HOUR(regs
,0);
509 MSTK_SET_REG_DOW(regs
,5);
510 MSTK_SET_REG_DOM(regs
,1);
511 MSTK_SET_REG_MONTH(regs
,8);
512 MSTK_SET_REG_YEAR(regs
,1996 - MSTK_YEAR_ZERO
);
513 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
514 tmp
&= ~MSTK_CREG_WRITE
;
515 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
517 spin_unlock_irq(&mostek_lock
);
519 /* Ensure the kick start bit is off. If it isn't, turn it off. */
520 while (mostek_read(regs
+ MOSTEK_HOUR
) & MSTK_KICK_START
) {
521 prom_printf("CLOCK: Kick start still on!\n");
523 spin_lock_irq(&mostek_lock
);
525 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
526 tmp
|= MSTK_CREG_WRITE
;
527 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
529 tmp
= mostek_read(regs
+ MOSTEK_HOUR
);
530 tmp
&= ~MSTK_KICK_START
;
531 mostek_write(regs
+ MOSTEK_HOUR
, tmp
);
533 tmp
= mostek_read(regs
+ MOSTEK_CREG
);
534 tmp
&= ~MSTK_CREG_WRITE
;
535 mostek_write(regs
+ MOSTEK_CREG
, tmp
);
537 spin_unlock_irq(&mostek_lock
);
540 prom_printf("CLOCK: Kick start procedure successful.\n");
543 /* Return nonzero if the clock chip battery is low. */
544 static int __init
has_low_battery(void)
546 void __iomem
*regs
= mstk48t02_regs
;
549 spin_lock_irq(&mostek_lock
);
551 data1
= mostek_read(regs
+ MOSTEK_EEPROM
); /* Read some data. */
552 mostek_write(regs
+ MOSTEK_EEPROM
, ~data1
); /* Write back the complement. */
553 data2
= mostek_read(regs
+ MOSTEK_EEPROM
); /* Read back the complement. */
554 mostek_write(regs
+ MOSTEK_EEPROM
, data1
); /* Restore original value. */
556 spin_unlock_irq(&mostek_lock
);
558 return (data1
== data2
); /* Was the write blocked? */
561 /* Probe for the real time clock chip. */
562 static void __init
set_system_time(void)
564 unsigned int year
, mon
, day
, hour
, min
, sec
;
565 void __iomem
*mregs
= mstk48t02_regs
;
567 unsigned long dregs
= ds1287_regs
;
569 unsigned long dregs
= 0UL;
573 if (!mregs
&& !dregs
) {
574 prom_printf("Something wrong, clock regs not mapped yet.\n");
579 spin_lock_irq(&mostek_lock
);
581 /* Traditional Mostek chip. */
582 tmp
= mostek_read(mregs
+ MOSTEK_CREG
);
583 tmp
|= MSTK_CREG_READ
;
584 mostek_write(mregs
+ MOSTEK_CREG
, tmp
);
586 sec
= MSTK_REG_SEC(mregs
);
587 min
= MSTK_REG_MIN(mregs
);
588 hour
= MSTK_REG_HOUR(mregs
);
589 day
= MSTK_REG_DOM(mregs
);
590 mon
= MSTK_REG_MONTH(mregs
);
591 year
= MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs
) );
593 /* Dallas 12887 RTC chip. */
596 sec
= CMOS_READ(RTC_SECONDS
);
597 min
= CMOS_READ(RTC_MINUTES
);
598 hour
= CMOS_READ(RTC_HOURS
);
599 day
= CMOS_READ(RTC_DAY_OF_MONTH
);
600 mon
= CMOS_READ(RTC_MONTH
);
601 year
= CMOS_READ(RTC_YEAR
);
602 } while (sec
!= CMOS_READ(RTC_SECONDS
));
604 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
612 if ((year
+= 1900) < 1970)
616 xtime
.tv_sec
= mktime(year
, mon
, day
, hour
, min
, sec
);
617 xtime
.tv_nsec
= (INITIAL_JIFFIES
% HZ
) * (NSEC_PER_SEC
/ HZ
);
618 set_normalized_timespec(&wall_to_monotonic
,
619 -xtime
.tv_sec
, -xtime
.tv_nsec
);
622 tmp
= mostek_read(mregs
+ MOSTEK_CREG
);
623 tmp
&= ~MSTK_CREG_READ
;
624 mostek_write(mregs
+ MOSTEK_CREG
, tmp
);
626 spin_unlock_irq(&mostek_lock
);
630 /* davem suggests we keep this within the 4M locked kernel image */
631 static u32
starfire_get_time(void)
633 static char obp_gettod
[32];
636 sprintf(obp_gettod
, "h# %08x unix-gettod",
637 (unsigned int) (long) &unix_tod
);
638 prom_feval(obp_gettod
);
643 static int starfire_set_time(u32 val
)
645 /* Do nothing, time is set using the service processor
646 * console on this platform.
651 static u32
hypervisor_get_time(void)
653 register unsigned long func
asm("%o5");
654 register unsigned long arg0
asm("%o0");
655 register unsigned long arg1
asm("%o1");
659 func
= HV_FAST_TOD_GET
;
662 __asm__
__volatile__("ta %6"
663 : "=&r" (func
), "=&r" (arg0
), "=&r" (arg1
)
664 : "0" (func
), "1" (arg0
), "2" (arg1
),
668 if (arg0
== HV_EWOULDBLOCK
) {
673 printk(KERN_WARNING
"SUN4V: tod_get() timed out.\n");
676 printk(KERN_WARNING
"SUN4V: tod_get() not supported.\n");
680 static int hypervisor_set_time(u32 secs
)
682 register unsigned long func
asm("%o5");
683 register unsigned long arg0
asm("%o0");
687 func
= HV_FAST_TOD_SET
;
689 __asm__
__volatile__("ta %4"
690 : "=&r" (func
), "=&r" (arg0
)
691 : "0" (func
), "1" (arg0
),
695 if (arg0
== HV_EWOULDBLOCK
) {
700 printk(KERN_WARNING
"SUN4V: tod_set() timed out.\n");
703 printk(KERN_WARNING
"SUN4V: tod_set() not supported.\n");
707 static int __init
clock_model_matches(char *model
)
709 if (strcmp(model
, "mk48t02") &&
710 strcmp(model
, "mk48t08") &&
711 strcmp(model
, "mk48t59") &&
712 strcmp(model
, "m5819") &&
713 strcmp(model
, "m5819p") &&
714 strcmp(model
, "m5823") &&
715 strcmp(model
, "ds1287"))
721 static int __devinit
clock_probe(struct of_device
*op
, const struct of_device_id
*match
)
723 struct device_node
*dp
= op
->node
;
724 char *model
= of_get_property(dp
, "model", NULL
);
725 unsigned long size
, flags
;
728 if (!model
|| !clock_model_matches(model
))
731 /* On an Enterprise system there can be multiple mostek clocks.
732 * We should only match the one that is on the central FHC bus.
734 if (!strcmp(dp
->parent
->name
, "fhc") &&
735 strcmp(dp
->parent
->parent
->name
, "central") != 0)
738 size
= (op
->resource
[0].end
- op
->resource
[0].start
) + 1;
739 regs
= of_ioremap(&op
->resource
[0], 0, size
, "clock");
744 if (!strcmp(model
, "ds1287") ||
745 !strcmp(model
, "m5819") ||
746 !strcmp(model
, "m5819p") ||
747 !strcmp(model
, "m5823")) {
748 ds1287_regs
= (unsigned long) regs
;
751 if (model
[5] == '0' && model
[6] == '2') {
752 mstk48t02_regs
= regs
;
753 } else if(model
[5] == '0' && model
[6] == '8') {
754 mstk48t08_regs
= regs
;
755 mstk48t02_regs
= mstk48t08_regs
+ MOSTEK_48T08_48T02
;
757 mstk48t59_regs
= regs
;
758 mstk48t02_regs
= mstk48t59_regs
+ MOSTEK_48T59_48T02
;
761 printk(KERN_INFO
"%s: Clock regs at %p\n", dp
->full_name
, regs
);
763 local_irq_save(flags
);
765 if (mstk48t02_regs
!= NULL
) {
766 /* Report a low battery voltage condition. */
767 if (has_low_battery())
768 prom_printf("NVRAM: Low battery voltage!\n");
770 /* Kick start the clock if it is completely stopped. */
771 if (mostek_read(mstk48t02_regs
+ MOSTEK_SEC
) & MSTK_STOP
)
777 local_irq_restore(flags
);
782 static struct of_device_id clock_match
[] = {
792 static struct of_platform_driver clock_driver
= {
794 .match_table
= clock_match
,
795 .probe
= clock_probe
,
798 static int __init
clock_init(void)
800 if (this_is_starfire
) {
801 xtime
.tv_sec
= starfire_get_time();
802 xtime
.tv_nsec
= (INITIAL_JIFFIES
% HZ
) * (NSEC_PER_SEC
/ HZ
);
803 set_normalized_timespec(&wall_to_monotonic
,
804 -xtime
.tv_sec
, -xtime
.tv_nsec
);
807 if (tlb_type
== hypervisor
) {
808 xtime
.tv_sec
= hypervisor_get_time();
809 xtime
.tv_nsec
= (INITIAL_JIFFIES
% HZ
) * (NSEC_PER_SEC
/ HZ
);
810 set_normalized_timespec(&wall_to_monotonic
,
811 -xtime
.tv_sec
, -xtime
.tv_nsec
);
815 return of_register_driver(&clock_driver
, &of_bus_type
);
818 /* Must be after subsys_initcall() so that busses are probed. Must
819 * be before device_initcall() because things like the RTC driver
820 * need to see the clock registers.
822 fs_initcall(clock_init
);
824 /* This is gets the master TICK_INT timer going. */
825 static unsigned long sparc64_init_timers(void)
827 struct device_node
*dp
;
828 struct property
*prop
;
831 extern void smp_tick_init(void);
834 dp
= of_find_node_by_path("/");
835 if (tlb_type
== spitfire
) {
836 unsigned long ver
, manuf
, impl
;
838 __asm__
__volatile__ ("rdpr %%ver, %0"
840 manuf
= ((ver
>> 48) & 0xffff);
841 impl
= ((ver
>> 32) & 0xffff);
842 if (manuf
== 0x17 && impl
== 0x13) {
843 /* Hummingbird, aka Ultra-IIe */
844 tick_ops
= &hbtick_operations
;
845 prop
= of_find_property(dp
, "stick-frequency", NULL
);
847 tick_ops
= &tick_operations
;
848 cpu_find_by_instance(0, &dp
, NULL
);
849 prop
= of_find_property(dp
, "clock-frequency", NULL
);
852 tick_ops
= &stick_operations
;
853 prop
= of_find_property(dp
, "stick-frequency", NULL
);
855 clock
= *(unsigned int *) prop
->value
;
865 unsigned long clock_tick_ref
;
866 unsigned int ref_freq
;
868 static DEFINE_PER_CPU(struct freq_table
, sparc64_freq_table
) = { 0, 0 };
870 unsigned long sparc64_get_clock_tick(unsigned int cpu
)
872 struct freq_table
*ft
= &per_cpu(sparc64_freq_table
, cpu
);
874 if (ft
->clock_tick_ref
)
875 return ft
->clock_tick_ref
;
876 return cpu_data(cpu
).clock_tick
;
879 #ifdef CONFIG_CPU_FREQ
881 static int sparc64_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
884 struct cpufreq_freqs
*freq
= data
;
885 unsigned int cpu
= freq
->cpu
;
886 struct freq_table
*ft
= &per_cpu(sparc64_freq_table
, cpu
);
889 ft
->ref_freq
= freq
->old
;
890 ft
->clock_tick_ref
= cpu_data(cpu
).clock_tick
;
892 if ((val
== CPUFREQ_PRECHANGE
&& freq
->old
< freq
->new) ||
893 (val
== CPUFREQ_POSTCHANGE
&& freq
->old
> freq
->new) ||
894 (val
== CPUFREQ_RESUMECHANGE
)) {
895 cpu_data(cpu
).clock_tick
=
896 cpufreq_scale(ft
->clock_tick_ref
,
904 static struct notifier_block sparc64_cpufreq_notifier_block
= {
905 .notifier_call
= sparc64_cpufreq_notifier
908 #endif /* CONFIG_CPU_FREQ */
910 static int sparc64_next_event(unsigned long delta
,
911 struct clock_event_device
*evt
)
913 return tick_ops
->add_compare(delta
);
916 static void sparc64_timer_setup(enum clock_event_mode mode
,
917 struct clock_event_device
*evt
)
920 case CLOCK_EVT_MODE_ONESHOT
:
923 case CLOCK_EVT_MODE_SHUTDOWN
:
924 tick_ops
->disable_irq();
927 case CLOCK_EVT_MODE_PERIODIC
:
928 case CLOCK_EVT_MODE_UNUSED
:
934 static struct clock_event_device sparc64_clockevent
= {
935 .features
= CLOCK_EVT_FEAT_ONESHOT
,
936 .set_mode
= sparc64_timer_setup
,
937 .set_next_event
= sparc64_next_event
,
942 static DEFINE_PER_CPU(struct clock_event_device
, sparc64_events
);
944 void timer_interrupt(int irq
, struct pt_regs
*regs
)
946 struct pt_regs
*old_regs
= set_irq_regs(regs
);
947 unsigned long tick_mask
= tick_ops
->softint_mask
;
948 int cpu
= smp_processor_id();
949 struct clock_event_device
*evt
= &per_cpu(sparc64_events
, cpu
);
951 clear_softint(tick_mask
);
955 kstat_this_cpu
.irqs
[0]++;
957 if (unlikely(!evt
->event_handler
)) {
959 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu
);
961 evt
->event_handler(evt
);
965 set_irq_regs(old_regs
);
968 void __devinit
setup_sparc64_timer(void)
970 struct clock_event_device
*sevt
;
971 unsigned long pstate
;
973 /* Guarantee that the following sequences execute
976 __asm__
__volatile__("rdpr %%pstate, %0\n\t"
977 "wrpr %0, %1, %%pstate"
981 tick_ops
->init_tick();
983 /* Restore PSTATE_IE. */
984 __asm__
__volatile__("wrpr %0, 0x0, %%pstate"
988 sevt
= &__get_cpu_var(sparc64_events
);
990 memcpy(sevt
, &sparc64_clockevent
, sizeof(*sevt
));
991 sevt
->cpumask
= cpumask_of_cpu(smp_processor_id());
993 clockevents_register_device(sevt
);
996 #define SPARC64_NSEC_PER_CYC_SHIFT 32UL
998 static struct clocksource clocksource_tick
= {
1000 .mask
= CLOCKSOURCE_MASK(64),
1002 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
1005 static void __init
setup_clockevent_multiplier(unsigned long hz
)
1007 unsigned long mult
, shift
= 32;
1010 mult
= div_sc(hz
, NSEC_PER_SEC
, shift
);
1011 if (mult
&& (mult
>> 32UL) == 0UL)
1017 sparc64_clockevent
.shift
= shift
;
1018 sparc64_clockevent
.mult
= mult
;
1021 void __init
time_init(void)
1023 unsigned long clock
= sparc64_init_timers();
1025 timer_ticks_per_nsec_quotient
=
1026 clocksource_hz2mult(clock
, SPARC64_NSEC_PER_CYC_SHIFT
);
1028 clocksource_tick
.name
= tick_ops
->name
;
1029 clocksource_tick
.mult
=
1030 clocksource_hz2mult(clock
,
1031 clocksource_tick
.shift
);
1032 clocksource_tick
.read
= tick_ops
->get_tick
;
1034 printk("clocksource: mult[%x] shift[%d]\n",
1035 clocksource_tick
.mult
, clocksource_tick
.shift
);
1037 clocksource_register(&clocksource_tick
);
1039 sparc64_clockevent
.name
= tick_ops
->name
;
1041 setup_clockevent_multiplier(clock
);
1043 sparc64_clockevent
.max_delta_ns
=
1044 clockevent_delta2ns(0x7fffffffffffffff, &sparc64_clockevent
);
1045 sparc64_clockevent
.min_delta_ns
=
1046 clockevent_delta2ns(0xF, &sparc64_clockevent
);
1048 printk("clockevent: mult[%lx] shift[%d]\n",
1049 sparc64_clockevent
.mult
, sparc64_clockevent
.shift
);
1051 setup_sparc64_timer();
1053 #ifdef CONFIG_CPU_FREQ
1054 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block
,
1055 CPUFREQ_TRANSITION_NOTIFIER
);
1059 unsigned long long sched_clock(void)
1061 unsigned long ticks
= tick_ops
->get_tick();
1063 return (ticks
* timer_ticks_per_nsec_quotient
)
1064 >> SPARC64_NSEC_PER_CYC_SHIFT
;
1067 static int set_rtc_mmss(unsigned long nowtime
)
1069 int real_seconds
, real_minutes
, chip_minutes
;
1070 void __iomem
*mregs
= mstk48t02_regs
;
1072 unsigned long dregs
= ds1287_regs
;
1074 unsigned long dregs
= 0UL;
1076 unsigned long flags
;
1080 * Not having a register set can lead to trouble.
1081 * Also starfire doesn't have a tod clock.
1083 if (!mregs
&& !dregs
)
1087 spin_lock_irqsave(&mostek_lock
, flags
);
1089 /* Read the current RTC minutes. */
1090 tmp
= mostek_read(mregs
+ MOSTEK_CREG
);
1091 tmp
|= MSTK_CREG_READ
;
1092 mostek_write(mregs
+ MOSTEK_CREG
, tmp
);
1094 chip_minutes
= MSTK_REG_MIN(mregs
);
1096 tmp
= mostek_read(mregs
+ MOSTEK_CREG
);
1097 tmp
&= ~MSTK_CREG_READ
;
1098 mostek_write(mregs
+ MOSTEK_CREG
, tmp
);
1101 * since we're only adjusting minutes and seconds,
1102 * don't interfere with hour overflow. This avoids
1103 * messing with unknown time zones but requires your
1104 * RTC not to be off by more than 15 minutes
1106 real_seconds
= nowtime
% 60;
1107 real_minutes
= nowtime
/ 60;
1108 if (((abs(real_minutes
- chip_minutes
) + 15)/30) & 1)
1109 real_minutes
+= 30; /* correct for half hour time zone */
1112 if (abs(real_minutes
- chip_minutes
) < 30) {
1113 tmp
= mostek_read(mregs
+ MOSTEK_CREG
);
1114 tmp
|= MSTK_CREG_WRITE
;
1115 mostek_write(mregs
+ MOSTEK_CREG
, tmp
);
1117 MSTK_SET_REG_SEC(mregs
,real_seconds
);
1118 MSTK_SET_REG_MIN(mregs
,real_minutes
);
1120 tmp
= mostek_read(mregs
+ MOSTEK_CREG
);
1121 tmp
&= ~MSTK_CREG_WRITE
;
1122 mostek_write(mregs
+ MOSTEK_CREG
, tmp
);
1124 spin_unlock_irqrestore(&mostek_lock
, flags
);
1128 spin_unlock_irqrestore(&mostek_lock
, flags
);
1134 unsigned char save_control
, save_freq_select
;
1136 /* Stolen from arch/i386/kernel/time.c, see there for
1137 * credits and descriptive comments.
1139 spin_lock_irqsave(&rtc_lock
, flags
);
1140 save_control
= CMOS_READ(RTC_CONTROL
); /* tell the clock it's being set */
1141 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
1143 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
); /* stop and reset prescaler */
1144 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
1146 chip_minutes
= CMOS_READ(RTC_MINUTES
);
1147 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
1148 BCD_TO_BIN(chip_minutes
);
1149 real_seconds
= nowtime
% 60;
1150 real_minutes
= nowtime
/ 60;
1151 if (((abs(real_minutes
- chip_minutes
) + 15)/30) & 1)
1155 if (abs(real_minutes
- chip_minutes
) < 30) {
1156 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
1157 BIN_TO_BCD(real_seconds
);
1158 BIN_TO_BCD(real_minutes
);
1160 CMOS_WRITE(real_seconds
,RTC_SECONDS
);
1161 CMOS_WRITE(real_minutes
,RTC_MINUTES
);
1164 "set_rtc_mmss: can't update from %d to %d\n",
1165 chip_minutes
, real_minutes
);
1169 CMOS_WRITE(save_control
, RTC_CONTROL
);
1170 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
1171 spin_unlock_irqrestore(&rtc_lock
, flags
);
1177 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1178 static unsigned char mini_rtc_status
; /* bitmapped status byte. */
1180 /* months start at 0 now */
1181 static unsigned char days_in_mo
[] =
1182 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1185 #define STARTOFTIME 1970
1186 #define SECDAY 86400L
1187 #define SECYR (SECDAY * 365)
1188 #define leapyear(year) ((year) % 4 == 0 && \
1189 ((year) % 100 != 0 || (year) % 400 == 0))
1190 #define days_in_year(a) (leapyear(a) ? 366 : 365)
1191 #define days_in_month(a) (month_days[(a) - 1])
1193 static int month_days
[12] = {
1194 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1198 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1200 static void GregorianDay(struct rtc_time
* tm
)
1205 int MonthOffset
[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1207 lastYear
= tm
->tm_year
- 1;
1210 * Number of leap corrections to apply up to end of last year
1212 leapsToDate
= lastYear
/ 4 - lastYear
/ 100 + lastYear
/ 400;
1215 * This year is a leap year if it is divisible by 4 except when it is
1216 * divisible by 100 unless it is divisible by 400
1218 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1220 day
= tm
->tm_mon
> 2 && leapyear(tm
->tm_year
);
1222 day
+= lastYear
*365 + leapsToDate
+ MonthOffset
[tm
->tm_mon
-1] +
1225 tm
->tm_wday
= day
% 7;
1228 static void to_tm(int tim
, struct rtc_time
*tm
)
1231 register long hms
, day
;
1236 /* Hours, minutes, seconds are easy */
1237 tm
->tm_hour
= hms
/ 3600;
1238 tm
->tm_min
= (hms
% 3600) / 60;
1239 tm
->tm_sec
= (hms
% 3600) % 60;
1241 /* Number of years in days */
1242 for (i
= STARTOFTIME
; day
>= days_in_year(i
); i
++)
1243 day
-= days_in_year(i
);
1246 /* Number of months in days left */
1247 if (leapyear(tm
->tm_year
))
1248 days_in_month(FEBRUARY
) = 29;
1249 for (i
= 1; day
>= days_in_month(i
); i
++)
1250 day
-= days_in_month(i
);
1251 days_in_month(FEBRUARY
) = 28;
1254 /* Days are what is left over (+1) from all that. */
1255 tm
->tm_mday
= day
+ 1;
1258 * Determine the day of week
1263 /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1264 * aka Unix time. So we have to convert to/from rtc_time.
1266 static inline void mini_get_rtc_time(struct rtc_time
*time
)
1268 unsigned long flags
;
1271 spin_lock_irqsave(&rtc_lock
, flags
);
1273 if (this_is_starfire
)
1274 seconds
= starfire_get_time();
1275 else if (tlb_type
== hypervisor
)
1276 seconds
= hypervisor_get_time();
1277 spin_unlock_irqrestore(&rtc_lock
, flags
);
1279 to_tm(seconds
, time
);
1280 time
->tm_year
-= 1900;
1284 static inline int mini_set_rtc_time(struct rtc_time
*time
)
1286 u32 seconds
= mktime(time
->tm_year
+ 1900, time
->tm_mon
+ 1,
1287 time
->tm_mday
, time
->tm_hour
,
1288 time
->tm_min
, time
->tm_sec
);
1289 unsigned long flags
;
1292 spin_lock_irqsave(&rtc_lock
, flags
);
1294 if (this_is_starfire
)
1295 err
= starfire_set_time(seconds
);
1296 else if (tlb_type
== hypervisor
)
1297 err
= hypervisor_set_time(seconds
);
1298 spin_unlock_irqrestore(&rtc_lock
, flags
);
1303 static int mini_rtc_ioctl(struct inode
*inode
, struct file
*file
,
1304 unsigned int cmd
, unsigned long arg
)
1306 struct rtc_time wtime
;
1307 void __user
*argp
= (void __user
*)arg
;
1317 case RTC_UIE_OFF
: /* disable ints from RTC updates. */
1320 case RTC_UIE_ON
: /* enable ints for RTC updates. */
1323 case RTC_RD_TIME
: /* Read the time/date from RTC */
1324 /* this doesn't get week-day, who cares */
1325 memset(&wtime
, 0, sizeof(wtime
));
1326 mini_get_rtc_time(&wtime
);
1328 return copy_to_user(argp
, &wtime
, sizeof(wtime
)) ? -EFAULT
: 0;
1330 case RTC_SET_TIME
: /* Set the RTC */
1333 unsigned char leap_yr
;
1335 if (!capable(CAP_SYS_TIME
))
1338 if (copy_from_user(&wtime
, argp
, sizeof(wtime
)))
1341 year
= wtime
.tm_year
+ 1900;
1342 leap_yr
= ((!(year
% 4) && (year
% 100)) ||
1345 if ((wtime
.tm_mon
< 0 || wtime
.tm_mon
> 11) || (wtime
.tm_mday
< 1))
1348 if (wtime
.tm_mday
< 0 || wtime
.tm_mday
>
1349 (days_in_mo
[wtime
.tm_mon
] + ((wtime
.tm_mon
== 1) && leap_yr
)))
1352 if (wtime
.tm_hour
< 0 || wtime
.tm_hour
>= 24 ||
1353 wtime
.tm_min
< 0 || wtime
.tm_min
>= 60 ||
1354 wtime
.tm_sec
< 0 || wtime
.tm_sec
>= 60)
1357 return mini_set_rtc_time(&wtime
);
1364 static int mini_rtc_open(struct inode
*inode
, struct file
*file
)
1366 if (mini_rtc_status
& RTC_IS_OPEN
)
1369 mini_rtc_status
|= RTC_IS_OPEN
;
1374 static int mini_rtc_release(struct inode
*inode
, struct file
*file
)
1376 mini_rtc_status
&= ~RTC_IS_OPEN
;
1381 static const struct file_operations mini_rtc_fops
= {
1382 .owner
= THIS_MODULE
,
1383 .ioctl
= mini_rtc_ioctl
,
1384 .open
= mini_rtc_open
,
1385 .release
= mini_rtc_release
,
1388 static struct miscdevice rtc_mini_dev
=
1392 .fops
= &mini_rtc_fops
,
1395 static int __init
rtc_mini_init(void)
1399 if (tlb_type
!= hypervisor
&& !this_is_starfire
)
1402 printk(KERN_INFO
"Mini RTC Driver\n");
1404 retval
= misc_register(&rtc_mini_dev
);
1411 static void __exit
rtc_mini_exit(void)
1413 misc_deregister(&rtc_mini_dev
);
1417 module_init(rtc_mini_init
);
1418 module_exit(rtc_mini_exit
);