2 * Real Time Clock interface for Linux
4 * Copyright (C) 1996 Paul Gortmaker
6 * This driver allows use of the real time clock (built into
7 * nearly all computers) from user space. It exports the /dev/rtc
8 * interface supporting various ioctl() and also the
9 * /proc/driver/rtc pseudo-file for status information.
11 * The ioctls can be used to set the interrupt behaviour and
12 * generation rate from the RTC via IRQ 8. Then the /dev/rtc
13 * interface can be used to make use of these timer interrupts,
14 * be they interval or alarm based.
16 * The /dev/rtc interface will block on reads until an interrupt
17 * has been received. If a RTC interrupt has already happened,
18 * it will output an unsigned long and then block. The output value
19 * contains the interrupt status in the low byte and the number of
20 * interrupts since the last read in the remaining high bytes. The
21 * /dev/rtc interface can also be used with the select(2) call.
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version
26 * 2 of the License, or (at your option) any later version.
28 * Based on other minimal char device drivers, like Alan's
29 * watchdog, Ted's random, etc. etc.
31 * 1.07 Paul Gortmaker.
32 * 1.08 Miquel van Smoorenburg: disallow certain things on the
33 * DEC Alpha as the CMOS clock is also used for other things.
34 * 1.09 Nikita Schmidt: epoch support and some Alpha cleanup.
35 * 1.09a Pete Zaitcev: Sun SPARC
36 * 1.09b Jeff Garzik: Modularize, init cleanup
37 * 1.09c Jeff Garzik: SMP cleanup
38 * 1.10 Paul Barton-Davis: add support for async I/O
39 * 1.10a Andrea Arcangeli: Alpha updates
40 * 1.10b Andrew Morton: SMP lock fix
41 * 1.10c Cesar Barros: SMP locking fixes and cleanup
42 * 1.10d Paul Gortmaker: delete paranoia check in rtc_exit
43 * 1.10e Maciej W. Rozycki: Handle DECstation's year weirdness.
44 * 1.11 Takashi Iwai: Kernel access functions
45 * rtc_register/rtc_unregister/rtc_control
46 * 1.11a Daniele Bellucci: Audit create_proc_read_entry in rtc_init
47 * 1.12 Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer
48 * CONFIG_HPET_EMULATE_RTC
52 #define RTC_VERSION "1.12"
54 #define RTC_IO_EXTENT 0x8
57 * Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
58 * interrupts disabled. Due to the index-port/data-port (0x70/0x71)
59 * design of the RTC, we don't want two different things trying to
60 * get to it at once. (e.g. the periodic 11 min sync from time.c vs.
64 #include <linux/config.h>
65 #include <linux/interrupt.h>
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/types.h>
69 #include <linux/miscdevice.h>
70 #include <linux/ioport.h>
71 #include <linux/fcntl.h>
72 #include <linux/mc146818rtc.h>
73 #include <linux/init.h>
74 #include <linux/poll.h>
75 #include <linux/proc_fs.h>
76 #include <linux/spinlock.h>
77 #include <linux/sysctl.h>
78 #include <linux/wait.h>
79 #include <linux/bcd.h>
81 #include <asm/current.h>
82 #include <asm/uaccess.h>
83 #include <asm/system.h>
90 #include <linux/pci.h>
96 static unsigned long rtc_port;
97 static int rtc_irq = PCI_IRQ_NONE;
101 static int rtc_has_irq = 1;
104 #ifndef CONFIG_HPET_EMULATE_RTC
105 #define is_hpet_enabled() 0
106 #define hpet_set_alarm_time(hrs, min, sec) 0
107 #define hpet_set_periodic_freq(arg) 0
108 #define hpet_mask_rtc_irq_bit(arg) 0
109 #define hpet_set_rtc_irq_bit(arg) 0
110 #define hpet_rtc_timer_init() do { } while (0)
111 #define hpet_rtc_dropped_irq() 0
112 static inline irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) {return 0;}
114 extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
118 * We sponge a minor off of the misc major. No need slurping
119 * up another valuable major dev number for this. If you add
120 * an ioctl, make sure you don't conflict with SPARC's RTC
124 static struct fasync_struct *rtc_async_queue;
126 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
129 static struct timer_list rtc_irq_timer;
132 static ssize_t rtc_read(struct file *file, char __user *buf,
133 size_t count, loff_t *ppos);
135 static int rtc_ioctl(struct inode *inode, struct file *file,
136 unsigned int cmd, unsigned long arg);
139 static unsigned int rtc_poll(struct file *file, poll_table *wait);
142 static void get_rtc_alm_time (struct rtc_time *alm_tm);
144 static void rtc_dropped_irq(unsigned long data);
146 static void set_rtc_irq_bit(unsigned char bit);
147 static void mask_rtc_irq_bit(unsigned char bit);
150 static int rtc_read_proc(char *page, char **start, off_t off,
151 int count, int *eof, void *data);
154 * Bits in rtc_status. (6 bits of room for future expansion)
157 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
158 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
161 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
162 * protected by the big kernel lock. However, ioctl can still disable the timer
163 * in rtc_status and then with del_timer after the interrupt has read
164 * rtc_status but before mod_timer is called, which would then reenable the
165 * timer (but you would need to have an awful timing before you'd trip on it)
167 static unsigned long rtc_status = 0; /* bitmapped status byte. */
168 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
169 static unsigned long rtc_irq_data = 0; /* our output to the world */
170 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
174 * rtc_task_lock nests inside rtc_lock.
176 static spinlock_t rtc_task_lock = SPIN_LOCK_UNLOCKED;
177 static rtc_task_t *rtc_callback = NULL;
181 * If this driver ever becomes modularised, it will be really nice
182 * to make the epoch retain its value across module reload...
185 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
187 static const unsigned char days_in_mo[] =
188 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
191 * Returns true if a clock update is in progress
193 static inline unsigned char rtc_is_updating(void)
197 spin_lock_irq(&rtc_lock);
198 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
199 spin_unlock_irq(&rtc_lock);
205 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
206 * but there is possibility of conflicting with the set_rtc_mmss()
207 * call (the rtc irq and the timer irq can easily run at the same
208 * time in two different CPUs). So we need to serialize
209 * accesses to the chip with the rtc_lock spinlock that each
210 * architecture should implement in the timer code.
211 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
214 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
217 * Can be an alarm interrupt, update complete interrupt,
218 * or a periodic interrupt. We store the status in the
219 * low byte and the number of interrupts received since
220 * the last read in the remainder of rtc_irq_data.
223 spin_lock (&rtc_lock);
224 rtc_irq_data += 0x100;
225 rtc_irq_data &= ~0xff;
226 if (is_hpet_enabled()) {
228 * In this case it is HPET RTC interrupt handler
229 * calling us, with the interrupt information
230 * passed as arg1, instead of irq.
232 rtc_irq_data |= (unsigned long)irq & 0xF0;
234 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
237 if (rtc_status & RTC_TIMER_ON)
238 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
240 spin_unlock (&rtc_lock);
242 /* Now do the rest of the actions */
243 spin_lock(&rtc_task_lock);
245 rtc_callback->func(rtc_callback->private_data);
246 spin_unlock(&rtc_task_lock);
247 wake_up_interruptible(&rtc_wait);
249 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
256 * sysctl-tuning infrastructure.
258 static ctl_table rtc_table[] = {
261 .procname = "max-user-freq",
262 .data = &rtc_max_user_freq,
263 .maxlen = sizeof(int),
265 .proc_handler = &proc_dointvec,
270 static ctl_table rtc_root[] = {
281 static ctl_table dev_root[] = {
292 static struct ctl_table_header *sysctl_header;
294 static int __init init_sysctl(void)
296 sysctl_header = register_sysctl_table(dev_root, 0);
300 static void __exit cleanup_sysctl(void)
302 unregister_sysctl_table(sysctl_header);
306 * Now all the various file operations that we export.
309 static ssize_t rtc_read(struct file *file, char __user *buf,
310 size_t count, loff_t *ppos)
315 DECLARE_WAITQUEUE(wait, current);
319 if (rtc_has_irq == 0)
322 if (count < sizeof(unsigned))
325 add_wait_queue(&rtc_wait, &wait);
328 /* First make it right. Then make it fast. Putting this whole
329 * block within the parentheses of a while would be too
330 * confusing. And no, xchg() is not the answer. */
332 __set_current_state(TASK_INTERRUPTIBLE);
334 spin_lock_irq (&rtc_lock);
337 spin_unlock_irq (&rtc_lock);
342 if (file->f_flags & O_NONBLOCK) {
346 if (signal_pending(current)) {
347 retval = -ERESTARTSYS;
353 if (count < sizeof(unsigned long))
354 retval = put_user(data, (unsigned int __user *)buf) ?: sizeof(int);
356 retval = put_user(data, (unsigned long __user *)buf) ?: sizeof(long);
358 current->state = TASK_RUNNING;
359 remove_wait_queue(&rtc_wait, &wait);
365 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
367 struct rtc_time wtime;
370 if (rtc_has_irq == 0) {
387 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
389 mask_rtc_irq_bit(RTC_AIE);
392 case RTC_AIE_ON: /* Allow alarm interrupts. */
394 set_rtc_irq_bit(RTC_AIE);
397 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
399 mask_rtc_irq_bit(RTC_PIE);
400 if (rtc_status & RTC_TIMER_ON) {
401 spin_lock_irq (&rtc_lock);
402 rtc_status &= ~RTC_TIMER_ON;
403 del_timer(&rtc_irq_timer);
404 spin_unlock_irq (&rtc_lock);
408 case RTC_PIE_ON: /* Allow periodic ints */
412 * We don't really want Joe User enabling more
413 * than 64Hz of interrupts on a multi-user machine.
415 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
416 (!capable(CAP_SYS_RESOURCE)))
419 if (!(rtc_status & RTC_TIMER_ON)) {
420 spin_lock_irq (&rtc_lock);
421 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
422 add_timer(&rtc_irq_timer);
423 rtc_status |= RTC_TIMER_ON;
424 spin_unlock_irq (&rtc_lock);
426 set_rtc_irq_bit(RTC_PIE);
429 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
431 mask_rtc_irq_bit(RTC_UIE);
434 case RTC_UIE_ON: /* Allow ints for RTC updates. */
436 set_rtc_irq_bit(RTC_UIE);
440 case RTC_ALM_READ: /* Read the present alarm time */
443 * This returns a struct rtc_time. Reading >= 0xc0
444 * means "don't care" or "match all". Only the tm_hour,
445 * tm_min, and tm_sec values are filled in.
447 memset(&wtime, 0, sizeof(struct rtc_time));
448 get_rtc_alm_time(&wtime);
451 case RTC_ALM_SET: /* Store a time into the alarm */
454 * This expects a struct rtc_time. Writing 0xff means
455 * "don't care" or "match all". Only the tm_hour,
456 * tm_min and tm_sec are used.
458 unsigned char hrs, min, sec;
459 struct rtc_time alm_tm;
461 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
462 sizeof(struct rtc_time)))
465 hrs = alm_tm.tm_hour;
469 spin_lock_irq(&rtc_lock);
470 if (hpet_set_alarm_time(hrs, min, sec)) {
472 * Fallthru and set alarm time in CMOS too,
473 * so that we will get proper value in RTC_ALM_READ
476 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
479 if (sec < 60) BIN_TO_BCD(sec);
482 if (min < 60) BIN_TO_BCD(min);
485 if (hrs < 24) BIN_TO_BCD(hrs);
488 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
489 CMOS_WRITE(min, RTC_MINUTES_ALARM);
490 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
491 spin_unlock_irq(&rtc_lock);
495 case RTC_RD_TIME: /* Read the time/date from RTC */
497 memset(&wtime, 0, sizeof(struct rtc_time));
498 rtc_get_rtc_time(&wtime);
501 case RTC_SET_TIME: /* Set the RTC */
503 struct rtc_time rtc_tm;
504 unsigned char mon, day, hrs, min, sec, leap_yr;
505 unsigned char save_control, save_freq_select;
507 #ifdef CONFIG_DECSTATION
508 unsigned int real_yrs;
511 if (!capable(CAP_SYS_TIME))
514 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
515 sizeof(struct rtc_time)))
518 yrs = rtc_tm.tm_year + 1900;
519 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
520 day = rtc_tm.tm_mday;
521 hrs = rtc_tm.tm_hour;
528 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
530 if ((mon > 12) || (day == 0))
533 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
536 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
539 if ((yrs -= epoch) > 255) /* They are unsigned */
542 spin_lock_irq(&rtc_lock);
543 #ifdef CONFIG_DECSTATION
548 * We want to keep the year set to 73 until March
549 * for non-leap years, so that Feb, 29th is handled
552 if (!leap_yr && mon < 3) {
557 /* These limits and adjustments are independent of
558 * whether the chip is in binary mode or not.
561 spin_unlock_irq(&rtc_lock);
567 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
577 save_control = CMOS_READ(RTC_CONTROL);
578 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
579 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
580 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
582 #ifdef CONFIG_DECSTATION
583 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
585 CMOS_WRITE(yrs, RTC_YEAR);
586 CMOS_WRITE(mon, RTC_MONTH);
587 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
588 CMOS_WRITE(hrs, RTC_HOURS);
589 CMOS_WRITE(min, RTC_MINUTES);
590 CMOS_WRITE(sec, RTC_SECONDS);
592 CMOS_WRITE(save_control, RTC_CONTROL);
593 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
595 spin_unlock_irq(&rtc_lock);
599 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
601 return put_user(rtc_freq, (unsigned long __user *)arg);
603 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
609 * The max we can do is 8192Hz.
611 if ((arg < 2) || (arg > 8192))
614 * We don't really want Joe User generating more
615 * than 64Hz of interrupts on a multi-user machine.
617 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
620 while (arg > (1<<tmp))
624 * Check that the input was really a power of 2.
629 spin_lock_irq(&rtc_lock);
630 if (hpet_set_periodic_freq(arg)) {
631 spin_unlock_irq(&rtc_lock);
636 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
638 CMOS_WRITE(val, RTC_FREQ_SELECT);
639 spin_unlock_irq(&rtc_lock);
643 case RTC_EPOCH_READ: /* Read the epoch. */
645 return put_user (epoch, (unsigned long __user *)arg);
647 case RTC_EPOCH_SET: /* Set the epoch. */
650 * There were no RTC clocks before 1900.
655 if (!capable(CAP_SYS_TIME))
664 return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
667 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
670 return rtc_do_ioctl(cmd, arg, 0);
674 * We enforce only one user at a time here with the open/close.
675 * Also clear the previous interrupt data on an open, and clean
676 * up things on a close.
679 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
680 * needed here. Or anywhere else in this driver. */
681 static int rtc_open(struct inode *inode, struct file *file)
683 spin_lock_irq (&rtc_lock);
685 if(rtc_status & RTC_IS_OPEN)
688 rtc_status |= RTC_IS_OPEN;
691 spin_unlock_irq (&rtc_lock);
695 spin_unlock_irq (&rtc_lock);
699 static int rtc_fasync (int fd, struct file *filp, int on)
702 return fasync_helper (fd, filp, on, &rtc_async_queue);
705 static int rtc_release(struct inode *inode, struct file *file)
710 if (rtc_has_irq == 0)
714 * Turn off all interrupts once the device is no longer
715 * in use, and clear the data.
718 spin_lock_irq(&rtc_lock);
719 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
720 tmp = CMOS_READ(RTC_CONTROL);
724 CMOS_WRITE(tmp, RTC_CONTROL);
725 CMOS_READ(RTC_INTR_FLAGS);
727 if (rtc_status & RTC_TIMER_ON) {
728 rtc_status &= ~RTC_TIMER_ON;
729 del_timer(&rtc_irq_timer);
731 spin_unlock_irq(&rtc_lock);
733 if (file->f_flags & FASYNC) {
734 rtc_fasync (-1, file, 0);
739 spin_lock_irq (&rtc_lock);
741 rtc_status &= ~RTC_IS_OPEN;
742 spin_unlock_irq (&rtc_lock);
747 /* Called without the kernel lock - fine */
748 static unsigned int rtc_poll(struct file *file, poll_table *wait)
752 if (rtc_has_irq == 0)
755 poll_wait(file, &rtc_wait, wait);
757 spin_lock_irq (&rtc_lock);
759 spin_unlock_irq (&rtc_lock);
762 return POLLIN | POLLRDNORM;
771 EXPORT_SYMBOL(rtc_register);
772 EXPORT_SYMBOL(rtc_unregister);
773 EXPORT_SYMBOL(rtc_control);
775 int rtc_register(rtc_task_t *task)
780 if (task == NULL || task->func == NULL)
782 spin_lock_irq(&rtc_lock);
783 if (rtc_status & RTC_IS_OPEN) {
784 spin_unlock_irq(&rtc_lock);
787 spin_lock(&rtc_task_lock);
789 spin_unlock(&rtc_task_lock);
790 spin_unlock_irq(&rtc_lock);
793 rtc_status |= RTC_IS_OPEN;
795 spin_unlock(&rtc_task_lock);
796 spin_unlock_irq(&rtc_lock);
801 int rtc_unregister(rtc_task_t *task)
808 spin_lock_irq(&rtc_lock);
809 spin_lock(&rtc_task_lock);
810 if (rtc_callback != task) {
811 spin_unlock(&rtc_task_lock);
812 spin_unlock_irq(&rtc_lock);
817 /* disable controls */
818 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
819 tmp = CMOS_READ(RTC_CONTROL);
823 CMOS_WRITE(tmp, RTC_CONTROL);
824 CMOS_READ(RTC_INTR_FLAGS);
826 if (rtc_status & RTC_TIMER_ON) {
827 rtc_status &= ~RTC_TIMER_ON;
828 del_timer(&rtc_irq_timer);
830 rtc_status &= ~RTC_IS_OPEN;
831 spin_unlock(&rtc_task_lock);
832 spin_unlock_irq(&rtc_lock);
837 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
842 spin_lock_irq(&rtc_task_lock);
843 if (rtc_callback != task) {
844 spin_unlock_irq(&rtc_task_lock);
847 spin_unlock_irq(&rtc_task_lock);
848 return rtc_do_ioctl(cmd, arg, 1);
854 * The various file operations we support.
857 static struct file_operations rtc_fops = {
858 .owner = THIS_MODULE,
866 .release = rtc_release,
867 .fasync = rtc_fasync,
870 static struct miscdevice rtc_dev=
878 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
881 static int __init rtc_init(void)
883 #if defined(__alpha__) || defined(__mips__)
884 unsigned int year, ctrl;
885 unsigned long uip_watchdog;
889 struct linux_ebus *ebus;
890 struct linux_ebus_device *edev;
892 struct sparc_isa_bridge *isa_br;
893 struct sparc_isa_device *isa_dev;
898 for_each_ebus(ebus) {
899 for_each_ebusdev(edev, ebus) {
900 if(strcmp(edev->prom_name, "rtc") == 0) {
901 rtc_port = edev->resource[0].start;
902 rtc_irq = edev->irqs[0];
908 for_each_isa(isa_br) {
909 for_each_isadev(isa_dev, isa_br) {
910 if (strcmp(isa_dev->prom_name, "rtc") == 0) {
911 rtc_port = isa_dev->resource.start;
912 rtc_irq = isa_dev->irq;
918 printk(KERN_ERR "rtc_init: no PC rtc found\n");
922 if (rtc_irq == PCI_IRQ_NONE) {
928 * XXX Interrupt pin #7 in Espresso is shared between RTC and
929 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
930 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
932 if (request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
934 * Standard way for sparc to print irq's is to use
935 * __irq_itoa(). I think for EBus it's ok to use %d.
937 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
942 if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
943 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
948 if (is_hpet_enabled()) {
949 rtc_int_handler_ptr = hpet_rtc_interrupt;
951 rtc_int_handler_ptr = rtc_interrupt;
954 if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
955 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
956 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
957 release_region(RTC_PORT(0), RTC_IO_EXTENT);
960 hpet_rtc_timer_init();
964 #endif /* __sparc__ vs. others */
966 if (misc_register(&rtc_dev)) {
968 free_irq(RTC_IRQ, NULL);
970 release_region(RTC_PORT(0), RTC_IO_EXTENT);
973 if (create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL) == NULL) {
975 free_irq(RTC_IRQ, NULL);
977 release_region(RTC_PORT(0), RTC_IO_EXTENT);
978 misc_deregister(&rtc_dev);
982 #if defined(__alpha__) || defined(__mips__)
985 /* Each operating system on an Alpha uses its own epoch.
986 Let's try to guess which one we are using now. */
988 uip_watchdog = jiffies;
989 if (rtc_is_updating() != 0)
990 while (jiffies - uip_watchdog < 2*HZ/100) {
995 spin_lock_irq(&rtc_lock);
996 year = CMOS_READ(RTC_YEAR);
997 ctrl = CMOS_READ(RTC_CONTROL);
998 spin_unlock_irq(&rtc_lock);
1000 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1001 BCD_TO_BIN(year); /* This should never happen... */
1005 guess = "SRM (post-2000)";
1006 } else if (year >= 20 && year < 48) {
1008 guess = "ARC console";
1009 } else if (year >= 48 && year < 72) {
1011 guess = "Digital UNIX";
1012 #if defined(__mips__)
1013 } else if (year >= 72 && year < 74) {
1015 guess = "Digital DECstation";
1017 } else if (year >= 70) {
1019 guess = "Standard PC (1900)";
1023 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1026 if (rtc_has_irq == 0)
1029 init_timer(&rtc_irq_timer);
1030 rtc_irq_timer.function = rtc_dropped_irq;
1031 spin_lock_irq(&rtc_lock);
1033 if (!hpet_set_periodic_freq(rtc_freq)) {
1034 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1035 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1037 spin_unlock_irq(&rtc_lock);
1041 (void) init_sysctl();
1043 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1048 static void __exit rtc_exit (void)
1051 remove_proc_entry ("driver/rtc", NULL);
1052 misc_deregister(&rtc_dev);
1056 free_irq (rtc_irq, &rtc_port);
1058 release_region (RTC_PORT (0), RTC_IO_EXTENT);
1061 free_irq (RTC_IRQ, NULL);
1063 #endif /* __sparc__ */
1066 module_init(rtc_init);
1067 module_exit(rtc_exit);
1071 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1072 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1073 * Since the interrupt handler doesn't get called, the IRQ status
1074 * byte doesn't get read, and the RTC stops generating interrupts.
1075 * A timer is set, and will call this function if/when that happens.
1076 * To get it out of this stalled state, we just read the status.
1077 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1078 * (You *really* shouldn't be trying to use a non-realtime system
1079 * for something that requires a steady > 1KHz signal anyways.)
1082 static void rtc_dropped_irq(unsigned long data)
1086 spin_lock_irq (&rtc_lock);
1088 if (hpet_rtc_dropped_irq()) {
1089 spin_unlock_irq(&rtc_lock);
1093 /* Just in case someone disabled the timer from behind our back... */
1094 if (rtc_status & RTC_TIMER_ON)
1095 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1097 rtc_irq_data += ((rtc_freq/HZ)<<8);
1098 rtc_irq_data &= ~0xff;
1099 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1103 spin_unlock_irq(&rtc_lock);
1105 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1107 /* Now we have new data */
1108 wake_up_interruptible(&rtc_wait);
1110 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1115 * Info exported via "/proc/driver/rtc".
1118 static int rtc_proc_output (char *buf)
1120 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1121 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1124 unsigned char batt, ctrl;
1127 spin_lock_irq(&rtc_lock);
1128 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1129 ctrl = CMOS_READ(RTC_CONTROL);
1131 spin_unlock_irq(&rtc_lock);
1135 rtc_get_rtc_time(&tm);
1138 * There is no way to tell if the luser has the RTC set for local
1139 * time or for Universal Standard Time (GMT). Probably local though.
1142 "rtc_time\t: %02d:%02d:%02d\n"
1143 "rtc_date\t: %04d-%02d-%02d\n"
1144 "rtc_epoch\t: %04lu\n",
1145 tm.tm_hour, tm.tm_min, tm.tm_sec,
1146 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1148 get_rtc_alm_time(&tm);
1151 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1152 * match any value for that particular field. Values that are
1153 * greater than a valid time, but less than 0xc0 shouldn't appear.
1155 p += sprintf(p, "alarm\t\t: ");
1156 if (tm.tm_hour <= 24)
1157 p += sprintf(p, "%02d:", tm.tm_hour);
1159 p += sprintf(p, "**:");
1161 if (tm.tm_min <= 59)
1162 p += sprintf(p, "%02d:", tm.tm_min);
1164 p += sprintf(p, "**:");
1166 if (tm.tm_sec <= 59)
1167 p += sprintf(p, "%02d\n", tm.tm_sec);
1169 p += sprintf(p, "**\n");
1172 "DST_enable\t: %s\n"
1175 "square_wave\t: %s\n"
1177 "update_IRQ\t: %s\n"
1178 "periodic_IRQ\t: %s\n"
1179 "periodic_freq\t: %ld\n"
1180 "batt_status\t: %s\n",
1189 batt ? "okay" : "dead");
1196 static int rtc_read_proc(char *page, char **start, off_t off,
1197 int count, int *eof, void *data)
1199 int len = rtc_proc_output (page);
1200 if (len <= off+count) *eof = 1;
1201 *start = page + off;
1203 if (len>count) len = count;
1208 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1210 unsigned long uip_watchdog = jiffies;
1212 #ifdef CONFIG_DECSTATION
1213 unsigned int real_year;
1217 * read RTC once any update in progress is done. The update
1218 * can take just over 2ms. We wait 10 to 20ms. There is no need to
1219 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1220 * If you need to know *exactly* when a second has started, enable
1221 * periodic update complete interrupts, (via ioctl) and then
1222 * immediately read /dev/rtc which will block until you get the IRQ.
1223 * Once the read clears, read the RTC time (again via ioctl). Easy.
1226 if (rtc_is_updating() != 0)
1227 while (jiffies - uip_watchdog < 2*HZ/100) {
1233 * Only the values that we read from the RTC are set. We leave
1234 * tm_wday, tm_yday and tm_isdst untouched. Even though the
1235 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
1236 * by the RTC when initially set to a non-zero value.
1238 spin_lock_irq(&rtc_lock);
1239 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1240 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1241 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1242 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1243 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1244 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1245 #ifdef CONFIG_DECSTATION
1246 real_year = CMOS_READ(RTC_DEC_YEAR);
1248 ctrl = CMOS_READ(RTC_CONTROL);
1249 spin_unlock_irq(&rtc_lock);
1251 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1253 BCD_TO_BIN(rtc_tm->tm_sec);
1254 BCD_TO_BIN(rtc_tm->tm_min);
1255 BCD_TO_BIN(rtc_tm->tm_hour);
1256 BCD_TO_BIN(rtc_tm->tm_mday);
1257 BCD_TO_BIN(rtc_tm->tm_mon);
1258 BCD_TO_BIN(rtc_tm->tm_year);
1261 #ifdef CONFIG_DECSTATION
1262 rtc_tm->tm_year += real_year - 72;
1266 * Account for differences between how the RTC uses the values
1267 * and how they are defined in a struct rtc_time;
1269 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1270 rtc_tm->tm_year += 100;
1275 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1280 * Only the values that we read from the RTC are set. That
1281 * means only tm_hour, tm_min, and tm_sec.
1283 spin_lock_irq(&rtc_lock);
1284 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1285 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1286 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1287 ctrl = CMOS_READ(RTC_CONTROL);
1288 spin_unlock_irq(&rtc_lock);
1290 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1292 BCD_TO_BIN(alm_tm->tm_sec);
1293 BCD_TO_BIN(alm_tm->tm_min);
1294 BCD_TO_BIN(alm_tm->tm_hour);
1300 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1301 * Rumour has it that if you frob the interrupt enable/disable
1302 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1303 * ensure you actually start getting interrupts. Probably for
1304 * compatibility with older/broken chipset RTC implementations.
1305 * We also clear out any old irq data after an ioctl() that
1306 * meddles with the interrupt enable/disable bits.
1309 static void mask_rtc_irq_bit(unsigned char bit)
1313 spin_lock_irq(&rtc_lock);
1314 if (hpet_mask_rtc_irq_bit(bit)) {
1315 spin_unlock_irq(&rtc_lock);
1318 val = CMOS_READ(RTC_CONTROL);
1320 CMOS_WRITE(val, RTC_CONTROL);
1321 CMOS_READ(RTC_INTR_FLAGS);
1324 spin_unlock_irq(&rtc_lock);
1327 static void set_rtc_irq_bit(unsigned char bit)
1331 spin_lock_irq(&rtc_lock);
1332 if (hpet_set_rtc_irq_bit(bit)) {
1333 spin_unlock_irq(&rtc_lock);
1336 val = CMOS_READ(RTC_CONTROL);
1338 CMOS_WRITE(val, RTC_CONTROL);
1339 CMOS_READ(RTC_INTR_FLAGS);
1342 spin_unlock_irq(&rtc_lock);
1346 MODULE_AUTHOR("Paul Gortmaker");
1347 MODULE_LICENSE("GPL");
1348 MODULE_ALIAS_MISCDEV(RTC_MINOR);