1 /* time.c: UltraSparc timer and TOD clock support.
3 * Copyright (C) 1997, 2008 David S. Miller (davem@davemloft.net)
4 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 * Based largely on code which is:
8 * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
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>
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>
40 #include <asm/oplib.h>
41 #include <asm/timer.h>
45 #include <asm/starfire.h>
47 #include <asm/sections.h>
48 #include <asm/cpudata.h>
49 #include <asm/uaccess.h>
50 #include <asm/irq_regs.h>
54 DEFINE_SPINLOCK(rtc_lock
);
56 unsigned long ds1287_regs
= 0UL;
57 static void __iomem
*bq4802_regs
;
60 static int set_rtc_mmss(unsigned long);
62 #define TICK_PRIV_BIT (1UL << 63)
63 #define TICKCMP_IRQ_BIT (1UL << 63)
66 unsigned long profile_pc(struct pt_regs
*regs
)
68 unsigned long pc
= instruction_pointer(regs
);
70 if (in_lock_functions(pc
))
71 return regs
->u_regs
[UREG_RETPC
];
74 EXPORT_SYMBOL(profile_pc
);
77 static void tick_disable_protection(void)
79 /* Set things up so user can access tick register for profiling
80 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
81 * read back of %tick after writing it.
87 "1: rd %%tick, %%g2\n"
88 " add %%g2, 6, %%g2\n"
89 " andn %%g2, %0, %%g2\n"
90 " wrpr %%g2, 0, %%tick\n"
97 static void tick_disable_irq(void)
103 "1: wr %0, 0x0, %%tick_cmpr\n"
104 " rd %%tick_cmpr, %%g0"
106 : "r" (TICKCMP_IRQ_BIT
));
109 static void tick_init_tick(void)
111 tick_disable_protection();
115 static unsigned long tick_get_tick(void)
119 __asm__
__volatile__("rd %%tick, %0\n\t"
123 return ret
& ~TICK_PRIV_BIT
;
126 static int tick_add_compare(unsigned long adj
)
128 unsigned long orig_tick
, new_tick
, new_compare
;
130 __asm__
__volatile__("rd %%tick, %0"
133 orig_tick
&= ~TICKCMP_IRQ_BIT
;
135 /* Workaround for Spitfire Errata (#54 I think??), I discovered
136 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
139 * On Blackbird writes to %tick_cmpr can fail, the
140 * workaround seems to be to execute the wr instruction
141 * at the start of an I-cache line, and perform a dummy
142 * read back from %tick_cmpr right after writing to it. -DaveM
144 __asm__
__volatile__("ba,pt %%xcc, 1f\n\t"
145 " add %1, %2, %0\n\t"
148 "wr %0, 0, %%tick_cmpr\n\t"
149 "rd %%tick_cmpr, %%g0\n\t"
151 : "r" (orig_tick
), "r" (adj
));
153 __asm__
__volatile__("rd %%tick, %0"
155 new_tick
&= ~TICKCMP_IRQ_BIT
;
157 return ((long)(new_tick
- (orig_tick
+adj
))) > 0L;
160 static unsigned long tick_add_tick(unsigned long adj
)
162 unsigned long new_tick
;
164 /* Also need to handle Blackbird bug here too. */
165 __asm__
__volatile__("rd %%tick, %0\n\t"
167 "wrpr %0, 0, %%tick\n\t"
174 static struct sparc64_tick_ops tick_operations __read_mostly
= {
176 .init_tick
= tick_init_tick
,
177 .disable_irq
= tick_disable_irq
,
178 .get_tick
= tick_get_tick
,
179 .add_tick
= tick_add_tick
,
180 .add_compare
= tick_add_compare
,
181 .softint_mask
= 1UL << 0,
184 struct sparc64_tick_ops
*tick_ops __read_mostly
= &tick_operations
;
186 static void stick_disable_irq(void)
188 __asm__
__volatile__(
189 "wr %0, 0x0, %%asr25"
191 : "r" (TICKCMP_IRQ_BIT
));
194 static void stick_init_tick(void)
196 /* Writes to the %tick and %stick register are not
197 * allowed on sun4v. The Hypervisor controls that
200 if (tlb_type
!= hypervisor
) {
201 tick_disable_protection();
204 /* Let the user get at STICK too. */
205 __asm__
__volatile__(
206 " rd %%asr24, %%g2\n"
207 " andn %%g2, %0, %%g2\n"
208 " wr %%g2, 0, %%asr24"
210 : "r" (TICK_PRIV_BIT
)
217 static unsigned long stick_get_tick(void)
221 __asm__
__volatile__("rd %%asr24, %0"
224 return ret
& ~TICK_PRIV_BIT
;
227 static unsigned long stick_add_tick(unsigned long adj
)
229 unsigned long new_tick
;
231 __asm__
__volatile__("rd %%asr24, %0\n\t"
233 "wr %0, 0, %%asr24\n\t"
240 static int stick_add_compare(unsigned long adj
)
242 unsigned long orig_tick
, new_tick
;
244 __asm__
__volatile__("rd %%asr24, %0"
246 orig_tick
&= ~TICKCMP_IRQ_BIT
;
248 __asm__
__volatile__("wr %0, 0, %%asr25"
250 : "r" (orig_tick
+ adj
));
252 __asm__
__volatile__("rd %%asr24, %0"
254 new_tick
&= ~TICKCMP_IRQ_BIT
;
256 return ((long)(new_tick
- (orig_tick
+adj
))) > 0L;
259 static struct sparc64_tick_ops stick_operations __read_mostly
= {
261 .init_tick
= stick_init_tick
,
262 .disable_irq
= stick_disable_irq
,
263 .get_tick
= stick_get_tick
,
264 .add_tick
= stick_add_tick
,
265 .add_compare
= stick_add_compare
,
266 .softint_mask
= 1UL << 16,
269 /* On Hummingbird the STICK/STICK_CMPR register is implemented
270 * in I/O space. There are two 64-bit registers each, the
271 * first holds the low 32-bits of the value and the second holds
274 * Since STICK is constantly updating, we have to access it carefully.
276 * The sequence we use to read is:
279 * 3) read high again, if it rolled re-read both low and high again.
281 * Writing STICK safely is also tricky:
282 * 1) write low to zero
286 #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
287 #define HBIRD_STICK_ADDR 0x1fe0000f070UL
289 static unsigned long __hbird_read_stick(void)
291 unsigned long ret
, tmp1
, tmp2
, tmp3
;
292 unsigned long addr
= HBIRD_STICK_ADDR
+8;
294 __asm__
__volatile__("ldxa [%1] %5, %2\n"
296 "sub %1, 0x8, %1\n\t"
297 "ldxa [%1] %5, %3\n\t"
298 "add %1, 0x8, %1\n\t"
299 "ldxa [%1] %5, %4\n\t"
301 "bne,a,pn %%xcc, 1b\n\t"
303 "sllx %4, 32, %4\n\t"
305 : "=&r" (ret
), "=&r" (addr
),
306 "=&r" (tmp1
), "=&r" (tmp2
), "=&r" (tmp3
)
307 : "i" (ASI_PHYS_BYPASS_EC_E
), "1" (addr
));
312 static void __hbird_write_stick(unsigned long val
)
314 unsigned long low
= (val
& 0xffffffffUL
);
315 unsigned long high
= (val
>> 32UL);
316 unsigned long addr
= HBIRD_STICK_ADDR
;
318 __asm__
__volatile__("stxa %%g0, [%0] %4\n\t"
319 "add %0, 0x8, %0\n\t"
320 "stxa %3, [%0] %4\n\t"
321 "sub %0, 0x8, %0\n\t"
324 : "0" (addr
), "r" (low
), "r" (high
),
325 "i" (ASI_PHYS_BYPASS_EC_E
));
328 static void __hbird_write_compare(unsigned long val
)
330 unsigned long low
= (val
& 0xffffffffUL
);
331 unsigned long high
= (val
>> 32UL);
332 unsigned long addr
= HBIRD_STICKCMP_ADDR
+ 0x8UL
;
334 __asm__
__volatile__("stxa %3, [%0] %4\n\t"
335 "sub %0, 0x8, %0\n\t"
338 : "0" (addr
), "r" (low
), "r" (high
),
339 "i" (ASI_PHYS_BYPASS_EC_E
));
342 static void hbtick_disable_irq(void)
344 __hbird_write_compare(TICKCMP_IRQ_BIT
);
347 static void hbtick_init_tick(void)
349 tick_disable_protection();
351 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
352 * XXX into actually sending STICK interrupts. I think because
353 * XXX of how we store %tick_cmpr in head.S this somehow resets the
354 * XXX {TICK + STICK} interrupt mux. -DaveM
356 __hbird_write_stick(__hbird_read_stick());
358 hbtick_disable_irq();
361 static unsigned long hbtick_get_tick(void)
363 return __hbird_read_stick() & ~TICK_PRIV_BIT
;
366 static unsigned long hbtick_add_tick(unsigned long adj
)
370 val
= __hbird_read_stick() + adj
;
371 __hbird_write_stick(val
);
376 static int hbtick_add_compare(unsigned long adj
)
378 unsigned long val
= __hbird_read_stick();
381 val
&= ~TICKCMP_IRQ_BIT
;
383 __hbird_write_compare(val
);
385 val2
= __hbird_read_stick() & ~TICKCMP_IRQ_BIT
;
387 return ((long)(val2
- val
)) > 0L;
390 static struct sparc64_tick_ops hbtick_operations __read_mostly
= {
392 .init_tick
= hbtick_init_tick
,
393 .disable_irq
= hbtick_disable_irq
,
394 .get_tick
= hbtick_get_tick
,
395 .add_tick
= hbtick_add_tick
,
396 .add_compare
= hbtick_add_compare
,
397 .softint_mask
= 1UL << 0,
400 static unsigned long timer_ticks_per_nsec_quotient __read_mostly
;
402 int update_persistent_clock(struct timespec now
)
404 struct rtc_device
*rtc
= rtc_class_open("rtc0");
407 return rtc_set_mmss(rtc
, now
.tv_sec
);
409 return set_rtc_mmss(now
.tv_sec
);
412 /* Probe for the real time clock chip. */
413 static void __init
set_system_time(void)
415 unsigned int year
, mon
, day
, hour
, min
, sec
;
417 unsigned long dregs
= ds1287_regs
;
418 void __iomem
*bregs
= bq4802_regs
;
420 unsigned long dregs
= 0UL;
421 void __iomem
*bregs
= 0UL;
424 if (!dregs
&& !bregs
) {
425 prom_printf("Something wrong, clock regs not mapped yet.\n");
430 unsigned char val
= readb(bregs
+ 0x0e);
431 unsigned int century
;
433 /* BQ4802 RTC chip. */
435 writeb(val
| 0x08, bregs
+ 0x0e);
437 sec
= readb(bregs
+ 0x00);
438 min
= readb(bregs
+ 0x02);
439 hour
= readb(bregs
+ 0x04);
440 day
= readb(bregs
+ 0x06);
441 mon
= readb(bregs
+ 0x09);
442 year
= readb(bregs
+ 0x0a);
443 century
= readb(bregs
+ 0x0f);
445 writeb(val
, bregs
+ 0x0e);
455 year
+= (century
* 100);
457 /* Dallas 12887 RTC chip. */
460 sec
= CMOS_READ(RTC_SECONDS
);
461 min
= CMOS_READ(RTC_MINUTES
);
462 hour
= CMOS_READ(RTC_HOURS
);
463 day
= CMOS_READ(RTC_DAY_OF_MONTH
);
464 mon
= CMOS_READ(RTC_MONTH
);
465 year
= CMOS_READ(RTC_YEAR
);
466 } while (sec
!= CMOS_READ(RTC_SECONDS
));
468 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
476 if ((year
+= 1900) < 1970)
480 xtime
.tv_sec
= mktime(year
, mon
, day
, hour
, min
, sec
);
481 xtime
.tv_nsec
= (INITIAL_JIFFIES
% HZ
) * (NSEC_PER_SEC
/ HZ
);
482 set_normalized_timespec(&wall_to_monotonic
,
483 -xtime
.tv_sec
, -xtime
.tv_nsec
);
486 /* davem suggests we keep this within the 4M locked kernel image */
487 static u32
starfire_get_time(void)
489 static char obp_gettod
[32];
492 sprintf(obp_gettod
, "h# %08x unix-gettod",
493 (unsigned int) (long) &unix_tod
);
494 prom_feval(obp_gettod
);
499 static int starfire_set_time(u32 val
)
501 /* Do nothing, time is set using the service processor
502 * console on this platform.
507 static u32
hypervisor_get_time(void)
509 unsigned long ret
, time
;
513 ret
= sun4v_tod_get(&time
);
516 if (ret
== HV_EWOULDBLOCK
) {
521 printk(KERN_WARNING
"SUN4V: tod_get() timed out.\n");
524 printk(KERN_WARNING
"SUN4V: tod_get() not supported.\n");
528 static int hypervisor_set_time(u32 secs
)
534 ret
= sun4v_tod_set(secs
);
537 if (ret
== HV_EWOULDBLOCK
) {
542 printk(KERN_WARNING
"SUN4V: tod_set() timed out.\n");
545 printk(KERN_WARNING
"SUN4V: tod_set() not supported.\n");
549 static int __init
rtc_model_matches(const char *model
)
551 if (strcmp(model
, "m5819") &&
552 strcmp(model
, "m5819p") &&
553 strcmp(model
, "m5823") &&
554 strcmp(model
, "ds1287") &&
555 strcmp(model
, "bq4802"))
561 static int __devinit
rtc_probe(struct of_device
*op
, const struct of_device_id
*match
)
563 struct device_node
*dp
= op
->node
;
564 const char *model
= of_get_property(dp
, "model", NULL
);
565 const char *compat
= of_get_property(dp
, "compatible", NULL
);
566 unsigned long size
, flags
;
572 if (!model
|| !rtc_model_matches(model
))
575 size
= (op
->resource
[0].end
- op
->resource
[0].start
) + 1;
576 regs
= of_ioremap(&op
->resource
[0], 0, size
, "clock");
581 if (!strcmp(model
, "ds1287") ||
582 !strcmp(model
, "m5819") ||
583 !strcmp(model
, "m5819p") ||
584 !strcmp(model
, "m5823")) {
585 ds1287_regs
= (unsigned long) regs
;
586 } else if (!strcmp(model
, "bq4802")) {
590 printk(KERN_INFO
"%s: Clock regs at %p\n", dp
->full_name
, regs
);
592 local_irq_save(flags
);
596 local_irq_restore(flags
);
601 static struct of_device_id rtc_match
[] = {
608 static struct of_platform_driver rtc_driver
= {
609 .match_table
= rtc_match
,
616 static unsigned char mostek_read_byte(struct device
*dev
, u32 ofs
)
618 struct platform_device
*pdev
= to_platform_device(dev
);
622 regs
= (void __iomem
*) pdev
->resource
[0].start
;
623 val
= readb(regs
+ ofs
);
625 /* the year 0 is 1968 */
626 if (ofs
== M48T59_YEAR
) {
634 static void mostek_write_byte(struct device
*dev
, u32 ofs
, u8 val
)
636 struct platform_device
*pdev
= to_platform_device(dev
);
639 regs
= (void __iomem
*) pdev
->resource
[0].start
;
640 if (ofs
== M48T59_YEAR
) {
647 if ((val
& 0xf0) > 0x9A)
650 writeb(val
, regs
+ ofs
);
653 static struct m48t59_plat_data m48t59_data
= {
654 .read_byte
= mostek_read_byte
,
655 .write_byte
= mostek_write_byte
,
658 static struct platform_device m48t59_rtc
= {
659 .name
= "rtc-m48t59",
663 .platform_data
= &m48t59_data
,
667 static int __devinit
mostek_probe(struct of_device
*op
, const struct of_device_id
*match
)
669 struct device_node
*dp
= op
->node
;
671 /* On an Enterprise system there can be multiple mostek clocks.
672 * We should only match the one that is on the central FHC bus.
674 if (!strcmp(dp
->parent
->name
, "fhc") &&
675 strcmp(dp
->parent
->parent
->name
, "central") != 0)
678 printk(KERN_INFO
"%s: Mostek regs at 0x%lx\n",
679 dp
->full_name
, op
->resource
[0].start
);
681 m48t59_rtc
.resource
= &op
->resource
[0];
682 return platform_device_register(&m48t59_rtc
);
685 static struct of_device_id mostek_match
[] = {
692 static struct of_platform_driver mostek_driver
= {
693 .match_table
= mostek_match
,
694 .probe
= mostek_probe
,
700 static int __init
clock_init(void)
702 if (this_is_starfire
) {
703 xtime
.tv_sec
= starfire_get_time();
704 xtime
.tv_nsec
= (INITIAL_JIFFIES
% HZ
) * (NSEC_PER_SEC
/ HZ
);
705 set_normalized_timespec(&wall_to_monotonic
,
706 -xtime
.tv_sec
, -xtime
.tv_nsec
);
709 if (tlb_type
== hypervisor
) {
710 xtime
.tv_sec
= hypervisor_get_time();
711 xtime
.tv_nsec
= (INITIAL_JIFFIES
% HZ
) * (NSEC_PER_SEC
/ HZ
);
712 set_normalized_timespec(&wall_to_monotonic
,
713 -xtime
.tv_sec
, -xtime
.tv_nsec
);
717 (void) of_register_driver(&rtc_driver
, &of_platform_bus_type
);
718 (void) of_register_driver(&mostek_driver
, &of_platform_bus_type
);
723 /* Must be after subsys_initcall() so that busses are probed. Must
724 * be before device_initcall() because things like the RTC driver
725 * need to see the clock registers.
727 fs_initcall(clock_init
);
729 /* This is gets the master TICK_INT timer going. */
730 static unsigned long sparc64_init_timers(void)
732 struct device_node
*dp
;
735 dp
= of_find_node_by_path("/");
736 if (tlb_type
== spitfire
) {
737 unsigned long ver
, manuf
, impl
;
739 __asm__
__volatile__ ("rdpr %%ver, %0"
741 manuf
= ((ver
>> 48) & 0xffff);
742 impl
= ((ver
>> 32) & 0xffff);
743 if (manuf
== 0x17 && impl
== 0x13) {
744 /* Hummingbird, aka Ultra-IIe */
745 tick_ops
= &hbtick_operations
;
746 clock
= of_getintprop_default(dp
, "stick-frequency", 0);
748 tick_ops
= &tick_operations
;
749 clock
= local_cpu_data().clock_tick
;
752 tick_ops
= &stick_operations
;
753 clock
= of_getintprop_default(dp
, "stick-frequency", 0);
760 unsigned long clock_tick_ref
;
761 unsigned int ref_freq
;
763 static DEFINE_PER_CPU(struct freq_table
, sparc64_freq_table
) = { 0, 0 };
765 unsigned long sparc64_get_clock_tick(unsigned int cpu
)
767 struct freq_table
*ft
= &per_cpu(sparc64_freq_table
, cpu
);
769 if (ft
->clock_tick_ref
)
770 return ft
->clock_tick_ref
;
771 return cpu_data(cpu
).clock_tick
;
774 #ifdef CONFIG_CPU_FREQ
776 static int sparc64_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
779 struct cpufreq_freqs
*freq
= data
;
780 unsigned int cpu
= freq
->cpu
;
781 struct freq_table
*ft
= &per_cpu(sparc64_freq_table
, cpu
);
784 ft
->ref_freq
= freq
->old
;
785 ft
->clock_tick_ref
= cpu_data(cpu
).clock_tick
;
787 if ((val
== CPUFREQ_PRECHANGE
&& freq
->old
< freq
->new) ||
788 (val
== CPUFREQ_POSTCHANGE
&& freq
->old
> freq
->new) ||
789 (val
== CPUFREQ_RESUMECHANGE
)) {
790 cpu_data(cpu
).clock_tick
=
791 cpufreq_scale(ft
->clock_tick_ref
,
799 static struct notifier_block sparc64_cpufreq_notifier_block
= {
800 .notifier_call
= sparc64_cpufreq_notifier
803 static int __init
register_sparc64_cpufreq_notifier(void)
806 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block
,
807 CPUFREQ_TRANSITION_NOTIFIER
);
811 core_initcall(register_sparc64_cpufreq_notifier
);
813 #endif /* CONFIG_CPU_FREQ */
815 static int sparc64_next_event(unsigned long delta
,
816 struct clock_event_device
*evt
)
818 return tick_ops
->add_compare(delta
) ? -ETIME
: 0;
821 static void sparc64_timer_setup(enum clock_event_mode mode
,
822 struct clock_event_device
*evt
)
825 case CLOCK_EVT_MODE_ONESHOT
:
826 case CLOCK_EVT_MODE_RESUME
:
829 case CLOCK_EVT_MODE_SHUTDOWN
:
830 tick_ops
->disable_irq();
833 case CLOCK_EVT_MODE_PERIODIC
:
834 case CLOCK_EVT_MODE_UNUSED
:
840 static struct clock_event_device sparc64_clockevent
= {
841 .features
= CLOCK_EVT_FEAT_ONESHOT
,
842 .set_mode
= sparc64_timer_setup
,
843 .set_next_event
= sparc64_next_event
,
848 static DEFINE_PER_CPU(struct clock_event_device
, sparc64_events
);
850 void timer_interrupt(int irq
, struct pt_regs
*regs
)
852 struct pt_regs
*old_regs
= set_irq_regs(regs
);
853 unsigned long tick_mask
= tick_ops
->softint_mask
;
854 int cpu
= smp_processor_id();
855 struct clock_event_device
*evt
= &per_cpu(sparc64_events
, cpu
);
857 clear_softint(tick_mask
);
861 kstat_this_cpu
.irqs
[0]++;
863 if (unlikely(!evt
->event_handler
)) {
865 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu
);
867 evt
->event_handler(evt
);
871 set_irq_regs(old_regs
);
874 void __devinit
setup_sparc64_timer(void)
876 struct clock_event_device
*sevt
;
877 unsigned long pstate
;
879 /* Guarantee that the following sequences execute
882 __asm__
__volatile__("rdpr %%pstate, %0\n\t"
883 "wrpr %0, %1, %%pstate"
887 tick_ops
->init_tick();
889 /* Restore PSTATE_IE. */
890 __asm__
__volatile__("wrpr %0, 0x0, %%pstate"
894 sevt
= &__get_cpu_var(sparc64_events
);
896 memcpy(sevt
, &sparc64_clockevent
, sizeof(*sevt
));
897 sevt
->cpumask
= cpumask_of_cpu(smp_processor_id());
899 clockevents_register_device(sevt
);
902 #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
904 static struct clocksource clocksource_tick
= {
906 .mask
= CLOCKSOURCE_MASK(64),
908 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
911 static void __init
setup_clockevent_multiplier(unsigned long hz
)
913 unsigned long mult
, shift
= 32;
916 mult
= div_sc(hz
, NSEC_PER_SEC
, shift
);
917 if (mult
&& (mult
>> 32UL) == 0UL)
923 sparc64_clockevent
.shift
= shift
;
924 sparc64_clockevent
.mult
= mult
;
927 static unsigned long tb_ticks_per_usec __read_mostly
;
929 void __delay(unsigned long loops
)
931 unsigned long bclock
, now
;
933 bclock
= tick_ops
->get_tick();
935 now
= tick_ops
->get_tick();
936 } while ((now
-bclock
) < loops
);
938 EXPORT_SYMBOL(__delay
);
940 void udelay(unsigned long usecs
)
942 __delay(tb_ticks_per_usec
* usecs
);
944 EXPORT_SYMBOL(udelay
);
946 void __init
time_init(void)
948 unsigned long clock
= sparc64_init_timers();
950 tb_ticks_per_usec
= clock
/ USEC_PER_SEC
;
952 timer_ticks_per_nsec_quotient
=
953 clocksource_hz2mult(clock
, SPARC64_NSEC_PER_CYC_SHIFT
);
955 clocksource_tick
.name
= tick_ops
->name
;
956 clocksource_tick
.mult
=
957 clocksource_hz2mult(clock
,
958 clocksource_tick
.shift
);
959 clocksource_tick
.read
= tick_ops
->get_tick
;
961 printk("clocksource: mult[%x] shift[%d]\n",
962 clocksource_tick
.mult
, clocksource_tick
.shift
);
964 clocksource_register(&clocksource_tick
);
966 sparc64_clockevent
.name
= tick_ops
->name
;
968 setup_clockevent_multiplier(clock
);
970 sparc64_clockevent
.max_delta_ns
=
971 clockevent_delta2ns(0x7fffffffffffffffUL
, &sparc64_clockevent
);
972 sparc64_clockevent
.min_delta_ns
=
973 clockevent_delta2ns(0xF, &sparc64_clockevent
);
975 printk("clockevent: mult[%lx] shift[%d]\n",
976 sparc64_clockevent
.mult
, sparc64_clockevent
.shift
);
978 setup_sparc64_timer();
981 unsigned long long sched_clock(void)
983 unsigned long ticks
= tick_ops
->get_tick();
985 return (ticks
* timer_ticks_per_nsec_quotient
)
986 >> SPARC64_NSEC_PER_CYC_SHIFT
;
989 static int set_rtc_mmss(unsigned long nowtime
)
991 int real_seconds
, real_minutes
, chip_minutes
;
993 unsigned long dregs
= ds1287_regs
;
994 void __iomem
*bregs
= bq4802_regs
;
996 unsigned long dregs
= 0UL;
997 void __iomem
*bregs
= 0UL;
1002 * Not having a register set can lead to trouble.
1003 * Also starfire doesn't have a tod clock.
1005 if (!dregs
&& !bregs
)
1010 unsigned char val
= readb(bregs
+ 0x0e);
1012 /* BQ4802 RTC chip. */
1014 writeb(val
| 0x08, bregs
+ 0x0e);
1016 chip_minutes
= readb(bregs
+ 0x02);
1017 BCD_TO_BIN(chip_minutes
);
1018 real_seconds
= nowtime
% 60;
1019 real_minutes
= nowtime
/ 60;
1020 if (((abs(real_minutes
- chip_minutes
) + 15)/30) & 1)
1024 if (abs(real_minutes
- chip_minutes
) < 30) {
1025 BIN_TO_BCD(real_seconds
);
1026 BIN_TO_BCD(real_minutes
);
1027 writeb(real_seconds
, bregs
+ 0x00);
1028 writeb(real_minutes
, bregs
+ 0x02);
1031 "set_rtc_mmss: can't update from %d to %d\n",
1032 chip_minutes
, real_minutes
);
1036 writeb(val
, bregs
+ 0x0e);
1041 unsigned char save_control
, save_freq_select
;
1043 /* Stolen from arch/i386/kernel/time.c, see there for
1044 * credits and descriptive comments.
1046 spin_lock_irqsave(&rtc_lock
, flags
);
1047 save_control
= CMOS_READ(RTC_CONTROL
); /* tell the clock it's being set */
1048 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
1050 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
); /* stop and reset prescaler */
1051 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
1053 chip_minutes
= CMOS_READ(RTC_MINUTES
);
1054 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
1055 BCD_TO_BIN(chip_minutes
);
1056 real_seconds
= nowtime
% 60;
1057 real_minutes
= nowtime
/ 60;
1058 if (((abs(real_minutes
- chip_minutes
) + 15)/30) & 1)
1062 if (abs(real_minutes
- chip_minutes
) < 30) {
1063 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
1064 BIN_TO_BCD(real_seconds
);
1065 BIN_TO_BCD(real_minutes
);
1067 CMOS_WRITE(real_seconds
,RTC_SECONDS
);
1068 CMOS_WRITE(real_minutes
,RTC_MINUTES
);
1071 "set_rtc_mmss: can't update from %d to %d\n",
1072 chip_minutes
, real_minutes
);
1076 CMOS_WRITE(save_control
, RTC_CONTROL
);
1077 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
1078 spin_unlock_irqrestore(&rtc_lock
, flags
);
1084 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1085 static unsigned char mini_rtc_status
; /* bitmapped status byte. */
1088 #define STARTOFTIME 1970
1089 #define SECDAY 86400L
1090 #define SECYR (SECDAY * 365)
1091 #define leapyear(year) ((year) % 4 == 0 && \
1092 ((year) % 100 != 0 || (year) % 400 == 0))
1093 #define days_in_year(a) (leapyear(a) ? 366 : 365)
1094 #define days_in_month(a) (month_days[(a) - 1])
1096 static int month_days
[12] = {
1097 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1101 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1103 static void GregorianDay(struct rtc_time
* tm
)
1108 int MonthOffset
[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1110 lastYear
= tm
->tm_year
- 1;
1113 * Number of leap corrections to apply up to end of last year
1115 leapsToDate
= lastYear
/ 4 - lastYear
/ 100 + lastYear
/ 400;
1118 * This year is a leap year if it is divisible by 4 except when it is
1119 * divisible by 100 unless it is divisible by 400
1121 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1123 day
= tm
->tm_mon
> 2 && leapyear(tm
->tm_year
);
1125 day
+= lastYear
*365 + leapsToDate
+ MonthOffset
[tm
->tm_mon
-1] +
1128 tm
->tm_wday
= day
% 7;
1131 static void to_tm(int tim
, struct rtc_time
*tm
)
1134 register long hms
, day
;
1139 /* Hours, minutes, seconds are easy */
1140 tm
->tm_hour
= hms
/ 3600;
1141 tm
->tm_min
= (hms
% 3600) / 60;
1142 tm
->tm_sec
= (hms
% 3600) % 60;
1144 /* Number of years in days */
1145 for (i
= STARTOFTIME
; day
>= days_in_year(i
); i
++)
1146 day
-= days_in_year(i
);
1149 /* Number of months in days left */
1150 if (leapyear(tm
->tm_year
))
1151 days_in_month(FEBRUARY
) = 29;
1152 for (i
= 1; day
>= days_in_month(i
); i
++)
1153 day
-= days_in_month(i
);
1154 days_in_month(FEBRUARY
) = 28;
1157 /* Days are what is left over (+1) from all that. */
1158 tm
->tm_mday
= day
+ 1;
1161 * Determine the day of week
1166 /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1167 * aka Unix time. So we have to convert to/from rtc_time.
1169 static void starfire_get_rtc_time(struct rtc_time
*time
)
1171 u32 seconds
= starfire_get_time();
1173 to_tm(seconds
, time
);
1174 time
->tm_year
-= 1900;
1178 static int starfire_set_rtc_time(struct rtc_time
*time
)
1180 u32 seconds
= mktime(time
->tm_year
+ 1900, time
->tm_mon
+ 1,
1181 time
->tm_mday
, time
->tm_hour
,
1182 time
->tm_min
, time
->tm_sec
);
1184 return starfire_set_time(seconds
);
1187 static void hypervisor_get_rtc_time(struct rtc_time
*time
)
1189 u32 seconds
= hypervisor_get_time();
1191 to_tm(seconds
, time
);
1192 time
->tm_year
-= 1900;
1196 static int hypervisor_set_rtc_time(struct rtc_time
*time
)
1198 u32 seconds
= mktime(time
->tm_year
+ 1900, time
->tm_mon
+ 1,
1199 time
->tm_mday
, time
->tm_hour
,
1200 time
->tm_min
, time
->tm_sec
);
1202 return hypervisor_set_time(seconds
);
1206 static void bq4802_get_rtc_time(struct rtc_time
*time
)
1208 unsigned char val
= readb(bq4802_regs
+ 0x0e);
1209 unsigned int century
;
1211 writeb(val
| 0x08, bq4802_regs
+ 0x0e);
1213 time
->tm_sec
= readb(bq4802_regs
+ 0x00);
1214 time
->tm_min
= readb(bq4802_regs
+ 0x02);
1215 time
->tm_hour
= readb(bq4802_regs
+ 0x04);
1216 time
->tm_mday
= readb(bq4802_regs
+ 0x06);
1217 time
->tm_mon
= readb(bq4802_regs
+ 0x09);
1218 time
->tm_year
= readb(bq4802_regs
+ 0x0a);
1219 time
->tm_wday
= readb(bq4802_regs
+ 0x08);
1220 century
= readb(bq4802_regs
+ 0x0f);
1222 writeb(val
, bq4802_regs
+ 0x0e);
1224 BCD_TO_BIN(time
->tm_sec
);
1225 BCD_TO_BIN(time
->tm_min
);
1226 BCD_TO_BIN(time
->tm_hour
);
1227 BCD_TO_BIN(time
->tm_mday
);
1228 BCD_TO_BIN(time
->tm_mon
);
1229 BCD_TO_BIN(time
->tm_year
);
1230 BCD_TO_BIN(time
->tm_wday
);
1231 BCD_TO_BIN(century
);
1233 time
->tm_year
+= (century
* 100);
1234 time
->tm_year
-= 1900;
1239 static int bq4802_set_rtc_time(struct rtc_time
*time
)
1241 unsigned char val
= readb(bq4802_regs
+ 0x0e);
1242 unsigned char sec
, min
, hrs
, day
, mon
, yrs
, century
;
1245 year
= time
->tm_year
+ 1900;
1246 century
= year
/ 100;
1249 mon
= time
->tm_mon
+ 1; /* tm_mon starts at zero */
1250 day
= time
->tm_mday
;
1251 hrs
= time
->tm_hour
;
1261 BIN_TO_BCD(century
);
1263 writeb(val
| 0x08, bq4802_regs
+ 0x0e);
1265 writeb(sec
, bq4802_regs
+ 0x00);
1266 writeb(min
, bq4802_regs
+ 0x02);
1267 writeb(hrs
, bq4802_regs
+ 0x04);
1268 writeb(day
, bq4802_regs
+ 0x06);
1269 writeb(mon
, bq4802_regs
+ 0x09);
1270 writeb(yrs
, bq4802_regs
+ 0x0a);
1271 writeb(century
, bq4802_regs
+ 0x0f);
1273 writeb(val
, bq4802_regs
+ 0x0e);
1278 static void cmos_get_rtc_time(struct rtc_time
*rtc_tm
)
1282 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
1283 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
1284 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
1285 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
1286 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
1287 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
1288 rtc_tm
->tm_wday
= CMOS_READ(RTC_DAY_OF_WEEK
);
1290 ctrl
= CMOS_READ(RTC_CONTROL
);
1291 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
1292 BCD_TO_BIN(rtc_tm
->tm_sec
);
1293 BCD_TO_BIN(rtc_tm
->tm_min
);
1294 BCD_TO_BIN(rtc_tm
->tm_hour
);
1295 BCD_TO_BIN(rtc_tm
->tm_mday
);
1296 BCD_TO_BIN(rtc_tm
->tm_mon
);
1297 BCD_TO_BIN(rtc_tm
->tm_year
);
1298 BCD_TO_BIN(rtc_tm
->tm_wday
);
1301 if (rtc_tm
->tm_year
<= 69)
1302 rtc_tm
->tm_year
+= 100;
1307 static int cmos_set_rtc_time(struct rtc_time
*rtc_tm
)
1309 unsigned char mon
, day
, hrs
, min
, sec
;
1310 unsigned char save_control
, save_freq_select
;
1313 yrs
= rtc_tm
->tm_year
;
1314 mon
= rtc_tm
->tm_mon
+ 1;
1315 day
= rtc_tm
->tm_mday
;
1316 hrs
= rtc_tm
->tm_hour
;
1317 min
= rtc_tm
->tm_min
;
1318 sec
= rtc_tm
->tm_sec
;
1323 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
1332 save_control
= CMOS_READ(RTC_CONTROL
);
1333 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
1334 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
1335 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
1337 CMOS_WRITE(yrs
, RTC_YEAR
);
1338 CMOS_WRITE(mon
, RTC_MONTH
);
1339 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
1340 CMOS_WRITE(hrs
, RTC_HOURS
);
1341 CMOS_WRITE(min
, RTC_MINUTES
);
1342 CMOS_WRITE(sec
, RTC_SECONDS
);
1344 CMOS_WRITE(save_control
, RTC_CONTROL
);
1345 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
1349 #endif /* CONFIG_PCI */
1351 struct mini_rtc_ops
{
1352 void (*get_rtc_time
)(struct rtc_time
*);
1353 int (*set_rtc_time
)(struct rtc_time
*);
1356 static struct mini_rtc_ops starfire_rtc_ops
= {
1357 .get_rtc_time
= starfire_get_rtc_time
,
1358 .set_rtc_time
= starfire_set_rtc_time
,
1361 static struct mini_rtc_ops hypervisor_rtc_ops
= {
1362 .get_rtc_time
= hypervisor_get_rtc_time
,
1363 .set_rtc_time
= hypervisor_set_rtc_time
,
1367 static struct mini_rtc_ops bq4802_rtc_ops
= {
1368 .get_rtc_time
= bq4802_get_rtc_time
,
1369 .set_rtc_time
= bq4802_set_rtc_time
,
1372 static struct mini_rtc_ops cmos_rtc_ops
= {
1373 .get_rtc_time
= cmos_get_rtc_time
,
1374 .set_rtc_time
= cmos_set_rtc_time
,
1376 #endif /* CONFIG_PCI */
1378 static struct mini_rtc_ops
*mini_rtc_ops
;
1380 static inline void mini_get_rtc_time(struct rtc_time
*time
)
1382 unsigned long flags
;
1384 spin_lock_irqsave(&rtc_lock
, flags
);
1385 mini_rtc_ops
->get_rtc_time(time
);
1386 spin_unlock_irqrestore(&rtc_lock
, flags
);
1389 static inline int mini_set_rtc_time(struct rtc_time
*time
)
1391 unsigned long flags
;
1394 spin_lock_irqsave(&rtc_lock
, flags
);
1395 err
= mini_rtc_ops
->set_rtc_time(time
);
1396 spin_unlock_irqrestore(&rtc_lock
, flags
);
1401 static int mini_rtc_ioctl(struct inode
*inode
, struct file
*file
,
1402 unsigned int cmd
, unsigned long arg
)
1404 struct rtc_time wtime
;
1405 void __user
*argp
= (void __user
*)arg
;
1415 case RTC_UIE_OFF
: /* disable ints from RTC updates. */
1418 case RTC_UIE_ON
: /* enable ints for RTC updates. */
1421 case RTC_RD_TIME
: /* Read the time/date from RTC */
1422 /* this doesn't get week-day, who cares */
1423 memset(&wtime
, 0, sizeof(wtime
));
1424 mini_get_rtc_time(&wtime
);
1426 return copy_to_user(argp
, &wtime
, sizeof(wtime
)) ? -EFAULT
: 0;
1428 case RTC_SET_TIME
: /* Set the RTC */
1432 if (!capable(CAP_SYS_TIME
))
1435 if (copy_from_user(&wtime
, argp
, sizeof(wtime
)))
1438 year
= wtime
.tm_year
+ 1900;
1439 days
= month_days
[wtime
.tm_mon
] +
1440 ((wtime
.tm_mon
== 1) && leapyear(year
));
1442 if ((wtime
.tm_mon
< 0 || wtime
.tm_mon
> 11) ||
1443 (wtime
.tm_mday
< 1))
1446 if (wtime
.tm_mday
< 0 || wtime
.tm_mday
> days
)
1449 if (wtime
.tm_hour
< 0 || wtime
.tm_hour
>= 24 ||
1450 wtime
.tm_min
< 0 || wtime
.tm_min
>= 60 ||
1451 wtime
.tm_sec
< 0 || wtime
.tm_sec
>= 60)
1454 return mini_set_rtc_time(&wtime
);
1461 static int mini_rtc_open(struct inode
*inode
, struct file
*file
)
1464 if (mini_rtc_status
& RTC_IS_OPEN
) {
1469 mini_rtc_status
|= RTC_IS_OPEN
;
1475 static int mini_rtc_release(struct inode
*inode
, struct file
*file
)
1477 mini_rtc_status
&= ~RTC_IS_OPEN
;
1482 static const struct file_operations mini_rtc_fops
= {
1483 .owner
= THIS_MODULE
,
1484 .ioctl
= mini_rtc_ioctl
,
1485 .open
= mini_rtc_open
,
1486 .release
= mini_rtc_release
,
1489 static struct miscdevice rtc_mini_dev
=
1493 .fops
= &mini_rtc_fops
,
1496 static int __init
rtc_mini_init(void)
1500 if (tlb_type
== hypervisor
)
1501 mini_rtc_ops
= &hypervisor_rtc_ops
;
1502 else if (this_is_starfire
)
1503 mini_rtc_ops
= &starfire_rtc_ops
;
1505 else if (bq4802_regs
)
1506 mini_rtc_ops
= &bq4802_rtc_ops
;
1507 else if (ds1287_regs
)
1508 mini_rtc_ops
= &cmos_rtc_ops
;
1509 #endif /* CONFIG_PCI */
1513 printk(KERN_INFO
"Mini RTC Driver\n");
1515 retval
= misc_register(&rtc_mini_dev
);
1522 static void __exit
rtc_mini_exit(void)
1524 misc_deregister(&rtc_mini_dev
);
1527 int __devinit
read_current_timer(unsigned long *timer_val
)
1529 *timer_val
= tick_ops
->get_tick();
1533 module_init(rtc_mini_init
);
1534 module_exit(rtc_mini_exit
);