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
49 * 1.12ac Alan Cox: Allow read access to the day of week register
52 #define RTC_VERSION "1.12ac"
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/interrupt.h>
65 #include <linux/module.h>
66 #include <linux/kernel.h>
67 #include <linux/types.h>
68 #include <linux/miscdevice.h>
69 #include <linux/ioport.h>
70 #include <linux/fcntl.h>
71 #include <linux/mc146818rtc.h>
72 #include <linux/init.h>
73 #include <linux/poll.h>
74 #include <linux/proc_fs.h>
75 #include <linux/seq_file.h>
76 #include <linux/spinlock.h>
77 #include <linux/sysctl.h>
78 #include <linux/wait.h>
79 #include <linux/bcd.h>
80 #include <linux/delay.h>
82 #include <asm/current.h>
83 #include <asm/uaccess.h>
84 #include <asm/system.h>
91 #include <linux/pci.h>
97 static unsigned long rtc_port;
98 static int rtc_irq = PCI_IRQ_NONE;
101 #ifdef CONFIG_HPET_RTC_IRQ
106 static int rtc_has_irq = 1;
109 #ifndef CONFIG_HPET_EMULATE_RTC
110 #define is_hpet_enabled() 0
111 #define hpet_set_alarm_time(hrs, min, sec) 0
112 #define hpet_set_periodic_freq(arg) 0
113 #define hpet_mask_rtc_irq_bit(arg) 0
114 #define hpet_set_rtc_irq_bit(arg) 0
115 #define hpet_rtc_timer_init() do { } while (0)
116 #define hpet_rtc_dropped_irq() 0
117 static inline irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) {return 0;}
119 extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
123 * We sponge a minor off of the misc major. No need slurping
124 * up another valuable major dev number for this. If you add
125 * an ioctl, make sure you don't conflict with SPARC's RTC
129 static struct fasync_struct *rtc_async_queue;
131 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
134 static struct timer_list rtc_irq_timer;
137 static ssize_t rtc_read(struct file *file, char __user *buf,
138 size_t count, loff_t *ppos);
140 static int rtc_ioctl(struct inode *inode, struct file *file,
141 unsigned int cmd, unsigned long arg);
144 static unsigned int rtc_poll(struct file *file, poll_table *wait);
147 static void get_rtc_alm_time (struct rtc_time *alm_tm);
149 static void rtc_dropped_irq(unsigned long data);
151 static void set_rtc_irq_bit_locked(unsigned char bit);
152 static void mask_rtc_irq_bit_locked(unsigned char bit);
154 static inline void set_rtc_irq_bit(unsigned char bit)
156 spin_lock_irq(&rtc_lock);
157 set_rtc_irq_bit_locked(bit);
158 spin_unlock_irq(&rtc_lock);
161 static void mask_rtc_irq_bit(unsigned char bit)
163 spin_lock_irq(&rtc_lock);
164 mask_rtc_irq_bit_locked(bit);
165 spin_unlock_irq(&rtc_lock);
169 static int rtc_proc_open(struct inode *inode, struct file *file);
172 * Bits in rtc_status. (6 bits of room for future expansion)
175 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
176 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
179 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
180 * protected by the big kernel lock. However, ioctl can still disable the timer
181 * in rtc_status and then with del_timer after the interrupt has read
182 * rtc_status but before mod_timer is called, which would then reenable the
183 * timer (but you would need to have an awful timing before you'd trip on it)
185 static unsigned long rtc_status = 0; /* bitmapped status byte. */
186 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
187 static unsigned long rtc_irq_data = 0; /* our output to the world */
188 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
192 * rtc_task_lock nests inside rtc_lock.
194 static DEFINE_SPINLOCK(rtc_task_lock);
195 static rtc_task_t *rtc_callback = NULL;
199 * If this driver ever becomes modularised, it will be really nice
200 * to make the epoch retain its value across module reload...
203 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
205 static const unsigned char days_in_mo[] =
206 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
209 * Returns true if a clock update is in progress
211 static inline unsigned char rtc_is_updating(void)
215 spin_lock_irq(&rtc_lock);
216 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
217 spin_unlock_irq(&rtc_lock);
223 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
224 * but there is possibility of conflicting with the set_rtc_mmss()
225 * call (the rtc irq and the timer irq can easily run at the same
226 * time in two different CPUs). So we need to serialize
227 * accesses to the chip with the rtc_lock spinlock that each
228 * architecture should implement in the timer code.
229 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
232 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
235 * Can be an alarm interrupt, update complete interrupt,
236 * or a periodic interrupt. We store the status in the
237 * low byte and the number of interrupts received since
238 * the last read in the remainder of rtc_irq_data.
241 spin_lock (&rtc_lock);
242 rtc_irq_data += 0x100;
243 rtc_irq_data &= ~0xff;
244 if (is_hpet_enabled()) {
246 * In this case it is HPET RTC interrupt handler
247 * calling us, with the interrupt information
248 * passed as arg1, instead of irq.
250 rtc_irq_data |= (unsigned long)irq & 0xF0;
252 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
255 if (rtc_status & RTC_TIMER_ON)
256 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
258 spin_unlock (&rtc_lock);
260 /* Now do the rest of the actions */
261 spin_lock(&rtc_task_lock);
263 rtc_callback->func(rtc_callback->private_data);
264 spin_unlock(&rtc_task_lock);
265 wake_up_interruptible(&rtc_wait);
267 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
274 * sysctl-tuning infrastructure.
276 static ctl_table rtc_table[] = {
279 .procname = "max-user-freq",
280 .data = &rtc_max_user_freq,
281 .maxlen = sizeof(int),
283 .proc_handler = &proc_dointvec,
288 static ctl_table rtc_root[] = {
299 static ctl_table dev_root[] = {
310 static struct ctl_table_header *sysctl_header;
312 static int __init init_sysctl(void)
314 sysctl_header = register_sysctl_table(dev_root, 0);
318 static void __exit cleanup_sysctl(void)
320 unregister_sysctl_table(sysctl_header);
324 * Now all the various file operations that we export.
327 static ssize_t rtc_read(struct file *file, char __user *buf,
328 size_t count, loff_t *ppos)
333 DECLARE_WAITQUEUE(wait, current);
337 if (rtc_has_irq == 0)
340 if (count < sizeof(unsigned))
343 add_wait_queue(&rtc_wait, &wait);
346 /* First make it right. Then make it fast. Putting this whole
347 * block within the parentheses of a while would be too
348 * confusing. And no, xchg() is not the answer. */
350 __set_current_state(TASK_INTERRUPTIBLE);
352 spin_lock_irq (&rtc_lock);
355 spin_unlock_irq (&rtc_lock);
360 if (file->f_flags & O_NONBLOCK) {
364 if (signal_pending(current)) {
365 retval = -ERESTARTSYS;
371 if (count < sizeof(unsigned long))
372 retval = put_user(data, (unsigned int __user *)buf) ?: sizeof(int);
374 retval = put_user(data, (unsigned long __user *)buf) ?: sizeof(long);
376 current->state = TASK_RUNNING;
377 remove_wait_queue(&rtc_wait, &wait);
383 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
385 struct rtc_time wtime;
388 if (rtc_has_irq == 0) {
405 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
407 mask_rtc_irq_bit(RTC_AIE);
410 case RTC_AIE_ON: /* Allow alarm interrupts. */
412 set_rtc_irq_bit(RTC_AIE);
415 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
417 unsigned long flags; /* can be called from isr via rtc_control() */
418 spin_lock_irqsave (&rtc_lock, flags);
419 mask_rtc_irq_bit_locked(RTC_PIE);
420 if (rtc_status & RTC_TIMER_ON) {
421 rtc_status &= ~RTC_TIMER_ON;
422 del_timer(&rtc_irq_timer);
424 spin_unlock_irqrestore (&rtc_lock, flags);
427 case RTC_PIE_ON: /* Allow periodic ints */
429 unsigned long flags; /* can be called from isr via rtc_control() */
431 * We don't really want Joe User enabling more
432 * than 64Hz of interrupts on a multi-user machine.
434 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
435 (!capable(CAP_SYS_RESOURCE)))
438 spin_lock_irqsave (&rtc_lock, flags);
439 if (!(rtc_status & RTC_TIMER_ON)) {
440 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
441 add_timer(&rtc_irq_timer);
442 rtc_status |= RTC_TIMER_ON;
444 set_rtc_irq_bit_locked(RTC_PIE);
445 spin_unlock_irqrestore (&rtc_lock, flags);
448 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
450 mask_rtc_irq_bit(RTC_UIE);
453 case RTC_UIE_ON: /* Allow ints for RTC updates. */
455 set_rtc_irq_bit(RTC_UIE);
459 case RTC_ALM_READ: /* Read the present alarm time */
462 * This returns a struct rtc_time. Reading >= 0xc0
463 * means "don't care" or "match all". Only the tm_hour,
464 * tm_min, and tm_sec values are filled in.
466 memset(&wtime, 0, sizeof(struct rtc_time));
467 get_rtc_alm_time(&wtime);
470 case RTC_ALM_SET: /* Store a time into the alarm */
473 * This expects a struct rtc_time. Writing 0xff means
474 * "don't care" or "match all". Only the tm_hour,
475 * tm_min and tm_sec are used.
477 unsigned char hrs, min, sec;
478 struct rtc_time alm_tm;
480 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
481 sizeof(struct rtc_time)))
484 hrs = alm_tm.tm_hour;
488 spin_lock_irq(&rtc_lock);
489 if (hpet_set_alarm_time(hrs, min, sec)) {
491 * Fallthru and set alarm time in CMOS too,
492 * so that we will get proper value in RTC_ALM_READ
495 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
498 if (sec < 60) BIN_TO_BCD(sec);
501 if (min < 60) BIN_TO_BCD(min);
504 if (hrs < 24) BIN_TO_BCD(hrs);
507 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
508 CMOS_WRITE(min, RTC_MINUTES_ALARM);
509 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
510 spin_unlock_irq(&rtc_lock);
514 case RTC_RD_TIME: /* Read the time/date from RTC */
516 memset(&wtime, 0, sizeof(struct rtc_time));
517 rtc_get_rtc_time(&wtime);
520 case RTC_SET_TIME: /* Set the RTC */
522 struct rtc_time rtc_tm;
523 unsigned char mon, day, hrs, min, sec, leap_yr;
524 unsigned char save_control, save_freq_select;
526 #ifdef CONFIG_MACH_DECSTATION
527 unsigned int real_yrs;
530 if (!capable(CAP_SYS_TIME))
533 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
534 sizeof(struct rtc_time)))
537 yrs = rtc_tm.tm_year + 1900;
538 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
539 day = rtc_tm.tm_mday;
540 hrs = rtc_tm.tm_hour;
547 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
549 if ((mon > 12) || (day == 0))
552 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
555 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
558 if ((yrs -= epoch) > 255) /* They are unsigned */
561 spin_lock_irq(&rtc_lock);
562 #ifdef CONFIG_MACH_DECSTATION
567 * We want to keep the year set to 73 until March
568 * for non-leap years, so that Feb, 29th is handled
571 if (!leap_yr && mon < 3) {
576 /* These limits and adjustments are independent of
577 * whether the chip is in binary mode or not.
580 spin_unlock_irq(&rtc_lock);
586 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
596 save_control = CMOS_READ(RTC_CONTROL);
597 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
598 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
599 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
601 #ifdef CONFIG_MACH_DECSTATION
602 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
604 CMOS_WRITE(yrs, RTC_YEAR);
605 CMOS_WRITE(mon, RTC_MONTH);
606 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
607 CMOS_WRITE(hrs, RTC_HOURS);
608 CMOS_WRITE(min, RTC_MINUTES);
609 CMOS_WRITE(sec, RTC_SECONDS);
611 CMOS_WRITE(save_control, RTC_CONTROL);
612 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
614 spin_unlock_irq(&rtc_lock);
618 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
620 return put_user(rtc_freq, (unsigned long __user *)arg);
622 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
626 unsigned long flags; /* can be called from isr via rtc_control() */
629 * The max we can do is 8192Hz.
631 if ((arg < 2) || (arg > 8192))
634 * We don't really want Joe User generating more
635 * than 64Hz of interrupts on a multi-user machine.
637 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
640 while (arg > (1<<tmp))
644 * Check that the input was really a power of 2.
649 spin_lock_irqsave(&rtc_lock, flags);
650 if (hpet_set_periodic_freq(arg)) {
651 spin_unlock_irqrestore(&rtc_lock, flags);
656 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
658 CMOS_WRITE(val, RTC_FREQ_SELECT);
659 spin_unlock_irqrestore(&rtc_lock, flags);
663 case RTC_EPOCH_READ: /* Read the epoch. */
665 return put_user (epoch, (unsigned long __user *)arg);
667 case RTC_EPOCH_SET: /* Set the epoch. */
670 * There were no RTC clocks before 1900.
675 if (!capable(CAP_SYS_TIME))
684 return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
687 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
690 return rtc_do_ioctl(cmd, arg, 0);
694 * We enforce only one user at a time here with the open/close.
695 * Also clear the previous interrupt data on an open, and clean
696 * up things on a close.
699 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
700 * needed here. Or anywhere else in this driver. */
701 static int rtc_open(struct inode *inode, struct file *file)
703 spin_lock_irq (&rtc_lock);
705 if(rtc_status & RTC_IS_OPEN)
708 rtc_status |= RTC_IS_OPEN;
711 spin_unlock_irq (&rtc_lock);
715 spin_unlock_irq (&rtc_lock);
719 static int rtc_fasync (int fd, struct file *filp, int on)
722 return fasync_helper (fd, filp, on, &rtc_async_queue);
725 static int rtc_release(struct inode *inode, struct file *file)
730 if (rtc_has_irq == 0)
734 * Turn off all interrupts once the device is no longer
735 * in use, and clear the data.
738 spin_lock_irq(&rtc_lock);
739 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
740 tmp = CMOS_READ(RTC_CONTROL);
744 CMOS_WRITE(tmp, RTC_CONTROL);
745 CMOS_READ(RTC_INTR_FLAGS);
747 if (rtc_status & RTC_TIMER_ON) {
748 rtc_status &= ~RTC_TIMER_ON;
749 del_timer(&rtc_irq_timer);
751 spin_unlock_irq(&rtc_lock);
753 if (file->f_flags & FASYNC) {
754 rtc_fasync (-1, file, 0);
759 spin_lock_irq (&rtc_lock);
761 rtc_status &= ~RTC_IS_OPEN;
762 spin_unlock_irq (&rtc_lock);
767 /* Called without the kernel lock - fine */
768 static unsigned int rtc_poll(struct file *file, poll_table *wait)
772 if (rtc_has_irq == 0)
775 poll_wait(file, &rtc_wait, wait);
777 spin_lock_irq (&rtc_lock);
779 spin_unlock_irq (&rtc_lock);
782 return POLLIN | POLLRDNORM;
791 EXPORT_SYMBOL(rtc_register);
792 EXPORT_SYMBOL(rtc_unregister);
793 EXPORT_SYMBOL(rtc_control);
795 int rtc_register(rtc_task_t *task)
800 if (task == NULL || task->func == NULL)
802 spin_lock_irq(&rtc_lock);
803 if (rtc_status & RTC_IS_OPEN) {
804 spin_unlock_irq(&rtc_lock);
807 spin_lock(&rtc_task_lock);
809 spin_unlock(&rtc_task_lock);
810 spin_unlock_irq(&rtc_lock);
813 rtc_status |= RTC_IS_OPEN;
815 spin_unlock(&rtc_task_lock);
816 spin_unlock_irq(&rtc_lock);
821 int rtc_unregister(rtc_task_t *task)
828 spin_lock_irq(&rtc_lock);
829 spin_lock(&rtc_task_lock);
830 if (rtc_callback != task) {
831 spin_unlock(&rtc_task_lock);
832 spin_unlock_irq(&rtc_lock);
837 /* disable controls */
838 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
839 tmp = CMOS_READ(RTC_CONTROL);
843 CMOS_WRITE(tmp, RTC_CONTROL);
844 CMOS_READ(RTC_INTR_FLAGS);
846 if (rtc_status & RTC_TIMER_ON) {
847 rtc_status &= ~RTC_TIMER_ON;
848 del_timer(&rtc_irq_timer);
850 rtc_status &= ~RTC_IS_OPEN;
851 spin_unlock(&rtc_task_lock);
852 spin_unlock_irq(&rtc_lock);
857 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
863 if (cmd != RTC_PIE_ON && cmd != RTC_PIE_OFF && cmd != RTC_IRQP_SET)
865 spin_lock_irqsave(&rtc_task_lock, flags);
866 if (rtc_callback != task) {
867 spin_unlock_irqrestore(&rtc_task_lock, flags);
870 spin_unlock_irqrestore(&rtc_task_lock, flags);
871 return rtc_do_ioctl(cmd, arg, 1);
877 * The various file operations we support.
880 static struct file_operations rtc_fops = {
881 .owner = THIS_MODULE,
889 .release = rtc_release,
890 .fasync = rtc_fasync,
893 static struct miscdevice rtc_dev = {
899 static struct file_operations rtc_proc_fops = {
900 .owner = THIS_MODULE,
901 .open = rtc_proc_open,
904 .release = single_release,
907 #if defined(RTC_IRQ) && !defined(__sparc__)
908 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
911 static int __init rtc_init(void)
913 struct proc_dir_entry *ent;
914 #if defined(__alpha__) || defined(__mips__)
915 unsigned int year, ctrl;
919 struct linux_ebus *ebus;
920 struct linux_ebus_device *edev;
922 struct sparc_isa_bridge *isa_br;
923 struct sparc_isa_device *isa_dev;
928 for_each_ebus(ebus) {
929 for_each_ebusdev(edev, ebus) {
930 if(strcmp(edev->prom_node->name, "rtc") == 0) {
931 rtc_port = edev->resource[0].start;
932 rtc_irq = edev->irqs[0];
938 for_each_isa(isa_br) {
939 for_each_isadev(isa_dev, isa_br) {
940 if (strcmp(isa_dev->prom_node->name, "rtc") == 0) {
941 rtc_port = isa_dev->resource.start;
942 rtc_irq = isa_dev->irq;
948 printk(KERN_ERR "rtc_init: no PC rtc found\n");
952 if (rtc_irq == PCI_IRQ_NONE) {
958 * XXX Interrupt pin #7 in Espresso is shared between RTC and
959 * PCI Slot 2 INTA# (and some INTx# in Slot 1).
961 if (request_irq(rtc_irq, rtc_interrupt, SA_SHIRQ, "rtc", (void *)&rtc_port)) {
962 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
967 if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
968 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
973 if (is_hpet_enabled()) {
974 rtc_int_handler_ptr = hpet_rtc_interrupt;
976 rtc_int_handler_ptr = rtc_interrupt;
979 if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
980 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
981 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
982 release_region(RTC_PORT(0), RTC_IO_EXTENT);
985 hpet_rtc_timer_init();
989 #endif /* __sparc__ vs. others */
991 if (misc_register(&rtc_dev)) {
993 free_irq(RTC_IRQ, NULL);
995 release_region(RTC_PORT(0), RTC_IO_EXTENT);
999 ent = create_proc_entry("driver/rtc", 0, NULL);
1002 free_irq(RTC_IRQ, NULL);
1004 release_region(RTC_PORT(0), RTC_IO_EXTENT);
1005 misc_deregister(&rtc_dev);
1008 ent->proc_fops = &rtc_proc_fops;
1010 #if defined(__alpha__) || defined(__mips__)
1013 /* Each operating system on an Alpha uses its own epoch.
1014 Let's try to guess which one we are using now. */
1016 if (rtc_is_updating() != 0)
1019 spin_lock_irq(&rtc_lock);
1020 year = CMOS_READ(RTC_YEAR);
1021 ctrl = CMOS_READ(RTC_CONTROL);
1022 spin_unlock_irq(&rtc_lock);
1024 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1025 BCD_TO_BIN(year); /* This should never happen... */
1029 guess = "SRM (post-2000)";
1030 } else if (year >= 20 && year < 48) {
1032 guess = "ARC console";
1033 } else if (year >= 48 && year < 72) {
1035 guess = "Digital UNIX";
1036 #if defined(__mips__)
1037 } else if (year >= 72 && year < 74) {
1039 guess = "Digital DECstation";
1041 } else if (year >= 70) {
1043 guess = "Standard PC (1900)";
1047 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1050 if (rtc_has_irq == 0)
1053 init_timer(&rtc_irq_timer);
1054 rtc_irq_timer.function = rtc_dropped_irq;
1055 spin_lock_irq(&rtc_lock);
1057 if (!hpet_set_periodic_freq(rtc_freq)) {
1058 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1059 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1061 spin_unlock_irq(&rtc_lock);
1065 (void) init_sysctl();
1067 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1072 static void __exit rtc_exit (void)
1075 remove_proc_entry ("driver/rtc", NULL);
1076 misc_deregister(&rtc_dev);
1080 free_irq (rtc_irq, &rtc_port);
1082 release_region (RTC_PORT (0), RTC_IO_EXTENT);
1085 free_irq (RTC_IRQ, NULL);
1087 #endif /* __sparc__ */
1090 module_init(rtc_init);
1091 module_exit(rtc_exit);
1095 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1096 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1097 * Since the interrupt handler doesn't get called, the IRQ status
1098 * byte doesn't get read, and the RTC stops generating interrupts.
1099 * A timer is set, and will call this function if/when that happens.
1100 * To get it out of this stalled state, we just read the status.
1101 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1102 * (You *really* shouldn't be trying to use a non-realtime system
1103 * for something that requires a steady > 1KHz signal anyways.)
1106 static void rtc_dropped_irq(unsigned long data)
1110 spin_lock_irq (&rtc_lock);
1112 if (hpet_rtc_dropped_irq()) {
1113 spin_unlock_irq(&rtc_lock);
1117 /* Just in case someone disabled the timer from behind our back... */
1118 if (rtc_status & RTC_TIMER_ON)
1119 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1121 rtc_irq_data += ((rtc_freq/HZ)<<8);
1122 rtc_irq_data &= ~0xff;
1123 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1127 spin_unlock_irq(&rtc_lock);
1129 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1131 /* Now we have new data */
1132 wake_up_interruptible(&rtc_wait);
1134 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1139 * Info exported via "/proc/driver/rtc".
1142 static int rtc_proc_show(struct seq_file *seq, void *v)
1144 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1145 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1147 unsigned char batt, ctrl;
1150 spin_lock_irq(&rtc_lock);
1151 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1152 ctrl = CMOS_READ(RTC_CONTROL);
1154 spin_unlock_irq(&rtc_lock);
1157 rtc_get_rtc_time(&tm);
1160 * There is no way to tell if the luser has the RTC set for local
1161 * time or for Universal Standard Time (GMT). Probably local though.
1164 "rtc_time\t: %02d:%02d:%02d\n"
1165 "rtc_date\t: %04d-%02d-%02d\n"
1166 "rtc_epoch\t: %04lu\n",
1167 tm.tm_hour, tm.tm_min, tm.tm_sec,
1168 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1170 get_rtc_alm_time(&tm);
1173 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1174 * match any value for that particular field. Values that are
1175 * greater than a valid time, but less than 0xc0 shouldn't appear.
1177 seq_puts(seq, "alarm\t\t: ");
1178 if (tm.tm_hour <= 24)
1179 seq_printf(seq, "%02d:", tm.tm_hour);
1181 seq_puts(seq, "**:");
1183 if (tm.tm_min <= 59)
1184 seq_printf(seq, "%02d:", tm.tm_min);
1186 seq_puts(seq, "**:");
1188 if (tm.tm_sec <= 59)
1189 seq_printf(seq, "%02d\n", tm.tm_sec);
1191 seq_puts(seq, "**\n");
1194 "DST_enable\t: %s\n"
1197 "square_wave\t: %s\n"
1199 "update_IRQ\t: %s\n"
1200 "periodic_IRQ\t: %s\n"
1201 "periodic_freq\t: %ld\n"
1202 "batt_status\t: %s\n",
1211 batt ? "okay" : "dead");
1218 static int rtc_proc_open(struct inode *inode, struct file *file)
1220 return single_open(file, rtc_proc_show, NULL);
1223 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1225 unsigned long uip_watchdog = jiffies;
1227 #ifdef CONFIG_MACH_DECSTATION
1228 unsigned int real_year;
1232 * read RTC once any update in progress is done. The update
1233 * can take just over 2ms. We wait 20ms. There is no need to
1234 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1235 * If you need to know *exactly* when a second has started, enable
1236 * periodic update complete interrupts, (via ioctl) and then
1237 * immediately read /dev/rtc which will block until you get the IRQ.
1238 * Once the read clears, read the RTC time (again via ioctl). Easy.
1241 while (rtc_is_updating() != 0 && jiffies - uip_watchdog < 2*HZ/100) {
1247 * Only the values that we read from the RTC are set. We leave
1248 * tm_wday, tm_yday and tm_isdst untouched. Note that while the
1249 * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
1250 * only updated by the RTC when initially set to a non-zero value.
1252 spin_lock_irq(&rtc_lock);
1253 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1254 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1255 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1256 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1257 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1258 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1259 /* Only set from 2.6.16 onwards */
1260 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1262 #ifdef CONFIG_MACH_DECSTATION
1263 real_year = CMOS_READ(RTC_DEC_YEAR);
1265 ctrl = CMOS_READ(RTC_CONTROL);
1266 spin_unlock_irq(&rtc_lock);
1268 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1270 BCD_TO_BIN(rtc_tm->tm_sec);
1271 BCD_TO_BIN(rtc_tm->tm_min);
1272 BCD_TO_BIN(rtc_tm->tm_hour);
1273 BCD_TO_BIN(rtc_tm->tm_mday);
1274 BCD_TO_BIN(rtc_tm->tm_mon);
1275 BCD_TO_BIN(rtc_tm->tm_year);
1276 BCD_TO_BIN(rtc_tm->tm_wday);
1279 #ifdef CONFIG_MACH_DECSTATION
1280 rtc_tm->tm_year += real_year - 72;
1284 * Account for differences between how the RTC uses the values
1285 * and how they are defined in a struct rtc_time;
1287 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1288 rtc_tm->tm_year += 100;
1293 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1298 * Only the values that we read from the RTC are set. That
1299 * means only tm_hour, tm_min, and tm_sec.
1301 spin_lock_irq(&rtc_lock);
1302 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1303 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1304 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1305 ctrl = CMOS_READ(RTC_CONTROL);
1306 spin_unlock_irq(&rtc_lock);
1308 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1310 BCD_TO_BIN(alm_tm->tm_sec);
1311 BCD_TO_BIN(alm_tm->tm_min);
1312 BCD_TO_BIN(alm_tm->tm_hour);
1318 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1319 * Rumour has it that if you frob the interrupt enable/disable
1320 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1321 * ensure you actually start getting interrupts. Probably for
1322 * compatibility with older/broken chipset RTC implementations.
1323 * We also clear out any old irq data after an ioctl() that
1324 * meddles with the interrupt enable/disable bits.
1327 static void mask_rtc_irq_bit_locked(unsigned char bit)
1331 if (hpet_mask_rtc_irq_bit(bit))
1333 val = CMOS_READ(RTC_CONTROL);
1335 CMOS_WRITE(val, RTC_CONTROL);
1336 CMOS_READ(RTC_INTR_FLAGS);
1341 static void set_rtc_irq_bit_locked(unsigned char bit)
1345 if (hpet_set_rtc_irq_bit(bit))
1347 val = CMOS_READ(RTC_CONTROL);
1349 CMOS_WRITE(val, RTC_CONTROL);
1350 CMOS_READ(RTC_INTR_FLAGS);
1356 MODULE_AUTHOR("Paul Gortmaker");
1357 MODULE_LICENSE("GPL");
1358 MODULE_ALIAS_MISCDEV(RTC_MINOR);