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 *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 inline unsigned char rtc_is_updating(void);
152 static int rtc_read_proc(char *page, char **start, off_t off,
153 int count, int *eof, void *data);
156 * Bits in rtc_status. (6 bits of room for future expansion)
159 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
160 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
163 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
164 * protected by the big kernel lock. However, ioctl can still disable the timer
165 * in rtc_status and then with del_timer after the interrupt has read
166 * rtc_status but before mod_timer is called, which would then reenable the
167 * timer (but you would need to have an awful timing before you'd trip on it)
169 static unsigned long rtc_status = 0; /* bitmapped status byte. */
170 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
171 static unsigned long rtc_irq_data = 0; /* our output to the world */
172 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
176 * rtc_task_lock nests inside rtc_lock.
178 static spinlock_t rtc_task_lock = SPIN_LOCK_UNLOCKED;
179 static rtc_task_t *rtc_callback = NULL;
183 * If this driver ever becomes modularised, it will be really nice
184 * to make the epoch retain its value across module reload...
187 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
189 static const unsigned char days_in_mo[] =
190 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
194 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
195 * but there is possibility of conflicting with the set_rtc_mmss()
196 * call (the rtc irq and the timer irq can easily run at the same
197 * time in two different CPUs). So we need to serialize
198 * accesses to the chip with the rtc_lock spinlock that each
199 * architecture should implement in the timer code.
200 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
203 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
206 * Can be an alarm interrupt, update complete interrupt,
207 * or a periodic interrupt. We store the status in the
208 * low byte and the number of interrupts received since
209 * the last read in the remainder of rtc_irq_data.
212 spin_lock (&rtc_lock);
213 rtc_irq_data += 0x100;
214 rtc_irq_data &= ~0xff;
215 if (is_hpet_enabled()) {
217 * In this case it is HPET RTC interrupt handler
218 * calling us, with the interrupt information
219 * passed as arg1, instead of irq.
221 rtc_irq_data |= (unsigned long)irq & 0xF0;
223 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
226 if (rtc_status & RTC_TIMER_ON)
227 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
229 spin_unlock (&rtc_lock);
231 /* Now do the rest of the actions */
232 spin_lock(&rtc_task_lock);
234 rtc_callback->func(rtc_callback->private_data);
235 spin_unlock(&rtc_task_lock);
236 wake_up_interruptible(&rtc_wait);
238 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
245 * sysctl-tuning infrastructure.
247 static ctl_table rtc_table[] = {
250 .procname = "max-user-freq",
251 .data = &rtc_max_user_freq,
252 .maxlen = sizeof(int),
254 .proc_handler = &proc_dointvec,
259 static ctl_table rtc_root[] = {
270 static ctl_table dev_root[] = {
281 static struct ctl_table_header *sysctl_header;
283 static int __init init_sysctl(void)
285 sysctl_header = register_sysctl_table(dev_root, 0);
289 static void __exit cleanup_sysctl(void)
291 unregister_sysctl_table(sysctl_header);
295 * Now all the various file operations that we export.
298 static ssize_t rtc_read(struct file *file, char *buf,
299 size_t count, loff_t *ppos)
304 DECLARE_WAITQUEUE(wait, current);
308 if (rtc_has_irq == 0)
311 if (count < sizeof(unsigned))
314 add_wait_queue(&rtc_wait, &wait);
317 /* First make it right. Then make it fast. Putting this whole
318 * block within the parentheses of a while would be too
319 * confusing. And no, xchg() is not the answer. */
321 __set_current_state(TASK_INTERRUPTIBLE);
323 spin_lock_irq (&rtc_lock);
326 spin_unlock_irq (&rtc_lock);
331 if (file->f_flags & O_NONBLOCK) {
335 if (signal_pending(current)) {
336 retval = -ERESTARTSYS;
342 if (count < sizeof(unsigned long))
343 retval = put_user(data, (unsigned int *)buf) ?: sizeof(int);
345 retval = put_user(data, (unsigned long *)buf) ?: sizeof(long);
347 current->state = TASK_RUNNING;
348 remove_wait_queue(&rtc_wait, &wait);
354 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
356 struct rtc_time wtime;
359 if (rtc_has_irq == 0) {
376 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
378 mask_rtc_irq_bit(RTC_AIE);
381 case RTC_AIE_ON: /* Allow alarm interrupts. */
383 set_rtc_irq_bit(RTC_AIE);
386 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
388 mask_rtc_irq_bit(RTC_PIE);
389 if (rtc_status & RTC_TIMER_ON) {
390 spin_lock_irq (&rtc_lock);
391 rtc_status &= ~RTC_TIMER_ON;
392 del_timer(&rtc_irq_timer);
393 spin_unlock_irq (&rtc_lock);
397 case RTC_PIE_ON: /* Allow periodic ints */
401 * We don't really want Joe User enabling more
402 * than 64Hz of interrupts on a multi-user machine.
404 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
405 (!capable(CAP_SYS_RESOURCE)))
408 if (!(rtc_status & RTC_TIMER_ON)) {
409 spin_lock_irq (&rtc_lock);
410 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
411 add_timer(&rtc_irq_timer);
412 rtc_status |= RTC_TIMER_ON;
413 spin_unlock_irq (&rtc_lock);
415 set_rtc_irq_bit(RTC_PIE);
418 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
420 mask_rtc_irq_bit(RTC_UIE);
423 case RTC_UIE_ON: /* Allow ints for RTC updates. */
425 set_rtc_irq_bit(RTC_UIE);
429 case RTC_ALM_READ: /* Read the present alarm time */
432 * This returns a struct rtc_time. Reading >= 0xc0
433 * means "don't care" or "match all". Only the tm_hour,
434 * tm_min, and tm_sec values are filled in.
436 memset(&wtime, 0, sizeof(struct rtc_time));
437 get_rtc_alm_time(&wtime);
440 case RTC_ALM_SET: /* Store a time into the alarm */
443 * This expects a struct rtc_time. Writing 0xff means
444 * "don't care" or "match all". Only the tm_hour,
445 * tm_min and tm_sec are used.
447 unsigned char hrs, min, sec;
448 struct rtc_time alm_tm;
450 if (copy_from_user(&alm_tm, (struct rtc_time*)arg,
451 sizeof(struct rtc_time)))
454 hrs = alm_tm.tm_hour;
458 spin_lock_irq(&rtc_lock);
459 if (hpet_set_alarm_time(hrs, min, sec)) {
461 * Fallthru and set alarm time in CMOS too,
462 * so that we will get proper value in RTC_ALM_READ
465 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
468 if (sec < 60) BIN_TO_BCD(sec);
471 if (min < 60) BIN_TO_BCD(min);
474 if (hrs < 24) BIN_TO_BCD(hrs);
477 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
478 CMOS_WRITE(min, RTC_MINUTES_ALARM);
479 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
480 spin_unlock_irq(&rtc_lock);
484 case RTC_RD_TIME: /* Read the time/date from RTC */
486 memset(&wtime, 0, sizeof(struct rtc_time));
487 rtc_get_rtc_time(&wtime);
490 case RTC_SET_TIME: /* Set the RTC */
492 struct rtc_time rtc_tm;
493 unsigned char mon, day, hrs, min, sec, leap_yr;
494 unsigned char save_control, save_freq_select;
496 #ifdef CONFIG_DECSTATION
497 unsigned int real_yrs;
500 if (!capable(CAP_SYS_TIME))
503 if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
504 sizeof(struct rtc_time)))
507 yrs = rtc_tm.tm_year + 1900;
508 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
509 day = rtc_tm.tm_mday;
510 hrs = rtc_tm.tm_hour;
517 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
519 if ((mon > 12) || (day == 0))
522 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
525 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
528 if ((yrs -= epoch) > 255) /* They are unsigned */
531 spin_lock_irq(&rtc_lock);
532 #ifdef CONFIG_DECSTATION
537 * We want to keep the year set to 73 until March
538 * for non-leap years, so that Feb, 29th is handled
541 if (!leap_yr && mon < 3) {
546 /* These limits and adjustments are independent of
547 * whether the chip is in binary mode or not.
550 spin_unlock_irq(&rtc_lock);
556 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
566 save_control = CMOS_READ(RTC_CONTROL);
567 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
568 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
569 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
571 #ifdef CONFIG_DECSTATION
572 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
574 CMOS_WRITE(yrs, RTC_YEAR);
575 CMOS_WRITE(mon, RTC_MONTH);
576 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
577 CMOS_WRITE(hrs, RTC_HOURS);
578 CMOS_WRITE(min, RTC_MINUTES);
579 CMOS_WRITE(sec, RTC_SECONDS);
581 CMOS_WRITE(save_control, RTC_CONTROL);
582 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
584 spin_unlock_irq(&rtc_lock);
588 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
590 return put_user(rtc_freq, (unsigned long *)arg);
592 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
598 * The max we can do is 8192Hz.
600 if ((arg < 2) || (arg > 8192))
603 * We don't really want Joe User generating more
604 * than 64Hz of interrupts on a multi-user machine.
606 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
609 while (arg > (1<<tmp))
613 * Check that the input was really a power of 2.
618 spin_lock_irq(&rtc_lock);
619 if (hpet_set_periodic_freq(arg)) {
620 spin_unlock_irq(&rtc_lock);
625 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
627 CMOS_WRITE(val, RTC_FREQ_SELECT);
628 spin_unlock_irq(&rtc_lock);
632 case RTC_EPOCH_READ: /* Read the epoch. */
634 return put_user (epoch, (unsigned long *)arg);
636 case RTC_EPOCH_SET: /* Set the epoch. */
639 * There were no RTC clocks before 1900.
644 if (!capable(CAP_SYS_TIME))
653 return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
656 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
659 return rtc_do_ioctl(cmd, arg, 0);
663 * We enforce only one user at a time here with the open/close.
664 * Also clear the previous interrupt data on an open, and clean
665 * up things on a close.
668 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
669 * needed here. Or anywhere else in this driver. */
670 static int rtc_open(struct inode *inode, struct file *file)
672 spin_lock_irq (&rtc_lock);
674 if(rtc_status & RTC_IS_OPEN)
677 rtc_status |= RTC_IS_OPEN;
680 spin_unlock_irq (&rtc_lock);
684 spin_unlock_irq (&rtc_lock);
688 static int rtc_fasync (int fd, struct file *filp, int on)
691 return fasync_helper (fd, filp, on, &rtc_async_queue);
694 static int rtc_release(struct inode *inode, struct file *file)
699 if (rtc_has_irq == 0)
703 * Turn off all interrupts once the device is no longer
704 * in use, and clear the data.
707 spin_lock_irq(&rtc_lock);
708 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
709 tmp = CMOS_READ(RTC_CONTROL);
713 CMOS_WRITE(tmp, RTC_CONTROL);
714 CMOS_READ(RTC_INTR_FLAGS);
716 if (rtc_status & RTC_TIMER_ON) {
717 rtc_status &= ~RTC_TIMER_ON;
718 del_timer(&rtc_irq_timer);
720 spin_unlock_irq(&rtc_lock);
722 if (file->f_flags & FASYNC) {
723 rtc_fasync (-1, file, 0);
728 spin_lock_irq (&rtc_lock);
730 rtc_status &= ~RTC_IS_OPEN;
731 spin_unlock_irq (&rtc_lock);
736 /* Called without the kernel lock - fine */
737 static unsigned int rtc_poll(struct file *file, poll_table *wait)
741 if (rtc_has_irq == 0)
744 poll_wait(file, &rtc_wait, wait);
746 spin_lock_irq (&rtc_lock);
748 spin_unlock_irq (&rtc_lock);
751 return POLLIN | POLLRDNORM;
760 EXPORT_SYMBOL(rtc_register);
761 EXPORT_SYMBOL(rtc_unregister);
762 EXPORT_SYMBOL(rtc_control);
764 int rtc_register(rtc_task_t *task)
769 if (task == NULL || task->func == NULL)
771 spin_lock_irq(&rtc_lock);
772 if (rtc_status & RTC_IS_OPEN) {
773 spin_unlock_irq(&rtc_lock);
776 spin_lock(&rtc_task_lock);
778 spin_unlock(&rtc_task_lock);
779 spin_unlock_irq(&rtc_lock);
782 rtc_status |= RTC_IS_OPEN;
784 spin_unlock(&rtc_task_lock);
785 spin_unlock_irq(&rtc_lock);
790 int rtc_unregister(rtc_task_t *task)
797 spin_lock_irq(&rtc_lock);
798 spin_lock(&rtc_task_lock);
799 if (rtc_callback != task) {
800 spin_unlock(&rtc_task_lock);
801 spin_unlock_irq(&rtc_lock);
806 /* disable controls */
807 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
808 tmp = CMOS_READ(RTC_CONTROL);
812 CMOS_WRITE(tmp, RTC_CONTROL);
813 CMOS_READ(RTC_INTR_FLAGS);
815 if (rtc_status & RTC_TIMER_ON) {
816 rtc_status &= ~RTC_TIMER_ON;
817 del_timer(&rtc_irq_timer);
819 rtc_status &= ~RTC_IS_OPEN;
820 spin_unlock(&rtc_task_lock);
821 spin_unlock_irq(&rtc_lock);
826 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
831 spin_lock_irq(&rtc_task_lock);
832 if (rtc_callback != task) {
833 spin_unlock_irq(&rtc_task_lock);
836 spin_unlock_irq(&rtc_task_lock);
837 return rtc_do_ioctl(cmd, arg, 1);
843 * The various file operations we support.
846 static struct file_operations rtc_fops = {
847 .owner = THIS_MODULE,
855 .release = rtc_release,
856 .fasync = rtc_fasync,
859 static struct miscdevice rtc_dev=
867 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
870 static int __init rtc_init(void)
872 #if defined(__alpha__) || defined(__mips__)
873 unsigned int year, ctrl;
874 unsigned long uip_watchdog;
878 struct linux_ebus *ebus;
879 struct linux_ebus_device *edev;
881 struct sparc_isa_bridge *isa_br;
882 struct sparc_isa_device *isa_dev;
887 for_each_ebus(ebus) {
888 for_each_ebusdev(edev, ebus) {
889 if(strcmp(edev->prom_name, "rtc") == 0) {
890 rtc_port = edev->resource[0].start;
891 rtc_irq = edev->irqs[0];
897 for_each_isa(isa_br) {
898 for_each_isadev(isa_dev, isa_br) {
899 if (strcmp(isa_dev->prom_name, "rtc") == 0) {
900 rtc_port = isa_dev->resource.start;
901 rtc_irq = isa_dev->irq;
907 printk(KERN_ERR "rtc_init: no PC rtc found\n");
911 if (rtc_irq == PCI_IRQ_NONE) {
917 * XXX Interrupt pin #7 in Espresso is shared between RTC and
918 * PCI Slot 2 INTA# (and some INTx# in Slot 1). SA_INTERRUPT here
919 * is asking for trouble with add-on boards. Change to SA_SHIRQ.
921 if (request_irq(rtc_irq, rtc_interrupt, SA_INTERRUPT, "rtc", (void *)&rtc_port)) {
923 * Standard way for sparc to print irq's is to use
924 * __irq_itoa(). I think for EBus it's ok to use %d.
926 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
931 if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
932 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
937 if (is_hpet_enabled()) {
938 rtc_int_handler_ptr = hpet_rtc_interrupt;
940 rtc_int_handler_ptr = rtc_interrupt;
943 if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
944 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
945 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
946 release_region(RTC_PORT(0), RTC_IO_EXTENT);
949 hpet_rtc_timer_init();
953 #endif /* __sparc__ vs. others */
955 if (misc_register(&rtc_dev)) {
957 free_irq(RTC_IRQ, NULL);
959 release_region(RTC_PORT(0), RTC_IO_EXTENT);
962 if (create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL) == NULL) {
964 free_irq(RTC_IRQ, NULL);
966 release_region(RTC_PORT(0), RTC_IO_EXTENT);
967 misc_deregister(&rtc_dev);
971 #if defined(__alpha__) || defined(__mips__)
974 /* Each operating system on an Alpha uses its own epoch.
975 Let's try to guess which one we are using now. */
977 uip_watchdog = jiffies;
978 if (rtc_is_updating() != 0)
979 while (jiffies - uip_watchdog < 2*HZ/100) {
984 spin_lock_irq(&rtc_lock);
985 year = CMOS_READ(RTC_YEAR);
986 ctrl = CMOS_READ(RTC_CONTROL);
987 spin_unlock_irq(&rtc_lock);
989 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
990 BCD_TO_BIN(year); /* This should never happen... */
994 guess = "SRM (post-2000)";
995 } else if (year >= 20 && year < 48) {
997 guess = "ARC console";
998 } else if (year >= 48 && year < 72) {
1000 guess = "Digital UNIX";
1001 #if defined(__mips__)
1002 } else if (year >= 72 && year < 74) {
1004 guess = "Digital DECstation";
1006 } else if (year >= 70) {
1008 guess = "Standard PC (1900)";
1012 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1015 if (rtc_has_irq == 0)
1018 init_timer(&rtc_irq_timer);
1019 rtc_irq_timer.function = rtc_dropped_irq;
1020 spin_lock_irq(&rtc_lock);
1022 if (!hpet_set_periodic_freq(rtc_freq)) {
1023 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1024 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1026 spin_unlock_irq(&rtc_lock);
1030 (void) init_sysctl();
1032 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1037 static void __exit rtc_exit (void)
1040 remove_proc_entry ("driver/rtc", NULL);
1041 misc_deregister(&rtc_dev);
1045 free_irq (rtc_irq, &rtc_port);
1047 release_region (RTC_PORT (0), RTC_IO_EXTENT);
1050 free_irq (RTC_IRQ, NULL);
1052 #endif /* __sparc__ */
1055 module_init(rtc_init);
1056 module_exit(rtc_exit);
1060 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1061 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1062 * Since the interrupt handler doesn't get called, the IRQ status
1063 * byte doesn't get read, and the RTC stops generating interrupts.
1064 * A timer is set, and will call this function if/when that happens.
1065 * To get it out of this stalled state, we just read the status.
1066 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1067 * (You *really* shouldn't be trying to use a non-realtime system
1068 * for something that requires a steady > 1KHz signal anyways.)
1071 static void rtc_dropped_irq(unsigned long data)
1075 spin_lock_irq (&rtc_lock);
1077 if (hpet_rtc_dropped_irq()) {
1078 spin_unlock_irq(&rtc_lock);
1082 /* Just in case someone disabled the timer from behind our back... */
1083 if (rtc_status & RTC_TIMER_ON)
1084 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1086 rtc_irq_data += ((rtc_freq/HZ)<<8);
1087 rtc_irq_data &= ~0xff;
1088 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1092 spin_unlock_irq(&rtc_lock);
1094 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1096 /* Now we have new data */
1097 wake_up_interruptible(&rtc_wait);
1099 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1104 * Info exported via "/proc/driver/rtc".
1107 static int rtc_proc_output (char *buf)
1109 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1110 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1113 unsigned char batt, ctrl;
1116 spin_lock_irq(&rtc_lock);
1117 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1118 ctrl = CMOS_READ(RTC_CONTROL);
1120 spin_unlock_irq(&rtc_lock);
1124 rtc_get_rtc_time(&tm);
1127 * There is no way to tell if the luser has the RTC set for local
1128 * time or for Universal Standard Time (GMT). Probably local though.
1131 "rtc_time\t: %02d:%02d:%02d\n"
1132 "rtc_date\t: %04d-%02d-%02d\n"
1133 "rtc_epoch\t: %04lu\n",
1134 tm.tm_hour, tm.tm_min, tm.tm_sec,
1135 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1137 get_rtc_alm_time(&tm);
1140 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1141 * match any value for that particular field. Values that are
1142 * greater than a valid time, but less than 0xc0 shouldn't appear.
1144 p += sprintf(p, "alarm\t\t: ");
1145 if (tm.tm_hour <= 24)
1146 p += sprintf(p, "%02d:", tm.tm_hour);
1148 p += sprintf(p, "**:");
1150 if (tm.tm_min <= 59)
1151 p += sprintf(p, "%02d:", tm.tm_min);
1153 p += sprintf(p, "**:");
1155 if (tm.tm_sec <= 59)
1156 p += sprintf(p, "%02d\n", tm.tm_sec);
1158 p += sprintf(p, "**\n");
1161 "DST_enable\t: %s\n"
1164 "square_wave\t: %s\n"
1166 "update_IRQ\t: %s\n"
1167 "periodic_IRQ\t: %s\n"
1168 "periodic_freq\t: %ld\n"
1169 "batt_status\t: %s\n",
1178 batt ? "okay" : "dead");
1185 static int rtc_read_proc(char *page, char **start, off_t off,
1186 int count, int *eof, void *data)
1188 int len = rtc_proc_output (page);
1189 if (len <= off+count) *eof = 1;
1190 *start = page + off;
1192 if (len>count) len = count;
1198 * Returns true if a clock update is in progress
1200 /* FIXME shouldn't this be above rtc_init to make it fully inlined? */
1201 static inline unsigned char rtc_is_updating(void)
1205 spin_lock_irq(&rtc_lock);
1206 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
1207 spin_unlock_irq(&rtc_lock);
1211 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1213 unsigned long uip_watchdog = jiffies;
1215 #ifdef CONFIG_DECSTATION
1216 unsigned int real_year;
1220 * read RTC once any update in progress is done. The update
1221 * can take just over 2ms. We wait 10 to 20ms. There is no need to
1222 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1223 * If you need to know *exactly* when a second has started, enable
1224 * periodic update complete interrupts, (via ioctl) and then
1225 * immediately read /dev/rtc which will block until you get the IRQ.
1226 * Once the read clears, read the RTC time (again via ioctl). Easy.
1229 if (rtc_is_updating() != 0)
1230 while (jiffies - uip_watchdog < 2*HZ/100) {
1236 * Only the values that we read from the RTC are set. We leave
1237 * tm_wday, tm_yday and tm_isdst untouched. Even though the
1238 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
1239 * by the RTC when initially set to a non-zero value.
1241 spin_lock_irq(&rtc_lock);
1242 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1243 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1244 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1245 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1246 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1247 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1248 #ifdef CONFIG_DECSTATION
1249 real_year = CMOS_READ(RTC_DEC_YEAR);
1251 ctrl = CMOS_READ(RTC_CONTROL);
1252 spin_unlock_irq(&rtc_lock);
1254 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1256 BCD_TO_BIN(rtc_tm->tm_sec);
1257 BCD_TO_BIN(rtc_tm->tm_min);
1258 BCD_TO_BIN(rtc_tm->tm_hour);
1259 BCD_TO_BIN(rtc_tm->tm_mday);
1260 BCD_TO_BIN(rtc_tm->tm_mon);
1261 BCD_TO_BIN(rtc_tm->tm_year);
1264 #ifdef CONFIG_DECSTATION
1265 rtc_tm->tm_year += real_year - 72;
1269 * Account for differences between how the RTC uses the values
1270 * and how they are defined in a struct rtc_time;
1272 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1273 rtc_tm->tm_year += 100;
1278 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1283 * Only the values that we read from the RTC are set. That
1284 * means only tm_hour, tm_min, and tm_sec.
1286 spin_lock_irq(&rtc_lock);
1287 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1288 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1289 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1290 ctrl = CMOS_READ(RTC_CONTROL);
1291 spin_unlock_irq(&rtc_lock);
1293 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1295 BCD_TO_BIN(alm_tm->tm_sec);
1296 BCD_TO_BIN(alm_tm->tm_min);
1297 BCD_TO_BIN(alm_tm->tm_hour);
1303 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1304 * Rumour has it that if you frob the interrupt enable/disable
1305 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1306 * ensure you actually start getting interrupts. Probably for
1307 * compatibility with older/broken chipset RTC implementations.
1308 * We also clear out any old irq data after an ioctl() that
1309 * meddles with the interrupt enable/disable bits.
1312 static void mask_rtc_irq_bit(unsigned char bit)
1316 spin_lock_irq(&rtc_lock);
1317 if (hpet_mask_rtc_irq_bit(bit)) {
1318 spin_unlock_irq(&rtc_lock);
1321 val = CMOS_READ(RTC_CONTROL);
1323 CMOS_WRITE(val, RTC_CONTROL);
1324 CMOS_READ(RTC_INTR_FLAGS);
1327 spin_unlock_irq(&rtc_lock);
1330 static void set_rtc_irq_bit(unsigned char bit)
1334 spin_lock_irq(&rtc_lock);
1335 if (hpet_set_rtc_irq_bit(bit)) {
1336 spin_unlock_irq(&rtc_lock);
1339 val = CMOS_READ(RTC_CONTROL);
1341 CMOS_WRITE(val, RTC_CONTROL);
1342 CMOS_READ(RTC_INTR_FLAGS);
1345 spin_unlock_irq(&rtc_lock);
1349 MODULE_AUTHOR("Paul Gortmaker");
1350 MODULE_LICENSE("GPL");
1351 MODULE_ALIAS_MISCDEV(RTC_MINOR);