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/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/seq_file.h>
77 #include <linux/spinlock.h>
78 #include <linux/sysctl.h>
79 #include <linux/wait.h>
80 #include <linux/bcd.h>
81 #include <linux/delay.h>
83 #include <asm/current.h>
84 #include <asm/uaccess.h>
85 #include <asm/system.h>
92 #include <linux/pci.h>
98 static unsigned long rtc_port;
99 static int rtc_irq = PCI_IRQ_NONE;
102 #ifdef CONFIG_HPET_RTC_IRQ
107 static int rtc_has_irq = 1;
110 #ifndef CONFIG_HPET_EMULATE_RTC
111 #define is_hpet_enabled() 0
112 #define hpet_set_alarm_time(hrs, min, sec) 0
113 #define hpet_set_periodic_freq(arg) 0
114 #define hpet_mask_rtc_irq_bit(arg) 0
115 #define hpet_set_rtc_irq_bit(arg) 0
116 #define hpet_rtc_timer_init() do { } while (0)
117 #define hpet_rtc_dropped_irq() 0
118 static inline irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs) {return 0;}
120 extern irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
124 * We sponge a minor off of the misc major. No need slurping
125 * up another valuable major dev number for this. If you add
126 * an ioctl, make sure you don't conflict with SPARC's RTC
130 static struct fasync_struct *rtc_async_queue;
132 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
135 static struct timer_list rtc_irq_timer;
138 static ssize_t rtc_read(struct file *file, char __user *buf,
139 size_t count, loff_t *ppos);
141 static int rtc_ioctl(struct inode *inode, struct file *file,
142 unsigned int cmd, unsigned long arg);
145 static unsigned int rtc_poll(struct file *file, poll_table *wait);
148 static void get_rtc_alm_time (struct rtc_time *alm_tm);
150 static void rtc_dropped_irq(unsigned long data);
152 static void set_rtc_irq_bit_locked(unsigned char bit);
153 static void mask_rtc_irq_bit_locked(unsigned char bit);
155 static inline void set_rtc_irq_bit(unsigned char bit)
157 spin_lock_irq(&rtc_lock);
158 set_rtc_irq_bit_locked(bit);
159 spin_unlock_irq(&rtc_lock);
162 static void mask_rtc_irq_bit(unsigned char bit)
164 spin_lock_irq(&rtc_lock);
165 mask_rtc_irq_bit_locked(bit);
166 spin_unlock_irq(&rtc_lock);
170 static int rtc_proc_open(struct inode *inode, struct file *file);
173 * Bits in rtc_status. (6 bits of room for future expansion)
176 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
177 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
180 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
181 * protected by the big kernel lock. However, ioctl can still disable the timer
182 * in rtc_status and then with del_timer after the interrupt has read
183 * rtc_status but before mod_timer is called, which would then reenable the
184 * timer (but you would need to have an awful timing before you'd trip on it)
186 static unsigned long rtc_status = 0; /* bitmapped status byte. */
187 static unsigned long rtc_freq = 0; /* Current periodic IRQ rate */
188 static unsigned long rtc_irq_data = 0; /* our output to the world */
189 static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */
193 * rtc_task_lock nests inside rtc_lock.
195 static DEFINE_SPINLOCK(rtc_task_lock);
196 static rtc_task_t *rtc_callback = NULL;
200 * If this driver ever becomes modularised, it will be really nice
201 * to make the epoch retain its value across module reload...
204 static unsigned long epoch = 1900; /* year corresponding to 0x00 */
206 static const unsigned char days_in_mo[] =
207 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
210 * Returns true if a clock update is in progress
212 static inline unsigned char rtc_is_updating(void)
217 spin_lock_irqsave(&rtc_lock, flags);
218 uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
219 spin_unlock_irqrestore(&rtc_lock, flags);
225 * A very tiny interrupt handler. It runs with SA_INTERRUPT set,
226 * but there is possibility of conflicting with the set_rtc_mmss()
227 * call (the rtc irq and the timer irq can easily run at the same
228 * time in two different CPUs). So we need to serialize
229 * accesses to the chip with the rtc_lock spinlock that each
230 * architecture should implement in the timer code.
231 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
234 irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
237 * Can be an alarm interrupt, update complete interrupt,
238 * or a periodic interrupt. We store the status in the
239 * low byte and the number of interrupts received since
240 * the last read in the remainder of rtc_irq_data.
243 spin_lock (&rtc_lock);
244 rtc_irq_data += 0x100;
245 rtc_irq_data &= ~0xff;
246 if (is_hpet_enabled()) {
248 * In this case it is HPET RTC interrupt handler
249 * calling us, with the interrupt information
250 * passed as arg1, instead of irq.
252 rtc_irq_data |= (unsigned long)irq & 0xF0;
254 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);
257 if (rtc_status & RTC_TIMER_ON)
258 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
260 spin_unlock (&rtc_lock);
262 /* Now do the rest of the actions */
263 spin_lock(&rtc_task_lock);
265 rtc_callback->func(rtc_callback->private_data);
266 spin_unlock(&rtc_task_lock);
267 wake_up_interruptible(&rtc_wait);
269 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
276 * sysctl-tuning infrastructure.
278 static ctl_table rtc_table[] = {
281 .procname = "max-user-freq",
282 .data = &rtc_max_user_freq,
283 .maxlen = sizeof(int),
285 .proc_handler = &proc_dointvec,
290 static ctl_table rtc_root[] = {
301 static ctl_table dev_root[] = {
312 static struct ctl_table_header *sysctl_header;
314 static int __init init_sysctl(void)
316 sysctl_header = register_sysctl_table(dev_root, 0);
320 static void __exit cleanup_sysctl(void)
322 unregister_sysctl_table(sysctl_header);
326 * Now all the various file operations that we export.
329 static ssize_t rtc_read(struct file *file, char __user *buf,
330 size_t count, loff_t *ppos)
335 DECLARE_WAITQUEUE(wait, current);
339 if (rtc_has_irq == 0)
342 if (count < sizeof(unsigned))
345 add_wait_queue(&rtc_wait, &wait);
348 /* First make it right. Then make it fast. Putting this whole
349 * block within the parentheses of a while would be too
350 * confusing. And no, xchg() is not the answer. */
352 __set_current_state(TASK_INTERRUPTIBLE);
354 spin_lock_irq (&rtc_lock);
357 spin_unlock_irq (&rtc_lock);
362 if (file->f_flags & O_NONBLOCK) {
366 if (signal_pending(current)) {
367 retval = -ERESTARTSYS;
373 if (count < sizeof(unsigned long))
374 retval = put_user(data, (unsigned int __user *)buf) ?: sizeof(int);
376 retval = put_user(data, (unsigned long __user *)buf) ?: sizeof(long);
378 current->state = TASK_RUNNING;
379 remove_wait_queue(&rtc_wait, &wait);
385 static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel)
387 struct rtc_time wtime;
390 if (rtc_has_irq == 0) {
407 case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
409 mask_rtc_irq_bit(RTC_AIE);
412 case RTC_AIE_ON: /* Allow alarm interrupts. */
414 set_rtc_irq_bit(RTC_AIE);
417 case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
419 unsigned long flags; /* can be called from isr via rtc_control() */
420 spin_lock_irqsave (&rtc_lock, flags);
421 mask_rtc_irq_bit_locked(RTC_PIE);
422 if (rtc_status & RTC_TIMER_ON) {
423 rtc_status &= ~RTC_TIMER_ON;
424 del_timer(&rtc_irq_timer);
426 spin_unlock_irqrestore (&rtc_lock, flags);
429 case RTC_PIE_ON: /* Allow periodic ints */
431 unsigned long flags; /* can be called from isr via rtc_control() */
433 * We don't really want Joe User enabling more
434 * than 64Hz of interrupts on a multi-user machine.
436 if (!kernel && (rtc_freq > rtc_max_user_freq) &&
437 (!capable(CAP_SYS_RESOURCE)))
440 spin_lock_irqsave (&rtc_lock, flags);
441 if (!(rtc_status & RTC_TIMER_ON)) {
442 rtc_irq_timer.expires = jiffies + HZ/rtc_freq + 2*HZ/100;
443 add_timer(&rtc_irq_timer);
444 rtc_status |= RTC_TIMER_ON;
446 set_rtc_irq_bit_locked(RTC_PIE);
447 spin_unlock_irqrestore (&rtc_lock, flags);
450 case RTC_UIE_OFF: /* Mask ints from RTC updates. */
452 mask_rtc_irq_bit(RTC_UIE);
455 case RTC_UIE_ON: /* Allow ints for RTC updates. */
457 set_rtc_irq_bit(RTC_UIE);
461 case RTC_ALM_READ: /* Read the present alarm time */
464 * This returns a struct rtc_time. Reading >= 0xc0
465 * means "don't care" or "match all". Only the tm_hour,
466 * tm_min, and tm_sec values are filled in.
468 memset(&wtime, 0, sizeof(struct rtc_time));
469 get_rtc_alm_time(&wtime);
472 case RTC_ALM_SET: /* Store a time into the alarm */
475 * This expects a struct rtc_time. Writing 0xff means
476 * "don't care" or "match all". Only the tm_hour,
477 * tm_min and tm_sec are used.
479 unsigned char hrs, min, sec;
480 struct rtc_time alm_tm;
482 if (copy_from_user(&alm_tm, (struct rtc_time __user *)arg,
483 sizeof(struct rtc_time)))
486 hrs = alm_tm.tm_hour;
490 spin_lock_irq(&rtc_lock);
491 if (hpet_set_alarm_time(hrs, min, sec)) {
493 * Fallthru and set alarm time in CMOS too,
494 * so that we will get proper value in RTC_ALM_READ
497 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) ||
500 if (sec < 60) BIN_TO_BCD(sec);
503 if (min < 60) BIN_TO_BCD(min);
506 if (hrs < 24) BIN_TO_BCD(hrs);
509 CMOS_WRITE(hrs, RTC_HOURS_ALARM);
510 CMOS_WRITE(min, RTC_MINUTES_ALARM);
511 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
512 spin_unlock_irq(&rtc_lock);
516 case RTC_RD_TIME: /* Read the time/date from RTC */
518 memset(&wtime, 0, sizeof(struct rtc_time));
519 rtc_get_rtc_time(&wtime);
522 case RTC_SET_TIME: /* Set the RTC */
524 struct rtc_time rtc_tm;
525 unsigned char mon, day, hrs, min, sec, leap_yr;
526 unsigned char save_control, save_freq_select;
528 #ifdef CONFIG_MACH_DECSTATION
529 unsigned int real_yrs;
532 if (!capable(CAP_SYS_TIME))
535 if (copy_from_user(&rtc_tm, (struct rtc_time __user *)arg,
536 sizeof(struct rtc_time)))
539 yrs = rtc_tm.tm_year + 1900;
540 mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
541 day = rtc_tm.tm_mday;
542 hrs = rtc_tm.tm_hour;
549 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
551 if ((mon > 12) || (day == 0))
554 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
557 if ((hrs >= 24) || (min >= 60) || (sec >= 60))
560 if ((yrs -= epoch) > 255) /* They are unsigned */
563 spin_lock_irq(&rtc_lock);
564 #ifdef CONFIG_MACH_DECSTATION
569 * We want to keep the year set to 73 until March
570 * for non-leap years, so that Feb, 29th is handled
573 if (!leap_yr && mon < 3) {
578 /* These limits and adjustments are independent of
579 * whether the chip is in binary mode or not.
582 spin_unlock_irq(&rtc_lock);
588 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
598 save_control = CMOS_READ(RTC_CONTROL);
599 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
600 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
601 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
603 #ifdef CONFIG_MACH_DECSTATION
604 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
606 CMOS_WRITE(yrs, RTC_YEAR);
607 CMOS_WRITE(mon, RTC_MONTH);
608 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
609 CMOS_WRITE(hrs, RTC_HOURS);
610 CMOS_WRITE(min, RTC_MINUTES);
611 CMOS_WRITE(sec, RTC_SECONDS);
613 CMOS_WRITE(save_control, RTC_CONTROL);
614 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
616 spin_unlock_irq(&rtc_lock);
620 case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
622 return put_user(rtc_freq, (unsigned long __user *)arg);
624 case RTC_IRQP_SET: /* Set periodic IRQ rate. */
628 unsigned long flags; /* can be called from isr via rtc_control() */
631 * The max we can do is 8192Hz.
633 if ((arg < 2) || (arg > 8192))
636 * We don't really want Joe User generating more
637 * than 64Hz of interrupts on a multi-user machine.
639 if (!kernel && (arg > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
642 while (arg > (1<<tmp))
646 * Check that the input was really a power of 2.
651 spin_lock_irqsave(&rtc_lock, flags);
652 if (hpet_set_periodic_freq(arg)) {
653 spin_unlock_irqrestore(&rtc_lock, flags);
658 val = CMOS_READ(RTC_FREQ_SELECT) & 0xf0;
660 CMOS_WRITE(val, RTC_FREQ_SELECT);
661 spin_unlock_irqrestore(&rtc_lock, flags);
665 case RTC_EPOCH_READ: /* Read the epoch. */
667 return put_user (epoch, (unsigned long __user *)arg);
669 case RTC_EPOCH_SET: /* Set the epoch. */
672 * There were no RTC clocks before 1900.
677 if (!capable(CAP_SYS_TIME))
686 return copy_to_user((void __user *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
689 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
692 return rtc_do_ioctl(cmd, arg, 0);
696 * We enforce only one user at a time here with the open/close.
697 * Also clear the previous interrupt data on an open, and clean
698 * up things on a close.
701 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
702 * needed here. Or anywhere else in this driver. */
703 static int rtc_open(struct inode *inode, struct file *file)
705 spin_lock_irq (&rtc_lock);
707 if(rtc_status & RTC_IS_OPEN)
710 rtc_status |= RTC_IS_OPEN;
713 spin_unlock_irq (&rtc_lock);
717 spin_unlock_irq (&rtc_lock);
721 static int rtc_fasync (int fd, struct file *filp, int on)
724 return fasync_helper (fd, filp, on, &rtc_async_queue);
727 static int rtc_release(struct inode *inode, struct file *file)
732 if (rtc_has_irq == 0)
736 * Turn off all interrupts once the device is no longer
737 * in use, and clear the data.
740 spin_lock_irq(&rtc_lock);
741 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
742 tmp = CMOS_READ(RTC_CONTROL);
746 CMOS_WRITE(tmp, RTC_CONTROL);
747 CMOS_READ(RTC_INTR_FLAGS);
749 if (rtc_status & RTC_TIMER_ON) {
750 rtc_status &= ~RTC_TIMER_ON;
751 del_timer(&rtc_irq_timer);
753 spin_unlock_irq(&rtc_lock);
755 if (file->f_flags & FASYNC) {
756 rtc_fasync (-1, file, 0);
761 spin_lock_irq (&rtc_lock);
763 rtc_status &= ~RTC_IS_OPEN;
764 spin_unlock_irq (&rtc_lock);
769 /* Called without the kernel lock - fine */
770 static unsigned int rtc_poll(struct file *file, poll_table *wait)
774 if (rtc_has_irq == 0)
777 poll_wait(file, &rtc_wait, wait);
779 spin_lock_irq (&rtc_lock);
781 spin_unlock_irq (&rtc_lock);
784 return POLLIN | POLLRDNORM;
793 EXPORT_SYMBOL(rtc_register);
794 EXPORT_SYMBOL(rtc_unregister);
795 EXPORT_SYMBOL(rtc_control);
797 int rtc_register(rtc_task_t *task)
802 if (task == NULL || task->func == NULL)
804 spin_lock_irq(&rtc_lock);
805 if (rtc_status & RTC_IS_OPEN) {
806 spin_unlock_irq(&rtc_lock);
809 spin_lock(&rtc_task_lock);
811 spin_unlock(&rtc_task_lock);
812 spin_unlock_irq(&rtc_lock);
815 rtc_status |= RTC_IS_OPEN;
817 spin_unlock(&rtc_task_lock);
818 spin_unlock_irq(&rtc_lock);
823 int rtc_unregister(rtc_task_t *task)
830 spin_lock_irq(&rtc_lock);
831 spin_lock(&rtc_task_lock);
832 if (rtc_callback != task) {
833 spin_unlock(&rtc_task_lock);
834 spin_unlock_irq(&rtc_lock);
839 /* disable controls */
840 if (!hpet_mask_rtc_irq_bit(RTC_PIE | RTC_AIE | RTC_UIE)) {
841 tmp = CMOS_READ(RTC_CONTROL);
845 CMOS_WRITE(tmp, RTC_CONTROL);
846 CMOS_READ(RTC_INTR_FLAGS);
848 if (rtc_status & RTC_TIMER_ON) {
849 rtc_status &= ~RTC_TIMER_ON;
850 del_timer(&rtc_irq_timer);
852 rtc_status &= ~RTC_IS_OPEN;
853 spin_unlock(&rtc_task_lock);
854 spin_unlock_irq(&rtc_lock);
859 int rtc_control(rtc_task_t *task, unsigned int cmd, unsigned long arg)
865 if (cmd != RTC_PIE_ON && cmd != RTC_PIE_OFF && cmd != RTC_IRQP_SET)
867 spin_lock_irqsave(&rtc_task_lock, flags);
868 if (rtc_callback != task) {
869 spin_unlock_irqrestore(&rtc_task_lock, flags);
872 spin_unlock_irqrestore(&rtc_task_lock, flags);
873 return rtc_do_ioctl(cmd, arg, 1);
879 * The various file operations we support.
882 static struct file_operations rtc_fops = {
883 .owner = THIS_MODULE,
891 .release = rtc_release,
892 .fasync = rtc_fasync,
895 static struct miscdevice rtc_dev = {
901 static struct file_operations rtc_proc_fops = {
902 .owner = THIS_MODULE,
903 .open = rtc_proc_open,
906 .release = single_release,
909 #if defined(RTC_IRQ) && !defined(__sparc__)
910 static irqreturn_t (*rtc_int_handler_ptr)(int irq, void *dev_id, struct pt_regs *regs);
913 static int __init rtc_init(void)
915 struct proc_dir_entry *ent;
916 #if defined(__alpha__) || defined(__mips__)
917 unsigned int year, ctrl;
921 struct linux_ebus *ebus;
922 struct linux_ebus_device *edev;
924 struct sparc_isa_bridge *isa_br;
925 struct sparc_isa_device *isa_dev;
930 for_each_ebus(ebus) {
931 for_each_ebusdev(edev, ebus) {
932 if(strcmp(edev->prom_name, "rtc") == 0) {
933 rtc_port = edev->resource[0].start;
934 rtc_irq = edev->irqs[0];
940 for_each_isa(isa_br) {
941 for_each_isadev(isa_dev, isa_br) {
942 if (strcmp(isa_dev->prom_name, "rtc") == 0) {
943 rtc_port = isa_dev->resource.start;
944 rtc_irq = isa_dev->irq;
950 printk(KERN_ERR "rtc_init: no PC rtc found\n");
954 if (rtc_irq == PCI_IRQ_NONE) {
960 * XXX Interrupt pin #7 in Espresso is shared between RTC and
961 * PCI Slot 2 INTA# (and some INTx# in Slot 1).
963 if (request_irq(rtc_irq, rtc_interrupt, SA_SHIRQ, "rtc", (void *)&rtc_port)) {
965 * Standard way for sparc to print irq's is to use
966 * __irq_itoa(). I think for EBus it's ok to use %d.
968 printk(KERN_ERR "rtc: cannot register IRQ %d\n", rtc_irq);
973 if (!request_region(RTC_PORT(0), RTC_IO_EXTENT, "rtc")) {
974 printk(KERN_ERR "rtc: I/O port %d is not free.\n", RTC_PORT (0));
979 if (is_hpet_enabled()) {
980 rtc_int_handler_ptr = hpet_rtc_interrupt;
982 rtc_int_handler_ptr = rtc_interrupt;
985 if(request_irq(RTC_IRQ, rtc_int_handler_ptr, SA_INTERRUPT, "rtc", NULL)) {
986 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
987 printk(KERN_ERR "rtc: IRQ %d is not free.\n", RTC_IRQ);
988 release_region(RTC_PORT(0), RTC_IO_EXTENT);
991 hpet_rtc_timer_init();
995 #endif /* __sparc__ vs. others */
997 if (misc_register(&rtc_dev)) {
999 free_irq(RTC_IRQ, NULL);
1001 release_region(RTC_PORT(0), RTC_IO_EXTENT);
1005 ent = create_proc_entry("driver/rtc", 0, NULL);
1008 free_irq(RTC_IRQ, NULL);
1010 release_region(RTC_PORT(0), RTC_IO_EXTENT);
1011 misc_deregister(&rtc_dev);
1014 ent->proc_fops = &rtc_proc_fops;
1016 #if defined(__alpha__) || defined(__mips__)
1019 /* Each operating system on an Alpha uses its own epoch.
1020 Let's try to guess which one we are using now. */
1022 if (rtc_is_updating() != 0)
1025 spin_lock_irq(&rtc_lock);
1026 year = CMOS_READ(RTC_YEAR);
1027 ctrl = CMOS_READ(RTC_CONTROL);
1028 spin_unlock_irq(&rtc_lock);
1030 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1031 BCD_TO_BIN(year); /* This should never happen... */
1035 guess = "SRM (post-2000)";
1036 } else if (year >= 20 && year < 48) {
1038 guess = "ARC console";
1039 } else if (year >= 48 && year < 72) {
1041 guess = "Digital UNIX";
1042 #if defined(__mips__)
1043 } else if (year >= 72 && year < 74) {
1045 guess = "Digital DECstation";
1047 } else if (year >= 70) {
1049 guess = "Standard PC (1900)";
1053 printk(KERN_INFO "rtc: %s epoch (%lu) detected\n", guess, epoch);
1056 if (rtc_has_irq == 0)
1059 init_timer(&rtc_irq_timer);
1060 rtc_irq_timer.function = rtc_dropped_irq;
1061 spin_lock_irq(&rtc_lock);
1063 if (!hpet_set_periodic_freq(rtc_freq)) {
1064 /* Initialize periodic freq. to CMOS reset default, which is 1024Hz */
1065 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT) & 0xF0) | 0x06), RTC_FREQ_SELECT);
1067 spin_unlock_irq(&rtc_lock);
1071 (void) init_sysctl();
1073 printk(KERN_INFO "Real Time Clock Driver v" RTC_VERSION "\n");
1078 static void __exit rtc_exit (void)
1081 remove_proc_entry ("driver/rtc", NULL);
1082 misc_deregister(&rtc_dev);
1086 free_irq (rtc_irq, &rtc_port);
1088 release_region (RTC_PORT (0), RTC_IO_EXTENT);
1091 free_irq (RTC_IRQ, NULL);
1093 #endif /* __sparc__ */
1096 module_init(rtc_init);
1097 module_exit(rtc_exit);
1101 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1102 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1103 * Since the interrupt handler doesn't get called, the IRQ status
1104 * byte doesn't get read, and the RTC stops generating interrupts.
1105 * A timer is set, and will call this function if/when that happens.
1106 * To get it out of this stalled state, we just read the status.
1107 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1108 * (You *really* shouldn't be trying to use a non-realtime system
1109 * for something that requires a steady > 1KHz signal anyways.)
1112 static void rtc_dropped_irq(unsigned long data)
1116 spin_lock_irq (&rtc_lock);
1118 if (hpet_rtc_dropped_irq()) {
1119 spin_unlock_irq(&rtc_lock);
1123 /* Just in case someone disabled the timer from behind our back... */
1124 if (rtc_status & RTC_TIMER_ON)
1125 mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100);
1127 rtc_irq_data += ((rtc_freq/HZ)<<8);
1128 rtc_irq_data &= ~0xff;
1129 rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0); /* restart */
1133 spin_unlock_irq(&rtc_lock);
1135 printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n", freq);
1137 /* Now we have new data */
1138 wake_up_interruptible(&rtc_wait);
1140 kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
1145 * Info exported via "/proc/driver/rtc".
1148 static int rtc_proc_show(struct seq_file *seq, void *v)
1150 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1151 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1153 unsigned char batt, ctrl;
1156 spin_lock_irq(&rtc_lock);
1157 batt = CMOS_READ(RTC_VALID) & RTC_VRT;
1158 ctrl = CMOS_READ(RTC_CONTROL);
1160 spin_unlock_irq(&rtc_lock);
1163 rtc_get_rtc_time(&tm);
1166 * There is no way to tell if the luser has the RTC set for local
1167 * time or for Universal Standard Time (GMT). Probably local though.
1170 "rtc_time\t: %02d:%02d:%02d\n"
1171 "rtc_date\t: %04d-%02d-%02d\n"
1172 "rtc_epoch\t: %04lu\n",
1173 tm.tm_hour, tm.tm_min, tm.tm_sec,
1174 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
1176 get_rtc_alm_time(&tm);
1179 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1180 * match any value for that particular field. Values that are
1181 * greater than a valid time, but less than 0xc0 shouldn't appear.
1183 seq_puts(seq, "alarm\t\t: ");
1184 if (tm.tm_hour <= 24)
1185 seq_printf(seq, "%02d:", tm.tm_hour);
1187 seq_puts(seq, "**:");
1189 if (tm.tm_min <= 59)
1190 seq_printf(seq, "%02d:", tm.tm_min);
1192 seq_puts(seq, "**:");
1194 if (tm.tm_sec <= 59)
1195 seq_printf(seq, "%02d\n", tm.tm_sec);
1197 seq_puts(seq, "**\n");
1200 "DST_enable\t: %s\n"
1203 "square_wave\t: %s\n"
1205 "update_IRQ\t: %s\n"
1206 "periodic_IRQ\t: %s\n"
1207 "periodic_freq\t: %ld\n"
1208 "batt_status\t: %s\n",
1217 batt ? "okay" : "dead");
1224 static int rtc_proc_open(struct inode *inode, struct file *file)
1226 return single_open(file, rtc_proc_show, NULL);
1229 void rtc_get_rtc_time(struct rtc_time *rtc_tm)
1231 unsigned long uip_watchdog = jiffies;
1233 #ifdef CONFIG_MACH_DECSTATION
1234 unsigned int real_year;
1238 * read RTC once any update in progress is done. The update
1239 * can take just over 2ms. We wait 20ms. There is no need to
1240 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1241 * If you need to know *exactly* when a second has started, enable
1242 * periodic update complete interrupts, (via ioctl) and then
1243 * immediately read /dev/rtc which will block until you get the IRQ.
1244 * Once the read clears, read the RTC time (again via ioctl). Easy.
1247 while (rtc_is_updating() != 0 && jiffies - uip_watchdog < 2*HZ/100) {
1253 * Only the values that we read from the RTC are set. We leave
1254 * tm_wday, tm_yday and tm_isdst untouched. Note that while the
1255 * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
1256 * only updated by the RTC when initially set to a non-zero value.
1258 spin_lock_irq(&rtc_lock);
1259 rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
1260 rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
1261 rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
1262 rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
1263 rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
1264 rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
1265 /* Only set from 2.6.16 onwards */
1266 rtc_tm->tm_wday = CMOS_READ(RTC_DAY_OF_WEEK);
1268 #ifdef CONFIG_MACH_DECSTATION
1269 real_year = CMOS_READ(RTC_DEC_YEAR);
1271 ctrl = CMOS_READ(RTC_CONTROL);
1272 spin_unlock_irq(&rtc_lock);
1274 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1276 BCD_TO_BIN(rtc_tm->tm_sec);
1277 BCD_TO_BIN(rtc_tm->tm_min);
1278 BCD_TO_BIN(rtc_tm->tm_hour);
1279 BCD_TO_BIN(rtc_tm->tm_mday);
1280 BCD_TO_BIN(rtc_tm->tm_mon);
1281 BCD_TO_BIN(rtc_tm->tm_year);
1282 BCD_TO_BIN(rtc_tm->tm_wday);
1285 #ifdef CONFIG_MACH_DECSTATION
1286 rtc_tm->tm_year += real_year - 72;
1290 * Account for differences between how the RTC uses the values
1291 * and how they are defined in a struct rtc_time;
1293 if ((rtc_tm->tm_year += (epoch - 1900)) <= 69)
1294 rtc_tm->tm_year += 100;
1299 static void get_rtc_alm_time(struct rtc_time *alm_tm)
1304 * Only the values that we read from the RTC are set. That
1305 * means only tm_hour, tm_min, and tm_sec.
1307 spin_lock_irq(&rtc_lock);
1308 alm_tm->tm_sec = CMOS_READ(RTC_SECONDS_ALARM);
1309 alm_tm->tm_min = CMOS_READ(RTC_MINUTES_ALARM);
1310 alm_tm->tm_hour = CMOS_READ(RTC_HOURS_ALARM);
1311 ctrl = CMOS_READ(RTC_CONTROL);
1312 spin_unlock_irq(&rtc_lock);
1314 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
1316 BCD_TO_BIN(alm_tm->tm_sec);
1317 BCD_TO_BIN(alm_tm->tm_min);
1318 BCD_TO_BIN(alm_tm->tm_hour);
1324 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1325 * Rumour has it that if you frob the interrupt enable/disable
1326 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1327 * ensure you actually start getting interrupts. Probably for
1328 * compatibility with older/broken chipset RTC implementations.
1329 * We also clear out any old irq data after an ioctl() that
1330 * meddles with the interrupt enable/disable bits.
1333 static void mask_rtc_irq_bit_locked(unsigned char bit)
1337 if (hpet_mask_rtc_irq_bit(bit))
1339 val = CMOS_READ(RTC_CONTROL);
1341 CMOS_WRITE(val, RTC_CONTROL);
1342 CMOS_READ(RTC_INTR_FLAGS);
1347 static void set_rtc_irq_bit_locked(unsigned char bit)
1351 if (hpet_set_rtc_irq_bit(bit))
1353 val = CMOS_READ(RTC_CONTROL);
1355 CMOS_WRITE(val, RTC_CONTROL);
1356 CMOS_READ(RTC_INTR_FLAGS);
1362 MODULE_AUTHOR("Paul Gortmaker");
1363 MODULE_LICENSE("GPL");
1364 MODULE_ALIAS_MISCDEV(RTC_MINOR);