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.12a Maciej W. Rozycki: Handle memory-mapped chips properly.
50 * 1.12ac Alan Cox: Allow read access to the day of week register
51 * 1.12b David John: Remove calls to the BKL.
54 #define RTC_VERSION "1.12b"
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
61 * kernel/time/ntp.c vs. this driver.)
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/sched/signal.h>
78 #include <linux/sysctl.h>
79 #include <linux/wait.h>
80 #include <linux/bcd.h>
81 #include <linux/delay.h>
82 #include <linux/uaccess.h>
83 #include <linux/ratelimit.h>
85 #include <asm/current.h>
93 #include <linux/of_device.h>
96 static unsigned long rtc_port
;
100 #ifdef CONFIG_HPET_EMULATE_RTC
105 static int rtc_has_irq
= 1;
108 #ifndef CONFIG_HPET_EMULATE_RTC
109 #define is_hpet_enabled() 0
110 #define hpet_set_alarm_time(hrs, min, sec) 0
111 #define hpet_set_periodic_freq(arg) 0
112 #define hpet_mask_rtc_irq_bit(arg) 0
113 #define hpet_set_rtc_irq_bit(arg) 0
114 #define hpet_rtc_timer_init() do { } while (0)
115 #define hpet_rtc_dropped_irq() 0
116 #define hpet_register_irq_handler(h) ({ 0; })
117 #define hpet_unregister_irq_handler(h) ({ 0; })
119 static irqreturn_t
hpet_rtc_interrupt(int irq
, void *dev_id
)
127 * We sponge a minor off of the misc major. No need slurping
128 * up another valuable major dev number for this. If you add
129 * an ioctl, make sure you don't conflict with SPARC's RTC
133 static struct fasync_struct
*rtc_async_queue
;
135 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait
);
138 static void rtc_dropped_irq(struct timer_list
*unused
);
140 static DEFINE_TIMER(rtc_irq_timer
, rtc_dropped_irq
);
143 static ssize_t
rtc_read(struct file
*file
, char __user
*buf
,
144 size_t count
, loff_t
*ppos
);
146 static long rtc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
147 static void rtc_get_rtc_time(struct rtc_time
*rtc_tm
);
150 static __poll_t
rtc_poll(struct file
*file
, poll_table
*wait
);
153 static void get_rtc_alm_time(struct rtc_time
*alm_tm
);
155 static void set_rtc_irq_bit_locked(unsigned char bit
);
156 static void mask_rtc_irq_bit_locked(unsigned char bit
);
158 static inline void set_rtc_irq_bit(unsigned char bit
)
160 spin_lock_irq(&rtc_lock
);
161 set_rtc_irq_bit_locked(bit
);
162 spin_unlock_irq(&rtc_lock
);
165 static void mask_rtc_irq_bit(unsigned char bit
)
167 spin_lock_irq(&rtc_lock
);
168 mask_rtc_irq_bit_locked(bit
);
169 spin_unlock_irq(&rtc_lock
);
173 #ifdef CONFIG_PROC_FS
174 static int rtc_proc_show(struct seq_file
*seq
, void *v
);
178 * Bits in rtc_status. (6 bits of room for future expansion)
181 #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
182 #define RTC_TIMER_ON 0x02 /* missed irq timer active */
185 * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
186 * protected by the spin lock rtc_lock. However, ioctl can still disable the
187 * timer in rtc_status and then with del_timer after the interrupt has read
188 * rtc_status but before mod_timer is called, which would then reenable the
189 * timer (but you would need to have an awful timing before you'd trip on it)
191 static unsigned long rtc_status
; /* bitmapped status byte. */
192 static unsigned long rtc_freq
; /* Current periodic IRQ rate */
193 static unsigned long rtc_irq_data
; /* our output to the world */
194 static unsigned long rtc_max_user_freq
= 64; /* > this, need CAP_SYS_RESOURCE */
197 * If this driver ever becomes modularised, it will be really nice
198 * to make the epoch retain its value across module reload...
201 static unsigned long epoch
= 1900; /* year corresponding to 0x00 */
203 static const unsigned char days_in_mo
[] =
204 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
207 * Returns true if a clock update is in progress
209 static inline unsigned char rtc_is_updating(void)
214 spin_lock_irqsave(&rtc_lock
, flags
);
215 uip
= (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
);
216 spin_unlock_irqrestore(&rtc_lock
, flags
);
222 * A very tiny interrupt handler. It runs with interrupts disabled,
223 * but there is possibility of conflicting with the set_rtc_mmss()
224 * call (the rtc irq and the timer irq can easily run at the same
225 * time in two different CPUs). So we need to serialize
226 * accesses to the chip with the rtc_lock spinlock that each
227 * architecture should implement in the timer code.
228 * (See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.)
231 static irqreturn_t
rtc_interrupt(int irq
, void *dev_id
)
234 * Can be an alarm interrupt, update complete interrupt,
235 * or a periodic interrupt. We store the status in the
236 * low byte and the number of interrupts received since
237 * the last read in the remainder of rtc_irq_data.
240 spin_lock(&rtc_lock
);
241 rtc_irq_data
+= 0x100;
242 rtc_irq_data
&= ~0xff;
243 if (is_hpet_enabled()) {
245 * In this case it is HPET RTC interrupt handler
246 * calling us, with the interrupt information
247 * passed as arg1, instead of irq.
249 rtc_irq_data
|= (unsigned long)irq
& 0xF0;
251 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0);
254 if (rtc_status
& RTC_TIMER_ON
)
255 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
257 spin_unlock(&rtc_lock
);
259 wake_up_interruptible(&rtc_wait
);
261 kill_fasync(&rtc_async_queue
, SIGIO
, POLL_IN
);
268 * sysctl-tuning infrastructure.
270 static struct ctl_table rtc_table
[] = {
272 .procname
= "max-user-freq",
273 .data
= &rtc_max_user_freq
,
274 .maxlen
= sizeof(int),
276 .proc_handler
= proc_dointvec
,
281 static struct ctl_table rtc_root
[] = {
290 static struct ctl_table dev_root
[] = {
299 static struct ctl_table_header
*sysctl_header
;
301 static int __init
init_sysctl(void)
303 sysctl_header
= register_sysctl_table(dev_root
);
307 static void __exit
cleanup_sysctl(void)
309 unregister_sysctl_table(sysctl_header
);
313 * Now all the various file operations that we export.
316 static ssize_t
rtc_read(struct file
*file
, char __user
*buf
,
317 size_t count
, loff_t
*ppos
)
322 DECLARE_WAITQUEUE(wait
, current
);
326 if (rtc_has_irq
== 0)
330 * Historically this function used to assume that sizeof(unsigned long)
331 * is the same in userspace and kernelspace. This lead to problems
332 * for configurations with multiple ABIs such a the MIPS o32 and 64
333 * ABIs supported on the same kernel. So now we support read of both
334 * 4 and 8 bytes and assume that's the sizeof(unsigned long) in the
337 if (count
!= sizeof(unsigned int) && count
!= sizeof(unsigned long))
340 add_wait_queue(&rtc_wait
, &wait
);
343 /* First make it right. Then make it fast. Putting this whole
344 * block within the parentheses of a while would be too
345 * confusing. And no, xchg() is not the answer. */
347 __set_current_state(TASK_INTERRUPTIBLE
);
349 spin_lock_irq(&rtc_lock
);
352 spin_unlock_irq(&rtc_lock
);
357 if (file
->f_flags
& O_NONBLOCK
) {
361 if (signal_pending(current
)) {
362 retval
= -ERESTARTSYS
;
368 if (count
== sizeof(unsigned int)) {
369 retval
= put_user(data
,
370 (unsigned int __user
*)buf
) ?: sizeof(int);
372 retval
= put_user(data
,
373 (unsigned long __user
*)buf
) ?: sizeof(long);
378 __set_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 /* can be called from isr via rtc_control() */
422 spin_lock_irqsave(&rtc_lock
, flags
);
423 mask_rtc_irq_bit_locked(RTC_PIE
);
424 if (rtc_status
& RTC_TIMER_ON
) {
425 rtc_status
&= ~RTC_TIMER_ON
;
426 del_timer(&rtc_irq_timer
);
428 spin_unlock_irqrestore(&rtc_lock
, flags
);
432 case RTC_PIE_ON
: /* Allow periodic ints */
434 /* can be called from isr via rtc_control() */
438 * We don't really want Joe User enabling more
439 * than 64Hz of interrupts on a multi-user machine.
441 if (!kernel
&& (rtc_freq
> rtc_max_user_freq
) &&
442 (!capable(CAP_SYS_RESOURCE
)))
445 spin_lock_irqsave(&rtc_lock
, flags
);
446 if (!(rtc_status
& RTC_TIMER_ON
)) {
447 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+
449 rtc_status
|= RTC_TIMER_ON
;
451 set_rtc_irq_bit_locked(RTC_PIE
);
452 spin_unlock_irqrestore(&rtc_lock
, flags
);
456 case RTC_UIE_OFF
: /* Mask ints from RTC updates. */
458 mask_rtc_irq_bit(RTC_UIE
);
461 case RTC_UIE_ON
: /* Allow ints for RTC updates. */
463 set_rtc_irq_bit(RTC_UIE
);
467 case RTC_ALM_READ
: /* Read the present alarm time */
470 * This returns a struct rtc_time. Reading >= 0xc0
471 * means "don't care" or "match all". Only the tm_hour,
472 * tm_min, and tm_sec values are filled in.
474 memset(&wtime
, 0, sizeof(struct rtc_time
));
475 get_rtc_alm_time(&wtime
);
478 case RTC_ALM_SET
: /* Store a time into the alarm */
481 * This expects a struct rtc_time. Writing 0xff means
482 * "don't care" or "match all". Only the tm_hour,
483 * tm_min and tm_sec are used.
485 unsigned char hrs
, min
, sec
;
486 struct rtc_time alm_tm
;
488 if (copy_from_user(&alm_tm
, (struct rtc_time __user
*)arg
,
489 sizeof(struct rtc_time
)))
492 hrs
= alm_tm
.tm_hour
;
496 spin_lock_irq(&rtc_lock
);
497 if (hpet_set_alarm_time(hrs
, min
, sec
)) {
499 * Fallthru and set alarm time in CMOS too,
500 * so that we will get proper value in RTC_ALM_READ
503 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
) ||
520 CMOS_WRITE(hrs
, RTC_HOURS_ALARM
);
521 CMOS_WRITE(min
, RTC_MINUTES_ALARM
);
522 CMOS_WRITE(sec
, RTC_SECONDS_ALARM
);
523 spin_unlock_irq(&rtc_lock
);
527 case RTC_RD_TIME
: /* Read the time/date from RTC */
529 memset(&wtime
, 0, sizeof(struct rtc_time
));
530 rtc_get_rtc_time(&wtime
);
533 case RTC_SET_TIME
: /* Set the RTC */
535 struct rtc_time rtc_tm
;
536 unsigned char mon
, day
, hrs
, min
, sec
, leap_yr
;
537 unsigned char save_control
, save_freq_select
;
539 #ifdef CONFIG_MACH_DECSTATION
540 unsigned int real_yrs
;
543 if (!capable(CAP_SYS_TIME
))
546 if (copy_from_user(&rtc_tm
, (struct rtc_time __user
*)arg
,
547 sizeof(struct rtc_time
)))
550 yrs
= rtc_tm
.tm_year
+ 1900;
551 mon
= rtc_tm
.tm_mon
+ 1; /* tm_mon starts at zero */
552 day
= rtc_tm
.tm_mday
;
553 hrs
= rtc_tm
.tm_hour
;
560 leap_yr
= ((!(yrs
% 4) && (yrs
% 100)) || !(yrs
% 400));
562 if ((mon
> 12) || (day
== 0))
565 if (day
> (days_in_mo
[mon
] + ((mon
== 2) && leap_yr
)))
568 if ((hrs
>= 24) || (min
>= 60) || (sec
>= 60))
572 if (yrs
> 255) /* They are unsigned */
575 spin_lock_irq(&rtc_lock
);
576 #ifdef CONFIG_MACH_DECSTATION
581 * We want to keep the year set to 73 until March
582 * for non-leap years, so that Feb, 29th is handled
585 if (!leap_yr
&& mon
< 3) {
590 /* These limits and adjustments are independent of
591 * whether the chip is in binary mode or not.
594 spin_unlock_irq(&rtc_lock
);
600 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
610 save_control
= CMOS_READ(RTC_CONTROL
);
611 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
612 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
613 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
615 #ifdef CONFIG_MACH_DECSTATION
616 CMOS_WRITE(real_yrs
, RTC_DEC_YEAR
);
618 CMOS_WRITE(yrs
, RTC_YEAR
);
619 CMOS_WRITE(mon
, RTC_MONTH
);
620 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
621 CMOS_WRITE(hrs
, RTC_HOURS
);
622 CMOS_WRITE(min
, RTC_MINUTES
);
623 CMOS_WRITE(sec
, RTC_SECONDS
);
625 CMOS_WRITE(save_control
, RTC_CONTROL
);
626 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
628 spin_unlock_irq(&rtc_lock
);
632 case RTC_IRQP_READ
: /* Read the periodic IRQ rate. */
634 return put_user(rtc_freq
, (unsigned long __user
*)arg
);
636 case RTC_IRQP_SET
: /* Set periodic IRQ rate. */
640 /* can be called from isr via rtc_control() */
644 * The max we can do is 8192Hz.
646 if ((arg
< 2) || (arg
> 8192))
649 * We don't really want Joe User generating more
650 * than 64Hz of interrupts on a multi-user machine.
652 if (!kernel
&& (arg
> rtc_max_user_freq
) &&
653 !capable(CAP_SYS_RESOURCE
))
656 while (arg
> (1<<tmp
))
660 * Check that the input was really a power of 2.
667 spin_lock_irqsave(&rtc_lock
, flags
);
668 if (hpet_set_periodic_freq(arg
)) {
669 spin_unlock_irqrestore(&rtc_lock
, flags
);
673 val
= CMOS_READ(RTC_FREQ_SELECT
) & 0xf0;
675 CMOS_WRITE(val
, RTC_FREQ_SELECT
);
676 spin_unlock_irqrestore(&rtc_lock
, flags
);
680 case RTC_EPOCH_READ
: /* Read the epoch. */
682 return put_user(epoch
, (unsigned long __user
*)arg
);
684 case RTC_EPOCH_SET
: /* Set the epoch. */
687 * There were no RTC clocks before 1900.
692 if (!capable(CAP_SYS_TIME
))
701 return copy_to_user((void __user
*)arg
,
702 &wtime
, sizeof wtime
) ? -EFAULT
: 0;
705 static long rtc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
708 ret
= rtc_do_ioctl(cmd
, arg
, 0);
713 * We enforce only one user at a time here with the open/close.
714 * Also clear the previous interrupt data on an open, and clean
715 * up things on a close.
717 static int rtc_open(struct inode
*inode
, struct file
*file
)
719 spin_lock_irq(&rtc_lock
);
721 if (rtc_status
& RTC_IS_OPEN
)
724 rtc_status
|= RTC_IS_OPEN
;
727 spin_unlock_irq(&rtc_lock
);
731 spin_unlock_irq(&rtc_lock
);
735 static int rtc_fasync(int fd
, struct file
*filp
, int on
)
737 return fasync_helper(fd
, filp
, on
, &rtc_async_queue
);
740 static int rtc_release(struct inode
*inode
, struct file
*file
)
745 if (rtc_has_irq
== 0)
749 * Turn off all interrupts once the device is no longer
750 * in use, and clear the data.
753 spin_lock_irq(&rtc_lock
);
754 if (!hpet_mask_rtc_irq_bit(RTC_PIE
| RTC_AIE
| RTC_UIE
)) {
755 tmp
= CMOS_READ(RTC_CONTROL
);
759 CMOS_WRITE(tmp
, RTC_CONTROL
);
760 CMOS_READ(RTC_INTR_FLAGS
);
762 if (rtc_status
& RTC_TIMER_ON
) {
763 rtc_status
&= ~RTC_TIMER_ON
;
764 del_timer(&rtc_irq_timer
);
766 spin_unlock_irq(&rtc_lock
);
771 spin_lock_irq(&rtc_lock
);
773 rtc_status
&= ~RTC_IS_OPEN
;
774 spin_unlock_irq(&rtc_lock
);
780 static __poll_t
rtc_poll(struct file
*file
, poll_table
*wait
)
784 if (rtc_has_irq
== 0)
787 poll_wait(file
, &rtc_wait
, wait
);
789 spin_lock_irq(&rtc_lock
);
791 spin_unlock_irq(&rtc_lock
);
794 return EPOLLIN
| EPOLLRDNORM
;
800 * The various file operations we support.
803 static const struct file_operations rtc_fops
= {
804 .owner
= THIS_MODULE
,
810 .unlocked_ioctl
= rtc_ioctl
,
812 .release
= rtc_release
,
813 .fasync
= rtc_fasync
,
816 static struct miscdevice rtc_dev
= {
822 static resource_size_t rtc_size
;
824 static struct resource
* __init
rtc_request_region(resource_size_t size
)
829 r
= request_region(RTC_PORT(0), size
, "rtc");
831 r
= request_mem_region(RTC_PORT(0), size
, "rtc");
839 static void rtc_release_region(void)
842 release_region(RTC_PORT(0), rtc_size
);
844 release_mem_region(RTC_PORT(0), rtc_size
);
847 static int __init
rtc_init(void)
849 #ifdef CONFIG_PROC_FS
850 struct proc_dir_entry
*ent
;
852 #if defined(__alpha__) || defined(__mips__)
853 unsigned int year
, ctrl
;
856 #ifdef CONFIG_SPARC32
857 struct device_node
*ebus_dp
;
858 struct platform_device
*op
;
862 irq_handler_t rtc_int_handler_ptr
;
866 #ifdef CONFIG_SPARC32
867 for_each_node_by_name(ebus_dp
, "ebus") {
868 struct device_node
*dp
;
869 for_each_child_of_node(ebus_dp
, dp
) {
870 if (of_node_name_eq(dp
, "rtc")) {
871 op
= of_find_device_by_node(dp
);
873 rtc_port
= op
->resource
[0].start
;
874 rtc_irq
= op
->irqs
[0];
881 printk(KERN_ERR
"rtc_init: no PC rtc found\n");
891 * XXX Interrupt pin #7 in Espresso is shared between RTC and
892 * PCI Slot 2 INTA# (and some INTx# in Slot 1).
894 if (request_irq(rtc_irq
, rtc_interrupt
, IRQF_SHARED
, "rtc",
895 (void *)&rtc_port
)) {
897 printk(KERN_ERR
"rtc: cannot register IRQ %d\n", rtc_irq
);
902 r
= rtc_request_region(RTC_IO_EXTENT
);
905 * If we've already requested a smaller range (for example, because
906 * PNPBIOS or ACPI told us how the device is configured), the request
907 * above might fail because it's too big.
909 * If so, request just the range we actually use.
912 r
= rtc_request_region(RTC_IO_EXTENT_USED
);
917 printk(KERN_ERR
"rtc: I/O resource %lx is not free.\n",
918 (long)(RTC_PORT(0)));
923 if (is_hpet_enabled()) {
926 rtc_int_handler_ptr
= hpet_rtc_interrupt
;
927 err
= hpet_register_irq_handler(rtc_interrupt
);
929 printk(KERN_WARNING
"hpet_register_irq_handler failed "
934 rtc_int_handler_ptr
= rtc_interrupt
;
937 if (request_irq(RTC_IRQ
, rtc_int_handler_ptr
, 0, "rtc", NULL
)) {
938 /* Yeah right, seeing as irq 8 doesn't even hit the bus. */
940 printk(KERN_ERR
"rtc: IRQ %d is not free.\n", RTC_IRQ
);
941 rtc_release_region();
945 hpet_rtc_timer_init();
949 #endif /* CONFIG_SPARC32 vs. others */
951 if (misc_register(&rtc_dev
)) {
953 free_irq(RTC_IRQ
, NULL
);
954 hpet_unregister_irq_handler(rtc_interrupt
);
957 rtc_release_region();
961 #ifdef CONFIG_PROC_FS
962 ent
= proc_create_single("driver/rtc", 0, NULL
, rtc_proc_show
);
964 printk(KERN_WARNING
"rtc: Failed to register with procfs.\n");
967 #if defined(__alpha__) || defined(__mips__)
970 /* Each operating system on an Alpha uses its own epoch.
971 Let's try to guess which one we are using now. */
973 if (rtc_is_updating() != 0)
976 spin_lock_irq(&rtc_lock
);
977 year
= CMOS_READ(RTC_YEAR
);
978 ctrl
= CMOS_READ(RTC_CONTROL
);
979 spin_unlock_irq(&rtc_lock
);
981 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
982 year
= bcd2bin(year
); /* This should never happen... */
986 guess
= "SRM (post-2000)";
987 } else if (year
>= 20 && year
< 48) {
989 guess
= "ARC console";
990 } else if (year
>= 48 && year
< 72) {
992 guess
= "Digital UNIX";
993 #if defined(__mips__)
994 } else if (year
>= 72 && year
< 74) {
996 guess
= "Digital DECstation";
998 } else if (year
>= 70) {
1000 guess
= "Standard PC (1900)";
1004 printk(KERN_INFO
"rtc: %s epoch (%lu) detected\n",
1008 if (rtc_has_irq
== 0)
1011 spin_lock_irq(&rtc_lock
);
1013 if (!hpet_set_periodic_freq(rtc_freq
)) {
1015 * Initialize periodic frequency to CMOS reset default,
1018 CMOS_WRITE(((CMOS_READ(RTC_FREQ_SELECT
) & 0xF0) | 0x06),
1021 spin_unlock_irq(&rtc_lock
);
1025 (void) init_sysctl();
1027 printk(KERN_INFO
"Real Time Clock Driver v" RTC_VERSION
"\n");
1032 static void __exit
rtc_exit(void)
1035 remove_proc_entry("driver/rtc", NULL
);
1036 misc_deregister(&rtc_dev
);
1038 #ifdef CONFIG_SPARC32
1040 free_irq(rtc_irq
, &rtc_port
);
1042 rtc_release_region();
1045 free_irq(RTC_IRQ
, NULL
);
1046 hpet_unregister_irq_handler(hpet_rtc_interrupt
);
1049 #endif /* CONFIG_SPARC32 */
1052 module_init(rtc_init
);
1053 module_exit(rtc_exit
);
1057 * At IRQ rates >= 4096Hz, an interrupt may get lost altogether.
1058 * (usually during an IDE disk interrupt, with IRQ unmasking off)
1059 * Since the interrupt handler doesn't get called, the IRQ status
1060 * byte doesn't get read, and the RTC stops generating interrupts.
1061 * A timer is set, and will call this function if/when that happens.
1062 * To get it out of this stalled state, we just read the status.
1063 * At least a jiffy of interrupts (rtc_freq/HZ) will have been lost.
1064 * (You *really* shouldn't be trying to use a non-realtime system
1065 * for something that requires a steady > 1KHz signal anyways.)
1068 static void rtc_dropped_irq(struct timer_list
*unused
)
1072 spin_lock_irq(&rtc_lock
);
1074 if (hpet_rtc_dropped_irq()) {
1075 spin_unlock_irq(&rtc_lock
);
1079 /* Just in case someone disabled the timer from behind our back... */
1080 if (rtc_status
& RTC_TIMER_ON
)
1081 mod_timer(&rtc_irq_timer
, jiffies
+ HZ
/rtc_freq
+ 2*HZ
/100);
1083 rtc_irq_data
+= ((rtc_freq
/HZ
)<<8);
1084 rtc_irq_data
&= ~0xff;
1085 rtc_irq_data
|= (CMOS_READ(RTC_INTR_FLAGS
) & 0xF0); /* restart */
1089 spin_unlock_irq(&rtc_lock
);
1091 printk_ratelimited(KERN_WARNING
"rtc: lost some interrupts at %ldHz.\n",
1094 /* Now we have new data */
1095 wake_up_interruptible(&rtc_wait
);
1097 kill_fasync(&rtc_async_queue
, SIGIO
, POLL_IN
);
1101 #ifdef CONFIG_PROC_FS
1103 * Info exported via "/proc/driver/rtc".
1106 static int rtc_proc_show(struct seq_file
*seq
, void *v
)
1108 #define YN(bit) ((ctrl & bit) ? "yes" : "no")
1109 #define NY(bit) ((ctrl & bit) ? "no" : "yes")
1111 unsigned char batt
, ctrl
;
1114 spin_lock_irq(&rtc_lock
);
1115 batt
= CMOS_READ(RTC_VALID
) & RTC_VRT
;
1116 ctrl
= CMOS_READ(RTC_CONTROL
);
1118 spin_unlock_irq(&rtc_lock
);
1121 rtc_get_rtc_time(&tm
);
1124 * There is no way to tell if the luser has the RTC set for local
1125 * time or for Universal Standard Time (GMT). Probably local though.
1128 "rtc_time\t: %ptRt\n"
1129 "rtc_date\t: %ptRd\n"
1130 "rtc_epoch\t: %04lu\n",
1133 get_rtc_alm_time(&tm
);
1136 * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
1137 * match any value for that particular field. Values that are
1138 * greater than a valid time, but less than 0xc0 shouldn't appear.
1140 seq_puts(seq
, "alarm\t\t: ");
1141 if (tm
.tm_hour
<= 24)
1142 seq_printf(seq
, "%02d:", tm
.tm_hour
);
1144 seq_puts(seq
, "**:");
1146 if (tm
.tm_min
<= 59)
1147 seq_printf(seq
, "%02d:", tm
.tm_min
);
1149 seq_puts(seq
, "**:");
1151 if (tm
.tm_sec
<= 59)
1152 seq_printf(seq
, "%02d\n", tm
.tm_sec
);
1154 seq_puts(seq
, "**\n");
1157 "DST_enable\t: %s\n"
1160 "square_wave\t: %s\n"
1162 "update_IRQ\t: %s\n"
1163 "periodic_IRQ\t: %s\n"
1164 "periodic_freq\t: %ld\n"
1165 "batt_status\t: %s\n",
1174 batt
? "okay" : "dead");
1182 static void rtc_get_rtc_time(struct rtc_time
*rtc_tm
)
1184 unsigned long uip_watchdog
= jiffies
, flags
;
1186 #ifdef CONFIG_MACH_DECSTATION
1187 unsigned int real_year
;
1191 * read RTC once any update in progress is done. The update
1192 * can take just over 2ms. We wait 20ms. There is no need to
1193 * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
1194 * If you need to know *exactly* when a second has started, enable
1195 * periodic update complete interrupts, (via ioctl) and then
1196 * immediately read /dev/rtc which will block until you get the IRQ.
1197 * Once the read clears, read the RTC time (again via ioctl). Easy.
1200 while (rtc_is_updating() != 0 &&
1201 time_before(jiffies
, uip_watchdog
+ 2*HZ
/100))
1205 * Only the values that we read from the RTC are set. We leave
1206 * tm_wday, tm_yday and tm_isdst untouched. Note that while the
1207 * RTC has RTC_DAY_OF_WEEK, we should usually ignore it, as it is
1208 * only updated by the RTC when initially set to a non-zero value.
1210 spin_lock_irqsave(&rtc_lock
, flags
);
1211 rtc_tm
->tm_sec
= CMOS_READ(RTC_SECONDS
);
1212 rtc_tm
->tm_min
= CMOS_READ(RTC_MINUTES
);
1213 rtc_tm
->tm_hour
= CMOS_READ(RTC_HOURS
);
1214 rtc_tm
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
1215 rtc_tm
->tm_mon
= CMOS_READ(RTC_MONTH
);
1216 rtc_tm
->tm_year
= CMOS_READ(RTC_YEAR
);
1217 /* Only set from 2.6.16 onwards */
1218 rtc_tm
->tm_wday
= CMOS_READ(RTC_DAY_OF_WEEK
);
1220 #ifdef CONFIG_MACH_DECSTATION
1221 real_year
= CMOS_READ(RTC_DEC_YEAR
);
1223 ctrl
= CMOS_READ(RTC_CONTROL
);
1224 spin_unlock_irqrestore(&rtc_lock
, flags
);
1226 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
1227 rtc_tm
->tm_sec
= bcd2bin(rtc_tm
->tm_sec
);
1228 rtc_tm
->tm_min
= bcd2bin(rtc_tm
->tm_min
);
1229 rtc_tm
->tm_hour
= bcd2bin(rtc_tm
->tm_hour
);
1230 rtc_tm
->tm_mday
= bcd2bin(rtc_tm
->tm_mday
);
1231 rtc_tm
->tm_mon
= bcd2bin(rtc_tm
->tm_mon
);
1232 rtc_tm
->tm_year
= bcd2bin(rtc_tm
->tm_year
);
1233 rtc_tm
->tm_wday
= bcd2bin(rtc_tm
->tm_wday
);
1236 #ifdef CONFIG_MACH_DECSTATION
1237 rtc_tm
->tm_year
+= real_year
- 72;
1241 * Account for differences between how the RTC uses the values
1242 * and how they are defined in a struct rtc_time;
1244 rtc_tm
->tm_year
+= epoch
- 1900;
1245 if (rtc_tm
->tm_year
<= 69)
1246 rtc_tm
->tm_year
+= 100;
1251 static void get_rtc_alm_time(struct rtc_time
*alm_tm
)
1256 * Only the values that we read from the RTC are set. That
1257 * means only tm_hour, tm_min, and tm_sec.
1259 spin_lock_irq(&rtc_lock
);
1260 alm_tm
->tm_sec
= CMOS_READ(RTC_SECONDS_ALARM
);
1261 alm_tm
->tm_min
= CMOS_READ(RTC_MINUTES_ALARM
);
1262 alm_tm
->tm_hour
= CMOS_READ(RTC_HOURS_ALARM
);
1263 ctrl
= CMOS_READ(RTC_CONTROL
);
1264 spin_unlock_irq(&rtc_lock
);
1266 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
1267 alm_tm
->tm_sec
= bcd2bin(alm_tm
->tm_sec
);
1268 alm_tm
->tm_min
= bcd2bin(alm_tm
->tm_min
);
1269 alm_tm
->tm_hour
= bcd2bin(alm_tm
->tm_hour
);
1275 * Used to disable/enable interrupts for any one of UIE, AIE, PIE.
1276 * Rumour has it that if you frob the interrupt enable/disable
1277 * bits in RTC_CONTROL, you should read RTC_INTR_FLAGS, to
1278 * ensure you actually start getting interrupts. Probably for
1279 * compatibility with older/broken chipset RTC implementations.
1280 * We also clear out any old irq data after an ioctl() that
1281 * meddles with the interrupt enable/disable bits.
1284 static void mask_rtc_irq_bit_locked(unsigned char bit
)
1288 if (hpet_mask_rtc_irq_bit(bit
))
1290 val
= CMOS_READ(RTC_CONTROL
);
1292 CMOS_WRITE(val
, RTC_CONTROL
);
1293 CMOS_READ(RTC_INTR_FLAGS
);
1298 static void set_rtc_irq_bit_locked(unsigned char bit
)
1302 if (hpet_set_rtc_irq_bit(bit
))
1304 val
= CMOS_READ(RTC_CONTROL
);
1306 CMOS_WRITE(val
, RTC_CONTROL
);
1307 CMOS_READ(RTC_INTR_FLAGS
);
1313 MODULE_AUTHOR("Paul Gortmaker");
1314 MODULE_LICENSE("GPL");
1315 MODULE_ALIAS_MISCDEV(RTC_MINOR
);