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/kernel_stat.h>
34 #include <linux/clockchips.h>
35 #include <linux/clocksource.h>
37 #include <asm/oplib.h>
38 #include <asm/mostek.h>
39 #include <asm/timer.h>
43 #include <asm/of_device.h>
44 #include <asm/starfire.h>
46 #include <asm/sections.h>
47 #include <asm/cpudata.h>
48 #include <asm/uaccess.h>
49 #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;
58 static void __iomem *bq4802_regs;
61 static void __iomem *mstk48t08_regs;
62 static void __iomem *mstk48t59_regs;
64 static int set_rtc_mmss(unsigned long);
66 #define TICK_PRIV_BIT (1UL << 63)
67 #define TICKCMP_IRQ_BIT (1UL << 63)
70 unsigned long profile_pc(struct pt_regs *regs)
72 unsigned long pc = instruction_pointer(regs);
74 if (in_lock_functions(pc))
75 return regs->u_regs[UREG_RETPC];
78 EXPORT_SYMBOL(profile_pc);
81 static void tick_disable_protection(void)
83 /* Set things up so user can access tick register for profiling
84 * purposes. Also workaround BB_ERRATA_1 by doing a dummy
85 * read back of %tick after writing it.
91 "1: rd %%tick, %%g2\n"
92 " add %%g2, 6, %%g2\n"
93 " andn %%g2, %0, %%g2\n"
94 " wrpr %%g2, 0, %%tick\n"
101 static void tick_disable_irq(void)
103 __asm__ __volatile__(
107 "1: wr %0, 0x0, %%tick_cmpr\n"
108 " rd %%tick_cmpr, %%g0"
110 : "r" (TICKCMP_IRQ_BIT));
113 static void tick_init_tick(void)
115 tick_disable_protection();
119 static unsigned long tick_get_tick(void)
123 __asm__ __volatile__("rd %%tick, %0\n\t"
127 return ret & ~TICK_PRIV_BIT;
130 static int tick_add_compare(unsigned long adj)
132 unsigned long orig_tick, new_tick, new_compare;
134 __asm__ __volatile__("rd %%tick, %0"
137 orig_tick &= ~TICKCMP_IRQ_BIT;
139 /* Workaround for Spitfire Errata (#54 I think??), I discovered
140 * this via Sun BugID 4008234, mentioned in Solaris-2.5.1 patch
143 * On Blackbird writes to %tick_cmpr can fail, the
144 * workaround seems to be to execute the wr instruction
145 * at the start of an I-cache line, and perform a dummy
146 * read back from %tick_cmpr right after writing to it. -DaveM
148 __asm__ __volatile__("ba,pt %%xcc, 1f\n\t"
149 " add %1, %2, %0\n\t"
152 "wr %0, 0, %%tick_cmpr\n\t"
153 "rd %%tick_cmpr, %%g0\n\t"
155 : "r" (orig_tick), "r" (adj));
157 __asm__ __volatile__("rd %%tick, %0"
159 new_tick &= ~TICKCMP_IRQ_BIT;
161 return ((long)(new_tick - (orig_tick+adj))) > 0L;
164 static unsigned long tick_add_tick(unsigned long adj)
166 unsigned long new_tick;
168 /* Also need to handle Blackbird bug here too. */
169 __asm__ __volatile__("rd %%tick, %0\n\t"
171 "wrpr %0, 0, %%tick\n\t"
178 static struct sparc64_tick_ops tick_operations __read_mostly = {
180 .init_tick = tick_init_tick,
181 .disable_irq = tick_disable_irq,
182 .get_tick = tick_get_tick,
183 .add_tick = tick_add_tick,
184 .add_compare = tick_add_compare,
185 .softint_mask = 1UL << 0,
188 struct sparc64_tick_ops *tick_ops __read_mostly = &tick_operations;
190 static void stick_disable_irq(void)
192 __asm__ __volatile__(
193 "wr %0, 0x0, %%asr25"
195 : "r" (TICKCMP_IRQ_BIT));
198 static void stick_init_tick(void)
200 /* Writes to the %tick and %stick register are not
201 * allowed on sun4v. The Hypervisor controls that
204 if (tlb_type != hypervisor) {
205 tick_disable_protection();
208 /* Let the user get at STICK too. */
209 __asm__ __volatile__(
210 " rd %%asr24, %%g2\n"
211 " andn %%g2, %0, %%g2\n"
212 " wr %%g2, 0, %%asr24"
214 : "r" (TICK_PRIV_BIT)
221 static unsigned long stick_get_tick(void)
225 __asm__ __volatile__("rd %%asr24, %0"
228 return ret & ~TICK_PRIV_BIT;
231 static unsigned long stick_add_tick(unsigned long adj)
233 unsigned long new_tick;
235 __asm__ __volatile__("rd %%asr24, %0\n\t"
237 "wr %0, 0, %%asr24\n\t"
244 static int stick_add_compare(unsigned long adj)
246 unsigned long orig_tick, new_tick;
248 __asm__ __volatile__("rd %%asr24, %0"
250 orig_tick &= ~TICKCMP_IRQ_BIT;
252 __asm__ __volatile__("wr %0, 0, %%asr25"
254 : "r" (orig_tick + adj));
256 __asm__ __volatile__("rd %%asr24, %0"
258 new_tick &= ~TICKCMP_IRQ_BIT;
260 return ((long)(new_tick - (orig_tick+adj))) > 0L;
263 static struct sparc64_tick_ops stick_operations __read_mostly = {
265 .init_tick = stick_init_tick,
266 .disable_irq = stick_disable_irq,
267 .get_tick = stick_get_tick,
268 .add_tick = stick_add_tick,
269 .add_compare = stick_add_compare,
270 .softint_mask = 1UL << 16,
273 /* On Hummingbird the STICK/STICK_CMPR register is implemented
274 * in I/O space. There are two 64-bit registers each, the
275 * first holds the low 32-bits of the value and the second holds
278 * Since STICK is constantly updating, we have to access it carefully.
280 * The sequence we use to read is:
283 * 3) read high again, if it rolled re-read both low and high again.
285 * Writing STICK safely is also tricky:
286 * 1) write low to zero
290 #define HBIRD_STICKCMP_ADDR 0x1fe0000f060UL
291 #define HBIRD_STICK_ADDR 0x1fe0000f070UL
293 static unsigned long __hbird_read_stick(void)
295 unsigned long ret, tmp1, tmp2, tmp3;
296 unsigned long addr = HBIRD_STICK_ADDR+8;
298 __asm__ __volatile__("ldxa [%1] %5, %2\n"
300 "sub %1, 0x8, %1\n\t"
301 "ldxa [%1] %5, %3\n\t"
302 "add %1, 0x8, %1\n\t"
303 "ldxa [%1] %5, %4\n\t"
305 "bne,a,pn %%xcc, 1b\n\t"
307 "sllx %4, 32, %4\n\t"
309 : "=&r" (ret), "=&r" (addr),
310 "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3)
311 : "i" (ASI_PHYS_BYPASS_EC_E), "1" (addr));
316 static void __hbird_write_stick(unsigned long val)
318 unsigned long low = (val & 0xffffffffUL);
319 unsigned long high = (val >> 32UL);
320 unsigned long addr = HBIRD_STICK_ADDR;
322 __asm__ __volatile__("stxa %%g0, [%0] %4\n\t"
323 "add %0, 0x8, %0\n\t"
324 "stxa %3, [%0] %4\n\t"
325 "sub %0, 0x8, %0\n\t"
328 : "0" (addr), "r" (low), "r" (high),
329 "i" (ASI_PHYS_BYPASS_EC_E));
332 static void __hbird_write_compare(unsigned long val)
334 unsigned long low = (val & 0xffffffffUL);
335 unsigned long high = (val >> 32UL);
336 unsigned long addr = HBIRD_STICKCMP_ADDR + 0x8UL;
338 __asm__ __volatile__("stxa %3, [%0] %4\n\t"
339 "sub %0, 0x8, %0\n\t"
342 : "0" (addr), "r" (low), "r" (high),
343 "i" (ASI_PHYS_BYPASS_EC_E));
346 static void hbtick_disable_irq(void)
348 __hbird_write_compare(TICKCMP_IRQ_BIT);
351 static void hbtick_init_tick(void)
353 tick_disable_protection();
355 /* XXX This seems to be necessary to 'jumpstart' Hummingbird
356 * XXX into actually sending STICK interrupts. I think because
357 * XXX of how we store %tick_cmpr in head.S this somehow resets the
358 * XXX {TICK + STICK} interrupt mux. -DaveM
360 __hbird_write_stick(__hbird_read_stick());
362 hbtick_disable_irq();
365 static unsigned long hbtick_get_tick(void)
367 return __hbird_read_stick() & ~TICK_PRIV_BIT;
370 static unsigned long hbtick_add_tick(unsigned long adj)
374 val = __hbird_read_stick() + adj;
375 __hbird_write_stick(val);
380 static int hbtick_add_compare(unsigned long adj)
382 unsigned long val = __hbird_read_stick();
385 val &= ~TICKCMP_IRQ_BIT;
387 __hbird_write_compare(val);
389 val2 = __hbird_read_stick() & ~TICKCMP_IRQ_BIT;
391 return ((long)(val2 - val)) > 0L;
394 static struct sparc64_tick_ops hbtick_operations __read_mostly = {
396 .init_tick = hbtick_init_tick,
397 .disable_irq = hbtick_disable_irq,
398 .get_tick = hbtick_get_tick,
399 .add_tick = hbtick_add_tick,
400 .add_compare = hbtick_add_compare,
401 .softint_mask = 1UL << 0,
404 static unsigned long timer_ticks_per_nsec_quotient __read_mostly;
406 int update_persistent_clock(struct timespec now)
408 return set_rtc_mmss(now.tv_sec);
411 /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
412 static void __init kick_start_clock(void)
414 void __iomem *regs = mstk48t02_regs;
418 prom_printf("CLOCK: Clock was stopped. Kick start ");
420 spin_lock_irq(&mostek_lock);
422 /* Turn on the kick start bit to start the oscillator. */
423 tmp = mostek_read(regs + MOSTEK_CREG);
424 tmp |= MSTK_CREG_WRITE;
425 mostek_write(regs + MOSTEK_CREG, tmp);
426 tmp = mostek_read(regs + MOSTEK_SEC);
428 mostek_write(regs + MOSTEK_SEC, tmp);
429 tmp = mostek_read(regs + MOSTEK_HOUR);
430 tmp |= MSTK_KICK_START;
431 mostek_write(regs + MOSTEK_HOUR, tmp);
432 tmp = mostek_read(regs + MOSTEK_CREG);
433 tmp &= ~MSTK_CREG_WRITE;
434 mostek_write(regs + MOSTEK_CREG, tmp);
436 spin_unlock_irq(&mostek_lock);
438 /* Delay to allow the clock oscillator to start. */
439 sec = MSTK_REG_SEC(regs);
440 for (i = 0; i < 3; i++) {
441 while (sec == MSTK_REG_SEC(regs))
442 for (count = 0; count < 100000; count++)
445 sec = MSTK_REG_SEC(regs);
449 spin_lock_irq(&mostek_lock);
451 /* Turn off kick start and set a "valid" time and date. */
452 tmp = mostek_read(regs + MOSTEK_CREG);
453 tmp |= MSTK_CREG_WRITE;
454 mostek_write(regs + MOSTEK_CREG, tmp);
455 tmp = mostek_read(regs + MOSTEK_HOUR);
456 tmp &= ~MSTK_KICK_START;
457 mostek_write(regs + MOSTEK_HOUR, tmp);
458 MSTK_SET_REG_SEC(regs,0);
459 MSTK_SET_REG_MIN(regs,0);
460 MSTK_SET_REG_HOUR(regs,0);
461 MSTK_SET_REG_DOW(regs,5);
462 MSTK_SET_REG_DOM(regs,1);
463 MSTK_SET_REG_MONTH(regs,8);
464 MSTK_SET_REG_YEAR(regs,1996 - MSTK_YEAR_ZERO);
465 tmp = mostek_read(regs + MOSTEK_CREG);
466 tmp &= ~MSTK_CREG_WRITE;
467 mostek_write(regs + MOSTEK_CREG, tmp);
469 spin_unlock_irq(&mostek_lock);
471 /* Ensure the kick start bit is off. If it isn't, turn it off. */
472 while (mostek_read(regs + MOSTEK_HOUR) & MSTK_KICK_START) {
473 prom_printf("CLOCK: Kick start still on!\n");
475 spin_lock_irq(&mostek_lock);
477 tmp = mostek_read(regs + MOSTEK_CREG);
478 tmp |= MSTK_CREG_WRITE;
479 mostek_write(regs + MOSTEK_CREG, tmp);
481 tmp = mostek_read(regs + MOSTEK_HOUR);
482 tmp &= ~MSTK_KICK_START;
483 mostek_write(regs + MOSTEK_HOUR, tmp);
485 tmp = mostek_read(regs + MOSTEK_CREG);
486 tmp &= ~MSTK_CREG_WRITE;
487 mostek_write(regs + MOSTEK_CREG, tmp);
489 spin_unlock_irq(&mostek_lock);
492 prom_printf("CLOCK: Kick start procedure successful.\n");
495 /* Return nonzero if the clock chip battery is low. */
496 static int __init has_low_battery(void)
498 void __iomem *regs = mstk48t02_regs;
501 spin_lock_irq(&mostek_lock);
503 data1 = mostek_read(regs + MOSTEK_EEPROM); /* Read some data. */
504 mostek_write(regs + MOSTEK_EEPROM, ~data1); /* Write back the complement. */
505 data2 = mostek_read(regs + MOSTEK_EEPROM); /* Read back the complement. */
506 mostek_write(regs + MOSTEK_EEPROM, data1); /* Restore original value. */
508 spin_unlock_irq(&mostek_lock);
510 return (data1 == data2); /* Was the write blocked? */
513 static void __init mostek_set_system_time(void __iomem *mregs)
515 unsigned int year, mon, day, hour, min, sec;
518 spin_lock_irq(&mostek_lock);
520 /* Traditional Mostek chip. */
521 tmp = mostek_read(mregs + MOSTEK_CREG);
522 tmp |= MSTK_CREG_READ;
523 mostek_write(mregs + MOSTEK_CREG, tmp);
525 sec = MSTK_REG_SEC(mregs);
526 min = MSTK_REG_MIN(mregs);
527 hour = MSTK_REG_HOUR(mregs);
528 day = MSTK_REG_DOM(mregs);
529 mon = MSTK_REG_MONTH(mregs);
530 year = MSTK_CVT_YEAR( MSTK_REG_YEAR(mregs) );
532 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
533 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
534 set_normalized_timespec(&wall_to_monotonic,
535 -xtime.tv_sec, -xtime.tv_nsec);
537 tmp = mostek_read(mregs + MOSTEK_CREG);
538 tmp &= ~MSTK_CREG_READ;
539 mostek_write(mregs + MOSTEK_CREG, tmp);
541 spin_unlock_irq(&mostek_lock);
544 /* Probe for the real time clock chip. */
545 static void __init set_system_time(void)
547 unsigned int year, mon, day, hour, min, sec;
548 void __iomem *mregs = mstk48t02_regs;
550 unsigned long dregs = ds1287_regs;
551 void __iomem *bregs = bq4802_regs;
553 unsigned long dregs = 0UL;
554 void __iomem *bregs = 0UL;
557 if (!mregs && !dregs && !bregs) {
558 prom_printf("Something wrong, clock regs not mapped yet.\n");
563 mostek_set_system_time(mregs);
568 unsigned char val = readb(bregs + 0x0e);
569 unsigned int century;
571 /* BQ4802 RTC chip. */
573 writeb(val | 0x08, bregs + 0x0e);
575 sec = readb(bregs + 0x00);
576 min = readb(bregs + 0x02);
577 hour = readb(bregs + 0x04);
578 day = readb(bregs + 0x06);
579 mon = readb(bregs + 0x09);
580 year = readb(bregs + 0x0a);
581 century = readb(bregs + 0x0f);
583 writeb(val, bregs + 0x0e);
593 year += (century * 100);
595 /* Dallas 12887 RTC chip. */
598 sec = CMOS_READ(RTC_SECONDS);
599 min = CMOS_READ(RTC_MINUTES);
600 hour = CMOS_READ(RTC_HOURS);
601 day = CMOS_READ(RTC_DAY_OF_MONTH);
602 mon = CMOS_READ(RTC_MONTH);
603 year = CMOS_READ(RTC_YEAR);
604 } while (sec != CMOS_READ(RTC_SECONDS));
606 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
614 if ((year += 1900) < 1970)
618 xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
619 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
620 set_normalized_timespec(&wall_to_monotonic,
621 -xtime.tv_sec, -xtime.tv_nsec);
624 /* davem suggests we keep this within the 4M locked kernel image */
625 static u32 starfire_get_time(void)
627 static char obp_gettod[32];
630 sprintf(obp_gettod, "h# %08x unix-gettod",
631 (unsigned int) (long) &unix_tod);
632 prom_feval(obp_gettod);
637 static int starfire_set_time(u32 val)
639 /* Do nothing, time is set using the service processor
640 * console on this platform.
645 static u32 hypervisor_get_time(void)
647 unsigned long ret, time;
651 ret = sun4v_tod_get(&time);
654 if (ret == HV_EWOULDBLOCK) {
659 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
662 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
666 static int hypervisor_set_time(u32 secs)
672 ret = sun4v_tod_set(secs);
675 if (ret == HV_EWOULDBLOCK) {
680 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
683 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
687 static int __init clock_model_matches(const char *model)
689 if (strcmp(model, "mk48t02") &&
690 strcmp(model, "mk48t08") &&
691 strcmp(model, "mk48t59") &&
692 strcmp(model, "m5819") &&
693 strcmp(model, "m5819p") &&
694 strcmp(model, "m5823") &&
695 strcmp(model, "ds1287") &&
696 strcmp(model, "bq4802"))
702 static int __devinit clock_probe(struct of_device *op, const struct of_device_id *match)
704 struct device_node *dp = op->node;
705 const char *model = of_get_property(dp, "model", NULL);
706 const char *compat = of_get_property(dp, "compatible", NULL);
707 unsigned long size, flags;
713 if (!model || !clock_model_matches(model))
716 /* On an Enterprise system there can be multiple mostek clocks.
717 * We should only match the one that is on the central FHC bus.
719 if (!strcmp(dp->parent->name, "fhc") &&
720 strcmp(dp->parent->parent->name, "central") != 0)
723 size = (op->resource[0].end - op->resource[0].start) + 1;
724 regs = of_ioremap(&op->resource[0], 0, size, "clock");
729 if (!strcmp(model, "ds1287") ||
730 !strcmp(model, "m5819") ||
731 !strcmp(model, "m5819p") ||
732 !strcmp(model, "m5823")) {
733 ds1287_regs = (unsigned long) regs;
734 } else if (!strcmp(model, "bq4802")) {
738 if (model[5] == '0' && model[6] == '2') {
739 mstk48t02_regs = regs;
740 } else if(model[5] == '0' && model[6] == '8') {
741 mstk48t08_regs = regs;
742 mstk48t02_regs = mstk48t08_regs + MOSTEK_48T08_48T02;
744 mstk48t59_regs = regs;
745 mstk48t02_regs = mstk48t59_regs + MOSTEK_48T59_48T02;
748 printk(KERN_INFO "%s: Clock regs at %p\n", dp->full_name, regs);
750 local_irq_save(flags);
752 if (mstk48t02_regs != NULL) {
753 /* Report a low battery voltage condition. */
754 if (has_low_battery())
755 prom_printf("NVRAM: Low battery voltage!\n");
757 /* Kick start the clock if it is completely stopped. */
758 if (mostek_read(mstk48t02_regs + MOSTEK_SEC) & MSTK_STOP)
764 local_irq_restore(flags);
769 static struct of_device_id clock_match[] = {
779 static struct of_platform_driver clock_driver = {
780 .match_table = clock_match,
781 .probe = clock_probe,
787 static int __init clock_init(void)
789 if (this_is_starfire) {
790 xtime.tv_sec = starfire_get_time();
791 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
792 set_normalized_timespec(&wall_to_monotonic,
793 -xtime.tv_sec, -xtime.tv_nsec);
796 if (tlb_type == hypervisor) {
797 xtime.tv_sec = hypervisor_get_time();
798 xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
799 set_normalized_timespec(&wall_to_monotonic,
800 -xtime.tv_sec, -xtime.tv_nsec);
804 return of_register_driver(&clock_driver, &of_platform_bus_type);
807 /* Must be after subsys_initcall() so that busses are probed. Must
808 * be before device_initcall() because things like the RTC driver
809 * need to see the clock registers.
811 fs_initcall(clock_init);
813 /* This is gets the master TICK_INT timer going. */
814 static unsigned long sparc64_init_timers(void)
816 struct device_node *dp;
819 dp = of_find_node_by_path("/");
820 if (tlb_type == spitfire) {
821 unsigned long ver, manuf, impl;
823 __asm__ __volatile__ ("rdpr %%ver, %0"
825 manuf = ((ver >> 48) & 0xffff);
826 impl = ((ver >> 32) & 0xffff);
827 if (manuf == 0x17 && impl == 0x13) {
828 /* Hummingbird, aka Ultra-IIe */
829 tick_ops = &hbtick_operations;
830 clock = of_getintprop_default(dp, "stick-frequency", 0);
832 tick_ops = &tick_operations;
833 clock = local_cpu_data().clock_tick;
836 tick_ops = &stick_operations;
837 clock = of_getintprop_default(dp, "stick-frequency", 0);
844 unsigned long clock_tick_ref;
845 unsigned int ref_freq;
847 static DEFINE_PER_CPU(struct freq_table, sparc64_freq_table) = { 0, 0 };
849 unsigned long sparc64_get_clock_tick(unsigned int cpu)
851 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
853 if (ft->clock_tick_ref)
854 return ft->clock_tick_ref;
855 return cpu_data(cpu).clock_tick;
858 #ifdef CONFIG_CPU_FREQ
860 static int sparc64_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
863 struct cpufreq_freqs *freq = data;
864 unsigned int cpu = freq->cpu;
865 struct freq_table *ft = &per_cpu(sparc64_freq_table, cpu);
868 ft->ref_freq = freq->old;
869 ft->clock_tick_ref = cpu_data(cpu).clock_tick;
871 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
872 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
873 (val == CPUFREQ_RESUMECHANGE)) {
874 cpu_data(cpu).clock_tick =
875 cpufreq_scale(ft->clock_tick_ref,
883 static struct notifier_block sparc64_cpufreq_notifier_block = {
884 .notifier_call = sparc64_cpufreq_notifier
887 static int __init register_sparc64_cpufreq_notifier(void)
890 cpufreq_register_notifier(&sparc64_cpufreq_notifier_block,
891 CPUFREQ_TRANSITION_NOTIFIER);
895 core_initcall(register_sparc64_cpufreq_notifier);
897 #endif /* CONFIG_CPU_FREQ */
899 static int sparc64_next_event(unsigned long delta,
900 struct clock_event_device *evt)
902 return tick_ops->add_compare(delta) ? -ETIME : 0;
905 static void sparc64_timer_setup(enum clock_event_mode mode,
906 struct clock_event_device *evt)
909 case CLOCK_EVT_MODE_ONESHOT:
910 case CLOCK_EVT_MODE_RESUME:
913 case CLOCK_EVT_MODE_SHUTDOWN:
914 tick_ops->disable_irq();
917 case CLOCK_EVT_MODE_PERIODIC:
918 case CLOCK_EVT_MODE_UNUSED:
924 static struct clock_event_device sparc64_clockevent = {
925 .features = CLOCK_EVT_FEAT_ONESHOT,
926 .set_mode = sparc64_timer_setup,
927 .set_next_event = sparc64_next_event,
932 static DEFINE_PER_CPU(struct clock_event_device, sparc64_events);
934 void timer_interrupt(int irq, struct pt_regs *regs)
936 struct pt_regs *old_regs = set_irq_regs(regs);
937 unsigned long tick_mask = tick_ops->softint_mask;
938 int cpu = smp_processor_id();
939 struct clock_event_device *evt = &per_cpu(sparc64_events, cpu);
941 clear_softint(tick_mask);
945 kstat_this_cpu.irqs[0]++;
947 if (unlikely(!evt->event_handler)) {
949 "Spurious SPARC64 timer interrupt on cpu %d\n", cpu);
951 evt->event_handler(evt);
955 set_irq_regs(old_regs);
958 void __devinit setup_sparc64_timer(void)
960 struct clock_event_device *sevt;
961 unsigned long pstate;
963 /* Guarantee that the following sequences execute
966 __asm__ __volatile__("rdpr %%pstate, %0\n\t"
967 "wrpr %0, %1, %%pstate"
971 tick_ops->init_tick();
973 /* Restore PSTATE_IE. */
974 __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
978 sevt = &__get_cpu_var(sparc64_events);
980 memcpy(sevt, &sparc64_clockevent, sizeof(*sevt));
981 sevt->cpumask = cpumask_of_cpu(smp_processor_id());
983 clockevents_register_device(sevt);
986 #define SPARC64_NSEC_PER_CYC_SHIFT 10UL
988 static struct clocksource clocksource_tick = {
990 .mask = CLOCKSOURCE_MASK(64),
992 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
995 static void __init setup_clockevent_multiplier(unsigned long hz)
997 unsigned long mult, shift = 32;
1000 mult = div_sc(hz, NSEC_PER_SEC, shift);
1001 if (mult && (mult >> 32UL) == 0UL)
1007 sparc64_clockevent.shift = shift;
1008 sparc64_clockevent.mult = mult;
1011 static unsigned long tb_ticks_per_usec __read_mostly;
1013 void __delay(unsigned long loops)
1015 unsigned long bclock, now;
1017 bclock = tick_ops->get_tick();
1019 now = tick_ops->get_tick();
1020 } while ((now-bclock) < loops);
1022 EXPORT_SYMBOL(__delay);
1024 void udelay(unsigned long usecs)
1026 __delay(tb_ticks_per_usec * usecs);
1028 EXPORT_SYMBOL(udelay);
1030 void __init time_init(void)
1032 unsigned long clock = sparc64_init_timers();
1034 tb_ticks_per_usec = clock / USEC_PER_SEC;
1036 timer_ticks_per_nsec_quotient =
1037 clocksource_hz2mult(clock, SPARC64_NSEC_PER_CYC_SHIFT);
1039 clocksource_tick.name = tick_ops->name;
1040 clocksource_tick.mult =
1041 clocksource_hz2mult(clock,
1042 clocksource_tick.shift);
1043 clocksource_tick.read = tick_ops->get_tick;
1045 printk("clocksource: mult[%x] shift[%d]\n",
1046 clocksource_tick.mult, clocksource_tick.shift);
1048 clocksource_register(&clocksource_tick);
1050 sparc64_clockevent.name = tick_ops->name;
1052 setup_clockevent_multiplier(clock);
1054 sparc64_clockevent.max_delta_ns =
1055 clockevent_delta2ns(0x7fffffffffffffffUL, &sparc64_clockevent);
1056 sparc64_clockevent.min_delta_ns =
1057 clockevent_delta2ns(0xF, &sparc64_clockevent);
1059 printk("clockevent: mult[%lx] shift[%d]\n",
1060 sparc64_clockevent.mult, sparc64_clockevent.shift);
1062 setup_sparc64_timer();
1065 unsigned long long sched_clock(void)
1067 unsigned long ticks = tick_ops->get_tick();
1069 return (ticks * timer_ticks_per_nsec_quotient)
1070 >> SPARC64_NSEC_PER_CYC_SHIFT;
1073 static int set_rtc_mmss(unsigned long nowtime)
1075 int real_seconds, real_minutes, chip_minutes;
1076 void __iomem *mregs = mstk48t02_regs;
1078 unsigned long dregs = ds1287_regs;
1079 void __iomem *bregs = bq4802_regs;
1081 unsigned long dregs = 0UL;
1082 void __iomem *bregs = 0UL;
1084 unsigned long flags;
1088 * Not having a register set can lead to trouble.
1089 * Also starfire doesn't have a tod clock.
1091 if (!mregs && !dregs && !bregs)
1095 spin_lock_irqsave(&mostek_lock, flags);
1097 /* Read the current RTC minutes. */
1098 tmp = mostek_read(mregs + MOSTEK_CREG);
1099 tmp |= MSTK_CREG_READ;
1100 mostek_write(mregs + MOSTEK_CREG, tmp);
1102 chip_minutes = MSTK_REG_MIN(mregs);
1104 tmp = mostek_read(mregs + MOSTEK_CREG);
1105 tmp &= ~MSTK_CREG_READ;
1106 mostek_write(mregs + MOSTEK_CREG, tmp);
1109 * since we're only adjusting minutes and seconds,
1110 * don't interfere with hour overflow. This avoids
1111 * messing with unknown time zones but requires your
1112 * RTC not to be off by more than 15 minutes
1114 real_seconds = nowtime % 60;
1115 real_minutes = nowtime / 60;
1116 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1117 real_minutes += 30; /* correct for half hour time zone */
1120 if (abs(real_minutes - chip_minutes) < 30) {
1121 tmp = mostek_read(mregs + MOSTEK_CREG);
1122 tmp |= MSTK_CREG_WRITE;
1123 mostek_write(mregs + MOSTEK_CREG, tmp);
1125 MSTK_SET_REG_SEC(mregs,real_seconds);
1126 MSTK_SET_REG_MIN(mregs,real_minutes);
1128 tmp = mostek_read(mregs + MOSTEK_CREG);
1129 tmp &= ~MSTK_CREG_WRITE;
1130 mostek_write(mregs + MOSTEK_CREG, tmp);
1132 spin_unlock_irqrestore(&mostek_lock, flags);
1136 spin_unlock_irqrestore(&mostek_lock, flags);
1142 unsigned char val = readb(bregs + 0x0e);
1144 /* BQ4802 RTC chip. */
1146 writeb(val | 0x08, bregs + 0x0e);
1148 chip_minutes = readb(bregs + 0x02);
1149 BCD_TO_BIN(chip_minutes);
1150 real_seconds = nowtime % 60;
1151 real_minutes = nowtime / 60;
1152 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1156 if (abs(real_minutes - chip_minutes) < 30) {
1157 BIN_TO_BCD(real_seconds);
1158 BIN_TO_BCD(real_minutes);
1159 writeb(real_seconds, bregs + 0x00);
1160 writeb(real_minutes, bregs + 0x02);
1163 "set_rtc_mmss: can't update from %d to %d\n",
1164 chip_minutes, real_minutes);
1168 writeb(val, bregs + 0x0e);
1173 unsigned char save_control, save_freq_select;
1175 /* Stolen from arch/i386/kernel/time.c, see there for
1176 * credits and descriptive comments.
1178 spin_lock_irqsave(&rtc_lock, flags);
1179 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
1180 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1182 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
1183 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1185 chip_minutes = CMOS_READ(RTC_MINUTES);
1186 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1187 BCD_TO_BIN(chip_minutes);
1188 real_seconds = nowtime % 60;
1189 real_minutes = nowtime / 60;
1190 if (((abs(real_minutes - chip_minutes) + 15)/30) & 1)
1194 if (abs(real_minutes - chip_minutes) < 30) {
1195 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1196 BIN_TO_BCD(real_seconds);
1197 BIN_TO_BCD(real_minutes);
1199 CMOS_WRITE(real_seconds,RTC_SECONDS);
1200 CMOS_WRITE(real_minutes,RTC_MINUTES);
1203 "set_rtc_mmss: can't update from %d to %d\n",
1204 chip_minutes, real_minutes);
1208 CMOS_WRITE(save_control, RTC_CONTROL);
1209 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1210 spin_unlock_irqrestore(&rtc_lock, flags);
1216 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
1217 static unsigned char mini_rtc_status; /* bitmapped status byte. */
1220 #define STARTOFTIME 1970
1221 #define SECDAY 86400L
1222 #define SECYR (SECDAY * 365)
1223 #define leapyear(year) ((year) % 4 == 0 && \
1224 ((year) % 100 != 0 || (year) % 400 == 0))
1225 #define days_in_year(a) (leapyear(a) ? 366 : 365)
1226 #define days_in_month(a) (month_days[(a) - 1])
1228 static int month_days[12] = {
1229 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1233 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1235 static void GregorianDay(struct rtc_time * tm)
1240 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1242 lastYear = tm->tm_year - 1;
1245 * Number of leap corrections to apply up to end of last year
1247 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1250 * This year is a leap year if it is divisible by 4 except when it is
1251 * divisible by 100 unless it is divisible by 400
1253 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1255 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1257 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1260 tm->tm_wday = day % 7;
1263 static void to_tm(int tim, struct rtc_time *tm)
1266 register long hms, day;
1271 /* Hours, minutes, seconds are easy */
1272 tm->tm_hour = hms / 3600;
1273 tm->tm_min = (hms % 3600) / 60;
1274 tm->tm_sec = (hms % 3600) % 60;
1276 /* Number of years in days */
1277 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1278 day -= days_in_year(i);
1281 /* Number of months in days left */
1282 if (leapyear(tm->tm_year))
1283 days_in_month(FEBRUARY) = 29;
1284 for (i = 1; day >= days_in_month(i); i++)
1285 day -= days_in_month(i);
1286 days_in_month(FEBRUARY) = 28;
1289 /* Days are what is left over (+1) from all that. */
1290 tm->tm_mday = day + 1;
1293 * Determine the day of week
1298 /* Both Starfire and SUN4V give us seconds since Jan 1st, 1970,
1299 * aka Unix time. So we have to convert to/from rtc_time.
1301 static void starfire_get_rtc_time(struct rtc_time *time)
1303 u32 seconds = starfire_get_time();
1305 to_tm(seconds, time);
1306 time->tm_year -= 1900;
1310 static int starfire_set_rtc_time(struct rtc_time *time)
1312 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1313 time->tm_mday, time->tm_hour,
1314 time->tm_min, time->tm_sec);
1316 return starfire_set_time(seconds);
1319 static void hypervisor_get_rtc_time(struct rtc_time *time)
1321 u32 seconds = hypervisor_get_time();
1323 to_tm(seconds, time);
1324 time->tm_year -= 1900;
1328 static int hypervisor_set_rtc_time(struct rtc_time *time)
1330 u32 seconds = mktime(time->tm_year + 1900, time->tm_mon + 1,
1331 time->tm_mday, time->tm_hour,
1332 time->tm_min, time->tm_sec);
1334 return hypervisor_set_time(seconds);
1338 static void bq4802_get_rtc_time(struct rtc_time *time)
1340 unsigned char val = readb(bq4802_regs + 0x0e);
1341 unsigned int century;
1343 writeb(val | 0x08, bq4802_regs + 0x0e);
1345 time->tm_sec = readb(bq4802_regs + 0x00);
1346 time->tm_min = readb(bq4802_regs + 0x02);
1347 time->tm_hour = readb(bq4802_regs + 0x04);
1348 time->tm_mday = readb(bq4802_regs + 0x06);
1349 time->tm_mon = readb(bq4802_regs + 0x09);
1350 time->tm_year = readb(bq4802_regs + 0x0a);
1351 time->tm_wday = readb(bq4802_regs + 0x08);
1352 century = readb(bq4802_regs + 0x0f);
1354 writeb(val, bq4802_regs + 0x0e);
1356 BCD_TO_BIN(time->tm_sec);
1357 BCD_TO_BIN(time->tm_min);
1358 BCD_TO_BIN(time->tm_hour);
1359 BCD_TO_BIN(time->tm_mday);
1360 BCD_TO_BIN(time->tm_mon);
1361 BCD_TO_BIN(time->tm_year);
1362 BCD_TO_BIN(time->tm_wday);
1363 BCD_TO_BIN(century);
1365 time->tm_year += (century * 100);
1366 time->tm_year -= 1900;
1371 static int bq4802_set_rtc_time(struct rtc_time *time)
1373 unsigned char val = readb(bq4802_regs + 0x0e);
1374 unsigned char sec, min, hrs, day, mon, yrs, century;
1377 year = time->tm_year + 1900;
1378 century = year / 100;
1381 mon = time->tm_mon + 1; /* tm_mon starts at zero */
1382 day = time->tm_mday;
1383 hrs = time->tm_hour;
1393 BIN_TO_BCD(century);
1395 writeb(val | 0x08, bq4802_regs + 0x0e);
1397 writeb(sec, bq4802_regs + 0x00);
1398 writeb(min, bq4802_regs + 0x02);
1399 writeb(hrs, bq4802_regs + 0x04);
1400 writeb(day, bq4802_regs + 0x06);
1401 writeb(mon, bq4802_regs + 0x09);
1402 writeb(yrs, bq4802_regs + 0x0a);
1403 writeb(century, bq4802_regs + 0x0f);
1405 writeb(val, bq4802_regs + 0x0e);
1410 static void cmos_get_rtc_time(struct rtc_time *rtc_tm)
1414 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1415 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1416 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1417 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1418 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1419 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1420 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1422 ctrl = CMOS_READ(RTC_CONTROL);
1423 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1424 BCD_TO_BIN(rtc_tm->tm_sec);
1425 BCD_TO_BIN(rtc_tm->tm_min);
1426 BCD_TO_BIN(rtc_tm->tm_hour);
1427 BCD_TO_BIN(rtc_tm->tm_mday);
1428 BCD_TO_BIN(rtc_tm->tm_mon);
1429 BCD_TO_BIN(rtc_tm->tm_year);
1430 BCD_TO_BIN(rtc_tm->tm_wday);
1433 if (rtc_tm->tm_year <= 69)
1434 rtc_tm->tm_year += 100;
1439 static int cmos_set_rtc_time(struct rtc_time *rtc_tm)
1441 unsigned char mon, day, hrs, min, sec;
1442 unsigned char save_control, save_freq_select;
1445 yrs = rtc_tm->tm_year;
1446 mon = rtc_tm->tm_mon + 1;
1447 day = rtc_tm->tm_mday;
1448 hrs = rtc_tm->tm_hour;
1449 min = rtc_tm->tm_min;
1450 sec = rtc_tm->tm_sec;
1455 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1464 save_control = CMOS_READ(RTC_CONTROL);
1465 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
1466 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1467 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
1469 CMOS_WRITE(yrs, RTC_YEAR);
1470 CMOS_WRITE(mon, RTC_MONTH);
1471 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
1472 CMOS_WRITE(hrs, RTC_HOURS);
1473 CMOS_WRITE(min, RTC_MINUTES);
1474 CMOS_WRITE(sec, RTC_SECONDS);
1476 CMOS_WRITE(save_control, RTC_CONTROL);
1477 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1481 #endif /* CONFIG_PCI */
1483 static void mostek_get_rtc_time(struct rtc_time *rtc_tm)
1485 void __iomem *regs = mstk48t02_regs;
1488 spin_lock_irq(&mostek_lock);
1490 tmp = mostek_read(regs + MOSTEK_CREG);
1491 tmp |= MSTK_CREG_READ;
1492 mostek_write(regs + MOSTEK_CREG, tmp);
1494 rtc_tm->tm_sec = MSTK_REG_SEC(regs);
1495 rtc_tm->tm_min = MSTK_REG_MIN(regs);
1496 rtc_tm->tm_hour = MSTK_REG_HOUR(regs);
1497 rtc_tm->tm_mday = MSTK_REG_DOM(regs);
1498 rtc_tm->tm_mon = MSTK_REG_MONTH(regs);
1499 rtc_tm->tm_year = MSTK_CVT_YEAR( MSTK_REG_YEAR(regs) );
1500 rtc_tm->tm_wday = MSTK_REG_DOW(regs);
1502 tmp = mostek_read(regs + MOSTEK_CREG);
1503 tmp &= ~MSTK_CREG_READ;
1504 mostek_write(regs + MOSTEK_CREG, tmp);
1506 spin_unlock_irq(&mostek_lock);
1510 rtc_tm->tm_year -= 1900;
1513 static int mostek_set_rtc_time(struct rtc_time *rtc_tm)
1515 unsigned char mon, day, hrs, min, sec, wday;
1516 void __iomem *regs = mstk48t02_regs;
1520 yrs = rtc_tm->tm_year + 1900;
1521 mon = rtc_tm->tm_mon + 1;
1522 day = rtc_tm->tm_mday;
1523 wday = rtc_tm->tm_wday + 1;
1524 hrs = rtc_tm->tm_hour;
1525 min = rtc_tm->tm_min;
1526 sec = rtc_tm->tm_sec;
1528 spin_lock_irq(&mostek_lock);
1530 tmp = mostek_read(regs + MOSTEK_CREG);
1531 tmp |= MSTK_CREG_WRITE;
1532 mostek_write(regs + MOSTEK_CREG, tmp);
1534 MSTK_SET_REG_SEC(regs, sec);
1535 MSTK_SET_REG_MIN(regs, min);
1536 MSTK_SET_REG_HOUR(regs, hrs);
1537 MSTK_SET_REG_DOW(regs, wday);
1538 MSTK_SET_REG_DOM(regs, day);
1539 MSTK_SET_REG_MONTH(regs, mon);
1540 MSTK_SET_REG_YEAR(regs, yrs - MSTK_YEAR_ZERO);
1542 tmp = mostek_read(regs + MOSTEK_CREG);
1543 tmp &= ~MSTK_CREG_WRITE;
1544 mostek_write(regs + MOSTEK_CREG, tmp);
1546 spin_unlock_irq(&mostek_lock);
1551 struct mini_rtc_ops {
1552 void (*get_rtc_time)(struct rtc_time *);
1553 int (*set_rtc_time)(struct rtc_time *);
1556 static struct mini_rtc_ops starfire_rtc_ops = {
1557 .get_rtc_time = starfire_get_rtc_time,
1558 .set_rtc_time = starfire_set_rtc_time,
1561 static struct mini_rtc_ops hypervisor_rtc_ops = {
1562 .get_rtc_time = hypervisor_get_rtc_time,
1563 .set_rtc_time = hypervisor_set_rtc_time,
1567 static struct mini_rtc_ops bq4802_rtc_ops = {
1568 .get_rtc_time = bq4802_get_rtc_time,
1569 .set_rtc_time = bq4802_set_rtc_time,
1572 static struct mini_rtc_ops cmos_rtc_ops = {
1573 .get_rtc_time = cmos_get_rtc_time,
1574 .set_rtc_time = cmos_set_rtc_time,
1576 #endif /* CONFIG_PCI */
1578 static struct mini_rtc_ops mostek_rtc_ops = {
1579 .get_rtc_time = mostek_get_rtc_time,
1580 .set_rtc_time = mostek_set_rtc_time,
1583 static struct mini_rtc_ops *mini_rtc_ops;
1585 static inline void mini_get_rtc_time(struct rtc_time *time)
1587 unsigned long flags;
1589 spin_lock_irqsave(&rtc_lock, flags);
1590 mini_rtc_ops->get_rtc_time(time);
1591 spin_unlock_irqrestore(&rtc_lock, flags);
1594 static inline int mini_set_rtc_time(struct rtc_time *time)
1596 unsigned long flags;
1599 spin_lock_irqsave(&rtc_lock, flags);
1600 err = mini_rtc_ops->set_rtc_time(time);
1601 spin_unlock_irqrestore(&rtc_lock, flags);
1606 static int mini_rtc_ioctl(struct inode *inode, struct file *file,
1607 unsigned int cmd, unsigned long arg)
1609 struct rtc_time wtime;
1610 void __user *argp = (void __user *)arg;
1620 case RTC_UIE_OFF: /* disable ints from RTC updates. */
1623 case RTC_UIE_ON: /* enable ints for RTC updates. */
1626 case RTC_RD_TIME: /* Read the time/date from RTC */
1627 /* this doesn't get week-day, who cares */
1628 memset(&wtime, 0, sizeof(wtime));
1629 mini_get_rtc_time(&wtime);
1631 return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
1633 case RTC_SET_TIME: /* Set the RTC */
1637 if (!capable(CAP_SYS_TIME))
1640 if (copy_from_user(&wtime, argp, sizeof(wtime)))
1643 year = wtime.tm_year + 1900;
1644 days = month_days[wtime.tm_mon] +
1645 ((wtime.tm_mon == 1) && leapyear(year));
1647 if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) ||
1648 (wtime.tm_mday < 1))
1651 if (wtime.tm_mday < 0 || wtime.tm_mday > days)
1654 if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
1655 wtime.tm_min < 0 || wtime.tm_min >= 60 ||
1656 wtime.tm_sec < 0 || wtime.tm_sec >= 60)
1659 return mini_set_rtc_time(&wtime);
1666 static int mini_rtc_open(struct inode *inode, struct file *file)
1669 if (mini_rtc_status & RTC_IS_OPEN) {
1674 mini_rtc_status |= RTC_IS_OPEN;
1680 static int mini_rtc_release(struct inode *inode, struct file *file)
1682 mini_rtc_status &= ~RTC_IS_OPEN;
1687 static const struct file_operations mini_rtc_fops = {
1688 .owner = THIS_MODULE,
1689 .ioctl = mini_rtc_ioctl,
1690 .open = mini_rtc_open,
1691 .release = mini_rtc_release,
1694 static struct miscdevice rtc_mini_dev =
1698 .fops = &mini_rtc_fops,
1701 static int __init rtc_mini_init(void)
1705 if (tlb_type == hypervisor)
1706 mini_rtc_ops = &hypervisor_rtc_ops;
1707 else if (this_is_starfire)
1708 mini_rtc_ops = &starfire_rtc_ops;
1710 else if (bq4802_regs)
1711 mini_rtc_ops = &bq4802_rtc_ops;
1712 else if (ds1287_regs)
1713 mini_rtc_ops = &cmos_rtc_ops;
1714 #endif /* CONFIG_PCI */
1715 else if (mstk48t02_regs)
1716 mini_rtc_ops = &mostek_rtc_ops;
1720 printk(KERN_INFO "Mini RTC Driver\n");
1722 retval = misc_register(&rtc_mini_dev);
1729 static void __exit rtc_mini_exit(void)
1731 misc_deregister(&rtc_mini_dev);
1734 int __devinit read_current_timer(unsigned long *timer_val)
1736 *timer_val = tick_ops->get_tick();
1740 module_init(rtc_mini_init);
1741 module_exit(rtc_mini_exit);